]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
a539fef6c238783365912191b037985ec8a75a3d
[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 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;
162             typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
163           | PAREN "("; vars = LIST1 IDENT;
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     [ "reduce" -> `Reduce
244     | "simpl" -> `Simpl
245     | "whd" -> `Whd ]
246   ];
247   tactic: [
248     [ [ IDENT "absurd" | IDENT "Absurd" ] -> return_tactic loc TacticAst.Absurd
249     | [ IDENT "apply" | IDENT "Apply" ];
250       t = tactic_term -> return_tactic loc (TacticAst.Apply t)
251     | [ IDENT "assumption" | IDENT "Assumption" ] ->
252         return_tactic loc TacticAst.Assumption
253     | [ IDENT "change" | IDENT "Change" ];
254       t1 = tactic_term; "with"; t2 = tactic_term;
255       where = tactic_where ->
256         return_tactic loc (TacticAst.Change (t1, t2, where))
257     (* TODO Change_pattern *)
258     | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
259         return_tactic loc TacticAst.Contradiction
260     | [ IDENT "cut" | IDENT "Cut" ];
261       t = tactic_term -> return_tactic loc (TacticAst.Cut t)
262     | [ IDENT "decompose" | IDENT "Decompose" ];
263       principles = ident_list1; where = IDENT ->
264         return_tactic loc (TacticAst.Decompose (where, principles))
265     | [ IDENT "discriminate" | IDENT "Discriminate" ];
266       hyp = IDENT ->
267         return_tactic loc (TacticAst.Discriminate hyp)
268     | [ IDENT "elim" | IDENT "Elim" ]; IDENT "type";
269       t = tactic_term ->
270         return_tactic loc (TacticAst.ElimType t)
271     | [ IDENT "elim" | IDENT "Elim" ];
272       t1 = tactic_term;
273       using = OPT [ "using"; using = tactic_term -> using ] ->
274         return_tactic loc (TacticAst.Elim (t1, using))
275     | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
276         return_tactic loc (TacticAst.Exact t)
277     | [ IDENT "exists" | IDENT "Exists" ] ->
278         return_tactic loc TacticAst.Exists
279     | [ IDENT "fold" | IDENT "Fold" ];
280       kind = reduction_kind; t = tactic_term ->
281         return_tactic loc (TacticAst.Fold (kind, t))
282     | [ IDENT "fourier" | IDENT "Fourier" ] ->
283         return_tactic loc TacticAst.Fourier
284     | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
285         return_tactic loc (TacticAst.Injection ident)
286     | [ IDENT "intros" | IDENT "Intros" ];
287       num = OPT [ num = int -> num ];
288       idents = OPT ident_list0 ->
289         let idents = match idents with None -> [] | Some idents -> idents in
290         return_tactic loc (TacticAst.Intros (num, idents))
291     | [ IDENT "intro" | IDENT "Intro" ] ->
292         return_tactic loc (TacticAst.Intros (Some 1, []))
293     | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
294     | [ "let" | "Let" ];
295       t = tactic_term; "in"; where = IDENT ->
296         return_tactic loc (TacticAst.LetIn (t, where))
297     (* TODO Reduce *)
298     | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
299         return_tactic loc TacticAst.Reflexivity
300     | [ IDENT "replace" | IDENT "Replace" ];
301       t1 = tactic_term; "with"; t2 = tactic_term ->
302         return_tactic loc (TacticAst.Replace (t1, t2))
303     (* TODO Rewrite *)
304     (* TODO Replace_pattern *)
305     | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
306     | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
307     | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
308     | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
309         return_tactic loc TacticAst.Symmetry
310     | [ IDENT "transitivity" | IDENT "Transitivity" ];
311       t = tactic_term ->
312         return_tactic loc (TacticAst.Transitivity t)
313     ]
314   ];
315   tactical0: [ [ t = tactical; SYMBOL "." -> t ] ];
316   tactical:
317     [ "command" NONA
318       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
319     | "sequence" LEFTA
320       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
321           return_tactical loc (TacticAst.Seq tactics)
322       ]
323     | "then" NONA
324       [ tac = tactical;
325         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
326           return_tactical loc (TacticAst.Then (tac, tacs))
327       ]
328     | "loops" RIGHTA
329       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
330           return_tactical loc (TacticAst.Do (count, tac))
331       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
332           return_tactical loc (TacticAst.Repeat tac)
333       ]
334     | "simple" NONA
335       [ [ IDENT "tries" | IDENT "Tries" ];
336         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
337           return_tactical loc (TacticAst.Tries tacs)
338       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
339           return_tactical loc (TacticAst.Try tac)
340       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
341       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
342       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
343       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
344       ]
345     ];
346   theorem_flavour: [  (* all flavours but Goal *)
347     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
348     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
349     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
350     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
351     | [ IDENT "theorem"     | IDENT "Theorem"     ] -> `Theorem
352     ]
353   ];
354   command: [
355     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
356     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
357     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
358     | [ IDENT "qed"   | IDENT "Qed"   ] ->
359         return_command loc (TacticAst.Qed None)
360     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
361         return_command loc (TacticAst.Qed (Some name))
362     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
363       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
364         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
365     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
366       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
367         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
368     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
369         return_command loc (TacticAst.Undo (int_opt steps))
370     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
371         return_command loc (TacticAst.Redo (int_opt steps))
372     ]
373   ];
374 END
375
376 let exc_located_wrapper f =
377   try
378     Lazy.force f
379   with Stdpp.Exc_located (floc, exn) ->
380     let (x, y) = CicAst.loc_of_floc floc in
381     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
382         (Printexc.to_string exn)))
383
384 let parse_term stream =
385   exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
386 let parse_tactic stream =
387   exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
388 let parse_tactical stream =
389   exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
390
391 (**/**)
392
393 (** {2 Interface for gTopLevel} *)
394
395 module EnvironmentP3 =
396   struct
397     type t = environment
398
399     let empty = ""
400
401     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
402     let aliases = Grammar.Entry.create aliases_grammar "aliases"
403
404     let to_string env =
405       let aliases =
406         Environment.fold
407           (fun domain_item (dsc, _) acc ->
408             let s =
409               match domain_item with
410               | Id id -> sprintf "alias id %s = %s" id dsc
411               | Symbol (symb, instance) ->
412                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
413                     symb instance dsc
414               | Num instance ->
415                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
416             in
417             s :: acc)
418           env []
419       in
420       String.concat "\n" (List.sort compare aliases)
421
422     EXTEND
423       GLOBAL: aliases;
424       aliases: [  (* build an environment from an aliases list *)
425         [ aliases = LIST0 alias; EOI ->
426             List.fold_left
427               (fun env (domain_item, codomain_item) ->
428                 Environment.add domain_item codomain_item env)
429               Environment.empty aliases
430         ]
431       ];
432       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
433         [ IDENT "alias";
434           choice =
435             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
436                 (Id id, choice_of_uri uri)
437             | IDENT "symbol"; symbol = QSTRING;
438               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
439               SYMBOL "="; dsc = QSTRING ->
440                 (Symbol (symbol, int_of_string instance),
441                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
442             | IDENT "num";
443               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
444               SYMBOL "="; dsc = QSTRING ->
445                 (Num (int_of_string instance),
446                  DisambiguateChoices.lookup_num_by_dsc dsc)
447             ] -> choice ]
448       ];
449     END
450
451     let of_string s =
452       if s = empty then
453         Environment.empty
454       else
455         try
456           Grammar.Entry.parse aliases (Stream.of_string s)
457         with Stdpp.Exc_located (floc, exn) ->
458           let (x, y) = CicAst.loc_of_floc floc in
459           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
460           (Printexc.to_string exn)))
461   end
462
463 (* vim:set encoding=utf8: *)