]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
index d758c2ca686e99c55d9bde8e86d1cdd737e58fc4..f16c1a98cfb0a90214bd2b3fe4474f1f7682d1d2 100644 (file)
 module Ref = NReference
 
+type ctx = 
+  | Ce of NCic.hypothesis * NCic.obj list
+  | Fix of Ref.reference * string * NCic.term
+
+(***** A function to restrict the context of a term getting rid of unsed
+       variables *******)
+
+let restrict octx ctx ot =
+ let odummy = Cic.Implicit None in
+ let dummy = NCic.Meta (~-1,(0,NCic.Irl 0)) in
+ let rec aux m acc ot t =
+  function
+     [],[] -> (ot,t),acc
+   | ohe::otl as octx,he::tl ->
+      if CicTypeChecker.does_not_occur octx 0 1 ot then
+       aux (m+1) acc (CicSubstitution.subst odummy ot)
+        (NCicSubstitution.subst dummy t) (otl,tl)
+      else
+       (match ohe,he with
+           None,_ -> assert false
+         | Some (name,Cic.Decl oty),Ce ((name', NCic.Decl ty),objs) ->
+            aux (m+1) ((m+1,objs,None)::acc) (Cic.Lambda (name,oty,ot))
+             (NCic.Lambda (name',ty,t)) (otl,tl)
+         | Some (name,Cic.Decl oty),Fix (ref,name',ty) ->
+            aux (m+1) ((m+1,[],Some ref)::acc) (Cic.Lambda (name,oty,ot))
+             (NCic.Lambda (name',ty,t)) (otl,tl)
+         | Some (name,Cic.Def (obo,oty)),Ce ((name', NCic.Def (bo,ty)),objs) ->
+            aux (m+1) ((m+1,objs,None)::acc) (Cic.LetIn (name,obo,oty,ot))
+             (NCic.LetIn (name',bo,ty,t)) (otl,tl)
+         | _,_ -> assert false)
+   | _,_ -> assert false in
+ let rec split_lambdas_and_letins octx ctx infos (ote,te) =
+  match infos, ote, te with
+     ([], _, _) -> octx,ctx,ote
+   | ((_,objs,None)::tl, Cic.Lambda(name,oso,ota), NCic.Lambda(name',so,ta)) ->
+       split_lambdas_and_letins ((Some(name,(Cic.Decl oso)))::octx)
+        (Ce ((name',NCic.Decl so),objs)::ctx) tl (ota,ta)
+   | ((_,objs,Some r)::tl,Cic.Lambda(name,oso,ota),NCic.Lambda(name',so,ta)) ->
+       split_lambdas_and_letins ((Some(name,(Cic.Decl oso)))::octx)
+        (Fix (r,name',so)::ctx) tl (ota,ta)
+   | ((_,objs,None)::tl,Cic.LetIn(name,obo,oty,ota),NCic.LetIn(nam',bo,ty,ta))->
+       split_lambdas_and_letins ((Some (name,(Cic.Def (obo,oty))))::octx)
+        (Ce ((nam',NCic.Def (bo,ty)),objs)::ctx) tl (ota,ta)
+   | (_, _, _) -> assert false
+ in
+  let long_t,infos = aux 0 [] ot dummy (octx,ctx) in
+  let clean_octx,clean_ctx,clean_ot= split_lambdas_and_letins [] [] infos long_t
+  in
+(*prerr_endline ("RESTRICT PRIMA: " ^ CicPp.pp ot (List.map (function None -> None | Some (name,_) -> Some name) octx));
+prerr_endline ("RESTRICT DOPO: " ^ CicPp.pp clean_ot (List.map (function None -> None | Some (name,_) -> Some name) clean_octx));
+*)
+   clean_octx,clean_ctx,clean_ot, List.map (fun (rel,_,_) -> rel) infos
+;;
+
+
+(**** The translation itself ****)
+
 let cn_to_s = function
   | Cic.Anonymous -> "_"
   | Cic.Name s -> s
 ;;
 
-type ctx = 
-  | Ce of NCic.hypothesis 
-  | Fix of Ref.reference * string * NCic.term
-
 let splat mk_pi ctx t =
   List.fold_left
-    (fun t c -> 
+    (fun (t,l) c -> 
       match c with
-      | Ce (name, NCic.Def (bo,ty)) -> NCic.LetIn (name, ty, bo, t)
-      | Ce (name, NCic.Decl ty) when mk_pi -> NCic.Prod (name, ty, t)
-      | Ce (name, NCic.Decl ty) -> NCic.Lambda (name, ty, t)
-      | Fix (_,name,ty) when mk_pi -> NCic.Prod (name, ty, t)
-      | Fix (_,name,ty) -> NCic.Lambda (name,ty,t))
-    t ctx
+      | Ce ((name, NCic.Def (bo,ty)),l') -> NCic.LetIn (name, ty, bo, t),l@l'
+      | Ce ((name, NCic.Decl ty),l') when mk_pi -> NCic.Prod (name, ty, t),l@l'
+      | Ce ((name, NCic.Decl ty),l') -> NCic.Lambda (name, ty, t),l@l'
+      | Fix (_,name,ty) when mk_pi -> NCic.Prod (name, ty, t),l
+      | Fix (_,name,ty) -> NCic.Lambda (name,ty,t),l)
+    (t,[]) ctx
 ;;
 
 let context_tassonomy ctx = 
     let rec split inner acc acc1 = function 
       | Ce _ :: tl when inner -> split inner (acc+1) (acc1+1) tl
       | Fix _ ::tl -> split false acc (acc1+1) tl
-      | _ as l -> acc, List.length l, acc1
+      | _ as l ->
+        let only_decl =
+         List.filter
+          (function Ce ((_, NCic.Decl _),_) | Fix _ -> true | _ -> false) l
+        in
+         acc, List.length l, List.length only_decl, acc1
     in
       split true 0 1 ctx
 ;;
 
-let splat_args_for_rel ctx t = 
-  let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
+let splat_args_for_rel ctx t ?rels n_fix =
+  let rels =
+   match rels with
+      Some rels -> rels
+    | None ->
+       let rec mk_irl = function 0 -> [] | n -> n::mk_irl (n - 1) in
+        mk_irl (List.length ctx)
+  in
+  let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
   if free = 0 then t 
   else
     let rec aux = function
-      | 0 -> []
-      | n -> 
-         (match List.nth ctx (n+bound) with
-         | Fix (refe, _, _) when (n+bound) < primo_ce_dopo_fix -> NCic.Const refe
-         | Fix _ | Ce _ -> NCic.Rel (n+bound)) :: aux (n-1)
+      | n,_ when n = bound + n_fix -> []
+      | n,he::tl -> 
+         (match List.nth ctx (n-1) with
+          | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
+             NCic.Const refe :: aux (n-1,tl)
+          | Fix _ | Ce ((_, NCic.Decl _),_)-> NCic.Rel (he - n_fix)::aux(n-1,tl)
+          | Ce ((_, NCic.Def _),_) -> aux (n-1,tl))
+      | _,_ -> assert false
     in
-    NCic.Appl (t:: aux free)
+    NCic.Appl (t:: aux (List.length ctx,rels))
 ;;
 
-let splat_args ctx t n_fix 
-  let bound, free, primo_ce_dopo_fix = context_tassonomy ctx in
+let splat_args ctx t n_fix rels =
+  let bound, free, _, primo_ce_dopo_fix = context_tassonomy ctx in
   if ctx = [] then t
   else
    let rec aux = function
-     | 0 -> []
-     | n -> 
+     | 0,[] -> []
+     | n,he::tl -> 
         (match List.nth ctx (n-1) with
-         | Ce _ when n <= bound -> NCic.Rel n
+         | Ce ((_, NCic.Decl _),_) when n <= bound -> NCic.Rel he:: aux (n-1,tl)
          | Fix (refe, _, _) when n < primo_ce_dopo_fix ->
-            splat_args_for_rel ctx (NCic.Const refe)
-         | Fix _ | Ce _ -> NCic.Rel (n - n_fix)
-        ) :: aux (n-1)
+            splat_args_for_rel ctx (NCic.Const refe) ~rels n_fix :: aux (n-1,tl)
+         | Fix _ | Ce ((_, NCic.Decl _),_) -> NCic.Rel (he - n_fix)::aux(n-1,tl)
+         | Ce ((_, NCic.Def _),_) -> aux (n - 1,tl)
+        ) 
+     | _,_ -> assert false
+   in
+   NCic.Appl (t:: aux ((List.length ctx,rels)))
+;;
+
+exception Nothing_to_do;;
+
+let fix_outty curi tyno t context outty =
+ let leftno,rightno =
+  match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
+     Cic.InductiveDefinition (tyl,_,leftno,_) ->
+      let _,_,arity,_ = List.nth tyl tyno in
+      let rec count_prods leftno context arity =
+       match leftno, CicReduction.whd context arity with
+          0, Cic.Sort _ -> 0
+        | 0, Cic.Prod (name,so,ty) ->
+           1 + count_prods 0 (Some (name, Cic.Decl so)::context) ty
+        | n, Cic.Prod (name,so,ty) ->
+           count_prods (leftno - 1) (Some (name, Cic.Decl so)::context) ty
+        | _,_ -> assert false
+      in
+(*prerr_endline (UriManager.string_of_uri curi);
+prerr_endline ("LEFTNO: " ^ string_of_int leftno ^ "  " ^ CicPp.ppterm arity);*)
+       leftno, count_prods leftno [] arity
+   | _ -> assert false in
+ let ens,args =
+  let tty,_= CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
+  match CicReduction.whd context tty with
+     Cic.MutInd (_,_,ens) -> ens,[]
+   | Cic.Appl (Cic.MutInd (_,_,ens)::args) ->
+      ens,fst (HExtlib.split_nth leftno args)
+   | _ -> assert false
+ in
+  let rec aux n irl context outsort =
+   match n, CicReduction.whd context outsort with
+      0, Cic.Prod _ -> raise Nothing_to_do
+    | 0, _ ->
+       let irl = List.rev irl in
+       let ty = CicSubstitution.lift rightno (Cic.MutInd (curi,tyno,ens)) in
+       let ty =
+        if args = [] && irl = [] then ty
+        else
+         Cic.Appl (ty::(List.map (CicSubstitution.lift rightno) args)@irl) in
+       let he = CicSubstitution.lift (rightno + 1) outty in
+       let t =
+        if irl = [] then he
+        else Cic.Appl (he::List.map (CicSubstitution.lift 1) irl)
+       in
+        Cic.Lambda (Cic.Anonymous, ty, t)
+    | n, Cic.Prod (name,so,ty) ->
+       let ty' =
+        aux (n - 1) (Cic.Rel n::irl) (Some (name, Cic.Decl so)::context) ty
+       in
+        Cic.Lambda (name,so,ty')
+    | _,_ -> assert false
+  in
+(*prerr_endline ("RIGHTNO = " ^ string_of_int rightno ^ " OUTTY = " ^ CicPp.ppterm outty);*)
+   let outsort =
+    fst (CicTypeChecker.type_of_aux' [] context outty CicUniv.oblivion_ugraph)
    in
-   NCic.Appl (t:: aux (List.length ctx))
+    try aux rightno [] context outsort
+    with Nothing_to_do -> outty
+(*prerr_endline (CicPp.ppterm outty ^ " <==> " ^ CicPp.ppterm outty');*)
+;;
+
+let fix_outtype t =
+ let module C = Cic in
+ let rec aux context =
+  function
+     C.Rel _ as t -> t
+   | C.Var (uri,exp_named_subst) ->
+      let exp_named_subst' =
+       List.map (function i,t -> i, (aux context t)) exp_named_subst in
+        C.Var (uri,exp_named_subst')
+   | C.Implicit _
+   | C.Meta _ -> assert false
+   | C.Sort _ as t -> t
+   | C.Cast (v,t) -> C.Cast (aux context v, aux context t)
+   | C.Prod (n,s,t) ->
+        C.Prod (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
+   | C.Lambda (n,s,t) ->
+       C.Lambda (n, aux context s, aux ((Some (n, C.Decl s))::context) t)
+   | C.LetIn (n,s,ty,t) ->
+      C.LetIn
+       (n, aux context s, aux context ty,
+        aux ((Some (n, C.Def(s,ty)))::context) t)
+   | C.Appl l -> C.Appl (List.map (aux context) l)
+   | C.Const (uri,exp_named_subst) ->
+      let exp_named_subst' =
+       List.map (function i,t -> i, (aux context t)) exp_named_subst
+      in
+       C.Const (uri,exp_named_subst')
+   | C.MutInd (uri,tyno,exp_named_subst) ->
+      let exp_named_subst' =
+       List.map (function i,t -> i, (aux context t)) exp_named_subst
+      in
+       C.MutInd (uri, tyno, exp_named_subst')
+   | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
+      let exp_named_subst' =
+       List.map (function i,t -> i, (aux context t)) exp_named_subst
+      in
+       C.MutConstruct (uri, tyno, consno, exp_named_subst')
+   | C.MutCase (uri, tyno, outty, term, patterns) ->
+      let outty = fix_outty uri tyno term context outty in
+       C.MutCase (uri, tyno, aux context outty,
+        aux context term, List.map (aux context) patterns)
+   | C.Fix (funno, funs) ->
+      let tys,_ =
+        List.fold_left
+          (fun (types,len) (n,_,ty,_) ->
+            ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
+              len+1
+         ) ([],0) funs
+      in
+       C.Fix (funno,
+        List.map
+         (fun (name, indidx, ty, bo) ->
+           (name, indidx, aux context ty, aux (tys@context) bo)
+         ) funs
+      )
+   | C.CoFix (funno, funs) ->
+      let tys,_ =
+        List.fold_left
+          (fun (types,len) (n,ty,_) ->
+            ((Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
+              len+1
+         ) ([],0) funs
+      in
+       C.CoFix (funno,
+        List.map
+         (fun (name, ty, bo) ->
+           (name, aux context ty, aux (tys@context) bo)
+         ) funs
+       )
+ in
+  aux [] t
+;;
+
+let get_fresh,reset_seed =
+ let seed = ref 0 in
+  (function () ->
+   incr seed;
+   string_of_int !seed),
+  (function () -> seed := 0)
+;;
+
+exception Found of NReference.reference;;
+let cache = Hashtbl.create 313;; 
+let same_obj =
+ function
+    (_,_,_,_,NCic.Fixpoint (_,[_,_,_,ty1,_],_)),
+    (_,_,_,_,NCic.Fixpoint (_,[_,_,_,ty2,_],_))
+     when ty1 = ty2 -> true
+  | _ -> false
+;;
+let find_in_cache name obj ref =
+ try
+  List.iter
+   (function (ref',obj') ->
+     let recno =
+      match ref with
+         NReference.Ref (_,_,NReference.Fix (_,recno)) -> recno
+       | _ -> assert false in
+     let recno' =
+      match ref' with
+         NReference.Ref (_,_,NReference.Fix (_,recno)) -> recno
+       | _ -> assert false in
+     if recno = recno' && same_obj (obj,obj') then
+(*(prerr_endline "!!!!!!!!!!! CACHE HIT !!!!!!!!!!";*)
+       raise (Found ref')
+(*);*)
+(*
+     else
+(prerr_endline ("CACHE SAME NAME: " ^ NReference.string_of_reference ref ^ " <==> " ^ NReference.string_of_reference ref');
+      raise Not_found
+)
+*)
+  ) (Hashtbl.find_all cache name);
+(*prerr_endline "<<< CACHE MISS >>>";*)
+  Hashtbl.add cache name (ref,obj);
+  None
+ with Found ref -> Some ref
 ;;
 
 (* we are lambda-lifting also variables that do not occur *)
 (* ctx does not distinguish successive blocks of cofix, since there may be no
  *   lambda separating them *)
 let convert_term uri t = 
-  let rec aux octx (ctx : ctx list) n_fix uri = function
-    | Cic.CoFix (k, fl) ->
+  (* k=true if we are converting a term to be pushed in a ctx or if we are
+            converting the type of a fix;
+     k=false if we are converting a term to be put in the body of a fix;
+     in the latter case, we must permute Rels since the Fix abstraction will
+     preceed its lefts parameters; in the former case, there is nothing to
+     permute *)
+  let rec aux k octx (ctx : ctx list) n_fix uri = function
+    | Cic.CoFix _ as cofix ->
+        let octx,ctx,fix,rels = restrict octx ctx cofix in
+        let cofixno,fl =
+         match fix with Cic.CoFix (cofixno,fl)->cofixno,fl | _-> assert false in
         let buri = 
           UriManager.uri_of_string 
            (UriManager.buri_of_uri uri^"/"^
-            UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
+            UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
         in
         let bctx, fixpoints_tys, tys, _ = 
           List.fold_right 
-            (fun (name,ty,_) (ctx, fixpoints, tys, idx) -> 
-              let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
+            (fun (name,ty,_) (bctx, fixpoints, tys, idx) -> 
+              let ty, fixpoints_ty = aux true octx ctx n_fix uri ty in
               let r = Ref.reference_of_ouri buri(Ref.CoFix idx) in
-              Fix (r,name,ty) :: ctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
+              Fix (r,name,ty) :: bctx, fixpoints_ty @ fixpoints,ty::tys,idx+1)
             fl ([], [], [], 0)
         in
         let bctx = bctx @ ctx in
@@ -91,9 +349,11 @@ let convert_term uri t =
         let fl, fixpoints =
           List.fold_right2 
             (fun (name,_,bo) ty  (l,fixpoints) -> 
-               let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
-               (([],name,~-1,splat true ctx ty, splat false ctx bo)::l),
-               fixpoints_bo @ fixpoints)
+               let bo, fixpoints_bo = aux false boctx bctx n_fl buri bo in
+               let splty,fixpoints_splty = splat true ctx ty in
+               let splbo,fixpoints_splbo = splat false ctx bo in
+               (([],name,~-1,splty,splbo)::l),
+               fixpoints_bo @ fixpoints_splty @ fixpoints_splbo @ fixpoints)
             fl tys ([],fixpoints_tys)
         in
         let obj = 
@@ -101,30 +361,34 @@ let convert_term uri t =
             NCic.Fixpoint (false, fl, (`Generated, `Definition)) 
         in
         splat_args ctx 
-         (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix k)))
-         n_fix,
+         (NCic.Const (Ref.reference_of_ouri buri (Ref.CoFix cofixno)))
+         n_fix rels,
         fixpoints @ [obj]
-    | Cic.Fix (k, fl) ->
+    | Cic.Fix _ as fix ->
+        let octx,ctx,fix,rels = restrict octx ctx fix in
+        let fixno,fl =
+         match fix with Cic.Fix (fixno,fl) -> fixno,fl | _ -> assert false in
         let buri = 
           UriManager.uri_of_string 
            (UriManager.buri_of_uri uri^"/"^
-            UriManager.name_of_uri uri ^ string_of_int (List.length ctx)^".con")
+            UriManager.name_of_uri uri ^ "___" ^ get_fresh () ^ ".con")
         in
         let bad_bctx, fixpoints_tys, tys, _ = 
           List.fold_right 
             (fun (name,recno,ty,_) (bctx, fixpoints, tys, idx) -> 
-              let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
+              let ty, fixpoints_ty = aux true octx ctx n_fix uri ty in
               let r =  (* recno is dummy here, must be lifted by the ctx len *)
                 Ref.reference_of_ouri buri (Ref.Fix (idx,recno)) 
               in
               Fix (r,name,ty) :: bctx, fixpoints_ty@fixpoints,ty::tys,idx+1)
             fl ([], [], [], 0)
         in
-        let _, free, _ = context_tassonomy (bad_bctx @ ctx) in
+        let _, _, free_decls, _ = context_tassonomy (bad_bctx @ ctx) in
         let bctx = 
           List.map (function 
             | Fix (Ref.Ref (_,_,Ref.Fix (idx, recno)),name, ty) ->
-              Fix (Ref.reference_of_ouri buri(Ref.Fix (idx,recno+free)),name,ty)
+              Fix (Ref.reference_of_ouri buri
+                    (Ref.Fix (idx,recno+free_decls)),name,ty)
             | _ -> assert false) bad_bctx @ ctx
         in
         let n_fl = List.length fl in
@@ -134,57 +398,67 @@ let convert_term uri t =
              (Some (Cic.Name n,(Cic.Decl (CicSubstitution.lift len ty)))::types,
               len+1)) (octx,0) fl
         in
-        let rno_k = ref 0 in
+        let rno_fixno = ref 0 in
         let fl, fixpoints,_ =
           List.fold_right2 
             (fun (name,rno,_,bo) ty (l,fixpoints,idx) -> 
-               let bo, fixpoints_bo = aux boctx bctx n_fl buri bo in
-               let rno = rno + free in
-               if idx = k then rno_k := rno;
-               (([],name,rno,splat true ctx ty, splat false ctx bo)::l),
-               fixpoints_bo @ fixpoints,idx+1)
+               let bo, fixpoints_bo = aux false boctx bctx n_fl buri bo in
+               let splty,fixpoints_splty = splat true ctx ty in
+               let splbo,fixpoints_splbo = splat false ctx bo in
+               let rno = rno + free_decls in
+               if idx = fixno then rno_fixno := rno;
+               (([],name,rno,splty,splbo)::l),
+               fixpoints_bo@fixpoints_splty@fixpoints_splbo@fixpoints,idx+1)
             fl tys ([],fixpoints_tys,0)
         in
         let obj = 
           NUri.nuri_of_ouri buri,max_int,[],[],
-            NCic.Fixpoint (true, fl, (`Generated, `Definition)) 
+            NCic.Fixpoint (true, fl, (`Generated, `Definition)) in
+        let r = Ref.reference_of_ouri buri (Ref.Fix (fixno,!rno_fixno)) in
+        let obj,r =
+         let _,name,_,_,_ = List.hd fl in
+         match find_in_cache name obj r with
+            Some r' -> [],r'
+          | None -> [obj],r
         in
-        splat_args ctx
-          (NCic.Const 
-            (Ref.reference_of_ouri buri (Ref.Fix (k,!rno_k))))
-          n_fix,
-        fixpoints @ [obj]
+        splat_args ctx (NCic.Const r) n_fix rels, fixpoints @ obj
     | Cic.Rel n ->
-        let bound, _, primo_ce_dopo_fix = context_tassonomy ctx in
+        let bound, _, _, primo_ce_dopo_fix = context_tassonomy ctx in
         (match List.nth ctx (n-1) with
         | Fix (r,_,_) when n < primo_ce_dopo_fix -> 
-            splat_args_for_rel ctx (NCic.Const r), []
+            splat_args_for_rel ctx (NCic.Const r) n_fix, []
         | Ce _ when n <= bound -> NCic.Rel n, []
-        | Fix _ (* BUG 3 fix nested *) 
-        | Ce _ -> NCic.Rel (n-n_fix), [])
+        | Fix _ when n <= bound -> assert false
+        | Fix _ | Ce _ when k = true -> NCic.Rel n, []
+        | Fix _ | Ce _ -> NCic.Rel (n-n_fix), [])
     | Cic.Lambda (name, (s as old_s), t) ->
-        let s, fixpoints_s = aux octx ctx n_fix uri s in
-        let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
+        let s, fixpoints_s = aux k octx ctx n_fix uri s in
+        let s', fixpoints_s' = aux true octx ctx n_fix uri old_s in
+        let ctx = Ce ((cn_to_s name, NCic.Decl s'),fixpoints_s') :: ctx in
         let octx = Some (name, Cic.Decl old_s) :: octx in
-        let t, fixpoints_t = aux octx ctx n_fix uri t in
+        let t, fixpoints_t = aux octx ctx n_fix uri t in
         NCic.Lambda (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
     | Cic.Prod (name, (s as old_s), t) ->
-        let s, fixpoints_s = aux octx ctx n_fix uri s in
-        let ctx = Ce (cn_to_s name, NCic.Decl s) :: ctx in
+        let s, fixpoints_s = aux k octx ctx n_fix uri s in
+        let s', fixpoints_s' = aux true octx ctx n_fix uri old_s in
+        let ctx = Ce ((cn_to_s name, NCic.Decl s'),fixpoints_s') :: ctx in
         let octx = Some (name, Cic.Decl old_s) :: octx in
-        let t, fixpoints_t = aux octx ctx n_fix uri t in
+        let t, fixpoints_t = aux octx ctx n_fix uri t in
         NCic.Prod (cn_to_s name, s, t), fixpoints_s @ fixpoints_t
     | Cic.LetIn (name, (te as old_te), (ty as old_ty), t) ->
-        let te, fixpoints_s = aux octx ctx n_fix uri te in
-        let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
-        let ctx = Ce (cn_to_s name, NCic.Def (te, ty)) :: ctx in
+        let te, fixpoints_s = aux k octx ctx n_fix uri te in
+        let te', fixpoints_s' = aux true octx ctx n_fix uri old_te in
+        let ty, fixpoints_ty = aux k octx ctx n_fix uri ty in
+        let ty', fixpoints_ty' = aux true octx ctx n_fix uri old_ty in
+        let fixpoints' = fixpoints_s' @ fixpoints_ty' in
+        let ctx = Ce ((cn_to_s name, NCic.Def (te', ty')),fixpoints') :: ctx in
         let octx = Some (name, Cic.Def (old_te, old_ty)) :: octx in
-        let t, fixpoints_t = aux octx ctx n_fix uri t in
+        let t, fixpoints_t = aux octx ctx n_fix uri t in
         NCic.LetIn (cn_to_s name, ty, te, t), 
         fixpoints_s @ fixpoints_t @ fixpoints_ty
     | Cic.Cast (t,ty) ->
-        let t, fixpoints_t = aux octx ctx n_fix uri t in
-        let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
+        let t, fixpoints_t = aux octx ctx n_fix uri t in
+        let ty, fixpoints_ty = aux octx ctx n_fix uri ty in
         NCic.LetIn ("cast", ty, t, NCic.Rel 1), fixpoints_t @ fixpoints_ty
     | Cic.Sort Cic.Prop -> NCic.Sort NCic.Prop,[]
     | Cic.Sort Cic.CProp -> NCic.Sort NCic.CProp,[]
@@ -195,7 +469,7 @@ let convert_term uri t =
         let l, fixpoints =
           List.fold_right 
              (fun t (l,acc) -> 
-               let t, fixpoints = aux octx ctx n_fix uri t in 
+               let t, fixpoints = aux octx ctx n_fix uri t in 
                (t::l,fixpoints@acc))
              l ([],[])
         in
@@ -203,7 +477,7 @@ let convert_term uri t =
         | (NCic.Appl l1)::l2 -> NCic.Appl (l1@l2), fixpoints
         | _ -> NCic.Appl l, fixpoints)
     | Cic.Const (curi, ens) -> 
-       aux_ens octx ctx n_fix uri ens
+       aux_ens k curi octx ctx n_fix uri ens
         (match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
         | Cic.Constant (_,Some _,_,_,_) ->
                NCic.Const (Ref.reference_of_ouri curi Ref.Def)
@@ -211,58 +485,83 @@ let convert_term uri t =
                NCic.Const (Ref.reference_of_ouri curi Ref.Decl)
         | _ -> assert false)
     | Cic.MutInd (curi, tyno, ens) -> 
-       aux_ens octx ctx n_fix uri ens
+       aux_ens k curi octx ctx n_fix uri ens
         (NCic.Const (Ref.reference_of_ouri curi (Ref.Ind tyno)))
     | Cic.MutConstruct (curi, tyno, consno, ens) -> 
-       aux_ens octx ctx n_fix uri ens
+       aux_ens k curi octx ctx n_fix uri ens
         (NCic.Const (Ref.reference_of_ouri curi (Ref.Con (tyno,consno))))
-    | Cic.MutCase (curi, tyno, oty, t, branches) ->
+    | Cic.Var (curi, ens) ->
+       (match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
+           Cic.Variable (_,Some bo,_,_,_) ->
+            aux k octx ctx n_fix uri (CicSubstitution.subst_vars ens bo)
+         | _ -> assert false)
+    | Cic.MutCase (curi, tyno, outty, t, branches) ->
         let r = Ref.reference_of_ouri curi (Ref.Ind tyno) in
-        let oty, fixpoints_oty = aux octx ctx n_fix uri oty in
-        let t, fixpoints_t = aux octx ctx n_fix uri t in
+        let outty, fixpoints_outty = aux k octx ctx n_fix uri outty in
+        let t, fixpoints_t = aux octx ctx n_fix uri t in
         let branches, fixpoints =
           List.fold_right 
              (fun t (l,acc) -> 
-               let t, fixpoints = aux octx ctx n_fix uri t in 
+               let t, fixpoints = aux octx ctx n_fix uri t in 
                (t::l,fixpoints@acc))
              branches ([],[])
         in
-        NCic.Match (r,oty,t,branches), fixpoints_oty @ fixpoints_t @ fixpoints
-    | Cic.Implicit _ | Cic.Meta _ | Cic.Var _ -> assert false
-  and aux_ens octx ctx n_fix uri ens he =
+        NCic.Match (r,outty,t,branches), fixpoints_outty@fixpoints_t@fixpoints
+    | Cic.Implicit _ | Cic.Meta _ -> assert false
+  and aux_ens k curi octx ctx n_fix uri ens he =
    match ens with
       [] -> he,[]
     | _::_ ->
+      let params =
+       match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph curi) with
+          Cic.Constant (_,_,_,params,_)
+        | Cic.InductiveDefinition (_,params,_,_) -> params
+        | Cic.Variable _
+        | Cic.CurrentProof _ -> assert false
+      in
       let ens,objs =
        List.fold_right
-        (fun (_,t) (l,objs) ->
-          let t,o = aux octx ctx n_fix uri t in
-           t::l, o@objs
-        ) ens ([],[])
+        (fun luri (l,objs) ->
+          match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph luri) with
+             Cic.Variable (_,Some _,_,_,_) -> l, objs
+           | Cic.Variable (_,None,_,_,_) ->
+              let t = List.assoc luri ens in
+              let t,o = aux k octx ctx n_fix uri t in
+               t::l, o@objs
+           | _ -> assert false
+        ) params ([],[])
       in
        NCic.Appl (he::ens),objs
   in
-   aux [] [] 0 uri t
+   aux false [] [] 0 uri t
 ;;
 
 let cook mode vars t =
- let t = CicSubstitution.lift (List.length vars) t in
- snd (List.fold_right
-  (fun uri (n,t) ->
-    let t = CicSubstitution.subst_vars [uri,Cic.Rel 1] t in
+ let t = fix_outtype t in
+ let varsno = List.length vars in
+ let t = CicSubstitution.lift varsno t in
+ let rec aux n acc l =
+  let subst =
+   snd(List.fold_left (fun (i,res) uri -> i+1,(uri,Cic.Rel i)::res) (1,[]) acc)
+  in
+  match l with
+     [] -> CicSubstitution.subst_vars subst t
+   | uri::uris ->
     let bo,ty =
      match fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
-        Cic.Variable (_,bo,ty,_,_) -> bo,ty
+        Cic.Variable (_,bo,ty,_,_) ->
+         HExtlib.map_option fix_outtype bo, fix_outtype ty
       | _ -> assert false in
+    let ty = CicSubstitution.subst_vars subst ty in
+    let bo = HExtlib.map_option (CicSubstitution.subst_vars subst) bo in
     let id = Cic.Name (UriManager.name_of_uri uri) in
-    let t =
+    let t = aux (n-1) (uri::acc) uris in
      match bo,ty,mode with
         None,ty,`Lambda -> Cic.Lambda (id,ty,t)
       | None,ty,`Pi -> Cic.Prod (id,ty,t)
       | Some bo,ty,_ -> Cic.LetIn (id,bo,ty,t)
-    in
-     n+1,t
-  ) vars (1,t))
+ in
+  aux varsno [] vars
 ;;
 
 let convert_obj_aux uri = function
@@ -305,6 +604,7 @@ let convert_obj_aux uri = function
 ;;
 
 let convert_obj uri obj = 
+  reset_seed ();
   let o, fixpoints = convert_obj_aux uri obj in
   let obj = NUri.nuri_of_ouri uri,max_int, [], [], o in
   fixpoints @ [obj]