]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/grafite_parser/grafiteParser.ml
The rewritingstep declarative command now takes also a list of arguments
[helm.git] / helm / software / 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   by_continuation: [
286     [ 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)
287     | IDENT "we" ; IDENT "proved" ; ty = tactic_term ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] ; 
288             IDENT "done" -> BYC_weproved (ty,None,t1)
289     | IDENT "done" -> BYC_done
290     | "let" ; id1 = IDENT ; SYMBOL ":" ; t1 = tactic_term ;
291       IDENT "such" ; IDENT "that" ; t2=tactic_term ; LPAREN ; id2 = IDENT ; RPAREN -> BYC_letsuchthat (id1,t1,id2,t2)
292     | IDENT "we" ; IDENT "have" ; t1=tactic_term ; LPAREN ; id1=IDENT ; RPAREN ;"and" ; t2=tactic_term ; LPAREN ; id2=IDENT ; RPAREN ->
293               BYC_wehaveand (id1,t1,id2,t2)
294     ]
295 ];
296   rewriting_step_continuation : [
297     [ IDENT "done" -> None
298     | IDENT "we" ; IDENT "proved" ; id=IDENT -> Some (Cic.Name id)
299     | -> Some Cic.Anonymous
300     ]
301 ];
302   atomic_tactical:
303     [ "sequence" LEFTA
304       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
305           let ts =
306             match t1 with
307             | GrafiteAst.Seq (_, l) -> l @ [ t2 ]
308             | _ -> [ t1; t2 ]
309           in
310           GrafiteAst.Seq (loc, ts)
311       ]
312     | "then" NONA
313       [ tac = SELF; SYMBOL ";";
314         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
315           (GrafiteAst.Then (loc, tac, tacs))
316       ]
317     | "loops" RIGHTA
318       [ IDENT "do"; count = int; tac = SELF; IDENT "end" ->
319           GrafiteAst.Do (loc, count, tac)
320       | IDENT "repeat"; tac = SELF; IDENT "end" -> GrafiteAst.Repeat (loc, tac)
321       ]
322     | "simple" NONA
323       [ IDENT "first";
324         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
325           GrafiteAst.First (loc, tacs)
326       | IDENT "try"; tac = SELF -> GrafiteAst.Try (loc, tac)
327       | IDENT "solve";
328         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
329           GrafiteAst.Solve (loc, tacs)
330       | IDENT "progress"; tac = SELF -> GrafiteAst.Progress (loc, tac)
331       | LPAREN; tac = SELF; RPAREN -> tac
332       | tac = tactic -> GrafiteAst.Tactic (loc, tac)
333       ]
334     ];
335   punctuation_tactical:
336     [
337       [ SYMBOL "[" -> GrafiteAst.Branch loc
338       | SYMBOL "|" -> GrafiteAst.Shift loc
339       | i = LIST1 int SEP SYMBOL ","; SYMBOL ":" -> GrafiteAst.Pos (loc, i)
340       | SYMBOL "*"; SYMBOL ":" -> GrafiteAst.Wildcard loc
341       | SYMBOL "]" -> GrafiteAst.Merge loc
342       | SYMBOL ";" -> GrafiteAst.Semicolon loc
343       | SYMBOL "." -> GrafiteAst.Dot loc
344       ]
345     ];
346   tactical:
347     [ "simple" NONA
348       [ IDENT "focus"; goals = LIST1 int -> GrafiteAst.Focus (loc, goals)
349       | IDENT "unfocus" -> GrafiteAst.Unfocus loc
350       | IDENT "skip" -> GrafiteAst.Skip loc
351       | tac = atomic_tactical LEVEL "loops" -> tac
352       ]
353     ];
354   theorem_flavour: [
355     [ [ IDENT "definition"  ] -> `Definition
356     | [ IDENT "fact"        ] -> `Fact
357     | [ IDENT "lemma"       ] -> `Lemma
358     | [ IDENT "remark"      ] -> `Remark
359     | [ IDENT "theorem"     ] -> `Theorem
360     ]
361   ];
362   inductive_spec: [ [
363     fst_name = IDENT; params = LIST0 CicNotationParser.protected_binder_vars;
364     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
365     fst_constructors = LIST0 constructor SEP SYMBOL "|";
366     tl = OPT [ "with";
367       types = LIST1 [
368         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
369        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
370           (name, true, typ, constructors) ] SEP "with" -> types
371     ] ->
372       let params =
373         List.fold_right
374           (fun (names, typ) acc ->
375             (List.map (fun name -> (name, typ)) names) @ acc)
376           params []
377       in
378       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
379       let tl_ind_types = match tl with None -> [] | Some types -> types in
380       let ind_types = fst_ind_type :: tl_ind_types in
381       (params, ind_types)
382   ] ];
383   
384   record_spec: [ [
385     name = IDENT; params = LIST0 CicNotationParser.protected_binder_vars ;
386      SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
387      fields = LIST0 [ 
388        name = IDENT ; 
389        coercion = [ 
390            SYMBOL ":" -> false,0 
391          | SYMBOL ":"; SYMBOL ">" -> true,0
392          | SYMBOL ":"; arity = int ; SYMBOL ">" -> true,arity
393        ]; 
394        ty = term -> 
395          let b,n = coercion in 
396          (name,ty,b,n) 
397      ] SEP SYMBOL ";"; SYMBOL "}" -> 
398       let params =
399         List.fold_right
400           (fun (names, typ) acc ->
401             (List.map (fun name -> (name, typ)) names) @ acc)
402           params []
403       in
404       (params,name,typ,fields)
405   ] ];
406   
407   macro: [
408     [ [ IDENT "check"   ]; t = term ->
409         GrafiteAst.Check (loc, t)
410     | [ IDENT "inline"]; suri = QSTRING; prefix = OPT QSTRING ->
411          let prefix = match prefix with None -> "" | Some prefix -> prefix in
412          GrafiteAst.Inline (loc,suri,prefix)
413     | [ IDENT "hint" ] -> GrafiteAst.Hint loc
414     | [ IDENT "whelp"; "match" ] ; t = term -> 
415         GrafiteAst.WMatch (loc,t)
416     | [ IDENT "whelp"; IDENT "instance" ] ; t = term -> 
417         GrafiteAst.WInstance (loc,t)
418     | [ IDENT "whelp"; IDENT "locate" ] ; id = QSTRING -> 
419         GrafiteAst.WLocate (loc,id)
420     | [ IDENT "whelp"; IDENT "elim" ] ; t = term ->
421         GrafiteAst.WElim (loc, t)
422     | [ IDENT "whelp"; IDENT "hint" ] ; t = term -> 
423         GrafiteAst.WHint (loc,t)
424     ]
425   ];
426   alias_spec: [
427     [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
428       let alpha = "[a-zA-Z]" in
429       let num = "[0-9]+" in
430       let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
431       let ident = "\\("^alpha^ident_cont^"*\\|_"^ident_cont^"+\\)" in
432       let rex = Str.regexp ("^"^ident^"$") in
433       if Str.string_match rex id 0 then
434         if (try ignore (UriManager.uri_of_string uri); true
435             with UriManager.IllFormedUri _ -> false)
436         then
437           LexiconAst.Ident_alias (id, uri)
438         else 
439           raise
440            (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
441       else
442         raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
443           sprintf "Not a valid identifier: %s" id)))
444     | IDENT "symbol"; symbol = QSTRING;
445       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
446       SYMBOL "="; dsc = QSTRING ->
447         let instance =
448           match instance with Some i -> i | None -> 0
449         in
450         LexiconAst.Symbol_alias (symbol, instance, dsc)
451     | IDENT "num";
452       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
453       SYMBOL "="; dsc = QSTRING ->
454         let instance =
455           match instance with Some i -> i | None -> 0
456         in
457         LexiconAst.Number_alias (instance, dsc)
458     ]
459   ];
460   argument: [
461     [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
462       id = IDENT ->
463         Ast.IdentArg (List.length l, id)
464     ]
465   ];
466   associativity: [
467     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
468     | IDENT "right"; IDENT "associative" -> Gramext.RightA
469     | IDENT "non"; IDENT "associative" -> Gramext.NonA
470     ]
471   ];
472   precedence: [
473     [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
474   ];
475   notation: [
476     [ dir = OPT direction; s = QSTRING;
477       assoc = OPT associativity; prec = OPT precedence;
478       IDENT "for";
479       p2 = 
480         [ blob = UNPARSED_AST ->
481             add_raw_attribute ~text:(sprintf "@{%s}" blob)
482               (CicNotationParser.parse_level2_ast
483                 (Ulexing.from_utf8_string blob))
484         | blob = UNPARSED_META ->
485             add_raw_attribute ~text:(sprintf "${%s}" blob)
486               (CicNotationParser.parse_level2_meta
487                 (Ulexing.from_utf8_string blob))
488         ] ->
489           let assoc =
490             match assoc with
491             | None -> default_associativity
492             | Some assoc -> assoc
493           in
494           let prec =
495             match prec with
496             | None -> default_precedence
497             | Some prec -> prec
498           in
499           let p1 =
500             add_raw_attribute ~text:s
501               (CicNotationParser.parse_level1_pattern
502                 (Ulexing.from_utf8_string s))
503           in
504           (dir, p1, assoc, prec, p2)
505     ]
506   ];
507   level3_term: [
508     [ u = URI -> Ast.UriPattern (UriManager.uri_of_string u)
509     | id = IDENT -> Ast.VarPattern id
510     | SYMBOL "_" -> Ast.ImplicitPattern
511     | LPAREN; terms = LIST1 SELF; RPAREN ->
512         (match terms with
513         | [] -> assert false
514         | [term] -> term
515         | terms -> Ast.ApplPattern terms)
516     ]
517   ];
518   interpretation: [
519     [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
520         (s, args, t)
521     ]
522   ];
523   
524   include_command: [ [
525       IDENT "include" ; path = QSTRING -> 
526         loc,path,LexiconAst.WithPreferences
527     | IDENT "include'" ; path = QSTRING -> 
528         loc,path,LexiconAst.WithoutPreferences
529    ]];
530
531   grafite_command: [ [
532       IDENT "set"; n = QSTRING; v = QSTRING ->
533         GrafiteAst.Set (loc, n, v)
534     | IDENT "drop" -> GrafiteAst.Drop loc
535     | IDENT "print"; s = IDENT -> GrafiteAst.Print (loc,s)
536     | IDENT "qed" -> GrafiteAst.Qed loc
537     | IDENT "variant" ; name = IDENT; SYMBOL ":"; 
538       typ = term; SYMBOL <:unicode<def>> ; newname = IDENT ->
539         GrafiteAst.Obj (loc, 
540           Ast.Theorem 
541             (`Variant,name,typ,Some (Ast.Ident (newname, None))))
542     | flavour = theorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
543       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
544         GrafiteAst.Obj (loc, Ast.Theorem (flavour, name, typ, body))
545     | flavour = theorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
546       body = term ->
547         GrafiteAst.Obj (loc,
548           Ast.Theorem (flavour, name, Ast.Implicit, Some body))
549     | IDENT "axiom"; name = IDENT; SYMBOL ":"; typ = term ->
550         GrafiteAst.Obj (loc, Ast.Theorem (`Axiom, name, typ, None))
551     | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
552         defs = CicNotationParser.let_defs -> 
553           let name,ty = 
554             match defs with
555             | (params,(Ast.Ident (name, None), Some ty),_,_) :: _ ->
556                 let ty =
557                  List.fold_right
558                   (fun var ty -> Ast.Binder (`Pi,var,ty)
559                   ) params ty
560                 in
561                  name,ty
562             | (_,(Ast.Ident (name, None), None),_,_) :: _ ->
563                 name, Ast.Implicit
564             | _ -> assert false 
565           in
566           let body = Ast.Ident (name,None) in
567           GrafiteAst.Obj (loc, Ast.Theorem(`Definition, name, ty,
568             Some (Ast.LetRec (ind_kind, defs, body))))
569     | IDENT "inductive"; spec = inductive_spec ->
570         let (params, ind_types) = spec in
571         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
572     | IDENT "coinductive"; spec = inductive_spec ->
573         let (params, ind_types) = spec in
574         let ind_types = (* set inductive flags to false (coinductive) *)
575           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
576             ind_types
577         in
578         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
579     | IDENT "coercion" ; suri = URI ; arity = OPT int ->
580         let arity = match arity with None -> 0 | Some x -> x in
581         GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true, arity)
582     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
583         GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
584     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
585        let uris = List.map UriManager.uri_of_string uris in
586         GrafiteAst.Default (loc,what,uris)
587     | IDENT "relation" ; aeq = tactic_term ; "on" ; a = tactic_term ;
588       refl = OPT [ IDENT "reflexivity" ; IDENT "proved" ; IDENT "by" ;
589                    refl = tactic_term -> refl ] ;
590       sym = OPT [ IDENT "symmetry" ; IDENT "proved" ; IDENT "by" ;
591                    sym = tactic_term -> sym ] ;
592       trans = OPT [ IDENT "transitivity" ; IDENT "proved" ; IDENT "by" ;
593                    trans = tactic_term -> trans ] ;
594       "as" ; id = IDENT ->
595        GrafiteAst.Relation (loc,id,a,aeq,refl,sym,trans)
596   ]];
597   lexicon_command: [ [
598       IDENT "alias" ; spec = alias_spec ->
599         LexiconAst.Alias (loc, spec)
600     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
601         LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
602     | IDENT "interpretation"; id = QSTRING;
603       (symbol, args, l3) = interpretation ->
604         LexiconAst.Interpretation (loc, id, (symbol, args), l3)
605   ]];
606   executable: [
607     [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
608     | tac = tactical; punct = punctuation_tactical ->
609         GrafiteAst.Tactical (loc, tac, Some punct)
610     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
611     | mac = macro; SYMBOL "." -> GrafiteAst.Macro (loc, mac)
612     ]
613   ];
614   comment: [
615     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
616        GrafiteAst.Code (loc, ex)
617     | str = NOTE -> 
618        GrafiteAst.Note (loc, str)
619     ]
620   ];
621   statement: [
622     [ ex = executable ->
623        fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
624     | com = comment ->
625        fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
626     | (iloc,fname,mode) = include_command ; SYMBOL "."  ->
627        fun ~include_paths status ->
628         let buri, fullpath = 
629           DependenciesParser.baseuri_of_script ~include_paths fname 
630         in
631         let status =
632          LexiconEngine.eval_command status 
633            (LexiconAst.Include (iloc,buri,mode,fullpath))
634         in
635          status,
636           LSome
637           (GrafiteAst.Executable
638            (loc,GrafiteAst.Command
639             (loc,GrafiteAst.Include (iloc,buri))))
640     | scom = lexicon_command ; SYMBOL "." ->
641        fun ~include_paths status ->
642         let status = LexiconEngine.eval_command status scom in
643          status,LNone loc
644     | EOI -> raise End_of_file
645     ]
646   ];
647 END
648
649 let exc_located_wrapper f =
650   try
651     f ()
652   with
653   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
654   | Stdpp.Exc_located (floc, Stream.Error msg) ->
655       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
656   | Stdpp.Exc_located (floc, exn) ->
657       raise
658        (HExtlib.Localized (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
659
660 let parse_statement lexbuf =
661   exc_located_wrapper
662     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))