Advertisement
VladNitu

TautologyCheckerHaskell

Feb 19th, 2024
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List (nub) -- The function 'nub' removes duplicates from a list
  2.  
  3. data Prop = Const Bool
  4.           | Var Char
  5.           | Not Prop
  6.           | And Prop Prop
  7.           | Imply Prop Prop
  8.           | Or Prop Prop
  9.           | Equiv Prop Prop
  10.   deriving (Show)
  11.          
  12. type Assoc k v = [(k,v)]
  13.          
  14. find :: (Eq k) => k -> Assoc k v -> v
  15. find k [] = error "Key not found!"
  16. find k ((k',x):xs)
  17.  | k == k'   = x
  18.   | otherwise = find k xs
  19.  
  20. type Subst = Assoc Char Bool
  21.  
  22. eval :: Subst -> Prop -> Bool
  23. eval _ (Const b)   = b
  24. eval s (Var x)     = find x s
  25. eval s (Not p)     = not (eval s p)
  26. eval s (And p q)   = eval s p && eval s q
  27. eval s (Imply p q) = eval s p <= eval s q
  28. eval s (Or p q) = eval s p || eval s q
  29. eval s (Equiv p q) = eval s p == eval s q
  30.  
  31. vars :: Prop -> [Char]
  32. vars (Const _)   = []
  33. vars (Var x)     = [x]
  34. vars (Not p)     = vars p
  35. vars (And p q)   = vars p ++ vars q
  36. vars (Imply p q) = vars p ++ vars q
  37. vars (Or p q) = vars p ++ vars q
  38. vars (Equiv p q) = vars p ++ vars q
  39.  
  40.  
  41. bools :: Int -> [[Bool]]
  42. bools 0       = [[]]
  43. bools n | n>0 = map (False:) bss ++ map (True:) bss
  44.   where bss = bools (n-1)
  45.  
  46. substs :: Prop -> [Subst]
  47. substs p = map (zip vs) (bools (length vs))
  48.   where vs = nub (vars p)
  49.  
  50. isTaut :: Prop -> Bool
  51. isTaut p = and [eval s p | s <- substs p]
  52.  
  53.  
  54. isSat :: Prop -> Maybe Subst
  55. isSat p = if maybe then Just (head [s | s <- substs p, eval s p]) else Nothing
  56.   where maybe =  or [eval s p | s <- substs p]
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement