]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_engine/grafiteEngine.ml
WARNING: partial commit.
[helm.git] / matita / components / grafite_engine / grafiteEngine.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 exception Drop
29 (* mo file name, ma file name *)
30 exception NMacro of GrafiteAst.loc * GrafiteAst.nmacro
31
32 type 'a disambiguator_input = string * int * 'a
33
34 type options = { 
35   do_heavy_checks: bool ; 
36 }
37
38 let basic_eval_unification_hint (t,n) status =
39  NCicUnifHint.add_user_provided_hint status t n
40 ;;
41
42 let inject_unification_hint =
43  let basic_eval_unification_hint (t,n) 
44    ~refresh_uri_in_universe 
45    ~refresh_uri_in_term
46  =
47   let t = refresh_uri_in_term t in basic_eval_unification_hint (t,n)
48  in
49   GrafiteTypes.Serializer.register#run "unification_hints"
50    basic_eval_unification_hint
51 ;;
52
53 let eval_unification_hint status t n = 
54  let metasenv,subst,status,t =
55   GrafiteDisambiguate.disambiguate_nterm None status [] [] [] ("",0,t) in
56  assert (metasenv=[]);
57  let t = NCicUntrusted.apply_subst subst [] t in
58  let status = basic_eval_unification_hint (t,n) status in
59  let dump = inject_unification_hint (t,n)::status#dump in
60  let status = status#set_dump dump in
61   status,[]
62 ;;
63
64 let basic_index_obj l status =
65   status#set_auto_cache 
66     (List.fold_left
67       (fun t (ks,v) -> 
68          List.fold_left (fun t k ->
69            NDiscriminationTree.DiscriminationTree.index t k v)
70           t ks) 
71     status#auto_cache l) 
72 ;;     
73
74 let record_index_obj = 
75  let aux l 
76    ~refresh_uri_in_universe 
77    ~refresh_uri_in_term
78  =
79     basic_index_obj
80       (List.map 
81         (fun ks,v -> List.map refresh_uri_in_term ks, refresh_uri_in_term v) 
82       l)
83  in
84   GrafiteTypes.Serializer.register#run "index_obj" aux
85 ;;
86
87 let compute_keys status uri height kind = 
88  let mk_item ty spec =
89    let orig_ty = NTacStatus.mk_cic_term [] ty in
90    let status,keys = NnAuto.keys_of_type status orig_ty in
91    let keys =  
92      List.map 
93        (fun t -> 
94           snd (NTacStatus.term_of_cic_term status t (NTacStatus.ctx_of t)))
95        keys
96    in
97    keys,NCic.Const(NReference.reference_of_spec uri spec)
98  in
99  let data = 
100   match kind with
101   | NCic.Fixpoint (ind,ifl,_) -> 
102      HExtlib.list_mapi 
103        (fun (_,_,rno,ty,_) i -> 
104           if ind then mk_item ty (NReference.Fix (i,rno,height)) 
105           else mk_item ty (NReference.CoFix height)) ifl
106   | NCic.Inductive (b,lno,itl,_) -> 
107      HExtlib.list_mapi 
108        (fun (_,_,ty,_) i -> mk_item ty (NReference.Ind (b,i,lno))) itl 
109      @
110      List.map (fun ((_,_,ty),i,j) -> mk_item ty (NReference.Con (i,j+1,lno)))
111        (List.flatten (HExtlib.list_mapi 
112          (fun (_,_,_,cl) i -> HExtlib.list_mapi (fun x j-> x,i,j) cl)
113          itl))
114   | NCic.Constant (_,_,Some _, ty, _) -> 
115      [ mk_item ty (NReference.Def height) ]
116   | NCic.Constant (_,_,None, ty, _) ->
117      [ mk_item ty NReference.Decl ]
118  in
119   HExtlib.filter_map
120    (fun (keys, t) ->
121      let keys = List.filter
122        (function 
123          | (NCic.Meta _) 
124          | (NCic.Appl (NCic.Meta _::_)) -> false 
125          | _ -> true) 
126        keys
127      in
128      if keys <> [] then 
129       begin
130         HLog.debug ("Indexing:" ^ 
131           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
132         HLog.debug ("With keys:" ^ String.concat "\n" (List.map (fun t ->
133           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t) keys));
134         Some (keys,t) 
135       end
136      else 
137       begin
138         HLog.debug ("Not indexing:" ^ 
139           NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
140         None
141       end)
142     data
143 ;;
144
145 let index_obj_for_auto status (uri, height, _, _, kind) = 
146  (*prerr_endline (string_of_int height);*)
147   let data = compute_keys status uri height kind in
148   let status = basic_index_obj data status in
149   let dump = record_index_obj data :: status#dump in   
150   status#set_dump dump
151 ;; 
152
153 let index_eq uri status =
154   let eq_status = status#eq_cache in
155   let eq_status1 = NCicParamod.index_obj eq_status uri in
156     status#set_eq_cache eq_status1
157 ;;
158
159 let record_index_eq =
160  let basic_index_eq uri
161    ~refresh_uri_in_universe 
162    ~refresh_uri_in_term 
163    = index_eq (NCicLibrary.refresh_uri uri) 
164  in
165   GrafiteTypes.Serializer.register#run "index_eq" basic_index_eq
166 ;;
167
168 let index_eq_for_auto status uri =
169  if NnAuto.is_a_fact_obj status uri then
170    let newstatus = index_eq uri status in
171      if newstatus#eq_cache == status#eq_cache then status 
172      else
173        ((*prerr_endline ("recording " ^ (NUri.string_of_uri uri));*)
174         let dump = record_index_eq uri :: newstatus#dump 
175         in newstatus#set_dump dump)
176  else 
177    ((*prerr_endline "Not a fact";*)
178    status)
179 ;; 
180
181 let basic_eval_add_constraint (u1,u2) status =
182  NCicLibrary.add_constraint status u1 u2
183 ;;
184
185 let inject_constraint =
186  let basic_eval_add_constraint (u1,u2) 
187        ~refresh_uri_in_universe 
188        ~refresh_uri_in_term
189  =
190   let u1 = refresh_uri_in_universe u1 in 
191   let u2 = refresh_uri_in_universe u2 in 
192   basic_eval_add_constraint (u1,u2)
193  in
194   GrafiteTypes.Serializer.register#run "constraints" basic_eval_add_constraint 
195 ;;
196
197 let eval_add_constraint status u1 u2 = 
198  let status = basic_eval_add_constraint (u1,u2) status in
199  let dump = inject_constraint (u1,u2)::status#dump in
200  let status = status#set_dump dump in
201   status,[]
202 ;;
203
204 let eval_ng_tac tac =
205  let rec aux f (text, prefix_len, tac) =
206   match tac with
207   | GrafiteAst.NApply (_loc, t) -> NTactics.apply_tac (text,prefix_len,t) 
208   | GrafiteAst.NSmartApply (_loc, t) -> 
209       NnAuto.smart_apply_tac (text,prefix_len,t) 
210   | GrafiteAst.NAssert (_loc, seqs) ->
211      NTactics.assert_tac
212       ((List.map
213         (function (hyps,concl) ->
214           List.map
215            (function
216               (id,`Decl t) -> id,`Decl (text,prefix_len,t)
217              |(id,`Def (b,t))->id,`Def((text,prefix_len,b),(text,prefix_len,t))
218            ) hyps,
219           (text,prefix_len,concl))
220        ) seqs)
221   | GrafiteAst.NAuto (_loc, (None,a)) -> 
222       NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
223   | GrafiteAst.NAuto (_loc, (Some l,a)) ->
224       NnAuto.auto_tac
225         ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
226   | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
227   | GrafiteAst.NCases (_loc, what, where) ->
228       NTactics.cases_tac 
229         ~what:(text,prefix_len,what)
230         ~where:(text,prefix_len,where)
231   | GrafiteAst.NCase1 (_loc,n) -> NTactics.case1_tac n
232   | GrafiteAst.NChange (_loc, pat, ww) -> 
233       NTactics.change_tac 
234        ~where:(text,prefix_len,pat) ~with_what:(text,prefix_len,ww) 
235   | GrafiteAst.NConstructor (_loc,num,args) -> 
236      NTactics.constructor_tac 
237        ?num ~args:(List.map (fun x -> text,prefix_len,x) args)
238   | GrafiteAst.NCut (_loc, t) -> NTactics.cut_tac (text,prefix_len,t) 
239 (*| GrafiteAst.NDiscriminate (_,what) -> NDestructTac.discriminate_tac ~what:(text,prefix_len,what)
240   | GrafiteAst.NSubst (_,what) -> NDestructTac.subst_tac ~what:(text,prefix_len,what)*)
241   | GrafiteAst.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
242   | GrafiteAst.NDot _ -> NTactics.dot_tac 
243   | GrafiteAst.NElim (_loc, what, where) ->
244       NTactics.elim_tac 
245         ~what:(text,prefix_len,what)
246         ~where:(text,prefix_len,where)
247   | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
248   | GrafiteAst.NGeneralize (_loc, where) -> 
249       NTactics.generalize_tac ~where:(text,prefix_len,where)
250   | GrafiteAst.NId _ -> (fun x -> x)
251   | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
252   | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
253   | GrafiteAst.NInversion (_loc, what, where) ->
254       NTactics.inversion_tac 
255         ~what:(text,prefix_len,what)
256         ~where:(text,prefix_len,where)
257   | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t) 
258   | GrafiteAst.NLetIn (_loc,where,what,name) ->
259       NTactics.letin_tac ~where:(text,prefix_len,where) 
260         ~what:(text,prefix_len,what) name
261   | GrafiteAst.NMerge _ -> NTactics.merge_tac 
262   | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
263   | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
264   | GrafiteAst.NReduce (_loc, reduction, where) ->
265       NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
266   | GrafiteAst.NRewrite (_loc,dir,what,where) ->
267      NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
268       ~where:(text,prefix_len,where)
269   | GrafiteAst.NSemicolon _ -> fun x -> x
270   | GrafiteAst.NShift _ -> NTactics.shift_tac 
271   | GrafiteAst.NSkip _ -> NTactics.skip_tac
272   | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
273   | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac 
274   | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
275       (aux f (text, prefix_len, tac))
276   | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
277   | GrafiteAst.NBlock (_,l) -> 
278       NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
279   |GrafiteAst.NRepeat (_,tac) ->
280       NTactics.repeat_tac (f f (text, prefix_len, tac))
281  in
282   aux aux tac (* trick for non uniform recursion call *)
283 ;;
284       
285 let subst_metasenv_and_fix_names status =
286   let u,h,metasenv, subst,o = status#obj in
287   let o = 
288     NCicUntrusted.map_obj_kind ~skip_body:true 
289      (NCicUntrusted.apply_subst subst []) o
290   in
291    status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
292 ;;
293
294
295 let rec eval_ncommand opts status (text,prefix_len,cmd) =
296   match cmd with
297   | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
298   | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
299       NCicCoercDeclaration.eval_ncoercion status name t ty source target
300   | GrafiteAst.NQed loc ->
301      if status#ng_mode <> `ProofMode then
302       raise (GrafiteTypes.Command_error "Not in proof mode")
303      else
304       let uri,height,menv,subst,obj_kind = status#obj in
305        if menv <> [] then
306         raise
307          (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
308        else
309         let obj_kind =
310          NCicUntrusted.map_obj_kind 
311           (NCicUntrusted.apply_subst subst []) obj_kind in
312         let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
313         (* fix the height inside the object *)
314         let rec fix () = function 
315           | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri -> 
316              NCic.Const (NReference.reference_of_spec u
317               (match spec with
318               | NReference.Def _ -> NReference.Def height
319               | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
320               | NReference.CoFix _ -> NReference.CoFix height
321               | NReference.Ind _ | NReference.Con _
322               | NReference.Decl as s -> s))
323           | t -> NCicUtils.map (fun _ () -> ()) () fix t
324         in
325         let obj_kind = 
326           match obj_kind with
327           | NCic.Fixpoint _ -> 
328               NCicUntrusted.map_obj_kind (fix ()) obj_kind 
329           | _ -> obj_kind
330         in
331         let obj = uri,height,[],[],obj_kind in
332         let old_status = status in
333         let status = NCicLibrary.add_obj status obj in
334         let index_obj =
335          match obj_kind with
336             NCic.Constant (_,_,_,_,(_,`Example,_))
337           | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
338           | _ -> true
339         in
340         let status =
341          if index_obj then
342           let status = index_obj_for_auto status obj in
343            (try index_eq_for_auto status uri
344            with _ -> status)
345          else
346           status in
347 (*
348           try 
349             index_eq uri status
350           with _ -> prerr_endline "got an exception"; status
351         in *)
352 (*         prerr_endline (NCicPp.ppobj obj); *)
353         HLog.message ("New object: " ^ NUri.string_of_uri uri);
354          (try
355        (*prerr_endline (NCicPp.ppobj obj);*)
356            let boxml = NCicElim.mk_elims obj in
357            let boxml = boxml @ NCicElim.mk_projections obj in
358 (*
359            let objs = [] in
360            let timestamp,uris_rev =
361              List.fold_left
362               (fun (status,uris_rev) (uri,_,_,_,_) as obj ->
363                 let status = NCicLibrary.add_obj status obj in
364                  status,uri::uris_rev
365               ) (status,[]) objs in
366            let uris = uri::List.rev uris_rev in
367 *)
368            let status = status#set_ng_mode `CommandMode in
369            let status = LexiconSync.add_aliases_for_objs status [uri] in
370            let status,uris =
371             List.fold_left
372              (fun (status,uris) boxml ->
373                try
374                 let nstatus,nuris =
375                  eval_ncommand opts status
376                   ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
377                 in
378                 if nstatus#ng_mode <> `CommandMode then
379                   begin
380                     (*HLog.warn "error in generating projection/eliminator";*)
381                     assert(status#ng_mode = `CommandMode);
382                     status, uris
383                   end
384                 else
385                   nstatus, uris@nuris
386                with
387                | MultiPassDisambiguator.DisambiguationError _
388                | NCicTypeChecker.TypeCheckerFailure _ ->
389                   (*HLog.warn "error in generating projection/eliminator";*)
390                   status,uris
391              ) (status,[] (* uris *)) boxml in             
392            let _,_,_,_,nobj = obj in 
393            let status = match nobj with
394                NCic.Inductive (is_ind,leftno,[it],_) ->
395                  let _,ind_name,ty,cl = it in
396                  List.fold_left 
397                    (fun status outsort ->
398                       let status = status#set_ng_mode `ProofMode in
399                       try
400                        (let status,invobj =
401                          NInversion.mk_inverter 
402                           (ind_name ^ "_inv_" ^
403                             (snd (NCicElim.ast_of_sort outsort)))
404                           is_ind it leftno outsort status status#baseuri in
405                        let _,_,menv,_,_ = invobj in
406                        fst (match menv with
407                              [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
408                            | _ -> status,[]))
409                        (* XXX *)
410                       with _ -> (*HLog.warn "error in generating inversion principle"; *)
411                                 let status = status#set_ng_mode `CommandMode in status)
412                   status
413                   (NCic.Prop::
414                     List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
415               | _ -> status
416            in
417            let coercions =
418             match obj with
419               _,_,_,_,NCic.Inductive
420                (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
421                ->
422                 HExtlib.filter_map
423                  (fun (name,is_coercion,arity) ->
424                    if is_coercion then Some(name,leftno,arity) else None) fields
425             | _ -> [] in
426            let status,uris =
427             List.fold_left
428              (fun (status,uris) (name,cpos,arity) ->
429                try
430                  let metasenv,subst,status,t =
431                   GrafiteDisambiguate.disambiguate_nterm None status [] [] []
432                    ("",0,NotationPt.Ident (name,None)) in
433                  assert (metasenv = [] && subst = []);
434                  let status, nuris = 
435                    NCicCoercDeclaration.
436                      basic_eval_and_record_ncoercion_from_t_cpos_arity 
437                       status (name,t,cpos,arity)
438                  in
439                  let uris = nuris@uris in
440                  status, uris
441                with MultiPassDisambiguator.DisambiguationError _-> 
442                  HLog.warn ("error in generating coercion: "^name);
443                  status, uris) 
444              (status,uris) coercions
445            in
446             status,uris
447           with
448            exn ->
449             NCicLibrary.time_travel old_status;
450             raise exn)
451   | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
452      if status#ng_mode <> `CommandMode then
453       raise (GrafiteTypes.Command_error "Not in command mode")
454      else
455        let tgt_uri_ext, old_ok = 
456          match NCicEnvironment.get_checked_obj src_uri with
457          | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
458          | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
459          | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
460          | _ -> assert false
461        in
462        let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
463        let map = (src_uri, tgt_uri) :: map in
464        let ok = 
465          let rec subst () = function
466            | NCic.Meta _ -> assert false
467            | NCic.Const (NReference.Ref (u,spec)) as t ->
468                (try NCic.Const 
469                  (NReference.reference_of_spec (List.assoc u map)spec)
470                with Not_found -> t)
471            | t -> NCicUtils.map (fun _ _ -> ()) () subst t
472          in
473          NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
474        in
475        let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
476        let status = status#set_obj (tgt_uri,0,[],[],ok) in
477        (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
478        let status = status#set_stack ninitial_stack in
479        let status = subst_metasenv_and_fix_names status in
480        let status = status#set_ng_mode `ProofMode in
481        eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
482   | GrafiteAst.NObj (loc,obj) ->
483      if status#ng_mode <> `CommandMode then
484       raise (GrafiteTypes.Command_error "Not in command mode")
485      else
486       let status,obj =
487        GrafiteDisambiguate.disambiguate_nobj status
488         ~baseuri:status#baseuri (text,prefix_len,obj) in
489       let uri,height,nmenv,nsubst,nobj = obj in
490       let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
491       let status = status#set_obj obj in
492       let status = status#set_stack ninitial_stack in
493       let status = subst_metasenv_and_fix_names status in
494       let status = status#set_ng_mode `ProofMode in
495       (match nmenv with
496           [] ->
497            eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
498         | _ -> status,[])
499   | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
500       if status#ng_mode <> `CommandMode then
501         raise (GrafiteTypes.Command_error "Not in command mode")
502       else
503         let status = status#set_ng_mode `ProofMode in
504         let metasenv,subst,status,indty =
505           GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
506         let indtyno, (_,_,tys,_,_) = match indty with
507             NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
508               indtyno, NCicEnvironment.get_checked_indtys r
509           | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
510         let it = List.nth tys indtyno in
511         let status,obj =  NDestructTac.mk_discriminator it status in
512         let _,_,menv,_,_ = obj in
513           (match menv with
514                [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
515              | _ -> prerr_endline ("Discriminator: non empty metasenv");
516                     status, []) *)
517   | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
518      if status#ng_mode <> `CommandMode then
519       raise (GrafiteTypes.Command_error "Not in command mode")
520      else
521       let metasenv,subst,status,sort = match sort with
522         | None -> [],[],status,NCic.Sort NCic.Prop
523         | Some s -> GrafiteDisambiguate.disambiguate_nterm None status [] [] []
524                       (text,prefix_len,s) 
525       in
526       assert (metasenv = []);
527       let sort = NCicReduction.whd ~subst [] sort in
528       let sort = match sort with 
529           NCic.Sort s -> s
530         | _ ->  raise (Invalid_argument (Printf.sprintf "ninverter: found target %s, which is not a sort" 
531                                            (NCicPp.ppterm ~metasenv ~subst ~context:[] sort)))
532       in
533       let status = status#set_ng_mode `ProofMode in
534       let metasenv,subst,status,indty =
535        GrafiteDisambiguate.disambiguate_nterm None status [] [] subst (text,prefix_len,indty) in
536       let indtyno,(_,leftno,tys,_,_) = match indty with
537           NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) -> 
538             indtyno, NCicEnvironment.get_checked_indtys r
539         | _ -> prerr_endline ("engine: indty ="  ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] indty) ; assert false in
540       let it = List.nth tys indtyno in
541      let status,obj = NInversion.mk_inverter name true it leftno ?selection sort 
542                         status status#baseuri in
543      let _,_,menv,_,_ = obj in
544      (match menv with
545         [] ->
546           eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
547       | _ -> assert false)
548   | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
549       eval_add_constraint status [`Type,u1] [`Type,u2]
550 ;;
551
552 let eval_comment opts status (text,prefix_len,c) = status, []
553
554 let rec eval_command opts status (text,prefix_len,cmd) =
555  let status,uris =
556   match cmd with
557   | GrafiteAst.Include (loc, baseuri) ->
558      let status,obj =
559        GrafiteTypes.Serializer.require ~baseuri:(NUri.uri_of_string baseuri)
560         status in
561      let status = status#set_dump (obj::status#dump) in
562        status,[]
563   | GrafiteAst.Print (_,_) -> status,[]
564   | GrafiteAst.Set (loc, name, value) -> status, []
565  in
566   status,uris
567
568 and eval_executable opts status (text,prefix_len,ex) =
569   match ex with
570   | GrafiteAst.NTactic (_(*loc*), tacl) ->
571       if status#ng_mode <> `ProofMode then
572        raise (GrafiteTypes.Command_error "Not in proof mode")
573       else
574        let status =
575         List.fold_left 
576           (fun status tac ->
577             let status = eval_ng_tac (text,prefix_len,tac) status in
578             subst_metasenv_and_fix_names status)
579           status tacl
580        in
581         status,[]
582   | GrafiteAst.Command (_, cmd) ->
583       eval_command opts status (text,prefix_len,cmd)
584   | GrafiteAst.NCommand (_, cmd) ->
585       eval_ncommand opts status (text,prefix_len,cmd)
586   | GrafiteAst.NMacro (loc, macro) ->
587      raise (NMacro (loc,macro))
588
589 and eval_ast ?(do_heavy_checks=false) status (text,prefix_len,st) =
590   let opts = { do_heavy_checks = do_heavy_checks ; } in
591   match st with
592   | GrafiteAst.Executable (_,ex) ->
593      eval_executable opts status (text,prefix_len,ex)
594   | GrafiteAst.Comment (_,c) -> 
595       eval_comment opts status (text,prefix_len,c) 
596 ;;