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