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: string) =
51 let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
52 (uri, (fun _ _ _ -> cic))
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 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
71 let name_of_string = function
72 | "_" -> Cic.Anonymous
76 GLOBAL: term term0 tactic tactical tactical0 command;
83 raise (Parse_error (sprintf
84 "integer literal expected at characters %d-%d" x y))
88 [ s = SYMBOL "_" -> None
89 | t = term -> Some t ]
92 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
93 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
94 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
95 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
101 | "CProp" -> `CProp ]
104 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
105 (name_of_string i, Some typ)
106 | i = IDENT -> (name_of_string i, None)
109 substituted_name: [ (* a subs.name is an explicit substitution subject *)
110 [ s = [ IDENT | SYMBOL ];
112 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
115 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
120 CicAst.Ident (s, subst)
123 name: [ (* as substituted_name with no explicit substitution *)
124 [ s = [ IDENT | SYMBOL ] -> s ]
127 [ n = name -> (n, [])
128 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
132 term0: [ [ t = term; EOI -> return_term loc t ] ];
135 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
136 * since applications are required to be bound by parens *)
137 [ "let"; var = typed_name;
138 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
140 IDENT "in"; t2 = term ->
141 return_term loc (CicAst.LetIn (var, t1, t2))
142 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
145 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
148 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
150 (var, t1, (match index with None -> 0 | Some i -> i))
152 IDENT "in"; body = term ->
153 return_term loc (CicAst.LetRec (ind_kind, defs, body))
159 [ vars = LIST1 IDENT SEP SYMBOL ",";
160 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
161 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
162 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
164 SYMBOL "."; body = term ->
168 let name = name_of_string var in
169 CicAst.Binder (b, (name, typ), body))
172 return_term loc binder
173 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
175 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
177 | "logic_add" LEFTA [ (* nothing here by default *) ]
178 | "logic_mult" LEFTA [ (* nothing here by default *) ]
179 | "logic_inv" NONA [ (* nothing here by default *) ]
181 [ t1 = term; SYMBOL "="; t2 = term ->
182 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
184 | "add" LEFTA [ (* nothing here by default *) ]
185 | "mult" LEFTA [ (* nothing here by default *) ]
186 | "power" LEFTA [ (* nothing here by default *) ]
187 | "inv" NONA [ (* nothing here by default *) ]
189 [ sort = sort -> CicAst.Sort sort
190 | n = substituted_name -> return_term loc n
191 | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
192 return_term loc (CicAst.Appl (head :: args))
193 | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
194 | IMPLICIT -> return_term loc CicAst.Implicit
197 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
202 int_of_string (String.sub m 1 (String.length m - 1))
203 with Failure "int_of_string" ->
204 fail loc ("Invalid meta variable number: " ^ m)
206 return_term loc (CicAst.Meta (index, substs))
207 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
209 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
213 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
214 ((lhs: CicAst.case_pattern), rhs)
218 (CicAst.Case (t, indty_ident, outtyp, patterns))
219 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
220 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
221 | PAREN "("; t = term; PAREN ")" -> return_term loc t
225 [ where = OPT [ IDENT "in"; ident = IDENT -> ident ] -> where ]
227 tactic_term: [ [ t = term -> t ] ];
229 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
232 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
235 [ "reduce" -> `Reduce
240 [ [ IDENT "absurd" | IDENT "Absurd" ] -> return_tactic loc TacticAst.Absurd
241 | [ IDENT "apply" | IDENT "Apply" ];
242 t = tactic_term -> return_tactic loc (TacticAst.Apply t)
243 | [ IDENT "assumption" | IDENT "Assumption" ] ->
244 return_tactic loc TacticAst.Assumption
245 | [ IDENT "change" | IDENT "Change" ];
246 t1 = tactic_term; "with"; t2 = tactic_term;
247 where = tactic_where ->
248 return_tactic loc (TacticAst.Change (t1, t2, where))
249 (* TODO Change_pattern *)
250 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
251 return_tactic loc TacticAst.Contradiction
252 | [ IDENT "cut" | IDENT "Cut" ];
253 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
254 | [ IDENT "decompose" | IDENT "Decompose" ];
255 principles = ident_list1; where = IDENT ->
256 return_tactic loc (TacticAst.Decompose (where, principles))
257 | [ IDENT "discriminate" | IDENT "Discriminate" ];
259 return_tactic loc (TacticAst.Discriminate hyp)
260 | [ IDENT "elim" | IDENT "Elim" ]; IDENT "type";
262 return_tactic loc (TacticAst.ElimType t)
263 | [ IDENT "elim" | IDENT "Elim" ];
265 using = OPT [ "using"; using = tactic_term -> using ] ->
266 return_tactic loc (TacticAst.Elim (t1, using))
267 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
268 return_tactic loc (TacticAst.Exact t)
269 | [ IDENT "exists" | IDENT "Exists" ] ->
270 return_tactic loc TacticAst.Exists
271 | [ IDENT "fold" | IDENT "Fold" ];
272 kind = reduction_kind; t = tactic_term ->
273 return_tactic loc (TacticAst.Fold (kind, t))
274 | [ IDENT "fourier" | IDENT "Fourier" ] ->
275 return_tactic loc TacticAst.Fourier
276 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
277 return_tactic loc (TacticAst.Injection ident)
278 | [ IDENT "intros" | IDENT "Intros" ];
279 num = OPT [ num = int -> num ];
280 idents = OPT ident_list0 ->
281 let idents = match idents with None -> [] | Some idents -> idents in
282 return_tactic loc (TacticAst.Intros (num, idents))
283 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
285 t = tactic_term; IDENT "in"; where = IDENT ->
286 return_tactic loc (TacticAst.LetIn (t, where))
288 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
289 return_tactic loc TacticAst.Reflexivity
290 | [ IDENT "replace" | IDENT "Replace" ];
291 t1 = tactic_term; "with"; t2 = tactic_term ->
292 return_tactic loc (TacticAst.Replace (t1, t2))
294 (* TODO Replace_pattern *)
295 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
296 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
297 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
298 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
299 return_tactic loc TacticAst.Symmetry
300 | [ IDENT "transitivity" | IDENT "Transitivity" ];
302 return_tactic loc (TacticAst.Transitivity t)
305 tactical0: [ [ t = tactical; SYMBOL "." -> t ] ];
308 [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
310 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
311 return_tactical loc (TacticAst.Seq tactics)
315 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
316 return_tactical loc (TacticAst.Then (tac, tacs))
319 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
320 return_tactical loc (TacticAst.Do (count, tac))
321 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
322 return_tactical loc (TacticAst.Repeat tac)
325 [ [ IDENT "tries" | IDENT "Tries" ];
326 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
327 return_tactical loc (TacticAst.Tries tacs)
328 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
329 return_tactical loc (TacticAst.Try tac)
330 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
331 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
332 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
333 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
336 theorem_flavour: [ (* all flavours but Goal *)
337 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
338 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
339 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
340 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
341 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
345 [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
346 | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
347 | [ IDENT "quit" | IDENT "Quit" ] -> return_command loc TacticAst.Quit
348 | [ IDENT "qed" | IDENT "Qed" ] ->
349 return_command loc (TacticAst.Qed None)
350 | [ IDENT "save" | IDENT "Save" ]; name = IDENT ->
351 return_command loc (TacticAst.Qed (Some name))
352 | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
353 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
354 return_command loc (TacticAst.Theorem (flavour, name, typ, body))
355 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
356 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
357 return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
362 let exc_located_wrapper f =
365 with Stdpp.Exc_located ((x, y), exn) ->
366 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
367 (Printexc.to_string exn)))
369 let parse_term stream =
370 exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
371 let parse_tactic stream =
372 exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
373 let parse_tactical stream =
374 exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
378 (** {2 Interface for gTopLevel} *)
380 module EnvironmentP3 =
386 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
387 let aliases = Grammar.Entry.create aliases_grammar "aliases"
392 (fun domain_item (dsc, _) acc ->
394 match domain_item with
395 | Id id -> sprintf "alias id %s = %s" id dsc
396 | Symbol (symb, instance) ->
397 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
400 sprintf "alias num (instance %d) = \"%s\"" instance dsc
405 String.concat "\n" (List.sort compare aliases)
409 aliases: [ (* build an environment from an aliases list *)
410 [ aliases = LIST0 alias; EOI ->
412 (fun env (domain_item, codomain_item) ->
413 Environment.add domain_item codomain_item env)
414 Environment.empty aliases
417 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
420 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
421 (Id id, choice_of_uri uri)
422 | IDENT "symbol"; symbol = QSTRING;
423 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
424 SYMBOL "="; dsc = QSTRING ->
425 (Symbol (symbol, int_of_string instance),
426 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
428 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
429 SYMBOL "="; dsc = QSTRING ->
430 (Num (int_of_string instance),
431 DisambiguateChoices.lookup_num_by_dsc dsc)
441 Grammar.Entry.parse aliases (Stream.of_string s)
442 with Stdpp.Exc_located ((x, y), exn) ->
443 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
444 (Printexc.to_string exn)))
447 (* vim:set encoding=utf8: *)