]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
added Auto parsing
[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     [ "reduce" -> `Reduce
244     | "simpl" -> `Simpl
245     | "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     (* TODO Reduce *)
300     | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
301         return_tactic loc TacticAst.Reflexivity
302     | [ IDENT "replace" | IDENT "Replace" ];
303       t1 = tactic_term; "with"; t2 = tactic_term ->
304         return_tactic loc (TacticAst.Replace (t1, t2))
305     (* TODO Rewrite *)
306     (* TODO Replace_pattern *)
307     | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
308     | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
309     | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
310     | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
311         return_tactic loc TacticAst.Symmetry
312     | [ IDENT "transitivity" | IDENT "Transitivity" ];
313       t = tactic_term ->
314         return_tactic loc (TacticAst.Transitivity t)
315     ]
316   ];
317   tactical0: [ [ t = tactical; SYMBOL ";;" -> t ] ];
318   tactical:
319     [ "command" NONA
320       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
321     | "sequence" LEFTA
322       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
323           return_tactical loc (TacticAst.Seq tactics)
324       ]
325     | "then" NONA
326       [ tac = tactical;
327         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
328           return_tactical loc (TacticAst.Then (tac, tacs))
329       ]
330     | "loops" RIGHTA
331       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
332           return_tactical loc (TacticAst.Do (count, tac))
333       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
334           return_tactical loc (TacticAst.Repeat tac)
335       ]
336     | "simple" NONA
337       [ [ IDENT "tries" | IDENT "Tries" ];
338         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
339           return_tactical loc (TacticAst.Tries tacs)
340       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
341           return_tactical loc (TacticAst.Try tac)
342       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
343       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
344       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
345       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
346       ]
347     ];
348   theorem_flavour: [  (* all flavours but Goal *)
349     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
350     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
351     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
352     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
353     | [ IDENT "theorem"     | IDENT "Theorem"     ] -> `Theorem
354     ]
355   ];
356   command: [
357     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
358     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
359     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
360     | [ IDENT "qed"   | IDENT "Qed"   ] ->
361         return_command loc (TacticAst.Qed None)
362     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
363         return_command loc (TacticAst.Qed (Some name))
364     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
365       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
366         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
367     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
368       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
369         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
370     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
371         return_command loc (TacticAst.Undo (int_opt steps))
372     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
373         return_command loc (TacticAst.Redo (int_opt steps))
374     | [ IDENT "check"   | IDENT "Check" ]; t = term ->
375         return_command loc (TacticAst.Check t)
376     ]
377   ];
378 END
379
380 let exc_located_wrapper f =
381   try
382     Lazy.force f
383   with Stdpp.Exc_located (floc, exn) ->
384     let (x, y) = CicAst.loc_of_floc floc in
385     raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
386         (Printexc.to_string exn)))
387
388 let parse_term stream =
389   exc_located_wrapper (lazy (Grammar.Entry.parse term0 stream))
390 let parse_tactic stream =
391   exc_located_wrapper (lazy (Grammar.Entry.parse tactic stream))
392 let parse_tactical stream =
393   exc_located_wrapper (lazy (Grammar.Entry.parse tactical0 stream))
394
395 (**/**)
396
397 (** {2 Interface for gTopLevel} *)
398
399 module EnvironmentP3 =
400   struct
401     type t = environment
402
403     let empty = ""
404
405     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
406     let aliases = Grammar.Entry.create aliases_grammar "aliases"
407
408     let to_string env =
409       let aliases =
410         Environment.fold
411           (fun domain_item (dsc, _) acc ->
412             let s =
413               match domain_item with
414               | Id id -> sprintf "alias id %s = %s" id dsc
415               | Symbol (symb, instance) ->
416                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
417                     symb instance dsc
418               | Num instance ->
419                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
420             in
421             s :: acc)
422           env []
423       in
424       String.concat "\n" (List.sort compare aliases)
425
426     EXTEND
427       GLOBAL: aliases;
428       aliases: [  (* build an environment from an aliases list *)
429         [ aliases = LIST0 alias; EOI ->
430             List.fold_left
431               (fun env (domain_item, codomain_item) ->
432                 Environment.add domain_item codomain_item env)
433               Environment.empty aliases
434         ]
435       ];
436       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
437         [ IDENT "alias";
438           choice =
439             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
440                 (Id id, choice_of_uri uri)
441             | IDENT "symbol"; symbol = QSTRING;
442               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
443               SYMBOL "="; dsc = QSTRING ->
444                 (Symbol (symbol, int_of_string instance),
445                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
446             | IDENT "num";
447               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
448               SYMBOL "="; dsc = QSTRING ->
449                 (Num (int_of_string instance),
450                  DisambiguateChoices.lookup_num_by_dsc dsc)
451             ] -> choice ]
452       ];
453     END
454
455     let of_string s =
456       if s = empty then
457         Environment.empty
458       else
459         try
460           Grammar.Entry.parse aliases (Stream.of_string s)
461         with Stdpp.Exc_located (floc, exn) ->
462           let (x, y) = CicAst.loc_of_floc floc in
463           raise (Parse_error (sprintf "parse error at characters %d-%d: %s" x y
464           (Printexc.to_string exn)))
465   end
466
467 (* vim:set encoding=utf8: *)