1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 module Ast = CicNotationPt
30 let grammar = CicNotationParser.level2_ast_grammar
32 let term = CicNotationParser.term
33 let statement = Grammar.Entry.create grammar "statement"
35 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
37 let default_precedence = 50
38 let default_associativity = Gramext.NonA
41 GLOBAL: term statement;
43 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
44 SYMBOL ":"; ty = term; RPAREN -> names,ty
45 | name = IDENT -> [name],Ast.Implicit
48 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
49 tactic_term: [ [ t = term -> t ] ];
51 [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
54 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
57 [ IDENT "normalize" -> `Normalize
58 | IDENT "reduce" -> `Reduce
59 | IDENT "simplify" -> `Simpl
60 | IDENT "unfold"; t = OPT term -> `Unfold t
61 | IDENT "whd" -> `Whd ]
63 sequent_pattern_spec: [
67 path = OPT [SYMBOL ":" ; path = term -> path ] ->
68 (id,match path with Some p -> p | None -> Ast.UserInput) ]
70 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
72 match goal_path, hyp_paths with
73 None, [] -> Ast.UserInput
74 | None, _::_ -> Ast.Implicit
75 | Some goal_path, _ -> goal_path
84 [ "match" ; wanted = term ;
85 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
87 | sps = sequent_pattern_spec ->
90 let wanted,hyp_paths,goal_path =
91 match wanted_and_sps with
92 wanted,None -> wanted, [], Ast.UserInput
93 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
95 wanted, hyp_paths, goal_path ] ->
97 None -> None,[],Ast.UserInput
101 [ SYMBOL ">" -> `LeftToRight
102 | SYMBOL "<" -> `RightToLeft ]
104 int: [ [ num = NUMBER -> int_of_string num ] ];
106 [ IDENT "absurd"; t = tactic_term ->
107 GrafiteAst.Absurd (loc, t)
108 | IDENT "apply"; t = tactic_term ->
109 GrafiteAst.Apply (loc, t)
110 | IDENT "assumption" ->
111 GrafiteAst.Assumption loc
113 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
114 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
115 paramodulation = OPT [ IDENT "paramodulation" ];
116 full = OPT [ IDENT "full" ] -> (* ALB *)
117 GrafiteAst.Auto (loc,depth,width,paramodulation,full)
118 | IDENT "clear"; id = IDENT ->
119 GrafiteAst.Clear (loc,id)
120 | IDENT "clearbody"; id = IDENT ->
121 GrafiteAst.ClearBody (loc,id)
122 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
123 GrafiteAst.Change (loc, what, t)
124 | IDENT "compare"; t = tactic_term ->
125 GrafiteAst.Compare (loc,t)
126 | IDENT "constructor"; n = int ->
127 GrafiteAst.Constructor (loc, n)
128 | IDENT "contradiction" ->
129 GrafiteAst.Contradiction loc
130 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
131 GrafiteAst.Cut (loc, ident, t)
132 | IDENT "decide"; IDENT "equality" ->
133 GrafiteAst.DecideEquality loc
134 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
135 OPT "names"; num = OPT [num = int -> num];
136 idents = OPT ident_list0 ->
137 let idents = match idents with None -> [] | Some idents -> idents in
138 let types = match types with None -> [] | Some types -> types in
139 let to_spec id = GrafiteAst.Ident id in
140 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
141 | IDENT "discriminate"; t = tactic_term ->
142 GrafiteAst.Discriminate (loc, t)
143 | IDENT "elim"; what = tactic_term;
144 using = OPT [ "using"; using = tactic_term -> using ];
145 OPT "names"; num = OPT [num = int -> num];
146 idents = OPT ident_list0 ->
147 let idents = match idents with None -> [] | Some idents -> idents in
148 GrafiteAst.Elim (loc, what, using, num, idents)
149 | IDENT "elimType"; what = tactic_term;
150 using = OPT [ "using"; using = tactic_term -> using ];
151 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
152 let idents = match idents with None -> [] | Some idents -> idents in
153 GrafiteAst.ElimType (loc, what, using, num, idents)
154 | IDENT "exact"; t = tactic_term ->
155 GrafiteAst.Exact (loc, t)
157 GrafiteAst.Exists loc
158 | IDENT "fail" -> GrafiteAst.Fail loc
159 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
162 raise (CicNotationParser.Parse_error
163 (loc, "the pattern cannot specify the term to replace, only its"
164 ^ " paths in the hypotheses and in the conclusion"))
166 GrafiteAst.Fold (loc, kind, t, p)
168 GrafiteAst.Fourier loc
169 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
170 let idents = match idents with None -> [] | Some idents -> idents in
171 GrafiteAst.FwdSimpl (loc, hyp, idents)
172 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
173 GrafiteAst.Generalize (loc,p,id)
174 | IDENT "goal"; n = int ->
175 GrafiteAst.Goal (loc, n)
176 | IDENT "id" -> GrafiteAst.IdTac loc
177 | IDENT "injection"; t = tactic_term ->
178 GrafiteAst.Injection (loc, t)
179 | IDENT "intro"; ident = OPT IDENT ->
180 let idents = match ident with None -> [] | Some id -> [id] in
181 GrafiteAst.Intros (loc, Some 1, idents)
182 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
183 let idents = match idents with None -> [] | Some idents -> idents in
184 GrafiteAst.Intros (loc, num, idents)
186 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
188 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
189 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
190 let to_what = match to_what with None -> [] | Some to_what -> to_what in
191 GrafiteAst.LApply (loc, depth, to_what, what, ident)
192 | IDENT "left" -> GrafiteAst.Left loc
193 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
194 GrafiteAst.LetIn (loc, t, where)
195 | kind = reduction_kind; p = pattern_spec ->
196 GrafiteAst.Reduce (loc, kind, p)
197 | IDENT "reflexivity" ->
198 GrafiteAst.Reflexivity loc
199 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
200 GrafiteAst.Replace (loc, p, t)
201 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
205 (CicNotationParser.Parse_error
206 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
208 GrafiteAst.Rewrite (loc, d, t, p)
215 | IDENT "symmetry" ->
216 GrafiteAst.Symmetry loc
217 | IDENT "transitivity"; t = tactic_term ->
218 GrafiteAst.Transitivity (loc, t)
223 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
227 | l -> GrafiteAst.Seq (loc, l)
230 [ tac = tactical; SYMBOL ";";
231 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
232 (GrafiteAst.Then (loc, tac, tacs))
235 [ IDENT "do"; count = int; tac = tactical ->
236 GrafiteAst.Do (loc, count, tac)
237 | IDENT "repeat"; tac = tactical ->
238 GrafiteAst.Repeat (loc, tac)
242 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
243 GrafiteAst.First (loc, tacs)
244 | IDENT "try"; tac = NEXT ->
245 GrafiteAst.Try (loc, tac)
247 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
248 GrafiteAst.Solve (loc, tacs)
249 | LPAREN; tac = tactical; RPAREN -> tac
250 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
254 [ [ IDENT "definition" ] -> `Definition
255 | [ IDENT "fact" ] -> `Fact
256 | [ IDENT "lemma" ] -> `Lemma
257 | [ IDENT "remark" ] -> `Remark
258 | [ IDENT "theorem" ] -> `Theorem
262 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
263 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
264 fst_constructors = LIST0 constructor SEP SYMBOL "|";
267 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
268 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
269 (name, true, typ, constructors) ] SEP "with" -> types
273 (fun (names, typ) acc ->
274 (List.map (fun name -> (name, typ)) names) @ acc)
277 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
278 let tl_ind_types = match tl with None -> [] | Some types -> types in
279 let ind_types = fst_ind_type :: tl_ind_types in
284 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
285 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
287 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
288 ] SEP SYMBOL ";"; SYMBOL "}" ->
291 (fun (names, typ) acc ->
292 (List.map (fun name -> (name, typ)) names) @ acc)
295 (params,name,typ,fields)
299 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
300 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
301 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
302 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
303 GrafiteAst.Undo (loc, int_opt steps)
304 | [ IDENT "redo" ]; steps = OPT NUMBER ->
305 GrafiteAst.Redo (loc, int_opt steps) *)
306 | [ IDENT "check" ]; t = term ->
307 GrafiteAst.Check (loc, t)
308 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
309 | [ IDENT "whelp"; "match" ] ; t = term ->
310 GrafiteAst.WMatch (loc,t)
311 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
312 GrafiteAst.WInstance (loc,t)
313 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
314 GrafiteAst.WLocate (loc,id)
315 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
316 GrafiteAst.WElim (loc, t)
317 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
318 GrafiteAst.WHint (loc,t)
319 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
323 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
324 let alpha = "[a-zA-Z]" in
325 let num = "[0-9]+" in
326 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
327 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
328 let rex = Str.regexp ("^"^ident^"$") in
329 if Str.string_match rex id 0 then
330 if (try ignore (UriManager.uri_of_string uri); true
331 with UriManager.IllFormedUri _ -> false)
333 GrafiteAst.Ident_alias (id, uri)
335 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
337 raise (CicNotationParser.Parse_error (loc,
338 sprintf "Not a valid identifier: %s" id))
339 | IDENT "symbol"; symbol = QSTRING;
340 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
341 SYMBOL "="; dsc = QSTRING ->
343 match instance with Some i -> i | None -> 0
345 GrafiteAst.Symbol_alias (symbol, instance, dsc)
347 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
348 SYMBOL "="; dsc = QSTRING ->
350 match instance with Some i -> i | None -> 0
352 GrafiteAst.Number_alias (instance, dsc)
356 [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
358 Ast.IdentArg (List.length l, id)
362 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
363 | IDENT "right"; IDENT "associative" -> Gramext.RightA
364 | IDENT "non"; IDENT "associative" -> Gramext.NonA
368 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
371 [ dir = OPT direction; s = QSTRING;
372 assoc = OPT associativity; prec = OPT precedence;
375 [ blob = UNPARSED_AST ->
376 add_raw_attribute ~text:(sprintf "@{%s}" blob)
377 (CicNotationParser.parse_level2_ast
378 (Ulexing.from_utf8_string blob))
379 | blob = UNPARSED_META ->
380 add_raw_attribute ~text:(sprintf "${%s}" blob)
381 (CicNotationParser.parse_level2_meta
382 (Ulexing.from_utf8_string blob))
386 | None -> default_associativity
387 | Some assoc -> assoc
391 | None -> default_precedence
395 add_raw_attribute ~text:s
396 (CicNotationParser.parse_level1_pattern
397 (Ulexing.from_utf8_string s))
399 (dir, p1, assoc, prec, p2)
403 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
404 | id = IDENT -> Ast.VarPattern id
405 | SYMBOL "_" -> Ast.ImplicitPattern
406 | LPAREN; terms = LIST1 SELF; RPAREN ->
410 | terms -> Ast.ApplPattern terms)
414 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
419 IDENT "set"; n = QSTRING; v = QSTRING ->
420 GrafiteAst.Set (loc, n, v)
421 | IDENT "drop" -> GrafiteAst.Drop loc
422 | IDENT "qed" -> GrafiteAst.Qed loc
423 | IDENT "variant" ; name = IDENT; SYMBOL ":";
424 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
427 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
428 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
429 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
430 GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
431 | flavour = theorem_flavour; name = IDENT;
432 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
434 GrafiteAst.Theorem (flavour, name, Ast.Implicit, body))
435 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
436 defs = CicNotationParser.let_defs ->
439 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
440 | ((Ast.Ident (name, None), None),_,_) :: _ ->
444 let body = Ast.Ident (name,None) in
445 GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
446 Some (Ast.LetRec (ind_kind, defs, body))))
447 | [ IDENT "inductive" ]; spec = inductive_spec ->
448 let (params, ind_types) = spec in
449 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
450 | [ IDENT "coinductive" ]; spec = inductive_spec ->
451 let (params, ind_types) = spec in
452 let ind_types = (* set inductive flags to false (coinductive) *)
453 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
456 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
457 | IDENT "coercion" ; name = IDENT ->
458 GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
459 | IDENT "coercion" ; name = URI ->
460 GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
461 | IDENT "alias" ; spec = alias_spec ->
462 GrafiteAst.Alias (loc, spec)
463 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
464 GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
465 | IDENT "include" ; path = QSTRING ->
466 GrafiteAst.Include (loc,path)
467 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
468 let uris = List.map UriManager.uri_of_string uris in
469 GrafiteAst.Default (loc,what,uris)
470 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
471 GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
472 | IDENT "interpretation"; id = QSTRING;
473 (symbol, args, l3) = interpretation ->
474 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
476 | IDENT "dump" -> GrafiteAst.Dump loc
477 | IDENT "render"; u = URI ->
478 GrafiteAst.Render (loc, UriManager.uri_of_string u)
481 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
482 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
483 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
487 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
488 GrafiteAst.Code (loc, ex)
490 GrafiteAst.Note (loc, str)
494 [ ex = executable -> GrafiteAst.Executable (loc,ex)
495 | com = comment -> GrafiteAst.Comment (loc, com)
496 | EOI -> raise End_of_file
501 let exc_located_wrapper f =
505 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
506 | Stdpp.Exc_located (floc, Stream.Error msg) ->
507 raise (CicNotationParser.Parse_error (floc, msg))
508 | Stdpp.Exc_located (floc, exn) ->
509 raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
511 let parse_statement lexbuf =
513 (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
515 let parse_dependencies lexbuf =
517 CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
521 | [< '("URI", u) >] ->
522 parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
523 | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
524 parse (GrafiteAst.IncludeDep fname :: acc)
525 | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
526 parse (GrafiteAst.BaseuriDep baseuri :: acc)
527 | [< '("EOI", _) >] -> acc
528 | [< 'tok >] -> parse acc
529 | [< >] -> acc) tok_stream