]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
paths trough terms implemented with a nice hack :)
[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 = false
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   (** does the lexer return COMMENT tokens? *)
39 let return_comments = false
40
41 open Printf
42
43 open DisambiguateTypes
44
45 exception Parse_error of Token.flocation * string
46
47 let cic_lexer = CicTextualLexer2.cic_lexer ~comments:return_comments ()
48
49 let fresh_num_instance =
50   let n = ref 0 in
51   if use_fresh_num_instances then
52     (fun () -> incr n; !n)
53   else
54     (fun () -> 0)
55
56 let choice_of_uri uri =
57   let term = CicUtil.term_of_uri uri in
58   (uri, (fun _ _ _ -> term))
59
60 let grammar = Grammar.gcreate cic_lexer
61
62 let term = Grammar.Entry.create grammar "term"
63 let term0 = Grammar.Entry.create grammar "term0"
64 let tactic = Grammar.Entry.create grammar "tactic"
65 let tactical = Grammar.Entry.create grammar "tactical"
66 let tactical0 = Grammar.Entry.create grammar "tactical0"
67 let command = Grammar.Entry.create grammar "command"
68 let alias_spec = Grammar.Entry.create grammar "alias_spec"
69 let macro = Grammar.Entry.create grammar "macro"
70 let script = Grammar.Entry.create grammar "script"
71 let statement = Grammar.Entry.create grammar "statement"
72 let statements = Grammar.Entry.create grammar "statements"
73
74 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
75
76 let fail floc msg =
77   let (x, y) = CicAst.loc_of_floc floc in
78   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
79
80 let name_of_string = function
81   | "_" -> Cic.Anonymous
82   | s -> Cic.Name s
83
84 let string_of_name = function
85   | Cic.Anonymous -> "_"
86   | Cic.Name s -> s
87   
88 let int_opt = function
89   | None -> None
90   | Some lexeme -> Some (int_of_string lexeme)
91
92 let int_of_string s =
93   try
94     Pervasives.int_of_string s
95   with Failure _ ->
96     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
97
98   (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
99   * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
100   * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
101   * type in a block of mutual inductive types.
102   *
103   * This function performs the expansion foo.ind -> foo#xpointer..., if needed
104   *)
105 let ind_expansion uri =
106   let len = String.length uri in
107   if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
108     uri ^ "#xpointer(1/1)"
109   else
110     uri
111
112 let mk_binder_ast binder typ vars body =
113   List.fold_right
114     (fun var body ->
115        let name = name_of_string var in
116        CicAst.Binder (binder, (name, typ), body))
117     vars body
118
119 EXTEND
120   GLOBAL: term term0 statement statements;
121   int: [
122     [ num = NUM ->
123         try
124           int_of_string num
125         with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
126     ]
127   ];
128   meta_subst: [
129     [ s = SYMBOL "_" -> None
130     | t = term -> Some t ]
131   ];
132   binder_low: [
133     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
134     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
135     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
136   ];
137   binder_high: [ [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda ] ];
138   sort: [
139     [ "Prop" -> `Prop
140     | "Set" -> `Set
141     | "Type" -> `Type
142     | "CProp" -> `CProp ]
143   ];
144   typed_name: [
145     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
146         (Cic.Name i, Some typ)
147     | i = IDENT -> (Cic.Name i, None)
148     ]
149   ];
150   subst: [
151     [ SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
152       PAREN "[";
153       substs = LIST1 [
154         i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
155       ] SEP SYMBOL ";";
156       PAREN "]" ->
157         substs
158     ]
159   ];
160   substituted_name: [ (* a subs.name is an explicit substitution subject *)
161     [ s = IDENT; subst = OPT subst -> CicAst.Ident (s, subst)
162     | s = URI; subst = OPT subst -> CicAst.Uri (ind_expansion s, subst)
163     ]
164   ];
165   name: [ (* as substituted_name with no explicit substitution *)
166     [ s = [ IDENT | SYMBOL ] -> s ]
167   ];
168   pattern: [
169     [ n = name -> (n, [])
170     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
171         (head, vars)
172     ]
173   ];
174   let_defs:[
175     [ defs = LIST1 [
176         name = IDENT;
177         args = LIST1 [
178           PAREN "(" ; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
179           ty = term; PAREN ")" ->
180             (names, ty)
181         ];
182         index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
183         ty = OPT [ SYMBOL ":" ; t = term -> t ];
184         SYMBOL <:unicode<def>> (* ≝ *);
185         t1 = term ->
186           let rec list_of_binder binder ty final_term = function
187             | [] -> final_term
188             | name::tl -> 
189                 CicAst.Binder (binder, (Cic.Name name, Some ty), 
190                   list_of_binder binder ty final_term tl)
191           in
192           let rec binder_of_arg_list binder final_term = function
193             | [] -> final_term
194             | (l,ty)::tl ->  
195                 list_of_binder binder ty 
196                   (binder_of_arg_list binder final_term tl) l
197           in
198           let t1' = binder_of_arg_list `Lambda t1 args in
199           let ty' = 
200             match ty with 
201             | None -> None
202             | Some ty -> Some (binder_of_arg_list `Pi ty args)
203           in
204           let rec get_position_of name n = function 
205             | [] -> (None,n)
206             | nam::tl -> 
207                 if nam = name then 
208                   (Some n,n) 
209                 else 
210                   (get_position_of name (n+1) tl)
211           in
212           let rec find_arg name n = function 
213             | [] -> (fail loc (sprintf "Argument %s not found" name))
214             | (l,_)::tl -> 
215                 let (got,len) = get_position_of name 0 l in
216                 (match got with 
217                 | None -> (find_arg name (n+len) tl)
218                 | Some where -> n + where)
219           in
220           let index = 
221             (match index_name with 
222              | None -> 0 
223              | (Some name) -> find_arg name 0 args)
224           in
225           ((Cic.Name name,ty'), t1', index)
226       ] SEP "and" -> defs
227     ]];
228   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
229   binder_vars: [
230       [ vars = LIST1 IDENT SEP SYMBOL ",";
231         typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
232       | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
233         typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
234       ]
235   ];
236   term0: [ [ t = term; EOI -> return_term loc t ] ];
237   term:
238     [ "letin" NONA
239       [ "let"; var = typed_name;
240         SYMBOL <:unicode<def>> (* ≝ *);
241         t1 = term; "in"; t2 = term ->
242           return_term loc (CicAst.LetIn (var, t1, t2))
243       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
244         defs = let_defs; "in"; body = term ->
245             return_term loc (CicAst.LetRec (ind_kind, defs, body))
246       ]
247     | "binder" RIGHTA
248       [
249         b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
250           let binder = mk_binder_ast b typ vars body in
251           return_term loc binder
252       | b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
253           let binder = mk_binder_ast b typ vars body in
254           return_term loc binder
255       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
256           return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
257       ]
258     | "logic_add" LEFTA   [ (* nothing here by default *) ]
259     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
260     | "logic_inv" NONA    [ (* nothing here by default *) ]
261     | "relop" LEFTA
262       [ t1 = term; SYMBOL "="; t2 = term ->
263         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
264       ]
265     | "add" LEFTA     [ (* nothing here by default *) ]
266     | "mult" LEFTA    [ (* nothing here by default *) ]
267     | "power" LEFTA   [ (* nothing here by default *) ]
268     | "inv" NONA      [ (* nothing here by default *) ]
269     | "apply" LEFTA
270       [ t1 = term; t2 = term ->
271         let rec aux = function
272           | CicAst.Appl (hd :: tl) -> aux hd @ tl
273           | term -> [term]
274         in
275         CicAst.Appl (aux t1 @ [t2])
276       ]
277     | "simple" NONA
278       [ sort = sort -> CicAst.Sort sort
279       | n = substituted_name -> return_term loc n
280       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
281       | IMPLICIT -> return_term loc CicAst.Implicit
282       | PLACEHOLDER -> return_term loc CicAst.UserInput
283       | m = META;
284         substs = [
285           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
286             substs
287         ] ->
288             let index =
289               try
290                 int_of_string (String.sub m 1 (String.length m - 1))
291               with Failure "int_of_string" ->
292                 fail loc ("Invalid meta variable number: " ^ m)
293             in
294             return_term loc (CicAst.Meta (index, substs))
295       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
296         "match"; t = term;
297         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
298         "with";
299         PAREN "[";
300         patterns = LIST0 [
301           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
302           ->
303             ((lhs: CicAst.case_pattern), rhs)
304         ] SEP SYMBOL "|";
305         PAREN "]" ->
306           return_term loc
307             (CicAst.Case (t, indty_ident, outtyp, patterns))
308       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
309           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
310       | PAREN "("; t = term; PAREN ")" -> return_term loc t
311       ]
312     ];
313   tactic_where: [
314     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
315   ];
316   tactic_term: [ [ t = term -> t ] ];
317   ident_list0: [
318     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
319   ];
320   ident_list1: [
321     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
322   ];
323   reduction_kind: [
324     [ [ IDENT "reduce" ] -> `Reduce
325     | [ IDENT "simplify" ] -> `Simpl
326     | [ IDENT "whd" ] -> `Whd 
327     | [ IDENT "normalize" ] -> `Normalize ]
328   ];
329   tactic: [
330     [ [ IDENT "absurd" ]; t = tactic_term ->
331         TacticAst.Absurd (loc, t)
332     | [ IDENT "apply" ]; t = tactic_term ->
333         TacticAst.Apply (loc, t)
334     | [ IDENT "assumption" ] ->
335         TacticAst.Assumption loc
336     | [ IDENT "auto" ] ; num = OPT [ i = NUM -> int_of_string i ] -> 
337           TacticAst.Auto (loc,num)
338     | [ IDENT "change" ];
339       t1 = tactic_term; "with"; t2 = tactic_term;
340       where = tactic_where ->
341         TacticAst.Change (loc, t1, t2, where)
342     (* TODO Change_pattern *)
343     | [ IDENT "contradiction" ] ->
344         TacticAst.Contradiction loc
345     | [ IDENT "cut" ];
346       t = tactic_term ->
347         TacticAst.Cut (loc, t)
348     | [ IDENT "decompose" ];
349       principles = ident_list1; where = IDENT ->
350         TacticAst.Decompose (loc, where, principles)
351     | [ IDENT "discriminate" ];
352       hyp = IDENT ->
353         TacticAst.Discriminate (loc, hyp)
354     | [ IDENT "elimType" ]; t = tactic_term ->
355         TacticAst.ElimType (loc, t)
356     | [ IDENT "elim" ];
357       t1 = tactic_term;
358       using = OPT [ "using"; using = tactic_term -> using ] ->
359         TacticAst.Elim (loc, t1, using)
360     | [ IDENT "exact" ]; t = tactic_term ->
361         TacticAst.Exact (loc, t)
362     | [ IDENT "exists" ] ->
363         TacticAst.Exists loc
364     | [ IDENT "fold" ];
365       kind = reduction_kind; t = tactic_term ->
366         TacticAst.Fold (loc, kind, t)
367     | [ IDENT "fourier" ] ->
368         TacticAst.Fourier loc
369     | IDENT "goal"; n = NUM -> TacticAst.Goal (loc, int_of_string n)
370     | [ IDENT "injection" ]; ident = IDENT ->
371         TacticAst.Injection (loc, ident)
372     | [ IDENT "intros" ];
373       num = OPT [ num = int -> num ];
374       idents = OPT ident_list0 ->
375         let idents = match idents with None -> [] | Some idents -> idents in
376         TacticAst.Intros (loc, num, idents)
377     | [ IDENT "intro" ] ->
378         TacticAst.Intros (loc, Some 1, [])
379     | [ IDENT "left" ] -> TacticAst.Left loc
380     | [ "let" | "Let" ];
381       t = tactic_term; "in"; where = IDENT ->
382         TacticAst.LetIn (loc, t, where)
383     | kind = reduction_kind;
384       pat = OPT [
385         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
386           pat
387       ];
388       terms = LIST0 term SEP SYMBOL "," ->
389         (match (pat, terms) with
390         | None, [] -> TacticAst.Reduce (loc, kind, None)
391         | None, terms -> TacticAst.Reduce (loc, kind, Some (terms, `Goal))
392         | Some pat, [] -> fail loc "Missing term [list]"
393         | Some pat, terms -> TacticAst.Reduce (loc, kind, Some (terms, pat)))
394     | kind = reduction_kind; where = IDENT ; IDENT "at" ; pat = term ->
395         TacticAst.ReduceAt (loc, kind, where, pat)
396     | [ IDENT "reflexivity" ] ->
397         TacticAst.Reflexivity loc
398     | [ IDENT "replace" ];
399       t1 = tactic_term; "with"; t2 = tactic_term ->
400         TacticAst.Replace (loc, t1, t2)
401     | [ IDENT "rewrite" ; IDENT "left" ] ; t = term ->
402         TacticAst.Rewrite (loc,`Left, t, None)
403     | [ IDENT "rewrite" ; IDENT "right" ] ; t = term ->
404         TacticAst.Rewrite (loc,`Right, t, None)
405     (* TODO Replace_pattern *)
406     | [ IDENT "right" ] -> TacticAst.Right loc
407     | [ IDENT "ring" ] -> TacticAst.Ring loc
408     | [ IDENT "split" ] -> TacticAst.Split loc
409     | [ IDENT "symmetry" ] ->
410         TacticAst.Symmetry loc
411     | [ IDENT "transitivity" ];
412       t = tactic_term ->
413         TacticAst.Transitivity (loc, t)
414     ]
415   ];
416   tactical:
417     [ "sequence" LEFTA
418       [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
419           TacticAst.Seq (loc, tacticals)
420       ]
421     | "then" NONA
422       [ tac = tactical;
423         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
424           (TacticAst.Then (loc, tac, tacs))
425       ]
426     | "loops" RIGHTA
427       [ [ IDENT "do" ]; count = int; tac = tactical ->
428           TacticAst.Do (loc, count, tac)
429       | [ IDENT "repeat" ]; tac = tactical ->
430           TacticAst.Repeat (loc, tac)
431       ]
432     | "simple" NONA
433       [ IDENT "tries";
434         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
435           TacticAst.Tries (loc, tacs)
436       | IDENT "try"; tac = NEXT ->
437           TacticAst.Try (loc, tac)
438       | IDENT "fail" -> TacticAst.Fail loc
439       | IDENT "id" -> TacticAst.IdTac loc
440       | PAREN "("; tac = tactical; PAREN ")" -> tac
441       | tac = tactic -> TacticAst.Tactic (loc, tac)
442       ]
443     ];
444   theorem_flavour: [
445     [ [ IDENT "definition"  ] -> `Definition
446     | [ IDENT "fact"        ] -> `Fact
447     | [ IDENT "lemma"       ] -> `Lemma
448     | [ IDENT "remark"      ] -> `Remark
449     | [ IDENT "theorem"     ] -> `Theorem
450     ]
451   ];
452   inductive_spec: [ [
453     fst_name = IDENT; params = LIST0 [
454       PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
455       typ = term; PAREN ")" -> (names, typ) ];
456     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
457     fst_constructors = LIST0 constructor SEP SYMBOL "|";
458     tl = OPT [ "with";
459       types = LIST1 [
460         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
461        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
462           (name, true, typ, constructors) ] SEP "with" -> types
463     ] ->
464       let params =
465         List.fold_right
466           (fun (names, typ) acc ->
467             (List.map (fun name -> (name, typ)) names) @ acc)
468           params []
469       in
470       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
471       let tl_ind_types = match tl with None -> [] | Some types -> types in
472       let ind_types = fst_ind_type :: tl_ind_types in
473       (params, ind_types)
474   ] ];
475
476   macro: [
477     [ [ IDENT "quit"  ] -> TacticAst.Quit loc
478 (*     | [ IDENT "abort" ] -> TacticAst.Abort loc *)
479     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
480 (*     | [ IDENT "undo"   ]; steps = OPT NUM ->
481         TacticAst.Undo (loc, int_opt steps)
482     | [ IDENT "redo"   ]; steps = OPT NUM ->
483         TacticAst.Redo (loc, int_opt steps) *)
484     | [ IDENT "check"   ]; t = term ->
485         TacticAst.Check (loc, t)
486     | [ IDENT "hint" ] -> TacticAst.Hint loc
487     | [ IDENT "whelp"; "match" ] ; t = term -> 
488         TacticAst.WMatch (loc,t)
489     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
490         TacticAst.WInstance (loc,t)
491     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
492         TacticAst.WLocate (loc,id)
493     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
494         TacticAst.WElim (loc, t)
495     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
496         TacticAst.WHint (loc,t)
497     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
498     ]
499   ];
500
501   alias_spec: [
502     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
503       let alpha = "[a-zA-Z]" in
504       let num = "[0-9]+" in
505       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
506       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
507       let rex = Str.regexp ("^"^ident^"$") in
508       if Str.string_match rex id 0 then
509         let rex = Str.regexp 
510           ("^\\(cic:/\\|theory:/\\)"^ident^
511            "\\(/"^ident^"+\\)*\\(\\."^ident^"\\)+"^
512            "\\(#xpointer("^ num^"\\(/"^num^"\\)+)\\)?$") 
513         in
514         if Str.string_match rex uri 0 then
515           TacticAst.Ident_alias (id, uri)
516         else 
517           raise (Parse_error (loc,sprintf "Not a valid uri: %s" uri))
518       else
519         raise (Parse_error (loc,sprintf "Not a valid identifier: %s" id))
520     | IDENT "symbol"; symbol = QSTRING;
521       instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
522       SYMBOL "="; dsc = QSTRING ->
523         let instance =
524           match instance with Some i -> int_of_string i | None -> 0
525         in
526         TacticAst.Symbol_alias (symbol, instance, dsc)
527     | IDENT "num";
528       instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
529       SYMBOL "="; dsc = QSTRING ->
530         let instance =
531           match instance with Some i -> int_of_string i | None -> 0
532         in
533         TacticAst.Number_alias (instance, dsc)
534     ]
535   ];
536   
537   command: [[
538       [ IDENT "set"    ]; n = QSTRING; v = QSTRING ->
539         TacticAst.Set (loc, n, v)
540     | [ IDENT "qed"   ] -> TacticAst.Qed loc
541     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
542       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
543         TacticAst.Theorem (loc, flavour, name, typ, body)
544     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
545         defs = let_defs -> 
546           let name,ty = 
547             match defs with
548             | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
549             | ((Cic.Name name,None),_,_) :: _ -> 
550                 fail loc ("No type given for " ^ name)
551             | _ -> assert false 
552           in
553           let body = CicAst.Ident (name,None) in
554           TacticAst.Theorem(loc, `Definition, Some name, ty,
555             Some (CicAst.LetRec (ind_kind, defs, body)))
556           
557     | [ IDENT "inductive" ]; spec = inductive_spec ->
558         let (params, ind_types) = spec in
559         TacticAst.Inductive (loc, params, ind_types)
560     | [ IDENT "coinductive" ]; spec = inductive_spec ->
561         let (params, ind_types) = spec in
562         let ind_types = (* set inductive flags to false (coinductive) *)
563           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
564             ind_types
565         in
566         TacticAst.Inductive (loc, params, ind_types)
567     | [ IDENT "coercion" ] ; name = IDENT -> 
568         TacticAst.Coercion (loc, CicAst.Ident (name,Some []))
569     | [ IDENT "coercion" ] ; name = URI -> 
570         TacticAst.Coercion (loc, CicAst.Uri (name,Some []))
571     | [ IDENT "alias"   ]; spec = alias_spec ->
572         TacticAst.Alias (loc, spec)
573   ]];
574
575   executable: [
576     [ cmd = command; SYMBOL "." -> TacticAst.Command (loc, cmd)
577     | tac = tactical; SYMBOL "." -> TacticAst.Tactical (loc, tac)
578     | mac = macro; SYMBOL "." -> TacticAst.Macro (loc, mac)
579     ]
580   ];
581   
582   comment: [
583     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
584        TacticAst.Code (loc, ex)
585     | str = NOTE -> 
586        TacticAst.Note (loc, str)
587     ]
588   ];
589   
590   statement: [
591     [ ex = executable -> TacticAst.Executable (loc,ex)
592     | com = comment -> TacticAst.Comment (loc, com)
593     ]
594   ];
595   statements: [
596     [ l = LIST0 [ statement ] -> l 
597     ]  
598   ];
599 END
600
601 let exc_located_wrapper f =
602   try
603     f ()
604   with
605   | Stdpp.Exc_located (floc, Stream.Error msg) ->
606       raise (Parse_error (floc, msg))
607   | Stdpp.Exc_located (floc, exn) ->
608       raise (Parse_error (floc, (Printexc.to_string exn)))
609
610 let parse_term stream =
611   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
612 let parse_statement stream =
613   exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
614 let parse_statements stream =
615   exc_located_wrapper (fun () -> (Grammar.Entry.parse statements stream))
616   
617
618 (**/**)
619
620 (** {2 Interface for gTopLevel} *)
621
622 module EnvironmentP3 =
623   struct
624     type t = environment
625
626     let empty = ""
627
628     let aliases_grammar = Grammar.gcreate cic_lexer
629     let aliases = Grammar.Entry.create aliases_grammar "aliases"
630
631     let to_string env =
632       let aliases =
633         Environment.fold
634           (fun domain_item (dsc, _) acc ->
635             let s =
636               match domain_item with
637               | Id id ->
638                   TacticAstPp.pp_alias (TacticAst.Ident_alias (id, dsc)) ^ "."
639               | Symbol (symb, i) ->
640                   TacticAstPp.pp_alias (TacticAst.Symbol_alias (symb, i, dsc))
641                   ^ "."
642               | Num i ->
643                   TacticAstPp.pp_alias (TacticAst.Number_alias (i, dsc)) ^ "."
644             in
645             s :: acc)
646           env []
647       in
648       String.concat "\n" (List.sort compare aliases)
649
650     EXTEND
651       GLOBAL: aliases;
652       aliases: [  (* build an environment from an aliases list *)
653         [ aliases = LIST0 alias; EOI ->
654             List.fold_left
655               (fun env (domain_item, codomain_item) ->
656                 Environment.add domain_item codomain_item env)
657               Environment.empty aliases
658         ]
659       ];
660       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
661         [ IDENT "alias";
662           choice =
663             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
664                 (Id id, choice_of_uri uri)
665             | IDENT "symbol"; symbol = QSTRING;
666               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
667               SYMBOL "="; dsc = QSTRING ->
668                 (Symbol (symbol, int_of_string instance),
669                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
670             | IDENT "num";
671               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
672               SYMBOL "="; dsc = QSTRING ->
673                 (Num (int_of_string instance),
674                  DisambiguateChoices.lookup_num_by_dsc dsc)
675             ] -> choice ]
676       ];
677     END
678
679     let of_string s =
680       if s = empty then
681         Environment.empty
682       else
683         exc_located_wrapper
684           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
685   end
686
687 (* vim:set encoding=utf8: *)