]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_engine/grafiteEngine.ml
- cic_exportation, cic_acic, acic_content (only parts related to acic)
[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 add_coercions_of_lemmas lemmas status =
293   let moo_content = 
294     HExtlib.filter_map 
295       (fun uri ->
296         match CoercDb.is_a_coercion (Cic.Const (uri,[])) with
297         | None -> None
298         | Some (_,tgt,_,sat,_) ->
299             let arity = match tgt with CoercDb.Fun n -> n | _ -> 0 in
300             Some (coercion_moo_statement_of (uri,arity,sat,0)))
301       lemmas
302   in
303   let status = GrafiteTypes.add_moo_content moo_content status in 
304    status#set_coercions (CoercDb.dump ()), 
305   lemmas
306
307 let eval_ng_punct (_text, _prefix_len, punct) =
308   match punct with
309   | GrafiteAst.Dot _ -> NTactics.dot_tac 
310   | GrafiteAst.Semicolon _ -> fun x -> x
311   | GrafiteAst.Branch _ -> NTactics.branch_tac ~force:false
312   | GrafiteAst.Shift _ -> NTactics.shift_tac 
313   | GrafiteAst.Pos (_,l) -> NTactics.pos_tac l
314   | GrafiteAst.Wildcard _ -> NTactics.wildcard_tac 
315   | GrafiteAst.Merge _ -> NTactics.merge_tac 
316 ;;
317
318 let eval_ng_tac tac =
319  let rec aux f (text, prefix_len, tac) =
320   match tac with
321   | GrafiteAst.NApply (_loc, t) -> NTactics.apply_tac (text,prefix_len,t) 
322   | GrafiteAst.NSmartApply (_loc, t) -> 
323       NnAuto.smart_apply_tac (text,prefix_len,t) 
324   | GrafiteAst.NAssert (_loc, seqs) ->
325      NTactics.assert_tac
326       ((List.map
327         (function (hyps,concl) ->
328           List.map
329            (function
330               (id,`Decl t) -> id,`Decl (text,prefix_len,t)
331              |(id,`Def (b,t))->id,`Def((text,prefix_len,b),(text,prefix_len,t))
332            ) hyps,
333           (text,prefix_len,concl))
334        ) seqs)
335   | GrafiteAst.NAuto (_loc, (None,a)) -> 
336       NnAuto.auto_tac ~params:(None,a) ?trace_ref:None
337   | GrafiteAst.NAuto (_loc, (Some l,a)) ->
338       NnAuto.auto_tac
339         ~params:(Some List.map (fun x -> "",0,x) l,a) ?trace_ref:None
340   | GrafiteAst.NBranch _ -> NTactics.branch_tac ~force:false
341   | GrafiteAst.NCases (_loc, what, where) ->
342       NTactics.cases_tac 
343         ~what:(text,prefix_len,what)
344         ~where:(text,prefix_len,where)
345   | GrafiteAst.NCase1 (_loc,n) -> NTactics.case1_tac n
346   | GrafiteAst.NChange (_loc, pat, ww) -> 
347       NTactics.change_tac 
348        ~where:(text,prefix_len,pat) ~with_what:(text,prefix_len,ww) 
349   | GrafiteAst.NConstructor (_loc,num,args) -> 
350      NTactics.constructor_tac 
351        ?num ~args:(List.map (fun x -> text,prefix_len,x) args)
352   | GrafiteAst.NCut (_loc, t) -> NTactics.cut_tac (text,prefix_len,t) 
353 (*| GrafiteAst.NDiscriminate (_,what) -> NDestructTac.discriminate_tac ~what:(text,prefix_len,what)
354   | GrafiteAst.NSubst (_,what) -> NDestructTac.subst_tac ~what:(text,prefix_len,what)*)
355   | GrafiteAst.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
356   | GrafiteAst.NDot _ -> NTactics.dot_tac 
357   | GrafiteAst.NElim (_loc, what, where) ->
358       NTactics.elim_tac 
359         ~what:(text,prefix_len,what)
360         ~where:(text,prefix_len,where)
361   | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
362   | GrafiteAst.NGeneralize (_loc, where) -> 
363       NTactics.generalize_tac ~where:(text,prefix_len,where)
364   | GrafiteAst.NId _ -> (fun x -> x)
365   | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
366   | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
367   | GrafiteAst.NInversion (_loc, what, where) ->
368       NTactics.inversion_tac 
369         ~what:(text,prefix_len,what)
370         ~where:(text,prefix_len,where)
371   | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t) 
372   | GrafiteAst.NLetIn (_loc,where,what,name) ->
373       NTactics.letin_tac ~where:(text,prefix_len,where) 
374         ~what:(text,prefix_len,what) name
375   | GrafiteAst.NMerge _ -> NTactics.merge_tac 
376   | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
377   | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
378   | GrafiteAst.NReduce (_loc, reduction, where) ->
379       NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
380   | GrafiteAst.NRewrite (_loc,dir,what,where) ->
381      NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
382       ~where:(text,prefix_len,where)
383   | GrafiteAst.NSemicolon _ -> fun x -> x
384   | GrafiteAst.NShift _ -> NTactics.shift_tac 
385   | GrafiteAst.NSkip _ -> NTactics.skip_tac
386   | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
387   | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac 
388   | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
389       (aux f (text, prefix_len, tac))
390   | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
391   | GrafiteAst.NBlock (_,l) -> 
392       NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
393   |GrafiteAst.NRepeat (_,tac) ->
394       NTactics.repeat_tac (f f (text, prefix_len, tac))
395  in
396   aux aux tac (* trick for non uniform recursion call *)
397 ;;
398       
399 let subst_metasenv_and_fix_names status =
400   let u,h,metasenv, subst,o = status#obj in
401   let o = 
402     NCicUntrusted.map_obj_kind ~skip_body:true 
403      (NCicUntrusted.apply_subst subst []) o
404   in
405    status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
406 ;;
407
408
409 let rec eval_ncommand opts status (text,prefix_len,cmd) =
410   match cmd with
411   | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
412   | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
413       NCicCoercDeclaration.eval_ncoercion status name t ty source target
414   | GrafiteAst.NQed loc ->
415      if status#ng_mode <> `ProofMode then
416       raise (GrafiteTypes.Command_error "Not in proof mode")
417      else
418       let uri,height,menv,subst,obj_kind = status#obj in
419        if menv <> [] then
420         raise
421          (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
422        else
423         let obj_kind =
424          NCicUntrusted.map_obj_kind 
425           (NCicUntrusted.apply_subst subst []) obj_kind in
426         let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
427         (* fix the height inside the object *)
428         let rec fix () = function 
429           | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri -> 
430              NCic.Const (NReference.reference_of_spec u
431               (match spec with
432               | NReference.Def _ -> NReference.Def height
433               | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
434               | NReference.CoFix _ -> NReference.CoFix height
435               | NReference.Ind _ | NReference.Con _
436               | NReference.Decl as s -> s))
437           | t -> NCicUtils.map (fun _ () -> ()) () fix t
438         in
439         let obj_kind = 
440           match obj_kind with
441           | NCic.Fixpoint _ -> 
442               NCicUntrusted.map_obj_kind (fix ()) obj_kind 
443           | _ -> obj_kind
444         in
445         let obj = uri,height,[],[],obj_kind in
446         prerr_endline ("pp new obj \n"^NCicPp.ppobj obj);
447         let old_status = status in
448         let status = NCicLibrary.add_obj status obj in
449         let index_obj =
450          match obj_kind with
451             NCic.Constant (_,_,_,_,(_,`Example,_))
452           | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
453           | _ -> true
454         in
455         let status =
456          if index_obj then
457           let status = index_obj_for_auto status obj in
458            (try index_eq_for_auto status uri
459            with _ -> status)
460          else
461           status in
462 (*
463           try 
464             index_eq uri status
465           with _ -> prerr_endline "got an exception"; status
466         in *)
467 (*         prerr_endline (NCicPp.ppobj obj); *)
468         HLog.message ("New object: " ^ NUri.string_of_uri uri);
469          (try
470        (*prerr_endline (NCicPp.ppobj obj);*)
471            let boxml = NCicElim.mk_elims obj in
472            let boxml = boxml @ NCicElim.mk_projections obj in
473 (*
474            let objs = [] in
475            let timestamp,uris_rev =
476              List.fold_left
477               (fun (status,uris_rev) (uri,_,_,_,_) as obj ->
478                 let status = NCicLibrary.add_obj status obj in
479                  status,uri::uris_rev
480               ) (status,[]) objs in
481            let uris = uri::List.rev uris_rev in
482 *)
483            let status = status#set_ng_mode `CommandMode in
484            let status = LexiconSync.add_aliases_for_objs status (`New [uri]) in
485            let status,uris =
486             List.fold_left
487              (fun (status,uris) boxml ->
488                try
489                 let nstatus,nuris =
490                  eval_ncommand opts status
491                   ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
492                 in
493                 if nstatus#ng_mode <> `CommandMode then
494                   begin
495                     (*HLog.warn "error in generating projection/eliminator";*)
496                     status, uris
497                   end
498                 else
499                   nstatus, concat_nuris uris nuris
500                with
501                | MultiPassDisambiguator.DisambiguationError _
502                | NCicTypeChecker.TypeCheckerFailure _ ->
503                   (*HLog.warn "error in generating projection/eliminator";*)
504                   status,uris
505              ) (status,`New [] (* uris *)) boxml in             
506            let _,_,_,_,nobj = obj in 
507            let status = match nobj with
508                NCic.Inductive (is_ind,leftno,[it],_) ->
509                  let _,ind_name,ty,cl = it in
510                  List.fold_left 
511                    (fun status outsort ->
512                       let status = status#set_ng_mode `ProofMode in
513                       try
514                        (let status,invobj =
515                          NInversion.mk_inverter 
516                           (ind_name ^ "_inv_" ^
517                             (snd (NCicElim.ast_of_sort outsort)))
518                           is_ind it leftno outsort status status#baseuri in
519                        let _,_,menv,_,_ = invobj in
520                        fst (match menv with
521                              [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
522                            | _ -> status,`New []))
523                        (* XXX *)
524                       with _ -> (*HLog.warn "error in generating inversion principle"; *)
525                                 let status = status#set_ng_mode `CommandMode in status)
526                   status
527                   (NCic.Prop::
528                     List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
529               | _ -> status
530            in
531            let coercions =
532             match obj with
533               _,_,_,_,NCic.Inductive
534                (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
535                ->
536                 HExtlib.filter_map
537                  (fun (name,is_coercion,arity) ->
538                    if is_coercion then Some(name,leftno,arity) else None) fields
539             | _ -> [] in
540            let status,uris =
541             List.fold_left
542              (fun (status,uris) (name,cpos,arity) ->
543                try
544                  let metasenv,subst,status,t =
545                   GrafiteDisambiguate.disambiguate_nterm None status [] [] []
546                    ("",0,CicNotationPt.Ident (name,None)) in
547                  assert (metasenv = [] && subst = []);
548                  let status, nuris = 
549                    NCicCoercDeclaration.
550                      basic_eval_and_record_ncoercion_from_t_cpos_arity 
551                       status (name,t,cpos,arity)
552                  in
553                  let uris = concat_nuris nuris uris in
554                  status, uris
555                with MultiPassDisambiguator.DisambiguationError _-> 
556                  HLog.warn ("error in generating coercion: "^name);
557                  status, uris) 
558              (status,uris) coercions
559            in
560             status,uris
561           with
562            exn ->
563             NCicLibrary.time_travel old_status;
564             raise exn)
565   | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
566      if status#ng_mode <> `CommandMode then
567       raise (GrafiteTypes.Command_error "Not in command mode")
568      else
569        let tgt_uri_ext, old_ok = 
570          match NCicEnvironment.get_checked_obj src_uri with
571          | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
572          | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
573          | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
574          | _ -> assert false
575        in
576        let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
577        let map = (src_uri, tgt_uri) :: map in
578        let ok = 
579          let rec subst () = function
580            | NCic.Meta _ -> assert false
581            | NCic.Const (NReference.Ref (u,spec)) as t ->
582                (try NCic.Const 
583                  (NReference.reference_of_spec (List.assoc u map)spec)
584                with Not_found -> t)
585            | t -> NCicUtils.map (fun _ _ -> ()) () subst t
586          in
587          NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
588        in
589        let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
590        let status = status#set_obj (tgt_uri,0,[],[],ok) in
591        (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
592        let status = status#set_stack ninitial_stack in
593        let status = subst_metasenv_and_fix_names status in
594        let status = status#set_ng_mode `ProofMode in
595        eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
596   | GrafiteAst.NObj (loc,obj) ->
597      if status#ng_mode <> `CommandMode then
598       raise (GrafiteTypes.Command_error "Not in command mode")
599      else
600       let status,obj =
601        GrafiteDisambiguate.disambiguate_nobj status
602         ~baseuri:status#baseuri (text,prefix_len,obj) in
603       let uri,height,nmenv,nsubst,nobj = obj in
604       let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
605       let status = status#set_obj obj in
606       let status = status#set_stack ninitial_stack in
607       let status = subst_metasenv_and_fix_names status in
608       let status = status#set_ng_mode `ProofMode in
609       (match nmenv with
610           [] ->
611            eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
612         | _ -> status,`New [])
613   | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
614       if status#ng_mode <> `CommandMode then
615         raise (GrafiteTypes.Command_error "Not in command mode")
616       else
617         let status = status#set_ng_mode `ProofMode in
618         let metasenv,subst,status,indty =
619           GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
620         let indtyno, (_,_,tys,_,_) = match indty with
621             NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
622               indtyno, NCicEnvironment.get_checked_indtys r
623           | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
624         let it = List.nth tys indtyno in
625         let status,obj =  NDestructTac.mk_discriminator it status in
626         let _,_,menv,_,_ = obj in
627           (match menv with
628                [] -> eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
629              | _ -> prerr_endline ("Discriminator: non empty metasenv");
630                     status, `New []) *)
631   | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
632      if status#ng_mode <> `CommandMode then
633       raise (GrafiteTypes.Command_error "Not in command mode")
634      else
635       let metasenv,subst,status,sort = match sort with
636         | None -> [],[],status,NCic.Sort NCic.Prop
637         | Some s -> GrafiteDisambiguate.disambiguate_nterm None status [] [] []
638                       (text,prefix_len,s) 
639       in
640       assert (metasenv = []);
641       let sort = NCicReduction.whd ~subst [] sort in
642       let sort = match sort with 
643           NCic.Sort s -> s
644         | _ ->  raise (Invalid_argument (Printf.sprintf "ninverter: found target %s, which is not a sort" 
645                                            (NCicPp.ppterm ~metasenv ~subst ~context:[] sort)))
646       in
647       let status = status#set_ng_mode `ProofMode in
648       let metasenv,subst,status,indty =
649        GrafiteDisambiguate.disambiguate_nterm None status [] [] subst (text,prefix_len,indty) in
650       let indtyno,(_,leftno,tys,_,_) = match indty with
651           NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) -> 
652             indtyno, NCicEnvironment.get_checked_indtys r
653         | _ -> prerr_endline ("engine: indty ="  ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] indty) ; assert false in
654       let it = List.nth tys indtyno in
655      let status,obj = NInversion.mk_inverter name true it leftno ?selection sort 
656                         status status#baseuri in
657      let _,_,menv,_,_ = obj in
658      (match menv with
659         [] ->
660           eval_ncommand opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
661       | _ -> assert false)
662   | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
663       eval_add_constraint status [`Type,u1] [`Type,u2]
664 ;;
665
666 let rec eval_command = {ec_go = fun ~disambiguate_command opts status
667 (text,prefix_len,cmd) ->
668  let status,cmd = disambiguate_command status (text,prefix_len,cmd) in
669  let status,uris =
670   match cmd with
671   | GrafiteAst.Default (loc, what, uris) as cmd ->
672      LibraryObjects.set_default what uris;
673      GrafiteTypes.add_moo_content [cmd] status,`New []
674   | GrafiteAst.Drop loc -> raise Drop
675   | GrafiteAst.Include (loc, mode, new_or_old, baseuri) ->
676      (* Old Include command is not recursive; new one is *)
677      let status =
678       if new_or_old = `OldAndNew then
679        let moopath_rw, moopath_r = 
680         LibraryMisc.obj_file_of_baseuri 
681           ~must_exist:false ~baseuri ~writable:true,
682         LibraryMisc.obj_file_of_baseuri 
683           ~must_exist:false ~baseuri ~writable:false in
684        let moopath = 
685         if Sys.file_exists moopath_r then moopath_r else
686           if Sys.file_exists moopath_rw then moopath_rw else
687             raise (IncludedFileNotCompiled (moopath_rw,baseuri))
688        in
689         eval_from_moo.efm_go status moopath
690       else
691        status
692      in
693       let status =
694        NCicLibrary.Serializer.require ~baseuri:(NUri.uri_of_string baseuri)
695         status in
696       let status =
697        GrafiteTypes.add_moo_content
698         [GrafiteAst.Include (loc,mode,`New,baseuri)] status
699       in
700        status,`New []
701   | GrafiteAst.Print (_,_) -> status,`New []
702   | GrafiteAst.Set (loc, name, value) -> status, `New []
703 (*       GrafiteTypes.set_option status name value,[] *)
704   | GrafiteAst.Obj (loc,obj) -> (* MATITA 1.0 *) assert false
705  in
706   status,uris
707
708 } and eval_executable = {ee_go = fun ~disambiguate_command
709 ~disambiguate_macro opts status (text,prefix_len,ex) ->
710   match ex with
711   | GrafiteAst.NTactic (_(*loc*), tacl) ->
712       if status#ng_mode <> `ProofMode then
713        raise (GrafiteTypes.Command_error "Not in proof mode")
714       else
715        let status =
716         List.fold_left 
717           (fun status tac ->
718             let status = eval_ng_tac (text,prefix_len,tac) status in
719             subst_metasenv_and_fix_names status)
720           status tacl
721        in
722         status,`New []
723   | GrafiteAst.Command (_, cmd) ->
724       eval_command.ec_go ~disambiguate_command opts status (text,prefix_len,cmd)
725   | GrafiteAst.NCommand (_, cmd) ->
726       eval_ncommand opts status (text,prefix_len,cmd)
727   | GrafiteAst.Macro (loc, macro) ->
728      raise (Macro (loc,disambiguate_macro status (text,prefix_len,macro)))
729   | GrafiteAst.NMacro (loc, macro) ->
730      raise (NMacro (loc,macro))
731
732 } and eval_from_moo = {efm_go = fun status fname ->
733   let ast_of_cmd cmd =
734     ("",0,GrafiteAst.Executable (HExtlib.dummy_floc,
735       GrafiteAst.Command (HExtlib.dummy_floc,
736         cmd)))
737   in
738   let moo = GrafiteMarshal.load_moo fname in
739   List.fold_left 
740     (fun status ast -> 
741       let ast = ast_of_cmd ast in
742       let status,lemmas =
743        eval_ast.ea_go
744          ~disambiguate_command:(fun status (_,_,cmd) -> status,cmd)
745          ~disambiguate_macro:(fun _ _ -> assert false)
746          status ast
747       in
748        assert (lemmas=`New []);
749        status)
750     status moo
751 } and eval_ast = {ea_go = fun ~disambiguate_command
752 ~disambiguate_macro ?(do_heavy_checks=false) status
753 (text,prefix_len,st)
754 ->
755   let opts = { do_heavy_checks = do_heavy_checks ; } in
756   match st with
757   | GrafiteAst.Executable (_,ex) ->
758      eval_executable.ee_go ~disambiguate_command
759       ~disambiguate_macro opts status (text,prefix_len,ex)
760   | GrafiteAst.Comment (_,c) -> 
761       eval_comment.ecm_go ~disambiguate_command opts status (text,prefix_len,c) 
762 } and eval_comment = { ecm_go = fun ~disambiguate_command opts status (text,prefix_len,c) -> 
763     status, `New []
764 }
765 ;;
766
767
768 let eval_ast = eval_ast.ea_go