]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
The type of a top-level "let rec" can be optional.
[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 ",";
179             SYMBOL ":"; ty = term; PAREN ")" ->
180               (names, Some ty)
181           | name = IDENT -> [name],None
182         ];
183         index_name = OPT [ IDENT "on"; idx = IDENT -> idx ];
184         ty = OPT [ SYMBOL ":" ; t = term -> t ];
185         SYMBOL <:unicode<def>> (* ≝ *);
186         t1 = term ->
187           let rec list_of_binder binder ty final_term = function
188             | [] -> final_term
189             | name::tl -> 
190                 CicAst.Binder (binder, (Cic.Name name, Some ty), 
191                   list_of_binder binder ty final_term tl)
192           in
193           let rec binder_of_arg_list binder final_term = function
194             | [] -> final_term
195             | (l,ty)::tl ->  
196                 list_of_binder binder ty 
197                   (binder_of_arg_list binder final_term tl) l
198           in
199           let args = 
200            List.map
201             (function
202                 names,Some ty -> names,ty
203               | names,None -> names,CicAst.Implicit
204             ) args in
205           let t1' = binder_of_arg_list `Lambda t1 args in
206           let ty' = 
207             match ty with 
208             | None -> None
209             | Some ty -> Some (binder_of_arg_list `Pi ty args)
210           in
211           let rec get_position_of name n = function 
212             | [] -> (None,n)
213             | nam::tl -> 
214                 if nam = name then 
215                   (Some n,n) 
216                 else 
217                   (get_position_of name (n+1) tl)
218           in
219           let rec find_arg name n = function 
220             | [] -> (fail loc (sprintf "Argument %s not found" name))
221             | (l,_)::tl -> 
222                 let (got,len) = get_position_of name 0 l in
223                 (match got with 
224                 | None -> (find_arg name (n+len) tl)
225                 | Some where -> n + where)
226           in
227           let index = 
228             (match index_name with 
229              | None -> 0 
230              | (Some name) -> find_arg name 0 args)
231           in
232           ((Cic.Name name,ty'), t1', index)
233       ] SEP "and" -> defs
234     ]];
235   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
236   binder_vars: [
237       [ vars = LIST1 IDENT SEP SYMBOL ",";
238         typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
239       | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
240         typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
241       ]
242   ];
243   term0: [ [ t = term; EOI -> return_term loc t ] ];
244   term:
245     [ "letin" NONA
246       [ "let"; var = typed_name;
247         SYMBOL <:unicode<def>> (* ≝ *);
248         t1 = term; "in"; t2 = term ->
249           return_term loc (CicAst.LetIn (var, t1, t2))
250       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
251         defs = let_defs; "in"; body = term ->
252             return_term loc (CicAst.LetRec (ind_kind, defs, body))
253       ]
254     | "binder" RIGHTA
255       [
256         b = binder_low; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
257           let binder = mk_binder_ast b typ vars body in
258           return_term loc binder
259       | b = binder_high; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
260           let binder = mk_binder_ast b typ vars body in
261           return_term loc binder
262       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
263           return_term loc (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
264       ]
265     | "logic_add" LEFTA   [ (* nothing here by default *) ]
266     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
267     | "logic_inv" NONA    [ (* nothing here by default *) ]
268     | "relop" LEFTA
269       [ t1 = term; SYMBOL "="; t2 = term ->
270         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
271       ]
272     | "add" LEFTA     [ (* nothing here by default *) ]
273     | "mult" LEFTA    [ (* nothing here by default *) ]
274     | "power" LEFTA   [ (* nothing here by default *) ]
275     | "inv" NONA      [ (* nothing here by default *) ]
276     | "apply" LEFTA
277       [ t1 = term; t2 = term ->
278         let rec aux = function
279           | CicAst.Appl (hd :: tl) -> aux hd @ tl
280           | term -> [term]
281         in
282         CicAst.Appl (aux t1 @ [t2])
283       ]
284     | "simple" NONA
285       [ sort = sort -> CicAst.Sort sort
286       | n = substituted_name -> return_term loc n
287       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
288       | IMPLICIT -> return_term loc CicAst.Implicit
289       | PLACEHOLDER -> return_term loc CicAst.UserInput
290       | m = META;
291         substs = [
292           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
293             substs
294         ] ->
295             let index =
296               try
297                 int_of_string (String.sub m 1 (String.length m - 1))
298               with Failure "int_of_string" ->
299                 fail loc ("Invalid meta variable number: " ^ m)
300             in
301             return_term loc (CicAst.Meta (index, substs))
302       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
303         "match"; t = term;
304         indty_ident = OPT ["in" ; id = IDENT -> id ];
305         "with";
306         PAREN "[";
307         patterns = LIST0 [
308           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term
309           ->
310             ((lhs: CicAst.case_pattern), rhs)
311         ] SEP SYMBOL "|";
312         PAREN "]" ->
313           return_term loc
314             (CicAst.Case (t, indty_ident, outtyp, patterns))
315       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
316           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
317       | PAREN "("; t = term; PAREN ")" -> return_term loc t
318       ]
319     ];
320   tactic_where: [
321     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
322   ];
323   tactic_term: [ [ t = term -> t ] ];
324   ident_list0: [
325     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
326   ];
327   ident_list1: [
328     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
329   ];
330   reduction_kind: [
331     [ [ IDENT "reduce" ] -> `Reduce
332     | [ IDENT "simplify" ] -> `Simpl
333     | [ IDENT "whd" ] -> `Whd 
334     | [ IDENT "normalize" ] -> `Normalize ]
335   ];
336   tactic: [
337     [ [ IDENT "absurd" ]; t = tactic_term ->
338         TacticAst.Absurd (loc, t)
339     | [ IDENT "apply" ]; t = tactic_term ->
340         TacticAst.Apply (loc, t)
341     | [ IDENT "assumption" ] ->
342         TacticAst.Assumption loc
343     | [ IDENT "auto" ] ; num = OPT [ i = NUM -> int_of_string i ] -> 
344           TacticAst.Auto (loc,num)
345     | [ IDENT "change" ];
346       t1 = tactic_term; "with"; t2 = tactic_term;
347       where = tactic_where ->
348         TacticAst.Change (loc, t1, t2, where)
349     (* TODO Change_pattern *)
350     | [ IDENT "contradiction" ] ->
351         TacticAst.Contradiction loc
352     | [ IDENT "cut" ];
353       t = tactic_term ->
354         TacticAst.Cut (loc, t)
355     | [ IDENT "decompose" ];
356       principles = ident_list1; where = IDENT ->
357         TacticAst.Decompose (loc, where, principles)
358     | [ IDENT "discriminate" ];
359       hyp = IDENT ->
360         TacticAst.Discriminate (loc, hyp)
361     | [ IDENT "elimType" ]; t = tactic_term ->
362         TacticAst.ElimType (loc, t)
363     | [ IDENT "elim" ];
364       t1 = tactic_term;
365       using = OPT [ "using"; using = tactic_term -> using ] ->
366         TacticAst.Elim (loc, t1, using)
367     | [ IDENT "exact" ]; t = tactic_term ->
368         TacticAst.Exact (loc, t)
369     | [ IDENT "exists" ] ->
370         TacticAst.Exists loc
371     | [ IDENT "fold" ];
372       kind = reduction_kind; t = tactic_term ->
373         TacticAst.Fold (loc, kind, t)
374     | [ IDENT "fourier" ] ->
375         TacticAst.Fourier loc
376     | IDENT "goal"; n = NUM -> TacticAst.Goal (loc, int_of_string n)
377     | [ IDENT "injection" ]; ident = IDENT ->
378         TacticAst.Injection (loc, ident)
379     | [ IDENT "intros" ];
380       num = OPT [ num = int -> num ];
381       idents = OPT ident_list0 ->
382         let idents = match idents with None -> [] | Some idents -> idents in
383         TacticAst.Intros (loc, num, idents)
384     | [ IDENT "intro" ] ->
385         TacticAst.Intros (loc, Some 1, [])
386     | [ IDENT "left" ] -> TacticAst.Left loc
387     | [ IDENT "letin" ];
388        where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
389         TacticAst.LetIn (loc, t, where)
390     | kind = reduction_kind;
391       pat = OPT [
392         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
393           pat
394       ];
395       terms = LIST0 term SEP SYMBOL "," ->
396         (match (pat, terms) with
397         | None, [] -> TacticAst.Reduce (loc, kind, None)
398         | None, terms -> TacticAst.Reduce (loc, kind, Some (terms, `Goal))
399         | Some pat, [] -> fail loc "Missing term [list]"
400         | Some pat, terms -> TacticAst.Reduce (loc, kind, Some (terms, pat)))
401     | kind = reduction_kind; where = IDENT ; IDENT "at" ; pat = term ->
402         TacticAst.ReduceAt (loc, kind, where, pat)
403     | [ IDENT "reflexivity" ] ->
404         TacticAst.Reflexivity loc
405     | [ IDENT "replace" ];
406       t1 = tactic_term; "with"; t2 = tactic_term ->
407         TacticAst.Replace (loc, t1, t2)
408     | [ IDENT "rewrite" ; IDENT "left" ] ; t = term ->
409         TacticAst.Rewrite (loc,`Left, t, None)
410     | [ IDENT "rewrite" ; IDENT "right" ] ; t = term ->
411         TacticAst.Rewrite (loc,`Right, t, None)
412     (* TODO Replace_pattern *)
413     | [ IDENT "right" ] -> TacticAst.Right loc
414     | [ IDENT "ring" ] -> TacticAst.Ring loc
415     | [ IDENT "split" ] -> TacticAst.Split loc
416     | [ IDENT "symmetry" ] ->
417         TacticAst.Symmetry loc
418     | [ IDENT "transitivity" ];
419       t = tactic_term ->
420         TacticAst.Transitivity (loc, t)
421     ]
422   ];
423   tactical:
424     [ "sequence" LEFTA
425       [ tacticals = LIST1 NEXT SEP SYMBOL ";" ->
426           TacticAst.Seq (loc, tacticals)
427       ]
428     | "then" NONA
429       [ tac = tactical;
430         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
431           (TacticAst.Then (loc, tac, tacs))
432       ]
433     | "loops" RIGHTA
434       [ [ IDENT "do" ]; count = int; tac = tactical ->
435           TacticAst.Do (loc, count, tac)
436       | [ IDENT "repeat" ]; tac = tactical ->
437           TacticAst.Repeat (loc, tac)
438       ]
439     | "simple" NONA
440       [ IDENT "tries";
441         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
442           TacticAst.Tries (loc, tacs)
443       | IDENT "try"; tac = NEXT ->
444           TacticAst.Try (loc, tac)
445       | IDENT "fail" -> TacticAst.Fail loc
446       | IDENT "id" -> TacticAst.IdTac loc
447       | PAREN "("; tac = tactical; PAREN ")" -> tac
448       | tac = tactic -> TacticAst.Tactic (loc, tac)
449       ]
450     ];
451   theorem_flavour: [
452     [ [ IDENT "definition"  ] -> `Definition
453     | [ IDENT "fact"        ] -> `Fact
454     | [ IDENT "lemma"       ] -> `Lemma
455     | [ IDENT "remark"      ] -> `Remark
456     | [ IDENT "theorem"     ] -> `Theorem
457     ]
458   ];
459   inductive_spec: [ [
460     fst_name = IDENT; params = LIST0 [
461       PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
462       typ = term; PAREN ")" -> (names, typ) ];
463     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
464     fst_constructors = LIST0 constructor SEP SYMBOL "|";
465     tl = OPT [ "with";
466       types = LIST1 [
467         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
468        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
469           (name, true, typ, constructors) ] SEP "with" -> types
470     ] ->
471       let params =
472         List.fold_right
473           (fun (names, typ) acc ->
474             (List.map (fun name -> (name, typ)) names) @ acc)
475           params []
476       in
477       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
478       let tl_ind_types = match tl with None -> [] | Some types -> types in
479       let ind_types = fst_ind_type :: tl_ind_types in
480       (params, ind_types)
481   ] ];
482
483   macro: [
484     [ [ IDENT "quit"  ] -> TacticAst.Quit loc
485 (*     | [ IDENT "abort" ] -> TacticAst.Abort loc *)
486     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
487 (*     | [ IDENT "undo"   ]; steps = OPT NUM ->
488         TacticAst.Undo (loc, int_opt steps)
489     | [ IDENT "redo"   ]; steps = OPT NUM ->
490         TacticAst.Redo (loc, int_opt steps) *)
491     | [ IDENT "check"   ]; t = term ->
492         TacticAst.Check (loc, t)
493     | [ IDENT "hint" ] -> TacticAst.Hint loc
494     | [ IDENT "whelp"; "match" ] ; t = term -> 
495         TacticAst.WMatch (loc,t)
496     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
497         TacticAst.WInstance (loc,t)
498     | [ IDENT "whelp"; IDENT "locate" ] ; id = IDENT -> 
499         TacticAst.WLocate (loc,id)
500     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
501         TacticAst.WElim (loc, t)
502     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
503         TacticAst.WHint (loc,t)
504     | [ IDENT "print" ]; name = QSTRING -> TacticAst.Print (loc, name)
505     ]
506   ];
507
508   alias_spec: [
509     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
510       let alpha = "[a-zA-Z]" in
511       let num = "[0-9]+" in
512       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
513       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
514       let rex = Str.regexp ("^"^ident^"$") in
515       if Str.string_match rex id 0 then
516         let rex = Str.regexp 
517           ("^\\(cic:/\\|theory:/\\)"^ident^
518            "\\(/"^ident^"+\\)*\\(\\."^ident^"\\)+"^
519            "\\(#xpointer("^ num^"\\(/"^num^"\\)+)\\)?$") 
520         in
521         if Str.string_match rex uri 0 then
522           TacticAst.Ident_alias (id, uri)
523         else 
524           raise (Parse_error (loc,sprintf "Not a valid uri: %s" uri))
525       else
526         raise (Parse_error (loc,sprintf "Not a valid identifier: %s" id))
527     | IDENT "symbol"; symbol = QSTRING;
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.Symbol_alias (symbol, instance, dsc)
534     | IDENT "num";
535       instance = OPT [ PAREN "("; IDENT "instance"; n = NUM; PAREN ")" -> n ];
536       SYMBOL "="; dsc = QSTRING ->
537         let instance =
538           match instance with Some i -> int_of_string i | None -> 0
539         in
540         TacticAst.Number_alias (instance, dsc)
541     ]
542   ];
543   
544   command: [[
545       [ IDENT "set"    ]; n = QSTRING; v = QSTRING ->
546         TacticAst.Set (loc, n, v)
547     | [ IDENT "qed"   ] -> TacticAst.Qed loc
548     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
549       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
550         TacticAst.Theorem (loc, flavour, name, typ, body)
551     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
552         defs = let_defs -> 
553           let name,ty = 
554             match defs with
555             | ((Cic.Name name,Some ty),_,_) :: _ -> name,ty
556             | ((Cic.Name name,None),_,_) :: _ -> name,CicAst.Implicit
557             | _ -> assert false 
558           in
559           let body = CicAst.Ident (name,None) in
560           TacticAst.Theorem(loc, `Definition, Some name, ty,
561             Some (CicAst.LetRec (ind_kind, defs, body)))
562           
563     | [ IDENT "inductive" ]; spec = inductive_spec ->
564         let (params, ind_types) = spec in
565         TacticAst.Inductive (loc, params, ind_types)
566     | [ IDENT "coinductive" ]; spec = inductive_spec ->
567         let (params, ind_types) = spec in
568         let ind_types = (* set inductive flags to false (coinductive) *)
569           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
570             ind_types
571         in
572         TacticAst.Inductive (loc, params, ind_types)
573     | [ IDENT "coercion" ] ; name = IDENT -> 
574         TacticAst.Coercion (loc, CicAst.Ident (name,Some []))
575     | [ IDENT "coercion" ] ; name = URI -> 
576         TacticAst.Coercion (loc, CicAst.Uri (name,Some []))
577     | [ IDENT "alias"   ]; spec = alias_spec ->
578         TacticAst.Alias (loc, spec)
579   ]];
580
581   executable: [
582     [ cmd = command; SYMBOL "." -> TacticAst.Command (loc, cmd)
583     | tac = tactical; SYMBOL "." -> TacticAst.Tactical (loc, tac)
584     | mac = macro; SYMBOL "." -> TacticAst.Macro (loc, mac)
585     ]
586   ];
587   
588   comment: [
589     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
590        TacticAst.Code (loc, ex)
591     | str = NOTE -> 
592        TacticAst.Note (loc, str)
593     ]
594   ];
595   
596   statement: [
597     [ ex = executable -> TacticAst.Executable (loc,ex)
598     | com = comment -> TacticAst.Comment (loc, com)
599     ]
600   ];
601   statements: [
602     [ l = LIST0 [ statement ] -> l 
603     ]  
604   ];
605 END
606
607 let exc_located_wrapper f =
608   try
609     f ()
610   with
611   | Stdpp.Exc_located (floc, Stream.Error msg) ->
612       raise (Parse_error (floc, msg))
613   | Stdpp.Exc_located (floc, exn) ->
614       raise (Parse_error (floc, (Printexc.to_string exn)))
615
616 let parse_term stream =
617   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
618 let parse_statement stream =
619   exc_located_wrapper (fun () -> (Grammar.Entry.parse statement stream))
620 let parse_statements stream =
621   exc_located_wrapper (fun () -> (Grammar.Entry.parse statements stream))
622   
623
624 (**/**)
625
626 (** {2 Interface for gTopLevel} *)
627
628 module EnvironmentP3 =
629   struct
630     type t = environment
631
632     let empty = ""
633
634     let aliases_grammar = Grammar.gcreate cic_lexer
635     let aliases = Grammar.Entry.create aliases_grammar "aliases"
636
637     let to_string env =
638       let aliases =
639         Environment.fold
640           (fun domain_item (dsc, _) acc ->
641             let s =
642               match domain_item with
643               | Id id ->
644                   TacticAstPp.pp_alias (TacticAst.Ident_alias (id, dsc)) ^ "."
645               | Symbol (symb, i) ->
646                   TacticAstPp.pp_alias (TacticAst.Symbol_alias (symb, i, dsc))
647                   ^ "."
648               | Num i ->
649                   TacticAstPp.pp_alias (TacticAst.Number_alias (i, dsc)) ^ "."
650             in
651             s :: acc)
652           env []
653       in
654       String.concat "\n" (List.sort compare aliases)
655
656     EXTEND
657       GLOBAL: aliases;
658       aliases: [  (* build an environment from an aliases list *)
659         [ aliases = LIST0 alias; EOI ->
660             List.fold_left
661               (fun env (domain_item, codomain_item) ->
662                 Environment.add domain_item codomain_item env)
663               Environment.empty aliases
664         ]
665       ];
666       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
667         [ IDENT "alias";
668           choice =
669             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
670                 (Id id, choice_of_uri uri)
671             | IDENT "symbol"; symbol = QSTRING;
672               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
673               SYMBOL "="; dsc = QSTRING ->
674                 (Symbol (symbol, int_of_string instance),
675                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
676             | IDENT "num";
677               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
678               SYMBOL "="; dsc = QSTRING ->
679                 (Num (int_of_string instance),
680                  DisambiguateChoices.lookup_num_by_dsc dsc)
681             ] -> choice ]
682       ];
683     END
684
685     let of_string s =
686       if s = empty then
687         Environment.empty
688       else
689         exc_located_wrapper
690           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
691   end
692
693 (* vim:set encoding=utf8: *)