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