]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
debian version 0.6.3-2
[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: string) =
51   let cic = HelmLibraryObjects.term_of_uri (UriManager.uri_of_string uri) in
52   (uri, (fun _ _ _ -> cic))
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 EXTEND
77   GLOBAL: term term0 tactic tactical tactical0 command;
78   int: [
79     [ num = NUM ->
80         try
81           int_of_string num
82         with Failure _ ->
83           let (x, y) = CicAst.loc_of_floc loc in
84           raise (Parse_error (sprintf
85             "integer literal expected at characters %d-%d" x y))
86     ]
87   ];
88   meta_subst: [
89     [ s = SYMBOL "_" -> None
90     | t = term -> Some t ]
91   ];
92   binder: [
93     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
94     | SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
95     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
96     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
97   ];
98   sort: [
99     [ "Prop" -> `Prop
100     | "Set" -> `Set
101     | "Type" -> `Type
102     | "CProp" -> `CProp ]
103   ];
104   typed_name: [
105     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
106         (name_of_string i, Some typ)
107     | i = IDENT -> (name_of_string i, None)
108     ]
109   ];
110   substituted_name: [ (* a subs.name is an explicit substitution subject *)
111     [ s = [ IDENT | SYMBOL ];
112       subst = OPT [
113         SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
114         PAREN "[";
115         substs = LIST1 [
116           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
117         ] SEP SYMBOL ";";
118         PAREN "]" ->
119           substs
120       ] ->
121         CicAst.Ident (s, subst)
122     ]
123   ];
124   name: [ (* as substituted_name with no explicit substitution *)
125     [ s = [ IDENT | SYMBOL ] -> s ]
126   ];
127   pattern: [
128     [ n = name -> (n, [])
129     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
130         (head, vars)
131     ]
132   ];
133   term0: [ [ t = term; EOI -> return_term loc t ] ];
134   term:
135     [ "letin" NONA
136         (* actually "in" and "and" are _not_ keywords. Parsing works anyway
137          * since applications are required to be bound by parens *)
138       [ "let"; var = typed_name;
139         SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
140         t1 = term;
141         IDENT "in"; t2 = term ->
142           return_term loc (CicAst.LetIn (var, t1, t2))
143       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
144           defs = LIST1 [
145             var = typed_name;
146             index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
147               int_of_string index
148             ];
149             SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
150             t1 = term ->
151               (var, t1, (match index with None -> 0 | Some i -> i))
152           ] SEP (IDENT "and");
153           IDENT "in"; body = term ->
154             return_term loc (CicAst.LetRec (ind_kind, defs, body))
155       ]
156     | "binder" RIGHTA
157       [
158         b = binder;
159         (vars, typ) =
160           [ vars = LIST1 IDENT SEP SYMBOL ",";
161             typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
162           | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
163             typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
164           ];
165         SYMBOL "."; body = term ->
166           let binder =
167             List.fold_right
168               (fun var body ->
169                 let name = name_of_string var in
170                 CicAst.Binder (b, (name, typ), body))
171               vars body
172           in
173           return_term loc binder
174       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
175             return_term loc
176               (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
177       ]
178     | "logic_add" LEFTA   [ (* nothing here by default *) ]
179     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
180     | "logic_inv" NONA    [ (* nothing here by default *) ]
181     | "relop" LEFTA
182       [ t1 = term; SYMBOL "="; t2 = term ->
183         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
184       ]
185     | "add" LEFTA     [ (* nothing here by default *) ]
186     | "mult" LEFTA    [ (* nothing here by default *) ]
187     | "power" LEFTA   [ (* nothing here by default *) ]
188     | "inv" NONA      [ (* nothing here by default *) ]
189     | "simple" NONA
190       [ sort = sort -> CicAst.Sort sort
191       | n = substituted_name -> return_term loc n
192       | PAREN "("; head = term; args = LIST1 term; PAREN ")" ->
193           return_term loc (CicAst.Appl (head :: args))
194       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
195       | IMPLICIT -> return_term loc CicAst.Implicit
196       | m = META;
197         substs = [
198           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
199             substs
200         ] ->
201             let index =
202               try
203                 int_of_string (String.sub m 1 (String.length m - 1))
204               with Failure "int_of_string" ->
205                 fail loc ("Invalid meta variable number: " ^ m)
206             in
207             return_term loc (CicAst.Meta (index, substs))
208       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
209         "match"; t = term;
210         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
211         "with";
212         PAREN "[";
213         patterns = LIST0 [
214           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
215             ((lhs: CicAst.case_pattern), rhs)
216         ] SEP SYMBOL "|";
217         PAREN "]" ->
218           return_term loc
219             (CicAst.Case (t, indty_ident, outtyp, patterns))
220       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
221           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
222       | PAREN "("; t = term; PAREN ")" -> return_term loc t
223       ]
224     ];
225   tactic_where: [
226     [ where = OPT [ IDENT "in"; ident = IDENT -> ident ] -> where ]
227   ];
228   tactic_term: [ [ t = term -> t ] ];
229   ident_list0: [
230     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
231   ];
232   ident_list1: [
233     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
234   ];
235   reduction_kind: [
236     [ "reduce" -> `Reduce
237     | "simpl" -> `Simpl
238     | "whd" -> `Whd ]
239   ];
240   tactic: [
241     [ [ IDENT "absurd" | IDENT "Absurd" ] -> return_tactic loc TacticAst.Absurd
242     | [ IDENT "apply" | IDENT "Apply" ];
243       t = tactic_term -> return_tactic loc (TacticAst.Apply t)
244     | [ IDENT "assumption" | IDENT "Assumption" ] ->
245         return_tactic loc TacticAst.Assumption
246     | [ IDENT "change" | IDENT "Change" ];
247       t1 = tactic_term; "with"; t2 = tactic_term;
248       where = tactic_where ->
249         return_tactic loc (TacticAst.Change (t1, t2, where))
250     (* TODO Change_pattern *)
251     | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
252         return_tactic loc TacticAst.Contradiction
253     | [ IDENT "cut" | IDENT "Cut" ];
254       t = tactic_term -> return_tactic loc (TacticAst.Cut t)
255     | [ IDENT "decompose" | IDENT "Decompose" ];
256       principles = ident_list1; where = IDENT ->
257         return_tactic loc (TacticAst.Decompose (where, principles))
258     | [ IDENT "discriminate" | IDENT "Discriminate" ];
259       hyp = IDENT ->
260         return_tactic loc (TacticAst.Discriminate hyp)
261     | [ IDENT "elim" | IDENT "Elim" ]; IDENT "type";
262       t = tactic_term ->
263         return_tactic loc (TacticAst.ElimType t)
264     | [ IDENT "elim" | IDENT "Elim" ];
265       t1 = tactic_term;
266       using = OPT [ "using"; using = tactic_term -> using ] ->
267         return_tactic loc (TacticAst.Elim (t1, using))
268     | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
269         return_tactic loc (TacticAst.Exact t)
270     | [ IDENT "exists" | IDENT "Exists" ] ->
271         return_tactic loc TacticAst.Exists
272     | [ IDENT "fold" | IDENT "Fold" ];
273       kind = reduction_kind; t = tactic_term ->
274         return_tactic loc (TacticAst.Fold (kind, t))
275     | [ IDENT "fourier" | IDENT "Fourier" ] ->
276         return_tactic loc TacticAst.Fourier
277     | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
278         return_tactic loc (TacticAst.Injection ident)
279     | [ IDENT "intros" | IDENT "Intros" ];
280       num = OPT [ num = int -> num ];
281       idents = OPT ident_list0 ->
282         let idents = match idents with None -> [] | Some idents -> idents in
283         return_tactic loc (TacticAst.Intros (num, idents))
284     | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
285     | [ "let" | "Let" ];
286       t = tactic_term; IDENT "in"; where = IDENT ->
287         return_tactic loc (TacticAst.LetIn (t, where))
288     (* TODO Reduce *)
289     | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
290         return_tactic loc TacticAst.Reflexivity
291     | [ IDENT "replace" | IDENT "Replace" ];
292       t1 = tactic_term; "with"; t2 = tactic_term ->
293         return_tactic loc (TacticAst.Replace (t1, t2))
294     (* TODO Rewrite *)
295     (* TODO Replace_pattern *)
296     | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
297     | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
298     | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
299     | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
300         return_tactic loc TacticAst.Symmetry
301     | [ IDENT "transitivity" | IDENT "Transitivity" ];
302       t = tactic_term ->
303         return_tactic loc (TacticAst.Transitivity t)
304     ]
305   ];
306   tactical0: [ [ t = tactical; SYMBOL "." -> t ] ];
307   tactical:
308     [ "command" NONA
309       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
310     | "sequence" LEFTA
311       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
312           return_tactical loc (TacticAst.Seq tactics)
313       ]
314     | "then" NONA
315       [ tac = tactical;
316         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
317           return_tactical loc (TacticAst.Then (tac, tacs))
318       ]
319     | "loops" RIGHTA
320       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
321           return_tactical loc (TacticAst.Do (count, tac))
322       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
323           return_tactical loc (TacticAst.Repeat tac)
324       ]
325     | "simple" NONA
326       [ [ IDENT "tries" | IDENT "Tries" ];
327         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
328           return_tactical loc (TacticAst.Tries tacs)
329       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
330           return_tactical loc (TacticAst.Try tac)
331       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
332       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
333       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
334       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
335       ]
336     ];
337   theorem_flavour: [  (* all flavours but Goal *)
338     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
339     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
340     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
341     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
342     | [ IDENT "theorem"     | IDENT "Theorem"     ] -> `Theorem
343     ]
344   ];
345   command: [
346     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
347     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
348     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
349     | [ IDENT "qed"   | IDENT "Qed"   ] ->
350         return_command loc (TacticAst.Qed None)
351     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
352         return_command loc (TacticAst.Qed (Some name))
353     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
354       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
355         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
356     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
357       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
358         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
359     ]
360   ];
361 END
362
363 let exc_located_wrapper f =
364   try
365     Lazy.force f
366   with Stdpp.Exc_located (floc, exn) ->
367     let (x, y) = CicAst.loc_of_floc floc in
368     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
369         (Printexc.to_string exn)))
370
371 let parse_term stream =
372   exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
373 let parse_tactic stream =
374   exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
375 let parse_tactical stream =
376   exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
377
378 (**/**)
379
380 (** {2 Interface for gTopLevel} *)
381
382 module EnvironmentP3 =
383   struct
384     type t = environment
385
386     let empty = ""
387
388     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
389     let aliases = Grammar.Entry.create aliases_grammar "aliases"
390
391     let to_string env =
392       let aliases =
393         Environment.fold
394           (fun domain_item (dsc, _) acc ->
395             let s =
396               match domain_item with
397               | Id id -> sprintf "alias id %s = %s" id dsc
398               | Symbol (symb, instance) ->
399                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
400                     symb instance dsc
401               | Num instance ->
402                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
403             in
404             s :: acc)
405           env []
406       in
407       String.concat "\n" (List.sort compare aliases)
408
409     EXTEND
410       GLOBAL: aliases;
411       aliases: [  (* build an environment from an aliases list *)
412         [ aliases = LIST0 alias; EOI ->
413             List.fold_left
414               (fun env (domain_item, codomain_item) ->
415                 Environment.add domain_item codomain_item env)
416               Environment.empty aliases
417         ]
418       ];
419       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
420         [ IDENT "alias";
421           choice =
422             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
423                 (Id id, choice_of_uri uri)
424             | IDENT "symbol"; symbol = QSTRING;
425               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
426               SYMBOL "="; dsc = QSTRING ->
427                 (Symbol (symbol, int_of_string instance),
428                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
429             | IDENT "num";
430               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
431               SYMBOL "="; dsc = QSTRING ->
432                 (Num (int_of_string instance),
433                  DisambiguateChoices.lookup_num_by_dsc dsc)
434             ] -> choice ]
435       ];
436     END
437
438     let of_string s =
439       if s = empty then
440         Environment.empty
441       else
442         try
443           Grammar.Entry.parse aliases (Stream.of_string s)
444         with Stdpp.Exc_located (floc, exn) ->
445           let (x, y) = CicAst.loc_of_floc floc in
446           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
447           (Printexc.to_string exn)))
448   end
449
450 (* vim:set encoding=utf8: *)