Function to replace variables in a Horn clause