]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite_engine/grafiteEngine.ml
Huge change!!!
[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.NDestruct (_,dom,skip) -> NDestructTac.destruct_tac dom skip
347   | GrafiteAst.NDot _ -> NTactics.dot_tac 
348   | GrafiteAst.NElim (_loc, what, where) ->
349       NTactics.elim_tac 
350         ~what:(text,prefix_len,what)
351         ~where:(text,prefix_len,where)
352   | GrafiteAst.NFocus (_,l) -> NTactics.focus_tac l
353   | GrafiteAst.NGeneralize (_loc, where) -> 
354       NTactics.generalize_tac ~where:(text,prefix_len,where)
355   | GrafiteAst.NId _ -> (fun x -> x)
356   | GrafiteAst.NIntro (_loc,n) -> NTactics.intro_tac n
357   | GrafiteAst.NIntros (_loc,ns) -> NTactics.intros_tac ns
358   | GrafiteAst.NInversion (_loc, what, where) ->
359       NTactics.inversion_tac 
360         ~what:(text,prefix_len,what)
361         ~where:(text,prefix_len,where)
362   | GrafiteAst.NLApply (_loc, t) -> NTactics.lapply_tac (text,prefix_len,t) 
363   | GrafiteAst.NLetIn (_loc,where,what,name) ->
364       NTactics.letin_tac ~where:(text,prefix_len,where) 
365         ~what:(text,prefix_len,what) name
366   | GrafiteAst.NMerge _ -> NTactics.merge_tac 
367   | GrafiteAst.NPos (_,l) -> NTactics.pos_tac l
368   | GrafiteAst.NPosbyname (_,s) -> NTactics.case_tac s
369   | GrafiteAst.NReduce (_loc, reduction, where) ->
370       NTactics.reduce_tac ~reduction ~where:(text,prefix_len,where)
371   | GrafiteAst.NRewrite (_loc,dir,what,where) ->
372      NTactics.rewrite_tac ~dir ~what:(text,prefix_len,what)
373       ~where:(text,prefix_len,where)
374   | GrafiteAst.NSemicolon _ -> fun x -> x
375   | GrafiteAst.NShift _ -> NTactics.shift_tac 
376   | GrafiteAst.NSkip _ -> NTactics.skip_tac
377   | GrafiteAst.NUnfocus _ -> NTactics.unfocus_tac
378   | GrafiteAst.NWildcard _ -> NTactics.wildcard_tac 
379   | GrafiteAst.NTry (_,tac) -> NTactics.try_tac
380       (aux f (text, prefix_len, tac))
381   | GrafiteAst.NAssumption _ -> NTactics.assumption_tac
382   | GrafiteAst.NBlock (_,l) -> 
383       NTactics.block_tac (List.map (fun x -> aux f (text,prefix_len,x)) l)
384   |GrafiteAst.NRepeat (_,tac) ->
385       NTactics.repeat_tac (f f (text, prefix_len, tac))
386  in
387   aux aux tac (* trick for non uniform recursion call *)
388 ;;
389       
390 let subst_metasenv_and_fix_names status =
391   let u,h,metasenv, subst,o = status#obj in
392   let o = 
393     NCicUntrusted.map_obj_kind ~skip_body:true 
394      (NCicUntrusted.apply_subst subst []) o
395   in
396    status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
397 ;;
398
399
400 let rec eval_ncommand ~include_paths opts status (text,prefix_len,cmd) =
401   match cmd with
402   | GrafiteAst.Include (loc, mode, fname) ->
403            let _root, baseuri, _fullpath, _rrelpath = 
404        Librarian.baseuri_of_script ~include_paths fname in
405      let status,obj =
406        GrafiteTypes.Serializer.require ~baseuri:(NUri.uri_of_string baseuri)
407         status in
408      let status = status#set_dump (obj::status#dump) in
409      let status = status#set_dependencies (fname::status#dependencies) in
410      (*assert false;*) (*  MATITA 1.0mode must be passed to GrafiteTypes.Serializer.require
411      somehow *)
412        status
413   | GrafiteAst.UnificationHint (loc, t, n) -> eval_unification_hint status t n
414   | GrafiteAst.NCoercion (loc, name, t, ty, source, target) ->
415      let status, composites =
416       NCicCoercDeclaration.eval_ncoercion status name t ty source target
417      in
418       GrafiteDisambiguate.add_aliases_for_objs status composites
419   | GrafiteAst.NQed loc ->
420      if status#ng_mode <> `ProofMode then
421       raise (GrafiteTypes.Command_error "Not in proof mode")
422      else
423       let uri,height,menv,subst,obj_kind = status#obj in
424        if menv <> [] then
425         raise
426          (GrafiteTypes.Command_error"You can't Qed an incomplete theorem")
427        else
428         let obj_kind =
429          NCicUntrusted.map_obj_kind 
430           (NCicUntrusted.apply_subst subst []) obj_kind in
431         let height = NCicTypeChecker.height_of_obj_kind uri [] obj_kind in
432         (* fix the height inside the object *)
433         let rec fix () = function 
434           | NCic.Const (NReference.Ref (u,spec)) when NUri.eq u uri -> 
435              NCic.Const (NReference.reference_of_spec u
436               (match spec with
437               | NReference.Def _ -> NReference.Def height
438               | NReference.Fix (i,j,_) -> NReference.Fix(i,j,height)
439               | NReference.CoFix _ -> NReference.CoFix height
440               | NReference.Ind _ | NReference.Con _
441               | NReference.Decl as s -> s))
442           | t -> NCicUtils.map (fun _ () -> ()) () fix t
443         in
444         let obj_kind = 
445           match obj_kind with
446           | NCic.Fixpoint _ -> 
447               NCicUntrusted.map_obj_kind (fix ()) obj_kind 
448           | _ -> obj_kind
449         in
450         let obj = uri,height,[],[],obj_kind in
451         let old_status = status in
452         let status = NCicLibrary.add_obj status obj in
453         let index_obj =
454          match obj_kind with
455             NCic.Constant (_,_,_,_,(_,`Example,_))
456           | NCic.Fixpoint (_,_,(_,`Example,_)) -> false
457           | _ -> true
458         in
459         let status =
460          if index_obj then
461           let status = index_obj_for_auto status obj in
462            (try index_eq_for_auto status uri
463            with _ -> status)
464          else
465           status in
466 (*
467           try 
468             index_eq uri status
469           with _ -> prerr_endline "got an exception"; status
470         in *)
471 (*         prerr_endline (NCicPp.ppobj obj); *)
472         HLog.message ("New object: " ^ NUri.string_of_uri uri);
473          (try
474        (*prerr_endline (NCicPp.ppobj obj);*)
475            let boxml = NCicElim.mk_elims obj in
476            let boxml = boxml @ NCicElim.mk_projections obj in
477 (*
478            let objs = [] in
479            let timestamp,uris_rev =
480              List.fold_left
481               (fun (status,uris_rev) (uri,_,_,_,_) as obj ->
482                 let status = NCicLibrary.add_obj status obj in
483                  status,uri::uris_rev
484               ) (status,[]) objs in
485            let uris = uri::List.rev uris_rev in
486 *)
487            let status = status#set_ng_mode `CommandMode in
488            let status = GrafiteDisambiguate.add_aliases_for_objs status [uri] in
489            let status =
490             List.fold_left
491              (fun status boxml ->
492                try
493                 let nstatus =
494                  eval_ncommand ~include_paths opts status
495                   ("",0,GrafiteAst.NObj (HExtlib.dummy_floc,boxml))
496                 in
497                 if nstatus#ng_mode <> `CommandMode then
498                   begin
499                     (*HLog.warn "error in generating projection/eliminator";*)
500                     assert(status#ng_mode = `CommandMode);
501                     status
502                   end
503                 else
504                   nstatus
505                with
506                | MultiPassDisambiguator.DisambiguationError _
507                | NCicTypeChecker.TypeCheckerFailure _ ->
508                   (*HLog.warn "error in generating projection/eliminator";*)
509                   status
510              ) status boxml in             
511            let _,_,_,_,nobj = obj in 
512            let status = match nobj with
513                NCic.Inductive (is_ind,leftno,[it],_) ->
514                  let _,ind_name,ty,cl = it in
515                  List.fold_left 
516                    (fun status outsort ->
517                       let status = status#set_ng_mode `ProofMode in
518                       try
519                        (let status,invobj =
520                          NInversion.mk_inverter 
521                           (ind_name ^ "_inv_" ^
522                             (snd (NCicElim.ast_of_sort outsort)))
523                           is_ind it leftno outsort status status#baseuri in
524                        let _,_,menv,_,_ = invobj in
525                         (match menv with
526                              [] -> eval_ncommand ~include_paths opts status
527                                     ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
528                            | _ -> status))
529                        (* XXX *)
530                       with _ -> (*HLog.warn "error in generating inversion principle"; *)
531                                 let status = status#set_ng_mode `CommandMode in status)
532                   status
533                   (NCic.Prop::
534                     List.map (fun s -> NCic.Type s) (NCicEnvironment.get_universes ()))
535               | _ -> status
536            in
537            let coercions =
538             match obj with
539               _,_,_,_,NCic.Inductive
540                (true,leftno,[_,_,_,[_,_,_]],(_,`Record fields))
541                ->
542                 HExtlib.filter_map
543                  (fun (name,is_coercion,arity) ->
544                    if is_coercion then Some(name,leftno,arity) else None) fields
545             | _ -> [] in
546            let status =
547             List.fold_left
548              (fun status (name,cpos,arity) ->
549                try
550                  let metasenv,subst,status,t =
551                   GrafiteDisambiguate.disambiguate_nterm status None [] [] []
552                    ("",0,NotationPt.Ident (name,None)) in
553                  assert (metasenv = [] && subst = []);
554                  let status, nuris = 
555                    NCicCoercDeclaration.
556                      basic_eval_and_record_ncoercion_from_t_cpos_arity 
557                       status (name,t,cpos,arity)
558                  in
559                   GrafiteDisambiguate.add_aliases_for_objs status nuris
560                with MultiPassDisambiguator.DisambiguationError _-> 
561                  HLog.warn ("error in generating coercion: "^name);
562                  status) 
563              status coercions
564            in
565             status
566           with
567            exn ->
568             NCicLibrary.time_travel old_status;
569             raise exn)
570   | GrafiteAst.NCopy (log,tgt,src_uri, map) ->
571      if status#ng_mode <> `CommandMode then
572       raise (GrafiteTypes.Command_error "Not in command mode")
573      else
574        let tgt_uri_ext, old_ok = 
575          match NCicEnvironment.get_checked_obj src_uri with
576          | _,_,[],[], (NCic.Inductive _ as ok) -> ".ind", ok
577          | _,_,[],[], (NCic.Fixpoint _ as ok) -> ".con", ok
578          | _,_,[],[], (NCic.Constant _ as ok) -> ".con", ok
579          | _ -> assert false
580        in
581        let tgt_uri = NUri.uri_of_string (status#baseuri^"/"^tgt^tgt_uri_ext) in
582        let map = (src_uri, tgt_uri) :: map in
583        let ok = 
584          let rec subst () = function
585            | NCic.Meta _ -> assert false
586            | NCic.Const (NReference.Ref (u,spec)) as t ->
587                (try NCic.Const 
588                  (NReference.reference_of_spec (List.assoc u map)spec)
589                with Not_found -> t)
590            | t -> NCicUtils.map (fun _ _ -> ()) () subst t
591          in
592          NCicUntrusted.map_obj_kind ~skip_body:false (subst ()) old_ok
593        in
594        let ninitial_stack = Continuationals.Stack.of_nmetasenv [] in
595        let status = status#set_obj (tgt_uri,0,[],[],ok) in
596        (*prerr_endline (NCicPp.ppobj (tgt_uri,0,[],[],ok));*)
597        let status = status#set_stack ninitial_stack in
598        let status = subst_metasenv_and_fix_names status in
599        let status = status#set_ng_mode `ProofMode in
600        eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
601   | GrafiteAst.NObj (loc,obj) ->
602      if status#ng_mode <> `CommandMode then
603       raise (GrafiteTypes.Command_error "Not in command mode")
604      else
605       let status,obj =
606        GrafiteDisambiguate.disambiguate_nobj status
607         ~baseuri:status#baseuri (text,prefix_len,obj) in
608       let uri,height,nmenv,nsubst,nobj = obj in
609       let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
610       let status = status#set_obj obj in
611       let status = status#set_stack ninitial_stack in
612       let status = subst_metasenv_and_fix_names status in
613       let status = status#set_ng_mode `ProofMode in
614       (match nmenv with
615           [] ->
616            eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
617         | _ -> status)
618   | GrafiteAst.NDiscriminator (_,_) -> assert false (*(loc, indty) ->
619       if status#ng_mode <> `CommandMode then
620         raise (GrafiteTypes.Command_error "Not in command mode")
621       else
622         let status = status#set_ng_mode `ProofMode in
623         let metasenv,subst,status,indty =
624           GrafiteDisambiguate.disambiguate_nterm None status [] [] [] (text,prefix_len,indty) in
625         let indtyno, (_,_,tys,_,_) = match indty with
626             NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
627               indtyno, NCicEnvironment.get_checked_indtys r
628           | _ -> prerr_endline ("engine: indty expected... (fix this error message)"); assert false in
629         let it = List.nth tys indtyno in
630         let status,obj =  NDestructTac.mk_discriminator it status in
631         let _,_,menv,_,_ = obj in
632           (match menv with
633                [] -> eval_ncommand ~include_paths opts status ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
634              | _ -> prerr_endline ("Discriminator: non empty metasenv");
635                     status, []) *)
636   | GrafiteAst.NInverter (loc, name, indty, selection, sort) ->
637      if status#ng_mode <> `CommandMode then
638       raise (GrafiteTypes.Command_error "Not in command mode")
639      else
640       let metasenv,subst,status,sort = match sort with
641         | None -> [],[],status,NCic.Sort NCic.Prop
642         | Some s ->
643            GrafiteDisambiguate.disambiguate_nterm status None [] [] []
644             (text,prefix_len,s) 
645       in
646       assert (metasenv = []);
647       let sort = NCicReduction.whd ~subst [] sort in
648       let sort =
649        match sort with 
650           NCic.Sort s -> s
651         | _ ->
652            raise (Invalid_argument (Printf.sprintf
653             "ninverter: found target %s, which is not a sort"
654              (NCicPp.ppterm ~metasenv ~subst ~context:[] sort))) in
655       let status = status#set_ng_mode `ProofMode in
656       let metasenv,subst,status,indty =
657        GrafiteDisambiguate.disambiguate_nterm status None [] [] subst
658         (text,prefix_len,indty) in
659       let indtyno,(_,leftno,tys,_,_) =
660        match indty with
661           NCic.Const ((NReference.Ref (_,NReference.Ind (_,indtyno,_))) as r) ->
662            indtyno, NCicEnvironment.get_checked_indtys r
663         | _ ->
664           prerr_endline ("engine: indty ="  ^ NCicPp.ppterm ~metasenv:[]
665            ~subst:[] ~context:[] indty);
666           assert false in
667       let it = List.nth tys indtyno in
668       let status,obj =
669        NInversion.mk_inverter name true it leftno ?selection sort 
670         status status#baseuri in
671       let _,_,menv,_,_ = obj in
672        (match menv with
673           [] ->
674             eval_ncommand ~include_paths opts status
675              ("",0,GrafiteAst.NQed Stdpp.dummy_loc)
676         | _ -> assert false)
677   | GrafiteAst.NUnivConstraint (loc,u1,u2) ->
678       eval_add_constraint status [`Type,u1] [`Type,u2]
679   (* ex lexicon commands *)
680   | GrafiteAst.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
681      let cic_appl_pattern =
682       GrafiteDisambiguate.disambiguate_cic_appl_pattern status args
683        cic_appl_pattern
684      in
685       eval_interpretation status (dsc,(symbol, args),cic_appl_pattern)
686   | GrafiteAst.Notation (loc, dir, l1, associativity, precedence, l2) ->
687       let l1 = 
688         CicNotationParser.check_l1_pattern
689          l1 (dir = Some `RightToLeft) precedence associativity
690       in
691       let status =
692         if dir <> Some `RightToLeft then eval_input_notation status (l1,l2)
693         else status
694       in
695        if dir <> Some `LeftToRight then eval_output_notation status (l1,l2)
696        else status
697   | GrafiteAst.Alias (loc, spec) -> 
698      let diff =
699       (*CSC: Warning: this code should be factorized with the corresponding
700              code in DisambiguatePp *)
701       match spec with
702       | GrafiteAst.Ident_alias (id,uri) -> 
703          [DisambiguateTypes.Id id,spec]
704       | GrafiteAst.Symbol_alias (symb, instance, desc) ->
705          [DisambiguateTypes.Symbol (symb,instance),spec]
706       | GrafiteAst.Number_alias (instance,desc) ->
707          [DisambiguateTypes.Num instance,spec]
708      in
709       let mode = GrafiteAst.WithPreferences in(*assert false in (* VEDI SOPRA *) MATITA 1.0*)
710        eval_alias status (mode,diff)
711 ;;
712
713 let eval_comment opts status (text,prefix_len,c) = status
714
715 let rec eval_executable ~include_paths opts status (text,prefix_len,ex) =
716   match ex with
717   | GrafiteAst.NTactic (_(*loc*), tacl) ->
718       if status#ng_mode <> `ProofMode then
719        raise (GrafiteTypes.Command_error "Not in proof mode")
720       else
721        let status =
722         List.fold_left 
723           (fun status tac ->
724             let status = eval_ng_tac (text,prefix_len,tac) status in
725             subst_metasenv_and_fix_names status)
726           status tacl
727        in
728         status
729   | GrafiteAst.NCommand (_, cmd) ->
730       eval_ncommand ~include_paths opts status (text,prefix_len,cmd)
731   | GrafiteAst.NMacro (loc, macro) ->
732      raise (NMacro (loc,macro))
733
734 and eval_ast ~include_paths ?(do_heavy_checks=false) status (text,prefix_len,st)
735 =
736   let opts = { do_heavy_checks = do_heavy_checks ; } in
737   match st with
738   | GrafiteAst.Executable (_,ex) ->
739      eval_executable ~include_paths opts status (text,prefix_len,ex)
740   | GrafiteAst.Comment (_,c) -> 
741       eval_comment opts status (text,prefix_len,c) 
742 ;;