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 ] ->
72 match goal_path, hyp_paths with
73 None, [] -> Ast.UserInput
74 | None, _::_ -> Ast.Implicit
75 | Some goal_path, _ -> goal_path
84 [ "match" ; wanted = term ;
85 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
87 | sps = sequent_pattern_spec ->
90 let wanted,hyp_paths,goal_path =
91 match wanted_and_sps with
92 wanted,None -> wanted, [], Ast.UserInput
93 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
95 wanted, hyp_paths, goal_path ] ->
97 None -> None,[],Ast.UserInput
101 [ SYMBOL ">" -> `LeftToRight
102 | SYMBOL "<" -> `RightToLeft ]
104 int: [ [ num = NUMBER -> int_of_string num ] ];
106 [ IDENT "absurd"; t = tactic_term ->
107 GrafiteAst.Absurd (loc, t)
108 | IDENT "apply"; t = tactic_term ->
109 GrafiteAst.Apply (loc, t)
110 | IDENT "assumption" ->
111 GrafiteAst.Assumption loc
113 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
114 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
115 paramodulation = OPT [ IDENT "paramodulation" ] -> (* ALB *)
116 GrafiteAst.Auto (loc,depth,width,paramodulation)
117 | IDENT "clear"; id = IDENT ->
118 GrafiteAst.Clear (loc,id)
119 | IDENT "clearbody"; id = IDENT ->
120 GrafiteAst.ClearBody (loc,id)
121 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
122 GrafiteAst.Change (loc, what, t)
123 | IDENT "compare"; t = tactic_term ->
124 GrafiteAst.Compare (loc,t)
125 | IDENT "constructor"; n = int ->
126 GrafiteAst.Constructor (loc, n)
127 | IDENT "contradiction" ->
128 GrafiteAst.Contradiction loc
129 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
130 GrafiteAst.Cut (loc, ident, t)
131 | IDENT "decide"; IDENT "equality" ->
132 GrafiteAst.DecideEquality loc
133 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
134 OPT "names"; num = OPT [num = int -> num];
135 idents = OPT ident_list0 ->
136 let idents = match idents with None -> [] | Some idents -> idents in
137 let types = match types with None -> [] | Some types -> types in
138 let to_spec id = GrafiteAst.Ident id in
139 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
140 | IDENT "discriminate"; t = tactic_term ->
141 GrafiteAst.Discriminate (loc, t)
142 | IDENT "elim"; what = tactic_term;
143 using = OPT [ "using"; using = tactic_term -> using ];
144 OPT "names"; num = OPT [num = int -> num];
145 idents = OPT ident_list0 ->
146 let idents = match idents with None -> [] | Some idents -> idents in
147 GrafiteAst.Elim (loc, what, using, num, idents)
148 | IDENT "elimType"; what = tactic_term;
149 using = OPT [ "using"; using = tactic_term -> using ];
150 OPT "names"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
151 let idents = match idents with None -> [] | Some idents -> idents in
152 GrafiteAst.ElimType (loc, what, using, num, idents)
153 | IDENT "exact"; t = tactic_term ->
154 GrafiteAst.Exact (loc, t)
156 GrafiteAst.Exists loc
157 | IDENT "fail" -> GrafiteAst.Fail loc
158 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
161 raise (CicNotationParser.Parse_error
162 (loc, "the pattern cannot specify the term to replace, only its"
163 ^ " paths in the hypotheses and in the conclusion"))
165 GrafiteAst.Fold (loc, kind, t, p)
167 GrafiteAst.Fourier loc
168 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
169 let idents = match idents with None -> [] | Some idents -> idents in
170 GrafiteAst.FwdSimpl (loc, hyp, idents)
171 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
172 GrafiteAst.Generalize (loc,p,id)
173 | IDENT "goal"; n = int ->
174 GrafiteAst.Goal (loc, n)
175 | IDENT "id" -> GrafiteAst.IdTac loc
176 | IDENT "injection"; t = tactic_term ->
177 GrafiteAst.Injection (loc, t)
178 | IDENT "intro"; ident = OPT IDENT ->
179 let idents = match ident with None -> [] | Some id -> [id] in
180 GrafiteAst.Intros (loc, Some 1, idents)
181 | IDENT "intros"; num = OPT [num = int -> num]; idents = OPT ident_list0 ->
182 let idents = match idents with None -> [] | Some idents -> idents in
183 GrafiteAst.Intros (loc, num, idents)
185 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
187 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
188 ident = OPT [ "using" ; ident = IDENT -> ident ] ->
189 let to_what = match to_what with None -> [] | Some to_what -> to_what in
190 GrafiteAst.LApply (loc, depth, to_what, what, ident)
191 | IDENT "left" -> GrafiteAst.Left loc
192 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
193 GrafiteAst.LetIn (loc, t, where)
194 | kind = reduction_kind; p = pattern_spec ->
195 GrafiteAst.Reduce (loc, kind, p)
196 | IDENT "reflexivity" ->
197 GrafiteAst.Reflexivity loc
198 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
199 GrafiteAst.Replace (loc, p, t)
200 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
204 (CicNotationParser.Parse_error
205 (loc,"the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion"))
207 GrafiteAst.Rewrite (loc, d, t, p)
214 | IDENT "symmetry" ->
215 GrafiteAst.Symmetry loc
216 | IDENT "transitivity"; t = tactic_term ->
217 GrafiteAst.Transitivity (loc, t)
222 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
226 | l -> GrafiteAst.Seq (loc, l)
229 [ tac = tactical; SYMBOL ";";
230 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
231 (GrafiteAst.Then (loc, tac, tacs))
234 [ IDENT "do"; count = int; tac = tactical ->
235 GrafiteAst.Do (loc, count, tac)
236 | IDENT "repeat"; tac = tactical ->
237 GrafiteAst.Repeat (loc, tac)
241 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
242 GrafiteAst.First (loc, tacs)
243 | IDENT "try"; tac = NEXT ->
244 GrafiteAst.Try (loc, tac)
246 SYMBOL "["; tacs = LIST0 tactical SEP SYMBOL "|"; SYMBOL "]" ->
247 GrafiteAst.Solve (loc, tacs)
248 | LPAREN; tac = tactical; RPAREN -> tac
249 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
253 [ [ IDENT "definition" ] -> `Definition
254 | [ IDENT "fact" ] -> `Fact
255 | [ IDENT "lemma" ] -> `Lemma
256 | [ IDENT "remark" ] -> `Remark
257 | [ IDENT "theorem" ] -> `Theorem
261 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
262 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
263 fst_constructors = LIST0 constructor SEP SYMBOL "|";
266 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
267 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
268 (name, true, typ, constructors) ] SEP "with" -> types
272 (fun (names, typ) acc ->
273 (List.map (fun name -> (name, typ)) names) @ acc)
276 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
277 let tl_ind_types = match tl with None -> [] | Some types -> types in
278 let ind_types = fst_ind_type :: tl_ind_types in
283 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
284 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
286 name = IDENT ; SYMBOL ":" ; ty = term -> (name,ty)
287 ] SEP SYMBOL ";"; SYMBOL "}" ->
290 (fun (names, typ) acc ->
291 (List.map (fun name -> (name, typ)) names) @ acc)
294 (params,name,typ,fields)
298 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
299 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
300 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
301 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
302 GrafiteAst.Undo (loc, int_opt steps)
303 | [ IDENT "redo" ]; steps = OPT NUMBER ->
304 GrafiteAst.Redo (loc, int_opt steps) *)
305 | [ IDENT "check" ]; t = term ->
306 GrafiteAst.Check (loc, t)
307 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
308 | [ IDENT "whelp"; "match" ] ; t = term ->
309 GrafiteAst.WMatch (loc,t)
310 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
311 GrafiteAst.WInstance (loc,t)
312 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
313 GrafiteAst.WLocate (loc,id)
314 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
315 GrafiteAst.WElim (loc, t)
316 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
317 GrafiteAst.WHint (loc,t)
318 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
322 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
323 let alpha = "[a-zA-Z]" in
324 let num = "[0-9]+" in
325 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
326 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
327 let rex = Str.regexp ("^"^ident^"$") in
328 if Str.string_match rex id 0 then
329 if (try ignore (UriManager.uri_of_string uri); true
330 with UriManager.IllFormedUri _ -> false)
332 GrafiteAst.Ident_alias (id, uri)
334 raise (CicNotationParser.Parse_error (loc,sprintf "Not a valid uri: %s" uri))
336 raise (CicNotationParser.Parse_error (loc,
337 sprintf "Not a valid identifier: %s" id))
338 | IDENT "symbol"; symbol = QSTRING;
339 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
340 SYMBOL "="; dsc = QSTRING ->
342 match instance with Some i -> i | None -> 0
344 GrafiteAst.Symbol_alias (symbol, instance, dsc)
346 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
347 SYMBOL "="; dsc = QSTRING ->
349 match instance with Some i -> i | None -> 0
351 GrafiteAst.Number_alias (instance, dsc)
355 [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
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
377 (Ulexing.from_utf8_string blob))
378 | blob = UNPARSED_META ->
379 add_raw_attribute ~text:(sprintf "${%s}" blob)
380 (CicNotationParser.parse_level2_meta
381 (Ulexing.from_utf8_string blob))
385 | None -> default_associativity
386 | Some assoc -> assoc
390 | None -> default_precedence
394 add_raw_attribute ~text:s
395 (CicNotationParser.parse_level1_pattern
396 (Ulexing.from_utf8_string s))
398 (dir, p1, assoc, prec, p2)
402 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
403 | id = IDENT -> Ast.VarPattern id
404 | SYMBOL "_" -> Ast.ImplicitPattern
405 | LPAREN; terms = LIST1 SELF; RPAREN ->
409 | terms -> Ast.ApplPattern terms)
413 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
418 IDENT "set"; n = QSTRING; v = QSTRING ->
419 GrafiteAst.Set (loc, n, v)
420 | IDENT "drop" -> GrafiteAst.Drop loc
421 | IDENT "qed" -> GrafiteAst.Qed loc
422 | IDENT "variant" ; name = IDENT; SYMBOL ":";
423 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
426 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
427 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
428 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
429 GrafiteAst.Obj (loc,GrafiteAst.Theorem (flavour, name, typ, body))
430 | flavour = theorem_flavour; name = IDENT;
431 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
433 GrafiteAst.Theorem (flavour, name, Ast.Implicit, body))
434 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
435 defs = CicNotationParser.let_defs ->
438 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
439 | ((Ast.Ident (name, None), None),_,_) :: _ ->
443 let body = Ast.Ident (name,None) in
444 GrafiteAst.Obj (loc,GrafiteAst.Theorem(`Definition, name, ty,
445 Some (Ast.LetRec (ind_kind, defs, body))))
446 | [ IDENT "inductive" ]; spec = inductive_spec ->
447 let (params, ind_types) = spec in
448 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
449 | [ IDENT "coinductive" ]; spec = inductive_spec ->
450 let (params, ind_types) = spec in
451 let ind_types = (* set inductive flags to false (coinductive) *)
452 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
455 GrafiteAst.Obj (loc,GrafiteAst.Inductive (params, ind_types))
456 | IDENT "coercion" ; name = IDENT ->
457 GrafiteAst.Coercion (loc, Ast.Ident (name,Some []))
458 | IDENT "coercion" ; name = URI ->
459 GrafiteAst.Coercion (loc, Ast.Uri (name,Some []))
460 | IDENT "alias" ; spec = alias_spec ->
461 GrafiteAst.Alias (loc, spec)
462 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
463 GrafiteAst.Obj (loc,GrafiteAst.Record (params,name,ty,fields))
464 | IDENT "include" ; path = QSTRING ->
465 GrafiteAst.Include (loc,path)
466 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
467 let uris = List.map UriManager.uri_of_string uris in
468 GrafiteAst.Default (loc,what,uris)
469 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
470 GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
471 | IDENT "interpretation"; id = QSTRING;
472 (symbol, args, l3) = interpretation ->
473 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
475 | IDENT "dump" -> GrafiteAst.Dump loc
476 | IDENT "render"; u = URI ->
477 GrafiteAst.Render (loc, UriManager.uri_of_string u)
480 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
481 | tac = tactical; SYMBOL "." -> GrafiteAst.Tactical (loc, tac)
482 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
486 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
487 GrafiteAst.Code (loc, ex)
489 GrafiteAst.Note (loc, str)
493 [ ex = executable -> GrafiteAst.Executable (loc,ex)
494 | com = comment -> GrafiteAst.Comment (loc, com)
495 | EOI -> raise End_of_file
500 let exc_located_wrapper f =
504 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
505 | Stdpp.Exc_located (floc, Stream.Error msg) ->
506 raise (CicNotationParser.Parse_error (floc, msg))
507 | Stdpp.Exc_located (floc, exn) ->
508 raise (CicNotationParser.Parse_error (floc, (Printexc.to_string exn)))
510 let parse_statement lexbuf =
512 (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
514 let parse_dependencies lexbuf =
516 CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
520 | [< '("URI", u) >] ->
521 parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
522 | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
523 parse (GrafiteAst.IncludeDep fname :: acc)
524 | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
525 parse (GrafiteAst.BaseuriDep baseuri :: acc)
526 | [< '("EOI", _) >] -> acc
527 | [< 'tok >] -> parse acc
528 | [< >] -> acc) tok_stream