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