]> matita.cs.unibo.it Git - helm.git/blob - components/grafite_parser/grafiteParser.ml
The auto parameters that can be given to a declarative rewritingstep must now
[helm.git] / components / grafite_parser / grafiteParser.ml
1 (* Copyright (C) 2005, 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 (* $Id$ *)
27
28 open Printf
29
30 module Ast = CicNotationPt
31
32 type 'a localized_option =
33    LSome of 'a
34  | LNone of GrafiteAst.loc
35
36 type ast_statement =
37   (CicNotationPt.term, CicNotationPt.term,
38    CicNotationPt.term GrafiteAst.reduction, CicNotationPt.obj, string)
39     GrafiteAst.statement
40
41 type statement =
42   include_paths:string list ->
43   LexiconEngine.status ->
44     LexiconEngine.status * ast_statement localized_option
45
46 let grammar = CicNotationParser.level2_ast_grammar
47
48 let term = CicNotationParser.term
49 let statement = Grammar.Entry.create grammar "statement"
50
51 let add_raw_attribute ~text t = Ast.AttributedTerm (`Raw text, t)
52
53 let default_precedence = 50
54 let default_associativity = Gramext.NonA
55
56 type by_continuation =
57    BYC_done
58  | BYC_weproved of CicNotationPt.term * string option * CicNotationPt.term option
59  | BYC_letsuchthat of string * CicNotationPt.term * string * CicNotationPt.term
60  | BYC_wehaveand of string * CicNotationPt.term * string * CicNotationPt.term
61
62 EXTEND
63   GLOBAL: term statement;
64   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
65   tactic_term: [ [ t = term LEVEL "90N" -> t ] ];
66   ident_list0: [ [ LPAREN; idents = LIST0 IDENT; RPAREN -> idents ] ];
67   tactic_term_list1: [
68     [ tactic_terms = LIST1 tactic_term SEP SYMBOL "," -> tactic_terms ]
69   ];
70   reduction_kind: [
71     [ IDENT "normalize" -> `Normalize
72     | IDENT "reduce" -> `Reduce
73     | IDENT "simplify" -> `Simpl
74     | IDENT "unfold"; t = OPT tactic_term -> `Unfold t
75     | IDENT "whd" -> `Whd ]
76   ];
77   sequent_pattern_spec: [
78    [ hyp_paths =
79       LIST0
80        [ id = IDENT ;
81          path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
82          (id,match path with Some p -> p | None -> Ast.UserInput) ];
83      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
84       let goal_path =
85        match goal_path, hyp_paths with
86           None, [] -> Some Ast.UserInput
87         | None, _::_ -> None
88         | Some goal_path, _ -> Some goal_path
89       in
90        hyp_paths,goal_path
91    ]
92   ];
93   pattern_spec: [
94     [ res = OPT [
95        "in";
96        wanted_and_sps =
97         [ "match" ; wanted = tactic_term ;
98           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
99            Some wanted,sps
100         | sps = sequent_pattern_spec ->
101            None,Some sps
102         ] ->
103          let wanted,hyp_paths,goal_path =
104           match wanted_and_sps with
105              wanted,None -> wanted, [], Some Ast.UserInput
106            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
107          in
108           wanted, hyp_paths, goal_path ] ->
109       match res with
110          None -> None,[],Some Ast.UserInput
111        | Some ps -> ps]
112   ];
113   direction: [
114     [ SYMBOL ">" -> `LeftToRight
115     | SYMBOL "<" -> `RightToLeft ]
116   ];
117   int: [ [ num = NUMBER -> int_of_string num ] ];
118   intros_spec: [
119     [ num = OPT [ num = int -> num ]; idents = OPT ident_list0 ->
120         let idents = match idents with None -> [] | Some idents -> idents in
121         num, idents
122     ]
123   ];
124   using: [ [ using = OPT [ IDENT "using"; t = tactic_term -> t ] -> using ] ];
125   tactic: [
126     [ IDENT "absurd"; t = tactic_term ->
127         GrafiteAst.Absurd (loc, t)
128     | IDENT "apply"; t = tactic_term ->
129         GrafiteAst.Apply (loc, t)
130     | IDENT "applyS"; t = tactic_term ; params = 
131         LIST0 [ i = IDENT -> i,"" | i = IDENT ; SYMBOL "="; v = [ v = int ->
132           string_of_int v | v = IDENT -> v ] -> i,v ] ->
133         GrafiteAst.ApplyS (loc, t, params)
134     | IDENT "assumption" ->
135         GrafiteAst.Assumption loc
136     | IDENT "auto"; params = auto_params ->
137         GrafiteAst.Auto (loc,params)
138     | IDENT "clear"; ids = LIST1 IDENT ->
139         GrafiteAst.Clear (loc, ids)
140     | IDENT "clearbody"; id = IDENT ->
141         GrafiteAst.ClearBody (loc,id)
142     | IDENT "change"; what = pattern_spec; "with"; t = tactic_term ->
143         GrafiteAst.Change (loc, what, t)
144     | IDENT "constructor"; n = int ->
145         GrafiteAst.Constructor (loc, n)
146     | IDENT "contradiction" ->
147         GrafiteAst.Contradiction loc
148     | IDENT "cut"; t = tactic_term; ident = OPT [ "as"; id = IDENT -> id] ->
149         GrafiteAst.Cut (loc, ident, t)
150     | IDENT "decompose"; types = OPT ident_list0; what = OPT IDENT;
151         idents = OPT [ "as"; idents = LIST1 IDENT -> idents ] ->
152         let types = match types with None -> [] | Some types -> types in
153         let idents = match idents with None -> [] | Some idents -> idents in
154         let to_spec id = GrafiteAst.Ident id in
155         GrafiteAst.Decompose (loc, List.rev_map to_spec types, what, idents)
156     | IDENT "demodulate" -> GrafiteAst.Demodulate loc
157     | IDENT "destruct"; t = tactic_term ->
158         GrafiteAst.Destruct (loc, t)
159     | IDENT "elim"; what = tactic_term; using = using;
160       (num, idents) = intros_spec ->
161         GrafiteAst.Elim (loc, what, using, num, idents)
162     | IDENT "elimType"; what = tactic_term; using = using;
163       (num, idents) = intros_spec ->
164         GrafiteAst.ElimType (loc, what, using, num, idents)
165     | IDENT "exact"; t = tactic_term ->
166         GrafiteAst.Exact (loc, t)
167     | IDENT "exists" ->
168         GrafiteAst.Exists loc
169     | IDENT "fail" -> GrafiteAst.Fail loc
170     | IDENT "fold"; kind = reduction_kind; t = tactic_term; p = pattern_spec ->
171         let (pt,_,_) = p in
172           if pt <> None then
173             raise (HExtlib.Localized (loc, CicNotationParser.Parse_error
174               ("the pattern cannot specify the term to replace, only its"
175               ^ " paths in the hypotheses and in the conclusion")))
176         else
177          GrafiteAst.Fold (loc, kind, t, p)
178     | IDENT "fourier" ->
179         GrafiteAst.Fourier loc
180     | IDENT "fwd"; hyp = IDENT; idents = OPT [ "as"; idents = LIST1 IDENT -> idents ] ->
181         let idents = match idents with None -> [] | Some idents -> idents in
182         GrafiteAst.FwdSimpl (loc, hyp, idents)
183     | IDENT "generalize"; p=pattern_spec; id = OPT ["as" ; id = IDENT -> id] ->
184        GrafiteAst.Generalize (loc,p,id)
185     | IDENT "goal"; n = int ->
186         GrafiteAst.Goal (loc, n)
187     | IDENT "id" -> GrafiteAst.IdTac loc
188     | IDENT "intro"; ident = OPT IDENT ->
189         let idents = match ident with None -> [] | Some id -> [id] in
190         GrafiteAst.Intros (loc, Some 1, idents)
191     | IDENT "intros"; (num, idents) = intros_spec ->
192         GrafiteAst.Intros (loc, num, idents)
193     | IDENT "inversion"; t = tactic_term ->
194         GrafiteAst.Inversion (loc, t)
195     | IDENT "lapply"; 
196       linear = OPT [ IDENT "linear" ];
197       depth = OPT [ IDENT "depth"; SYMBOL "="; i = int -> i ];
198       what = tactic_term; 
199       to_what = OPT [ "to" ; t = tactic_term_list1 -> t ];
200       ident = OPT [ "as" ; ident = IDENT -> ident ] ->
201         let linear = match linear with None -> false | Some _ -> true in 
202         let to_what = match to_what with None -> [] | Some to_what -> to_what in
203         GrafiteAst.LApply (loc, linear, depth, to_what, what, ident)
204     | IDENT "left" -> GrafiteAst.Left loc
205     | IDENT "letin"; where = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term ->
206         GrafiteAst.LetIn (loc, t, where)
207     | kind = reduction_kind; p = pattern_spec ->
208         GrafiteAst.Reduce (loc, kind, p)
209     | IDENT "reflexivity" ->
210         GrafiteAst.Reflexivity loc
211     | IDENT "replace"; p = pattern_spec; "with"; t = tactic_term ->
212         GrafiteAst.Replace (loc, p, t)
213     | IDENT "rewrite" ; d = direction; t = tactic_term ; p = pattern_spec ->
214        let (pt,_,_) = p in
215         if pt <> None then
216          raise
217           (HExtlib.Localized (loc,
218            (CicNotationParser.Parse_error
219             "the pattern cannot specify the term to rewrite, only its paths in the hypotheses and in the conclusion")))
220         else
221          GrafiteAst.Rewrite (loc, d, t, p)
222     | IDENT "right" ->
223         GrafiteAst.Right loc
224     | IDENT "ring" ->
225         GrafiteAst.Ring loc
226     | IDENT "split" ->
227         GrafiteAst.Split loc
228     | IDENT "subst" ->
229         GrafiteAst.Subst loc    
230     | IDENT "symmetry" ->
231         GrafiteAst.Symmetry loc
232     | IDENT "transitivity"; t = tactic_term ->
233         GrafiteAst.Transitivity (loc, t)
234      (* Produzioni Aggiunte *)
235     | IDENT "assume" ; id = IDENT ; SYMBOL ":" ; t = tactic_term ->
236         GrafiteAst.Assume (loc, id, t)
237     | IDENT "suppose" ; t = tactic_term ; LPAREN ; id = IDENT ; RPAREN ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t' = tactic_term -> t']->
238         GrafiteAst.Suppose (loc, t, id, t1)
239     | IDENT "by" ; t = [t' = tactic_term -> LSome t' | SYMBOL "_" -> LNone loc];
240       cont=by_continuation ->
241        let t' = match t with LNone _ -> None | LSome t -> Some t in
242        (match cont with
243            BYC_done -> GrafiteAst.Bydone (loc, t')
244          | BYC_weproved (ty,id,t1) ->
245             GrafiteAst.By_term_we_proved(loc, t', ty, id, t1)
246          | BYC_letsuchthat (id1,t1,id2,t2) ->
247             (match t with
248                 LNone floc ->
249                  raise (HExtlib.Localized
250                   (floc,CicNotationParser.Parse_error
251                     "tactic_term expected here"))
252               | LSome t -> GrafiteAst.ExistsElim (loc, t, id1, t1, id2, t2))
253          | BYC_wehaveand (id1,t1,id2,t2) ->
254             (match t with
255                 LNone floc ->
256                  raise (HExtlib.Localized
257                   (floc,CicNotationParser.Parse_error
258                     "tactic_term expected here"))
259               | LSome t -> GrafiteAst.AndElim (loc, t, id1, t1, id2, t2)))
260     | IDENT "we" ; IDENT "need" ; "to" ; IDENT "prove" ; t = tactic_term ; id = OPT [ LPAREN ; id = IDENT ; RPAREN -> id ] ; t1 = OPT [IDENT "or" ; IDENT "equivalently"; t' = tactic_term -> t']->
261         GrafiteAst.We_need_to_prove (loc, t, id, t1)
262     | IDENT "we" ; IDENT "proceed" ; IDENT "by" ; IDENT "induction" ; "on" ; t=tactic_term ; "to" ; IDENT "prove" ; t1=tactic_term ->  
263         GrafiteAst.We_proceed_by_induction_on (loc, t, t1)
264     | IDENT "by" ; IDENT "induction" ; IDENT "hypothesis" ; IDENT "we" ; IDENT "know" ; t=tactic_term ; LPAREN ; id = IDENT ; RPAREN ->
265         GrafiteAst.Byinduction(loc, t, id)
266     | IDENT "the" ; IDENT "thesis" ; IDENT "becomes" ; t=tactic_term ->
267         GrafiteAst.Thesisbecomes(loc, t)
268     | IDENT "case" ; id = IDENT ; params=LIST0[LPAREN ; i=IDENT ;
269         SYMBOL":" ; t=tactic_term ; RPAREN -> i,t] ->
270          GrafiteAst.Case(loc,id,params)
271     | IDENT "obtain" ; termine=tactic_term ; SYMBOL "=" ; t1=tactic_term ; IDENT "by" ; t2=[ t=tactic_term -> `Term t | SYMBOL "_" ; params = auto_params' -> `Auto params  ] ; cont=rewriting_step_continuation ->
272      GrafiteAst.RewritingStep(loc, Some termine, t1, t2, cont)
273     | SYMBOL "=" ; t1=tactic_term ; IDENT "by" ; t2=[ t=tactic_term -> `Term t | SYMBOL "_" ; params = auto_params' -> `Auto params ] ;
274       cont=rewriting_step_continuation  ->
275      GrafiteAst.RewritingStep(loc, None, t1, t2, cont)
276   ]
277 ];
278   auto_params: [
279    [ params =
280       LIST0 [ i = IDENT -> i,"" | i = IDENT ; SYMBOL "="; v = [ v = int ->
281        string_of_int v | v = IDENT -> v ] -> i,v ] ->
282       params
283    ]
284 ];
285   auto_params': [
286    [ LPAREN; params = auto_params; RPAREN -> params
287    | -> []
288    ]
289 ];
290   by_continuation: [
291     [ IDENT "we" ; IDENT "proved" ; ty = tactic_term ; LPAREN ; id = IDENT ; RPAREN ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] -> BYC_weproved (ty,Some id,t1)
292     | IDENT "we" ; IDENT "proved" ; ty = tactic_term ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] ; 
293             IDENT "done" -> BYC_weproved (ty,None,t1)
294     | IDENT "done" -> BYC_done
295     | "let" ; id1 = IDENT ; SYMBOL ":" ; t1 = tactic_term ;
296       IDENT "such" ; IDENT "that" ; t2=tactic_term ; LPAREN ; id2 = IDENT ; RPAREN -> BYC_letsuchthat (id1,t1,id2,t2)
297     | IDENT "we" ; IDENT "have" ; t1=tactic_term ; LPAREN ; id1=IDENT ; RPAREN ;"and" ; t2=tactic_term ; LPAREN ; id2=IDENT ; RPAREN ->
298               BYC_wehaveand (id1,t1,id2,t2)
299     ]
300 ];
301   rewriting_step_continuation : [
302     [ IDENT "done" -> None
303     | IDENT "we" ; IDENT "proved" ; id=IDENT -> Some (Cic.Name id)
304     | -> Some Cic.Anonymous
305     ]
306 ];
307   atomic_tactical:
308     [ "sequence" LEFTA
309       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
310           let ts =
311             match t1 with
312             | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
313             | _ -> [ t1; t2 ]
314           in
315           GrafiteAst.Seq (loc, ts)
316       ]
317     | "then" NONA
318       [ tac = SELF; SYMBOL ";";
319         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
320           (GrafiteAst.Then (loc, tac, tacs))
321       ]
322     | "loops" RIGHTA
323       [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
324           GrafiteAst.Do (loc, count, tac)
325       | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
326       ]
327     | "simple" NONA
328       [ IDENT "first";
329         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
330           GrafiteAst.First (loc, tacs)
331       | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
332       | IDENT "solve";
333         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
334           GrafiteAst.Solve (loc, tacs)
335       | IDENT "progress"; tac = SELF -> GrafiteAst.Progress (loc, tac)
336       | LPAREN; tac = SELF; RPAREN -> tac
337       | tac = tactic -> GrafiteAst.Tactic (loc, tac)
338       ]
339     ];
340   punctuation_tactical:
341     [
342       [ SYMBOL "[" -> GrafiteAst.Branch loc
343       | SYMBOL "|" -> GrafiteAst.Shift loc
344       | i = LIST1 int SEP SYMBOL ","; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
345       | SYMBOL "*"; SYMBOL ":" -> GrafiteAst.Wildcard loc
346       | SYMBOL "]" -> GrafiteAst.Merge loc
347       | SYMBOL ";" -> GrafiteAst.Semicolon loc
348       | SYMBOL "." -> GrafiteAst.Dot loc
349       ]
350     ];
351   tactical:
352     [ "simple" NONA
353       [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
354       | IDENT "unfocus" -> GrafiteAst.Unfocus loc
355       | IDENT "skip" -> GrafiteAst.Skip loc
356       | tac = atomic_tactical LEVEL "loops" -> tac
357       ]
358     ];
359   theorem_flavour: [
360     [ [ IDENT "definition"  ] -> `Definition
361     | [ IDENT "fact"        ] -> `Fact
362     | [ IDENT "lemma"       ] -> `Lemma
363     | [ IDENT "remark"      ] -> `Remark
364     | [ IDENT "theorem"     ] -> `Theorem
365     ]
366   ];
367   inductive_spec: [ [
368     fst_name = IDENT; params = LIST0 CicNotationParser.protected_binder_vars;
369     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
370     fst_constructors = LIST0 constructor SEP SYMBOL "|";
371     tl = OPT [ "with";
372       types = LIST1 [
373         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
374        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
375           (name, true, typ, constructors) ] SEP "with" -> types
376     ] ->
377       let params =
378         List.fold_right
379           (fun (names, typ) acc ->
380             (List.map (fun name -> (name, typ)) names) @ acc)
381           params []
382       in
383       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
384       let tl_ind_types = match tl with None -> [] | Some types -> types in
385       let ind_types = fst_ind_type :: tl_ind_types in
386       (params, ind_types)
387   ] ];
388   
389   record_spec: [ [
390     name = IDENT; params = LIST0 CicNotationParser.protected_binder_vars ;
391      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
392      fields = LIST0 [ 
393        name = IDENT ; 
394        coercion = [ 
395            SYMBOL ":" -> false,0 
396          | SYMBOL ":"; SYMBOL ">" -> true,0
397          | SYMBOL ":"; arity = int ; SYMBOL ">" -> true,arity
398        ]; 
399        ty = term -> 
400          let b,n = coercion in 
401          (name,ty,b,n) 
402      ] SEP SYMBOL ";"; SYMBOL "}" -> 
403       let params =
404         List.fold_right
405           (fun (names, typ) acc ->
406             (List.map (fun name -> (name, typ)) names) @ acc)
407           params []
408       in
409       (params,name,typ,fields)
410   ] ];
411   
412   macro: [
413     [ [ IDENT "check"   ]; t = term ->
414         GrafiteAst.Check (loc, t)
415     | [ IDENT "inline"]; suri = QSTRING; prefix = OPT QSTRING ->
416          let prefix = match prefix with None -> "" | Some prefix -> prefix in
417          GrafiteAst.Inline (loc,suri,prefix)
418     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
419     | [ IDENT "whelp"; "match" ] ; t = term -> 
420         GrafiteAst.WMatch (loc,t)
421     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
422         GrafiteAst.WInstance (loc,t)
423     | [ IDENT "whelp"; IDENT "locate" ] ; id = QSTRING -> 
424         GrafiteAst.WLocate (loc,id)
425     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
426         GrafiteAst.WElim (loc, t)
427     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
428         GrafiteAst.WHint (loc,t)
429     ]
430   ];
431   alias_spec: [
432     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
433       let alpha = "[a-zA-Z]" in
434       let num = "[0-9]+" in
435       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
436       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
437       let rex = Str.regexp ("^"^ident^"$") in
438       if Str.string_match rex id 0 then
439         if (try ignore (UriManager.uri_of_string uri); true
440             with UriManager.IllFormedUri _ -> false)
441         then
442           LexiconAst.Ident_alias (id, uri)
443         else 
444           raise
445            (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
446       else
447         raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
448           sprintf "Not a valid identifier: %s" id)))
449     | IDENT "symbol"; symbol = QSTRING;
450       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
451       SYMBOL "="; dsc = QSTRING ->
452         let instance =
453           match instance with Some i -> i | None -> 0
454         in
455         LexiconAst.Symbol_alias (symbol, instance, dsc)
456     | IDENT "num";
457       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
458       SYMBOL "="; dsc = QSTRING ->
459         let instance =
460           match instance with Some i -> i | None -> 0
461         in
462         LexiconAst.Number_alias (instance, dsc)
463     ]
464   ];
465   argument: [
466     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
467       id = IDENT ->
468         Ast.IdentArg (List.length l, id)
469     ]
470   ];
471   associativity: [
472     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
473     | IDENT "right"; IDENT "associative" -> Gramext.RightA
474     | IDENT "non"; IDENT "associative" -> Gramext.NonA
475     ]
476   ];
477   precedence: [
478     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
479   ];
480   notation: [
481     [ dir = OPT direction; s = QSTRING;
482       assoc = OPT associativity; prec = OPT precedence;
483       IDENT "for";
484       p2 = 
485         [ blob = UNPARSED_AST ->
486             add_raw_attribute ~text:(sprintf "@{%s}" blob)
487               (CicNotationParser.parse_level2_ast
488                 (Ulexing.from_utf8_string blob))
489         | blob = UNPARSED_META ->
490             add_raw_attribute ~text:(sprintf "${%s}" blob)
491               (CicNotationParser.parse_level2_meta
492                 (Ulexing.from_utf8_string blob))
493         ] ->
494           let assoc =
495             match assoc with
496             | None -> default_associativity
497             | Some assoc -> assoc
498           in
499           let prec =
500             match prec with
501             | None -> default_precedence
502             | Some prec -> prec
503           in
504           let p1 =
505             add_raw_attribute ~text:s
506               (CicNotationParser.parse_level1_pattern
507                 (Ulexing.from_utf8_string s))
508           in
509           (dir, p1, assoc, prec, p2)
510     ]
511   ];
512   level3_term: [
513     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
514     | id = IDENT -> Ast.VarPattern id
515     | SYMBOL "_" -> Ast.ImplicitPattern
516     | LPAREN; terms = LIST1 SELF; RPAREN ->
517         (match terms with
518         | [] -> assert false
519         | [term] -> term
520         | terms -> Ast.ApplPattern terms)
521     ]
522   ];
523   interpretation: [
524     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
525         (s, args, t)
526     ]
527   ];
528   
529   include_command: [ [
530       IDENT "include" ; path = QSTRING -> 
531         loc,path,LexiconAst.WithPreferences
532     | IDENT "include'" ; path = QSTRING -> 
533         loc,path,LexiconAst.WithoutPreferences
534    ]];
535
536   grafite_command: [ [
537       IDENT "set"; n = QSTRING; v = QSTRING ->
538         GrafiteAst.Set (loc, n, v)
539     | IDENT "drop" -> GrafiteAst.Drop loc
540     | IDENT "print"; s = IDENT -> GrafiteAst.Print (loc,s)
541     | IDENT "qed" -> GrafiteAst.Qed loc
542     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
543       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
544         GrafiteAst.Obj (loc, 
545           Ast.Theorem 
546             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
547     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
548       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
549         GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
550     | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
551       body = term ->
552         GrafiteAst.Obj (loc,
553           Ast.Theorem (flavour, name, Ast.Implicit, Some body))
554     | IDENT "axiom"; name = IDENT; SYMBOL ":"; typ = term ->
555         GrafiteAst.Obj (loc, Ast.Theorem (`Axiom, name, typ, None))
556     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
557         defs = CicNotationParser.let_defs -> 
558           let name,ty = 
559             match defs with
560             | (params,(Ast.Ident (name, None), Some ty),_,_) :: _ ->
561                 let ty =
562                  List.fold_right
563                   (fun var ty -> Ast.Binder (`Pi,var,ty)
564                   ) params ty
565                 in
566                  name,ty
567             | (_,(Ast.Ident (name, None), None),_,_) :: _ ->
568                 name, Ast.Implicit
569             | _ -> assert false 
570           in
571           let body = Ast.Ident (name,None) in
572           GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
573             Some (Ast.LetRec (ind_kind, defs, body))))
574     | IDENT "inductive"; spec = inductive_spec ->
575         let (params, ind_types) = spec in
576         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
577     | IDENT "coinductive"; spec = inductive_spec ->
578         let (params, ind_types) = spec in
579         let ind_types = (* set inductive flags to false (coinductive) *)
580           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
581             ind_types
582         in
583         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
584     | IDENT "coercion" ; suri = URI ; arity = OPT int ->
585         let arity = match arity with None -> 0 | Some x -> x in
586         GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true, arity)
587     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
588         GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
589     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
590        let uris = List.map UriManager.uri_of_string uris in
591         GrafiteAst.Default (loc,what,uris)
592     | IDENT "relation" ; aeq = tactic_term ; "on" ; a = tactic_term ;
593       refl = OPT [ IDENT "reflexivity" ; IDENT "proved" ; IDENT "by" ;
594                    refl = tactic_term -> refl ] ;
595       sym = OPT [ IDENT "symmetry" ; IDENT "proved" ; IDENT "by" ;
596                    sym = tactic_term -> sym ] ;
597       trans = OPT [ IDENT "transitivity" ; IDENT "proved" ; IDENT "by" ;
598                    trans = tactic_term -> trans ] ;
599       "as" ; id = IDENT ->
600        GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans)
601   ]];
602   lexicon_command: [ [
603       IDENT "alias" ; spec = alias_spec ->
604         LexiconAst.Alias (loc, spec)
605     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
606         LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
607     | IDENT "interpretation"; id = QSTRING;
608       (symbol, args, l3) = interpretation ->
609         LexiconAst.Interpretation (loc, id, (symbol, args), l3)
610   ]];
611   executable: [
612     [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
613     | tac = tactical; punct = punctuation_tactical ->
614         GrafiteAst.Tactical (loc, tac, Some punct)
615     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
616     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
617     ]
618   ];
619   comment: [
620     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
621        GrafiteAst.Code (loc, ex)
622     | str = NOTE -> 
623        GrafiteAst.Note (loc, str)
624     ]
625   ];
626   statement: [
627     [ ex = executable ->
628        fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
629     | com = comment ->
630        fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
631     | (iloc,fname,mode) = include_command ; SYMBOL "."  ->
632        fun ~include_paths status ->
633         let buri, fullpath = 
634           DependenciesParser.baseuri_of_script ~include_paths fname 
635         in
636         let status =
637          LexiconEngine.eval_command status 
638            (LexiconAst.Include (iloc,buri,mode,fullpath))
639         in
640          status,
641           LSome
642           (GrafiteAst.Executable
643            (loc,GrafiteAst.Command
644             (loc,GrafiteAst.Include (iloc,buri))))
645     | scom = lexicon_command ; SYMBOL "." ->
646        fun ~include_paths status ->
647         let status = LexiconEngine.eval_command status scom in
648          status,LNone loc
649     | EOI -> raise End_of_file
650     ]
651   ];
652 END
653
654 let exc_located_wrapper f =
655   try
656     f ()
657   with
658   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
659   | Stdpp.Exc_located (floc, Stream.Error msg) ->
660       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
661   | Stdpp.Exc_located (floc, exn) ->
662       raise
663        (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
664
665 let parse_statement lexbuf =
666   exc_located_wrapper
667     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))