-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.hs
More file actions
334 lines (251 loc) · 7.31 KB
/
Parser.hs
File metadata and controls
334 lines (251 loc) · 7.31 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
module Parser (Polynomial(..), Parser (..), polynomial, parse, expand, simplify) where
import Control.Applicative
{--
Do not import anything besides Control.Applicative.
You are allowed to use anything available in Prelude and Control.Applicative, as well as any syntax features.
====
TASK
====
Write an instance of show for the Polynomial data type that produces *exactly* the
behaviour in the example.
In particular, only print brackets when necessary.
Note that we're allowing zero coefficients on monos to simplify the parser.
=======
EXAMPLE
=======
> Mono 1 1
x
> Mono 1 0
1
> (m,n,j,k) = (Mono 1 2, Mono 3 4, Mono 5 6, Mono 7 8)
(x^2,3x^4,5x^6,7x^8)
> Add (m) (Add (n) (j))
x^2 + 3x^4 + 5x^6
> Add (Add (m) (n)) (j)
x^2 + 3x^4 + 5x^6
> Mul (Add (m) (n)) (Add (j) (k))
(x^2 + 3x^4)(5x^6 + 7x^8)
> Mul m n
(x^2)(3x^4)
> Mul (Mul m n) j
(x^2)(3x^4)(5x^6)
> Mul m (Mul n j)
(x^2)(3x^4)(5x^6)
====
TASK
====
Write a parser
polynomial :: Parser Polynomial
for reading the string representation of a polynomial back into the Polynomial
data type.
Use polynomial to define an instance of read for the Polynomial data type.
=======
EXAMPLE
=======
> (parse polynomial) ")("
Nothing
> (parse polynomial) "2x^3"
> (parse polynomial) "(2x^3)"
Just (Mono 2 3,"")
> (parse polynomial) ("0x^2") -- It's okay to do this
Just (Mono 0 2,"")
> (parse polynomial) ("3x^24x^3")
Just (Mul (Mono 3 24) (Mono 1 3),"")
> (parse polynomial) "(2x^2+3)(x^3)"
Just (Mul (Add (Mono 2 2) (Mono 3 0)) (Mono 1 3),"")
> (parse polynomial) "(1+x^2)+x^3"
Just (Add (Add (Mono 1 0) (Mono 1 2)) (Mono 1 3),"")
> (parse polynomial) "1+(x^2+x^3)"
Just (Add (Mono 1 0) (Add (Mono 1 2) (Mono 1 3)),"")
> (parse polynomial) "(x)(x^2)+x^3"
Just (Add (Mul (Mono 1 1) (Mono 1 2)) (Mono 1 3),"")
> (parse polynomial) "(x)(x)(x)"
Just (Mul (Mono 1 1) (Mul (Mono 1 1) (Mono 1 1)),"")
> (parse polynomial) "(((x)))"
Just (Mono 1 1,"")
This task is worth 10 POINTS.
The part you need to implement starts on the line 242.
--}
-- start: DO NOT MODIFY --
type Deg = Integer -- precondition: always nonnegative.
type Coeff = Integer -- precondition: always nonnegative.
data Polynomial = Mono Coeff Deg | Add Polynomial Polynomial | Mul Polynomial Polynomial deriving (Eq)
-- expand and simplify from A2
--
expand :: Polynomial -> Polynomial
expand (Mono c d) = Mono c d
expand (Add f g) = Add (expand f) (expand g)
expand (Mul (Mono c0 d0) (Mono c1 d1)) = Mono (c0*c1) (d0+d1)
expand (Mul (Add f g) h) = Add (expand $ Mul f h) (expand $ Mul g h) -- right dist
expand (Mul f (Add g h)) = Add (expand $ Mul f g) (expand $ Mul f h) -- left dist
expand (Mul f g) = expand $ Mul (expand f) (expand g)
-- simplified polynomial is returned in descending degree
simplify :: Polynomial -> Polynomial
simplify (Mono c d) = Mono c d
simplify (Add g h) = merge' (simplify g) (simplify h)
simplify f = simplify $ expand f
-- Precondition: input is simplified
merge' :: Polynomial -> Polynomial -> Polynomial
merge' (Mono a b) (Mono c d)
| b > d = Add (Mono a b) (Mono c d)
| d > b = Add (Mono c d) (Mono a b)
| otherwise = Mono (a+c) d
merge' (Mono lcf df) g
| df > dg = Add (Mono lcf df) g
| dg > df = Add (Mono lcg dg) $ merge' (Mono lcf df) gt
| otherwise = Add (Mono (lcf+lcg) df) gt
where
Add (Mono lcg dg) gt = g
merge' f (Mono c d) = merge' (Mono c d) f
merge' f g
| df > dg = Add (Mono lcf df) (merge' ft g)
| dg > df = Add (Mono lcg dg) (merge' gt f)
| otherwise = Add (Mono (lcf+lcg) df) (merge' ft gt)
where
Add (Mono lcf df) ft = f
Add (Mono lcg dg) gt = g
-- Parser type
newtype Parser a = P (String -> Maybe (a, String))
instance Functor Parser where
fmap :: (a -> b) -> Parser a -> Parser b
fmap g pa = do
a <- pa
return $ g a
instance Applicative Parser where
pure :: a -> Parser a
pure a = P (\cs -> Just (a,cs))
(<*>) :: Parser (a -> b) -> Parser a -> Parser b
pg <*> pa = do
g <- pg
g <$> pa
instance Monad Parser where
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = P $ \cs ->
case parse p cs of
Nothing -> Nothing
Just (a, str') -> parse (f a) str'
instance Alternative Parser where
empty :: Parser a
empty = P $ \str -> Nothing
(<|>) :: Parser a -> Parser a -> Parser a
p <|> q = P $ \cs ->
case parse p cs of
Nothing -> parse q cs
mx -> mx
-- aux function for removing decorator
parse :: Parser a -> String -> Maybe (a, String)
parse (P p) cs = p cs
-- parase one character
item :: Parser Char
item = P $ foo
where
foo (c:cs) = Just $ (c, cs)
foo _ = Nothing
-- parse a char c when P c.
sat :: (Char -> Bool) -> Parser Char
sat p = do
x <- item
if p x then return x else empty
-- parse a digit
digit :: Parser Char
digit = sat (\x -> elem x ['0'..'9'])
-- parse the character x
char :: Char -> Parser Char
char x = sat (== x)
-- parse the string xs
string :: String -> Parser String
string [] = return []
string (x:xs) = (\x xs -> x:xs) <$> (char x) <*> (string xs)
-- parse a natural number
nat :: Parser Integer
nat = read <$> (some digit)
-- throw away space
space :: Parser ()
space = (\x -> ()) <$> (many $ char ' ')
-- ignore surrounding whitespace
token :: Parser a -> Parser a
token pa = do
space
a <- pa
space
return a
-- parse a symbol, ignoring whitespace
symbol :: String -> Parser String
symbol xs = token $ string xs
-- end DO NOT MODIFY --
-- Your code goes below
-- Your code goes below
-- Your code goes below
instance Show Polynomial where
show = showPoly
showPoly :: Polynomial -> String
showPoly (Mono 0 _) = "0"
showPoly (Mono c d)
| c == 1 && d == 1 = "x"
| c == 1 && d == 0 = "1"
| d == 0 = show c
| c == 1 = "x^" ++ show d
| d == 1 = show c ++ "x"
| otherwise = show c ++ "x^" ++ show d
showPoly (Add p q) = showAdd p ++ " + " ++ showAdd q
showPoly (Mul p q) = showMul p ++ showMul q
showAdd :: Polynomial -> String
showAdd p@(Add _ _) = showPoly p
showAdd p@(Mul _ _) = showPoly p
showAdd p = showPoly p
showMul :: Polynomial -> String
showMul p@(Mul _ _) = showPoly p
showMul p@(Add _ _) = "(" ++ showPoly p ++ ")"
showMul p = "(" ++ showPoly p ++ ")"
polynomial :: Parser Polynomial
polynomial = addPolynom
addPolynom :: Parser Polynomial
addPolynom = do
f <- factors
(do
_ <- symbol "+"
p <- addPolynom
return $ Add f p) <|> return f
factors :: Parser Polynomial
factors = do
f <- factor
(do
fs <- factors
return $ Mul f fs) <|> return f
factor :: Parser Polynomial
factor = (parens polynomial) <|> mono
mono :: Parser Polynomial
mono = tryConstXExp <|> tryXExp <|> tryConstX <|> tryX <|> tryConst
tryConstXExp :: Parser Polynomial
tryConstXExp = do
c <- nat
_ <- symbol "x"
_ <- symbol "^"
d <- nat
return $ Mono c d
tryXExp :: Parser Polynomial
tryXExp = do
_ <- symbol "x"
_ <- symbol "^"
d <- nat
return $ Mono 1 d
tryConstX :: Parser Polynomial
tryConstX = do
c <- nat
_ <- symbol "x"
return $ Mono c 1
tryX :: Parser Polynomial
tryX = do
_ <- symbol "x"
return $ Mono 1 1
tryConst :: Parser Polynomial
tryConst = do
c <- nat
return $ Mono c 0
parens :: Parser a -> Parser a
parens p = do
_ <- symbol "("
n <- p
_ <- symbol ")"
return n
f = (:).(:)