Implement constraint generation. This commit squashes a whole bunch of local commits that got lost due to a fight with git. Still to do: constraint simplification.