]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/oCic2NCic.ml
The cache of objects is now used also for cofixpoints.
[helm.git] / helm / software / components / ng_kernel / oCic2NCic.ml
1 module Ref = NReference
2
3 type ctx = 
4   | Ce of NCic.hypothesis * NCic.obj list
5   | Fix of Ref.reference * string * NCic.term
6
7 (***** A function to restrict the context of a term getting rid of unsed
8        variables *******)
9
10 let restrict octx ctx ot =
11  let odummy = Cic.Implicit None in
12  let dummy = NCic.Meta (~-1,(0,NCic.Irl 0)) in
13  let rec aux m acc ot t =
14   function
15      [],[] -> (ot,t),acc
16    | ohe::otl as octx,he::tl ->
17       if CicTypeChecker.does_not_occur octx 0 1 ot then
18        aux (m+1) acc (CicSubstitution.subst odummy ot)
19         (NCicSubstitution.subst dummy t) (otl,tl)
20       else
21        (match ohe,he with
22            None,_ -> assert false
23          | Some (name,Cic.Decl oty),Ce ((name', NCic.Decl ty),objs) ->
24             aux (m+1) ((m+1,objs,None)::acc) (Cic.Lambda (name,oty,ot))
25              (NCic.Lambda (name',ty,t)) (otl,tl)
26          | Some (name,Cic.Decl oty),Fix (ref,name',ty) ->
27             aux (m+1) ((m+1,[],Some ref)::acc) (Cic.Lambda (name,oty,ot))
28              (NCic.Lambda (name',ty,t)) (otl,tl)
29          | Some (name,Cic.Def (obo,oty)),Ce ((name', NCic.Def (bo,ty)),objs) ->
30             aux (m+1) ((m+1,objs,None)::acc) (Cic.LetIn (name,obo,oty,ot))
31              (NCic.LetIn (name',bo,ty,t)) (otl,tl)
32          | _,_ -> assert false)
33    | _,_ -> assert false in
34  let rec split_lambdas_and_letins octx ctx infos (ote,te) =
35   match infos, ote, te with
36      ([], _, _) -> octx,ctx,ote
37    | ((_,objs,None)::tl, Cic.Lambda(name,oso,ota), NCic.Lambda(name',so,ta)) ->
38        split_lambdas_and_letins ((Some(name,(Cic.Decl oso)))::octx)
39         (Ce ((name',NCic.Decl so),objs)::ctx) tl (ota,ta)
40    | ((_,objs,Some r)::tl,Cic.Lambda(name,oso,ota),NCic.Lambda(name',so,ta)) ->
41        split_lambdas_and_letins ((Some(name,(Cic.Decl oso)))::octx)
42         (Fix (r,name',so)::ctx) tl (ota,ta)
43    | ((_,objs,None)::tl,Cic.LetIn(name,obo,oty,ota),NCic.LetIn(nam',bo,ty,ta))->
44        split_lambdas_and_letins ((Some (name,(Cic.Def (obo,oty))))::octx)
45         (Ce ((nam',NCic.Def (bo,ty)),objs)::ctx) tl (ota,ta)
46    | (_, _, _) -> assert false
47  in
48   let long_t,infos = aux 0 [] ot dummy (octx,ctx) in
49   let clean_octx,clean_ctx,clean_ot= split_lambdas_and_letins [] [] infos long_t
50   in
51 (*prerr_endline ("RESTRICT PRIMA: " ^ CicPp.pp ot (List.map (function None -> None | Some (name,_) -> Some name) octx));
52 prerr_endline ("RESTRICT DOPO: " ^ CicPp.pp clean_ot (List.map (function None -> None | Some (name,_) -> Some name) clean_octx));
53 *)
54    clean_octx,clean_ctx,clean_ot, List.map (fun (rel,_,_) -> rel) infos
55 ;;
56
57
58 (**** The translation itself ****)
59
60 let cn_to_s = function
61   | Cic.Anonymous -> "_"
62   | Cic.Name s -> s
63 ;;
64
65 let splat mk_pi ctx t =
66   List.fold_left
67     (fun (t,l) c -> 
68       match c with
69       | Ce ((name, NCic.Def (bo,ty)),l') -> NCic.LetIn (name, ty, bo, t),l@l'
70       | Ce ((name, NCic.Decl ty),l') when mk_pi -> NCic.Prod (name, ty, t),l@l'
71       | Ce ((name, NCic.Decl ty),l') -> NCic.Lambda (name, ty, t),l@l'
72       | Fix (_,name,ty) when mk_pi -> NCic.Prod (name, ty, t),l
73       | Fix (_,name,ty) -> NCic.Lambda (name,ty,t),l)
74     (t,[]) ctx
75 ;;
76
77 let context_tassonomy ctx = 
78     let rec split inner acc acc1 = function 
79       | Ce _ :: tl when inner -> split inner (acc+1) (acc1+1) tl
80       | Fix _ ::tl -> split false acc (acc1+1) tl
81       | _ as l ->
82         let only_decl =
83          List.filter
84           (function Ce ((_, NCic.Decl _),_) | Fix _ -> true | _ -> false) l
85         in
86          acc, List.length l, List.length only_decl, acc1
87     in
88       split true 0 1 ctx
89 ;;
90
91 let splat_args_for_rel ctx t ?rels n_fix =
92   let rels =
93    match rels with
94       Some rels -> rels
95     | None ->
96        let rec mk_irl = function 0 -> [] | n -> n::mk_irl (n - 1) in
97         mk_irl (List.length ctx)
98   in
99   let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
100   if free = 0 then t 
101   else
102     let rec aux = function
103       | n,_ when n = bound + n_fix -> []
104       | n,he::tl -> 
105          (match List.nth ctx (n-1) with
106           | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
107              NCic.Const refe :: aux (n-1,tl)
108           | Fix _ | Ce ((_, NCic.Decl _),_)-> NCic.Rel (he - n_fix)::aux(n-1,tl)
109           | Ce ((_, NCic.Def _),_) -> aux (n-1,tl))
110       | _,_ -> assert false
111     in
112     NCic.Appl (t:: aux (List.length ctx,rels))
113 ;;
114
115 let splat_args ctx t n_fix rels =
116   let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
117   if ctx = [] then t
118   else
119    let rec aux = function
120      | 0,[] -> []
121      | n,he::tl -> 
122         (match List.nth ctx (n-1) with
123          | Ce ((_, NCic.Decl _),_) when n <= bound -> NCic.Rel he:: aux (n-1,tl)
124          | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
125             splat_args_for_rel ctx (NCic.Const refe) ~rels n_fix :: aux (n-1,tl)
126          | Fix _ | Ce ((_, NCic.Decl _),_) -> NCic.Rel (he - n_fix)::aux(n-1,tl)
127          | Ce ((_, NCic.Def _),_) -> aux (n - 1,tl)
128         ) 
129      | _,_ -> assert false
130    in
131    NCic.Appl (t:: aux ((List.length ctx,rels)))
132 ;;
133
134 exception Nothing_to_do;;
135
136 let fix_outty curi tyno t context outty =
137  let leftno,rightno =
138   match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
139      Cic.InductiveDefinition (tyl,_,leftno,_) ->
140       let _,_,arity,_ = List.nth tyl tyno in
141       let rec count_prods leftno context arity =
142        match leftno, CicReduction.whd context arity with
143           0, Cic.Sort _ -> 0
144         | 0, Cic.Prod (name,so,ty) ->
145            1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
146         | n, Cic.Prod (name,so,ty) ->
147            count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
148         | _,_ -> assert false
149       in
150 (*prerr_endline (UriManager.string_of_uri curi);
151 prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ "  " ^ CicPp.ppterm arity);*)
152        leftno, count_prods leftno [] arity
153    | _ -> assert false in
154  let ens,args =
155   let tty,_= CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
156   match CicReduction.whd context tty with
157      Cic.MutInd (_,_,ens) -> ens,[]
158    | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
159       ens,fst (HExtlib.split_nth leftno args)
160    | _ -> assert false
161  in
162   let rec aux n irl context outsort =
163    match n, CicReduction.whd context outsort with
164       0, Cic.Prod _ -> raise Nothing_to_do
165     | 0, _ ->
166        let irl = List.rev irl in
167        let ty = CicSubstitution.lift rightno (Cic.MutInd (curi,tyno,ens)) in
168        let ty =
169         if args = [] && irl = [] then ty
170         else
171          Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
172        let he = CicSubstitution.lift (rightno + 1) outty in
173        let t =
174         if irl = [] then he
175         else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
176        in
177         Cic.Lambda (Cic.Anonymous, ty, t)
178     | n, Cic.Prod (name,so,ty) ->
179        let ty' =
180         aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
181        in
182         Cic.Lambda (name,so,ty')
183     | _,_ -> assert false
184   in
185 (*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
186    let outsort =
187     fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
188    in
189     try aux rightno [] context outsort
190     with Nothing_to_do -> outty
191 (*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
192 ;;
193
194 let fix_outtype t =
195  let module C = Cic in
196  let rec aux context =
197   function
198      C.Rel _ as t -> t
199    | C.Var (uri,exp_named_subst) ->
200       let exp_named_subst' =
201        List.map (function i,t -> i, (aux context t)) exp_named_subst in
202         C.Var (uri,exp_named_subst')
203    | C.Implicit _
204    | C.Meta _ -> assert false
205    | C.Sort _ as t -> t
206    | C.Cast (v,t) -> C.Cast (aux context v, aux context t)
207    | C.Prod (n,s,t) ->
208         C.Prod (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
209    | C.Lambda (n,s,t) ->
210        C.Lambda (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
211    | C.LetIn (n,s,ty,t) ->
212       C.LetIn
213        (n, aux context s, aux context ty,
214         aux ((Some (n, C.Def(s,ty)))::context) t)
215    | C.Appl l -> C.Appl (List.map (aux context) l)
216    | C.Const (uri,exp_named_subst) ->
217       let exp_named_subst' =
218        List.map (function i,t -> i, (aux context t)) exp_named_subst
219       in
220        C.Const (uri,exp_named_subst')
221    | C.MutInd (uri,tyno,exp_named_subst) ->
222       let exp_named_subst' =
223        List.map (function i,t -> i, (aux context t)) exp_named_subst
224       in
225        C.MutInd (uri, tyno, exp_named_subst')
226    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
227       let exp_named_subst' =
228        List.map (function i,t -> i, (aux context t)) exp_named_subst
229       in
230        C.MutConstruct (uri, tyno, consno, exp_named_subst')
231    | C.MutCase (uri, tyno, outty, term, patterns) ->
232       let outty = fix_outty uri tyno term context outty in
233        C.MutCase (uri, tyno, aux context outty,
234         aux context term, List.map (aux context) patterns)
235    | C.Fix (funno, funs) ->
236       let tys,_ =
237         List.fold_left
238           (fun (types,len) (n,_,ty,_) ->
239             ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
240               len+1
241           ) ([],0) funs
242       in
243        C.Fix (funno,
244         List.map
245          (fun (name, indidx, ty, bo) ->
246            (name, indidx, aux context ty, aux (tys@context) bo)
247          ) funs
248       )
249    | C.CoFix (funno, funs) ->
250       let tys,_ =
251         List.fold_left
252           (fun (types,len) (n,ty,_) ->
253             ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
254               len+1
255           ) ([],0) funs
256       in
257        C.CoFix (funno,
258         List.map
259          (fun (name, ty, bo) ->
260            (name, aux context ty, aux (tys@context) bo)
261          ) funs
262        )
263  in
264   aux [] t
265 ;;
266
267 let get_fresh,reset_seed =
268  let seed = ref 0 in
269   (function () ->
270    incr seed;
271    string_of_int !seed),
272   (function () -> seed := 0)
273 ;;
274
275 exception Found of NReference.reference;;
276 let cache = Hashtbl.create 313;; 
277 let same_obj =
278  function
279     (_,_,_,_,NCic.Fixpoint (_,[_,_,_,ty1,_],_)),
280     (_,_,_,_,NCic.Fixpoint (_,[_,_,_,ty2,_],_))
281      when ty1 = ty2 -> true
282   | _ -> false
283 ;;
284 let find_in_cache name obj ref =
285  try
286   List.iter
287    (function (ref',obj') ->
288      let recno =
289       match ref with
290          NReference.Ref (_,_,NReference.Fix (_,recno)) -> recno
291        | _ -> assert false in
292      let recno' =
293       match ref' with
294          NReference.Ref (_,_,NReference.Fix (_,recno)) -> recno
295        | _ -> assert false in
296      if recno = recno' && same_obj (obj,obj') then
297 (*(prerr_endline "!!!!!!!!!!! CACHE HIT !!!!!!!!!!";*)
298        raise (Found ref')
299 (*);*)
300 (*
301      else
302 (prerr_endline ("CACHE SAME NAME: " ^ NReference.string_of_reference ref ^ " <==> " ^ NReference.string_of_reference ref');
303       raise Not_found
304 )
305 *)
306   ) (Hashtbl.find_all cache name);
307 (*prerr_endline "<<< CACHE MISS >>>";*)
308   Hashtbl.add cache name (ref,obj);
309   None
310  with Found ref -> Some ref
311 ;;
312
313 (* we are lambda-lifting also variables that do not occur *)
314 (* ctx does not distinguish successive blocks of cofix, since there may be no
315  *   lambda separating them *)
316 let convert_term uri t = 
317   (* k=true if we are converting a term to be pushed in a ctx or if we are
318             converting the type of a fix;
319      k=false if we are converting a term to be put in the body of a fix;
320      in the latter case, we must permute Rels since the Fix abstraction will
321      preceed its lefts parameters; in the former case, there is nothing to
322      permute *)
323   let rec aux k octx (ctx : ctx list) n_fix uri = function
324     | Cic.CoFix _ as cofix ->
325         let octx,ctx,fix,rels = restrict octx ctx cofix in
326         let cofixno,fl =
327          match fix with Cic.CoFix (cofixno,fl)->cofixno,fl | _-> assert false in
328         let buri = 
329           UriManager.uri_of_string 
330            (UriManager.buri_of_uri uri^"/"^
331             UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
332         in
333         let bctx, fixpoints_tys, tys, _ = 
334           List.fold_right 
335             (fun (name,ty,_) (bctx, fixpoints, tys, idx) -> 
336               let ty, fixpoints_ty = aux true octx ctx n_fix uri ty in
337               let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
338               Fix (r,name,ty) :: bctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
339             fl ([], [], [], 0)
340         in
341         let bctx = bctx @ ctx in
342         let n_fl = List.length fl in
343         let boctx,_ =
344          List.fold_left
345           (fun (types,len) (n,ty,_) ->
346              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
347               len+1)) (octx,0) fl
348         in
349         let fl, fixpoints =
350           List.fold_right2 
351             (fun (name,_,bo) ty  (l,fixpoints) -> 
352                let bo, fixpoints_bo = aux false boctx bctx n_fl buri bo in
353                let splty,fixpoints_splty = splat true ctx ty in
354                let splbo,fixpoints_splbo = splat false ctx bo in
355                (([],name,~-1,splty,splbo)::l),
356                fixpoints_bo @ fixpoints_splty @ fixpoints_splbo @ fixpoints)
357             fl tys ([],fixpoints_tys)
358         in
359         let obj = 
360           NUri.nuri_of_ouri buri,0,[],[],
361             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
362         in
363         splat_args ctx 
364          (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix cofixno)))
365          n_fix rels,
366         fixpoints @ [obj]
367     | Cic.Fix _ as fix ->
368         let octx,ctx,fix,rels = restrict octx ctx fix in
369         let fixno,fl =
370          match fix with Cic.Fix (fixno,fl) -> fixno,fl | _ -> assert false in
371         let buri = 
372           UriManager.uri_of_string 
373            (UriManager.buri_of_uri uri^"/"^
374             UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
375         in
376         let bad_bctx, fixpoints_tys, tys, _ = 
377           List.fold_right 
378             (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) -> 
379               let ty, fixpoints_ty = aux true octx ctx n_fix uri ty in
380               let r =  (* recno is dummy here, must be lifted by the ctx len *)
381                 Ref.reference_of_ouri buri (Ref.Fix (idx,recno)) 
382               in
383               Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
384             fl ([], [], [], 0)
385         in
386         let _, _, free_decls, _ = context_tassonomy (bad_bctx @ ctx) in
387         let bctx = 
388           List.map (function 
389             | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
390               Fix (Ref.reference_of_ouri buri
391                     (Ref.Fix (idx,recno+free_decls)),name,ty)
392             | _ -> assert false) bad_bctx @ ctx
393         in
394         let n_fl = List.length fl in
395         let boctx,_ =
396          List.fold_left
397           (fun (types,len) (n,_,ty,_) ->
398              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
399               len+1)) (octx,0) fl
400         in
401         let rno_fixno = ref 0 in
402         let fl, fixpoints,_ =
403           List.fold_right2 
404             (fun (name,rno,_,bo) ty (l,fixpoints,idx) -> 
405                let bo, fixpoints_bo = aux false boctx bctx n_fl buri bo in
406                let splty,fixpoints_splty = splat true ctx ty in
407                let splbo,fixpoints_splbo = splat false ctx bo in
408                let rno = rno + free_decls in
409                if idx = fixno then rno_fixno := rno;
410                (([],name,rno,splty,splbo)::l),
411                fixpoints_bo@fixpoints_splty@fixpoints_splbo@fixpoints,idx+1)
412             fl tys ([],fixpoints_tys,0)
413         in
414         let obj = 
415           NUri.nuri_of_ouri buri,max_int,[],[],
416             NCic.Fixpoint (true, fl, (`Generated, `Definition)) in
417         let r = Ref.reference_of_ouri buri (Ref.Fix (fixno,!rno_fixno)) in
418         let obj,r =
419          let _,name,_,_,_ = List.hd fl in
420          match find_in_cache name obj r with
421             Some r' -> [],r'
422           | None -> [obj],r
423         in
424         splat_args ctx (NCic.Const r) n_fix rels, fixpoints @ obj
425     | Cic.Rel n ->
426         let bound, _, _, primo_ce_dopo_fix = context_tassonomy ctx in
427         (match List.nth ctx (n-1) with
428         | Fix (r,_,_) when n < primo_ce_dopo_fix -> 
429             splat_args_for_rel ctx (NCic.Const r) n_fix, []
430         | Ce _ when n <= bound -> NCic.Rel n, []
431         | Fix _ when n <= bound -> assert false
432         | Fix _ | Ce _ when k = true -> NCic.Rel n, []
433         | Fix _ | Ce _ -> NCic.Rel (n-n_fix), [])
434     | Cic.Lambda (name, (s as old_s), t) ->
435         let s, fixpoints_s = aux k octx ctx n_fix uri s in
436         let s', fixpoints_s' = aux true octx ctx n_fix uri old_s in
437         let ctx = Ce ((cn_to_s name, NCic.Decl s'),fixpoints_s') :: ctx in
438         let octx = Some (name, Cic.Decl old_s) :: octx in
439         let t, fixpoints_t = aux k octx ctx n_fix uri t in
440         NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
441     | Cic.Prod (name, (s as old_s), t) ->
442         let s, fixpoints_s = aux k octx ctx n_fix uri s in
443         let s', fixpoints_s' = aux true octx ctx n_fix uri old_s in
444         let ctx = Ce ((cn_to_s name, NCic.Decl s'),fixpoints_s') :: ctx in
445         let octx = Some (name, Cic.Decl old_s) :: octx in
446         let t, fixpoints_t = aux k octx ctx n_fix uri t in
447         NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
448     | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
449         let te, fixpoints_s = aux k octx ctx n_fix uri te in
450         let te', fixpoints_s' = aux true octx ctx n_fix uri old_te in
451         let ty, fixpoints_ty = aux k octx ctx n_fix uri ty in
452         let ty', fixpoints_ty' = aux true octx ctx n_fix uri old_ty in
453         let fixpoints' = fixpoints_s' @ fixpoints_ty' in
454         let ctx = Ce ((cn_to_s name, NCic.Def (te', ty')),fixpoints') :: ctx in
455         let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
456         let t, fixpoints_t = aux k octx ctx n_fix uri t in
457         NCic.LetIn (cn_to_s name, ty, te, t), 
458         fixpoints_s @ fixpoints_t @ fixpoints_ty
459     | Cic.Cast (t,ty) ->
460         let t, fixpoints_t = aux k octx ctx n_fix uri t in
461         let ty, fixpoints_ty = aux k octx ctx n_fix uri ty in
462         NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
463     | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
464     | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
465     | Cic.Sort (Cic.Type _) -> NCic.Sort (NCic.Type 0),[] 
466     | Cic.Sort Cic.Set -> NCic.Sort (NCic.Type 0),[] 
467        (* calculate depth in the univ_graph*)
468     | Cic.Appl l -> 
469         let l, fixpoints =
470           List.fold_right 
471              (fun t (l,acc) -> 
472                let t, fixpoints = aux k octx ctx n_fix uri t in 
473                (t::l,fixpoints@acc))
474              l ([],[])
475         in
476         (match l with
477         | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
478         | _ -> NCic.Appl l, fixpoints)
479     | Cic.Const (curi, ens) -> 
480        aux_ens k curi octx ctx n_fix uri ens
481         (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
482         | Cic.Constant (_,Some _,_,_,_) ->
483                NCic.Const (Ref.reference_of_ouri curi Ref.Def)
484         | Cic.Constant (_,None,_,_,_) ->
485                NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
486         | _ -> assert false)
487     | Cic.MutInd (curi, tyno, ens) -> 
488        aux_ens k curi octx ctx n_fix uri ens
489         (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
490     | Cic.MutConstruct (curi, tyno, consno, ens) -> 
491        aux_ens k curi octx ctx n_fix uri ens
492         (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
493     | Cic.Var (curi, ens) ->
494        (match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
495            Cic.Variable (_,Some bo,_,_,_) ->
496             aux k octx ctx n_fix uri (CicSubstitution.subst_vars ens bo)
497          | _ -> assert false)
498     | Cic.MutCase (curi, tyno, outty, t, branches) ->
499         let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
500         let outty, fixpoints_outty = aux k octx ctx n_fix uri outty in
501         let t, fixpoints_t = aux k octx ctx n_fix uri t in
502         let branches, fixpoints =
503           List.fold_right 
504              (fun t (l,acc) -> 
505                let t, fixpoints = aux k octx ctx n_fix uri t in 
506                (t::l,fixpoints@acc))
507              branches ([],[])
508         in
509         NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
510     | Cic.Implicit _ | Cic.Meta _ -> assert false
511   and aux_ens k curi octx ctx n_fix uri ens he =
512    match ens with
513       [] -> he,[]
514     | _::_ ->
515       let params =
516        match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
517           Cic.Constant (_,_,_,params,_)
518         | Cic.InductiveDefinition (_,params,_,_) -> params
519         | Cic.Variable _
520         | Cic.CurrentProof _ -> assert false
521       in
522       let ens,objs =
523        List.fold_right
524         (fun luri (l,objs) ->
525           match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph luri) with
526              Cic.Variable (_,Some _,_,_,_) -> l, objs
527            | Cic.Variable (_,None,_,_,_) ->
528               let t = List.assoc luri ens in
529               let t,o = aux k octx ctx n_fix uri t in
530                t::l, o@objs
531            | _ -> assert false
532         ) params ([],[])
533       in
534        NCic.Appl (he::ens),objs
535   in
536    aux false [] [] 0 uri t
537 ;;
538
539 let cook mode vars t =
540  let t = fix_outtype t in
541  let varsno = List.length vars in
542  let t = CicSubstitution.lift varsno t in
543  let rec aux n acc l =
544   let subst =
545    snd(List.fold_left (fun (i,res) uri -> i+1,(uri,Cic.Rel i)::res) (1,[]) acc)
546   in
547   match l with
548      [] -> CicSubstitution.subst_vars subst t
549    | uri::uris ->
550     let bo,ty =
551      match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
552         Cic.Variable (_,bo,ty,_,_) ->
553          HExtlib.map_option fix_outtype bo, fix_outtype ty
554       | _ -> assert false in
555     let ty = CicSubstitution.subst_vars subst ty in
556     let bo = HExtlib.map_option (CicSubstitution.subst_vars subst) bo in
557     let id = Cic.Name (UriManager.name_of_uri uri) in
558     let t = aux (n-1) (uri::acc) uris in
559      match bo,ty,mode with
560         None,ty,`Lambda -> Cic.Lambda (id,ty,t)
561       | None,ty,`Pi -> Cic.Prod (id,ty,t)
562       | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
563  in
564   aux varsno [] vars
565 ;;
566
567 let convert_obj_aux uri = function
568  | Cic.Constant (name, None, ty, vars, _) ->
569      let ty = cook `Pi vars ty in
570      let nty, fixpoints = convert_term uri ty in
571      assert(fixpoints = []);
572      NCic.Constant ([], name, None, nty, (`Provided,`Theorem,`Regular)),
573      fixpoints
574  | Cic.Constant (name, Some bo, ty, vars, _) ->
575      let bo = cook `Lambda vars bo in
576      let ty = cook `Pi vars ty in
577      let nbo, fixpoints_bo = convert_term uri bo in
578      let nty, fixpoints_ty = convert_term uri ty in
579      assert(fixpoints_ty = []);
580      NCic.Constant ([], name, Some nbo, nty, (`Provided,`Theorem,`Regular)),
581      fixpoints_bo @ fixpoints_ty
582  | Cic.InductiveDefinition (itl,vars,leftno,_) -> 
583      let ind = let _,x,_,_ = List.hd itl in x in
584      let itl, fix_itl = 
585        List.fold_right
586          (fun (name, _, ty, cl) (itl,acc) ->
587             let ty = cook `Pi vars ty in
588             let ty, fix_ty = convert_term uri ty in
589             let cl, fix_cl = 
590               List.fold_right
591                (fun (name, ty) (cl,acc) -> 
592                  let ty = cook `Pi vars ty in
593                  let ty, fix_ty = convert_term uri ty in
594                  ([], name, ty)::cl, acc @ fix_ty)
595                cl ([],[])
596             in
597             ([], name, ty, cl)::itl, fix_ty @ fix_cl @ acc)
598          itl ([],[])
599      in
600      NCic.Inductive(ind, leftno + List.length vars, itl, (`Provided, `Regular)),
601      fix_itl
602  | Cic.Variable _ 
603  | Cic.CurrentProof _ -> assert false
604 ;;
605
606 let convert_obj uri obj = 
607   reset_seed ();
608   let o, fixpoints = convert_obj_aux uri obj in
609   let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in
610   fixpoints @ [obj]
611 ;;