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 alias_spec = Grammar.Entry.create grammar "alias_spec"
69 let macro = Grammar.Entry.create grammar "macro"
70 let script = Grammar.Entry.create grammar "script"
71 let statement = Grammar.Entry.create grammar "statement"
73 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
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)
93 Pervasives.int_of_string s
95 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
97 (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
98 * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
99 * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
100 * type in a block of mutual inductive types.
102 * This function performs the expansion foo.ind -> foo#xpointer..., if needed
104 let ind_expansion uri =
105 let len = String.length uri in
106 if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
107 uri ^ "#xpointer(1/1)"
111 let mk_binder_ast binder typ vars body =
114 let name = name_of_string var in
115 CicAst.Binder (binder, (name, typ), body))
119 GLOBAL: term term0 statement;
124 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
128 [ s = SYMBOL "_" -> None
129 | t = term -> Some t ]
132 [ SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
133 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
134 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
136 binder_high: [ [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda ] ];
141 | "CProp" -> `CProp ]
144 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
145 (Cic.Name i, Some typ)
146 | i = IDENT -> (Cic.Name i, None)
151 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
154 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
161 substituted_name: [ (* a subs.name is an explicit substitution subject *)
162 [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
163 | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
166 name: [ (* as substituted_name with no explicit substitution *)
167 [ s = [ IDENT | SYMBOL ] -> s ]
170 [ n = name -> (n, [])
171 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
179 PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
180 ty = term; PAREN ")" ->
183 index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
184 ty = OPT [ SYMBOL ":" ; t = term -> t ];
185 SYMBOL <:unicode<def>> (* ≝ *);
187 let rec list_of_binder binder ty final_term = function
190 CicAst.Binder (binder, (Cic.Name name, Some ty),
191 list_of_binder binder ty final_term tl)
193 let rec binder_of_arg_list binder final_term = function
196 list_of_binder binder ty
197 (binder_of_arg_list binder final_term tl) l
199 let t1' = binder_of_arg_list `Lambda t1 args in
203 | Some ty -> Some (binder_of_arg_list `Pi ty args)
205 let rec get_position_of name n = function
211 (get_position_of name (n+1) tl)
213 let rec find_arg name n = function
214 | [] -> (fail loc (sprintf "Argument %s not found" name))
216 let (got,len) = get_position_of name 0 l in
218 | None -> (find_arg name (n+len) tl)
219 | Some where -> n + where)
222 (match index_name with
224 | (Some name) -> find_arg name 0 args)
226 ((Cic.Name name,ty'), t1', index)
229 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
231 [ vars = LIST1 IDENT SEP SYMBOL ",";
232 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
233 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
234 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
237 term0: [ [ t = term; EOI -> return_term loc t ] ];
240 [ "let"; var = typed_name;
241 SYMBOL <:unicode<def>> (* ≝ *);
242 t1 = term; "in"; t2 = term ->
243 return_term loc (CicAst.LetIn (var, t1, t2))
244 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
245 defs = let_defs; "in"; body = term ->
246 return_term loc (CicAst.LetRec (ind_kind, defs, body))
250 b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
251 let binder = mk_binder_ast b typ vars body in
252 return_term loc binder
253 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
254 return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
256 | "logic_add" LEFTA [ (* nothing here by default *) ]
257 | "logic_mult" LEFTA [ (* nothing here by default *) ]
258 | "logic_inv" NONA [ (* nothing here by default *) ]
260 [ t1 = term; SYMBOL "="; t2 = term ->
261 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
263 | "add" LEFTA [ (* nothing here by default *) ]
264 | "mult" LEFTA [ (* nothing here by default *) ]
265 | "power" LEFTA [ (* nothing here by default *) ]
266 | "inv" NONA [ (* nothing here by default *) ]
268 [ t1 = term; t2 = term ->
269 let rec aux = function
270 | CicAst.Appl (hd :: tl) -> aux hd @ tl
273 CicAst.Appl (aux t1 @ [t2])
277 b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
278 let binder = mk_binder_ast b typ vars body in
279 return_term loc binder
282 [ sort = sort -> CicAst.Sort sort
283 | n = substituted_name -> return_term loc n
284 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
285 | IMPLICIT -> return_term loc CicAst.Implicit
288 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
293 int_of_string (String.sub m 1 (String.length m - 1))
294 with Failure "int_of_string" ->
295 fail loc ("Invalid meta variable number: " ^ m)
297 return_term loc (CicAst.Meta (index, substs))
298 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
300 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
304 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
306 ((lhs: CicAst.case_pattern), rhs)
310 (CicAst.Case (t, indty_ident, outtyp, patterns))
311 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
312 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
313 | PAREN "("; t = term; PAREN ")" -> return_term loc t
317 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
319 tactic_term: [ [ t = term -> t ] ];
321 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
324 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
327 [ [ IDENT "reduce" ] -> `Reduce
328 | [ IDENT "simplify" ] -> `Simpl
329 | [ IDENT "whd" ] -> `Whd ]
332 [ [ IDENT "absurd" ]; t = tactic_term ->
333 TacticAst.Absurd (loc, t)
334 | [ IDENT "apply" ]; t = tactic_term ->
335 TacticAst.Apply (loc, t)
336 | [ IDENT "assumption" ] ->
337 TacticAst.Assumption loc
338 | [ IDENT "auto" ] ; num = OPT [ i = NUM -> int_of_string i ] ->
339 TacticAst.Auto (loc,num)
340 | [ IDENT "change" ];
341 t1 = tactic_term; "with"; t2 = tactic_term;
342 where = tactic_where ->
343 TacticAst.Change (loc, t1, t2, where)
344 (* TODO Change_pattern *)
345 | [ IDENT "contradiction" ] ->
346 TacticAst.Contradiction loc
349 TacticAst.Cut (loc, t)
350 | [ IDENT "decompose" ];
351 principles = ident_list1; where = IDENT ->
352 TacticAst.Decompose (loc, where, principles)
353 | [ IDENT "discriminate" ];
355 TacticAst.Discriminate (loc, hyp)
356 | [ IDENT "elimType" ]; t = tactic_term ->
357 TacticAst.ElimType (loc, t)
360 using = OPT [ "using"; using = tactic_term -> using ] ->
361 TacticAst.Elim (loc, t1, using)
362 | [ IDENT "exact" ]; t = tactic_term ->
363 TacticAst.Exact (loc, t)
364 | [ IDENT "exists" ] ->
367 kind = reduction_kind; t = tactic_term ->
368 TacticAst.Fold (loc, kind, t)
369 | [ IDENT "fourier" ] ->
370 TacticAst.Fourier loc
371 | IDENT "goal"; n = NUM -> TacticAst.Goal (loc, int_of_string n)
372 | [ IDENT "injection" ]; ident = IDENT ->
373 TacticAst.Injection (loc, ident)
374 | [ IDENT "intros" ];
375 num = OPT [ num = int -> num ];
376 idents = OPT ident_list0 ->
377 let idents = match idents with None -> [] | Some idents -> idents in
378 TacticAst.Intros (loc, num, idents)
379 | [ IDENT "intro" ] ->
380 TacticAst.Intros (loc, None, [])
381 | [ IDENT "left" ] -> TacticAst.Left loc
383 t = tactic_term; "in"; where = IDENT ->
384 TacticAst.LetIn (loc, t, where)
385 | kind = reduction_kind;
387 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
390 terms = LIST0 term SEP SYMBOL "," ->
391 (match (pat, terms) with
392 | None, [] -> TacticAst.Reduce (loc, kind, None)
393 | None, terms -> TacticAst.Reduce (loc, kind, Some (terms, `Goal))
394 | Some pat, [] -> TacticAst.Reduce (loc, kind, Some ([], pat))
395 | Some pat, terms -> TacticAst.Reduce (loc, kind, Some (terms, pat)))
396 | [ IDENT "reflexivity" ] ->
397 TacticAst.Reflexivity loc
398 | [ IDENT "replace" ];
399 t1 = tactic_term; "with"; t2 = tactic_term ->
400 TacticAst.Replace (loc, t1, t2)
402 (* TODO Replace_pattern *)
403 | [ IDENT "right" ] -> TacticAst.Right loc
404 | [ IDENT "ring" ] -> TacticAst.Ring loc
405 | [ IDENT "split" ] -> TacticAst.Split loc
406 | [ IDENT "symmetry" ] ->
407 TacticAst.Symmetry loc
408 | [ IDENT "transitivity" ];
410 TacticAst.Transitivity (loc, t)
415 [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
416 TacticAst.Seq (loc, tacticals)
420 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
421 (TacticAst.Then (loc, tac, tacs))
424 [ [ IDENT "do" ]; count = int; tac = tactical ->
425 TacticAst.Do (loc, count, tac)
426 | [ IDENT "repeat" ]; tac = tactical ->
427 TacticAst.Repeat (loc, tac)
431 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
432 TacticAst.Tries (loc, tacs)
433 | IDENT "try"; tac = NEXT ->
434 TacticAst.Try (loc, tac)
435 | IDENT "fail" -> TacticAst.Fail loc
436 | IDENT "id" -> TacticAst.IdTac loc
437 | PAREN "("; tac = tactical; PAREN ")" -> tac
438 | tac = tactic -> TacticAst.Tactic (loc, tac)
442 [ [ IDENT "definition" ] -> `Definition
443 | [ IDENT "fact" ] -> `Fact
444 | [ IDENT "lemma" ] -> `Lemma
445 | [ IDENT "remark" ] -> `Remark
446 | [ 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
474 [ IDENT "abort" ] -> TacticAst.Abort loc
475 | [ IDENT "quit" ] -> TacticAst.Quit loc
476 | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
477 | [ IDENT "undo" ]; steps = OPT NUM ->
478 TacticAst.Undo (loc, int_opt steps)
479 | [ IDENT "redo" ]; steps = OPT NUM ->
480 TacticAst.Redo (loc, int_opt steps)
481 | [ IDENT "check" ]; t = term ->
482 TacticAst.Check (loc, t)
483 | [ IDENT "hint" ] -> TacticAst.Hint loc
484 | [ IDENT "pmatch" ] ; t = term -> TacticAst.Match (loc,t)
485 | [ IDENT "instance" ] ; t = term -> TacticAst.Instance (loc,t)
486 | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
490 [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
491 let alpha = "[a-zA-Z]" in
492 let num = "[0-9]+" in
493 let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
494 let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
495 let rex = Str.regexp ("^"^ident^"$") in
496 if Str.string_match rex id 0 then
498 ("^\\(cic:/\\|theory:/\\)"^ident^
499 "\\(/"^ident^"+\\)*\\(\\."^ident^"\\)+"^
500 "\\(#xpointer("^ num^"\\(/"^num^"\\)+)\\)?$")
502 if Str.string_match rex uri 0 then
503 TacticAst.Ident_alias (id, uri)
505 raise (Parse_error (loc,sprintf "Not a valid uri: %s" uri))
507 raise (Parse_error (loc,sprintf "Not a valid identifier: %s" id))
508 | IDENT "symbol"; symbol = QSTRING;
509 instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
510 SYMBOL "="; dsc = QSTRING ->
512 match instance with Some i -> int_of_string i | None -> 0
514 TacticAst.Symbol_alias (symbol, instance, dsc)
516 instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
517 SYMBOL "="; dsc = QSTRING ->
519 match instance with Some i -> int_of_string i | None -> 0
521 TacticAst.Number_alias (instance, dsc)
526 [ IDENT "set" ]; n = QSTRING; v = QSTRING ->
527 TacticAst.Set (loc, n, v)
528 | [ IDENT "qed" ] -> TacticAst.Qed loc
529 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
530 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
531 TacticAst.Theorem (loc, flavour, name, typ, body)
532 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
536 | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
537 | ((Cic.Name name,None),_,_) :: _ ->
538 fail loc ("No type given for " ^ name)
541 let body = CicAst.Ident (name,None) in
542 TacticAst.Theorem(loc, `Definition, Some name, ty,
543 Some (CicAst.LetRec (ind_kind, defs, body)))
545 | [ IDENT "inductive" ]; spec = inductive_spec ->
546 let (params, ind_types) = spec in
547 TacticAst.Inductive (loc, params, ind_types)
548 | [ IDENT "coinductive" ]; spec = inductive_spec ->
549 let (params, ind_types) = spec in
550 let ind_types = (* set inductive flags to false (coinductive) *)
551 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
554 TacticAst.Inductive (loc, params, ind_types)
555 | [ IDENT "coercion" ] ; name = IDENT ->
556 TacticAst.Coercion (loc, CicAst.Ident (name,Some []))
557 | [ IDENT "coercion" ] ; name = URI ->
558 TacticAst.Coercion (loc, CicAst.Uri (name,Some []))
559 | [ IDENT "alias" ]; spec = alias_spec ->
560 TacticAst.Alias (loc, spec)
564 [ cmd = command; SYMBOL "." -> TacticAst.Command (loc, cmd)
565 | tac = tactical; SYMBOL "." -> TacticAst.Tactical (loc, tac)
566 | mac = macro; SYMBOL "." -> TacticAst.Macro (loc, mac)
571 let exc_located_wrapper f =
575 | Stdpp.Exc_located (floc, Stream.Error msg) ->
576 raise (Parse_error (floc, msg))
577 | Stdpp.Exc_located (floc, exn) ->
578 raise (Parse_error (floc, (Printexc.to_string exn)))
580 let parse_term stream =
581 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
582 let parse_statement stream =
583 exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
587 (** {2 Interface for gTopLevel} *)
589 module EnvironmentP3 =
595 let aliases_grammar = Grammar.gcreate cic_lexer
596 let aliases = Grammar.Entry.create aliases_grammar "aliases"
601 (fun domain_item (dsc, _) acc ->
603 match domain_item with
605 TacticAstPp.pp_alias (TacticAst.Ident_alias (id, dsc)) ^ "."
606 | Symbol (symb, i) ->
607 TacticAstPp.pp_alias (TacticAst.Symbol_alias (symb, i, dsc))
610 TacticAstPp.pp_alias (TacticAst.Number_alias (i, dsc)) ^ "."
615 String.concat "\n" (List.sort compare aliases)
619 aliases: [ (* build an environment from an aliases list *)
620 [ aliases = LIST0 alias; EOI ->
622 (fun env (domain_item, codomain_item) ->
623 Environment.add domain_item codomain_item env)
624 Environment.empty aliases
627 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
630 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
631 (Id id, choice_of_uri uri)
632 | IDENT "symbol"; symbol = QSTRING;
633 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
634 SYMBOL "="; dsc = QSTRING ->
635 (Symbol (symbol, int_of_string instance),
636 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
638 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
639 SYMBOL "="; dsc = QSTRING ->
640 (Num (int_of_string instance),
641 DisambiguateChoices.lookup_num_by_dsc dsc)
651 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
654 (* vim:set encoding=utf8: *)