open Ast let grammar = Grammar.gcreate Lexer.lex let term = Grammar.Entry.create grammar "term" (* let tactic = Grammar.Entry.create grammar "tactic" *) (* let tactical = Grammar.Entry.create grammar "tactical" *) let return_term loc term = LocatedTerm (loc, term) EXTEND GLOBAL: term; meta_subst: [ [ s = SYMBOL "_" -> None | t = term -> Some t ] ]; binder: [ [ SYMBOL <:unicode> (* λ *) -> `Lambda | SYMBOL <:unicode> (* π *) -> `Pi | SYMBOL <:unicode> (* ∃ *) -> `Exists | SYMBOL <:unicode> (* ∀ *) -> `Forall ] ]; substituted_name: [ (* a subs.name is an explicit substitution subject *) [ s = [ IDENT | SYMBOL ]; subst = OPT [ SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *) LPAREN "["; substs = LIST1 [ i = IDENT; SYMBOL <:unicode> (* ≔ *); t = term -> (i, t) ] SEP SYMBOL ";"; RPAREN "]" -> substs ] -> (match subst with | Some l -> Ident (s, l) | None -> Ident (s, [])) ] ]; name: [ (* as substituted_name with no explicit substitution *) [ s = [ IDENT | SYMBOL ] -> s ] ]; pattern: [ [ n = name -> [n] | LPAREN "("; names = LIST1 name; RPAREN ")" -> names ] ]; term: [ "add" LEFTA [ (* nothing here by default *) ] | "mult" LEFTA [ (* nothing here by default *) ] | "inv" NONA [ (* nothing here by default *) ] | "simple" NONA [ b = binder; vars = LIST1 IDENT SEP SYMBOL ","; typ = OPT [ SYMBOL ":"; t = term -> t ]; SYMBOL "."; body = term -> let binder = List.fold_right (fun var body -> Binder (b, var, typ, body)) vars body in return_term loc binder | n = substituted_name -> return_term loc n | LPAREN "("; head = term; args = LIST1 term; RPAREN ")" -> return_term loc (Appl (head :: args)) | i = INT -> return_term loc (Int (int_of_string i)) | m = META; substs = [ LPAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; RPAREN "]" -> substs ] -> return_term loc (Meta (m, substs)) (* actually "in" and "and" are _not_ keywords. Parsing works anyway * since applications are required to be bound by parens *) | "let"; name = IDENT; SYMBOL <:unicode> (* ≝ *); t1 = term; IDENT "in"; t2 = term -> return_term loc (LetIn (name, t1, t2)) | "let"; "rec"; defs = LIST1 [ name = IDENT; index = OPT [ LPAREN "("; index = INT; RPAREN ")" -> int_of_string index ]; typ = OPT [ SYMBOL ":"; typ = term -> typ ]; SYMBOL <:unicode> (* ≝ *); t1 = term -> (name, t1, typ, (match index with None -> 1 | Some i -> i)) ] SEP (IDENT "and"); IDENT "in"; body = term -> return_term loc (LetRec (defs, body)) | typ = OPT [ LPAREN "["; typ = term; RPAREN "]" -> typ ]; "match"; t = term; "with"; LPAREN "["; patterns = LIST0 [ p = pattern; SYMBOL <:unicode> (* ⇒*); t = term -> (p, t) ] SEP SYMBOL "|"; RPAREN "]" -> return_term loc (Case (t, typ, patterns)) | LPAREN "("; t = term; RPAREN ")" -> return_term loc t ] ]; END let parse_term = Grammar.Entry.parse term