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
31 (CicNotationPt.term, CicNotationPt.term, CicNotationPt.term GrafiteAst.reduction,
32 CicNotationPt.obj, string)
35 let grammar = CicNotationParser.level2_ast_grammar
37 let term = CicNotationParser.term
38 let statement = Grammar.Entry.create grammar "statement"
40 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
42 let default_precedence = 50
43 let default_associativity = Gramext.NonA
46 GLOBAL: term statement;
48 [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
49 SYMBOL ":"; ty = term; RPAREN -> names,ty
50 | name = IDENT -> [name],Ast.Implicit
53 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
54 tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
55 ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
57 [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
60 [ IDENT "normalize" -> `Normalize
61 | IDENT "reduce" -> `Reduce
62 | IDENT "simplify" -> `Simpl
63 | IDENT "unfold"; t = OPT term -> `Unfold t
64 | IDENT "whd" -> `Whd ]
66 sequent_pattern_spec: [
70 path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
71 (id,match path with Some p -> p | None -> Ast.UserInput) ];
72 goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
74 match goal_path, hyp_paths with
75 None, [] -> Some Ast.UserInput
77 | Some goal_path, _ -> Some goal_path
86 [ "match" ; wanted = tactic_term ;
87 sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
89 | sps = sequent_pattern_spec ->
92 let wanted,hyp_paths,goal_path =
93 match wanted_and_sps with
94 wanted,None -> wanted, [], Some Ast.UserInput
95 | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
97 wanted, hyp_paths, goal_path ] ->
99 None -> None,[],Some Ast.UserInput
103 [ SYMBOL ">" -> `LeftToRight
104 | SYMBOL "<" -> `RightToLeft ]
106 int: [ [ num = NUMBER -> int_of_string num ] ];
108 [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
109 let idents = match idents with None -> [] | Some idents -> idents in
113 using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
115 [ IDENT "absurd"; t = tactic_term ->
116 GrafiteAst.Absurd (loc, t)
117 | IDENT "apply"; t = tactic_term ->
118 GrafiteAst.Apply (loc, t)
119 | IDENT "assumption" ->
120 GrafiteAst.Assumption loc
122 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
123 width = OPT [ IDENT "width"; SYMBOL "="; i = int -> i ];
124 paramodulation = OPT [ IDENT "paramodulation" ];
125 full = OPT [ IDENT "full" ] -> (* ALB *)
126 GrafiteAst.Auto (loc,depth,width,paramodulation,full)
127 | IDENT "clear"; id = IDENT ->
128 GrafiteAst.Clear (loc,id)
129 | IDENT "clearbody"; id = IDENT ->
130 GrafiteAst.ClearBody (loc,id)
131 | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
132 GrafiteAst.Change (loc, what, t)
133 | IDENT "compare"; t = tactic_term ->
134 GrafiteAst.Compare (loc,t)
135 | IDENT "constructor"; n = int ->
136 GrafiteAst.Constructor (loc, n)
137 | IDENT "contradiction" ->
138 GrafiteAst.Contradiction loc
139 | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
140 GrafiteAst.Cut (loc, ident, t)
141 | IDENT "decide"; IDENT "equality" ->
142 GrafiteAst.DecideEquality loc
143 | IDENT "decompose"; types = OPT ident_list0; what = IDENT;
144 (num, idents) = intros_spec ->
145 let types = match types with None -> [] | Some types -> types in
146 let to_spec id = GrafiteAst.Ident id in
147 GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
148 | IDENT "discriminate"; t = tactic_term ->
149 GrafiteAst.Discriminate (loc, t)
150 | IDENT "elim"; what = tactic_term; using = using;
151 (num, idents) = intros_spec ->
152 GrafiteAst.Elim (loc, what, using, num, idents)
153 | IDENT "elimType"; what = tactic_term; using = using;
154 (num, idents) = intros_spec ->
155 GrafiteAst.ElimType (loc, what, using, num, idents)
156 | IDENT "exact"; t = tactic_term ->
157 GrafiteAst.Exact (loc, t)
159 GrafiteAst.Exists loc
160 | IDENT "fail" -> GrafiteAst.Fail loc
161 | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
164 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error
165 ("the pattern cannot specify the term to replace, only its"
166 ^ " paths in the hypotheses and in the conclusion")))
168 GrafiteAst.Fold (loc, kind, t, p)
170 GrafiteAst.Fourier loc
171 | IDENT "fwd"; hyp = IDENT; idents = OPT ident_list0 ->
172 let idents = match idents with None -> [] | Some idents -> idents in
173 GrafiteAst.FwdSimpl (loc, hyp, idents)
174 | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
175 GrafiteAst.Generalize (loc,p,id)
176 | IDENT "goal"; n = int ->
177 GrafiteAst.Goal (loc, n)
178 | IDENT "id" -> GrafiteAst.IdTac loc
179 | IDENT "injection"; t = tactic_term ->
180 GrafiteAst.Injection (loc, t)
181 | IDENT "intro"; ident = OPT IDENT ->
182 let idents = match ident with None -> [] | Some id -> [id] in
183 GrafiteAst.Intros (loc, Some 1, idents)
184 | IDENT "intros"; (num, idents) = intros_spec ->
185 GrafiteAst.Intros (loc, num, idents)
187 depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
189 to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
190 ident = OPT [ IDENT "using" ; ident = IDENT -> ident ] ->
191 let to_what = match to_what with None -> [] | Some to_what -> to_what in
192 GrafiteAst.LApply (loc, depth, to_what, what, ident)
193 | IDENT "left" -> GrafiteAst.Left loc
194 | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
195 GrafiteAst.LetIn (loc, t, where)
196 | kind = reduction_kind; p = pattern_spec ->
197 GrafiteAst.Reduce (loc, kind, p)
198 | IDENT "reflexivity" ->
199 GrafiteAst.Reflexivity loc
200 | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
201 GrafiteAst.Replace (loc, p, t)
202 | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
206 (HExtlib.Localized (loc,
207 (CicNotationParser.Parse_error
208 "the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion")))
210 GrafiteAst.Rewrite (loc, d, t, p)
217 | IDENT "symmetry" ->
218 GrafiteAst.Symmetry loc
219 | IDENT "transitivity"; t = tactic_term ->
220 GrafiteAst.Transitivity (loc, t)
225 [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
228 | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
231 GrafiteAst.Seq (loc, ts)
234 [ tac = SELF; SYMBOL ";";
235 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
236 (GrafiteAst.Then (loc, tac, tacs))
239 [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
240 GrafiteAst.Do (loc, count, tac)
241 | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
245 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
246 GrafiteAst.First (loc, tacs)
247 | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
249 SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
250 GrafiteAst.Solve (loc, tacs)
251 | LPAREN; tac = SELF; RPAREN -> tac
252 | tac = tactic -> GrafiteAst.Tactic (loc, tac)
255 punctuation_tactical:
257 [ SYMBOL "[" -> GrafiteAst.Branch loc
258 | SYMBOL "|" -> GrafiteAst.Shift loc
259 | i = int; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
260 | SYMBOL "]" -> GrafiteAst.Merge loc
261 | SYMBOL ";" -> GrafiteAst.Semicolon loc
262 | SYMBOL "." -> GrafiteAst.Dot loc
267 [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
268 | IDENT "unfocus" -> GrafiteAst.Unfocus loc
269 | IDENT "skip" -> GrafiteAst.Skip loc
270 | tac = atomic_tactical LEVEL "loops" -> tac
274 [ [ IDENT "definition" ] -> `Definition
275 | [ IDENT "fact" ] -> `Fact
276 | [ IDENT "lemma" ] -> `Lemma
277 | [ IDENT "remark" ] -> `Remark
278 | [ IDENT "theorem" ] -> `Theorem
282 fst_name = IDENT; params = LIST0 [ arg=arg -> arg ];
283 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
284 fst_constructors = LIST0 constructor SEP SYMBOL "|";
287 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
288 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
289 (name, true, typ, constructors) ] SEP "with" -> types
293 (fun (names, typ) acc ->
294 (List.map (fun name -> (name, typ)) names) @ acc)
297 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
298 let tl_ind_types = match tl with None -> [] | Some types -> types in
299 let ind_types = fst_ind_type :: tl_ind_types in
304 name = IDENT; params = LIST0 [ arg = arg -> arg ] ;
305 SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ;
308 coercion = [ SYMBOL ":" -> false | SYMBOL ":"; SYMBOL ">" -> true ] ;
309 ty = term -> (name,ty,coercion)
310 ] SEP SYMBOL ";"; SYMBOL "}" ->
313 (fun (names, typ) acc ->
314 (List.map (fun name -> (name, typ)) names) @ acc)
317 (params,name,typ,fields)
321 [ [ IDENT "quit" ] -> GrafiteAst.Quit loc
322 (* | [ IDENT "abort" ] -> GrafiteAst.Abort loc *)
323 (* | [ IDENT "undo" ]; steps = OPT NUMBER ->
324 GrafiteAst.Undo (loc, int_opt steps)
325 | [ IDENT "redo" ]; steps = OPT NUMBER ->
326 GrafiteAst.Redo (loc, int_opt steps) *)
327 | [ IDENT "check" ]; t = term ->
328 GrafiteAst.Check (loc, t)
329 | [ IDENT "hint" ] -> GrafiteAst.Hint loc
330 | [ IDENT "whelp"; "match" ] ; t = term ->
331 GrafiteAst.WMatch (loc,t)
332 | [ IDENT "whelp"; IDENT "instance" ] ; t = term ->
333 GrafiteAst.WInstance (loc,t)
334 | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT ->
335 GrafiteAst.WLocate (loc,id)
336 | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
337 GrafiteAst.WElim (loc, t)
338 | [ IDENT "whelp"; IDENT "hint" ] ; t = term ->
339 GrafiteAst.WHint (loc,t)
340 | [ IDENT "print" ]; name = QSTRING -> GrafiteAst.Print (loc, name)
344 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
345 let alpha = "[a-zA-Z]" in
346 let num = "[0-9]+" in
347 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
348 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
349 let rex = Str.regexp ("^"^ident^"$") in
350 if Str.string_match rex id 0 then
351 if (try ignore (UriManager.uri_of_string uri); true
352 with UriManager.IllFormedUri _ -> false)
354 GrafiteAst.Ident_alias (id, uri)
357 (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
359 raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
360 sprintf "Not a valid identifier: %s" id)))
361 | IDENT "symbol"; symbol = QSTRING;
362 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
363 SYMBOL "="; dsc = QSTRING ->
365 match instance with Some i -> i | None -> 0
367 GrafiteAst.Symbol_alias (symbol, instance, dsc)
369 instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
370 SYMBOL "="; dsc = QSTRING ->
372 match instance with Some i -> i | None -> 0
374 GrafiteAst.Number_alias (instance, dsc)
378 [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
380 Ast.IdentArg (List.length l, id)
384 [ IDENT "left"; IDENT "associative" -> Gramext.LeftA
385 | IDENT "right"; IDENT "associative" -> Gramext.RightA
386 | IDENT "non"; IDENT "associative" -> Gramext.NonA
390 [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
393 [ dir = OPT direction; s = QSTRING;
394 assoc = OPT associativity; prec = OPT precedence;
397 [ blob = UNPARSED_AST ->
398 add_raw_attribute ~text:(sprintf "@{%s}" blob)
399 (CicNotationParser.parse_level2_ast
400 (Ulexing.from_utf8_string blob))
401 | blob = UNPARSED_META ->
402 add_raw_attribute ~text:(sprintf "${%s}" blob)
403 (CicNotationParser.parse_level2_meta
404 (Ulexing.from_utf8_string blob))
408 | None -> default_associativity
409 | Some assoc -> assoc
413 | None -> default_precedence
417 add_raw_attribute ~text:s
418 (CicNotationParser.parse_level1_pattern
419 (Ulexing.from_utf8_string s))
421 (dir, p1, assoc, prec, p2)
425 [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
426 | id = IDENT -> Ast.VarPattern id
427 | SYMBOL "_" -> Ast.ImplicitPattern
428 | LPAREN; terms = LIST1 SELF; RPAREN ->
432 | terms -> Ast.ApplPattern terms)
436 [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
441 IDENT "set"; n = QSTRING; v = QSTRING ->
442 GrafiteAst.Set (loc, n, v)
443 | IDENT "drop" -> GrafiteAst.Drop loc
444 | IDENT "qed" -> GrafiteAst.Qed loc
445 | IDENT "variant" ; name = IDENT; SYMBOL ":";
446 typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
449 (`Variant,name,typ,Some (Ast.Ident (newname, None))))
450 | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
451 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
452 GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
453 | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
456 Ast.Theorem (flavour, name, Ast.Implicit, Some body))
457 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
458 defs = CicNotationParser.let_defs ->
461 | ((Ast.Ident (name, None), Some ty),_,_) :: _ -> name,ty
462 | ((Ast.Ident (name, None), None),_,_) :: _ ->
466 let body = Ast.Ident (name,None) in
467 GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
468 Some (Ast.LetRec (ind_kind, defs, body))))
469 | IDENT "inductive"; spec = inductive_spec ->
470 let (params, ind_types) = spec in
471 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
472 | IDENT "coinductive"; spec = inductive_spec ->
473 let (params, ind_types) = spec in
474 let ind_types = (* set inductive flags to false (coinductive) *)
475 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
478 GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
479 | IDENT "coercion" ; suri = URI ->
480 GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true)
481 | IDENT "alias" ; spec = alias_spec ->
482 GrafiteAst.Alias (loc, spec)
483 | IDENT "record" ; (params,name,ty,fields) = record_spec ->
484 GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
485 | IDENT "include" ; path = QSTRING ->
486 GrafiteAst.Include (loc,path)
487 | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
488 let uris = List.map UriManager.uri_of_string uris in
489 GrafiteAst.Default (loc,what,uris)
490 | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
491 GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
492 | IDENT "interpretation"; id = QSTRING;
493 (symbol, args, l3) = interpretation ->
494 GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
496 | IDENT "dump" -> GrafiteAst.Dump loc
497 | IDENT "render"; u = URI ->
498 GrafiteAst.Render (loc, UriManager.uri_of_string u)
501 [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
502 | tac = tactical; punct = punctuation_tactical ->
503 GrafiteAst.Tactical (loc, tac, Some punct)
504 | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
505 | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
509 [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT ->
510 GrafiteAst.Code (loc, ex)
512 GrafiteAst.Note (loc, str)
516 [ ex = executable -> GrafiteAst.Executable (loc,ex)
517 | com = comment -> GrafiteAst.Comment (loc, com)
518 | EOI -> raise End_of_file
523 let exc_located_wrapper f =
527 | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
528 | Stdpp.Exc_located (floc, Stream.Error msg) ->
529 raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
530 | Stdpp.Exc_located (floc, exn) ->
532 (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
534 let parse_statement lexbuf =
536 (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
538 let parse_dependencies lexbuf =
540 CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
544 | [< '("URI", u) >] ->
545 parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
546 | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
547 parse (GrafiteAst.IncludeDep fname :: acc)
548 | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
549 parse (GrafiteAst.BaseuriDep baseuri :: acc)
550 | [< '("EOI", _) >] -> acc
551 | [< 'tok >] -> parse acc
552 | [< >] -> acc) tok_stream