This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLec15Live.hs
222 lines (175 loc) · 5.62 KB
/
Lec15Live.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
module Lec15Live where
import Control.Applicative
import Data.Char
{- LECTURE 15 : YET MORE PARSING -}
newtype Parser a = -- a parser of things 'a'
MkParser (String -> -- a function from strings to
Maybe ( String -- the possibility of pairs of strings
, a)) -- and things
runParser :: Parser a -> String -> Maybe (String, a)
runParser (MkParser p) s = p s
{- The interface:
- char :: Parser Char
- fmap :: (a -> b) -> Parser a -> Parser b
- pure :: a -> Parser a "parse nothing"
- (<*>) :: Parser (a -> b) -> Parser a -> Parser b "sequence"
- empty :: Parser a
- (<|>) :: Parser a -> Parser a -> Parser a "try the first, then 2nd"
- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
-}
digit :: Parser Int
digit = do c <- char
if isDigit c then
pure (digitToInt c)
else
empty
-- char >>= \c -> if isDigit c then pure (digitToInt c) else empty
isChar :: Char -> Parser ()
isChar c = do c' <- char
if c == c' then pure () else empty
expr :: Parser Expr
expr = do d <- digit; pure (Digit d)
<|> parens (do e1 <- expr
isChar '+'
e2 <- expr
pure (Add e1 e2))
parens :: Parser a -> Parser a
parens p = do isChar '('
x <- p
isChar ')'
pure x
data JSON
= Null
| Bool Bool
| Number Int
| String String
| Array [JSON]
| Object [(String,JSON)]
deriving Show
isString :: String -> Parser ()
isString "" = pure ()
isString (x:xs) = do isChar x; isString xs
pNull :: Parser JSON
pNull = do isString "null"
pure Null
pBool :: Parser JSON
pBool = do isString "true"; pure (Bool True)
<|> do isString "false"; pure (Bool False)
digits :: Parser [Int]
digits = do d <- digit; ds <- digits; pure (d:ds)
<|> do d <- digit; pure [d]
ofDigits :: [Int] -> Int
ofDigits xs = go 0 xs
where go n [] = n
go n (d:ds) = go (n*10+d) ds
pNumber :: Parser JSON
pNumber = do ds <- digits
pure (Number (ofDigits ds))
stringChars :: Parser String
stringChars = do c <- char
case c of
'\\' -> do c <- char
cs <- stringChars
pure (c:cs)
'"' -> pure []
c -> do cs <- stringChars
pure (c:cs)
-- "syuyu\"fyufs"
pString :: Parser JSON
pString = do isChar '"'
cs <- stringChars
-- isChar '"'
pure (String cs)
-- [1,2]
pArray :: Parser a -> Parser [a]
pArray p = do isChar '['
xs <- sepBy (do isChar ','; whitespace) p
isChar ']'
pure xs
pObject :: Parser a -> Parser [(String,a)]
pObject p = do isChar '{'
fields <- sepBy (isChar ',') (do String name <- pString
isChar ':'
obj <- p
pure (name,obj))
isChar '}'
pure fields
pJSON :: Parser JSON
pJSON = pNull
<|> pBool
<|> pNumber
<|> pString
<|> fmap (\xs -> Array xs) (pArray pJSON)
<|> fmap (\xs -> Object xs) (pObject pJSON)
whitespace :: Parser ()
whitespace = do c <- char; if c == ' ' then whitespace else failure
<|> pure ()
sepBy :: Parser () -> Parser a -> Parser [a]
sepBy sep p = do x <- p
((do sep; xs <- sepBy sep p; pure (x:xs)) <|> pure [x])
<|> pure []
{-
do d1 <- digit
isChar '+'
d2 <- digit
pure (d1, d2)-}
{- 4+((1+2)+3) -}
data Expr
= Digit Int
| Add Expr Expr
deriving Show
char :: Parser Char
char = MkParser (\s -> case s of
(c:rest) -> Just (rest, c)
"" -> Nothing)
-- "alter", and Functors
alter :: (a -> b) -> Parser a -> Parser b
alter f (MkParser p) =
MkParser (\s -> case p s of
Nothing -> Nothing
Just (rest, a) -> Just (rest, f a))
instance Functor Parser where
--fmap :: (a -> b) -> Parser a -> Parser b
fmap = alter
andThen :: Parser a -> Parser b -> Parser (a,b)
andThen (MkParser p1) (MkParser p2) =
MkParser (\s -> case p1 s of
Nothing ->
Nothing
Just (rest, a) ->
case p2 rest of
Nothing ->
Nothing
Just (rest', b) ->
Just (rest', (a,b)))
nothing :: a -> Parser a
nothing a = MkParser (\s -> Just (s, a))
instance Applicative Parser where
-- pure :: a -> Parser a
pure = nothing
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
pf <*> pa = fmap (\(f,a) -> f a) (pf `andThen` pa)
orElse :: Parser a -> Parser a -> Parser a
orElse (MkParser p1) (MkParser p2) =
MkParser (\s -> case p1 s of
Nothing ->
p2 s
Just (rest,a) ->
Just (rest,a))
failure :: Parser a
failure = MkParser (\s -> Nothing)
instance Alternative Parser where
empty = failure
(<|>) = orElse
instance Monad Parser where
return = pure
(>>=) = sequ
sequ :: Parser a -> (a -> Parser b) -> Parser b
sequ (MkParser p1) f =
MkParser (\s -> case p1 s of
Nothing -> Nothing
Just (rest, a) ->
-- continue (f a) rest)
case f a of
MkParser p2 ->
p2 rest)