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