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