
Sigh, I seem to have done a reply to sender. Reposting to the list.
On 06/02/07, phiroc@free.fr
Hello,
I would like to create a Haskell function that generates a truth table, for all Boolean values, say, using the following "and" function :
and :: Bool -> Bool -> Bool and a b = a && b
A fairly old thread, but I had an interesting idea:
combos :: (Enum a, Enum b) => a -> b -> (a -> b -> c) -> [(a, b, c)] combos min1 min2 op = [(x, y, x `op` y) | x <- [min1..], y <- [min2..]]
Then: *Main> combos False (&&) [(False,False,False),(False,True,False),(True,False,False),(True,True,True)] In the case of Bool and a few others, you can use a slightly nicer one:
bCombos :: (Enum a, Bounded a, Enum b, Bounded b) => (a -> b -> c) -> [(a, b, c)] bCombos op = [(x, y, x `op` y) | x <- [minBound..maxBound], y <- [minBound..maxBound]]
And:
*Main> bCombos (&&)
[(False,False,False),(False,True,False),(True,False,False),(True,True,True)]
The secret of these is of course in the Enum and Bounded type classes, which
define, respectively,
* enumFrom and enumFromTo (which have syntactic sugar in [foo..] and [foo..bar]
respectively), and
* minBound and maxBound.
You can do the same with any instance of these classes.
--
Peter Berry