let cleanup_tmp = true;;
 
-let never_trust = function uri -> false;;
-let always_trust = function uri -> true;;
-let trust_obj = ref never_trust;;
+let trust_obj = function uri -> true;;
 
 type type_checked_obj =
    CheckedObj of Cic.obj     (* cooked obj *)
 (* must be called once the type-checking of uri is finished *)
 (* The object whose uri is uri is unfreezed                 *)
 let set_type_checking_info uri =
- trust_obj := never_trust ;
  Cache.frozen_to_cooked uri
 ;;
 
 (* otherwise it freezes the term for type-checking and returns
  it *)
 (* set_type_checking_info must be called to unfreeze the term         *)
-let is_type_checked uri =
+let is_type_checked ?(trust=true) uri =
  try
   CheckedObj (Cache.find_cooked uri)
  with
   Not_found ->
    let obj = find_or_add_unchecked_to_cache uri in
     Cache.unchecked_to_frozen uri ;
-    if !trust_obj uri then
+    if trust && trust_obj uri then
      begin
       Logger.log (`Trusting uri) ;
       set_type_checking_info uri ;
-      trust_obj := always_trust ;
       CheckedObj (Cache.find_cooked uri)
      end
     else
-     begin
-      trust_obj := always_trust ;
-      UncheckedObj obj
-     end
+     UncheckedObj obj
 ;;
 
-(* get_cooked_obj uri                                                        *)
-(* returns the cooked cic object whose uri is uri. The term must be present  *)
-(* and cooked in cache                                                       *)
-let get_cooked_obj uri =
+(* get_cooked_obj ~trust uri *)
+(* returns the object if it is already type-checked or if it can be *)
+(* trusted (if [trust] = true and the trusting function accepts it) *)
+(* Otherwise it raises Not_found                                    *)
+let get_cooked_obj ?(trust=true) uri =
  try
   Cache.find_cooked uri
  with Not_found ->
-  if not (!trust_obj uri) then
+  if trust && trust_obj uri then
    begin
-    prerr_endline ("@@@ OOOOOOOPS: WE DO NOT TRUST " ^ UriManager.string_of_uri uri ^ " EVEN IF WE ARE REQUIRED TO DO THAT! THAT MAY MEAN LOOKING FOR TROUBLES ;-(") ;
-    raise Not_found
+    match is_type_checked uri with
+       CheckedObj obj -> obj
+     | _ -> assert false
    end
   else
-   match is_type_checked uri with
-      CheckedObj obj -> obj
-    | _ -> assert false
+   begin
+    prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
+    raise Not_found
+   end
 ;;
 
 (* get_obj uri                                                                *)
 
 (* otherwise it returns (false,object) and freeze the object for    *)
 (* type-checking                                                    *)
 (* set_type_checking_info must be called to unfreeze the object     *)
-val is_type_checked : UriManager.uri -> type_checked_obj
+val is_type_checked : ?trust:bool -> UriManager.uri -> type_checked_obj
 
 (* set_type_checking_info uri                                         *)
 (* must be called once the type-checking of uri is finished           *)
 (* again in the future (is_type_checked will return true)             *)
 val set_type_checking_info : UriManager.uri -> unit
 
-(* get_cooked_obj uri *)
-val get_cooked_obj : UriManager.uri -> Cic.obj
+(* get_cooked_obj ~trust uri *)
+(* returns the object if it is already type-checked or if it can be *)
+(* trusted (if [trust] = true and the trusting function accepts it) *)
+(* Otherwise it raises Not_found                                    *)
+val get_cooked_obj : ?trust:bool -> UriManager.uri -> Cic.obj
 
         CicSubstitution.lift m (List.assq uri ens)
        else
         let params =
-         (match CicEnvironment.get_cooked_obj uri with
+         (match CicEnvironment.get_obj uri with
              C.Constant _ -> raise ReferenceToConstant
            | C.Variable (_,_,_,params) -> params
            | C.CurrentProof _ -> raise ReferenceToCurrentProof
      | C.Appl l -> C.Appl (List.map (unwind_aux m) l)
      | C.Const (uri,exp_named_subst) ->
         let params =
-         (match CicEnvironment.get_cooked_obj uri with
+         (match CicEnvironment.get_obj uri with
              C.Constant (_,_,_,params) -> params
            | C.Variable _ -> raise ReferenceToVariable
            | C.CurrentProof (_,_,_,_,params) -> params
           C.Const (uri,exp_named_subst')
      | C.MutInd (uri,i,exp_named_subst) ->
         let params =
-         (match CicEnvironment.get_cooked_obj uri with
+         (match CicEnvironment.get_obj uri with
              C.Constant _ -> raise ReferenceToConstant
            | C.Variable _ -> raise ReferenceToVariable
            | C.CurrentProof _ -> raise ReferenceToCurrentProof
           C.MutInd (uri,i,exp_named_subst')
      | C.MutConstruct (uri,i,j,exp_named_subst) ->
         let params =
-         (match CicEnvironment.get_cooked_obj uri with
+         (match CicEnvironment.get_obj uri with
              C.Constant _ -> raise ReferenceToConstant
            | C.Variable _ -> raise ReferenceToVariable
            | C.CurrentProof _ -> raise ReferenceToCurrentProof
        if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
         reduce (0, [], [], List.assq uri ens, s)
        else
-        (match CicEnvironment.get_cooked_obj uri with
+        (match CicEnvironment.get_obj uri with
             C.Constant _ -> raise ReferenceToConstant
           | C.CurrentProof _ -> raise ReferenceToCurrentProof
           | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
        C.Appl (List.append (List.map (unwind k e ens) l) s)
 *)
    | (k, e, ens, (C.Const (uri,exp_named_subst) as t), s) ->
-      (match CicEnvironment.get_cooked_obj uri with
+      (match CicEnvironment.get_obj uri with
           C.Constant (_,Some body,_,_) ->
            let ens' = push_exp_named_subst k e ens exp_named_subst in
             (* constants are closed *)
    else
     begin
      debug t1 [t2] "PREWHD";
-     let t1' = whd context t1 
-     and t2' = whd context t2 in
+     let t1' = whd context t1 in
+     let t2' = whd context t2 in
       debug t1' [t2'] "POSTWHD";
       aux2 t1' t2'
     end
 
         | None -> raise RelToHiddenHypothesis
        )
     | C.Var (uri,exp_named_subst) as t ->
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            C.Constant _ -> raise ReferenceToConstant
          | C.CurrentProof _ -> raise ReferenceToCurrentProof
          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
     | C.Appl (he::tl) -> whdaux (tl@l) he
     | C.Appl [] -> raise (Impossible 1)
     | C.Const (uri,exp_named_subst) as t ->
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            C.Constant (_,Some body,_,_) ->
             whdaux l (CicSubstitution.subst_vars exp_named_subst body)
          | C.Constant _ -> if l = [] then t else C.Appl (t::l)
 
         with
          Not_found ->
           let params =
-           (match CicEnvironment.get_cooked_obj uri with
+           (match CicEnvironment.get_cooked_obj ~trust:true uri with
                C.Constant _ -> raise ReferenceToConstant
              | C.Variable (_,_,_,params) -> params
              | C.CurrentProof _ -> raise ReferenceToCurrentProof
     | C.Appl _ -> assert false
     | C.Const (uri,exp_named_subst')  ->
        let params =
-        (match CicEnvironment.get_cooked_obj uri with
+        (match CicEnvironment.get_cooked_obj ~trust:true uri with
             C.Constant (_,_,_,params) -> params
           | C.Variable _ -> raise ReferenceToVariable
           | C.CurrentProof (_,_,_,_,params) -> params
          C.Const (uri,exp_named_subst'')
     | C.MutInd (uri,typeno,exp_named_subst') ->
        let params =
-        (match CicEnvironment.get_cooked_obj uri with
+        (match CicEnvironment.get_cooked_obj ~trust:true uri with
             C.Constant _ -> raise ReferenceToConstant
           | C.Variable _ -> raise ReferenceToVariable
           | C.CurrentProof _ -> raise ReferenceToCurrentProof
          C.MutInd (uri,typeno,exp_named_subst'')
     | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
        let params =
-        (match CicEnvironment.get_cooked_obj uri with
+        (match CicEnvironment.get_cooked_obj ~trust:true uri with
             C.Constant _ -> raise ReferenceToConstant
           | C.Variable _ -> raise ReferenceToVariable
           | C.CurrentProof _ -> raise ReferenceToCurrentProof
 
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
        ) ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       match CicEnvironment.is_type_checked uri with
+       match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
   in
  let module R = CicReduction in
  let module U = UriManager in
   (* 0 because a variable is never cooked => no partial cooking at one level *)
-  match CicEnvironment.is_type_checked uri with
+  match CicEnvironment.is_type_checked ~trust:true uri with
      CicEnvironment.CheckedObj (C.Variable (_,_,ty,_)) -> ty
    | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty,_)) ->
       Logger.log (`Start_type_checking uri) ;
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
        check_mutual_inductive_defs uri uobj ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       (match CicEnvironment.is_type_checked uri with
+       (match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
        )
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
        check_mutual_inductive_defs uri uobj ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       (match CicEnvironment.is_type_checked uri with
+       (match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
        )
        List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
    | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
       let consty =
-       match CicEnvironment.get_cooked_obj uri with
+       match CicEnvironment.get_cooked_obj ~trust:false uri with
           C.InductiveDefinition (itl,_,_) ->
            let (_,_,_,cl) = List.nth itl i in
             let (_,cons) = List.nth cl (j - 1) in
 
         (* let's check if the type of branches are right *)
         let parsno =
-         match CicEnvironment.get_cooked_obj uri with
+         match CicEnvironment.get_cooked_obj ~trust:false uri with
             C.InductiveDefinition (_,_,parsno) -> parsno
           | _ ->
             raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
     | ((uri,t) as subst)::tl ->
        let typeofvar =
         CicSubstitution.subst_vars substs (type_of_variable uri) in
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            Cic.Variable (_,Some bo,_,_) ->
             raise
              (NotWellTyped
    match CicReduction.whd context ty with
       C.MutInd (uri,i,_) ->
        (*CSC: definire una funzioncina per questo codice sempre replicato *)
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            C.InductiveDefinition (itl,_,_) ->
             let (_,is_inductive,_,_) = List.nth itl i in
              if is_inductive then None else (Some uri)
  let module C = Cic in
  let module R = CicReduction in
  let module U = UriManager in
-  match CicEnvironment.is_type_checked uri with
+  match CicEnvironment.is_type_checked ~trust:false uri with
      CicEnvironment.CheckedObj _ -> ()
    | CicEnvironment.UncheckedObj uobj ->
       (* let's typecheck the uncooked object *)