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
/
Lec13Live.hs
120 lines (96 loc) · 2.97 KB
/
Lec13Live.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
module Lec13Live where
{- LECTURE 13 : PARSER COMBINATORS -}
{- What is a parser?
A parser of things is---
a function
from strings
to lists
of pairs
of strings
and things!
-- Fritz Ruehr (after Dr Suess)
-}
-- (1+2)+3
-- time flies like an arrow
-- fruit flies like a banana
newtype Parser a = -- a parser of things 'a'
MkParser (String -> -- a function from strings
Maybe ( String -- pairs of strings
, a)) -- and things
runParser :: Parser a -> String -> Maybe (String, a)
runParser (MkParser p) s = p s
oldbob :: Parser ()
oldbob = MkParser recogniseBob
where recogniseBob ('B':'o':'b':rest) = Just (rest, ())
recogniseBob _ = Nothing
oldben :: Parser ()
oldben = MkParser recogniseBen
where recogniseBen ('B':'e':'n':rest) = Just (rest, ())
recogniseBen _ = Nothing
-- "(plus Z Z)"
string :: String -> Parser ()
string expected = MkParser (p expected)
where
p :: String -> String -> Maybe (String, ())
p [] rest = Just (rest, ())
p (_:_) [] = Nothing
p (e:es) (c:cs) | e == c = p es cs
| otherwise = Nothing
bob, ben, fred :: Parser ()
bob = string "Bob"
ben = string "Ben"
fred = string "Fred"
orElse :: Parser a -> Parser a -> Parser a
orElse (MkParser p1) (MkParser p2) =
-- p1 :: String -> Maybe (String, a)
-- p2 :: String -> Maybe (String, a)
MkParser (\s -> case p1 s of
Just result -> Just result
Nothing -> p2 s)
failure :: Parser a
failure = MkParser (\s -> Nothing)
data Person
= Bob
| Ben
| Fred
deriving Show
alter :: (b -> a) -> Parser b -> Parser a
alter f (MkParser p) =
MkParser (\s -> case p s of
Nothing -> Nothing
Just (rest, y) -> Just (rest, f y))
instance Functor Parser where
fmap f p = alter f p
bob' = alter (\() -> Bob) bob
ben' = alter (\() -> Ben) ben
fred' = alter (\() -> Fred) fred
personToString :: Person -> String
personToString Bob = "Bob"
personToString Ben = "Ben"
personToString Fred = "Fred"
andThen :: Parser a -> Parser b -> Parser (a,b)
andThen (MkParser p1) (MkParser p2) =
-- p1 :: String -> Maybe (String, a)
-- p2 :: String -> Maybe (String, b)
MkParser (\s -> case p1 s of
Nothing -> Nothing
Just (s0, a) ->
case p2 s0 of
Nothing -> Nothing
Just (s1, b) -> Just (s1, (a,b)))
person :: Parser Person
person = bob' `orElse` ben' `orElse` fred'
sequ parser =
fmap (\((p,()),ps) -> p:ps) (parser `andThen`
string "," `andThen`
sequ parser)
`orElse`
fmap (\() -> []) (string "")
{-
fmap (\((((a,()),b),()),c) -> (a,b,c))
( person
`andThen` string ","
`andThen` sequ
`andThen` string ","
`andThen` person)
-}