1 (* Copyright (C) 2004, 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/
29 prerr_endline "<NEW_TEXTUAL_PARSER>";
31 prerr_endline "</NEW_TEXTUAL_PARSER>"
34 (** if set to true each number will have a different insance number and can
35 * thus be interpreted differently than others *)
36 let use_fresh_num_instances = false
40 open DisambiguateTypes
42 exception Parse_error of Token.flocation * string
44 let fresh_num_instance =
46 if use_fresh_num_instances then
47 (fun () -> incr n; !n)
51 let choice_of_uri uri =
52 let term = CicUtil.term_of_uri uri in
53 (uri, (fun _ _ _ -> term))
55 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
57 let term = Grammar.Entry.create grammar "term"
58 let term0 = Grammar.Entry.create grammar "term0"
59 let tactic = Grammar.Entry.create grammar "tactic"
60 let tactical = Grammar.Entry.create grammar "tactical"
61 let tactical0 = Grammar.Entry.create grammar "tactical0"
62 let command = Grammar.Entry.create grammar "command"
63 let script = Grammar.Entry.create grammar "script"
65 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
66 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
67 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
68 let return_command loc cmd = cmd (* TODO ZACK FIXME uhm ... why we drop loc? *)
71 let (x, y) = CicAst.loc_of_floc floc in
72 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
74 let name_of_string = function
75 | "_" -> Cic.Anonymous
78 let string_of_name = function
79 | Cic.Anonymous -> "_"
82 let int_opt = function
84 | Some lexeme -> Some (int_of_string lexeme)
86 (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
87 * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
88 * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
89 * type in a block of mutual inductive types.
91 * This function performs the expansion foo.ind -> foo#xpointer..., if needed
93 let ind_expansion uri =
94 let len = String.length uri in
95 if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
96 uri ^ "#xpointer(1/1)"
101 GLOBAL: term term0 tactic tactical tactical0 command script;
106 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
110 [ s = SYMBOL "_" -> None
111 | t = term -> Some t ]
114 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
115 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
116 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
117 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
123 | "CProp" -> `CProp ]
126 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
127 (Cic.Name i, Some typ)
128 | i = IDENT -> (Cic.Name i, None)
133 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
136 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
143 substituted_name: [ (* a subs.name is an explicit substitution subject *)
144 [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
145 | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
148 name: [ (* as substituted_name with no explicit substitution *)
149 [ s = [ IDENT | SYMBOL ] -> s ]
152 [ n = name -> (n, [])
153 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
161 PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
162 ty = term; PAREN ")" ->
165 index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
166 SYMBOL <:unicode<def>> (* ≝ *);
168 let rec list_of_lambda ty final_term = function
171 CicAst.Binder (`Lambda, (Cic.Name name, Some ty),
172 list_of_lambda ty final_term tl)
174 let rec lambda_of_arg_list final_term = function
177 list_of_lambda ty (lambda_of_arg_list final_term tl) l
179 let t1' = lambda_of_arg_list t1 args in
180 let rec get_position_of name n = function
186 (get_position_of name (n+1) tl)
188 let rec find_arg name n = function
189 | [] -> (fail loc (sprintf "Argument %s not found" name))
191 let (got,len) = get_position_of name 0 l in
193 | None -> (find_arg name (n+len) tl)
194 | Some where -> n + where)
197 (match index_name with
199 | (Some name) -> find_arg name 0 args)
204 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
205 term0: [ [ t = term; EOI -> return_term loc t ] ];
208 [ "let"; var = typed_name;
209 SYMBOL <:unicode<def>> (* ≝ *);
210 t1 = term; "in"; t2 = term ->
211 return_term loc (CicAst.LetIn (var, t1, t2))
212 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
213 defs = let_defs; "in"; body = term ->
214 return_term loc (CicAst.LetRec (ind_kind, defs, body))
220 [ vars = LIST1 IDENT SEP SYMBOL ",";
221 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
222 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
223 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
225 SYMBOL "."; body = term ->
229 let name = name_of_string var in
230 CicAst.Binder (b, (name, typ), body))
233 return_term loc binder
234 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
236 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
238 | "logic_add" LEFTA [ (* nothing here by default *) ]
239 | "logic_mult" LEFTA [ (* nothing here by default *) ]
240 | "logic_inv" NONA [ (* nothing here by default *) ]
242 [ t1 = term; SYMBOL "="; t2 = term ->
243 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
245 | "add" LEFTA [ (* nothing here by default *) ]
246 | "mult" LEFTA [ (* nothing here by default *) ]
247 | "power" LEFTA [ (* nothing here by default *) ]
248 | "inv" NONA [ (* nothing here by default *) ]
250 [ t1 = term; t2 = term ->
251 let rec aux = function
252 | CicAst.Appl (hd :: tl) -> aux hd @ tl
255 CicAst.Appl (aux t1 @ [t2])
258 [ sort = sort -> CicAst.Sort sort
259 | n = substituted_name -> return_term loc n
260 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
261 | IMPLICIT -> return_term loc CicAst.Implicit
264 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
269 int_of_string (String.sub m 1 (String.length m - 1))
270 with Failure "int_of_string" ->
271 fail loc ("Invalid meta variable number: " ^ m)
273 return_term loc (CicAst.Meta (index, substs))
274 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
276 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
280 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
282 ((lhs: CicAst.case_pattern), rhs)
286 (CicAst.Case (t, indty_ident, outtyp, patterns))
287 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
288 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
289 | PAREN "("; t = term; PAREN ")" -> return_term loc t
293 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
295 tactic_term: [ [ t = term -> t ] ];
297 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
300 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
303 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
304 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
305 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
308 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
309 return_tactic loc (TacticAst.Absurd t)
310 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
311 return_tactic loc (TacticAst.Apply t)
312 | [ IDENT "assumption" | IDENT "Assumption" ] ->
313 return_tactic loc TacticAst.Assumption
314 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
315 | [ IDENT "change" | IDENT "Change" ];
316 t1 = tactic_term; "with"; t2 = tactic_term;
317 where = tactic_where ->
318 return_tactic loc (TacticAst.Change (t1, t2, where))
319 (* TODO Change_pattern *)
320 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
321 return_tactic loc TacticAst.Contradiction
322 | [ IDENT "cut" | IDENT "Cut" ];
323 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
324 | [ IDENT "decompose" | IDENT "Decompose" ];
325 principles = ident_list1; where = IDENT ->
326 return_tactic loc (TacticAst.Decompose (where, principles))
327 | [ IDENT "discriminate" | IDENT "Discriminate" ];
329 return_tactic loc (TacticAst.Discriminate hyp)
330 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
331 return_tactic loc (TacticAst.ElimType t)
332 | [ IDENT "elim" | IDENT "Elim" ];
334 using = OPT [ "using"; using = tactic_term -> using ] ->
335 return_tactic loc (TacticAst.Elim (t1, using))
336 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
337 return_tactic loc (TacticAst.Exact t)
338 | [ IDENT "exists" | IDENT "Exists" ] ->
339 return_tactic loc TacticAst.Exists
340 | [ IDENT "fold" | IDENT "Fold" ];
341 kind = reduction_kind; t = tactic_term ->
342 return_tactic loc (TacticAst.Fold (kind, t))
343 | [ IDENT "fourier" | IDENT "Fourier" ] ->
344 return_tactic loc TacticAst.Fourier
345 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
346 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
347 return_tactic loc (TacticAst.Injection ident)
348 | [ IDENT "intros" | IDENT "Intros" ];
349 num = OPT [ num = int -> num ];
350 idents = OPT ident_list0 ->
351 let idents = match idents with None -> [] | Some idents -> idents in
352 return_tactic loc (TacticAst.Intros (num, idents))
353 | [ IDENT "intro" | IDENT "Intro" ] ->
354 return_tactic loc (TacticAst.Intros (Some 1, []))
355 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
357 t = tactic_term; "in"; where = IDENT ->
358 return_tactic loc (TacticAst.LetIn (t, where))
359 | kind = reduction_kind;
361 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
364 terms = LIST0 term SEP SYMBOL "," ->
366 (match (pat, terms) with
367 | None, [] -> TacticAst.Reduce (kind, None)
368 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
369 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
370 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
372 return_tactic loc tac
373 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
374 return_tactic loc TacticAst.Reflexivity
375 | [ IDENT "replace" | IDENT "Replace" ];
376 t1 = tactic_term; "with"; t2 = tactic_term ->
377 return_tactic loc (TacticAst.Replace (t1, t2))
379 (* TODO Replace_pattern *)
380 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
381 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
382 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
383 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
384 return_tactic loc TacticAst.Symmetry
385 | [ IDENT "transitivity" | IDENT "Transitivity" ];
387 return_tactic loc (TacticAst.Transitivity t)
390 tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
393 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
395 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
396 return_tactical loc (TacticAst.Seq tactics)
400 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
401 return_tactical loc (TacticAst.Then (tac, tacs))
404 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
405 return_tactical loc (TacticAst.Do (count, tac))
406 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
407 return_tactical loc (TacticAst.Repeat tac)
410 [ [ IDENT "tries" | IDENT "Tries" ];
411 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
412 return_tactical loc (TacticAst.Tries tacs)
413 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
414 return_tactical loc (TacticAst.Try tac)
415 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
416 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
417 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
418 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
421 theorem_flavour: [ (* all flavours but Goal *)
422 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
423 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
424 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
425 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
426 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
430 fst_name = IDENT; params = LIST0 [
431 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
432 typ = term; PAREN ")" -> (names, typ) ];
433 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
434 fst_constructors = LIST0 constructor SEP SYMBOL "|";
437 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
438 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
439 (name, true, typ, constructors) ] SEP "with" -> types
443 (fun (names, typ) acc ->
444 (List.map (fun name -> (name, typ)) names) @ acc)
447 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
448 let tl_ind_types = match tl with None -> [] | Some types -> types in
449 let ind_types = fst_ind_type :: tl_ind_types in
453 [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
458 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
459 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
460 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
461 | [ IDENT "qed" | IDENT "Qed" ] ->
462 return_command loc (TacticAst.Qed None)
463 | [ IDENT "print" | IDENT "Print" ];
464 print_kind = print_kind ->
465 return_command loc (TacticAst.Print print_kind)
466 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
467 return_command loc (TacticAst.Qed (Some name))
468 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
469 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
470 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
471 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
475 | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
476 | ((Cic.Name name,None),_,_) :: _ ->
477 fail loc ("No type given for " ^ name)
480 let body = CicAst.Ident (name,None) in
481 TacticAst.Theorem(`Definition, Some name, ty,
482 Some (CicAst.LetRec (ind_kind, defs, body)))
484 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
485 let (params, ind_types) = spec in
486 return_command loc (TacticAst.Inductive (params, ind_types))
487 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
488 let (params, ind_types) = spec in
489 let ind_types = (* set inductive flags to false (coinductive) *)
490 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
493 return_command loc (TacticAst.Inductive (params, ind_types))
494 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT ->
495 return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
496 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI ->
497 return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
498 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
499 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
500 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
501 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
502 return_command loc (TacticAst.Undo (int_opt steps))
503 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
504 return_command loc (TacticAst.Redo (int_opt steps))
505 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
506 return_command loc (TacticAst.Baseuri uri)
507 | [ IDENT "check" | IDENT "Check" ]; t = term ->
508 return_command loc (TacticAst.Check t)
510 | [ IDENT "alias" | IDENT "Alias" ]; spec = alias_spec ->
511 return_command loc (TacticAst.Alias spec)
516 [ cmd = tactical0 -> Command cmd
517 | s = COMMENT -> Comment (loc, s)
520 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
523 let exc_located_wrapper f =
527 | Stdpp.Exc_located (floc, Stream.Error msg) ->
528 raise (Parse_error (floc, msg))
529 | Stdpp.Exc_located (floc, exn) ->
530 raise (Parse_error (floc, (Printexc.to_string exn)))
532 let parse_term stream =
533 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
534 let parse_tactic stream =
535 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
536 let parse_tactical stream =
537 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
538 let parse_script stream =
539 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
543 (** {2 Interface for gTopLevel} *)
545 module EnvironmentP3 =
551 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
552 let aliases = Grammar.Entry.create aliases_grammar "aliases"
557 (fun domain_item (dsc, _) acc ->
559 match domain_item with
560 | Id id -> sprintf "alias id %s = %s" id dsc
561 | Symbol (symb, instance) ->
562 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
565 sprintf "alias num (instance %d) = \"%s\"" instance dsc
570 String.concat "\n" (List.sort compare aliases)
574 aliases: [ (* build an environment from an aliases list *)
575 [ aliases = LIST0 alias; EOI ->
577 (fun env (domain_item, codomain_item) ->
578 Environment.add domain_item codomain_item env)
579 Environment.empty aliases
582 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
585 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
586 (Id id, choice_of_uri uri)
587 | IDENT "symbol"; symbol = QSTRING;
588 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
589 SYMBOL "="; dsc = QSTRING ->
590 (Symbol (symbol, int_of_string instance),
591 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
593 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
594 SYMBOL "="; dsc = QSTRING ->
595 (Num (int_of_string instance),
596 DisambiguateChoices.lookup_num_by_dsc dsc)
606 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
609 (* vim:set encoding=utf8: *)