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