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 let grammar = CicNotationParser.level2_ast_grammar
30 let term = CicNotationParser.term
31 let statement = Grammar.Entry.create grammar "statement"
33 let add_raw_attribute ~text t = CicNotationPt.AttributedTerm (`Raw text, t)
35 let default_precedence = 50
36 let default_associativity = Gramext.NonA
39 GLOBAL: term statement;
41 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
42 SYMBOL ":"; ty = term; RPAREN -> names,ty
43 | name = IDENT -> [name],CicNotationPt.Implicit
46 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
47 tactic_term: [ [ t = term -> t ] ];
49 [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
52 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
55 [ IDENT "reduce" -> `Reduce
56 | IDENT "simplify" -> `Simpl
58 | IDENT "normalize" -> `Normalize ]
60 sequent_pattern_spec: [
64 path = OPT [SYMBOL ":" ; path = term -> path ] ->
65 (id,match path with Some p -> p | None -> CicNotationPt.UserInput) ]
67 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
70 None -> CicNotationPt.UserInput
71 | Some goal_path -> goal_path
80 [ "match" ; wanted = term ;
81 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
83 | sps = sequent_pattern_spec ->
86 let wanted,hyp_paths,goal_path =
87 match wanted_and_sps with
88 wanted,None -> wanted, [], CicNotationPt.UserInput
89 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
91 wanted, hyp_paths, goal_path ] ->
93 None -> None,[],CicNotationPt.UserInput
97 [ SYMBOL ">" -> `LeftToRight
98 | SYMBOL "<" -> `RightToLeft ]
100 int: [ [ num = NUMBER -> int_of_string num ] ];
102 [ IDENT "absurd"; t = tactic_term ->
103 GrafiteAst.Absurd (loc, t)
104 | IDENT "apply"; t = tactic_term ->
105 GrafiteAst.Apply (loc, t)
106 | IDENT "assumption" ->
107 GrafiteAst.Assumption loc
109 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
110 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ] ->
111 GrafiteAst.Auto (loc,depth,width)
112 | IDENT "clear"; id = IDENT ->
113 GrafiteAst.Clear (loc,id)
114 | IDENT "clearbody"; id = IDENT ->
115 GrafiteAst.ClearBody (loc,id)
116 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
117 GrafiteAst.Change (loc, what, t)
118 | IDENT "compare"; t = tactic_term ->
119 GrafiteAst.Compare (loc,t)
120 | IDENT "constructor"; n = int ->
121 GrafiteAst.Constructor (loc, n)
122 | IDENT "contradiction" ->
123 GrafiteAst.Contradiction loc
124 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
125 GrafiteAst.Cut (loc, ident, t)
126 | IDENT "decide"; IDENT "equality" ->
127 GrafiteAst.DecideEquality loc
128 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
129 OPT "names"; num = OPT [num = int -> num];
130 idents = OPT ident_list0 ->
131 let idents = match idents with None -> [] | Some idents -> idents in
132 let types = match types with None -> [] | Some types -> types in
133 let to_spec id = GrafiteAst.Ident id in
134 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
135 | IDENT "discriminate"; t = tactic_term ->
136 GrafiteAst.Discriminate (loc, t)
137 | IDENT "elim"; what = tactic_term;
138 using = OPT [ "using"; using = tactic_term -> using ];
139 OPT "names"; num = OPT [num = int -> num];
140 idents = OPT ident_list0 ->
141 let idents = match idents with None -> [] | Some idents -> idents in
142 GrafiteAst.Elim (loc, what, using, num, idents)
143 | IDENT "elimType"; what = tactic_term;
144 using = OPT [ "using"; using = tactic_term -> using ];
145 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
146 let idents = match idents with None -> [] | Some idents -> idents in
147 GrafiteAst.ElimType (loc, what, using, num, idents)
148 | IDENT "exact"; t = tactic_term ->
149 GrafiteAst.Exact (loc, t)
151 GrafiteAst.Exists loc
152 | IDENT "fail" -> GrafiteAst.Fail loc
153 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
156 raise (CicNotationParser.Parse_error
157 (loc, "the pattern cannot specify the term to replace, only its"
158 ^ " paths in the hypotheses and in the conclusion"))
160 GrafiteAst.Fold (loc, kind, t, p)
162 GrafiteAst.Fourier loc
163 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
164 let idents = match idents with None -> [] | Some idents -> idents in
165 GrafiteAst.FwdSimpl (loc, hyp, idents)
166 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
167 GrafiteAst.Generalize (loc,p,id)
168 | IDENT "goal"; n = int ->
169 GrafiteAst.Goal (loc, n)
170 | IDENT "id" -> GrafiteAst.IdTac loc
171 | IDENT "injection"; t = tactic_term ->
172 GrafiteAst.Injection (loc, t)
173 | IDENT "intro"; ident = OPT IDENT ->
174 let idents = match ident with None -> [] | Some id -> [id] in
175 GrafiteAst.Intros (loc, Some 1, idents)
176 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
177 let idents = match idents with None -> [] | Some idents -> idents in
178 GrafiteAst.Intros (loc, num, idents)
180 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
182 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
183 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
184 let to_what = match to_what with None -> [] | Some to_what -> to_what in
185 GrafiteAst.LApply (loc, depth, to_what, what, ident)
186 | IDENT "left" -> GrafiteAst.Left loc
187 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
188 GrafiteAst.LetIn (loc, t, where)
189 | kind = reduction_kind; p = pattern_spec ->
190 GrafiteAst.Reduce (loc, kind, p)
191 | IDENT "reflexivity" ->
192 GrafiteAst.Reflexivity loc
193 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
194 GrafiteAst.Replace (loc, p, t)
195 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
199 (CicNotationParser.Parse_error
200 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
202 GrafiteAst.Rewrite (loc, d, t, p)
209 | IDENT "symmetry" ->
210 GrafiteAst.Symmetry loc
211 | IDENT "transitivity"; t = tactic_term ->
212 GrafiteAst.Transitivity (loc, t)
217 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
221 | l -> GrafiteAst.Seq (loc, l)
224 [ tac = tactical; SYMBOL ";";
225 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
226 (GrafiteAst.Then (loc, tac, tacs))
229 [ IDENT "do"; count = int; tac = tactical ->
230 GrafiteAst.Do (loc, count, tac)
231 | IDENT "repeat"; tac = tactical ->
232 GrafiteAst.Repeat (loc, tac)
236 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
237 GrafiteAst.First (loc, tacs)
238 | IDENT "try"; tac = NEXT ->
239 GrafiteAst.Try (loc, tac)
241 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
242 GrafiteAst.Solve (loc, tacs)
243 | LPAREN; tac = tactical; RPAREN -> tac
244 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
248 [ [ IDENT "definition" ] -> `Definition
249 | [ IDENT "fact" ] -> `Fact
250 | [ IDENT "lemma" ] -> `Lemma
251 | [ IDENT "remark" ] -> `Remark
252 | [ IDENT "theorem" ] -> `Theorem
256 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
257 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
258 fst_constructors = LIST0 constructor SEP SYMBOL "|";
261 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
262 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
263 (name, true, typ, constructors) ] SEP "with" -> types
267 (fun (names, typ) acc ->
268 (List.map (fun name -> (name, typ)) names) @ acc)
271 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
272 let tl_ind_types = match tl with None -> [] | Some types -> types in
273 let ind_types = fst_ind_type :: tl_ind_types in
278 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
279 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
281 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
282 ] SEP SYMBOL ";"; SYMBOL "}" ->
285 (fun (names, typ) acc ->
286 (List.map (fun name -> (name, typ)) names) @ acc)
289 (params,name,typ,fields)
293 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
294 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
295 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
296 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
297 GrafiteAst.Undo (loc, int_opt steps)
298 | [ IDENT "redo" ]; steps = OPT NUMBER ->
299 GrafiteAst.Redo (loc, int_opt steps) *)
300 | [ IDENT "check" ]; t = term ->
301 GrafiteAst.Check (loc, t)
302 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
303 | [ IDENT "whelp"; "match" ] ; t = term ->
304 GrafiteAst.WMatch (loc,t)
305 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
306 GrafiteAst.WInstance (loc,t)
307 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
308 GrafiteAst.WLocate (loc,id)
309 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
310 GrafiteAst.WElim (loc, t)
311 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
312 GrafiteAst.WHint (loc,t)
313 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
317 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
318 let alpha = "[a-zA-Z]" in
319 let num = "[0-9]+" in
320 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
321 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
322 let rex = Str.regexp ("^"^ident^"$") in
323 if Str.string_match rex id 0 then
324 if (try ignore (UriManager.uri_of_string uri); true
325 with UriManager.IllFormedUri _ -> false)
327 GrafiteAst.Ident_alias (id, uri)
329 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
331 raise (CicNotationParser.Parse_error (loc,
332 sprintf "Not a valid identifier: %s" id))
333 | IDENT "symbol"; symbol = QSTRING;
334 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
335 SYMBOL "="; dsc = QSTRING ->
337 match instance with Some i -> i | None -> 0
339 GrafiteAst.Symbol_alias (symbol, instance, dsc)
341 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
342 SYMBOL "="; dsc = QSTRING ->
344 match instance with Some i -> i | None -> 0
346 GrafiteAst.Number_alias (instance, dsc)
350 [ id = IDENT -> CicNotationPt.IdentArg (0, id)
351 | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
352 SYMBOL "."; id = IDENT ->
353 CicNotationPt.IdentArg (List.length l, id)
357 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
358 | IDENT "right"; IDENT "associative" -> Gramext.RightA
359 | IDENT "non"; IDENT "associative" -> Gramext.NonA
363 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
367 assoc = OPT associativity; prec = OPT precedence;
370 [ blob = UNPARSED_AST ->
371 add_raw_attribute ~text:(sprintf "@{%s}" blob)
372 (CicNotationParser.parse_level2_ast (Stream.of_string blob))
373 | blob = UNPARSED_META ->
374 add_raw_attribute ~text:(sprintf "${%s}" blob)
375 (CicNotationParser.parse_level2_meta (Stream.of_string blob))
379 | None -> default_associativity
380 | Some assoc -> assoc
384 | None -> default_precedence
387 (add_raw_attribute ~text:s
388 (CicNotationParser.parse_level1_pattern (Stream.of_string s)),
393 [ u = URI -> CicNotationPt.UriPattern (UriManager.uri_of_string u)
394 | id = IDENT -> CicNotationPt.VarPattern id
395 | SYMBOL "_" -> CicNotationPt.ImplicitPattern
396 | LPAREN; terms = LIST1 SELF; RPAREN ->
400 | terms -> CicNotationPt.ApplPattern terms)
404 [ s = CSYMBOL; args = LIST1 argument; SYMBOL "="; t = level3_term ->
409 IDENT "set"; n = QSTRING; v = QSTRING ->
410 GrafiteAst.Set (loc, n, v)
411 | IDENT "drop" -> GrafiteAst.Drop loc
412 | IDENT "qed" -> GrafiteAst.Qed loc
413 | IDENT "variant" ; name = IDENT; SYMBOL ":";
414 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
417 (`Variant,name,typ,Some (CicNotationPt.Ident (newname, None))))
418 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
419 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
420 GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
421 | flavour = theorem_flavour; name = IDENT;
422 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
424 GrafiteAst.Theorem (flavour, name, CicNotationPt.Implicit, body))
425 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
426 defs = CicNotationParser.let_defs ->
429 | ((CicNotationPt.Ident (name, None), Some ty),_,_) :: _ -> name,ty
430 | ((CicNotationPt.Ident (name, None), None),_,_) :: _ ->
431 name, CicNotationPt.Implicit
434 let body = CicNotationPt.Ident (name,None) in
435 GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
436 Some (CicNotationPt.LetRec (ind_kind, defs, body))))
437 | [ IDENT "inductive" ]; spec = inductive_spec ->
438 let (params, ind_types) = spec in
439 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
440 | [ IDENT "coinductive" ]; spec = inductive_spec ->
441 let (params, ind_types) = spec in
442 let ind_types = (* set inductive flags to false (coinductive) *)
443 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
446 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
447 | IDENT "coercion" ; name = IDENT ->
448 GrafiteAst.Coercion (loc, CicNotationPt.Ident (name,Some []))
449 | IDENT "coercion" ; name = URI ->
450 GrafiteAst.Coercion (loc, CicNotationPt.Uri (name,Some []))
451 | IDENT "alias" ; spec = alias_spec ->
452 GrafiteAst.Alias (loc, spec)
453 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
454 GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
455 | IDENT "include" ; path = QSTRING ->
456 GrafiteAst.Include (loc,path)
457 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
458 let uris = List.map UriManager.uri_of_string uris in
459 GrafiteAst.Default (loc,what,uris)
460 | IDENT "notation"; (l1, assoc, prec, l2) = notation ->
461 GrafiteAst.Notation (loc, l1, assoc, prec, l2)
462 | IDENT "interpretation"; id = QSTRING;
463 (symbol, args, l3) = interpretation ->
464 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
466 | IDENT "dump" -> GrafiteAst.Dump loc
467 | IDENT "render"; u = URI ->
468 GrafiteAst.Render (loc, UriManager.uri_of_string u)
471 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
472 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
473 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
477 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
478 GrafiteAst.Code (loc, ex)
480 GrafiteAst.Note (loc, str)
484 [ ex = executable -> GrafiteAst.Executable (loc,ex)
485 | com = comment -> GrafiteAst.Comment (loc, com)
486 | EOI -> raise End_of_file
491 let exc_located_wrapper f =
495 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
496 | Stdpp.Exc_located (floc, Stream.Error msg) ->
497 raise (CicNotationParser.Parse_error (floc, msg))
498 | Stdpp.Exc_located (floc, exn) ->
499 raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
501 let parse_statement stream =
502 exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))