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
39 open DisambiguateTypes
41 exception Parse_error of string
43 let fresh_num_instance =
45 if use_fresh_num_instances then
46 (fun () -> incr n; !n)
50 let choice_of_uri uri =
51 let term = CicUtil.term_of_uri uri in
52 (uri, (fun _ _ _ -> term))
54 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
56 let term = Grammar.Entry.create grammar "term"
57 let term0 = Grammar.Entry.create grammar "term0"
58 let tactic = Grammar.Entry.create grammar "tactic"
59 let tactical = Grammar.Entry.create grammar "tactical"
60 let tactical0 = Grammar.Entry.create grammar "tactical0"
61 let command = Grammar.Entry.create grammar "command"
63 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
64 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
65 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
66 let return_command loc cmd = cmd
69 let (x, y) = CicAst.loc_of_floc floc in
70 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
72 let name_of_string = function
73 | "_" -> Cic.Anonymous
76 let int_opt = function
78 | Some lexeme -> Some (int_of_string lexeme)
81 GLOBAL: term term0 tactic tactical tactical0 command;
87 let (x, y) = CicAst.loc_of_floc loc in
88 raise (Parse_error (sprintf
89 "integer literal expected at characters %d-%d" x y))
93 [ s = SYMBOL "_" -> None
94 | t = term -> Some t ]
97 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
98 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
99 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
100 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
106 | "CProp" -> `CProp ]
109 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
110 (name_of_string i, Some typ)
111 | i = IDENT -> (name_of_string i, None)
114 substituted_name: [ (* a subs.name is an explicit substitution subject *)
117 SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *)
120 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
125 CicAst.Ident (s, subst)
128 name: [ (* as substituted_name with no explicit substitution *)
129 [ s = [ IDENT | SYMBOL ] -> s ]
132 [ n = name -> (n, [])
133 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
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 [ "reduce" -> `Reduce
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 "change" | IDENT "Change" ];
255 t1 = tactic_term; "with"; t2 = tactic_term;
256 where = tactic_where ->
257 return_tactic loc (TacticAst.Change (t1, t2, where))
258 (* TODO Change_pattern *)
259 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
260 return_tactic loc TacticAst.Contradiction
261 | [ IDENT "cut" | IDENT "Cut" ];
262 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
263 | [ IDENT "decompose" | IDENT "Decompose" ];
264 principles = ident_list1; where = IDENT ->
265 return_tactic loc (TacticAst.Decompose (where, principles))
266 | [ IDENT "discriminate" | IDENT "Discriminate" ];
268 return_tactic loc (TacticAst.Discriminate hyp)
269 | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
270 return_tactic loc (TacticAst.ElimType t)
271 | [ IDENT "elim" | IDENT "Elim" ];
273 using = OPT [ "using"; using = tactic_term -> using ] ->
274 return_tactic loc (TacticAst.Elim (t1, using))
275 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
276 return_tactic loc (TacticAst.Exact t)
277 | [ IDENT "exists" | IDENT "Exists" ] ->
278 return_tactic loc TacticAst.Exists
279 | [ IDENT "fold" | IDENT "Fold" ];
280 kind = reduction_kind; t = tactic_term ->
281 return_tactic loc (TacticAst.Fold (kind, t))
282 | [ IDENT "fourier" | IDENT "Fourier" ] ->
283 return_tactic loc TacticAst.Fourier
284 | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
285 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
286 return_tactic loc (TacticAst.Injection ident)
287 | [ IDENT "intros" | IDENT "Intros" ];
288 num = OPT [ num = int -> num ];
289 idents = OPT ident_list0 ->
290 let idents = match idents with None -> [] | Some idents -> idents in
291 return_tactic loc (TacticAst.Intros (num, idents))
292 | [ IDENT "intro" | IDENT "Intro" ] ->
293 return_tactic loc (TacticAst.Intros (Some 1, []))
294 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
296 t = tactic_term; "in"; where = IDENT ->
297 return_tactic loc (TacticAst.LetIn (t, where))
299 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
300 return_tactic loc TacticAst.Reflexivity
301 | [ IDENT "replace" | IDENT "Replace" ];
302 t1 = tactic_term; "with"; t2 = tactic_term ->
303 return_tactic loc (TacticAst.Replace (t1, t2))
305 (* TODO Replace_pattern *)
306 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
307 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
308 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
309 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
310 return_tactic loc TacticAst.Symmetry
311 | [ IDENT "transitivity" | IDENT "Transitivity" ];
313 return_tactic loc (TacticAst.Transitivity t)
316 tactical0: [ [ t = tactical; SYMBOL ";;" -> t ] ];
319 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
321 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
322 return_tactical loc (TacticAst.Seq tactics)
326 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
327 return_tactical loc (TacticAst.Then (tac, tacs))
330 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
331 return_tactical loc (TacticAst.Do (count, tac))
332 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
333 return_tactical loc (TacticAst.Repeat tac)
336 [ [ IDENT "tries" | IDENT "Tries" ];
337 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
338 return_tactical loc (TacticAst.Tries tacs)
339 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
340 return_tactical loc (TacticAst.Try tac)
341 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
342 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
343 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
344 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
347 theorem_flavour: [ (* all flavours but Goal *)
348 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
349 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
350 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
351 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
352 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
356 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
357 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
358 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
359 | [ IDENT "qed" | IDENT "Qed" ] ->
360 return_command loc (TacticAst.Qed None)
361 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
362 return_command loc (TacticAst.Qed (Some name))
363 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
364 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
365 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
366 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
367 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
368 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
369 | [ IDENT "undo" | IDENT "Undo" ]; steps = OPT NUM ->
370 return_command loc (TacticAst.Undo (int_opt steps))
371 | [ IDENT "redo" | IDENT "Redo" ]; steps = OPT NUM ->
372 return_command loc (TacticAst.Redo (int_opt steps))
373 | [ IDENT "check" | IDENT "Check" ]; t = term ->
374 return_command loc (TacticAst.Check t)
379 let exc_located_wrapper f =
382 with Stdpp.Exc_located (floc, exn) ->
383 let (x, y) = CicAst.loc_of_floc floc in
384 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
385 (Printexc.to_string exn)))
387 let parse_term stream =
388 exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
389 let parse_tactic stream =
390 exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
391 let parse_tactical stream =
392 exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
396 (** {2 Interface for gTopLevel} *)
398 module EnvironmentP3 =
404 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
405 let aliases = Grammar.Entry.create aliases_grammar "aliases"
410 (fun domain_item (dsc, _) acc ->
412 match domain_item with
413 | Id id -> sprintf "alias id %s = %s" id dsc
414 | Symbol (symb, instance) ->
415 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
418 sprintf "alias num (instance %d) = \"%s\"" instance dsc
423 String.concat "\n" (List.sort compare aliases)
427 aliases: [ (* build an environment from an aliases list *)
428 [ aliases = LIST0 alias; EOI ->
430 (fun env (domain_item, codomain_item) ->
431 Environment.add domain_item codomain_item env)
432 Environment.empty aliases
435 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
438 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
439 (Id id, choice_of_uri uri)
440 | IDENT "symbol"; symbol = QSTRING;
441 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
442 SYMBOL "="; dsc = QSTRING ->
443 (Symbol (symbol, int_of_string instance),
444 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
446 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
447 SYMBOL "="; dsc = QSTRING ->
448 (Num (int_of_string instance),
449 DisambiguateChoices.lookup_num_by_dsc dsc)
459 Grammar.Entry.parse aliases (Stream.of_string s)
460 with Stdpp.Exc_located (floc, exn) ->
461 let (x, y) = CicAst.loc_of_floc floc in
462 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
463 (Printexc.to_string exn)))
466 (* vim:set encoding=utf8: *)