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