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 let choice_of_uri (uri: string) =
39 let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
40 (uri, (fun _ _ _ -> cic))
42 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
44 let term = Grammar.Entry.create grammar "term"
45 let term0 = Grammar.Entry.create grammar "term0"
46 let tactic = Grammar.Entry.create grammar "tactic"
47 let tactical = Grammar.Entry.create grammar "tactical"
48 let tactical0 = Grammar.Entry.create grammar "tactical0"
50 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
51 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
52 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
55 failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
57 let name_of_string = function
58 | "_" -> Cic.Anonymous
62 GLOBAL: term term0 tactic tactical tactical0;
69 raise (Parse_error (sprintf
70 "integer literal expected at characters %d-%d" x y))
74 [ s = SYMBOL "_" -> None
75 | t = term -> Some t ]
78 [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
79 | SYMBOL <:unicode<Pi>> (* Π *) -> `Pi
80 | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
81 | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
90 [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
91 (name_of_string i, Some typ)
92 | i = IDENT -> (name_of_string i, None)
95 substituted_name: [ (* a subs.name is an explicit substitution subject *)
96 [ s = [ IDENT | SYMBOL ];
98 SYMBOL "\subst"; (* to avoid catching frequent "a [1]" cases *)
101 i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
107 | Some l -> CicAst.Ident (s, l)
108 | None -> CicAst.Ident (s, []))
111 name: [ (* as substituted_name with no explicit substitution *)
112 [ s = [ IDENT | SYMBOL ] -> s ]
115 [ n = name -> (n, [])
116 | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
120 term0: [ [ t = term; EOI -> return_term loc t ] ];
126 [ vars = LIST1 IDENT SEP SYMBOL ",";
127 typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
128 | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
129 typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
131 SYMBOL "."; body = term ->
135 let name = name_of_string var in
136 CicAst.Binder (b, (name, typ), body))
139 return_term loc binder
140 | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
142 (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
145 [ t1 = term; SYMBOL "="; t2 = term ->
146 return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
148 | "add" LEFTA [ (* nothing here by default *) ]
149 | "mult" LEFTA [ (* nothing here by default *) ]
150 | "inv" NONA [ (* nothing here by default *) ]
152 [ sort = sort -> CicAst.Sort sort
153 | n = substituted_name -> return_term loc n
154 | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
155 return_term loc (CicAst.Appl (head :: args))
156 | i = NUM -> return_term loc (CicAst.Num (i, 0))
157 | IMPLICIT -> return_term loc CicAst.Implicit
160 PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
165 int_of_string (String.sub m 1 (String.length m - 1))
166 with Failure "int_of_string" ->
167 fail loc ("Invalid meta variable number: " ^ m)
169 return_term loc (CicAst.Meta (index, substs))
170 (* actually "in" and "and" are _not_ keywords. Parsing works anyway
171 * since applications are required to be bound by parens *)
172 | "let"; var = typed_name;
173 (* SYMBOL <:unicode<def>> (* ≝ *); *)
176 IDENT "in"; t2 = term ->
177 return_term loc (CicAst.LetIn (var, t1, t2))
178 | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
181 index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
184 (* SYMBOL <:unicode<def>> (* ≝ *); *)
187 (var, t1, (match index with None -> 0 | Some i -> i))
189 IDENT "in"; body = term ->
190 return_term loc (CicAst.LetRec (ind_kind, defs, body))
191 | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
193 indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
197 lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
198 ((lhs: CicAst.case_pattern), rhs)
202 (CicAst.Case (t, indty_ident, outtyp, patterns))
203 | PAREN "("; t = term; PAREN ")" -> return_term loc t
207 [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
213 [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
216 [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
219 [ "reduce" -> `Reduce
224 [ IDENT "absurd" -> return_tactic loc TacticAst.Absurd
225 | IDENT "apply"; t = tactic_term -> return_tactic loc (TacticAst.Apply t)
226 | IDENT "assumption" -> return_tactic loc TacticAst.Assumption
227 | IDENT "change"; t1 = tactic_term; "with"; t2 = tactic_term;
228 where = tactic_where ->
229 return_tactic loc (TacticAst.Change (t1, t2, where))
230 (* TODO Change_pattern *)
231 | IDENT "contradiction" -> return_tactic loc TacticAst.Contradiction
232 | IDENT "cut"; t = tactic_term -> return_tactic loc (TacticAst.Cut t)
233 | IDENT "decompose"; principles = ident_list1; where = IDENT ->
234 return_tactic loc (TacticAst.Decompose (where, principles))
235 | IDENT "discriminate"; hyp = IDENT ->
236 return_tactic loc (TacticAst.Discriminate hyp)
237 | IDENT "elim"; IDENT "type"; t = tactic_term ->
238 return_tactic loc (TacticAst.ElimType t)
239 | IDENT "elim"; t1 = tactic_term;
240 using = OPT [ IDENT "using"; using = tactic_term -> using ] ->
241 return_tactic loc (TacticAst.Elim (t1, using))
242 | IDENT "exact"; t = tactic_term -> return_tactic loc (TacticAst.Exact t)
243 | IDENT "exists" -> return_tactic loc TacticAst.Exists
244 | IDENT "fold"; kind = reduction_kind; t = tactic_term ->
245 return_tactic loc (TacticAst.Fold (kind, t))
246 | IDENT "fourier" -> return_tactic loc TacticAst.Fourier
247 | IDENT "injection"; ident = IDENT ->
248 return_tactic loc (TacticAst.Injection ident)
250 num = OPT [ num = int -> num ];
251 idents = OPT ident_list0 ->
252 let idents = match idents with None -> [] | Some idents -> idents in
253 return_tactic loc (TacticAst.Intros (num, idents))
254 | IDENT "left" -> return_tactic loc TacticAst.Left
255 | "let"; t = tactic_term; IDENT "in"; where = IDENT ->
256 return_tactic loc (TacticAst.LetIn (t, where))
258 | IDENT "reflexivity" -> return_tactic loc TacticAst.Reflexivity
259 | IDENT "replace"; t1 = tactic_term; "with"; t2 = tactic_term ->
260 return_tactic loc (TacticAst.Replace (t1, t2))
262 (* TODO Replace_pattern *)
263 | IDENT "right" -> return_tactic loc TacticAst.Right
264 | IDENT "ring" -> return_tactic loc TacticAst.Ring
265 | IDENT "split" -> return_tactic loc TacticAst.Split
266 | IDENT "symmetry" -> return_tactic loc TacticAst.Symmetry
267 | IDENT "transitivity"; t = tactic_term ->
268 return_tactic loc (TacticAst.Transitivity t)
271 tactical0 : [ [ t = tactical; SYMBOL "." -> t ] ];
273 [ IDENT "fail" -> return_tactical loc TacticAst.Fail
274 | IDENT "do"; count = int; tac = tactical ->
275 return_tactical loc (TacticAst.Do (count, tac))
276 | IDENT "id" -> return_tactical loc TacticAst.IdTac
277 | IDENT "repeat"; tac = tactical ->
278 return_tactical loc (TacticAst.Repeat tac)
279 | tactics = LIST0 tactical SEP SYMBOL ";" ->
280 return_tactical loc (TacticAst.Seq tactics)
282 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
283 return_tactical loc (TacticAst.Then (tac, tacs))
285 PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
286 return_tactical loc (TacticAst.Tries tacs)
287 | IDENT "try"; tac = tactical -> return_tactical loc (TacticAst.Try tac)
288 | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
289 | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
294 let parse_term stream =
296 Grammar.Entry.parse term0 stream
297 with Stdpp.Exc_located ((x, y), exn) ->
298 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
299 (Printexc.to_string exn)))
301 let parse_tactic stream =
303 Grammar.Entry.parse tactic stream
304 with Stdpp.Exc_located ((x, y), exn) ->
305 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
306 (Printexc.to_string exn)))
308 let parse_tactical stream =
310 Grammar.Entry.parse tactical0 stream
311 with Stdpp.Exc_located ((x, y), exn) ->
312 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
313 (Printexc.to_string exn)))
317 (** {2 Interface for gTopLevel} *)
319 open DisambiguateTypes
321 module EnvironmentP3 =
327 let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
328 let aliases = Grammar.Entry.create aliases_grammar "aliases"
333 (fun domain_item (dsc, _) acc ->
335 match domain_item with
336 | Id id -> sprintf "alias id %s = %s" id dsc
337 | Symbol (symb, instance) ->
338 sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
341 sprintf "alias num (instance %d) = \"%s\"" instance dsc
346 String.concat "\n" (List.sort compare aliases)
350 aliases: [ (* build an environment from an aliases list *)
351 [ aliases = LIST0 alias; EOI ->
353 (fun env (domain_item, codomain_item) ->
354 Environment.add domain_item codomain_item env)
355 Environment.empty aliases
358 alias: [ (* return a pair <domain_item, codomain_item> from an alias *)
361 [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
362 (Id id, choice_of_uri uri)
363 | IDENT "symbol"; symbol = QSTRING;
364 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
365 SYMBOL "="; dsc = QSTRING ->
366 (Symbol (symbol, int_of_string instance),
367 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
369 PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
370 SYMBOL "="; dsc = QSTRING ->
371 (Num (int_of_string instance),
372 DisambiguateChoices.lookup_num_by_dsc dsc)
382 Grammar.Entry.parse aliases (Stream.of_string s)
383 with Stdpp.Exc_located ((x, y), exn) ->
384 raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
385 (Printexc.to_string exn)))