src/HOL/Library/reflection.ML
changeset 31386 8624b75a7784
parent 30969 fd9c89419358
child 31387 c4a3c3e9dc8e
     1.1 --- a/src/HOL/Library/reflection.ML	Wed Jun 03 07:51:11 2009 +1000
     1.2 +++ b/src/HOL/Library/reflection.ML	Tue Jun 02 18:38:13 2009 +0200
     1.3 @@ -29,18 +29,18 @@
     1.4    (*  + the a list of names of the A1 .. An, Those are fresh in the ctxt*)
     1.5  
     1.6  
     1.7 -fun mk_congeq ctxt fs th = 
     1.8 -  let 
     1.9 -   val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq 
    1.10 +fun mk_congeq ctxt fs th =
    1.11 +  let
    1.12 +   val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
    1.13       |> fst |> strip_comb |> fst
    1.14     val thy = ProofContext.theory_of ctxt
    1.15     val cert = Thm.cterm_of thy
    1.16     val (((_,_),[th']), ctxt') = Variable.import_thms true [th] ctxt
    1.17     val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th'))
    1.18 -   fun add_fterms (t as t1 $ t2) = 
    1.19 +   fun add_fterms (t as t1 $ t2) =
    1.20         if exists (fn f => Term.could_unify (t |> strip_comb |> fst, f)) fs then insert (op aconv) t
    1.21         else add_fterms t1 #> add_fterms t2
    1.22 -     | add_fterms (t as Abs(xn,xT,t')) = 
    1.23 +     | add_fterms (t as Abs(xn,xT,t')) =
    1.24         if exists_Const (fn (c, _) => c = fN) t then (fn _ => [t]) else (fn _ => [])
    1.25       | add_fterms _ = I
    1.26     val fterms = add_fterms rhs []
    1.27 @@ -48,7 +48,7 @@
    1.28     val tys = map fastype_of fterms
    1.29     val vs = map Free (xs ~~ tys)
    1.30     val env = fterms ~~ vs
    1.31 -		    (* FIXME!!!!*)	
    1.32 +		    (* FIXME!!!!*)
    1.33     fun replace_fterms (t as t1 $ t2) =
    1.34         (case AList.lookup (op aconv) env t of
    1.35  	    SOME v => v
    1.36 @@ -56,244 +56,246 @@
    1.37       | replace_fterms t = (case AList.lookup (op aconv) env t of
    1.38  			       SOME v => v
    1.39  			     | NONE => t)
    1.40 -      
    1.41 +
    1.42     fun mk_def (Abs(x,xT,t),v) = HOLogic.mk_Trueprop ((HOLogic.all_const xT)$ Abs(x,xT,HOLogic.mk_eq(v$(Bound 0), t)))
    1.43       | mk_def (t, v) = HOLogic.mk_Trueprop (HOLogic.mk_eq (v, t))
    1.44     fun tryext x = (x RS ext2 handle THM _ =>  x)
    1.45     val cong = (Goal.prove ctxt'' [] (map mk_def env)
    1.46  			  (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, replace_fterms rhs)))
    1.47 -			  (fn x => LocalDefs.unfold_tac (#context x) (map tryext (#prems x)) 
    1.48 +			  (fn x => LocalDefs.unfold_tac (#context x) (map tryext (#prems x))
    1.49  							THEN rtac th' 1)) RS sym
    1.50 -	      
    1.51 -   val (cong' :: vars') = 
    1.52 +
    1.53 +   val (cong' :: vars') =
    1.54         Variable.export ctxt'' ctxt (cong :: map (Drule.mk_term o cert) vs)
    1.55     val vs' = map (fst o fst o Term.dest_Var o Thm.term_of o Drule.dest_term) vars'
    1.56 -					      
    1.57 -  in  (vs', cong') end; 
    1.58 +
    1.59 +  in  (vs', cong') end;
    1.60   (* congs is a list of pairs (P,th) where th is a theorem for *)
    1.61          (* [| f p1 = A1; ...; f pn = An|] ==> f (C p1 .. pn) = P *)
    1.62  val FWD = curry (op OF);
    1.63  
    1.64 - (* da is the decomposition for atoms, ie. it returns ([],g) where g
    1.65 - returns the right instance f (AtC n) = t , where AtC is the Atoms
    1.66 - constructor and n is the number of the atom corresponding to t *)
    1.67 -
    1.68 -(* Generic decomp for reification : matches the actual term with the
    1.69 -rhs of one cong rule. The result of the matching guides the
    1.70 -proof synthesis: The matches of the introduced Variables A1 .. An are
    1.71 -processed recursively
    1.72 - The rest is instantiated in the cong rule,i.e. no reification is needed *)
    1.73  
    1.74  exception REIF of string;
    1.75  
    1.76  fun dest_listT (Type ("List.list", [T])) = T;
    1.77  
    1.78 -fun rearrange congs = 
    1.79 -let 
    1.80 - fun P (_, th) = 
    1.81 -  let val @{term "Trueprop"}$(Const ("op =",_) $l$_) = concl_of th
    1.82 -  in can dest_Var l end
    1.83 - val (yes,no) = List.partition P congs 
    1.84 - in no @ yes end
    1.85 +fun rearrange congs =
    1.86 +  let
    1.87 +    fun P (_, th) =
    1.88 +      let val @{term "Trueprop"}$(Const ("op =",_) $l$_) = concl_of th
    1.89 +      in can dest_Var l end
    1.90 +    val (yes,no) = List.partition P congs
    1.91 +  in no @ yes end
    1.92  
    1.93  fun genreif ctxt raw_eqs t =
    1.94 - let
    1.95 -val bds = ref ([]: (typ * ((term list) * (term list))) list);
    1.96 +  let
    1.97 +    val bds = ref ([]: (typ * ((term list) * (term list))) list);
    1.98  
    1.99 -fun index_of t = 
   1.100 - let 
   1.101 -  val tt = HOLogic.listT (fastype_of t)
   1.102 - in 
   1.103 -  (case AList.lookup Type.could_unify (!bds) tt of
   1.104 -    NONE => error "index_of : type not found in environements!"
   1.105 -  | SOME (tbs,tats) =>
   1.106 -    let
   1.107 -     val i = find_index_eq t tats
   1.108 -     val j = find_index_eq t tbs 
   1.109 -    in (if j= ~1 then 
   1.110 -	    if i= ~1 
   1.111 -	    then (bds := AList.update Type.could_unify (tt,(tbs,tats@[t])) (!bds) ; 
   1.112 -		  length tbs + length tats) 
   1.113 -	    else i else j)
   1.114 -    end)
   1.115 - end;
   1.116 +    fun index_of t =
   1.117 +      let
   1.118 +        val tt = HOLogic.listT (fastype_of t)
   1.119 +      in
   1.120 +       (case AList.lookup Type.could_unify (!bds) tt of
   1.121 +          NONE => error "index_of : type not found in environements!"
   1.122 +        | SOME (tbs,tats) =>
   1.123 +          let
   1.124 +            val i = find_index_eq t tats
   1.125 +            val j = find_index_eq t tbs
   1.126 +          in (if j= ~1 then
   1.127 +	      if i= ~1
   1.128 +              then (bds := AList.update Type.could_unify (tt,(tbs,tats@[t])) (!bds) ;
   1.129 +                    length tbs + length tats)
   1.130 +              else i else j)
   1.131 +          end)
   1.132 +      end;
   1.133 +
   1.134 +    (* Generic decomp for reification : matches the actual term with the
   1.135 +       rhs of one cong rule. The result of the matching guides the
   1.136 +       proof synthesis: The matches of the introduced Variables A1 .. An are
   1.137 +       processed recursively
   1.138 +       The rest is instantiated in the cong rule,i.e. no reification is needed *)
   1.139  
   1.140 -fun decomp_genreif da cgns (t,ctxt) =
   1.141 - let 
   1.142 -  val thy = ProofContext.theory_of ctxt 
   1.143 -  val cert = cterm_of thy
   1.144 -  fun tryabsdecomp (s,ctxt) = 
   1.145 -   (case s of 
   1.146 -     Abs(xn,xT,ta) => 
   1.147 -     (let
   1.148 -       val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
   1.149 -       val (xn,ta) = variant_abs (xn,xT,ta)
   1.150 -       val x = Free(xn,xT)
   1.151 -       val _ = (case AList.lookup Type.could_unify (!bds) (HOLogic.listT xT)
   1.152 -		 of NONE => error "tryabsdecomp: Type not found in the Environement"
   1.153 -		  | SOME (bsT,atsT) => 
   1.154 -		    (bds := AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) (!bds)))
   1.155 -      in ([(ta, ctxt')] , 
   1.156 -	  fn [th] => ((let val (bsT,asT) = the(AList.lookup Type.could_unify (!bds) (HOLogic.listT xT))
   1.157 -		       in (bds := AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) (!bds))
   1.158 -		       end) ; 
   1.159 -		      hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
   1.160 -	end)
   1.161 -    | _ => da (s,ctxt))
   1.162 -  in 
   1.163 -  (case cgns of 
   1.164 -    [] => tryabsdecomp (t,ctxt)
   1.165 -  | ((vns,cong)::congs) => ((let
   1.166 +    (* da is the decomposition for atoms, ie. it returns ([],g) where g
   1.167 +       returns the right instance f (AtC n) = t , where AtC is the Atoms
   1.168 +       constructor and n is the number of the atom corresponding to t *)
   1.169 +    fun decomp_genreif da cgns (t,ctxt) =
   1.170 +      let
   1.171 +        val thy = ProofContext.theory_of ctxt
   1.172          val cert = cterm_of thy
   1.173 -	val certy = ctyp_of thy
   1.174 -        val (tyenv, tmenv) =
   1.175 -        Pattern.match thy
   1.176 -        ((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (concl_of cong), t)
   1.177 -        (Envir.type_env (Envir.empty 0), Vartab.empty)
   1.178 -        val (fnvs,invs) = List.partition (fn ((vn,_),_) => vn mem vns) (Vartab.dest tmenv)
   1.179 -        val (fts,its) = 
   1.180 -	    (map (snd o snd) fnvs,
   1.181 -             map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
   1.182 -	val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
   1.183 -    in (fts ~~ (replicate (length fts) ctxt), FWD (instantiate (ctyenv, its) cong))
   1.184 -    end)
   1.185 -      handle MATCH => decomp_genreif da congs (t,ctxt)))
   1.186 -  end;
   1.187 +        fun tryabsdecomp (s,ctxt) =
   1.188 +          (case s of
   1.189 +             Abs(xn,xT,ta) => (
   1.190 +               let
   1.191 +                 val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
   1.192 +                 val (xn,ta) = variant_abs (xn,xT,ta)
   1.193 +                 val x = Free(xn,xT)
   1.194 +                 val _ = (case AList.lookup Type.could_unify (!bds) (HOLogic.listT xT)
   1.195 +		          of NONE => error "tryabsdecomp: Type not found in the Environement"
   1.196 +                           | SOME (bsT,atsT) =>
   1.197 +                             (bds := AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) (!bds)))
   1.198 +               in ([(ta, ctxt')] ,
   1.199 +                  fn [th] => ((let val (bsT,asT) = the(AList.lookup Type.could_unify (!bds) (HOLogic.listT xT))
   1.200 +		               in (bds := AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) (!bds))
   1.201 +		               end) ;
   1.202 +                              hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
   1.203 +               end)
   1.204 +           | _ => da (s,ctxt))
   1.205 +      in (case cgns of
   1.206 +          [] => tryabsdecomp (t,ctxt)
   1.207 +        | ((vns,cong)::congs) => ((let
   1.208 +            val cert = cterm_of thy
   1.209 +            val certy = ctyp_of thy
   1.210 +            val (tyenv, tmenv) =
   1.211 +              Pattern.match thy
   1.212 +              ((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (concl_of cong), t)
   1.213 +              (Envir.type_env (Envir.empty 0), Vartab.empty)
   1.214 +            val (fnvs,invs) = List.partition (fn ((vn,_),_) => vn mem vns) (Vartab.dest tmenv)
   1.215 +            val (fts,its) =
   1.216 +	      (map (snd o snd) fnvs,
   1.217 +               map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
   1.218 +	    val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
   1.219 +          in (fts ~~ (replicate (length fts) ctxt), FWD (instantiate (ctyenv, its) cong))
   1.220 +          end)
   1.221 +        handle MATCH => decomp_genreif da congs (t,ctxt)))
   1.222 +      end;
   1.223  
   1.224   (* looks for the atoms equation and instantiates it with the right number *)
   1.225 -fun mk_decompatom eqs (t,ctxt) =
   1.226 -let 
   1.227 - val tT = fastype_of t
   1.228 - fun isat eq = 
   1.229 -  let 
   1.230 -   val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   1.231 -   in exists_Const 
   1.232 -	  (fn (n,ty) => n="List.nth" 
   1.233 -			andalso 
   1.234 -			AList.defined Type.could_unify (!bds) (domain_type ty)) rhs 
   1.235 -	  andalso Type.could_unify (fastype_of rhs, tT)
   1.236 -   end
   1.237 - fun get_nths t acc = 
   1.238 -  case t of
   1.239 -    Const("List.nth",_)$vs$n => insert (fn ((a,_),(b,_)) => a aconv b) (t,(vs,n)) acc
   1.240 -  | t1$t2 => get_nths t1 (get_nths t2 acc)
   1.241 -  | Abs(_,_,t') => get_nths t'  acc
   1.242 -  | _ => acc
   1.243 +    fun mk_decompatom eqs (t,ctxt) =
   1.244 +      let
   1.245 +        val tT = fastype_of t
   1.246 +        fun isat eq =
   1.247 +          let
   1.248 +            val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   1.249 +          in exists_Const
   1.250 +	    (fn (n,ty) => n="List.nth"
   1.251 +                          andalso
   1.252 +			  AList.defined Type.could_unify (!bds) (domain_type ty)) rhs
   1.253 +            andalso Type.could_unify (fastype_of rhs, tT)
   1.254 +          end
   1.255 +
   1.256 +        fun get_nths t acc =
   1.257 +          case t of
   1.258 +            Const("List.nth",_)$vs$n => insert (fn ((a,_),(b,_)) => a aconv b) (t,(vs,n)) acc
   1.259 +          | t1$t2 => get_nths t1 (get_nths t2 acc)
   1.260 +          | Abs(_,_,t') => get_nths t'  acc
   1.261 +          | _ => acc
   1.262  
   1.263 - fun 
   1.264 -   tryeqs [] = error "Can not find the atoms equation"
   1.265 - | tryeqs (eq::eqs) = ((
   1.266 -  let 
   1.267 -   val rhs = eq |> prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd
   1.268 -   val nths = get_nths rhs []
   1.269 -   val (vss,ns) = fold_rev (fn (_,(vs,n)) => fn (vss,ns) => 
   1.270 -                             (insert (op aconv) vs vss, insert (op aconv) n ns)) nths ([],[]) 
   1.271 -   val (vsns, ctxt') = Variable.variant_fixes (replicate (length vss) "vs") ctxt
   1.272 -   val (xns, ctxt'') = Variable.variant_fixes (replicate (length nths) "x") ctxt' 
   1.273 -   val thy = ProofContext.theory_of ctxt''
   1.274 -   val cert = cterm_of thy
   1.275 -   val certT = ctyp_of thy
   1.276 -   val vsns_map = vss ~~ vsns
   1.277 -   val xns_map = (fst (split_list nths)) ~~ xns
   1.278 -   val subst = map (fn (nt, xn) => (nt, Var ((xn,0), fastype_of nt))) xns_map
   1.279 -   val rhs_P = subst_free subst rhs
   1.280 -   val (tyenv, tmenv) = Pattern.match 
   1.281 -	                    thy (rhs_P, t)
   1.282 -	                    (Envir.type_env (Envir.empty 0), Vartab.empty)
   1.283 -   val sbst = Envir.subst_vars (tyenv, tmenv)
   1.284 -   val sbsT = Envir.typ_subst_TVars tyenv
   1.285 -   val subst_ty = map (fn (n,(s,t)) => (certT (TVar (n, s)), certT t)) 
   1.286 -                      (Vartab.dest tyenv)
   1.287 -   val tml = Vartab.dest tmenv
   1.288 -   val t's = map (fn xn => snd (valOf (AList.lookup (op =) tml (xn,0)))) xns (* FIXME : Express with sbst*)
   1.289 -   val subst_ns = map (fn (Const _ $ vs $ n, Var (xn0,T)) => 
   1.290 -                          (cert n, snd (valOf (AList.lookup (op =) tml xn0)) 
   1.291 -                             |> (index_of #> HOLogic.mk_nat #> cert))) 
   1.292 -                      subst
   1.293 -   val subst_vs = 
   1.294 -    let 
   1.295 -     fun ty (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = (certT T, certT (sbsT T))
   1.296 -     fun h (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = 
   1.297 -      let 
   1.298 -       val cns = sbst (Const("List.list.Cons", T --> lT --> lT))
   1.299 -       val lT' = sbsT lT
   1.300 -       val (bsT,asT) = the (AList.lookup Type.could_unify (!bds) lT)
   1.301 -       val vsn = valOf (AList.lookup (op =) vsns_map vs)
   1.302 -       val cvs = cert (fold_rev (fn x => fn xs => cns$x$xs) bsT (Free (vsn, lT')))
   1.303 -      in (cert vs, cvs) end
   1.304 -    in map h subst end
   1.305 -   val cts = map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) 
   1.306 -                 (fold (AList.delete (fn (((a: string),_),(b,_)) => a = b)) 
   1.307 -                       (map (fn n => (n,0)) xns) tml)
   1.308 -   val substt = 
   1.309 -    let val ih = Drule.cterm_rule (Thm.instantiate (subst_ty,[]))
   1.310 -    in map (fn (v,t) => (ih v, ih t)) (subst_ns@subst_vs@cts)  end
   1.311 -   val th = (instantiate (subst_ty, substt)  eq) RS sym
   1.312 -  in  hd (Variable.export ctxt'' ctxt [th]) end)
   1.313 - handle MATCH => tryeqs eqs)
   1.314 -in ([], fn _ => tryeqs (filter isat eqs))
   1.315 -end;
   1.316 +        fun
   1.317 +           tryeqs [] = error "Can not find the atoms equation"
   1.318 +         | tryeqs (eq::eqs) = ((
   1.319 +          let
   1.320 +            val rhs = eq |> prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd
   1.321 +            val nths = get_nths rhs []
   1.322 +            val (vss,ns) = fold_rev (fn (_,(vs,n)) => fn (vss,ns) =>
   1.323 +                                      (insert (op aconv) vs vss, insert (op aconv) n ns)) nths ([],[])
   1.324 +            val (vsns, ctxt') = Variable.variant_fixes (replicate (length vss) "vs") ctxt
   1.325 +            val (xns, ctxt'') = Variable.variant_fixes (replicate (length nths) "x") ctxt'
   1.326 +            val thy = ProofContext.theory_of ctxt''
   1.327 +            val cert = cterm_of thy
   1.328 +            val certT = ctyp_of thy
   1.329 +            val vsns_map = vss ~~ vsns
   1.330 +            val xns_map = (fst (split_list nths)) ~~ xns
   1.331 +            val subst = map (fn (nt, xn) => (nt, Var ((xn,0), fastype_of nt))) xns_map
   1.332 +            val rhs_P = subst_free subst rhs
   1.333 +            val (tyenv, tmenv) = Pattern.match
   1.334 +                              thy (rhs_P, t)
   1.335 +                              (Envir.type_env (Envir.empty 0), Vartab.empty)
   1.336 +            val sbst = Envir.subst_vars (tyenv, tmenv)
   1.337 +            val sbsT = Envir.typ_subst_TVars tyenv
   1.338 +            val subst_ty = map (fn (n,(s,t)) => (certT (TVar (n, s)), certT t))
   1.339 +                               (Vartab.dest tyenv)
   1.340 +            val tml = Vartab.dest tmenv
   1.341 +            val t's = map (fn xn => snd (valOf (AList.lookup (op =) tml (xn,0)))) xns (* FIXME : Express with sbst*)
   1.342 +            val subst_ns = map (fn (Const _ $ vs $ n, Var (xn0,T)) =>
   1.343 +                                   (cert n, snd (valOf (AList.lookup (op =) tml xn0))
   1.344 +                                      |> (index_of #> HOLogic.mk_nat #> cert)))
   1.345 +                               subst
   1.346 +            val subst_vs =
   1.347 +              let
   1.348 +                fun ty (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = (certT T, certT (sbsT T))
   1.349 +                fun h (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) =
   1.350 +                  let
   1.351 +                    val cns = sbst (Const("List.list.Cons", T --> lT --> lT))
   1.352 +                    val lT' = sbsT lT
   1.353 +                    val (bsT,asT) = the (AList.lookup Type.could_unify (!bds) lT)
   1.354 +                    val vsn = valOf (AList.lookup (op =) vsns_map vs)
   1.355 +                    val cvs = cert (fold_rev (fn x => fn xs => cns$x$xs) bsT (Free (vsn, lT')))
   1.356 +                  in (cert vs, cvs) end
   1.357 +              in map h subst end
   1.358 +            val cts = map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t))
   1.359 +                          (fold (AList.delete (fn (((a: string),_),(b,_)) => a = b))
   1.360 +                                (map (fn n => (n,0)) xns) tml)
   1.361 +            val substt =
   1.362 +              let val ih = Drule.cterm_rule (Thm.instantiate (subst_ty,[]))
   1.363 +              in map (fn (v,t) => (ih v, ih t)) (subst_ns@subst_vs@cts)  end
   1.364 +            val th = (instantiate (subst_ty, substt)  eq) RS sym
   1.365 +          in hd (Variable.export ctxt'' ctxt [th]) end)
   1.366 +          handle MATCH => tryeqs eqs)
   1.367 +      in ([], fn _ => tryeqs (filter isat eqs))
   1.368 +      end;
   1.369  
   1.370    (* Generic reification procedure: *)
   1.371    (* creates all needed cong rules and then just uses the theorem synthesis *)
   1.372  
   1.373 -fun mk_congs ctxt raw_eqs = 
   1.374 -let
   1.375 -  val fs = fold_rev (fn eq =>
   1.376 -		     insert (op =) (eq |> prop_of |> HOLogic.dest_Trueprop 
   1.377 -			 |> HOLogic.dest_eq |> fst |> strip_comb 
   1.378 -			 |> fst)) raw_eqs []
   1.379 -  val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl) 
   1.380 -				    ) fs []
   1.381 -  val _ = bds := AList.make (fn _ => ([],[])) tys
   1.382 -  val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
   1.383 -  val thy = ProofContext.theory_of ctxt'
   1.384 -  val cert = cterm_of thy
   1.385 -  val vstys = map (fn (t,v) => (t,SOME (cert (Free(v,t))))) 
   1.386 -		  (tys ~~ vs)
   1.387 -  val is_Var = can dest_Var
   1.388 -  fun insteq eq vs = 
   1.389 -   let
   1.390 -     val subst = map (fn (v as Var(n,t)) => (cert v, (valOf o valOf) (AList.lookup (op =) vstys t)))  
   1.391 -  (filter is_Var vs)
   1.392 -   in Thm.instantiate ([],subst) eq
   1.393 -   end
   1.394 -  val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop 
   1.395 -			     |> HOLogic.dest_eq |> fst |> strip_comb |> snd |> tl
   1.396 -			     |> (insteq eq)) raw_eqs
   1.397 -  val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
   1.398 -in ps ~~ (Variable.export ctxt' ctxt congs)
   1.399 -end
   1.400 +    fun mk_congs ctxt raw_eqs =
   1.401 +      let
   1.402 +        val fs = fold_rev (fn eq =>
   1.403 +                           insert (op =) (eq |> prop_of |> HOLogic.dest_Trueprop
   1.404 +                           |> HOLogic.dest_eq |> fst |> strip_comb
   1.405 +                           |> fst)) raw_eqs []
   1.406 +        val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)
   1.407 +                            ) fs []
   1.408 +        val _ = bds := AList.make (fn _ => ([],[])) tys
   1.409 +        val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
   1.410 +        val thy = ProofContext.theory_of ctxt'
   1.411 +        val cert = cterm_of thy
   1.412 +        val vstys = map (fn (t,v) => (t,SOME (cert (Free(v,t)))))
   1.413 +                    (tys ~~ vs)
   1.414 +        val is_Var = can dest_Var
   1.415 +        fun insteq eq vs =
   1.416 +          let
   1.417 +            val subst = map (fn (v as Var(n,t)) => (cert v, (valOf o valOf) (AList.lookup (op =) vstys t)))
   1.418 +                        (filter is_Var vs)
   1.419 +          in Thm.instantiate ([],subst) eq
   1.420 +          end
   1.421  
   1.422 -  val congs = rearrange (mk_congs ctxt raw_eqs)
   1.423 -  val th = divide_and_conquer (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt)
   1.424 -  fun is_listVar (Var (_,t)) = can dest_listT t
   1.425 -       | is_listVar _ = false
   1.426 -  val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   1.427 -	       |> strip_comb |> snd |> filter is_listVar
   1.428 -  val cert = cterm_of (ProofContext.theory_of ctxt)
   1.429 -  val cvs = map (fn (v as Var(n,t)) => (cert v, the (AList.lookup Type.could_unify (!bds) t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   1.430 -  val th' = instantiate ([], cvs) th
   1.431 -  val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
   1.432 -  val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
   1.433 -			(fn _ => simp_tac (local_simpset_of ctxt) 1)
   1.434 -  val _ = bds := []
   1.435 -in FWD trans [th'',th']
   1.436 -end
   1.437 +        val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop
   1.438 +  	                           |> HOLogic.dest_eq |> fst |> strip_comb |> snd |> tl
   1.439 +                                   |> (insteq eq)) raw_eqs
   1.440 +        val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
   1.441 +      in ps ~~ (Variable.export ctxt' ctxt congs)
   1.442 +      end
   1.443 +
   1.444 +    val congs = rearrange (mk_congs ctxt raw_eqs)
   1.445 +    val th = divide_and_conquer (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt)
   1.446 +    fun is_listVar (Var (_,t)) = can dest_listT t
   1.447 +         | is_listVar _ = false
   1.448 +    val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   1.449 +	          |> strip_comb |> snd |> filter is_listVar
   1.450 +    val cert = cterm_of (ProofContext.theory_of ctxt)
   1.451 +    val cvs = map (fn (v as Var(n,t)) => (cert v,
   1.452 +                  the (AList.lookup Type.could_unify (!bds) t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   1.453 +    val th' = instantiate ([], cvs) th
   1.454 +    val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
   1.455 +    val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
   1.456 +	       (fn _ => simp_tac (local_simpset_of ctxt) 1)
   1.457 +    val _ = bds := []
   1.458 +  in FWD trans [th'',th']
   1.459 +  end
   1.460  
   1.461  
   1.462  fun genreflect ctxt conv corr_thms raw_eqs t =
   1.463 -let 
   1.464 -  val reifth = genreif ctxt raw_eqs t
   1.465 -  fun trytrans [] = error "No suitable correctness theorem found"
   1.466 -    | trytrans (th::ths) = 
   1.467 -         (FWD trans [reifth, th RS sym] handle THM _ => trytrans ths)
   1.468 -  val th = trytrans corr_thms
   1.469 -  val ft = (Thm.dest_arg1 o Thm.dest_arg o Thm.dest_arg o cprop_of) th
   1.470 -  val rth = conv ft
   1.471 -in simplify (HOL_basic_ss addsimps raw_eqs addsimps [nth_Cons_0, nth_Cons_Suc])
   1.472 -           (simplify (HOL_basic_ss addsimps [rth]) th)
   1.473 -end
   1.474 +  let
   1.475 +    val reifth = genreif ctxt raw_eqs t
   1.476 +    fun trytrans [] = error "No suitable correctness theorem found"
   1.477 +      | trytrans (th::ths) =
   1.478 +           (FWD trans [reifth, th RS sym] handle THM _ => trytrans ths)
   1.479 +    val th = trytrans corr_thms
   1.480 +    val ft = (Thm.dest_arg1 o Thm.dest_arg o Thm.dest_arg o cprop_of) th
   1.481 +    val rth = conv ft
   1.482 +  in simplify (HOL_basic_ss addsimps raw_eqs addsimps [nth_Cons_0, nth_Cons_Suc])
   1.483 +             (simplify (HOL_basic_ss addsimps [rth]) th)
   1.484 +  end
   1.485  
   1.486  fun genreify_tac ctxt eqs to i = (fn st =>
   1.487    let