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