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 int_opt = function
80 | Some lexeme -> Some (int_of_string lexeme)
82 (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
83 * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
84 * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
85 * type in a block of mutual inductive types.
87 * This function performs the expansion foo.ind -> foo#xpointer..., if needed
89 let ind_expansion uri =
90 let len = String.length uri in
91 if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
92 uri ^ "#xpointer(1/1)"
97 GLOBAL: term term0 tactic tactical tactical0 command script;
102 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
106 [ s = SYMBOL "_" -> None
107 | t = term -> Some t ]
110 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
111 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
112 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
113 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
119 | "CProp" -> `CProp ]
122 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
123 (name_of_string i, Some typ)
124 | i = IDENT -> (name_of_string i, None)
129 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
132 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
139 substituted_name: [ (* a subs.name is an explicit substitution subject *)
140 [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
141 | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
144 name: [ (* as substituted_name with no explicit substitution *)
145 [ s = [ IDENT | SYMBOL ] -> s ]
148 [ n = name -> (n, [])
149 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
153 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
154 term0: [ [ t = term; EOI -> return_term loc t ] ];
157 [ "let"; var = typed_name;
158 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
159 t1 = term; "in"; t2 = term ->
160 return_term loc (CicAst.LetIn (var, t1, t2))
161 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
164 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
167 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
169 (var, t1, (match index with None -> 0 | Some i -> i))
172 return_term loc (CicAst.LetRec (ind_kind, defs, body))
178 [ vars = LIST1 IDENT SEP SYMBOL ",";
179 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
180 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
181 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
183 SYMBOL "."; body = term ->
187 let name = name_of_string var in
188 CicAst.Binder (b, (name, typ), body))
191 return_term loc binder
192 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
194 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
196 | "logic_add" LEFTA [ (* nothing here by default *) ]
197 | "logic_mult" LEFTA [ (* nothing here by default *) ]
198 | "logic_inv" NONA [ (* nothing here by default *) ]
200 [ t1 = term; SYMBOL "="; t2 = term ->
201 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
203 | "add" LEFTA [ (* nothing here by default *) ]
204 | "mult" LEFTA [ (* nothing here by default *) ]
205 | "power" LEFTA [ (* nothing here by default *) ]
206 | "inv" NONA [ (* nothing here by default *) ]
208 [ t1 = term; t2 = term ->
209 let rec aux = function
210 | CicAst.Appl (hd :: tl) -> aux hd @ tl
213 CicAst.Appl (aux t1 @ [t2])
216 [ sort = sort -> CicAst.Sort sort
217 | n = substituted_name -> return_term loc n
218 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
219 | IMPLICIT -> return_term loc CicAst.Implicit
222 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
227 int_of_string (String.sub m 1 (String.length m - 1))
228 with Failure "int_of_string" ->
229 fail loc ("Invalid meta variable number: " ^ m)
231 return_term loc (CicAst.Meta (index, substs))
232 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
234 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
238 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
239 ((lhs: CicAst.case_pattern), rhs)
243 (CicAst.Case (t, indty_ident, outtyp, patterns))
244 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
245 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
246 | PAREN "("; t = term; PAREN ")" -> return_term loc t
250 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
252 tactic_term: [ [ t = term -> t ] ];
254 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
257 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
260 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
261 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
262 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
265 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
266 return_tactic loc (TacticAst.Absurd t)
267 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
268 return_tactic loc (TacticAst.Apply t)
269 | [ IDENT "assumption" | IDENT "Assumption" ] ->
270 return_tactic loc TacticAst.Assumption
271 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
272 | [ IDENT "change" | IDENT "Change" ];
273 t1 = tactic_term; "with"; t2 = tactic_term;
274 where = tactic_where ->
275 return_tactic loc (TacticAst.Change (t1, t2, where))
276 (* TODO Change_pattern *)
277 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
278 return_tactic loc TacticAst.Contradiction
279 | [ IDENT "cut" | IDENT "Cut" ];
280 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
281 | [ IDENT "decompose" | IDENT "Decompose" ];
282 principles = ident_list1; where = IDENT ->
283 return_tactic loc (TacticAst.Decompose (where, principles))
284 | [ IDENT "discriminate" | IDENT "Discriminate" ];
286 return_tactic loc (TacticAst.Discriminate hyp)
287 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
288 return_tactic loc (TacticAst.ElimType t)
289 | [ IDENT "elim" | IDENT "Elim" ];
291 using = OPT [ "using"; using = tactic_term -> using ] ->
292 return_tactic loc (TacticAst.Elim (t1, using))
293 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
294 return_tactic loc (TacticAst.Exact t)
295 | [ IDENT "exists" | IDENT "Exists" ] ->
296 return_tactic loc TacticAst.Exists
297 | [ IDENT "fold" | IDENT "Fold" ];
298 kind = reduction_kind; t = tactic_term ->
299 return_tactic loc (TacticAst.Fold (kind, t))
300 | [ IDENT "fourier" | IDENT "Fourier" ] ->
301 return_tactic loc TacticAst.Fourier
302 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
303 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
304 return_tactic loc (TacticAst.Injection ident)
305 | [ IDENT "intros" | IDENT "Intros" ];
306 num = OPT [ num = int -> num ];
307 idents = OPT ident_list0 ->
308 let idents = match idents with None -> [] | Some idents -> idents in
309 return_tactic loc (TacticAst.Intros (num, idents))
310 | [ IDENT "intro" | IDENT "Intro" ] ->
311 return_tactic loc (TacticAst.Intros (Some 1, []))
312 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
314 t = tactic_term; "in"; where = IDENT ->
315 return_tactic loc (TacticAst.LetIn (t, where))
316 | kind = reduction_kind;
318 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
321 terms = LIST0 term SEP SYMBOL "," ->
323 (match (pat, terms) with
324 | None, [] -> TacticAst.Reduce (kind, None)
325 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
326 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
327 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
329 return_tactic loc tac
330 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
331 return_tactic loc TacticAst.Reflexivity
332 | [ IDENT "replace" | IDENT "Replace" ];
333 t1 = tactic_term; "with"; t2 = tactic_term ->
334 return_tactic loc (TacticAst.Replace (t1, t2))
336 (* TODO Replace_pattern *)
337 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
338 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
339 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
340 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
341 return_tactic loc TacticAst.Symmetry
342 | [ IDENT "transitivity" | IDENT "Transitivity" ];
344 return_tactic loc (TacticAst.Transitivity t)
347 tactical0: [ [ t = tactical; SYMBOL ";;" -> return_tactical loc t ] ];
350 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
352 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
353 return_tactical loc (TacticAst.Seq tactics)
357 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
358 return_tactical loc (TacticAst.Then (tac, tacs))
361 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
362 return_tactical loc (TacticAst.Do (count, tac))
363 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
364 return_tactical loc (TacticAst.Repeat tac)
367 [ [ IDENT "tries" | IDENT "Tries" ];
368 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
369 return_tactical loc (TacticAst.Tries tacs)
370 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
371 return_tactical loc (TacticAst.Try tac)
372 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
373 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
374 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
375 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
378 theorem_flavour: [ (* all flavours but Goal *)
379 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
380 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
381 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
382 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
383 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
387 fst_name = IDENT; params = LIST0 [
388 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
389 typ = term; PAREN ")" -> (names, typ) ];
390 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
391 fst_constructors = LIST0 constructor SEP SYMBOL "|";
394 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
395 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
396 (name, true, typ, constructors) ] SEP "with" -> types
400 (fun (names, typ) acc ->
401 (List.map (fun name -> (name, typ)) names) @ acc)
404 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
405 let tl_ind_types = match tl with None -> [] | Some types -> types in
406 let ind_types = fst_ind_type :: tl_ind_types in
410 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
411 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
412 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
413 | [ IDENT "qed" | IDENT "Qed" ] ->
414 return_command loc (TacticAst.Qed None)
415 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
416 return_command loc (TacticAst.Qed (Some name))
417 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
418 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
419 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
420 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
421 let (params, ind_types) = spec in
422 return_command loc (TacticAst.Inductive (params, ind_types))
423 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
424 let (params, ind_types) = spec in
425 let ind_types = (* set inductive flags to false (coinductive) *)
426 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
429 return_command loc (TacticAst.Inductive (params, ind_types))
430 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
431 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
432 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
433 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
434 return_command loc (TacticAst.Undo (int_opt steps))
435 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
436 return_command loc (TacticAst.Redo (int_opt steps))
437 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
438 return_command loc (TacticAst.Baseuri uri)
439 | [ IDENT "check" | IDENT "Check" ]; t = term ->
440 return_command loc (TacticAst.Check t)
444 [ cmd = tactical0 -> Command cmd
445 | s = COMMENT -> Comment (loc, s)
448 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
451 let exc_located_wrapper f =
455 | Stdpp.Exc_located (floc, Stream.Error msg) ->
456 raise (Parse_error (floc, msg))
457 | Stdpp.Exc_located (floc, exn) ->
458 raise (Parse_error (floc, (Printexc.to_string exn)))
460 let parse_term stream =
461 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
462 let parse_tactic stream =
463 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
464 let parse_tactical stream =
465 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
466 let parse_script stream =
467 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
471 (** {2 Interface for gTopLevel} *)
473 module EnvironmentP3 =
479 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
480 let aliases = Grammar.Entry.create aliases_grammar "aliases"
485 (fun domain_item (dsc, _) acc ->
487 match domain_item with
488 | Id id -> sprintf "alias id %s = %s" id dsc
489 | Symbol (symb, instance) ->
490 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
493 sprintf "alias num (instance %d) = \"%s\"" instance dsc
498 String.concat "\n" (List.sort compare aliases)
502 aliases: [ (* build an environment from an aliases list *)
503 [ aliases = LIST0 alias; EOI ->
505 (fun env (domain_item, codomain_item) ->
506 Environment.add domain_item codomain_item env)
507 Environment.empty aliases
510 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
513 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
514 (Id id, choice_of_uri uri)
515 | IDENT "symbol"; symbol = QSTRING;
516 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
517 SYMBOL "="; dsc = QSTRING ->
518 (Symbol (symbol, int_of_string instance),
519 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
521 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
522 SYMBOL "="; dsc = QSTRING ->
523 (Num (int_of_string instance),
524 DisambiguateChoices.lookup_num_by_dsc dsc)
534 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
537 (* vim:set encoding=utf8: *)