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)"
106 GLOBAL: term term0 tactic tactical tactical0 command script;
111 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
115 [ s = SYMBOL "_" -> None
116 | t = term -> Some t ]
119 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
120 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
121 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
122 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
128 | "CProp" -> `CProp ]
131 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
132 (Cic.Name i, Some typ)
133 | i = IDENT -> (Cic.Name i, None)
138 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
141 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
148 substituted_name: [ (* a subs.name is an explicit substitution subject *)
149 [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
150 | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
153 name: [ (* as substituted_name with no explicit substitution *)
154 [ s = [ IDENT | SYMBOL ] -> s ]
157 [ n = name -> (n, [])
158 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
166 PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
167 ty = term; PAREN ")" ->
170 index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
171 ty = OPT [ SYMBOL ":" ; t = term -> t ];
172 SYMBOL <:unicode<def>> (* ≝ *);
174 let rec list_of_binder binder ty final_term = function
177 CicAst.Binder (binder, (Cic.Name name, Some ty),
178 list_of_binder binder ty final_term tl)
180 let rec binder_of_arg_list binder final_term = function
183 list_of_binder binder ty
184 (binder_of_arg_list binder final_term tl) l
186 let t1' = binder_of_arg_list `Lambda t1 args in
190 | Some ty -> Some (binder_of_arg_list `Pi ty args)
192 let rec get_position_of name n = function
198 (get_position_of name (n+1) tl)
200 let rec find_arg name n = function
201 | [] -> (fail loc (sprintf "Argument %s not found" name))
203 let (got,len) = get_position_of name 0 l in
205 | None -> (find_arg name (n+len) tl)
206 | Some where -> n + where)
209 (match index_name with
211 | (Some name) -> find_arg name 0 args)
213 ((Cic.Name name,ty'), t1', index)
216 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
217 term0: [ [ t = term; EOI -> return_term loc t ] ];
220 [ "let"; var = typed_name;
221 SYMBOL <:unicode<def>> (* ≝ *);
222 t1 = term; "in"; t2 = term ->
223 return_term loc (CicAst.LetIn (var, t1, t2))
224 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
225 defs = let_defs; "in"; body = term ->
226 return_term loc (CicAst.LetRec (ind_kind, defs, body))
229 [ t1 = term; t2 = term ->
230 let rec aux = function
231 | CicAst.Appl (hd :: tl) -> aux hd @ tl
234 CicAst.Appl (aux t1 @ [t2])
240 [ vars = LIST1 IDENT SEP SYMBOL ",";
241 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
242 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
243 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
245 SYMBOL "."; body = term ->
249 let name = name_of_string var in
250 CicAst.Binder (b, (name, typ), body))
253 return_term loc binder
254 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
256 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
258 | "logic_add" LEFTA [ (* nothing here by default *) ]
259 | "logic_mult" LEFTA [ (* nothing here by default *) ]
260 | "logic_inv" NONA [ (* nothing here by default *) ]
262 [ t1 = term; SYMBOL "="; t2 = term ->
263 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
265 | "add" LEFTA [ (* nothing here by default *) ]
266 | "mult" LEFTA [ (* nothing here by default *) ]
267 | "power" LEFTA [ (* nothing here by default *) ]
268 | "inv" NONA [ (* nothing here by default *) ]
270 [ sort = sort -> CicAst.Sort sort
271 | n = substituted_name -> return_term loc n
272 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
273 | IMPLICIT -> return_term loc CicAst.Implicit
276 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
281 int_of_string (String.sub m 1 (String.length m - 1))
282 with Failure "int_of_string" ->
283 fail loc ("Invalid meta variable number: " ^ m)
285 return_term loc (CicAst.Meta (index, substs))
286 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
288 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
292 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
294 ((lhs: CicAst.case_pattern), rhs)
298 (CicAst.Case (t, indty_ident, outtyp, patterns))
299 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
300 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
301 | PAREN "("; t = term; PAREN ")" -> return_term loc t
305 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
307 tactic_term: [ [ t = term -> t ] ];
309 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
312 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
315 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
316 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
317 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
320 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
321 return_tactic loc (TacticAst.Absurd t)
322 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
323 return_tactic loc (TacticAst.Apply t)
324 | [ IDENT "assumption" | IDENT "Assumption" ] ->
325 return_tactic loc TacticAst.Assumption
326 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
327 | [ IDENT "change" | IDENT "Change" ];
328 t1 = tactic_term; "with"; t2 = tactic_term;
329 where = tactic_where ->
330 return_tactic loc (TacticAst.Change (t1, t2, where))
331 (* TODO Change_pattern *)
332 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
333 return_tactic loc TacticAst.Contradiction
334 | [ IDENT "cut" | IDENT "Cut" ];
335 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
336 | [ IDENT "decompose" | IDENT "Decompose" ];
337 principles = ident_list1; where = IDENT ->
338 return_tactic loc (TacticAst.Decompose (where, principles))
339 | [ IDENT "discriminate" | IDENT "Discriminate" ];
341 return_tactic loc (TacticAst.Discriminate hyp)
342 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
343 return_tactic loc (TacticAst.ElimType t)
344 | [ IDENT "elim" | IDENT "Elim" ];
346 using = OPT [ "using"; using = tactic_term -> using ] ->
347 return_tactic loc (TacticAst.Elim (t1, using))
348 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
349 return_tactic loc (TacticAst.Exact t)
350 | [ IDENT "exists" | IDENT "Exists" ] ->
351 return_tactic loc TacticAst.Exists
352 | [ IDENT "fold" | IDENT "Fold" ];
353 kind = reduction_kind; t = tactic_term ->
354 return_tactic loc (TacticAst.Fold (kind, t))
355 | [ IDENT "fourier" | IDENT "Fourier" ] ->
356 return_tactic loc TacticAst.Fourier
357 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
358 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
359 return_tactic loc (TacticAst.Injection ident)
360 | [ IDENT "intros" | IDENT "Intros" ];
361 num = OPT [ num = int -> num ];
362 idents = OPT ident_list0 ->
363 let idents = match idents with None -> [] | Some idents -> idents in
364 return_tactic loc (TacticAst.Intros (num, idents))
365 | [ IDENT "intro" | IDENT "Intro" ] ->
366 return_tactic loc (TacticAst.Intros (None, []))
367 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
369 t = tactic_term; "in"; where = IDENT ->
370 return_tactic loc (TacticAst.LetIn (t, where))
371 | kind = reduction_kind;
373 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
376 terms = LIST0 term SEP SYMBOL "," ->
378 (match (pat, terms) with
379 | None, [] -> TacticAst.Reduce (kind, None)
380 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
381 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
382 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
384 return_tactic loc tac
385 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
386 return_tactic loc TacticAst.Reflexivity
387 | [ IDENT "replace" | IDENT "Replace" ];
388 t1 = tactic_term; "with"; t2 = tactic_term ->
389 return_tactic loc (TacticAst.Replace (t1, t2))
391 (* TODO Replace_pattern *)
392 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
393 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
394 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
395 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
396 return_tactic loc TacticAst.Symmetry
397 | [ IDENT "transitivity" | IDENT "Transitivity" ];
399 return_tactic loc (TacticAst.Transitivity t)
402 tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
405 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
407 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
410 | _ -> return_tactical loc (TacticAst.Seq tactics))
414 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
415 return_tactical loc (TacticAst.Then (tac, tacs))
418 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
419 return_tactical loc (TacticAst.Do (count, tac))
420 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
421 return_tactical loc (TacticAst.Repeat tac)
424 [ [ IDENT "tries" | IDENT "Tries" ];
425 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
426 return_tactical loc (TacticAst.Tries tacs)
427 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
428 return_tactical loc (TacticAst.Try tac)
429 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
430 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
431 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
432 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
435 theorem_flavour: [ (* all flavours but Goal *)
436 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
437 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
438 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
439 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
440 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
444 fst_name = IDENT; params = LIST0 [
445 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
446 typ = term; PAREN ")" -> (names, typ) ];
447 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
448 fst_constructors = LIST0 constructor SEP SYMBOL "|";
451 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
452 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
453 (name, true, typ, constructors) ] SEP "with" -> types
457 (fun (names, typ) acc ->
458 (List.map (fun name -> (name, typ)) names) @ acc)
461 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
462 let tl_ind_types = match tl with None -> [] | Some types -> types in
463 let ind_types = fst_ind_type :: tl_ind_types in
467 [ [ IDENT "Env" | IDENT "env" | IDENT "Environment" | IDENT "environment" ]
469 | [ IDENT "Coer" | IDENT "coer" | IDENT "Coercions" | IDENT "coercions" ]
474 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
475 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
476 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
477 | [ IDENT "qed" | IDENT "Qed" ] ->
478 return_command loc (TacticAst.Qed None)
479 | [ IDENT "print" | IDENT "Print" ];
480 print_kind = print_kind ->
481 return_command loc (TacticAst.Print print_kind)
482 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
483 return_command loc (TacticAst.Qed (Some name))
484 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
485 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
486 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
487 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
491 | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
492 | ((Cic.Name name,None),_,_) :: _ ->
493 fail loc ("No type given for " ^ name)
496 let body = CicAst.Ident (name,None) in
497 TacticAst.Theorem(`Definition, Some name, ty,
498 Some (CicAst.LetRec (ind_kind, defs, body)))
500 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
501 let (params, ind_types) = spec in
502 return_command loc (TacticAst.Inductive (params, ind_types))
503 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
504 let (params, ind_types) = spec in
505 let ind_types = (* set inductive flags to false (coinductive) *)
506 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
509 return_command loc (TacticAst.Inductive (params, ind_types))
510 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT ->
511 return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
512 | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI ->
513 return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
514 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
515 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
516 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
517 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
518 return_command loc (TacticAst.Undo (int_opt steps))
519 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
520 return_command loc (TacticAst.Redo (int_opt steps))
521 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
522 return_command loc (TacticAst.Baseuri uri)
523 | [ IDENT "basedir" | IDENT "Basedir" ]; uri = OPT QSTRING ->
524 return_command loc (TacticAst.Basedir uri)
525 | [ IDENT "check" | IDENT "Check" ]; t = term ->
526 return_command loc (TacticAst.Check t)
528 | [ IDENT "alias" | IDENT "Alias" ]; spec = alias_spec ->
529 return_command loc (TacticAst.Alias spec)
534 [ cmd = tactical0 -> Command cmd
535 (* | s = COMMENT -> Comment (loc, s) *)
538 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
541 let exc_located_wrapper f =
545 | Stdpp.Exc_located (floc, Stream.Error msg) ->
546 raise (Parse_error (floc, msg))
547 | Stdpp.Exc_located (floc, exn) ->
548 raise (Parse_error (floc, (Printexc.to_string exn)))
550 let parse_term stream =
551 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
552 let parse_tactic stream =
553 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
554 let parse_tactical stream =
555 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
556 let parse_script stream =
557 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
561 (** {2 Interface for gTopLevel} *)
563 module EnvironmentP3 =
569 let aliases_grammar = Grammar.gcreate cic_lexer
570 let aliases = Grammar.Entry.create aliases_grammar "aliases"
575 (fun domain_item (dsc, _) acc ->
577 match domain_item with
578 | Id id -> sprintf "alias id %s = %s" id dsc
579 | Symbol (symb, instance) ->
580 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
583 sprintf "alias num (instance %d) = \"%s\"" instance dsc
588 String.concat "\n" (List.sort compare aliases)
592 aliases: [ (* build an environment from an aliases list *)
593 [ aliases = LIST0 alias; EOI ->
595 (fun env (domain_item, codomain_item) ->
596 Environment.add domain_item codomain_item env)
597 Environment.empty aliases
600 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
603 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
604 (Id id, choice_of_uri uri)
605 | IDENT "symbol"; symbol = QSTRING;
606 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
607 SYMBOL "="; dsc = QSTRING ->
608 (Symbol (symbol, int_of_string instance),
609 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
611 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
612 SYMBOL "="; dsc = QSTRING ->
613 (Num (int_of_string instance),
614 DisambiguateChoices.lookup_num_by_dsc dsc)
624 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
627 (* vim:set encoding=utf8: *)