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