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)
83 GLOBAL: term term0 tactic tactical tactical0 command script;
88 with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
92 [ s = SYMBOL "_" -> None
93 | t = term -> Some t ]
96 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
97 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
98 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
99 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
105 | "CProp" -> `CProp ]
108 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
109 (name_of_string i, Some typ)
110 | i = IDENT -> (name_of_string i, None)
113 substituted_name: [ (* a subs.name is an explicit substitution subject *)
116 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
119 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
124 CicAst.Ident (s, subst)
127 name: [ (* as substituted_name with no explicit substitution *)
128 [ s = [ IDENT | SYMBOL ] -> s ]
131 [ n = name -> (n, [])
132 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
136 constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
137 term0: [ [ t = term; EOI -> return_term loc t ] ];
140 [ "let"; var = typed_name;
141 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
142 t1 = term; "in"; t2 = term ->
143 return_term loc (CicAst.LetIn (var, t1, t2))
144 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
147 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
150 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
152 (var, t1, (match index with None -> 0 | Some i -> i))
155 return_term loc (CicAst.LetRec (ind_kind, defs, body))
161 [ vars = LIST1 IDENT SEP SYMBOL ",";
162 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
163 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
164 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
166 SYMBOL "."; body = term ->
170 let name = name_of_string var in
171 CicAst.Binder (b, (name, typ), body))
174 return_term loc binder
175 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
177 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
179 | "logic_add" LEFTA [ (* nothing here by default *) ]
180 | "logic_mult" LEFTA [ (* nothing here by default *) ]
181 | "logic_inv" NONA [ (* nothing here by default *) ]
183 [ t1 = term; SYMBOL "="; t2 = term ->
184 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
186 | "add" LEFTA [ (* nothing here by default *) ]
187 | "mult" LEFTA [ (* nothing here by default *) ]
188 | "power" LEFTA [ (* nothing here by default *) ]
189 | "inv" NONA [ (* nothing here by default *) ]
191 [ t1 = term; t2 = term ->
192 let rec aux = function
193 | CicAst.Appl (hd :: tl) -> aux hd @ tl
196 CicAst.Appl (aux t1 @ [t2])
199 [ sort = sort -> CicAst.Sort sort
200 | n = substituted_name -> return_term loc n
201 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
202 | IMPLICIT -> return_term loc CicAst.Implicit
205 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
210 int_of_string (String.sub m 1 (String.length m - 1))
211 with Failure "int_of_string" ->
212 fail loc ("Invalid meta variable number: " ^ m)
214 return_term loc (CicAst.Meta (index, substs))
215 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
217 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
221 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
222 ((lhs: CicAst.case_pattern), rhs)
226 (CicAst.Case (t, indty_ident, outtyp, patterns))
227 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
228 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
229 | PAREN "("; t = term; PAREN ")" -> return_term loc t
233 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
235 tactic_term: [ [ t = term -> t ] ];
237 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
240 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
243 [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
244 | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
245 | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
248 [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
249 return_tactic loc (TacticAst.Absurd t)
250 | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
251 return_tactic loc (TacticAst.Apply t)
252 | [ IDENT "assumption" | IDENT "Assumption" ] ->
253 return_tactic loc TacticAst.Assumption
254 | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
255 | [ IDENT "change" | IDENT "Change" ];
256 t1 = tactic_term; "with"; t2 = tactic_term;
257 where = tactic_where ->
258 return_tactic loc (TacticAst.Change (t1, t2, where))
259 (* TODO Change_pattern *)
260 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
261 return_tactic loc TacticAst.Contradiction
262 | [ IDENT "cut" | IDENT "Cut" ];
263 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
264 | [ IDENT "decompose" | IDENT "Decompose" ];
265 principles = ident_list1; where = IDENT ->
266 return_tactic loc (TacticAst.Decompose (where, principles))
267 | [ IDENT "discriminate" | IDENT "Discriminate" ];
269 return_tactic loc (TacticAst.Discriminate hyp)
270 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
271 return_tactic loc (TacticAst.ElimType t)
272 | [ IDENT "elim" | IDENT "Elim" ];
274 using = OPT [ "using"; using = tactic_term -> using ] ->
275 return_tactic loc (TacticAst.Elim (t1, using))
276 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
277 return_tactic loc (TacticAst.Exact t)
278 | [ IDENT "exists" | IDENT "Exists" ] ->
279 return_tactic loc TacticAst.Exists
280 | [ IDENT "fold" | IDENT "Fold" ];
281 kind = reduction_kind; t = tactic_term ->
282 return_tactic loc (TacticAst.Fold (kind, t))
283 | [ IDENT "fourier" | IDENT "Fourier" ] ->
284 return_tactic loc TacticAst.Fourier
285 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
286 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
287 return_tactic loc (TacticAst.Injection ident)
288 | [ IDENT "intros" | IDENT "Intros" ];
289 num = OPT [ num = int -> num ];
290 idents = OPT ident_list0 ->
291 let idents = match idents with None -> [] | Some idents -> idents in
292 return_tactic loc (TacticAst.Intros (num, idents))
293 | [ IDENT "intro" | IDENT "Intro" ] ->
294 return_tactic loc (TacticAst.Intros (Some 1, []))
295 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
297 t = tactic_term; "in"; where = IDENT ->
298 return_tactic loc (TacticAst.LetIn (t, where))
299 | kind = reduction_kind;
301 "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
304 terms = LIST0 term SEP SYMBOL "," ->
306 (match (pat, terms) with
307 | None, [] -> TacticAst.Reduce (kind, None)
308 | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
309 | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
310 | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
312 return_tactic loc tac
313 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
314 return_tactic loc TacticAst.Reflexivity
315 | [ IDENT "replace" | IDENT "Replace" ];
316 t1 = tactic_term; "with"; t2 = tactic_term ->
317 return_tactic loc (TacticAst.Replace (t1, t2))
319 (* TODO Replace_pattern *)
320 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
321 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
322 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
323 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
324 return_tactic loc TacticAst.Symmetry
325 | [ IDENT "transitivity" | IDENT "Transitivity" ];
327 return_tactic loc (TacticAst.Transitivity t)
330 tactical0: [ [ t = tactical; SYMBOL ";;" -> return_tactical loc t ] ];
333 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
335 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
336 return_tactical loc (TacticAst.Seq tactics)
340 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
341 return_tactical loc (TacticAst.Then (tac, tacs))
344 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
345 return_tactical loc (TacticAst.Do (count, tac))
346 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
347 return_tactical loc (TacticAst.Repeat tac)
350 [ [ IDENT "tries" | IDENT "Tries" ];
351 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
352 return_tactical loc (TacticAst.Tries tacs)
353 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
354 return_tactical loc (TacticAst.Try tac)
355 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
356 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
357 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
358 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
361 theorem_flavour: [ (* all flavours but Goal *)
362 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
363 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
364 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
365 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
366 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
370 fst_name = IDENT; params = LIST0 [
371 PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
372 typ = term; PAREN ")" -> (names, typ) ];
373 SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
374 fst_constructors = LIST0 constructor SEP SYMBOL "|";
377 name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
378 OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
379 (name, true, typ, constructors) ] SEP "with" -> types
383 (fun (names, typ) acc ->
384 (List.map (fun name -> (name, typ)) names) @ acc)
387 let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
388 let tl_ind_types = match tl with None -> [] | Some types -> types in
389 let ind_types = fst_ind_type :: tl_ind_types in
393 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
394 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
395 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
396 | [ IDENT "qed" | IDENT "Qed" ] ->
397 return_command loc (TacticAst.Qed None)
398 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
399 return_command loc (TacticAst.Qed (Some name))
400 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
401 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
402 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
403 | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
404 let (params, ind_types) = spec in
405 return_command loc (TacticAst.Inductive (params, ind_types))
406 | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
407 let (params, ind_types) = spec in
408 let ind_types = (* set inductive flags to false (coinductive) *)
409 List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
412 return_command loc (TacticAst.Inductive (params, ind_types))
413 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
414 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
415 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
416 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
417 return_command loc (TacticAst.Undo (int_opt steps))
418 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
419 return_command loc (TacticAst.Redo (int_opt steps))
420 | [ IDENT "baseuri" | IDENT "Baseuri" ]; uri = OPT QSTRING ->
421 return_command loc (TacticAst.Baseuri uri)
422 | [ IDENT "check" | IDENT "Check" ]; t = term ->
423 return_command loc (TacticAst.Check t)
427 [ cmd = tactical0 -> Command cmd
428 | s = COMMENT -> Comment (loc, s)
431 script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
434 let exc_located_wrapper f =
438 | Stdpp.Exc_located (floc, Stream.Error msg) ->
439 raise (Parse_error (floc, msg))
440 | Stdpp.Exc_located (floc, exn) ->
441 raise (Parse_error (floc, (Printexc.to_string exn)))
443 let parse_term stream =
444 exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
445 let parse_tactic stream =
446 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
447 let parse_tactical stream =
448 exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
449 let parse_script stream =
450 exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
454 (** {2 Interface for gTopLevel} *)
456 module EnvironmentP3 =
462 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
463 let aliases = Grammar.Entry.create aliases_grammar "aliases"
468 (fun domain_item (dsc, _) acc ->
470 match domain_item with
471 | Id id -> sprintf "alias id %s = %s" id dsc
472 | Symbol (symb, instance) ->
473 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
476 sprintf "alias num (instance %d) = \"%s\"" instance dsc
481 String.concat "\n" (List.sort compare aliases)
485 aliases: [ (* build an environment from an aliases list *)
486 [ aliases = LIST0 alias; EOI ->
488 (fun env (domain_item, codomain_item) ->
489 Environment.add domain_item codomain_item env)
490 Environment.empty aliases
493 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
496 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
497 (Id id, choice_of_uri uri)
498 | IDENT "symbol"; symbol = QSTRING;
499 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
500 SYMBOL "="; dsc = QSTRING ->
501 (Symbol (symbol, int_of_string instance),
502 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
504 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
505 SYMBOL "="; dsc = QSTRING ->
506 (Num (int_of_string instance),
507 DisambiguateChoices.lookup_num_by_dsc dsc)
517 (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
520 (* vim:set encoding=utf8: *)