4 let grammar = Grammar.gcreate Lexer.lex
6 let term = Grammar.Entry.create grammar "term"
7 (* let tactic = Grammar.Entry.create grammar "tactic" *)
8 (* let tactical = Grammar.Entry.create grammar "tactical" *)
10 let return_term loc term = LocatedTerm (loc, term)
15 [ s = SYMBOL "_" -> None
16 | t = term -> Some t ]
19 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
20 | SYMBOL <:unicode<pi>> (* π *) -> `Pi
21 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
22 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
25 substituted_name: [ (* a subs.name is an explicit substitution subject *)
26 [ s = [ IDENT | SYMBOL ];
28 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
31 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
37 | Some l -> Ident (s, l)
38 | None -> Ident (s, []))
41 name: [ (* as substituted_name with no explicit substitution *)
42 [ s = [ IDENT | SYMBOL ] -> s ]
46 | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ]
49 [ "add" LEFTA [ (* nothing here by default *) ]
50 | "mult" LEFTA [ (* nothing here by default *) ]
51 | "inv" NONA [ (* nothing here by default *) ]
54 b = binder; vars = LIST1 IDENT SEP SYMBOL ",";
55 typ = OPT [ SYMBOL ":"; t = term -> t ];
56 SYMBOL "."; body = term ->
58 List.fold_right (fun var body -> Binder (b, var, typ, body))
61 return_term loc binder
62 | n = substituted_name -> return_term loc n
63 | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" ->
64 return_term loc (Appl (head :: args))
65 | i = INT -> return_term loc (Int (int_of_string i))
68 LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" ->
71 return_term loc (Meta (m, substs))
72 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
73 * since applications are required to be bound by parens *)
74 | "let"; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *); t1 = term;
75 IDENT "in"; t2 = term ->
76 return_term loc (LetIn (name, t1, t2))
77 | "let"; "rec"; defs = LIST1 [
79 index = OPT [ LPAREN "("; index = INT; RPAREN ")" ->
82 typ = OPT [ SYMBOL ":"; typ = term -> typ ];
83 SYMBOL <:unicode<def>> (* ≝ *); t1 = term ->
84 (name, t1, typ, (match index with None -> 1 | Some i -> i))
86 IDENT "in"; body = term ->
87 return_term loc (LetRec (defs, body))
88 | typ = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ];
89 "match"; t = term; "with";
92 p = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒*); t = term -> (p, t)
95 return_term loc (Case (t, typ, patterns))
96 | LPAREN "("; t = term; RPAREN ")" -> return_term loc t
101 let parse_term = Grammar.Entry.parse term