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