-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop.hs
69 lines (56 loc) · 1.82 KB
/
prop.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Data.List
{-
Learnt during / taken from the University of Edinburgh Lecture Series Intro to Computation and FP, INF1A
Original Author: Prof Wadler. P
Any weird changes / removals / mistakes, those are mine! Sorry!
Still trying to remember how to use lookup and envs, may need to revisit the original lecture...
-}
type Name = String
data Prop = Var Name
| F
| T
| Not Prop
| Prop :|: Prop
| Prop :&: Prop
deriving (Eq, Show)
type Names = [Name]
type Env = [(Name, Bool)]
showProp :: Prop -> String
showProp (Var x) = x
showProp (F) = "F"
showProp (T) = "T"
showProp (Not p) = par ("~" ++ showProp p)
showProp (p :|: q) = par (showProp p ++ "|" ++ showProp q)
showProp (p :&: q) = par (showProp p ++ "&" ++ showProp q)
par :: String -> String
par s = "(" ++ s ++ ")"
names :: Prop -> Names
names (Var x) = [x]
names (F) = []
names (T) = []
names (Not p) = names p
names (p :|: q) = nub (names p ++ names q)
names (p :&: q) = nub (names p ++ names q)
evalProp :: Env -> Prop -> Bool
evalProp e (Var x) = lookUp e x
evalProp e (F) = False
evalProp e (T) = True
evalProp e (Not p) = not (evalProp e p)
evalProp e (p :|: q) = evalProp e p || evalProp e q
evalProp e (p :&: q) = evalProp e p && evalProp e q
envs :: Names -> [Env]
envs [] = [[]]
envs (x:xs) = [ (x,b):e | b <- bs, e <- envs xs ]
where
bs = [False, True]
lookUp :: Eq a => [(a,b)] -> a -> b
lookUp xys x = the [y | (x',y) <- xys, x == x']
where
the [x] = x
main :: IO ()
main = do
let p1 = (Var "a" :&: Var "b")
print (showProp p1)
let p2 = (Var "a" :&: Var "b") :|: (Not (Var "a") :&: Not (Var "b"))
print (showProp p2)
print (evalProp [("a", False), ("b", False)] p2)