-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkSheet5.lhs
86 lines (66 loc) · 2.57 KB
/
workSheet5.lhs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
pred ::= "t" | "f" | pred "&&" pred | "!" pred
> import Text.Yoda
> import Data.Char
> import Data.Bool
> data Pred = T
> | F
> | Not Pred
> | And Pred Pred
> deriving Show
> evalPred :: Pred -> Bool
> evalPred (T) = True
> evalPred (F) = False
> evalPred (Not p) | (evalPred p == True) = False
> | otherwise = True
> evalPred (And p1 p2) | evalPred p1 = evalPred p2
> | otherwise = False
> whitespace :: Parser ()
> whitespace = skip (many (oneOf [' ', '\t', '\n']))
> token :: String -> Parser String
> token t = string t <* whitespace
> predParser :: Parser Pred
> predParser = T <$ token "t"
> <|> F <$ token "f"
> <|> Not <$ token "!" <*> predParser
> <|> And <$> predParser <* token "&&" <*> predParser
pred ::= "t" pred' | "f" pred' | "!" pred pred'
pred' ::= "&&" pred pred' | epsilon
> data Pred2 = T2 Pred2'
> | F2 Pred2'
> | Not2 Pred2 Pred2'
> deriving Show
> data Pred2' = And2 Pred2 Pred2'
> | Epsilon
> deriving Show
> pred2Parser :: Parser Pred2
> pred2Parser = T2 <$ token "t" <*> pred2'Parser
> <|> F2 <$ token "f" <*> pred2'Parser
> <|> Not2 <$ token "!" <*> pred2Parser <*> pred2'Parser
> pred2'Parser :: Parser Pred2'
> pred2'Parser = And2 <$ token "&&" <*> pred2Parser <*> pred2'Parser
> <|> pure Epsilon
pred ::= "t" | "f" | pred "&&" pred | "!" pred
> predChainParser :: Parser Pred
> predChainParser = chainr1 term (And <$ token "&&")
> where term = prefix (Not <$ token "!") atom
> atom = T <$ token "t"
> <|> F <$ token "f"
graph ::= graph -> graph | graph + graph | "[" {number} "]"
graph ::= graph (-> | +) graph | "[" {number} "]"
> number :: Parser Int
> number = read <$> some(oneOf(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']))
> data Graph = Empty | Vertex Int | Overlay Graph Graph | Connect Graph Graph
> deriving Show
> graphParser :: Parser Graph
> graphParser = chainl1 term (Overlay <$ token "+" <|> Connect <$ token "->")
> where term = token "[" *> Vertex <$> number <|> pure Empty <* token "]"
graph ::= graph "+" graph | connects
connects ::= connects "->" connects | atom
atom ::= "[" {number} "]"
> newGraphParser :: Parser Graph
> newGraphParser = chainl1 term (Overlay <$ token "+")
> where term = chainl1 atom (Connect <$ token "->")
> atom = token "[" *> Vertex <$> number <|> pure Empty <* token "]"
parse expr1 "1 + 3 * 4"
(<$>) :: (a -> b) -> Parser a -> Parser b
f <$> pa = pure f <*> pa