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"
32 let statements = Grammar.Entry.create grammar "statements"
35 GLOBAL: term statement statements;
37 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
38 SYMBOL ":"; ty = term; RPAREN -> names,ty
39 | name = IDENT -> [name],CicNotationPt.Implicit
42 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
43 tactic_term: [ [ t = term -> t ] ];
45 [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
48 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
51 [ IDENT "reduce" -> `Reduce
52 | IDENT "simplify" -> `Simpl
54 | IDENT "normalize" -> `Normalize ]
56 sequent_pattern_spec: [
60 path = OPT [SYMBOL ":" ; path = term -> path ] ->
61 (id,match path with Some p -> p | None -> CicNotationPt.UserInput) ]
63 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
66 None -> CicNotationPt.UserInput
67 | Some goal_path -> goal_path
76 [ "match" ; wanted = term ;
77 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
79 | sps = sequent_pattern_spec ->
82 let wanted,hyp_paths,goal_path =
83 match wanted_and_sps with
84 wanted,None -> wanted, [], CicNotationPt.UserInput
85 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
87 wanted, hyp_paths, goal_path ] ->
89 None -> None,[],CicNotationPt.UserInput
93 [ SYMBOL ">" -> `LeftToRight
94 | SYMBOL "<" -> `RightToLeft ]
96 int: [ [ num = NUMBER -> int_of_string num ] ];
98 [ IDENT "absurd"; t = tactic_term ->
99 GrafiteAst.Absurd (loc, t)
100 | IDENT "apply"; t = tactic_term ->
101 GrafiteAst.Apply (loc, t)
102 | IDENT "assumption" ->
103 GrafiteAst.Assumption loc
105 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
106 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ] ->
107 GrafiteAst.Auto (loc,depth,width)
108 | IDENT "clear"; id = IDENT ->
109 GrafiteAst.Clear (loc,id)
110 | IDENT "clearbody"; id = IDENT ->
111 GrafiteAst.ClearBody (loc,id)
112 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
113 GrafiteAst.Change (loc, what, t)
114 | IDENT "compare"; t = tactic_term ->
115 GrafiteAst.Compare (loc,t)
116 | IDENT "constructor"; n = int ->
117 GrafiteAst.Constructor (loc, n)
118 | IDENT "contradiction" ->
119 GrafiteAst.Contradiction loc
120 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
121 GrafiteAst.Cut (loc, ident, t)
122 | IDENT "decide"; IDENT "equality" ->
123 GrafiteAst.DecideEquality loc
124 | IDENT "decompose"; where = tactic_term ->
125 GrafiteAst.Decompose (loc, where)
126 | IDENT "discriminate"; t = tactic_term ->
127 GrafiteAst.Discriminate (loc, t)
128 | IDENT "elim"; what = tactic_term;
129 using = OPT [ "using"; using = tactic_term -> using ];
130 OPT "names"; num = OPT [num = int -> num];
131 idents = OPT ident_list0 ->
132 let idents = match idents with None -> [] | Some idents -> idents in
133 GrafiteAst.Elim (loc, what, using, num, idents)
134 | IDENT "elimType"; what = tactic_term;
135 using = OPT [ "using"; using = tactic_term -> using ];
136 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
137 let idents = match idents with None -> [] | Some idents -> idents in
138 GrafiteAst.ElimType (loc, what, using, num, idents)
139 | IDENT "exact"; t = tactic_term ->
140 GrafiteAst.Exact (loc, t)
142 GrafiteAst.Exists loc
143 | IDENT "fail" -> GrafiteAst.Fail loc
144 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
147 raise (CicNotationParser.Parse_error
148 (loc, "the pattern cannot specify the term to replace, only its"
149 ^ " paths in the hypotheses and in the conclusion"))
151 GrafiteAst.Fold (loc, kind, t, p)
153 GrafiteAst.Fourier loc
154 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
155 let idents = match idents with None -> [] | Some idents -> idents in
156 GrafiteAst.FwdSimpl (loc, hyp, idents)
157 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
158 GrafiteAst.Generalize (loc,p,id)
159 | IDENT "goal"; n = int ->
160 GrafiteAst.Goal (loc, n)
161 | IDENT "id" -> GrafiteAst.IdTac loc
162 | IDENT "injection"; t = tactic_term ->
163 GrafiteAst.Injection (loc, t)
164 | IDENT "intro"; ident = OPT IDENT ->
165 let idents = match ident with None -> [] | Some id -> [id] in
166 GrafiteAst.Intros (loc, Some 1, idents)
167 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
168 let idents = match idents with None -> [] | Some idents -> idents in
169 GrafiteAst.Intros (loc, num, idents)
171 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
173 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
174 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
175 let to_what = match to_what with None -> [] | Some to_what -> to_what in
176 GrafiteAst.LApply (loc, depth, to_what, what, ident)
177 | IDENT "left" -> GrafiteAst.Left loc
178 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
179 GrafiteAst.LetIn (loc, t, where)
180 | kind = reduction_kind; p = pattern_spec ->
181 GrafiteAst.Reduce (loc, kind, p)
182 | IDENT "reflexivity" ->
183 GrafiteAst.Reflexivity loc
184 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
185 GrafiteAst.Replace (loc, p, t)
186 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
190 (CicNotationParser.Parse_error
191 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
193 GrafiteAst.Rewrite (loc, d, t, p)
200 | IDENT "symmetry" ->
201 GrafiteAst.Symmetry loc
202 | IDENT "transitivity"; t = tactic_term ->
203 GrafiteAst.Transitivity (loc, t)
208 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
212 | l -> GrafiteAst.Seq (loc, l)
215 [ tac = tactical; SYMBOL ";";
216 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
217 (GrafiteAst.Then (loc, tac, tacs))
220 [ IDENT "do"; count = int; tac = tactical ->
221 GrafiteAst.Do (loc, count, tac)
222 | IDENT "repeat"; tac = tactical ->
223 GrafiteAst.Repeat (loc, tac)
227 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
228 GrafiteAst.First (loc, tacs)
229 | IDENT "try"; tac = NEXT ->
230 GrafiteAst.Try (loc, tac)
232 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
233 GrafiteAst.Solve (loc, tacs)
234 | LPAREN; tac = tactical; RPAREN -> tac
235 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
239 [ [ IDENT "definition" ] -> `Definition
240 | [ IDENT "fact" ] -> `Fact
241 | [ IDENT "lemma" ] -> `Lemma
242 | [ IDENT "remark" ] -> `Remark
243 | [ IDENT "theorem" ] -> `Theorem
247 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
248 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
249 fst_constructors = LIST0 constructor SEP SYMBOL "|";
252 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
253 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
254 (name, true, typ, constructors) ] SEP "with" -> types
258 (fun (names, typ) acc ->
259 (List.map (fun name -> (name, typ)) names) @ acc)
262 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
263 let tl_ind_types = match tl with None -> [] | Some types -> types in
264 let ind_types = fst_ind_type :: tl_ind_types in
269 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
270 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
272 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
273 ] SEP SYMBOL ";"; SYMBOL "}" ->
276 (fun (names, typ) acc ->
277 (List.map (fun name -> (name, typ)) names) @ acc)
280 (params,name,typ,fields)
284 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
285 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
286 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
287 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
288 GrafiteAst.Undo (loc, int_opt steps)
289 | [ IDENT "redo" ]; steps = OPT NUMBER ->
290 GrafiteAst.Redo (loc, int_opt steps) *)
291 | [ IDENT "check" ]; t = term ->
292 GrafiteAst.Check (loc, t)
293 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
294 | [ IDENT "whelp"; "match" ] ; t = term ->
295 GrafiteAst.WMatch (loc,t)
296 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
297 GrafiteAst.WInstance (loc,t)
298 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
299 GrafiteAst.WLocate (loc,id)
300 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
301 GrafiteAst.WElim (loc, t)
302 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
303 GrafiteAst.WHint (loc,t)
304 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
308 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
309 let alpha = "[a-zA-Z]" in
310 let num = "[0-9]+" in
311 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
312 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
313 let rex = Str.regexp ("^"^ident^"$") in
314 if Str.string_match rex id 0 then
315 if (try ignore (UriManager.uri_of_string uri); true
316 with UriManager.IllFormedUri _ -> false)
318 GrafiteAst.Ident_alias (id, uri)
320 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
322 raise (CicNotationParser.Parse_error (loc,
323 sprintf "Not a valid identifier: %s" id))
324 | IDENT "symbol"; symbol = QSTRING;
325 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
326 SYMBOL "="; dsc = QSTRING ->
328 match instance with Some i -> i | None -> 0
330 GrafiteAst.Symbol_alias (symbol, instance, dsc)
332 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
333 SYMBOL "="; dsc = QSTRING ->
335 match instance with Some i -> i | None -> 0
337 GrafiteAst.Number_alias (instance, dsc)
341 [ id = IDENT -> GrafiteAst.IdentArg (0, id)
342 | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
343 SYMBOL "."; id = IDENT ->
344 GrafiteAst.IdentArg (List.length l, id)
348 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
349 | IDENT "right"; IDENT "associative" -> Gramext.RightA
350 | IDENT "non"; IDENT "associative" -> Gramext.NonA
354 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
358 assoc = OPT associativity; prec = OPT precedence;
361 [ blob = UNPARSED_AST ->
362 CicNotationParser.parse_level2_ast (Stream.of_string blob)
363 | blob = UNPARSED_META ->
364 CicNotationParser.parse_level2_meta (Stream.of_string blob) ]
366 (CicNotationParser.parse_level1_pattern (Stream.of_string s), assoc, prec, p2)
370 [ u = URI -> GrafiteAst.UriPattern (UriManager.uri_of_string u)
371 | id = IDENT -> GrafiteAst.VarPattern id
372 | LPAREN; terms = LIST1 SELF; RPAREN ->
376 | terms -> GrafiteAst.ApplPattern terms)
380 [ s = CSYMBOL; args = LIST1 argument; SYMBOL "="; t = level3_term ->
385 IDENT "set"; n = QSTRING; v = QSTRING ->
386 GrafiteAst.Set (loc, n, v)
387 | IDENT "drop" -> GrafiteAst.Drop loc
388 | IDENT "qed" -> GrafiteAst.Qed loc
389 | IDENT "variant" ; name = IDENT; SYMBOL ":";
390 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
393 (`Variant,name,typ,Some (CicNotationPt.Ident (newname, None))))
394 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
395 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
396 GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
397 | flavour = theorem_flavour; name = IDENT;
398 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
400 GrafiteAst.Theorem (flavour, name, CicNotationPt.Implicit, body))
401 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
402 defs = CicNotationParser.let_defs ->
405 | ((CicNotationPt.Ident (name, None), Some ty),_,_) :: _ -> name,ty
406 | ((CicNotationPt.Ident (name, None), None),_,_) :: _ ->
407 name, CicNotationPt.Implicit
410 let body = CicNotationPt.Ident (name,None) in
411 GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
412 Some (CicNotationPt.LetRec (ind_kind, defs, body))))
413 | [ IDENT "inductive" ]; spec = inductive_spec ->
414 let (params, ind_types) = spec in
415 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
416 | [ IDENT "coinductive" ]; spec = inductive_spec ->
417 let (params, ind_types) = spec in
418 let ind_types = (* set inductive flags to false (coinductive) *)
419 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
422 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
423 | IDENT "coercion" ; name = IDENT ->
424 GrafiteAst.Coercion (loc, CicNotationPt.Ident (name,Some []))
425 | IDENT "coercion" ; name = URI ->
426 GrafiteAst.Coercion (loc, CicNotationPt.Uri (name,Some []))
427 | IDENT "alias" ; spec = alias_spec ->
428 GrafiteAst.Alias (loc, spec)
429 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
430 GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
431 | IDENT "include" ; path = QSTRING ->
432 GrafiteAst.Include (loc,path)
433 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
434 let uris = List.map UriManager.uri_of_string uris in
435 GrafiteAst.Default (loc,what,uris)
436 | IDENT "notation"; (l1, assoc, prec, l2) = notation ->
437 GrafiteAst.Notation (loc, l1, assoc, prec, l2)
438 | IDENT "interpretation"; (symbol, args, l3) = interpretation ->
439 GrafiteAst.Interpretation (loc, (symbol, args), l3)
441 | IDENT "dump" -> GrafiteAst.Dump loc
442 | IDENT "render"; u = URI -> GrafiteAst.Render (loc, UriManager.uri_of_string u)
445 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
446 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
447 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
451 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
452 GrafiteAst.Code (loc, ex)
454 GrafiteAst.Note (loc, str)
458 [ ex = executable -> GrafiteAst.Executable (loc,ex)
459 | com = comment -> GrafiteAst.Comment (loc, com)
463 [ l = LIST0 statement ; EOI -> l
468 let exc_located_wrapper f =
472 | Stdpp.Exc_located (floc, Stream.Error msg) ->
473 raise (CicNotationParser.Parse_error (floc, msg))
474 | Stdpp.Exc_located (floc, exn) ->
475 raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
477 let parse_statement stream =
478 exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
479 let parse_statements stream =
480 exc_located_wrapper (fun () -> (Grammar.Entry.parse statements stream))