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>"
36 exception Parse_error of string
38 type tactic = (CicAst.term, string) TacticAst.tactic
39 type tactical = (CicAst.term, string) TacticAst.tactic TacticAst.tactical
40 type command = CicAst.term CommandAst.command
41 type script = CicAst.term CommandAst.Script.script
43 let choice_of_uri (uri: string) =
44 let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
45 (uri, (fun _ _ _ -> cic))
47 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
49 let term = Grammar.Entry.create grammar "term"
50 let term0 = Grammar.Entry.create grammar "term0"
51 let tactic = Grammar.Entry.create grammar "tactic"
52 let tactical = Grammar.Entry.create grammar "tactical"
53 let tactical0 = Grammar.Entry.create grammar "tactical0"
54 let command = Grammar.Entry.create grammar "command"
55 let script = Grammar.Entry.create grammar "script"
57 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
58 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
59 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
60 let return_command loc cmd = cmd
61 let return_script loc script = script
64 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
66 let name_of_string = function
67 | "_" -> Cic.Anonymous
71 GLOBAL: term term0 tactic tactical tactical0 command script;
78 raise (Parse_error (sprintf
79 "integer literal expected at characters %d-%d" x y))
83 [ s = SYMBOL "_" -> None
84 | t = term -> Some t ]
87 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
88 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
89 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
90 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
99 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
100 (name_of_string i, Some typ)
101 | i = IDENT -> (name_of_string i, None)
104 substituted_name: [ (* a subs.name is an explicit substitution subject *)
105 [ s = [ IDENT | SYMBOL ];
107 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
110 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
116 | Some l -> CicAst.Ident (s, l)
117 | None -> CicAst.Ident (s, []))
120 name: [ (* as substituted_name with no explicit substitution *)
121 [ s = [ IDENT | SYMBOL ] -> s ]
124 [ n = name -> (n, [])
125 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
129 term0: [ [ t = term; EOI -> return_term loc t ] ];
132 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
133 * since applications are required to be bound by parens *)
134 [ "let"; var = typed_name;
135 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
137 IDENT "in"; t2 = term ->
138 return_term loc (CicAst.LetIn (var, t1, t2))
139 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
142 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
145 SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
147 (var, t1, (match index with None -> 0 | Some i -> i))
149 IDENT "in"; body = term ->
150 return_term loc (CicAst.LetRec (ind_kind, defs, body))
156 [ vars = LIST1 IDENT SEP SYMBOL ",";
157 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
158 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
159 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
161 SYMBOL "."; body = term ->
165 let name = name_of_string var in
166 CicAst.Binder (b, (name, typ), body))
169 return_term loc binder
170 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
172 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
175 [ t1 = term; SYMBOL "="; t2 = term ->
176 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
178 | "add" LEFTA [ (* nothing here by default *) ]
179 | "mult" LEFTA [ (* nothing here by default *) ]
180 | "inv" NONA [ (* nothing here by default *) ]
182 [ sort = sort -> CicAst.Sort sort
183 | n = substituted_name -> return_term loc n
184 | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
185 return_term loc (CicAst.Appl (head :: args))
186 | i = NUM -> return_term loc (CicAst.Num (i, 0))
187 | IMPLICIT -> return_term loc CicAst.Implicit
190 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
195 int_of_string (String.sub m 1 (String.length m - 1))
196 with Failure "int_of_string" ->
197 fail loc ("Invalid meta variable number: " ^ m)
199 return_term loc (CicAst.Meta (index, substs))
200 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
202 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
206 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
207 ((lhs: CicAst.case_pattern), rhs)
211 (CicAst.Case (t, indty_ident, outtyp, patterns))
212 | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
213 return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
214 | PAREN "("; t = term; PAREN ")" -> return_term loc t
218 [ where = OPT [ IDENT "in"; ident = IDENT -> ident ] -> where ]
220 tactic_term: [ [ t = term -> t ] ];
222 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
225 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
228 [ "reduce" -> `Reduce
233 [ [ IDENT "absurd" | IDENT "Absurd" ] -> return_tactic loc TacticAst.Absurd
234 | [ IDENT "apply" | IDENT "Apply" ];
235 t = tactic_term -> return_tactic loc (TacticAst.Apply t)
236 | [ IDENT "assumption" | IDENT "Assumption" ] ->
237 return_tactic loc TacticAst.Assumption
238 | [ IDENT "change" | IDENT "Change" ];
239 t1 = tactic_term; "with"; t2 = tactic_term;
240 where = tactic_where ->
241 return_tactic loc (TacticAst.Change (t1, t2, where))
242 (* TODO Change_pattern *)
243 | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
244 return_tactic loc TacticAst.Contradiction
245 | [ IDENT "cut" | IDENT "Cut" ];
246 t = tactic_term -> return_tactic loc (TacticAst.Cut t)
247 | [ IDENT "decompose" | IDENT "Decompose" ];
248 principles = ident_list1; where = IDENT ->
249 return_tactic loc (TacticAst.Decompose (where, principles))
250 | [ IDENT "discriminate" | IDENT "Discriminate" ];
252 return_tactic loc (TacticAst.Discriminate hyp)
253 | [ IDENT "elim" | IDENT "Elim" ]; IDENT "type";
255 return_tactic loc (TacticAst.ElimType t)
256 | [ IDENT "elim" | IDENT "Elim" ];
258 using = OPT [ "using"; using = tactic_term -> using ] ->
259 return_tactic loc (TacticAst.Elim (t1, using))
260 | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
261 return_tactic loc (TacticAst.Exact t)
262 | [ IDENT "exists" | IDENT "Exists" ] ->
263 return_tactic loc TacticAst.Exists
264 | [ IDENT "fold" | IDENT "Fold" ];
265 kind = reduction_kind; t = tactic_term ->
266 return_tactic loc (TacticAst.Fold (kind, t))
267 | [ IDENT "fourier" | IDENT "Fourier" ] ->
268 return_tactic loc TacticAst.Fourier
269 | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
270 return_tactic loc (TacticAst.Injection ident)
271 | [ IDENT "intros" | IDENT "Intros" ];
272 num = OPT [ num = int -> num ];
273 idents = OPT ident_list0 ->
274 let idents = match idents with None -> [] | Some idents -> idents in
275 return_tactic loc (TacticAst.Intros (num, idents))
276 | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
278 t = tactic_term; IDENT "in"; where = IDENT ->
279 return_tactic loc (TacticAst.LetIn (t, where))
281 | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
282 return_tactic loc TacticAst.Reflexivity
283 | [ IDENT "replace" | IDENT "Replace" ];
284 t1 = tactic_term; "with"; t2 = tactic_term ->
285 return_tactic loc (TacticAst.Replace (t1, t2))
287 (* TODO Replace_pattern *)
288 | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
289 | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
290 | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
291 | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
292 return_tactic loc TacticAst.Symmetry
293 | [ IDENT "transitivity" | IDENT "Transitivity" ];
295 return_tactic loc (TacticAst.Transitivity t)
298 tactical0: [ [ t = tactical; SYMBOL "." -> t ] ];
301 [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
302 return_tactical loc (TacticAst.Seq tactics)
306 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
307 return_tactical loc (TacticAst.Then (tac, tacs))
310 [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
311 return_tactical loc (TacticAst.Do (count, tac))
312 | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
313 return_tactical loc (TacticAst.Repeat tac)
316 [ [ IDENT "tries" | IDENT "Tries" ];
317 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
318 return_tactical loc (TacticAst.Tries tacs)
319 | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
320 return_tactical loc (TacticAst.Try tac)
321 | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
322 | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
323 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
324 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
327 theorem_flavour: [ (* all flavours but Goal *)
328 [ [ IDENT "definition" | IDENT "Definition" ] -> `Definition
329 | [ IDENT "fact" | IDENT "Fact" ] -> `Fact
330 | [ IDENT "lemma" | IDENT "Lemma" ] -> `Lemma
331 | [ IDENT "remark" | IDENT "Remark" ] -> `Remark
332 | [ IDENT "theorem" | IDENT "Theorem" ] -> `Theorem
333 (* | [ IDENT "goal" | IDENT "Goal" ] -> `Goal *)
337 [ flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
338 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ];
340 (loc, flavour, name, typ, body)
341 | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
342 body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ];
344 (loc, `Goal, None, typ, body)
347 proof_cmd: [ [ [ IDENT "proof" | IDENT "Proof" ]; SYMBOL "." -> loc ] ];
349 [ [ IDENT "qed" | IDENT "Qed" ]; SYMBOL "." -> (loc, None)
350 | [ IDENT "save" | IDENT "Save" ]; name = IDENT; SYMBOL "." ->
355 [ (loc', flavour, name, typ, body) = theorem_cmd ->
356 return_command loc (CommandAst.Theorem (loc', flavour, name, typ, body))
357 | (loc') = proof_cmd -> return_command loc (CommandAst.Proof loc')
358 | (loc, name) = qed_cmd -> return_command loc (CommandAst.Qed (loc, name))
362 [ theorem = theorem_cmd;
364 proof_cmd; tacticals = LIST1 tactical0; qed = qed_cmd ->
367 let (loc', flavour, name', typ, body_verbatim) = theorem in
370 | None | Some (_, (_, None)) -> None
371 | Some (_, (_, Some name)) -> Some name
374 match (name', name'') with
375 | Some name, None -> name
376 | None, Some name -> name
378 Stdpp.raise_with_loc loc (Failure "theorem's name is missing")
379 | Some name', Some name'' when name' <> name'' ->
380 Stdpp.raise_with_loc loc (Failure (sprintf
381 "theorem's name mismatch: %s <> %s" name' name''))
382 | Some name, _ -> name
385 match (body_verbatim, proof) with
386 | Some term, None -> CommandAst.Script.Verbatim (loc', term)
387 | None, Some (tacticals, (loc'', _)) ->
388 CommandAst.Script.Tactics (loc'', tacticals)
390 Stdpp.raise_with_loc loc (Failure (sprintf
391 "theorem %s has too many proofs" name))
393 Stdpp.raise_with_loc loc (Failure (sprintf
394 "theorem %s has no proof" name))
396 return_script loc (CommandAst.Script.Theorem (flavour, name, typ, body))
399 script: [ [ entries = LIST0 script_entry; EOI -> entries ] ];
402 let exc_located_wrapper f =
405 with Stdpp.Exc_located ((x, y), exn) ->
406 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
407 (Printexc.to_string exn)))
409 let parse_term stream =
410 exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
411 let parse_tactic stream =
412 exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
413 let parse_tactical stream =
414 exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
415 let parse_command stream =
416 exc_located_wrapper (lazy (Grammar.Entry.parse command stream))
417 let parse_script stream =
418 exc_located_wrapper (lazy (Grammar.Entry.parse script stream))
422 (** {2 Interface for gTopLevel} *)
424 open DisambiguateTypes
426 module EnvironmentP3 =
432 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
433 let aliases = Grammar.Entry.create aliases_grammar "aliases"
438 (fun domain_item (dsc, _) acc ->
440 match domain_item with
441 | Id id -> sprintf "alias id %s = %s" id dsc
442 | Symbol (symb, instance) ->
443 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
446 sprintf "alias num (instance %d) = \"%s\"" instance dsc
451 String.concat "\n" (List.sort compare aliases)
455 aliases: [ (* build an environment from an aliases list *)
456 [ aliases = LIST0 alias; EOI ->
458 (fun env (domain_item, codomain_item) ->
459 Environment.add domain_item codomain_item env)
460 Environment.empty aliases
463 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
466 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
467 (Id id, choice_of_uri uri)
468 | IDENT "symbol"; symbol = QSTRING;
469 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
470 SYMBOL "="; dsc = QSTRING ->
471 (Symbol (symbol, int_of_string instance),
472 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
474 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
475 SYMBOL "="; dsc = QSTRING ->
476 (Num (int_of_string instance),
477 DisambiguateChoices.lookup_num_by_dsc dsc)
487 Grammar.Entry.parse aliases (Stream.of_string s)
488 with Stdpp.Exc_located ((x, y), exn) ->
489 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
490 (Printexc.to_string exn)))
493 (* vim:set encoding=utf8: *)