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