]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
1b2ef365730c6310ac89997b711eb98f92f81af9
[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 (x, y) msg =
69   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
70
71 let name_of_string = function
72   | "_" -> Cic.Anonymous
73   | s -> Cic.Name s
74
75 EXTEND
76   GLOBAL: term term0 tactic tactical tactical0 command;
77   int: [
78     [ num = NUM ->
79         try
80           int_of_string num
81         with Failure _ ->
82           let (x, y) = loc in
83           raise (Parse_error (sprintf
84             "integer literal expected at characters %d-%d" x y))
85     ]
86   ];
87   meta_subst: [
88     [ s = SYMBOL "_" -> None
89     | t = term -> Some t ]
90   ];
91   binder: [
92     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
93     | SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
94     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
95     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
96   ];
97   sort: [
98     [ "Prop" -> `Prop
99     | "Set" -> `Set
100     | "Type" -> `Type
101     | "CProp" -> `CProp ]
102   ];
103   typed_name: [
104     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
105         (name_of_string i, Some typ)
106     | i = IDENT -> (name_of_string i, None)
107     ]
108   ];
109   substituted_name: [ (* a subs.name is an explicit substitution subject *)
110     [ s = [ IDENT | SYMBOL ];
111       subst = OPT [
112         SYMBOL "\subst";  (* to avoid catching frequent "a [1]" cases *)
113         PAREN "[";
114         substs = LIST1 [
115           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
116         ] SEP SYMBOL ";";
117         PAREN "]" ->
118           substs
119       ] ->
120         CicAst.Ident (s, subst)
121     ]
122   ];
123   name: [ (* as substituted_name with no explicit substitution *)
124     [ s = [ IDENT | SYMBOL ] -> s ]
125   ];
126   pattern: [
127     [ n = name -> (n, [])
128     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
129         (head, vars)
130     ]
131   ];
132   term0: [ [ t = term; EOI -> return_term loc t ] ];
133   term:
134     [ "letin" NONA
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>> (* ≝ *); *)
139         t1 = term;
140         IDENT "in"; t2 = term ->
141           return_term loc (CicAst.LetIn (var, t1, t2))
142       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
143           defs = LIST1 [
144             var = typed_name;
145             index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
146               int_of_string index
147             ];
148             SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
149             t1 = term ->
150               (var, t1, (match index with None -> 0 | Some i -> i))
151           ] SEP (IDENT "and");
152           IDENT "in"; body = term ->
153             return_term loc (CicAst.LetRec (ind_kind, defs, body))
154       ]
155     | "binder" RIGHTA
156       [
157         b = binder;
158         (vars, typ) =
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)
163           ];
164         SYMBOL "."; body = term ->
165           let binder =
166             List.fold_right
167               (fun var body ->
168                 let name = name_of_string var in
169                 CicAst.Binder (b, (name, typ), body))
170               vars body
171           in
172           return_term loc binder
173       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
174             return_term loc
175               (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
176       ]
177     | "logic_add" LEFTA   [ (* nothing here by default *) ]
178     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
179     | "logic_inv" NONA    [ (* nothing here by default *) ]
180     | "relop" LEFTA
181       [ t1 = term; SYMBOL "="; t2 = term ->
182         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
183       ]
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 *) ]
188     | "simple" NONA
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
195       | m = META;
196         substs = [
197           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
198             substs
199         ] ->
200             let index =
201               try
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)
205             in
206             return_term loc (CicAst.Meta (index, substs))
207       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
208         "match"; t = term;
209         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
210         "with";
211         PAREN "[";
212         patterns = LIST0 [
213           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
214             ((lhs: CicAst.case_pattern), rhs)
215         ] SEP SYMBOL "|";
216         PAREN "]" ->
217           return_term loc
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
222       ]
223     ];
224   tactic_where: [
225     [ where = OPT [ IDENT "in"; ident = IDENT -> ident ] -> where ]
226   ];
227   tactic_term: [ [ t = term -> t ] ];
228   ident_list0: [
229     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
230   ];
231   ident_list1: [
232     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
233   ];
234   reduction_kind: [
235     [ "reduce" -> `Reduce
236     | "simpl" -> `Simpl
237     | "whd" -> `Whd ]
238   ];
239   tactic: [
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" ];
258       hyp = IDENT ->
259         return_tactic loc (TacticAst.Discriminate hyp)
260     | [ IDENT "elim" | IDENT "Elim" ]; IDENT "type";
261       t = tactic_term ->
262         return_tactic loc (TacticAst.ElimType t)
263     | [ IDENT "elim" | IDENT "Elim" ];
264       t1 = tactic_term;
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
284     | [ "let" | "Let" ];
285       t = tactic_term; IDENT "in"; where = IDENT ->
286         return_tactic loc (TacticAst.LetIn (t, where))
287     (* TODO Reduce *)
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))
293     (* TODO Rewrite *)
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" ];
301       t = tactic_term ->
302         return_tactic loc (TacticAst.Transitivity t)
303     ]
304   ];
305   tactical0: [ [ t = tactical; SYMBOL "." -> t ] ];
306   tactical:
307     [ "command" NONA
308       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
309     | "sequence" LEFTA
310       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
311           return_tactical loc (TacticAst.Seq tactics)
312       ]
313     | "then" NONA
314       [ tac = tactical;
315         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
316           return_tactical loc (TacticAst.Then (tac, tacs))
317       ]
318     | "loops" RIGHTA
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)
323       ]
324     | "simple" NONA
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)
334       ]
335     ];
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
342     ]
343   ];
344   command: [
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))
358     ]
359   ];
360 END
361
362 let exc_located_wrapper f =
363   try
364     Lazy.force 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)))
368
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))
375
376 (**/**)
377
378 (** {2 Interface for gTopLevel} *)
379
380 module EnvironmentP3 =
381   struct
382     type t = environment
383
384     let empty = ""
385
386     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
387     let aliases = Grammar.Entry.create aliases_grammar "aliases"
388
389     let to_string env =
390       let aliases =
391         Environment.fold
392           (fun domain_item (dsc, _) acc ->
393             let s =
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\""
398                     symb instance dsc
399               | Num instance ->
400                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
401             in
402             s :: acc)
403           env []
404       in
405       String.concat "\n" (List.sort compare aliases)
406
407     EXTEND
408       GLOBAL: aliases;
409       aliases: [  (* build an environment from an aliases list *)
410         [ aliases = LIST0 alias; EOI ->
411             List.fold_left
412               (fun env (domain_item, codomain_item) ->
413                 Environment.add domain_item codomain_item env)
414               Environment.empty aliases
415         ]
416       ];
417       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
418         [ IDENT "alias";
419           choice =
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)
427             | IDENT "num";
428               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
429               SYMBOL "="; dsc = QSTRING ->
430                 (Num (int_of_string instance),
431                  DisambiguateChoices.lookup_num_by_dsc dsc)
432             ] -> choice ]
433       ];
434     END
435
436     let of_string s =
437       if s = empty then
438         Environment.empty
439       else
440         try
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)))
445   end
446
447 (* vim:set encoding=utf8: *)