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