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 ty = OPT [ SYMBOL ":" ; t = term -> t ];
167 SYMBOL <:unicode<def>> (* ≝ *);
169 let rec list_of_binder binder ty final_term = function
172 CicAst.Binder (binder, (Cic.Name name, Some ty),
173 list_of_binder binder ty final_term tl)
175 let rec binder_of_arg_list binder final_term = function
178 list_of_binder binder ty
179 (binder_of_arg_list binder final_term tl) l
181 let t1' = binder_of_arg_list `Lambda t1 args in
185 | Some ty -> Some (binder_of_arg_list `Pi ty args)
187 let rec get_position_of name n = function
193 (get_position_of name (n+1) tl)
195 let rec find_arg name n = function
196 | [] -> (fail loc (sprintf "Argument %s not found" name))
198 let (got,len) = get_position_of name 0 l in
200 | None -> (find_arg name (n+len) tl)
201 | Some where -> n + where)
204 (match index_name with
206 | (Some name) -> find_arg name 0 args)
208 ((Cic.Name name,ty'), t1', index)
211 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
212 term0: [ [ t = term; EOI -> return_term loc t ] ];
215 [ "let"; var = typed_name;
216 SYMBOL <:unicode<def>> (* ≝ *);
217 t1 = term; "in"; t2 = term ->
218 return_term loc (CicAst.LetIn (var, t1, t2))
219 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
220 defs = let_defs; "in"; body = term ->
221 return_term loc (CicAst.LetRec (ind_kind, defs, body))
227 [ vars = LIST1 IDENT SEP SYMBOL ",";
228 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
229 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
230 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
232 SYMBOL "."; body = term ->
236 let name = name_of_string var in
237 CicAst.Binder (b, (name, typ), body))
240 return_term loc binder
241 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
243 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
245 | "logic_add" LEFTA [ (* nothing here by default *) ]
246 | "logic_mult" LEFTA [ (* nothing here by default *) ]
247 | "logic_inv" NONA [ (* nothing here by default *) ]
249 [ t1 = term; SYMBOL "="; t2 = term ->
250 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
252 | "add" LEFTA [ (* nothing here by default *) ]
253 | "mult" LEFTA [ (* nothing here by default *) ]
254 | "power" LEFTA [ (* nothing here by default *) ]
255 | "inv" NONA [ (* nothing here by default *) ]
257 [ t1 = term; t2 = term ->
258 let rec aux = function
259 | CicAst.Appl (hd :: tl) -> aux hd @ tl
262 CicAst.Appl (aux t1 @ [t2])
265 [ sort = sort -> CicAst.Sort sort
266 | n = substituted_name -> return_term loc n
267 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
268 | IMPLICIT -> return_term loc CicAst.Implicit
271 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
276 int_of_string (String.sub m 1 (String.length m - 1))
277 with Failure "int_of_string" ->
278 fail loc ("Invalid meta variable number: " ^ m)
280 return_term loc (CicAst.Meta (index, substs))
281 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
283 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
287 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
289 ((lhs: CicAst.case_pattern), rhs)
293 (CicAst.Case (t, indty_ident, outtyp, patterns))
294 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
295 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
296 | PAREN "("; t = term; PAREN ")" -> return_term loc t
300 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
302 tactic_term: [ [ t = term -> t ] ];
304 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
307 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
310 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
311 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
312 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
315 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
316 return_tactic loc (TacticAst.Absurd t)
317 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
318 return_tactic loc (TacticAst.Apply t)
319 | [ IDENT "assumption" | IDENT "Assumption" ] ->
320 return_tactic loc TacticAst.Assumption
321 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
322 | [ IDENT "change" | IDENT "Change" ];
323 t1 = tactic_term; "with"; t2 = tactic_term;
324 where = tactic_where ->
325 return_tactic loc (TacticAst.Change (t1, t2, where))
326 (* TODO Change_pattern *)
327 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
328 return_tactic loc TacticAst.Contradiction
329 | [ IDENT "cut" | IDENT "Cut" ];
330 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
331 | [ IDENT "decompose" | IDENT "Decompose" ];
332 principles = ident_list1; where = IDENT ->
333 return_tactic loc (TacticAst.Decompose (where, principles))
334 | [ IDENT "discriminate" | IDENT "Discriminate" ];
336 return_tactic loc (TacticAst.Discriminate hyp)
337 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
338 return_tactic loc (TacticAst.ElimType t)
339 | [ IDENT "elim" | IDENT "Elim" ];
341 using = OPT [ "using"; using = tactic_term -> using ] ->
342 return_tactic loc (TacticAst.Elim (t1, using))
343 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
344 return_tactic loc (TacticAst.Exact t)
345 | [ IDENT "exists" | IDENT "Exists" ] ->
346 return_tactic loc TacticAst.Exists
347 | [ IDENT "fold" | IDENT "Fold" ];
348 kind = reduction_kind; t = tactic_term ->
349 return_tactic loc (TacticAst.Fold (kind, t))
350 | [ IDENT "fourier" | IDENT "Fourier" ] ->
351 return_tactic loc TacticAst.Fourier
352 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
353 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
354 return_tactic loc (TacticAst.Injection ident)
355 | [ IDENT "intros" | IDENT "Intros" ];
356 num = OPT [ num = int -> num ];
357 idents = OPT ident_list0 ->
358 let idents = match idents with None -> [] | Some idents -> idents in
359 return_tactic loc (TacticAst.Intros (num, idents))
360 | [ IDENT "intro" | IDENT "Intro" ] ->
361 return_tactic loc (TacticAst.Intros (Some 1, []))
362 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
364 t = tactic_term; "in"; where = IDENT ->
365 return_tactic loc (TacticAst.LetIn (t, where))
366 | kind = reduction_kind;
368 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
371 terms = LIST0 term SEP SYMBOL "," ->
373 (match (pat, terms) with
374 | None, [] -> TacticAst.Reduce (kind, None)
375 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
376 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
377 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
379 return_tactic loc tac
380 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
381 return_tactic loc TacticAst.Reflexivity
382 | [ IDENT "replace" | IDENT "Replace" ];
383 t1 = tactic_term; "with"; t2 = tactic_term ->
384 return_tactic loc (TacticAst.Replace (t1, t2))
386 (* TODO Replace_pattern *)
387 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
388 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
389 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
390 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
391 return_tactic loc TacticAst.Symmetry
392 | [ IDENT "transitivity" | IDENT "Transitivity" ];
394 return_tactic loc (TacticAst.Transitivity t)
397 tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
400 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
402 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
403 return_tactical loc (TacticAst.Seq tactics)
407 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
408 return_tactical loc (TacticAst.Then (tac, tacs))
411 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
412 return_tactical loc (TacticAst.Do (count, tac))
413 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
414 return_tactical loc (TacticAst.Repeat tac)
417 [ [ IDENT "tries" | IDENT "Tries" ];
418 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
419 return_tactical loc (TacticAst.Tries tacs)
420 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
421 return_tactical loc (TacticAst.Try tac)
422 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
423 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
424 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
425 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
428 theorem_flavour: [ (* all flavours but Goal *)
429 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
430 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
431 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
432 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
433 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
437 fst_name = IDENT; params = LIST0 [
438 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
439 typ = term; PAREN ")" -> (names, typ) ];
440 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
441 fst_constructors = LIST0 constructor SEP SYMBOL "|";
444 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
445 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
446 (name, true, typ, constructors) ] SEP "with" -> types
450 (fun (names, typ) acc ->
451 (List.map (fun name -> (name, typ)) names) @ acc)
454 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
455 let tl_ind_types = match tl with None -> [] | Some types -> types in
456 let ind_types = fst_ind_type :: tl_ind_types in
460 [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
462 | [ IDENT "Coer" | IDENT "coer" | IDENT "Coercions" | IDENT "coercions" ]
467 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
468 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
469 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
470 | [ IDENT "qed" | IDENT "Qed" ] ->
471 return_command loc (TacticAst.Qed None)
472 | [ IDENT "print" | IDENT "Print" ];
473 print_kind = print_kind ->
474 return_command loc (TacticAst.Print print_kind)
475 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
476 return_command loc (TacticAst.Qed (Some name))
477 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
478 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
479 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
480 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
484 | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
485 | ((Cic.Name name,None),_,_) :: _ ->
486 fail loc ("No type given for " ^ name)
489 let body = CicAst.Ident (name,None) in
490 TacticAst.Theorem(`Definition, Some name, ty,
491 Some (CicAst.LetRec (ind_kind, defs, body)))
493 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
494 let (params, ind_types) = spec in
495 return_command loc (TacticAst.Inductive (params, ind_types))
496 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
497 let (params, ind_types) = spec in
498 let ind_types = (* set inductive flags to false (coinductive) *)
499 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
502 return_command loc (TacticAst.Inductive (params, ind_types))
503 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT ->
504 return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
505 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI ->
506 return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
507 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
508 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
509 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
510 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
511 return_command loc (TacticAst.Undo (int_opt steps))
512 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
513 return_command loc (TacticAst.Redo (int_opt steps))
514 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
515 return_command loc (TacticAst.Baseuri uri)
516 | [ IDENT "basedir" | IDENT "Basedir" ]; uri = OPT QSTRING ->
517 return_command loc (TacticAst.Basedir uri)
518 | [ IDENT "check" | IDENT "Check" ]; t = term ->
519 return_command loc (TacticAst.Check t)
521 | [ IDENT "alias" | IDENT "Alias" ]; spec = alias_spec ->
522 return_command loc (TacticAst.Alias spec)
527 [ cmd = tactical0 -> Command cmd
528 | s = COMMENT -> Comment (loc, s)
531 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
534 let exc_located_wrapper f =
538 | Stdpp.Exc_located (floc, Stream.Error msg) ->
539 raise (Parse_error (floc, msg))
540 | Stdpp.Exc_located (floc, exn) ->
541 raise (Parse_error (floc, (Printexc.to_string exn)))
543 let parse_term stream =
544 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
545 let parse_tactic stream =
546 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
547 let parse_tactical stream =
548 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
549 let parse_script stream =
550 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
554 (** {2 Interface for gTopLevel} *)
556 module EnvironmentP3 =
562 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
563 let aliases = Grammar.Entry.create aliases_grammar "aliases"
568 (fun domain_item (dsc, _) acc ->
570 match domain_item with
571 | Id id -> sprintf "alias id %s = %s" id dsc
572 | Symbol (symb, instance) ->
573 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
576 sprintf "alias num (instance %d) = \"%s\"" instance dsc
581 String.concat "\n" (List.sort compare aliases)
585 aliases: [ (* build an environment from an aliases list *)
586 [ aliases = LIST0 alias; EOI ->
588 (fun env (domain_item, codomain_item) ->
589 Environment.add domain_item codomain_item env)
590 Environment.empty aliases
593 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
596 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
597 (Id id, choice_of_uri uri)
598 | IDENT "symbol"; symbol = QSTRING;
599 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
600 SYMBOL "="; dsc = QSTRING ->
601 (Symbol (symbol, int_of_string instance),
602 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
604 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
605 SYMBOL "="; dsc = QSTRING ->
606 (Num (int_of_string instance),
607 DisambiguateChoices.lookup_num_by_dsc dsc)
617 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
620 (* vim:set encoding=utf8: *)