-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.hs
223 lines (175 loc) · 3.91 KB
/
Parse.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
module Parse where
-- MAYBE usar indentacion
import Lang hiding ( var )
import Text.Parsec hiding ( runP )
import Text.ParserCombinators.Parsec.Language
import qualified Text.Parsec.Token as Tok
type P = Parsec String ()
-- NICETOHAVE parsear, por ej, un punto pegado a una barra
-- NICETOHAVE parsear identificadores a lo agda
langDef :: LanguageDef u
langDef = emptyDef {
commentStart = "/*",
commentEnd = "*/",
commentLine = "//",
reservedNames = ["let", "rec", "λ", "\\", "fix", "in", "data", "elim",
"Set", "Nat", "suc", "zero", "refl"],
reservedOpNames = ["->", "→", ":", ":=", "≔", "=", "."]
}
lexer :: Tok.TokenParser u
lexer = Tok.makeTokenParser langDef
parens :: P a -> P a
parens = Tok.parens lexer
braces :: P a -> P a
braces = Tok.braces lexer
semiSep :: P a -> P [a]
semiSep = Tok.semiSep lexer
whiteSpace :: P ()
whiteSpace = Tok.whiteSpace lexer
reserved :: String -> P ()
reserved = Tok.reserved lexer
reservedOp :: String -> P ()
reservedOp = Tok.reservedOp lexer
identifier :: P String
identifier = Tok.identifier lexer
symbol :: String -> P String
symbol = Tok.symbol lexer
natural :: P Integer
natural = Tok.natural lexer
-------------------------------------------------------------
num :: P Int
num = fromInteger <$> natural
name :: P Name
name = identifier
lit :: P STerm
lit = Lit <$> num
var :: P STerm
var = SV <$> name
snat :: P STerm
snat = reserved "Nat" >> return SNat
szero :: P STerm
szero = reserved "zero" >> return SZero
ssuc :: P STerm
ssuc = reserved "suc" >> return SSuc
srefl :: P STerm
srefl = reserved "refl" >> return SRefl
spi :: P STerm
spi = do
a <- arg
reservedOp "->" <|> reservedOp "→"
ty <- stype
return (SPi a ty)
sort :: P STerm
sort = try set <|> set0
where
set = do
reserved "Set"
n <- num
return (SSort (Set n))
set0 = do
reserved "Set"
return (SSort (Set 0))
arg :: P SArg
arg = parens $ do
xs <- many1 name
reservedOp ":"
ty <- stype
return (Arg xs ty)
atom :: P STerm
atom =
lit
<|> var
<|> snat
<|> szero
<|> ssuc
<|> srefl
<|> try spi
<|> sort
<|> parens sterm
lam :: P STerm
lam = do reserved "\\" <|> reserved "λ"
a <- arg
symbol "."
SLam a <$> sterm
fix :: P STerm
fix = do
reserved "fix"
f <- name
a <- arg
reservedOp ":"
ty <- stype
reservedOp "."
t <- sterm
return (SFix f a ty t)
elim :: P STerm
elim = do
reserved "elim"
t <- sterm
bs <- braces (semiSep branch)
return (SElim t bs)
branch :: P SElimBranch
branch = do
c <- name <|> choice (map reservedName cons)
as <- many name
reservedOp ":=" <|> reservedOp "≔"
t <- sterm
return (ElimBranch c as t)
where
reservedName n = reserved n >> return n
cons = ["zero", "suc", "refl"]
app :: P STerm
app = do
f <- atom
args <- many atom
return (foldl SApp f args)
expr :: P STerm
expr = app <|> lam <|> fix <|> elim
equalOp :: STerm -> P STerm
equalOp t = do
reservedOp "="
u <- expr
return (SEq t u)
ann :: STerm -> P STerm
ann t = do
reservedOp ":"
ty <- stype
return (SAnn t ty)
sterm :: P STerm
sterm = do
t <- expr
ann t <|> equalOp t <|> return t
stype :: P SType
stype = Type <$> sterm
decl :: P SDecl
decl = do
reserved "let"
n <- name
args <- many arg
reservedOp ":"
ty <- stype
reservedOp ":="
t <- sterm
return (SDecl n args ty t)
datacons :: P SConsDecl
datacons =
ConsDecl
<$> name
<* reservedOp ":"
<*> stype
datadecl :: P SDataDecl
datadecl =
DataDecl
<$ reserved "data"
<*> name
<* reservedOp ":"
<*> stype
<*> braces (semiSep datacons)
program :: P SProgram
program = many def
where def = (PDecl <$> decl) <|> (PData <$> datadecl)
parse :: String -> STerm
parse s = case runP sterm s "" of
Right t -> t
Left e -> error $ show e
runP :: P a -> String -> String -> Either ParseError a
runP p s filename = runParser (whiteSpace *> p <* eof) () filename s