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/
30 module Ast = CicNotationPt
32 type 'a localized_option =
34 | LNone of Token.flocation
37 include_paths:string list ->
38 LexiconEngine.status ->
39 LexiconEngine.status *
40 (CicNotationPt.term, CicNotationPt.term,
41 CicNotationPt.term GrafiteAst.reduction, CicNotationPt.obj, string)
42 GrafiteAst.statement localized_option
44 let grammar = CicNotationParser.level2_ast_grammar
46 let term = CicNotationParser.term
47 let statement = Grammar.Entry.create grammar "statement"
49 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
51 let default_precedence = 50
52 let default_associativity = Gramext.NonA
55 GLOBAL: term statement;
57 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
58 SYMBOL ":"; ty = term; RPAREN -> names,ty
59 | name = IDENT -> [name],Ast.Implicit
62 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
63 tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
64 ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
66 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
69 [ IDENT "demodulate" -> `Demodulate
70 | IDENT "normalize" -> `Normalize
71 | IDENT "reduce" -> `Reduce
72 | IDENT "simplify" -> `Simpl
73 | IDENT "unfold"; t = OPT term -> `Unfold t
74 | IDENT "whd" -> `Whd ]
76 sequent_pattern_spec: [
80 path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
81 (id,match path with Some p -> p | None -> Ast.UserInput) ];
82 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
84 match goal_path, hyp_paths with
85 None, [] -> Some Ast.UserInput
87 | Some goal_path, _ -> Some goal_path
96 [ "match" ; wanted = tactic_term ;
97 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
99 | sps = sequent_pattern_spec ->
102 let wanted,hyp_paths,goal_path =
103 match wanted_and_sps with
104 wanted,None -> wanted, [], Some Ast.UserInput
105 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
107 wanted, hyp_paths, goal_path ] ->
109 None -> None,[],Some Ast.UserInput
113 [ SYMBOL ">" -> `LeftToRight
114 | SYMBOL "<" -> `RightToLeft ]
116 int: [ [ num = NUMBER -> int_of_string num ] ];
118 [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
119 let idents = match idents with None -> [] | Some idents -> idents in
123 using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
125 [ IDENT "absurd"; t = tactic_term ->
126 GrafiteAst.Absurd (loc, t)
127 | IDENT "apply"; t = tactic_term ->
128 GrafiteAst.Apply (loc, t)
129 | IDENT "assumption" ->
130 GrafiteAst.Assumption loc
132 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
133 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
134 paramodulation = OPT [ IDENT "paramodulation" ];
135 full = OPT [ IDENT "full" ] -> (* ALB *)
136 GrafiteAst.Auto (loc,depth,width,paramodulation,full)
137 | IDENT "clear"; id = IDENT ->
138 GrafiteAst.Clear (loc,id)
139 | IDENT "clearbody"; id = IDENT ->
140 GrafiteAst.ClearBody (loc,id)
141 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
142 GrafiteAst.Change (loc, what, t)
143 | IDENT "compare"; t = tactic_term ->
144 GrafiteAst.Compare (loc,t)
145 | IDENT "constructor"; n = int ->
146 GrafiteAst.Constructor (loc, n)
147 | IDENT "contradiction" ->
148 GrafiteAst.Contradiction loc
149 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
150 GrafiteAst.Cut (loc, ident, t)
151 | IDENT "decide"; IDENT "equality" ->
152 GrafiteAst.DecideEquality loc
153 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
154 (num, idents) = intros_spec ->
155 let types = match types with None -> [] | Some types -> types in
156 let to_spec id = GrafiteAst.Ident id in
157 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
158 | IDENT "discriminate"; t = tactic_term ->
159 GrafiteAst.Discriminate (loc, t)
160 | IDENT "elim"; what = tactic_term; using = using;
161 (num, idents) = intros_spec ->
162 GrafiteAst.Elim (loc, what, using, num, idents)
163 | IDENT "elimType"; what = tactic_term; using = using;
164 (num, idents) = intros_spec ->
165 GrafiteAst.ElimType (loc, what, using, num, idents)
166 | IDENT "exact"; t = tactic_term ->
167 GrafiteAst.Exact (loc, t)
169 GrafiteAst.Exists loc
170 | IDENT "fail" -> GrafiteAst.Fail loc
171 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
174 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error
175 ("the pattern cannot specify the term to replace, only its"
176 ^ " paths in the hypotheses and in the conclusion")))
178 GrafiteAst.Fold (loc, kind, t, p)
180 GrafiteAst.Fourier loc
181 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
182 let idents = match idents with None -> [] | Some idents -> idents in
183 GrafiteAst.FwdSimpl (loc, hyp, idents)
184 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
185 GrafiteAst.Generalize (loc,p,id)
186 | IDENT "goal"; n = int ->
187 GrafiteAst.Goal (loc, n)
188 | IDENT "id" -> GrafiteAst.IdTac loc
189 | IDENT "injection"; t = tactic_term ->
190 GrafiteAst.Injection (loc, t)
191 | IDENT "intro"; ident = OPT IDENT ->
192 let idents = match ident with None -> [] | Some id -> [id] in
193 GrafiteAst.Intros (loc, Some 1, idents)
194 | IDENT "intros"; (num, idents) = intros_spec ->
195 GrafiteAst.Intros (loc, num, idents)
196 | IDENT "inversion"; t = tactic_term ->
197 GrafiteAst.Inversion (loc, t)
199 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
201 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
202 ident = OPT [ IDENT "using" ; ident = IDENT -> ident ] ->
203 let to_what = match to_what with None -> [] | Some to_what -> to_what in
204 GrafiteAst.LApply (loc, depth, to_what, what, ident)
205 | IDENT "left" -> GrafiteAst.Left loc
206 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
207 GrafiteAst.LetIn (loc, t, where)
208 | kind = reduction_kind; p = pattern_spec ->
209 GrafiteAst.Reduce (loc, kind, p)
210 | IDENT "reflexivity" ->
211 GrafiteAst.Reflexivity loc
212 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
213 GrafiteAst.Replace (loc, p, t)
214 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
218 (HExtlib.Localized (loc,
219 (CicNotationParser.Parse_error
220 "the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion")))
222 GrafiteAst.Rewrite (loc, d, t, p)
229 | IDENT "symmetry" ->
230 GrafiteAst.Symmetry loc
231 | IDENT "transitivity"; t = tactic_term ->
232 GrafiteAst.Transitivity (loc, t)
237 [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
240 | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
243 GrafiteAst.Seq (loc, ts)
246 [ tac = SELF; SYMBOL ";";
247 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
248 (GrafiteAst.Then (loc, tac, tacs))
251 [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
252 GrafiteAst.Do (loc, count, tac)
253 | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
257 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
258 GrafiteAst.First (loc, tacs)
259 | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
261 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
262 GrafiteAst.Solve (loc, tacs)
263 | LPAREN; tac = SELF; RPAREN -> tac
264 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
267 punctuation_tactical:
269 [ SYMBOL "[" -> GrafiteAst.Branch loc
270 | SYMBOL "|" -> GrafiteAst.Shift loc
271 | i = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
272 | SYMBOL "]" -> GrafiteAst.Merge loc
273 | SYMBOL ";" -> GrafiteAst.Semicolon loc
274 | SYMBOL "." -> GrafiteAst.Dot loc
279 [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
280 | IDENT "unfocus" -> GrafiteAst.Unfocus loc
281 | IDENT "skip" -> GrafiteAst.Skip loc
282 | tac = atomic_tactical LEVEL "loops" -> tac
286 [ [ IDENT "definition" ] -> `Definition
287 | [ IDENT "fact" ] -> `Fact
288 | [ IDENT "lemma" ] -> `Lemma
289 | [ IDENT "remark" ] -> `Remark
290 | [ IDENT "theorem" ] -> `Theorem
294 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
295 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
296 fst_constructors = LIST0 constructor SEP SYMBOL "|";
299 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
300 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
301 (name, true, typ, constructors) ] SEP "with" -> types
305 (fun (names, typ) acc ->
306 (List.map (fun name -> (name, typ)) names) @ acc)
309 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
310 let tl_ind_types = match tl with None -> [] | Some types -> types in
311 let ind_types = fst_ind_type :: tl_ind_types in
316 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
317 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
320 coercion = [ SYMBOL ":" -> false | SYMBOL ":"; SYMBOL ">" -> true ] ;
321 ty = term -> (name,ty,coercion)
322 ] SEP SYMBOL ";"; SYMBOL "}" ->
325 (fun (names, typ) acc ->
326 (List.map (fun name -> (name, typ)) names) @ acc)
329 (params,name,typ,fields)
333 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
334 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
335 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
336 GrafiteAst.Undo (loc, int_opt steps)
337 | [ IDENT "redo" ]; steps = OPT NUMBER ->
338 GrafiteAst.Redo (loc, int_opt steps) *)
339 | [ IDENT "check" ]; t = term ->
340 GrafiteAst.Check (loc, t)
341 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
342 | [ IDENT "whelp"; "match" ] ; t = term ->
343 GrafiteAst.WMatch (loc,t)
344 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
345 GrafiteAst.WInstance (loc,t)
346 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
347 GrafiteAst.WLocate (loc,id)
348 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
349 GrafiteAst.WElim (loc, t)
350 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
351 GrafiteAst.WHint (loc,t)
352 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
356 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
357 let alpha = "[a-zA-Z]" in
358 let num = "[0-9]+" in
359 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
360 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
361 let rex = Str.regexp ("^"^ident^"$") in
362 if Str.string_match rex id 0 then
363 if (try ignore (UriManager.uri_of_string uri); true
364 with UriManager.IllFormedUri _ -> false)
366 LexiconAst.Ident_alias (id, uri)
369 (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
371 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
372 sprintf "Not a valid identifier: %s" id)))
373 | IDENT "symbol"; symbol = QSTRING;
374 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
375 SYMBOL "="; dsc = QSTRING ->
377 match instance with Some i -> i | None -> 0
379 LexiconAst.Symbol_alias (symbol, instance, dsc)
381 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
382 SYMBOL "="; dsc = QSTRING ->
384 match instance with Some i -> i | None -> 0
386 LexiconAst.Number_alias (instance, dsc)
390 [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
392 Ast.IdentArg (List.length l, id)
396 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
397 | IDENT "right"; IDENT "associative" -> Gramext.RightA
398 | IDENT "non"; IDENT "associative" -> Gramext.NonA
402 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
405 [ dir = OPT direction; s = QSTRING;
406 assoc = OPT associativity; prec = OPT precedence;
409 [ blob = UNPARSED_AST ->
410 add_raw_attribute ~text:(sprintf "@{%s}" blob)
411 (CicNotationParser.parse_level2_ast
412 (Ulexing.from_utf8_string blob))
413 | blob = UNPARSED_META ->
414 add_raw_attribute ~text:(sprintf "${%s}" blob)
415 (CicNotationParser.parse_level2_meta
416 (Ulexing.from_utf8_string blob))
420 | None -> default_associativity
421 | Some assoc -> assoc
425 | None -> default_precedence
429 add_raw_attribute ~text:s
430 (CicNotationParser.parse_level1_pattern
431 (Ulexing.from_utf8_string s))
433 (dir, p1, assoc, prec, p2)
437 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
438 | id = IDENT -> Ast.VarPattern id
439 | SYMBOL "_" -> Ast.ImplicitPattern
440 | LPAREN; terms = LIST1 SELF; RPAREN ->
444 | terms -> Ast.ApplPattern terms)
448 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
454 IDENT "include" ; path = QSTRING -> loc,path
458 IDENT "set"; n = QSTRING; v = QSTRING ->
459 GrafiteAst.Set (loc, n, v)
460 | IDENT "drop" -> GrafiteAst.Drop loc
461 | IDENT "qed" -> GrafiteAst.Qed loc
462 | IDENT "variant" ; name = IDENT; SYMBOL ":";
463 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
466 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
467 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
468 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
469 GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
470 | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
473 Ast.Theorem (flavour, name, Ast.Implicit, Some body))
474 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
475 defs = CicNotationParser.let_defs ->
478 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
479 | ((Ast.Ident (name, None), None),_,_) :: _ ->
483 let body = Ast.Ident (name,None) in
484 GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
485 Some (Ast.LetRec (ind_kind, defs, body))))
486 | IDENT "inductive"; spec = inductive_spec ->
487 let (params, ind_types) = spec in
488 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
489 | IDENT "coinductive"; spec = inductive_spec ->
490 let (params, ind_types) = spec in
491 let ind_types = (* set inductive flags to false (coinductive) *)
492 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
495 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
496 | IDENT "coercion" ; suri = URI ->
497 GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true)
498 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
499 GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
500 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
501 let uris = List.map UriManager.uri_of_string uris in
502 GrafiteAst.Default (loc,what,uris)
505 IDENT "alias" ; spec = alias_spec ->
506 LexiconAst.Alias (loc, spec)
507 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
508 LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
509 | IDENT "interpretation"; id = QSTRING;
510 (symbol, args, l3) = interpretation ->
511 LexiconAst.Interpretation (loc, id, (symbol, args), l3)
514 [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
515 | tac = tactical; punct = punctuation_tactical ->
516 GrafiteAst.Tactical (loc, tac, Some punct)
517 | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
518 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
522 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
523 GrafiteAst.Code (loc, ex)
525 GrafiteAst.Note (loc, str)
530 fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
532 fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
533 | (iloc,fname) = include_command ; SYMBOL "." ->
534 fun ~include_paths status ->
535 let path = DependenciesParser.baseuri_of_script ~include_paths fname in
537 LexiconEngine.eval_command status (LexiconAst.Include (iloc,path))
541 (GrafiteAst.Executable
542 (loc,GrafiteAst.Command
543 (loc,GrafiteAst.Include (iloc,path))))
544 | scom = lexicon_command ; SYMBOL "." ->
545 fun ~include_paths status ->
546 let status = LexiconEngine.eval_command status scom in
548 | EOI -> raise End_of_file
553 let exc_located_wrapper f =
557 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
558 | Stdpp.Exc_located (floc, Stream.Error msg) ->
559 raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
560 | Stdpp.Exc_located (floc, exn) ->
562 (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
564 let parse_statement lexbuf =
566 (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))