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
38 (** does the lexer return COMMENT tokens? *)
39 let return_comments = false
43 open DisambiguateTypes
45 exception Parse_error of Token.flocation * string
47 let cic_lexer = CicTextualLexer2.cic_lexer ~comments:return_comments ()
49 let fresh_num_instance =
51 if use_fresh_num_instances then
52 (fun () -> incr n; !n)
56 let choice_of_uri uri =
57 let term = CicUtil.term_of_uri uri in
58 (uri, (fun _ _ _ -> term))
60 let grammar = Grammar.gcreate cic_lexer
62 let term = Grammar.Entry.create grammar "term"
63 let term0 = Grammar.Entry.create grammar "term0"
64 let tactic = Grammar.Entry.create grammar "tactic"
65 let tactical = Grammar.Entry.create grammar "tactical"
66 let tactical0 = Grammar.Entry.create grammar "tactical0"
67 let command = Grammar.Entry.create grammar "command"
68 let script = Grammar.Entry.create grammar "script"
70 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
71 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
72 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
73 let return_command loc cmd = cmd (* TODO ZACK FIXME uhm ... why we drop loc? *)
76 let (x, y) = CicAst.loc_of_floc floc in
77 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
79 let name_of_string = function
80 | "_" -> Cic.Anonymous
83 let string_of_name = function
84 | Cic.Anonymous -> "_"
87 let int_opt = function
89 | Some lexeme -> Some (int_of_string lexeme)
91 (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
92 * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
93 * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
94 * type in a block of mutual inductive types.
96 * This function performs the expansion foo.ind -> foo#xpointer..., if needed
98 let ind_expansion uri =
99 let len = String.length uri in
100 if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
101 uri ^ "#xpointer(1/1)"
105 let mk_binder_ast binder typ vars body =
108 let name = name_of_string var in
109 CicAst.Binder (binder, (name, typ), body))
113 GLOBAL: term term0 tactic tactical tactical0 command script;
118 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
122 [ s = SYMBOL "_" -> None
123 | t = term -> Some t ]
126 [ SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
127 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
128 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
130 binder_high: [ [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda ] ];
135 | "CProp" -> `CProp ]
138 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
139 (Cic.Name i, Some typ)
140 | i = IDENT -> (Cic.Name i, None)
145 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
148 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
155 substituted_name: [ (* a subs.name is an explicit substitution subject *)
156 [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
157 | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
160 name: [ (* as substituted_name with no explicit substitution *)
161 [ s = [ IDENT | SYMBOL ] -> s ]
164 [ n = name -> (n, [])
165 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
173 PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
174 ty = term; PAREN ")" ->
177 index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
178 ty = OPT [ SYMBOL ":" ; t = term -> t ];
179 SYMBOL <:unicode<def>> (* ≝ *);
181 let rec list_of_binder binder ty final_term = function
184 CicAst.Binder (binder, (Cic.Name name, Some ty),
185 list_of_binder binder ty final_term tl)
187 let rec binder_of_arg_list binder final_term = function
190 list_of_binder binder ty
191 (binder_of_arg_list binder final_term tl) l
193 let t1' = binder_of_arg_list `Lambda t1 args in
197 | Some ty -> Some (binder_of_arg_list `Pi ty args)
199 let rec get_position_of name n = function
205 (get_position_of name (n+1) tl)
207 let rec find_arg name n = function
208 | [] -> (fail loc (sprintf "Argument %s not found" name))
210 let (got,len) = get_position_of name 0 l in
212 | None -> (find_arg name (n+len) tl)
213 | Some where -> n + where)
216 (match index_name with
218 | (Some name) -> find_arg name 0 args)
220 ((Cic.Name name,ty'), t1', index)
223 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
225 [ vars = LIST1 IDENT SEP SYMBOL ",";
226 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
227 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
228 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
231 term0: [ [ t = term; EOI -> return_term loc t ] ];
234 [ "let"; var = typed_name;
235 SYMBOL <:unicode<def>> (* ≝ *);
236 t1 = term; "in"; t2 = term ->
237 return_term loc (CicAst.LetIn (var, t1, t2))
238 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
239 defs = let_defs; "in"; body = term ->
240 return_term loc (CicAst.LetRec (ind_kind, defs, body))
244 b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
245 let binder = mk_binder_ast b typ vars body in
246 return_term loc binder
247 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
248 return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
250 | "logic_add" LEFTA [ (* nothing here by default *) ]
251 | "logic_mult" LEFTA [ (* nothing here by default *) ]
252 | "logic_inv" NONA [ (* nothing here by default *) ]
254 [ t1 = term; SYMBOL "="; t2 = term ->
255 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
257 | "add" LEFTA [ (* nothing here by default *) ]
258 | "mult" LEFTA [ (* nothing here by default *) ]
259 | "power" LEFTA [ (* nothing here by default *) ]
260 | "inv" NONA [ (* nothing here by default *) ]
262 [ t1 = term; t2 = term ->
263 let rec aux = function
264 | CicAst.Appl (hd :: tl) -> aux hd @ tl
267 CicAst.Appl (aux t1 @ [t2])
271 b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
272 let binder = mk_binder_ast b typ vars body in
273 return_term loc binder
276 [ sort = sort -> CicAst.Sort sort
277 | n = substituted_name -> return_term loc n
278 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
279 | IMPLICIT -> return_term loc CicAst.Implicit
282 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
287 int_of_string (String.sub m 1 (String.length m - 1))
288 with Failure "int_of_string" ->
289 fail loc ("Invalid meta variable number: " ^ m)
291 return_term loc (CicAst.Meta (index, substs))
292 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
294 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
298 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
300 ((lhs: CicAst.case_pattern), rhs)
304 (CicAst.Case (t, indty_ident, outtyp, patterns))
305 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
306 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
307 | PAREN "("; t = term; PAREN ")" -> return_term loc t
311 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
313 tactic_term: [ [ t = term -> t ] ];
315 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
318 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
321 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
322 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
323 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
326 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
327 return_tactic loc (TacticAst.Absurd t)
328 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
329 return_tactic loc (TacticAst.Apply t)
330 | [ IDENT "assumption" | IDENT "Assumption" ] ->
331 return_tactic loc TacticAst.Assumption
332 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
333 | [ IDENT "change" | IDENT "Change" ];
334 t1 = tactic_term; "with"; t2 = tactic_term;
335 where = tactic_where ->
336 return_tactic loc (TacticAst.Change (t1, t2, where))
337 (* TODO Change_pattern *)
338 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
339 return_tactic loc TacticAst.Contradiction
340 | [ IDENT "cut" | IDENT "Cut" ];
341 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
342 | [ IDENT "decompose" | IDENT "Decompose" ];
343 principles = ident_list1; where = IDENT ->
344 return_tactic loc (TacticAst.Decompose (where, principles))
345 | [ IDENT "discriminate" | IDENT "Discriminate" ];
347 return_tactic loc (TacticAst.Discriminate hyp)
348 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
349 return_tactic loc (TacticAst.ElimType t)
350 | [ IDENT "elim" | IDENT "Elim" ];
352 using = OPT [ "using"; using = tactic_term -> using ] ->
353 return_tactic loc (TacticAst.Elim (t1, using))
354 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
355 return_tactic loc (TacticAst.Exact t)
356 | [ IDENT "exists" | IDENT "Exists" ] ->
357 return_tactic loc TacticAst.Exists
358 | [ IDENT "fold" | IDENT "Fold" ];
359 kind = reduction_kind; t = tactic_term ->
360 return_tactic loc (TacticAst.Fold (kind, t))
361 | [ IDENT "fourier" | IDENT "Fourier" ] ->
362 return_tactic loc TacticAst.Fourier
363 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
364 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
365 return_tactic loc (TacticAst.Injection ident)
366 | [ IDENT "intros" | IDENT "Intros" ];
367 num = OPT [ num = int -> num ];
368 idents = OPT ident_list0 ->
369 let idents = match idents with None -> [] | Some idents -> idents in
370 return_tactic loc (TacticAst.Intros (num, idents))
371 | [ IDENT "intro" | IDENT "Intro" ] ->
372 return_tactic loc (TacticAst.Intros (None, []))
373 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
375 t = tactic_term; "in"; where = IDENT ->
376 return_tactic loc (TacticAst.LetIn (t, where))
377 | kind = reduction_kind;
379 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
382 terms = LIST0 term SEP SYMBOL "," ->
384 (match (pat, terms) with
385 | None, [] -> TacticAst.Reduce (kind, None)
386 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
387 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
388 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
390 return_tactic loc tac
391 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
392 return_tactic loc TacticAst.Reflexivity
393 | [ IDENT "replace" | IDENT "Replace" ];
394 t1 = tactic_term; "with"; t2 = tactic_term ->
395 return_tactic loc (TacticAst.Replace (t1, t2))
397 (* TODO Replace_pattern *)
398 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
399 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
400 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
401 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
402 return_tactic loc TacticAst.Symmetry
403 | [ IDENT "transitivity" | IDENT "Transitivity" ];
405 return_tactic loc (TacticAst.Transitivity t)
408 tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
411 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
413 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
416 | _ -> return_tactical loc (TacticAst.Seq tactics))
420 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
421 return_tactical loc (TacticAst.Then (tac, tacs))
424 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
425 return_tactical loc (TacticAst.Do (count, tac))
426 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
427 return_tactical loc (TacticAst.Repeat tac)
430 [ [ IDENT "tries" | IDENT "Tries" ];
431 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
432 return_tactical loc (TacticAst.Tries tacs)
433 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
434 return_tactical loc (TacticAst.Try tac)
435 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
436 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
437 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
438 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
441 theorem_flavour: [ (* all flavours but Goal *)
442 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
443 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
444 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
445 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
446 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
450 fst_name = IDENT; params = LIST0 [
451 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
452 typ = term; PAREN ")" -> (names, typ) ];
453 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
454 fst_constructors = LIST0 constructor SEP SYMBOL "|";
457 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
458 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
459 (name, true, typ, constructors) ] SEP "with" -> types
463 (fun (names, typ) acc ->
464 (List.map (fun name -> (name, typ)) names) @ acc)
467 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
468 let tl_ind_types = match tl with None -> [] | Some types -> types in
469 let ind_types = fst_ind_type :: tl_ind_types in
473 [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
475 | [ IDENT "Coer" | IDENT "coer" | IDENT "Coercions" | IDENT "coercions" ]
480 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
481 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
482 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
483 | [ IDENT "qed" | IDENT "Qed" ] ->
484 return_command loc (TacticAst.Qed None)
485 | [ IDENT "print" | IDENT "Print" ];
486 print_kind = print_kind ->
487 return_command loc (TacticAst.Print print_kind)
488 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
489 return_command loc (TacticAst.Qed (Some name))
490 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
491 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
492 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
493 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
497 | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
498 | ((Cic.Name name,None),_,_) :: _ ->
499 fail loc ("No type given for " ^ name)
502 let body = CicAst.Ident (name,None) in
503 TacticAst.Theorem(`Definition, Some name, ty,
504 Some (CicAst.LetRec (ind_kind, defs, body)))
506 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
507 let (params, ind_types) = spec in
508 return_command loc (TacticAst.Inductive (params, ind_types))
509 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
510 let (params, ind_types) = spec in
511 let ind_types = (* set inductive flags to false (coinductive) *)
512 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
515 return_command loc (TacticAst.Inductive (params, ind_types))
516 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT ->
517 return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
518 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI ->
519 return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
520 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
521 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
522 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
523 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
524 return_command loc (TacticAst.Undo (int_opt steps))
525 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
526 return_command loc (TacticAst.Redo (int_opt steps))
527 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
528 return_command loc (TacticAst.Baseuri uri)
529 | [ IDENT "basedir" | IDENT "Basedir" ]; uri = OPT QSTRING ->
530 return_command loc (TacticAst.Basedir uri)
531 | [ IDENT "check" | IDENT "Check" ]; t = term ->
532 return_command loc (TacticAst.Check t)
534 | [ IDENT "alias" | IDENT "Alias" ]; spec = alias_spec ->
535 return_command loc (TacticAst.Alias spec)
540 [ cmd = tactical0 -> Command cmd
541 (* | s = COMMENT -> Comment (loc, s) *)
544 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
547 let exc_located_wrapper f =
551 | Stdpp.Exc_located (floc, Stream.Error msg) ->
552 raise (Parse_error (floc, msg))
553 | Stdpp.Exc_located (floc, exn) ->
554 raise (Parse_error (floc, (Printexc.to_string exn)))
556 let parse_term stream =
557 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
558 let parse_tactic stream =
559 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
560 let parse_tactical stream =
561 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
562 let parse_script stream =
563 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
567 (** {2 Interface for gTopLevel} *)
569 module EnvironmentP3 =
575 let aliases_grammar = Grammar.gcreate cic_lexer
576 let aliases = Grammar.Entry.create aliases_grammar "aliases"
581 (fun domain_item (dsc, _) acc ->
583 match domain_item with
584 | Id id -> sprintf "alias id %s = %s" id dsc
585 | Symbol (symb, instance) ->
586 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
589 sprintf "alias num (instance %d) = \"%s\"" instance dsc
594 String.concat "\n" (List.sort compare aliases)
598 aliases: [ (* build an environment from an aliases list *)
599 [ aliases = LIST0 alias; EOI ->
601 (fun env (domain_item, codomain_item) ->
602 Environment.add domain_item codomain_item env)
603 Environment.empty aliases
606 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
609 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
610 (Id id, choice_of_uri uri)
611 | IDENT "symbol"; symbol = QSTRING;
612 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
613 SYMBOL "="; dsc = QSTRING ->
614 (Symbol (symbol, int_of_string instance),
615 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
617 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
618 SYMBOL "="; dsc = QSTRING ->
619 (Num (int_of_string instance),
620 DisambiguateChoices.lookup_num_by_dsc dsc)
630 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
633 (* vim:set encoding=utf8: *)