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