-- Tautology example from chapter 8 of Programming in Haskell, -- Graham Hutton, Cambridge University Press, 2016. -- This example develops a language of propositions -- and check which ones are a tautology. -- It builds the trurh tables and evaluated each propostion. -- If in all cases the proposition evaluates to truth, -- then it a tautology -- Propositions data Prop = Const Bool -- values | Var Char -- varibles | Not Prop -- negation | And Prop Prop -- conjuntion | Imply Prop Prop -- implication -- operations in this propositions can be defined using truth tables -- A and (not A) p1 :: Prop p1 = And (Var 'A') (Not (Var 'A')) -- (A and B) => A is a Tautology p2 :: Prop p2 = Imply (And (Var 'A') (Var 'B')) (Var 'A') -- A => (A and B) p3 :: Prop p3 = Imply (Var 'A') (And (Var 'A') (Var 'B')) -- (A and (A => B)) => B is a Tautology p4 :: Prop p4 = Imply (And (Var 'A') (Imply (Var 'A') (Var 'B'))) (Var 'B') -- Substitutions -- substitutions will help us to associate variables to their values -- example [('A', False), ('B', True)] type Subst = Assoc Char Bool type Assoc k v = [(k,v)] -- example -- find 'A' [('A', False), ('B', True)] find :: Eq k => k -> Assoc k v -> v find k t = head [v | (k',v) <- t, k == k'] -- Tautology checker -- this function evaluates a proposition -- in the context of a substitution -- eval [('A', False), ('B', True)] p2 -- eval [('A', True), ('B', False)] p2 -- eval :: Subst -> Prop -> Bool -- eval _ (Const b) = -- eval s (Var x) = -- eval s (Not p) = -- eval s (And p q) = -- eval s (Imply p q) = -- this funtion returns all variables in a proposition -- Example: vars p2 returns "ABA" -- vars :: Prop -> [Char] -- vars (Const _) = -- vars (Var x) = -- vars (Not p) = -- vars (And p q) = -- vars (Imply p q) = -- this function retuns all Boolean cases combination of a number of variables -- bools 2 -- [[False,False],[False,True],[True,False],[True,True]] bools :: Int -> [[Bool]] bools 0 = [[]] bools n = map (False:) bss ++ map (True:) bss where bss = bools (n-1) -- remove duplicates rmdups :: Eq a => [a] -> [a] rmdups [] = [] rmdups (x:xs) = x : filter (/= x) (rmdups xs) -- associate variables in a proposition to a truth table -- substs p2 (for two variables A and B) is: -- [[('A',False), ('B',False)], -- [('A',False), ('B',True)], -- [('A',True), ('B',False)], -- [('A',True), ('B',True)]] -- substs :: Prop -> [Subst] -- substs p = map (zip vs) ... -- where vs = ... -- this function check if a proposition is a tautology -- by using eval s p in each substs p -- isTaut :: Prop -> Bool -- isTaut p =