-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek08Live2023.hs
298 lines (233 loc) · 6.47 KB
/
Week08Live2023.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{-# LANGUAGE InstanceSigs #-}
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Week08Live2023 where
import Data.Char (toUpper, isDigit, digitToInt, isSpace, isAlpha)
import Data.Foldable (for_)
--- REMINDER: Week 09 (Resit) class test
-- -- Wednesday 15th Nov -> Thursday 16th Nov (12:00noon)
-- Parsing
-- "asd,klj","sdjhds",90,fdfhg,kjh
-- { "a": [1,2], "b": {"c": [ 1,2,3 sdf], "d": null }
type Parser_v1 a = String -> Maybe a
boolParser_v1 :: Parser_v1 Bool
boolParser_v1 "true" = Just True
boolParser_v1 "false" = Just False
boolParser_v1 _ = Nothing
-- truefalse => (True, False)
-- input -> splitOn ' ' -> (boolParser, boolParser)
newtype Parser a = MkParser (String -> Maybe (a, String))
runParser :: Parser a -> String -> Maybe (a, String)
runParser (MkParser p) s = p s
boolParser :: Parser Bool
boolParser = MkParser (\input ->
case input of
't':'r':'u':'e':rest -> Just (True, rest)
'f':'a':'l':'s':'e':rest -> Just (False, rest)
_ -> Nothing)
andThen :: Parser a -> Parser b -> Parser (a,b)
andThen (MkParser p1) (MkParser p2) =
MkParser (\input ->
case p1 input of
Just (a, rest) ->
case p2 rest of
Just (b, final) -> Just ((a,b), final)
Nothing -> Nothing
Nothing -> Nothing)
char :: Parser Char
char = MkParser (\input -> case input of
c:cs -> Just (c, cs)
[] -> Nothing)
bind :: Parser a -> (a -> Parser b) -> Parser b
bind (MkParser p) k =
MkParser (\input ->
case p input of
Just (a, rest) ->
case k a of
MkParser p2 ->
case p2 rest of
Just (b, final) -> Just (b, final)
Nothing -> Nothing
Nothing -> Nothing)
nothing :: a -> Parser a
nothing a = MkParser (\input -> Just (a, input))
andThen2 :: Parser a -> Parser b -> Parser (a,b)
andThen2 p1 p2 =
bind p1 (\a -> bind p2 (\b -> nothing (a,b)))
-- isChar :: Char -> Parser ()
-- isChar c = bind char (\c' -> if c == c' then nothing () else failure)
failure :: Parser a
failure = MkParser (\input -> Nothing)
instance Monad Parser where
(>>=) = bind
instance Applicative Parser where
pure = nothing
f <*> a = do x <- f; y <- a; return (x y)
instance Functor Parser where
fmap :: (a -> b) -> Parser a -> Parser b
fmap f (MkParser p) =
MkParser (\input -> case p input of
Just (a, rest) -> Just (f a, rest)
Nothing -> Nothing)
pairOfBools :: Parser (Bool, Bool)
pairOfBools =
do b1 <- boolParser
isChar ','
b2 <- boolParser
return (b1, b2)
trueP :: Parser Bool
trueP =
do isString "true"
return True
falseP :: Parser Bool
falseP =
do isString "false"
return False
orElse :: Parser a -> Parser a -> Parser a
orElse (MkParser p1) (MkParser p2) =
MkParser (\input ->
case p1 input of
Nothing -> p2 input
Just (a, rest) -> Just (a,rest))
boolParser_v2 :: Parser Bool
boolParser_v2 = trueP `orElse` falseP
{- <exp> := ...
| true
| false
-}
{- Parser a <--- represents a parser of things of type 'a'
return :: a -> Parser a <-- parse nothing and return 'a'
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
orElse :: Parser a -> Parser a -> Parser a
failure :: Parser a
char :: Parser Char
-}
isChar :: Char -> Parser ()
isChar c =
do c' <- char
if c == c' then return () else failure
isString :: String -> Parser ()
isString str = for_ str (\c -> isChar c)
{- PLAN: write a parser for an expression language using the combinators. -}
-- 1. Fix a grammar
{- <expr> ::= <mulexpr> + <expr>
| <mulexpr>
<mulexpr> ::= <baseexpr> * <mulexpr>
| <baseexpr>
<baseexpr> ::= <number>
| <variable>
| ( <expr> )
<number> ::= [0-9]+
(one or more of characters in 0 .. 9)
<variable> ::= [A-Za-z]+
(one or more of alphabetic characters)
-}
-- 2. Design an Abstract Syntax Tree type
data Expr
= Add Expr Expr
| Mul Expr Expr
| Variable String
| FunCall String [Expr]
| Number Integer
deriving (Show, Eq)
-- 3. Write a parser
whitespace :: Parser ()
whitespace =
do zeroOrMore (isChar ' ')
return ()
expr :: Parser Expr
expr =
do e1 <- mulexpr
whitespace
isChar '+'
whitespace
e2 <- expr
return (Add e1 e2)
`orElse`
do e <- mulexpr
return e
mulexpr :: Parser Expr
mulexpr =
do e1 <- baseexpr
whitespace
isChar '*'
whitespace
e2 <- mulexpr
return (Mul e1 e2)
`orElse`
do e <- baseexpr
return e
baseexpr :: Parser Expr
baseexpr =
do n <- number
return (Number n)
`orElse`
do f <- variable
isChar '('
args <- sepBy (isChar ',') expr
isChar ')'
return (FunCall f args)
`orElse`
do v <- variable
return (Variable v)
`orElse`
do isChar '('
whitespace
e <- expr
whitespace
isChar ')'
return e
wholeExpr :: Parser Expr
wholeExpr =
do whitespace
e <- expr
whitespace
return e
-- Plan for number:
-- 1. write a parser for digits 0-9
-- 2. write a parser for sequences of digits
-- 3. turn lists of digits into numbers
digit :: Parser Integer
digit =
do c <- char
case c of
'0' -> return 0
'1' -> return 1
'2' -> return 2
'3' -> return 3
'4' -> return 4
'5' -> return 5
'6' -> return 6
'7' -> return 7
'8' -> return 8
'9' -> return 9
_ -> failure
zeroOrMore :: Parser a -> Parser [a]
zeroOrMore p =
do d <- p
ds <- zeroOrMore p
return (d:ds)
`orElse`
return []
sepBy :: Parser () -> Parser a -> Parser [a]
sepBy sep p =
do x <- p
xs <- zeroOrMore (do sep; p)
return (x:xs)
`orElse`
return []
oneOrMore :: Parser a -> Parser [a]
oneOrMore p = do d <- p
ds <- zeroOrMore p
return (d:ds)
number :: Parser Integer
number =
do ds <- oneOrMore digit
return (fromDigits ds)
fromDigits :: [Integer] -> Integer
fromDigits = foldl (\n d -> n*10 + d) 0
alphabetic :: Parser Char
alphabetic =
do c <- char
if isAlpha c then return c else failure
variable :: Parser String
variable = oneOrMore alphabetic