]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/grafite_parser/grafiteParser.ml
7985512d938db4f14b773107530bacbe516f93c3
[helm.git] / matitaB / 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 module N  = NotationPt
29 module G  = GrafiteAst
30
31 let exc_located_wrapper f =
32   try
33     f ()
34   with
35   | Stdpp.Exc_located (_, End_of_file) -> raise End_of_file
36   | Stdpp.Exc_located (floc, Stream.Error msg) ->
37       raise (HExtlib.Localized (floc,CicNotationParser.Parse_error msg))
38   | Stdpp.Exc_located (floc, HExtlib.Localized(_,exn)) ->
39       raise (HExtlib.Localized 
40         (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
41   | Stdpp.Exc_located (floc, exn) ->
42       raise (HExtlib.Localized 
43         (floc,CicNotationParser.Parse_error (Printexc.to_string exn)))
44
45 type parsable = Grammar.parsable
46
47 let parsable_statement status buf =
48  let grammar = CicNotationParser.level2_ast_grammar status in
49   Grammar.parsable grammar (Obj.magic buf)
50
51 let parse_statement grafite_parser parsable =
52   exc_located_wrapper
53     (fun () -> (Grammar.Entry.parse_parsable (Obj.magic grafite_parser) parsable))
54
55 let add_raw_attribute ~text t = N.AttributedTerm (`Raw text, t)
56
57 let default_associativity = Gramext.NonA
58         
59 let mk_rec_corec ind_kind defs loc = 
60   let name,ty = 
61     match defs with
62     | (params,(N.Ident (name, `Ambiguous), ty),_,_) :: _ ->
63         let ty = match ty with Some ty -> ty | None -> N.Implicit `JustOne in
64         let ty =
65          List.fold_right
66           (fun var ty -> N.Binder (`Pi,var,ty)
67           ) params ty
68         in
69          name,ty
70     | _ -> assert false 
71   in
72   let body = N.Ident (name,`Ambiguous) in
73    (loc, N.Theorem(`Definition, name, ty, Some (N.LetRec (ind_kind, defs, body)), `Regular))
74
75 let nmk_rec_corec ind_kind defs loc index = 
76  let loc,t = mk_rec_corec ind_kind defs loc in
77   G.NObj (loc,t,index)
78
79 (*
80 let nnon_punct_of_punct = function
81   | G.Skip loc -> G.NSkip loc
82   | G.Unfocus loc -> G.NUnfocus loc
83   | G.Focus (loc,l) -> G.NFocus (loc,l)
84 ;; *)
85
86 type by_continuation =
87    BYC_done
88  | BYC_weproved of N.term * string option * N.term option
89  | BYC_letsuchthat of string * N.term * string * N.term
90  | BYC_wehaveand of string * N.term * string * N.term
91
92 let mk_parser statement lstatus =
93 (*   let grammar = CicNotationParser.level2_ast_grammar lstatus in *)
94   let term = CicNotationParser.term lstatus in
95   let let_defs = CicNotationParser.let_defs lstatus in
96   let let_codefs = CicNotationParser.let_codefs lstatus in
97   let protected_binder_vars = CicNotationParser.protected_binder_vars lstatus in
98   (* {{{ parser initialization *)
99 EXTEND
100   GLOBAL: term statement;
101   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
102   tactic_term: [ [ t = term LEVEL "90" -> t ] ];
103   ident_list1: [ [ LPAREN; idents = LIST1 IDENT; RPAREN -> idents ] ];
104   nreduction_kind: [
105     [ IDENT "normalize" ; delta = OPT [ IDENT "nodelta" -> () ] ->
106        let delta = match delta with None -> true | _ -> false in
107         `Normalize delta
108     | IDENT "whd" ; delta = OPT [ IDENT "nodelta" -> () ] ->
109        let delta = match delta with None -> true | _ -> false in
110         `Whd delta]
111   ];
112   sequent_pattern_spec: [
113    [ hyp_paths =
114       LIST0
115        [ id = IDENT ;
116          path = OPT [SYMBOL ":" ; path = tactic_term -> path ] ->
117          (id,match path with Some p -> p | None -> N.UserInput) ];
118      goal_path = OPT [ SYMBOL <:unicode<vdash>>; term = tactic_term -> term ] ->
119       let goal_path =
120        match goal_path, hyp_paths with
121           None, [] -> Some N.UserInput
122         | None, _::_ -> None
123         | Some goal_path, _ -> Some goal_path
124       in
125        hyp_paths,goal_path
126    ]
127   ];
128   pattern_spec: [
129     [ res = OPT [
130        "in";
131        wanted_and_sps =
132         [ "match" ; wanted = tactic_term ;
133           sps = OPT [ "in"; sps = sequent_pattern_spec -> sps ] ->
134            Some wanted,sps
135         | sps = sequent_pattern_spec ->
136            None,Some sps
137         ];
138        SYMBOL ";" ->
139          let wanted,hyp_paths,goal_path =
140           match wanted_and_sps with
141              wanted,None -> wanted, [], Some N.UserInput
142            | wanted,Some (hyp_paths,goal_path) -> wanted,hyp_paths,goal_path
143          in
144           wanted, hyp_paths, goal_path ] ->
145       match res with
146          None -> None,[],Some N.UserInput
147        | Some ps -> ps]
148   ];
149   inverter_param_list: [ 
150     [ params = tactic_term -> 
151       let deannotate = function
152         | N.AttributedTerm (_,t) | t -> t
153       in match deannotate params with
154       | N.Implicit _ -> [false]
155       | N.UserInput -> [true]
156       | N.Appl l -> 
157          List.map (fun x -> match deannotate x with  
158            | N.Implicit _ -> false
159            | N.UserInput -> true
160            | _ -> raise (Invalid_argument "malformed target parameter list 1")) l
161       | _ ->
162        (*CSC: new NCicPp.status is the best I can do here without changing the
163          result type *)
164        raise (Invalid_argument ("malformed target parameter list 2\n" ^
165               NotationPp.pp_term (new NCicPp.status None ) params)) ]
166   ];
167   direction: [
168     [ SYMBOL ">" -> `LeftToRight
169     | SYMBOL "<" -> `RightToLeft ]
170   ];
171   int: [ [ num = NUMBER -> int_of_string num ] ];
172   ntactic: [
173     [ SYMBOL "@"; t = tactic_term -> G.NTactic(loc,[G.NApply (loc, t)])
174     | IDENT "applyS"; t = tactic_term -> G.NTactic(loc,[G.NSmartApply(loc, t)])
175     | IDENT "assert";
176        seqs = LIST0 [
177         hyps = LIST0
178          [ id = IDENT ; SYMBOL ":" ; ty = tactic_term -> id,`Decl ty
179          | id = IDENT ; SYMBOL ":" ; ty = tactic_term ;
180                         SYMBOL <:unicode<def>> ; bo = tactic_term ->
181             id,`Def (bo,ty)];
182         SYMBOL <:unicode<vdash>>;
183         concl = tactic_term -> (List.rev hyps,concl) ] ->
184          G.NTactic(loc,[G.NAssert (loc, seqs)])
185     | SYMBOL "/"; num = OPT NUMBER ; 
186        just_and_params = auto_params; SYMBOL "/" ->
187        let just,params = just_and_params in
188        let depth = match num with Some n -> n | None -> "1" in
189        (match just with
190        | None -> 
191                  G.NTactic(loc,
192             [G.NAuto(loc,(None,["depth",depth]@params))])
193        | Some (b,`Univ univ) ->
194                  G.NTactic(loc,
195             [G.NAuto(loc,(Some (b,univ),["depth",depth]@params))])
196        | Some (b,`Trace) ->
197                  G.NMacro(loc,
198              G.NAutoInteractive (loc, (None,["depth",depth]@params))))
199     | SYMBOL "#"; SYMBOL "#" -> G.NMacro (loc, G.NIntroGuess loc)
200     | IDENT "check"; t = tactic_term -> G.NMacro(loc,G.NCheck (loc,t))
201     | IDENT "screenshot"; fname = QSTRING -> 
202         G.NMacro(loc,G.Screenshot (loc, fname))
203     | IDENT "cases"; what = tactic_term ; where = pattern_spec ->
204         G.NTactic(loc,[G.NCases (loc, what, where)])
205     | IDENT "change";  "with"; with_what = tactic_term; what = pattern_spec -> 
206         G.NTactic(loc,[G.NChange (loc, what, with_what)])
207     | SYMBOL "-"; id = IDENT ->
208         G.NTactic(loc,[G.NClear (loc, [id])])
209     | PLACEHOLDER; num = OPT NUMBER; 
210         l = OPT [ SYMBOL "{"; l = LIST1 tactic_term; SYMBOL "}" -> l ] -> 
211         G.NTactic(loc,[G.NConstructor (loc, (match num with None -> None | Some x -> Some (int_of_string x)),match l with None -> [] | Some l -> l)])
212     | IDENT "cut"; t = tactic_term -> G.NTactic(loc,[G.NCut (loc, t)])
213     | IDENT "destruct"; just = OPT [ dom = ident_list1 -> dom ];
214       exclude = OPT [ IDENT "skip"; skip = ident_list1 -> skip ]
215         -> let exclude' = match exclude with None -> [] | Some l -> l in
216            G.NTactic(loc,[G.NDestruct (loc,just,exclude')])
217     | IDENT "elim"; what = tactic_term ; where = pattern_spec ->
218         G.NTactic(loc,[G.NElim (loc, what, where)])
219     | IDENT "generalize"; p=pattern_spec ->
220         G.NTactic(loc,[G.NGeneralize (loc, p)])
221     | IDENT "inversion"; what = tactic_term ; where = pattern_spec ->
222         G.NTactic(loc,[G.NInversion (loc, what, where)])
223     | IDENT "lapply"; t = tactic_term -> G.NTactic(loc,[G.NLApply (loc, t)])
224     | IDENT "letin"; name = IDENT ; SYMBOL <:unicode<def>> ; t = tactic_term;
225         where = pattern_spec ->
226         G.NTactic(loc,[G.NLetIn (loc,where,t,name)])
227     | kind = nreduction_kind; p = pattern_spec ->
228         G.NTactic(loc,[G.NReduce (loc, kind, p)])
229     | dir = direction; what = tactic_term ; where = pattern_spec ->     
230         G.NTactic(loc,[G.NRewrite (loc, dir, what, where)])
231     | IDENT "try"; tac = SELF -> 
232         let tac = match tac with G.NTactic(_,[t]) -> t | _ -> assert false in
233         G.NTactic(loc,[ G.NTry (loc,tac)])
234     | IDENT "repeat"; tac = SELF -> 
235         let tac = match tac with G.NTactic(_,[t]) -> t | _ -> assert false in
236         G.NTactic(loc,[ G.NRepeat (loc,tac)])
237     | LPAREN; l = LIST1 SELF; RPAREN -> 
238         let l = 
239           List.flatten 
240             (List.map (function G.NTactic(_,t) -> t | _ -> assert false) l) in
241         G.NTactic(loc,[G.NBlock (loc,l)])
242     | IDENT "assumption" -> G.NTactic(loc,[ G.NAssumption loc])
243     | SYMBOL "#"; ns=IDENT -> G.NTactic(loc,[ G.NIntros (loc,[ns])])
244     | SYMBOL "#"; SYMBOL "_" -> G.NTactic(loc,[ G.NIntro (loc,"_")])
245     | SYMBOL "*" -> G.NTactic(loc,[ G.NCase1 (loc,"_")])
246     | SYMBOL "*"; "as"; n=IDENT -> G.NTactic(loc,[ G.NCase1 (loc,n)])
247     ]
248   ];
249   auto_fixed_param: [
250    [ IDENT "demod"
251    | IDENT "fast_paramod"
252    | IDENT "paramod"
253    | IDENT "width"
254    | IDENT "size"
255    ]
256 ];
257   auto_params: [
258     [ params = 
259       LIST0 [
260          i = auto_fixed_param -> i,""
261        | i = auto_fixed_param ; SYMBOL "="; v = [ v = int ->
262               string_of_int v | v = IDENT -> v ] -> i,v ]; 
263       just = OPT [ is_user_trace = 
264               [ IDENT "by" -> true
265               | IDENT "trace" -> false ]; 
266           by = 
267            [ univ = LIST0 tactic_term SEP SYMBOL "," -> `Univ univ
268            | SYMBOL "_" -> `Trace ] -> is_user_trace,by ] -> just,params
269    ]
270 ];
271
272 (* MATITA 1.0
273   by_continuation: [
274     [ WEPROVED; 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)
275     | WEPROVED; ty = tactic_term ; t1 = OPT [IDENT "that" ; IDENT "is" ; IDENT "equivalent" ; "to" ; t2 = tactic_term -> t2] ; 
276             "done" -> BYC_weproved (ty,None,t1)
277     | "done" -> BYC_done
278     | "let" ; id1 = IDENT ; SYMBOL ":" ; t1 = tactic_term ;
279       IDENT "such" ; IDENT "that" ; t2=tactic_term ; LPAREN ; 
280       id2 = IDENT ; RPAREN -> BYC_letsuchthat (id1,t1,id2,t2)
281     | WEHAVE; t1=tactic_term ; LPAREN ; id1=IDENT ; RPAREN ;"and" ; t2=tactic_term ; LPAREN ; id2=IDENT ; RPAREN ->
282               BYC_wehaveand (id1,t1,id2,t2)
283     ]
284 ];
285 *)
286 (* MATITA 1.0
287   rewriting_step_continuation : [
288     [ "done" -> true
289     | -> false
290     ]
291 ];
292 *)
293 (* MATITA 1.0
294   atomic_tactical:
295     [ "sequence" LEFTA
296       [ t1 = SELF; SYMBOL ";"; t2 = SELF ->
297           let ts =
298             match t1 with
299             | G.Seq (_, l) -> l @ [ t2 ]
300             | _ -> [ t1; t2 ]
301           in
302           G.Seq (loc, ts)
303       ]
304     | "then" NONA
305       [ tac = SELF; SYMBOL ";";
306         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
307           (G.Then (loc, tac, tacs))
308       ]
309     | "loops" RIGHTA
310       [ IDENT "do"; count = int; tac = SELF ->
311           G.Do (loc, count, tac)
312       | IDENT "repeat"; tac = SELF -> G.Repeat (loc, tac)
313       ]
314     | "simple" NONA
315       [ IDENT "first";
316         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
317           G.First (loc, tacs)
318       | IDENT "try"; tac = SELF -> G.Try (loc, tac)
319       | IDENT "solve";
320         SYMBOL "["; tacs = LIST0 SELF SEP SYMBOL "|"; SYMBOL "]"->
321           G.Solve (loc, tacs)
322       | IDENT "progress"; tac = SELF -> G.Progress (loc, tac)
323       | LPAREN; tac = SELF; RPAREN -> tac
324       | tac = tactic -> tac
325         ]
326       ];
327 *)
328   npunctuation_tactical:
329     [
330       [ SYMBOL "[" -> G.NBranch loc
331       | SYMBOL "|" -> G.NShift loc
332       | i = LIST1 int SEP SYMBOL ","; SYMBOL ":" -> G.NPos (loc, i)
333       | SYMBOL "*"; SYMBOL ":" -> G.NWildcard loc
334       | name = IDENT; SYMBOL ":" -> G.NPosbyname (loc, name)
335       | SYMBOL "]" -> G.NMerge loc
336       | SYMBOL ";" -> G.NSemicolon loc
337       | SYMBOL "." -> G.NDot loc
338       ]
339     ];
340   nnon_punctuation_tactical:
341     [ "simple" NONA
342       [ IDENT "focus"; goals = LIST1 int -> G.NFocus (loc, goals)
343       | IDENT "unfocus" -> G.NUnfocus loc
344       | IDENT "skip" -> G.NSkip loc
345       ]
346       ];
347   ntheorem_flavour: [
348     [ [ IDENT "definition"  ] -> `Definition
349     | [ IDENT "fact"        ] -> `Fact
350     | [ IDENT "lemma"       ] -> `Lemma
351     | [ IDENT "example"     ] -> `Example
352     | [ IDENT "theorem"     ] -> `Theorem
353     | [ IDENT "corollary"   ] -> `Corollary
354     ]
355   ];
356   inductive_spec: [ [
357     fst_name = IDENT; 
358       params = LIST0 protected_binder_vars;
359     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
360     fst_constructors = LIST0 constructor SEP SYMBOL "|";
361     tl = OPT [ "with";
362         types = LIST1 [
363           name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
364          OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
365             (name, true, typ, constructors) ] SEP "with" -> types
366       ] ->
367         let params =
368           List.fold_right
369             (fun (names, typ) acc ->
370               (List.map (fun name -> (name, typ)) names) @ acc)
371             params []
372         in
373         let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
374         let tl_ind_types = match tl with None -> [] | Some types -> types in
375         let ind_types = fst_ind_type :: tl_ind_types in
376         (params, ind_types)
377     ] ];
378     
379     record_spec: [ [
380       name = IDENT; 
381       params = LIST0 protected_binder_vars;
382        SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>; SYMBOL "{" ; 
383        fields = LIST0 [ 
384          name = IDENT ; 
385          coercion = [ 
386              SYMBOL ":" -> false,0 
387            | SYMBOL ":"; SYMBOL ">" -> true,0
388            | SYMBOL ":"; arity = int ; SYMBOL ">" -> true,arity
389          ]; 
390          ty = term -> 
391            let b,n = coercion in 
392            (name,ty,b,n) 
393        ] SEP SYMBOL ";"; SYMBOL "}" -> 
394         let params =
395           List.fold_right
396             (fun (names, typ) acc ->
397               (List.map (fun name -> (name, typ)) names) @ acc)
398             params []
399         in
400         (params,name,typ,fields)
401     ] ];
402
403     (* XXX: alias spec must be revised (no more instance nums) *)
404     alias_spec: [
405       [ IDENT "id"; id = QSTRING; SYMBOL "="; uri = QSTRING ->
406         let alpha = "[a-zA-Z]" in
407         let num = "[0-9]+" in
408         let ident_cont = "\\("^alpha^"\\|"^num^"\\|_\\|\\\\\\)" in
409         let decoration = "\\'" in
410         let ident = "\\("^alpha^ident_cont^"*"^decoration^"*\\|_"^ident_cont^"+"^decoration^"*\\)" in
411         let rex = Str.regexp ("^"^ident^"$") in
412         if Str.string_match rex id 0 then
413           if (try ignore (NReference.reference_of_string uri); true
414               with NReference.IllFormedReference _ -> false)
415           then
416             G.Ident_alias (id, uri)
417           else
418             raise
419              (HExtlib.Localized (loc, CicNotationParser.Parse_error (Printf.sprintf "Not a valid uri: %s" uri)))
420         else
421           raise (HExtlib.Localized (loc, CicNotationParser.Parse_error (
422             Printf.sprintf "Not a valid identifier: %s" id)))
423       | IDENT "symbol"; symbol = QSTRING;
424         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
425         SYMBOL "="; dsc = QSTRING ->
426           let instance =
427             match instance with Some i -> i | None -> 0
428           in
429           G.Symbol_alias (symbol, None, dsc)
430       | IDENT "num";
431         instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
432         SYMBOL "="; dsc = QSTRING ->
433           let instance =
434             match instance with Some i -> i | None -> 0
435           in
436           G.Number_alias (None,dsc)
437       ]
438      ];
439     argument: [
440       [ l = LIST0 [ SYMBOL <:unicode<eta>> (* η *); SYMBOL "." -> () ];
441         id = IDENT ->
442           N.IdentArg (List.length l, id)
443       ]
444     ];
445     associativity: [
446       [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
447       | IDENT "right"; IDENT "associative" -> Gramext.RightA
448       | IDENT "non"; IDENT "associative" -> Gramext.NonA
449       ]
450     ];
451     precedence: [
452       [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
453     ];
454     notation: [
455       [ dir = OPT direction; s = QSTRING;
456         assoc = OPT associativity; prec = precedence;
457         IDENT "for";
458         p2 = 
459           [ blob = UNPARSED_AST ->
460               add_raw_attribute ~text:(Printf.sprintf "@{%s}" blob)
461                 (CicNotationParser.parse_level2_ast lstatus
462                   (Ulexing.from_utf8_string blob))
463           | blob = UNPARSED_META ->
464               add_raw_attribute ~text:(Printf.sprintf "${%s}" blob)
465                 (CicNotationParser.parse_level2_meta lstatus
466                   (Ulexing.from_utf8_string blob))
467           ] ->
468             let assoc =
469               match assoc with
470               | None -> default_associativity
471               | Some assoc -> assoc
472             in
473             let p1 =
474               add_raw_attribute ~text:s
475                 (CicNotationParser.parse_level1_pattern lstatus prec
476                   (Ulexing.from_utf8_string s))
477             in
478             (dir, p1, assoc, prec, p2)
479       ]
480     ];
481     level3_term: [
482       [ r = NREF -> N.NRefPattern (NReference.reference_of_string r)
483       | IMPLICIT -> N.ImplicitPattern
484       | id = IDENT -> N.VarPattern id
485       | LPAREN; terms = LIST1 SELF; RPAREN ->
486           (match terms with
487           | [] -> assert false
488           | [term] -> term
489           | terms -> N.ApplPattern terms)
490       ]
491     ];
492     interpretation: [
493       [ s = CSYMBOL; args = LIST0 argument; SYMBOL "="; t = level3_term ->
494           (s, args, t)
495       ]
496     ];
497     
498     include_command: [ [
499         IDENT "include" ; path = QSTRING -> 
500           loc,path,G.WithPreferences
501       | IDENT "include" ; IDENT "alias"; path = QSTRING -> 
502           loc,path,G.OnlyPreferences
503       | IDENT "include'" ; path = QSTRING -> 
504           loc,path,G.WithoutPreferences
505      ]];
506
507   index: [[ b = OPT SYMBOL "-" -> match b with None -> true | _ -> false ]];
508
509   grafite_ncommand: [ [
510       IDENT "qed" ;  i = index -> G.NQed (loc,i)
511     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL ":"; typ = term;
512       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
513         G.NObj (loc, N.Theorem (nflavour, name, typ, body,`Regular),true)
514     | nflavour = ntheorem_flavour; name = IDENT; SYMBOL <:unicode<def>> (* ≝ *);
515       body = term ->
516         G.NObj (loc, 
517           N.Theorem(nflavour, name, N.Implicit `JustOne, Some body,`Regular),
518           true)
519     | IDENT "axiom"; i = index; name = IDENT; SYMBOL ":"; typ = term ->
520         G.NObj (loc, N.Theorem (`Axiom, name, typ, None, `Regular),i)
521     | IDENT "discriminator" ; indty = tactic_term -> G.NDiscriminator (loc,indty)
522     | IDENT "inverter"; name = IDENT; IDENT "for" ; indty = tactic_term ;
523       paramspec = OPT inverter_param_list ; 
524       outsort = OPT [ SYMBOL ":" ; outsort = term -> outsort ] -> 
525         G.NInverter (loc,name,indty,paramspec,outsort)
526     | LETCOREC ; defs = let_codefs -> 
527         nmk_rec_corec `CoInductive defs loc true
528     | LETREC ; defs = let_defs -> 
529         nmk_rec_corec `Inductive defs loc true
530     | IDENT "inductive"; spec = inductive_spec ->
531         let (params, ind_types) = spec in
532         G.NObj (loc, N.Inductive (params, ind_types),true)
533     | IDENT "coinductive"; spec = inductive_spec ->
534         let (params, ind_types) = spec in
535         let ind_types = (* set inductive flags to false (coinductive) *)
536           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
537             ind_types
538         in
539         G.NObj (loc, N.Inductive (params, ind_types),true)
540     | IDENT "universe"; IDENT "constraint"; u1 = tactic_term; 
541         SYMBOL <:unicode<lt>> ; u2 = tactic_term ->
542         let urify = function 
543           | NotationPt.AttributedTerm (_, NotationPt.Sort (`NType i)) ->
544               NUri.uri_of_string ("cic:/matita/pts/Type"^i^".univ")
545           | _ -> raise (Failure "only a Type[…] sort can be constrained")
546         in
547         let u1 = urify u1 in
548         let u2 = urify u2 in
549          G.NUnivConstraint (loc,u1,u2)
550     | IDENT "unification"; IDENT "hint"; n = int; t = tactic_term ->
551         G.UnificationHint (loc, t, n)
552     | IDENT "coercion"; name = IDENT;
553         compose = OPT [ IDENT "nocomposites" -> () ];
554         spec = OPT [ SYMBOL ":"; ty = term; 
555         SYMBOL <:unicode<def>>; t = term; "on"; 
556         id = [ IDENT | PIDENT ]; SYMBOL ":"; source = term;
557         "to"; target = term -> t,ty,(id,source),target ] ->
558           let compose = compose = None in
559           G.NCoercion(loc,name,compose,spec)
560     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
561         G.NObj (loc, N.Record (params,name,ty,fields),true)
562     | IDENT "copy" ; s = IDENT; IDENT "from"; u = URI; "with"; 
563       m = LIST0 [ u1 = URI; SYMBOL <:unicode<mapsto>>; u2 = URI -> u1,u2 ] ->
564         G.NCopy (loc,s,NUri.uri_of_string u,
565           List.map (fun a,b -> NUri.uri_of_string a, NUri.uri_of_string b) m)
566     | lc = lexicon_command -> lc
567   ]];
568
569   lexicon_command: [ [
570       IDENT "alias" ; spec = alias_spec ->
571         G.Alias (loc, spec)
572     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
573         G.Notation (loc, dir, l1, assoc, prec, l2)
574     | IDENT "interpretation"; id = QSTRING;
575       (symbol, args, l3) = interpretation ->
576         G.Interpretation (loc, id, (symbol, args), l3)
577   ]];
578   executable: [
579     [ ncmd = grafite_ncommand; SYMBOL "." -> G.NCommand (loc, ncmd)
580     | punct = npunctuation_tactical -> G.NTactic (loc, [punct])
581     | tac = nnon_punctuation_tactical(*; punct = npunctuation_tactical*) ->
582           G.NTactic (loc, [tac])
583     | tac = ntactic (*; punct = npunctuation_tactical*) ->
584          tac 
585 (*
586     | tac = nnon_punctuation_tactical; 
587         punct = npunctuation_tactical ->
588           G.NTactic (loc, [tac; punct])
589 *)
590     ]
591   ];
592   comment: [
593     [ BEGINCOMMENT ; ex = executable ; ENDCOMMENT -> 
594        G.Code (loc, ex)
595     | str = NOTE -> 
596        G.Note (loc, str)
597     ]
598   ];
599   statement: [
600     [ ex = executable -> G.Executable (loc, ex)
601     | com = comment -> G.Comment (loc, com)
602     | (iloc,fname,mode) = include_command ; SYMBOL "."  ->
603                G.Executable (loc,G.NCommand (loc,G.Include (iloc,mode,fname)))
604     | EOI -> raise End_of_file
605     ]
606   ];
607   END;
608 (* }}} *)
609   statement
610 ;;
611
612 type db = GrafiteAst.statement Grammar.Entry.e ;;
613
614 class type g_status =
615  object
616   inherit CicNotationParser.g_status
617   method parser_db: db
618  end
619
620 class virtual status uid =
621  object(self)
622   inherit CicNotationParser.status uid ~keywords:[]
623   val mutable db = None (* mutable only to initialize it :-( *)
624   method parser_db = match db with None -> assert false | Some x -> x
625   method set_parser_db v = {< db = Some v >}
626   method set_parser_status
627    : 'status. #g_status as 'status -> 'self
628    = fun o -> {< db = Some o#parser_db >}#set_notation_parser_status o
629   initializer
630    let grammar = CicNotationParser.level2_ast_grammar self in
631    db <- Some (mk_parser (Grammar.Entry.create grammar "statement") self)
632  end
633
634 let extend status l1 action = 
635   let status = CicNotationParser.extend status l1 action in
636   let grammar = CicNotationParser.level2_ast_grammar status in
637   status#set_parser_db
638     (mk_parser (Grammar.Entry.create grammar "statement") status)
639 ;;
640
641
642 let parse_statement status =
643   status#reset_loctable (); 
644   parse_statement status#parser_db
645
646 (* vim:set foldmethod=marker: *)