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 type 'a localized_option =
32 | LNone of Token.flocation
35 include_paths:string list ->
36 LexiconEngine.status ->
37 LexiconEngine.status *
38 (CicNotationPt.term, CicNotationPt.term,
39 CicNotationPt.term GrafiteAst.reduction, CicNotationPt.obj, string)
40 GrafiteAst.statement localized_option
42 let grammar = CicNotationParser.level2_ast_grammar
44 let term = CicNotationParser.term
45 let statement = Grammar.Entry.create grammar "statement"
47 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
49 let default_precedence = 50
50 let default_associativity = Gramext.NonA
53 GLOBAL: term statement;
55 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
56 SYMBOL ":"; ty = term; RPAREN -> names,ty
57 | name = IDENT -> [name],Ast.Implicit
60 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
61 tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
62 ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
64 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
67 [ IDENT "normalize" -> `Normalize
68 | IDENT "reduce" -> `Reduce
69 | IDENT "simplify" -> `Simpl
70 | IDENT "unfold"; t = OPT term -> `Unfold t
71 | IDENT "whd" -> `Whd ]
73 sequent_pattern_spec: [
77 path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
78 (id,match path with Some p -> p | None -> Ast.UserInput) ];
79 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
81 match goal_path, hyp_paths with
82 None, [] -> Some Ast.UserInput
84 | Some goal_path, _ -> Some goal_path
93 [ "match" ; wanted = tactic_term ;
94 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
96 | sps = sequent_pattern_spec ->
99 let wanted,hyp_paths,goal_path =
100 match wanted_and_sps with
101 wanted,None -> wanted, [], Some Ast.UserInput
102 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
104 wanted, hyp_paths, goal_path ] ->
106 None -> None,[],Some Ast.UserInput
110 [ SYMBOL ">" -> `LeftToRight
111 | SYMBOL "<" -> `RightToLeft ]
113 int: [ [ num = NUMBER -> int_of_string num ] ];
115 [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
116 let idents = match idents with None -> [] | Some idents -> idents in
120 using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
122 [ IDENT "absurd"; t = tactic_term ->
123 GrafiteAst.Absurd (loc, t)
124 | IDENT "apply"; t = tactic_term ->
125 GrafiteAst.Apply (loc, t)
126 | IDENT "assumption" ->
127 GrafiteAst.Assumption loc
129 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
130 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
131 paramodulation = OPT [ IDENT "paramodulation" ];
132 full = OPT [ IDENT "full" ] -> (* ALB *)
133 GrafiteAst.Auto (loc,depth,width,paramodulation,full)
134 | IDENT "clear"; id = IDENT ->
135 GrafiteAst.Clear (loc,id)
136 | IDENT "clearbody"; id = IDENT ->
137 GrafiteAst.ClearBody (loc,id)
138 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
139 GrafiteAst.Change (loc, what, t)
140 | IDENT "compare"; t = tactic_term ->
141 GrafiteAst.Compare (loc,t)
142 | IDENT "constructor"; n = int ->
143 GrafiteAst.Constructor (loc, n)
144 | IDENT "contradiction" ->
145 GrafiteAst.Contradiction loc
146 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
147 GrafiteAst.Cut (loc, ident, t)
148 | IDENT "decide"; IDENT "equality" ->
149 GrafiteAst.DecideEquality loc
150 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
151 (num, idents) = intros_spec ->
152 let types = match types with None -> [] | Some types -> types in
153 let to_spec id = GrafiteAst.Ident id in
154 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
155 | IDENT "discriminate"; t = tactic_term ->
156 GrafiteAst.Discriminate (loc, t)
157 | IDENT "elim"; what = tactic_term; using = using;
158 (num, idents) = intros_spec ->
159 GrafiteAst.Elim (loc, what, using, num, idents)
160 | IDENT "elimType"; what = tactic_term; using = using;
161 (num, idents) = intros_spec ->
162 GrafiteAst.ElimType (loc, what, using, num, idents)
163 | IDENT "exact"; t = tactic_term ->
164 GrafiteAst.Exact (loc, t)
166 GrafiteAst.Exists loc
167 | IDENT "fail" -> GrafiteAst.Fail loc
168 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
171 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error
172 ("the pattern cannot specify the term to replace, only its"
173 ^ " paths in the hypotheses and in the conclusion")))
175 GrafiteAst.Fold (loc, kind, t, p)
177 GrafiteAst.Fourier loc
178 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
179 let idents = match idents with None -> [] | Some idents -> idents in
180 GrafiteAst.FwdSimpl (loc, hyp, idents)
181 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
182 GrafiteAst.Generalize (loc,p,id)
183 | IDENT "goal"; n = int ->
184 GrafiteAst.Goal (loc, n)
185 | IDENT "id" -> GrafiteAst.IdTac loc
186 | IDENT "injection"; t = tactic_term ->
187 GrafiteAst.Injection (loc, t)
188 | IDENT "intro"; ident = OPT IDENT ->
189 let idents = match ident with None -> [] | Some id -> [id] in
190 GrafiteAst.Intros (loc, Some 1, idents)
191 | IDENT "intros"; (num, idents) = intros_spec ->
192 GrafiteAst.Intros (loc, num, idents)
193 | IDENT "inversion"; t = tactic_term ->
194 GrafiteAst.Inversion (loc, t)
196 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
198 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
199 ident = OPT [ IDENT "using" ; ident = IDENT -> ident ] ->
200 let to_what = match to_what with None -> [] | Some to_what -> to_what in
201 GrafiteAst.LApply (loc, depth, to_what, what, ident)
202 | IDENT "left" -> GrafiteAst.Left loc
203 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
204 GrafiteAst.LetIn (loc, t, where)
205 | kind = reduction_kind; p = pattern_spec ->
206 GrafiteAst.Reduce (loc, kind, p)
207 | IDENT "reflexivity" ->
208 GrafiteAst.Reflexivity loc
209 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
210 GrafiteAst.Replace (loc, p, t)
211 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
215 (HExtlib.Localized (loc,
216 (CicNotationParser.Parse_error
217 "the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion")))
219 GrafiteAst.Rewrite (loc, d, t, p)
226 | IDENT "symmetry" ->
227 GrafiteAst.Symmetry loc
228 | IDENT "transitivity"; t = tactic_term ->
229 GrafiteAst.Transitivity (loc, t)
234 [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
237 | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
240 GrafiteAst.Seq (loc, ts)
243 [ tac = SELF; SYMBOL ";";
244 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
245 (GrafiteAst.Then (loc, tac, tacs))
248 [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
249 GrafiteAst.Do (loc, count, tac)
250 | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
254 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
255 GrafiteAst.First (loc, tacs)
256 | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
258 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
259 GrafiteAst.Solve (loc, tacs)
260 | LPAREN; tac = SELF; RPAREN -> tac
261 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
264 punctuation_tactical:
266 [ SYMBOL "[" -> GrafiteAst.Branch loc
267 | SYMBOL "|" -> GrafiteAst.Shift loc
268 | i = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
269 | SYMBOL "]" -> GrafiteAst.Merge loc
270 | SYMBOL ";" -> GrafiteAst.Semicolon loc
271 | SYMBOL "." -> GrafiteAst.Dot loc
276 [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
277 | IDENT "unfocus" -> GrafiteAst.Unfocus loc
278 | IDENT "skip" -> GrafiteAst.Skip loc
279 | tac = atomic_tactical LEVEL "loops" -> tac
283 [ [ IDENT "definition" ] -> `Definition
284 | [ IDENT "fact" ] -> `Fact
285 | [ IDENT "lemma" ] -> `Lemma
286 | [ IDENT "remark" ] -> `Remark
287 | [ IDENT "theorem" ] -> `Theorem
291 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
292 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
293 fst_constructors = LIST0 constructor SEP SYMBOL "|";
296 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
297 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
298 (name, true, typ, constructors) ] SEP "with" -> types
302 (fun (names, typ) acc ->
303 (List.map (fun name -> (name, typ)) names) @ acc)
306 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
307 let tl_ind_types = match tl with None -> [] | Some types -> types in
308 let ind_types = fst_ind_type :: tl_ind_types in
313 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
314 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
317 coercion = [ SYMBOL ":" -> false | SYMBOL ":"; SYMBOL ">" -> true ] ;
318 ty = term -> (name,ty,coercion)
319 ] SEP SYMBOL ";"; SYMBOL "}" ->
322 (fun (names, typ) acc ->
323 (List.map (fun name -> (name, typ)) names) @ acc)
326 (params,name,typ,fields)
330 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
331 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
332 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
333 GrafiteAst.Undo (loc, int_opt steps)
334 | [ IDENT "redo" ]; steps = OPT NUMBER ->
335 GrafiteAst.Redo (loc, int_opt steps) *)
336 | [ IDENT "check" ]; t = term ->
337 GrafiteAst.Check (loc, t)
338 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
339 | [ IDENT "whelp"; "match" ] ; t = term ->
340 GrafiteAst.WMatch (loc,t)
341 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
342 GrafiteAst.WInstance (loc,t)
343 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
344 GrafiteAst.WLocate (loc,id)
345 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
346 GrafiteAst.WElim (loc, t)
347 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
348 GrafiteAst.WHint (loc,t)
349 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
353 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
354 let alpha = "[a-zA-Z]" in
355 let num = "[0-9]+" in
356 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
357 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
358 let rex = Str.regexp ("^"^ident^"$") in
359 if Str.string_match rex id 0 then
360 if (try ignore (UriManager.uri_of_string uri); true
361 with UriManager.IllFormedUri _ -> false)
363 LexiconAst.Ident_alias (id, uri)
366 (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
368 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
369 sprintf "Not a valid identifier: %s" id)))
370 | IDENT "symbol"; symbol = QSTRING;
371 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
372 SYMBOL "="; dsc = QSTRING ->
374 match instance with Some i -> i | None -> 0
376 LexiconAst.Symbol_alias (symbol, instance, dsc)
378 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
379 SYMBOL "="; dsc = QSTRING ->
381 match instance with Some i -> i | None -> 0
383 LexiconAst.Number_alias (instance, dsc)
387 [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
389 Ast.IdentArg (List.length l, id)
393 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
394 | IDENT "right"; IDENT "associative" -> Gramext.RightA
395 | IDENT "non"; IDENT "associative" -> Gramext.NonA
399 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
402 [ dir = OPT direction; s = QSTRING;
403 assoc = OPT associativity; prec = OPT precedence;
406 [ blob = UNPARSED_AST ->
407 add_raw_attribute ~text:(sprintf "@{%s}" blob)
408 (CicNotationParser.parse_level2_ast
409 (Ulexing.from_utf8_string blob))
410 | blob = UNPARSED_META ->
411 add_raw_attribute ~text:(sprintf "${%s}" blob)
412 (CicNotationParser.parse_level2_meta
413 (Ulexing.from_utf8_string blob))
417 | None -> default_associativity
418 | Some assoc -> assoc
422 | None -> default_precedence
426 add_raw_attribute ~text:s
427 (CicNotationParser.parse_level1_pattern
428 (Ulexing.from_utf8_string s))
430 (dir, p1, assoc, prec, p2)
434 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
435 | id = IDENT -> Ast.VarPattern id
436 | SYMBOL "_" -> Ast.ImplicitPattern
437 | LPAREN; terms = LIST1 SELF; RPAREN ->
441 | terms -> Ast.ApplPattern terms)
445 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
451 IDENT "include" ; path = QSTRING -> loc,path
455 IDENT "set"; n = QSTRING; v = QSTRING ->
456 GrafiteAst.Set (loc, n, v)
457 | IDENT "drop" -> GrafiteAst.Drop loc
458 | IDENT "qed" -> GrafiteAst.Qed loc
459 | IDENT "variant" ; name = IDENT; SYMBOL ":";
460 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
463 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
464 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
465 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
466 GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
467 | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
470 Ast.Theorem (flavour, name, Ast.Implicit, Some body))
471 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
472 defs = CicNotationParser.let_defs ->
475 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
476 | ((Ast.Ident (name, None), None),_,_) :: _ ->
480 let body = Ast.Ident (name,None) in
481 GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
482 Some (Ast.LetRec (ind_kind, defs, body))))
483 | IDENT "inductive"; spec = inductive_spec ->
484 let (params, ind_types) = spec in
485 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
486 | IDENT "coinductive"; spec = inductive_spec ->
487 let (params, ind_types) = spec in
488 let ind_types = (* set inductive flags to false (coinductive) *)
489 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
492 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
493 | IDENT "coercion" ; suri = URI ->
494 GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true)
495 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
496 GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
497 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
498 let uris = List.map UriManager.uri_of_string uris in
499 GrafiteAst.Default (loc,what,uris)
502 IDENT "alias" ; spec = alias_spec ->
503 LexiconAst.Alias (loc, spec)
504 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
505 LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
506 | IDENT "interpretation"; id = QSTRING;
507 (symbol, args, l3) = interpretation ->
508 LexiconAst.Interpretation (loc, id, (symbol, args), l3)
511 [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
512 | tac = tactical; punct = punctuation_tactical ->
513 GrafiteAst.Tactical (loc, tac, Some punct)
514 | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
515 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
519 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
520 GrafiteAst.Code (loc, ex)
522 GrafiteAst.Note (loc, str)
527 fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
529 fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
530 | (iloc,fname) = include_command ; SYMBOL "." ->
531 fun ~include_paths status ->
532 let path = DependenciesParser.baseuri_of_script ~include_paths fname in
534 LexiconEngine.eval_command status (LexiconAst.Include (iloc,path))
538 (GrafiteAst.Executable
539 (loc,GrafiteAst.Command
540 (loc,GrafiteAst.Include (iloc,path))))
541 | scom = lexicon_command ; SYMBOL "." ->
542 fun ~include_paths status ->
543 let status = LexiconEngine.eval_command status scom in
545 | EOI -> raise End_of_file
550 let exc_located_wrapper f =
554 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
555 | Stdpp.Exc_located (floc, Stream.Error msg) ->
556 raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
557 | Stdpp.Exc_located (floc, exn) ->
559 (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
561 let parse_statement lexbuf =
563 (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))