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