forked from AlexanderEkdahl/EDAN40
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class2.hs
43 lines (32 loc) · 1.27 KB
/
class2.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
import Data.Maybe
data Proposition = Var Name
| Proposition :&: Proposition
| Proposition :|: Proposition
| Not Proposition
deriving ( Eq, Show )
type Name = String
-- uniques?
vars :: Proposition -> [Name]
vars (Var x) = [x]
vars (a :&: b) = vars a ++ vars b
vars (a :|: b) = vars a ++ vars b
vars (Not a) = vars a
-- truthValue (Var "q" :&: Var "p") [("q", True), ("p", True)]
truthValue :: Proposition -> [(Name, Bool)] -> Bool
truthValue (Var x) v = fromJust (lookup x v)
truthValue (a :&: b) v = truthValue a v && truthValue b v
truthValue (a :|: b) v = truthValue a v || truthValue b v
truthValue (Not a) v = not (truthValue a v)
-- tautology (Var "q" :|: Not (Var "q"))
tautology :: Proposition -> Bool
tautology p = and (map (truthValue p) (allVals (vars p)))
allVals :: [Name] -> [[(Name,Bool)]]
allVals [] = [[]]
allVals (x:xs) = [ (x,b):val | val <- allVals xs, b <- [False,True] ]
-- nice print function :)
data File = File Name | Dir Name [File] deriving ( Eq, Show )
search :: File -> Name -> Bool
search (Dir _ ns) x = any id (map (search x) ns)
search (File n) x = True
-- Dir "~" [File "a", Dir "b" [File "x", File "y"], File "c"]
-- search "x", (Dir "~" [File "a", Dir "b" [File "x", File "y"], File "c"])