-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlang_parser.ml
76 lines (56 loc) · 2.04 KB
/
lang_parser.ml
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
module Make (P : Prim_parser.S) (T : Pure.THEORY) =
struct
open Pure
open P
let arrow = symbol "->" <|> symbol "→"
let forall = symbol "\\/" <|> symbol "∀" <|> symbol "Π"
let lambda = symbol "\\" <|> symbol "λ"
let rec expr i = chainr1 expr1 (arrow *> return (fun t1 t2 -> PI (("",t1),t2))) i
and expr1 i = chainl1 expr2 (return (fun m n -> APP (m,n))) i
and expr2 i = (annot <|> sort <|> var <|> lam <|> alam <|> pi <|> (fun i -> paren expr i)) i
and annot i = paren ((fun e t -> ANNOT (e,t)) <$>
expr <*>
symbol ":" *>
expr) i
and sort i = ((fun s -> SORT s) <$>
let* v = variable in
if List.mem v T.sorts
then return v
else fail) i
and var i = ((fun v -> F v) <$> variable) i
and lam i = (bind_fold (fun (x,e) -> LAM (x,e)) <$>
lambda *>
paren (many1 variable) <*>
expr) i
and alam i = (multi_bind (fun (x,e) -> ALAM (x,e)) <$>
lambda *>
many1 (paren args) <*>
expr) i
and pi i = (multi_bind (fun (x,e) -> PI (x,e)) <$>
forall *>
many1 (paren args) <*>
expr) i
and args i = ((fun xs t -> xs,t) <$>
many1 variable <*>
symbol ":" *>
expr) i
let plain_dec i = ((fun x y -> (x,y)) <$>
symbol "let" *>
variable <*>
symbol "=" *>
expr) i
let annot_dec i = ((fun x t e -> (x,ANNOT (e,t))) <$>
symbol "let" *>
variable <*>
symbol ":" *>
expr <*>
symbol "=" *>
expr) i
let dec = pre (annot_dec <|> plain_dec)
let exp = pre expr
let prgm = many dec
type cmd =
| EXP of term
| DEC of string * term
let cmd = ((fun (x,y) -> DEC (x,y)) <$> dec) <|> ((fun x -> EXP x) <$> exp)
end