Today I wanted to test a few boolean expressions, and ended up with some quick truth table generation hackery in Haskell which I thought I’d note down for next time. I’m sure there are many better ways of doing this, but this way was mine.
Here’s a list of all booleans in GHCi:
We can use the default applicative instance for lists to run all combinations of bools through a function. If we use a tuple constructor, we’ll get truth table inputs (shown below for 2 and 3 argument expressions).
Let’s check a basic expression p, and its equivalent using De Morgan’s laws. Writing x <$> bools <*> bools gets repetitive, so we’ll start off by defining tt2 to get truth-tableish output values for a 2 input function Bool -> Bool -> Bool.
We can then zipWith to get truth table inputs with the corresponding truth table output (using a bit of uncurry trickery):
Or for more arguments:
We can write something to format these nicely, but this was enough for me to get the information I wanted about a few expressions.