]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_engine/grafiteEngine.ml
1. bug in addition of universe constraints fixed: the recursive
[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 ~alias_only ~baseuri ~fname status
480   | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
481   | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
482      let status, composites =
483       NCicCoercDeclaration.eval_ncoercion status name t ty source target in
484      let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
485      let aliases = GrafiteDisambiguate.aliases_for_objs composites in
486       eval_alias status (mode,aliases)
487   | GrafiteAst.NQed loc ->
488      if status#ng_mode <> `ProofMode then
489       raise (GrafiteTypes.Command_error "Not in proof mode")
490      else
491       let uri,height,menv,subst,obj_kind = status#obj in
492        if menv <> [] then
493         raise
494          (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
495        else
496         let obj_kind =
497          NCicUntrusted.map_obj_kind 
498           (NCicUntrusted.apply_subst subst []) obj_kind in
499         let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
500         (* fix the height inside the object *)
501         let rec fix () = function 
502           | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri -> 
503              NCic.Const (NReference.reference_of_spec u
504               (match spec with
505               | NReference.Def _ -> NReference.Def height
506               | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
507               | NReference.CoFix _ -> NReference.CoFix height
508               | NReference.Ind _ | NReference.Con _
509               | NReference.Decl as s -> s))
510           | t -> NCicUtils.map (fun _ () -> ()) () fix t
511         in
512         let obj_kind = 
513           match obj_kind with
514           | NCic.Fixpoint _ -> 
515               NCicUntrusted.map_obj_kind (fix ()) obj_kind 
516           | _ -> obj_kind
517         in
518         let obj_kind = compute_relevance uri obj_kind in
519         let obj = uri,height,[],[],obj_kind in
520         let old_status = status in
521         let status = NCicLibrary.add_obj status obj in
522         let index_obj =
523          match obj_kind with
524             NCic.Constant (_,_,_,_,(_,`Example,_))
525           | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
526           | _ -> true
527         in
528         let status =
529          if index_obj then
530           let status = index_obj_for_auto status obj in
531            (try index_eq_for_auto status uri
532            with _ -> status)
533          else
534           status in
535 (*
536           try 
537             index_eq uri status
538           with _ -> prerr_endline "got an exception"; status
539         in *)
540 (*         prerr_endline (NCicPp.ppobj obj); *)
541         HLog.message ("New object: " ^ NUri.string_of_uri uri);
542          (try
543        (*prerr_endline (NCicPp.ppobj obj);*)
544            let boxml = NCicElim.mk_elims obj in
545            let boxml = boxml @ NCicElim.mk_projections obj in
546            let status = status#set_ng_mode `CommandMode in
547            let xxaliases = GrafiteDisambiguate.aliases_for_objs [uri] in
548            let mode = GrafiteAst.WithPreferences in (* MATITA 1.0: fixme *)
549            let status = eval_alias status (mode,xxaliases) in
550            let status =
551             List.fold_left
552              (fun status boxml ->
553                try
554                 let nstatus =
555                  eval_ncommand ~include_paths opts status
556                   ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
557                 in
558                 if nstatus#ng_mode <> `CommandMode then
559                   begin
560                     (*HLog.warn "error in generating projection/eliminator";*)
561                     assert(status#ng_mode = `CommandMode);
562                     status
563                   end
564                 else
565                   nstatus
566                with
567                | MultiPassDisambiguator.DisambiguationError _
568                | NCicTypeChecker.TypeCheckerFailure _ ->
569                   (*HLog.warn "error in generating projection/eliminator";*)
570                   status
571              ) status boxml in             
572            let _,_,_,_,nobj = obj in 
573            let status = match nobj with
574                NCic.Inductive (is_ind,leftno,[it],_) ->
575                  let _,ind_name,ty,cl = it in
576                  List.fold_left 
577                    (fun status outsort ->
578                       let status = status#set_ng_mode `ProofMode in
579                       try
580                        (let status,invobj =
581                          NInversion.mk_inverter 
582                           (ind_name ^ "_inv_" ^
583                             (snd (NCicElim.ast_of_sort outsort)))
584                           is_ind it leftno outsort status status#baseuri in
585                        let _,_,menv,_,_ = invobj in
586                         (match menv with
587                              [] -> eval_ncommand ~include_paths opts status
588                                     ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
589                            | _ -> status))
590                        (* XXX *)
591                       with _ -> (*HLog.warn "error in generating inversion principle"; *)
592                                 let status = status#set_ng_mode `CommandMode in status)
593                   status
594                   (NCic.Prop::
595                     List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
596               | _ -> status
597            in
598            let coercions =
599             match obj with
600               _,_,_,_,NCic.Inductive
601                (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
602                ->
603                 HExtlib.filter_map
604                  (fun (name,is_coercion,arity) ->
605                    if is_coercion then Some(name,leftno,arity) else None) fields
606             | _ -> [] in
607            let status =
608             List.fold_left
609              (fun status (name,cpos,arity) ->
610                try
611                  let metasenv,subst,status,t =
612                   GrafiteDisambiguate.disambiguate_nterm status None [] [] []
613                    ("",0,NotationPt.Ident (name,None)) in
614                  assert (metasenv = [] && subst = []);
615                  let status, nuris = 
616                    NCicCoercDeclaration.
617                      basic_eval_and_record_ncoercion_from_t_cpos_arity 
618                       status (name,t,cpos,arity) in
619                  let aliases = GrafiteDisambiguate.aliases_for_objs nuris in
620                   eval_alias status (mode,aliases)
621                with MultiPassDisambiguator.DisambiguationError _-> 
622                  HLog.warn ("error in generating coercion: "^name);
623                  status) 
624              status coercions
625            in
626             status
627           with
628            exn ->
629             NCicLibrary.time_travel old_status;
630             raise exn)
631   | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
632      if status#ng_mode <> `CommandMode then
633       raise (GrafiteTypes.Command_error "Not in command mode")
634      else
635        let tgt_uri_ext, old_ok = 
636          match NCicEnvironment.get_checked_obj src_uri with
637          | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
638          | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
639          | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
640          | _ -> assert false
641        in
642        let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
643        let map = (src_uri, tgt_uri) :: map in
644        let ok = 
645          let rec subst () = function
646            | NCic.Meta _ -> assert false
647            | NCic.Const (NReference.Ref (u,spec)) as t ->
648                (try NCic.Const 
649                  (NReference.reference_of_spec (List.assoc u map)spec)
650                with Not_found -> t)
651            | t -> NCicUtils.map (fun _ _ -> ()) () subst t
652          in
653          NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
654        in
655        let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
656        let status = status#set_obj (tgt_uri,0,[],[],ok) in
657        (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
658        let status = status#set_stack ninitial_stack in
659        let status = subst_metasenv_and_fix_names status in
660        let status = status#set_ng_mode `ProofMode in
661        eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
662   | GrafiteAst.NObj (loc,obj) ->
663      if status#ng_mode <> `CommandMode then
664       raise (GrafiteTypes.Command_error "Not in command mode")
665      else
666       let status,obj =
667        GrafiteDisambiguate.disambiguate_nobj status
668         ~baseuri:status#baseuri (text,prefix_len,obj) in
669       let uri,height,nmenv,nsubst,nobj = obj in
670       let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
671       let status = status#set_obj obj in
672       let status = status#set_stack ninitial_stack in
673       let status = subst_metasenv_and_fix_names status in
674       let status = status#set_ng_mode `ProofMode in
675       (match nmenv with
676           [] ->
677            eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
678         | _ -> status)
679   | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
680       if status#ng_mode <> `CommandMode then
681         raise (GrafiteTypes.Command_error "Not in command mode")
682       else
683         let status = status#set_ng_mode `ProofMode in
684         let metasenv,subst,status,indty =
685           GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
686         let indtyno, (_,_,tys,_,_) = match indty with
687             NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
688               indtyno, NCicEnvironment.get_checked_indtys r
689           | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
690         let it = List.nth tys indtyno in
691         let status,obj =  NDestructTac.mk_discriminator it status in
692         let _,_,menv,_,_ = obj in
693           (match menv with
694                [] -> eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
695              | _ -> prerr_endline ("Discriminator: non empty metasenv");
696                     status, []) *)
697   | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
698      if status#ng_mode <> `CommandMode then
699       raise (GrafiteTypes.Command_error "Not in command mode")
700      else
701       let metasenv,subst,status,sort = match sort with
702         | None -> [],[],status,NCic.Sort NCic.Prop
703         | Some s ->
704            GrafiteDisambiguate.disambiguate_nterm status None [] [] []
705             (text,prefix_len,s) 
706       in
707       assert (metasenv = []);
708       let sort = NCicReduction.whd ~subst [] sort in
709       let sort =
710        match sort with 
711           NCic.Sort s -> s
712         | _ ->
713            raise (Invalid_argument (Printf.sprintf
714             "ninverter: found target %s, which is not a sort"
715              (NCicPp.ppterm ~metasenv ~subst ~context:[] sort))) in
716       let status = status#set_ng_mode `ProofMode in
717       let metasenv,subst,status,indty =
718        GrafiteDisambiguate.disambiguate_nterm status None [] [] subst
719         (text,prefix_len,indty) in
720       let indtyno,(_,leftno,tys,_,_) =
721        match indty with
722           NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
723            indtyno, NCicEnvironment.get_checked_indtys r
724         | _ ->
725           prerr_endline ("engine: indty ="  ^ NCicPp.ppterm ~metasenv:[]
726            ~subst:[] ~context:[] indty);
727           assert false in
728       let it = List.nth tys indtyno in
729       let status,obj =
730        NInversion.mk_inverter name true it leftno ?selection sort 
731         status status#baseuri in
732       let _,_,menv,_,_ = obj in
733        (match menv with
734           [] ->
735             eval_ncommand ~include_paths opts status
736              ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
737         | _ -> assert false)
738   | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
739       eval_add_constraint status [`Type,u1] [`Type,u2]
740   (* ex lexicon commands *)
741   | GrafiteAst.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
742      let cic_appl_pattern =
743       GrafiteDisambiguate.disambiguate_cic_appl_pattern status args
744        cic_appl_pattern
745      in
746       eval_interpretation status (dsc,(symbol, args),cic_appl_pattern)
747   | GrafiteAst.Notation (loc, dir, l1, associativity, precedence, l2) ->
748       let l1 = 
749         CicNotationParser.check_l1_pattern
750          l1 (dir = Some `RightToLeft) precedence associativity
751       in
752       let status =
753         if dir <> Some `RightToLeft then eval_input_notation status (l1,l2)
754         else status
755       in
756        if dir <> Some `LeftToRight then eval_output_notation status (l1,l2)
757        else status
758   | GrafiteAst.Alias (loc, spec) -> 
759      let diff =
760       (*CSC: Warning: this code should be factorized with the corresponding
761              code in DisambiguatePp *)
762       match spec with
763       | GrafiteAst.Ident_alias (id,uri) -> 
764          [DisambiguateTypes.Id id,spec]
765       | GrafiteAst.Symbol_alias (symb, instance, desc) ->
766          [DisambiguateTypes.Symbol (symb,instance),spec]
767       | GrafiteAst.Number_alias (instance,desc) ->
768          [DisambiguateTypes.Num instance,spec]
769      in
770       let mode = GrafiteAst.WithPreferences in(*assert false in (* VEDI SOPRA *) MATITA 1.0*)
771        eval_alias status (mode,diff)
772 ;;
773
774 let eval_comment opts status (text,prefix_len,c) = status
775
776 let rec eval_executable ~include_paths opts status (text,prefix_len,ex) =
777   match ex with
778   | GrafiteAst.NTactic (_(*loc*), tacl) ->
779       if status#ng_mode <> `ProofMode then
780        raise (GrafiteTypes.Command_error "Not in proof mode")
781       else
782        let status =
783         List.fold_left 
784           (fun status tac ->
785             let status = eval_ng_tac (text,prefix_len,tac) status in
786             subst_metasenv_and_fix_names status)
787           status tacl
788        in
789         status
790   | GrafiteAst.NCommand (_, cmd) ->
791       eval_ncommand ~include_paths opts status (text,prefix_len,cmd)
792   | GrafiteAst.NMacro (loc, macro) ->
793      raise (NMacro (loc,macro))
794
795 and eval_ast ~include_paths ?(do_heavy_checks=false) status (text,prefix_len,st)
796 =
797   let opts = { do_heavy_checks = do_heavy_checks ; } in
798   match st with
799   | GrafiteAst.Executable (_,ex) ->
800      eval_executable ~include_paths opts status (text,prefix_len,ex)
801   | GrafiteAst.Comment (_,c) -> 
802       eval_comment opts status (text,prefix_len,c) 
803 ;;