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"
34 GLOBAL: term statement;
36 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
37 SYMBOL ":"; ty = term; RPAREN -> names,ty
38 | name = IDENT -> [name],CicNotationPt.Implicit
41 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
42 tactic_term: [ [ t = term -> t ] ];
44 [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
47 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
50 [ IDENT "reduce" -> `Reduce
51 | IDENT "simplify" -> `Simpl
53 | IDENT "normalize" -> `Normalize ]
55 sequent_pattern_spec: [
59 path = OPT [SYMBOL ":" ; path = term -> path ] ->
60 (id,match path with Some p -> p | None -> CicNotationPt.UserInput) ]
62 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
65 None -> CicNotationPt.UserInput
66 | Some goal_path -> goal_path
75 [ "match" ; wanted = term ;
76 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
78 | sps = sequent_pattern_spec ->
81 let wanted,hyp_paths,goal_path =
82 match wanted_and_sps with
83 wanted,None -> wanted, [], CicNotationPt.UserInput
84 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
86 wanted, hyp_paths, goal_path ] ->
88 None -> None,[],CicNotationPt.UserInput
92 [ SYMBOL ">" -> `LeftToRight
93 | SYMBOL "<" -> `RightToLeft ]
95 int: [ [ num = NUMBER -> int_of_string num ] ];
97 [ IDENT "absurd"; t = tactic_term ->
98 GrafiteAst.Absurd (loc, t)
99 | IDENT "apply"; t = tactic_term ->
100 GrafiteAst.Apply (loc, t)
101 | IDENT "assumption" ->
102 GrafiteAst.Assumption loc
104 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
105 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ] ->
106 GrafiteAst.Auto (loc,depth,width)
107 | IDENT "clear"; id = IDENT ->
108 GrafiteAst.Clear (loc,id)
109 | IDENT "clearbody"; id = IDENT ->
110 GrafiteAst.ClearBody (loc,id)
111 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
112 GrafiteAst.Change (loc, what, t)
113 | IDENT "compare"; t = tactic_term ->
114 GrafiteAst.Compare (loc,t)
115 | IDENT "constructor"; n = int ->
116 GrafiteAst.Constructor (loc, n)
117 | IDENT "contradiction" ->
118 GrafiteAst.Contradiction loc
119 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
120 GrafiteAst.Cut (loc, ident, t)
121 | IDENT "decide"; IDENT "equality" ->
122 GrafiteAst.DecideEquality loc
123 | IDENT "decompose"; where = tactic_term ->
124 GrafiteAst.Decompose (loc, where)
125 | IDENT "discriminate"; t = tactic_term ->
126 GrafiteAst.Discriminate (loc, t)
127 | IDENT "elim"; what = tactic_term;
128 using = OPT [ "using"; using = tactic_term -> using ];
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 GrafiteAst.Elim (loc, what, using, num, idents)
133 | IDENT "elimType"; what = tactic_term;
134 using = OPT [ "using"; using = tactic_term -> using ];
135 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
136 let idents = match idents with None -> [] | Some idents -> idents in
137 GrafiteAst.ElimType (loc, what, using, num, idents)
138 | IDENT "exact"; t = tactic_term ->
139 GrafiteAst.Exact (loc, t)
141 GrafiteAst.Exists loc
142 | IDENT "fail" -> GrafiteAst.Fail loc
143 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
146 raise (CicNotationParser.Parse_error
147 (loc, "the pattern cannot specify the term to replace, only its"
148 ^ " paths in the hypotheses and in the conclusion"))
150 GrafiteAst.Fold (loc, kind, t, p)
152 GrafiteAst.Fourier loc
153 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
154 let idents = match idents with None -> [] | Some idents -> idents in
155 GrafiteAst.FwdSimpl (loc, hyp, idents)
156 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
157 GrafiteAst.Generalize (loc,p,id)
158 | IDENT "goal"; n = int ->
159 GrafiteAst.Goal (loc, n)
160 | IDENT "id" -> GrafiteAst.IdTac loc
161 | IDENT "injection"; t = tactic_term ->
162 GrafiteAst.Injection (loc, t)
163 | IDENT "intro"; ident = OPT IDENT ->
164 let idents = match ident with None -> [] | Some id -> [id] in
165 GrafiteAst.Intros (loc, Some 1, idents)
166 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
167 let idents = match idents with None -> [] | Some idents -> idents in
168 GrafiteAst.Intros (loc, num, idents)
170 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
172 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
173 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
174 let to_what = match to_what with None -> [] | Some to_what -> to_what in
175 GrafiteAst.LApply (loc, depth, to_what, what, ident)
176 | IDENT "left" -> GrafiteAst.Left loc
177 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
178 GrafiteAst.LetIn (loc, t, where)
179 | kind = reduction_kind; p = pattern_spec ->
180 GrafiteAst.Reduce (loc, kind, p)
181 | IDENT "reflexivity" ->
182 GrafiteAst.Reflexivity loc
183 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
184 GrafiteAst.Replace (loc, p, t)
185 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
189 (CicNotationParser.Parse_error
190 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
192 GrafiteAst.Rewrite (loc, d, t, p)
199 | IDENT "symmetry" ->
200 GrafiteAst.Symmetry loc
201 | IDENT "transitivity"; t = tactic_term ->
202 GrafiteAst.Transitivity (loc, t)
207 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
211 | l -> GrafiteAst.Seq (loc, l)
214 [ tac = tactical; SYMBOL ";";
215 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
216 (GrafiteAst.Then (loc, tac, tacs))
219 [ IDENT "do"; count = int; tac = tactical ->
220 GrafiteAst.Do (loc, count, tac)
221 | IDENT "repeat"; tac = tactical ->
222 GrafiteAst.Repeat (loc, tac)
226 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
227 GrafiteAst.First (loc, tacs)
228 | IDENT "try"; tac = NEXT ->
229 GrafiteAst.Try (loc, tac)
231 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
232 GrafiteAst.Solve (loc, tacs)
233 | LPAREN; tac = tactical; RPAREN -> tac
234 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
238 [ [ IDENT "definition" ] -> `Definition
239 | [ IDENT "fact" ] -> `Fact
240 | [ IDENT "lemma" ] -> `Lemma
241 | [ IDENT "remark" ] -> `Remark
242 | [ IDENT "theorem" ] -> `Theorem
246 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
247 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
248 fst_constructors = LIST0 constructor SEP SYMBOL "|";
251 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
252 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
253 (name, true, typ, constructors) ] SEP "with" -> types
257 (fun (names, typ) acc ->
258 (List.map (fun name -> (name, typ)) names) @ acc)
261 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
262 let tl_ind_types = match tl with None -> [] | Some types -> types in
263 let ind_types = fst_ind_type :: tl_ind_types in
268 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
269 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
271 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
272 ] SEP SYMBOL ";"; SYMBOL "}" ->
275 (fun (names, typ) acc ->
276 (List.map (fun name -> (name, typ)) names) @ acc)
279 (params,name,typ,fields)
283 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
284 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
285 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
286 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
287 GrafiteAst.Undo (loc, int_opt steps)
288 | [ IDENT "redo" ]; steps = OPT NUMBER ->
289 GrafiteAst.Redo (loc, int_opt steps) *)
290 | [ IDENT "check" ]; t = term ->
291 GrafiteAst.Check (loc, t)
292 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
293 | [ IDENT "whelp"; "match" ] ; t = term ->
294 GrafiteAst.WMatch (loc,t)
295 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
296 GrafiteAst.WInstance (loc,t)
297 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
298 GrafiteAst.WLocate (loc,id)
299 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
300 GrafiteAst.WElim (loc, t)
301 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
302 GrafiteAst.WHint (loc,t)
303 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
307 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
308 let alpha = "[a-zA-Z]" in
309 let num = "[0-9]+" in
310 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
311 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
312 let rex = Str.regexp ("^"^ident^"$") in
313 if Str.string_match rex id 0 then
314 if (try ignore (UriManager.uri_of_string uri); true
315 with UriManager.IllFormedUri _ -> false)
317 GrafiteAst.Ident_alias (id, uri)
319 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
321 raise (CicNotationParser.Parse_error (loc,
322 sprintf "Not a valid identifier: %s" id))
323 | IDENT "symbol"; symbol = QSTRING;
324 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
325 SYMBOL "="; dsc = QSTRING ->
327 match instance with Some i -> i | None -> 0
329 GrafiteAst.Symbol_alias (symbol, instance, dsc)
331 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
332 SYMBOL "="; dsc = QSTRING ->
334 match instance with Some i -> i | None -> 0
336 GrafiteAst.Number_alias (instance, dsc)
340 [ id = IDENT -> CicNotationPt.IdentArg (0, id)
341 | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
342 SYMBOL "."; id = IDENT ->
343 CicNotationPt.IdentArg (List.length l, id)
347 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
348 | IDENT "right"; IDENT "associative" -> Gramext.RightA
349 | IDENT "non"; IDENT "associative" -> Gramext.NonA
353 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
357 assoc = OPT associativity; prec = OPT precedence;
360 [ blob = UNPARSED_AST ->
361 CicNotationParser.parse_level2_ast (Stream.of_string blob)
362 | blob = UNPARSED_META ->
363 CicNotationParser.parse_level2_meta (Stream.of_string blob) ]
365 (CicNotationParser.parse_level1_pattern (Stream.of_string s), assoc, prec, p2)
369 [ u = URI -> CicNotationPt.UriPattern (UriManager.uri_of_string u)
370 | id = IDENT -> CicNotationPt.VarPattern id
371 | SYMBOL "_" -> CicNotationPt.ImplicitPattern
372 | LPAREN; terms = LIST1 SELF; RPAREN ->
376 | terms -> CicNotationPt.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"; id = QSTRING;
439 (symbol, args, l3) = interpretation ->
440 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
442 | IDENT "dump" -> GrafiteAst.Dump loc
443 | IDENT "render"; u = URI ->
444 GrafiteAst.Render (loc, UriManager.uri_of_string u)
447 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
448 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
449 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
453 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
454 GrafiteAst.Code (loc, ex)
456 GrafiteAst.Note (loc, str)
460 [ ex = executable -> GrafiteAst.Executable (loc,ex)
461 | com = comment -> GrafiteAst.Comment (loc, com)
462 | EOI -> raise End_of_file
467 let exc_located_wrapper f =
471 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
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))