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 let grammar = CicNotationParser.level2_ast_grammar
32 let term = CicNotationParser.term
33 let statement = Grammar.Entry.create grammar "statement"
35 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
37 let default_precedence = 50
38 let default_associativity = Gramext.NonA
41 GLOBAL: term statement;
43 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
44 SYMBOL ":"; ty = term; RPAREN -> names,ty
45 | name = IDENT -> [name],Ast.Implicit
48 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
49 tactic_term: [ [ t = term -> t ] ];
51 [ SYMBOL "["; idents = LIST0 IDENT SEP SYMBOL ";"; SYMBOL "]" -> idents ]
54 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
57 [ IDENT "normalize" -> `Normalize
58 | IDENT "reduce" -> `Reduce
59 | IDENT "simplify" -> `Simpl
60 | IDENT "unfold"; t = OPT term -> `Unfold t
61 | IDENT "whd" -> `Whd ]
63 sequent_pattern_spec: [
67 path = OPT [SYMBOL ":" ; path = term -> path ] ->
68 (id,match path with Some p -> p | None -> Ast.UserInput) ]
70 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = term -> term ] ->
74 | Some goal_path -> goal_path
83 [ "match" ; wanted = term ;
84 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
86 | sps = sequent_pattern_spec ->
89 let wanted,hyp_paths,goal_path =
90 match wanted_and_sps with
91 wanted,None -> wanted, [], Ast.UserInput
92 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
94 wanted, hyp_paths, goal_path ] ->
96 None -> None,[],Ast.UserInput
100 [ SYMBOL ">" -> `LeftToRight
101 | SYMBOL "<" -> `RightToLeft ]
103 int: [ [ num = NUMBER -> int_of_string num ] ];
105 [ IDENT "absurd"; t = tactic_term ->
106 GrafiteAst.Absurd (loc, t)
107 | IDENT "apply"; t = tactic_term ->
108 GrafiteAst.Apply (loc, t)
109 | IDENT "assumption" ->
110 GrafiteAst.Assumption loc
112 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
113 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
114 paramodulation = OPT [ IDENT "paramodulation" ] -> (* ALB *)
115 GrafiteAst.Auto (loc,depth,width,paramodulation)
116 | IDENT "clear"; id = IDENT ->
117 GrafiteAst.Clear (loc,id)
118 | IDENT "clearbody"; id = IDENT ->
119 GrafiteAst.ClearBody (loc,id)
120 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
121 GrafiteAst.Change (loc, what, t)
122 | IDENT "compare"; t = tactic_term ->
123 GrafiteAst.Compare (loc,t)
124 | IDENT "constructor"; n = int ->
125 GrafiteAst.Constructor (loc, n)
126 | IDENT "contradiction" ->
127 GrafiteAst.Contradiction loc
128 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
129 GrafiteAst.Cut (loc, ident, t)
130 | IDENT "decide"; IDENT "equality" ->
131 GrafiteAst.DecideEquality loc
132 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
133 OPT "names"; num = OPT [num = int -> num];
134 idents = OPT ident_list0 ->
135 let idents = match idents with None -> [] | Some idents -> idents in
136 let types = match types with None -> [] | Some types -> types in
137 let to_spec id = GrafiteAst.Ident id in
138 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
139 | IDENT "discriminate"; t = tactic_term ->
140 GrafiteAst.Discriminate (loc, t)
141 | IDENT "elim"; what = tactic_term;
142 using = OPT [ "using"; using = tactic_term -> using ];
143 OPT "names"; num = OPT [num = int -> num];
144 idents = OPT ident_list0 ->
145 let idents = match idents with None -> [] | Some idents -> idents in
146 GrafiteAst.Elim (loc, what, using, num, idents)
147 | IDENT "elimType"; what = tactic_term;
148 using = OPT [ "using"; using = tactic_term -> using ];
149 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
150 let idents = match idents with None -> [] | Some idents -> idents in
151 GrafiteAst.ElimType (loc, what, using, num, idents)
152 | IDENT "exact"; t = tactic_term ->
153 GrafiteAst.Exact (loc, t)
155 GrafiteAst.Exists loc
156 | IDENT "fail" -> GrafiteAst.Fail loc
157 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
160 raise (CicNotationParser.Parse_error
161 (loc, "the pattern cannot specify the term to replace, only its"
162 ^ " paths in the hypotheses and in the conclusion"))
164 GrafiteAst.Fold (loc, kind, t, p)
166 GrafiteAst.Fourier loc
167 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
168 let idents = match idents with None -> [] | Some idents -> idents in
169 GrafiteAst.FwdSimpl (loc, hyp, idents)
170 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
171 GrafiteAst.Generalize (loc,p,id)
172 | IDENT "goal"; n = int ->
173 GrafiteAst.Goal (loc, n)
174 | IDENT "id" -> GrafiteAst.IdTac loc
175 | IDENT "injection"; t = tactic_term ->
176 GrafiteAst.Injection (loc, t)
177 | IDENT "intro"; ident = OPT IDENT ->
178 let idents = match ident with None -> [] | Some id -> [id] in
179 GrafiteAst.Intros (loc, Some 1, idents)
180 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
181 let idents = match idents with None -> [] | Some idents -> idents in
182 GrafiteAst.Intros (loc, num, idents)
184 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
186 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
187 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
188 let to_what = match to_what with None -> [] | Some to_what -> to_what in
189 GrafiteAst.LApply (loc, depth, to_what, what, ident)
190 | IDENT "left" -> GrafiteAst.Left loc
191 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
192 GrafiteAst.LetIn (loc, t, where)
193 | kind = reduction_kind; p = pattern_spec ->
194 GrafiteAst.Reduce (loc, kind, p)
195 | IDENT "reflexivity" ->
196 GrafiteAst.Reflexivity loc
197 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
198 GrafiteAst.Replace (loc, p, t)
199 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
203 (CicNotationParser.Parse_error
204 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
206 GrafiteAst.Rewrite (loc, d, t, p)
213 | IDENT "symmetry" ->
214 GrafiteAst.Symmetry loc
215 | IDENT "transitivity"; t = tactic_term ->
216 GrafiteAst.Transitivity (loc, t)
221 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
225 | l -> GrafiteAst.Seq (loc, l)
228 [ tac = tactical; SYMBOL ";";
229 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
230 (GrafiteAst.Then (loc, tac, tacs))
233 [ IDENT "do"; count = int; tac = tactical ->
234 GrafiteAst.Do (loc, count, tac)
235 | IDENT "repeat"; tac = tactical ->
236 GrafiteAst.Repeat (loc, tac)
240 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
241 GrafiteAst.First (loc, tacs)
242 | IDENT "try"; tac = NEXT ->
243 GrafiteAst.Try (loc, tac)
245 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
246 GrafiteAst.Solve (loc, tacs)
247 | LPAREN; tac = tactical; RPAREN -> tac
248 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
252 [ [ IDENT "definition" ] -> `Definition
253 | [ IDENT "fact" ] -> `Fact
254 | [ IDENT "lemma" ] -> `Lemma
255 | [ IDENT "remark" ] -> `Remark
256 | [ IDENT "theorem" ] -> `Theorem
260 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
261 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
262 fst_constructors = LIST0 constructor SEP SYMBOL "|";
265 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
266 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
267 (name, true, typ, constructors) ] SEP "with" -> types
271 (fun (names, typ) acc ->
272 (List.map (fun name -> (name, typ)) names) @ acc)
275 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
276 let tl_ind_types = match tl with None -> [] | Some types -> types in
277 let ind_types = fst_ind_type :: tl_ind_types in
282 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
283 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
285 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
286 ] SEP SYMBOL ";"; SYMBOL "}" ->
289 (fun (names, typ) acc ->
290 (List.map (fun name -> (name, typ)) names) @ acc)
293 (params,name,typ,fields)
297 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
298 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
299 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
300 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
301 GrafiteAst.Undo (loc, int_opt steps)
302 | [ IDENT "redo" ]; steps = OPT NUMBER ->
303 GrafiteAst.Redo (loc, int_opt steps) *)
304 | [ IDENT "check" ]; t = term ->
305 GrafiteAst.Check (loc, t)
306 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
307 | [ IDENT "whelp"; "match" ] ; t = term ->
308 GrafiteAst.WMatch (loc,t)
309 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
310 GrafiteAst.WInstance (loc,t)
311 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
312 GrafiteAst.WLocate (loc,id)
313 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
314 GrafiteAst.WElim (loc, t)
315 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
316 GrafiteAst.WHint (loc,t)
317 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
321 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
322 let alpha = "[a-zA-Z]" in
323 let num = "[0-9]+" in
324 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
325 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
326 let rex = Str.regexp ("^"^ident^"$") in
327 if Str.string_match rex id 0 then
328 if (try ignore (UriManager.uri_of_string uri); true
329 with UriManager.IllFormedUri _ -> false)
331 GrafiteAst.Ident_alias (id, uri)
333 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
335 raise (CicNotationParser.Parse_error (loc,
336 sprintf "Not a valid identifier: %s" id))
337 | IDENT "symbol"; symbol = QSTRING;
338 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
339 SYMBOL "="; dsc = QSTRING ->
341 match instance with Some i -> i | None -> 0
343 GrafiteAst.Symbol_alias (symbol, instance, dsc)
345 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
346 SYMBOL "="; dsc = QSTRING ->
348 match instance with Some i -> i | None -> 0
350 GrafiteAst.Number_alias (instance, dsc)
354 [ id = IDENT -> Ast.IdentArg (0, id)
355 | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
356 SYMBOL "."; id = IDENT ->
357 Ast.IdentArg (List.length l, id)
361 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
362 | IDENT "right"; IDENT "associative" -> Gramext.RightA
363 | IDENT "non"; IDENT "associative" -> Gramext.NonA
367 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
370 [ dir = OPT direction; s = QSTRING;
371 assoc = OPT associativity; prec = OPT precedence;
374 [ blob = UNPARSED_AST ->
375 add_raw_attribute ~text:(sprintf "@{%s}" blob)
376 (CicNotationParser.parse_level2_ast (Stream.of_string blob))
377 | blob = UNPARSED_META ->
378 add_raw_attribute ~text:(sprintf "${%s}" blob)
379 (CicNotationParser.parse_level2_meta (Stream.of_string blob))
383 | None -> default_associativity
384 | Some assoc -> assoc
388 | None -> default_precedence
392 add_raw_attribute ~text:s
393 (CicNotationParser.parse_level1_pattern (Stream.of_string s))
395 (dir, p1, assoc, prec, p2)
399 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
400 | id = IDENT -> Ast.VarPattern id
401 | SYMBOL "_" -> Ast.ImplicitPattern
402 | LPAREN; terms = LIST1 SELF; RPAREN ->
406 | terms -> Ast.ApplPattern terms)
410 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
415 IDENT "set"; n = QSTRING; v = QSTRING ->
416 GrafiteAst.Set (loc, n, v)
417 | IDENT "drop" -> GrafiteAst.Drop loc
418 | IDENT "qed" -> GrafiteAst.Qed loc
419 | IDENT "variant" ; name = IDENT; SYMBOL ":";
420 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
423 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
424 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
425 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
426 GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
427 | flavour = theorem_flavour; name = IDENT;
428 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
430 GrafiteAst.Theorem (flavour, name, Ast.Implicit, body))
431 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
432 defs = CicNotationParser.let_defs ->
435 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
436 | ((Ast.Ident (name, None), None),_,_) :: _ ->
440 let body = Ast.Ident (name,None) in
441 GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
442 Some (Ast.LetRec (ind_kind, defs, body))))
443 | [ IDENT "inductive" ]; spec = inductive_spec ->
444 let (params, ind_types) = spec in
445 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
446 | [ IDENT "coinductive" ]; spec = inductive_spec ->
447 let (params, ind_types) = spec in
448 let ind_types = (* set inductive flags to false (coinductive) *)
449 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
452 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
453 | IDENT "coercion" ; name = IDENT ->
454 GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
455 | IDENT "coercion" ; name = URI ->
456 GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
457 | IDENT "alias" ; spec = alias_spec ->
458 GrafiteAst.Alias (loc, spec)
459 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
460 GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
461 | IDENT "include" ; path = QSTRING ->
462 GrafiteAst.Include (loc,path)
463 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
464 let uris = List.map UriManager.uri_of_string uris in
465 GrafiteAst.Default (loc,what,uris)
466 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
467 GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
468 | IDENT "interpretation"; id = QSTRING;
469 (symbol, args, l3) = interpretation ->
470 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
472 | IDENT "dump" -> GrafiteAst.Dump loc
473 | IDENT "render"; u = URI ->
474 GrafiteAst.Render (loc, UriManager.uri_of_string u)
477 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
478 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
479 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
483 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
484 GrafiteAst.Code (loc, ex)
486 GrafiteAst.Note (loc, str)
490 [ ex = executable -> GrafiteAst.Executable (loc,ex)
491 | com = comment -> GrafiteAst.Comment (loc, com)
492 | EOI -> raise End_of_file
497 let exc_located_wrapper f =
501 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
502 | Stdpp.Exc_located (floc, Stream.Error msg) ->
503 raise (CicNotationParser.Parse_error (floc, msg))
504 | Stdpp.Exc_located (floc, exn) ->
505 raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
507 let parse_statement stream =
508 exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))