]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
fddf27e9995a100f72df291fcc478def9cf601fd
[helm.git] / helm / ocaml / cic_disambiguation / cicTextualParser2.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 let debug = true
27 let debug_print s =
28   if debug then begin
29     prerr_endline "<NEW_TEXTUAL_PARSER>";
30     prerr_endline s;
31     prerr_endline "</NEW_TEXTUAL_PARSER>"
32   end
33
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
37
38 open Printf
39 open DisambiguateTypes
40
41 exception Parse_error of string
42
43 let fresh_num_instance =
44   let n = ref 0 in
45   if use_fresh_num_instances then
46     (fun () -> incr n; !n)
47   else
48     (fun () -> 0)
49
50 let choice_of_uri uri =
51   let term = CicUtil.term_of_uri uri in
52   (uri, (fun _ _ _ -> term))
53
54 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
55
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"
62
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
67
68 let fail floc msg =
69   let (x, y) = CicAst.loc_of_floc floc in
70   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
71
72 let name_of_string = function
73   | "_" -> Cic.Anonymous
74   | s -> Cic.Name s
75
76 let int_opt = function
77   | None -> None
78   | Some lexeme -> Some (int_of_string lexeme)
79
80 EXTEND
81   GLOBAL: term term0 tactic tactical tactical0 command;
82   int: [
83     [ num = NUM ->
84         try
85           int_of_string num
86         with Failure _ ->
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))
90     ]
91   ];
92   meta_subst: [
93     [ s = SYMBOL "_" -> None
94     | t = term -> Some t ]
95   ];
96   binder: [
97     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
98     | SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
99     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
100     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
101   ];
102   sort: [
103     [ "Prop" -> `Prop
104     | "Set" -> `Set
105     | "Type" -> `Type
106     | "CProp" -> `CProp ]
107   ];
108   typed_name: [
109     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
110         (name_of_string i, Some typ)
111     | i = IDENT -> (name_of_string i, None)
112     ]
113   ];
114   substituted_name: [ (* a subs.name is an explicit substitution subject *)
115     [ s = IDENT;
116       subst = OPT [
117         SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
118         PAREN "[";
119         substs = LIST1 [
120           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
121         ] SEP SYMBOL ";";
122         PAREN "]" ->
123           substs
124       ] ->
125         CicAst.Ident (s, subst)
126     ]
127   ];
128   name: [ (* as substituted_name with no explicit substitution *)
129     [ s = [ IDENT | SYMBOL ] -> s ]
130   ];
131   pattern: [
132     [ n = name -> (n, [])
133     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
134         (head, vars)
135     ]
136   ];
137   term0: [ [ t = term; EOI -> return_term loc t ] ];
138   term:
139     [ "letin" NONA
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 ];
145           defs = LIST1 [
146             var = typed_name;
147             index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
148               int_of_string index
149             ];
150             SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
151             t1 = term ->
152               (var, t1, (match index with None -> 0 | Some i -> i))
153           ] SEP "and";
154           "in"; body = term ->
155             return_term loc (CicAst.LetRec (ind_kind, defs, body))
156       ]
157     | "binder" RIGHTA
158       [
159         b = binder;
160         (vars, typ) =
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)
165           ];
166         SYMBOL "."; body = term ->
167           let binder =
168             List.fold_right
169               (fun var body ->
170                 let name = name_of_string var in
171                 CicAst.Binder (b, (name, typ), body))
172               vars body
173           in
174           return_term loc binder
175       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
176             return_term loc
177               (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
178       ]
179     | "logic_add" LEFTA   [ (* nothing here by default *) ]
180     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
181     | "logic_inv" NONA    [ (* nothing here by default *) ]
182     | "relop" LEFTA
183       [ t1 = term; SYMBOL "="; t2 = term ->
184         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
185       ]
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 *) ]
190     | "apply" LEFTA
191       [ t1 = term; t2 = term ->
192         let rec aux = function
193           | CicAst.Appl (hd :: tl) -> aux hd @ tl
194           | term -> [term]
195         in
196         CicAst.Appl (aux t1 @ [t2])
197       ]
198     | "simple" NONA
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
203       | m = META;
204         substs = [
205           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
206             substs
207         ] ->
208             let index =
209               try
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)
213             in
214             return_term loc (CicAst.Meta (index, substs))
215       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
216         "match"; t = term;
217         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
218         "with";
219         PAREN "[";
220         patterns = LIST0 [
221           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
222             ((lhs: CicAst.case_pattern), rhs)
223         ] SEP SYMBOL "|";
224         PAREN "]" ->
225           return_term loc
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
230       ]
231     ];
232   tactic_where: [
233     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
234   ];
235   tactic_term: [ [ t = term -> t ] ];
236   ident_list0: [
237     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
238   ];
239   ident_list1: [
240     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
241   ];
242   reduction_kind: [
243     [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
244     | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
245     | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
246   ];
247   tactic: [
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" ];
268       hyp = IDENT ->
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" ];
273       t1 = tactic_term;
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
296     | [ "let" | "Let" ];
297       t = tactic_term; "in"; where = IDENT ->
298         return_tactic loc (TacticAst.LetIn (t, where))
299     | kind = reduction_kind;
300       pat = OPT [
301         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
302           pat
303       ];
304       terms = LIST0 term SEP SYMBOL "," ->
305         let tac =
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)))
311         in
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))
318     (* TODO Rewrite *)
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" ];
326       t = tactic_term ->
327         return_tactic loc (TacticAst.Transitivity t)
328     ]
329   ];
330   tactical0: [ [ t = tactical; SYMBOL ";;" -> return_tactical loc t ] ];
331   tactical:
332     [ "command" NONA
333       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
334     | "sequence" LEFTA
335       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
336           return_tactical loc (TacticAst.Seq tactics)
337       ]
338     | "then" NONA
339       [ tac = tactical;
340         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
341           return_tactical loc (TacticAst.Then (tac, tacs))
342       ]
343     | "loops" RIGHTA
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)
348       ]
349     | "simple" NONA
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)
359       ]
360     ];
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
367     ]
368   ];
369   command: [
370     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
371     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
372     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
373     | [ IDENT "qed"   | IDENT "Qed"   ] ->
374         return_command loc (TacticAst.Qed None)
375     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
376         return_command loc (TacticAst.Qed (Some name))
377     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
378       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
379         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
380     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
381       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
382         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
383     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
384         return_command loc (TacticAst.Undo (int_opt steps))
385     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
386         return_command loc (TacticAst.Redo (int_opt steps))
387     | [ IDENT "baseuri"   | IDENT "Baseuri" ]; uri = OPT QSTRING ->
388         return_command loc (TacticAst.Baseuri uri)
389     | [ IDENT "check"   | IDENT "Check" ]; t = term ->
390         return_command loc (TacticAst.Check t)
391     ]
392   ];
393 END
394
395 let exc_located_wrapper f =
396   try
397     Lazy.force f
398   with Stdpp.Exc_located (floc, exn) ->
399     let (x, y) = CicAst.loc_of_floc floc in
400     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
401         (Printexc.to_string exn)))
402
403 let parse_term stream =
404   exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
405 let parse_tactic stream =
406   exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
407 let parse_tactical stream =
408   exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
409
410 (**/**)
411
412 (** {2 Interface for gTopLevel} *)
413
414 module EnvironmentP3 =
415   struct
416     type t = environment
417
418     let empty = ""
419
420     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
421     let aliases = Grammar.Entry.create aliases_grammar "aliases"
422
423     let to_string env =
424       let aliases =
425         Environment.fold
426           (fun domain_item (dsc, _) acc ->
427             let s =
428               match domain_item with
429               | Id id -> sprintf "alias id %s = %s" id dsc
430               | Symbol (symb, instance) ->
431                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
432                     symb instance dsc
433               | Num instance ->
434                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
435             in
436             s :: acc)
437           env []
438       in
439       String.concat "\n" (List.sort compare aliases)
440
441     EXTEND
442       GLOBAL: aliases;
443       aliases: [  (* build an environment from an aliases list *)
444         [ aliases = LIST0 alias; EOI ->
445             List.fold_left
446               (fun env (domain_item, codomain_item) ->
447                 Environment.add domain_item codomain_item env)
448               Environment.empty aliases
449         ]
450       ];
451       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
452         [ IDENT "alias";
453           choice =
454             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
455                 (Id id, choice_of_uri uri)
456             | IDENT "symbol"; symbol = QSTRING;
457               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
458               SYMBOL "="; dsc = QSTRING ->
459                 (Symbol (symbol, int_of_string instance),
460                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
461             | IDENT "num";
462               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
463               SYMBOL "="; dsc = QSTRING ->
464                 (Num (int_of_string instance),
465                  DisambiguateChoices.lookup_num_by_dsc dsc)
466             ] -> choice ]
467       ];
468     END
469
470     let of_string s =
471       if s = empty then
472         Environment.empty
473       else
474         try
475           Grammar.Entry.parse aliases (Stream.of_string s)
476         with Stdpp.Exc_located (floc, exn) ->
477           let (x, y) = CicAst.loc_of_floc floc in
478           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
479           (Printexc.to_string exn)))
480   end
481
482 (* vim:set encoding=utf8: *)