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