src/HOL/Library/reflection.ML
changeset 31387 c4a3c3e9dc8e
parent 31386 8624b75a7784
child 31412 f2e6b6526092
     1.1 --- a/src/HOL/Library/reflection.ML	Tue Jun 02 18:38:13 2009 +0200
     1.2 +++ b/src/HOL/Library/reflection.ML	Wed Jun 03 11:33:16 2009 +0200
     1.3 @@ -28,7 +28,6 @@
     1.4        [|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn) *)
     1.5    (*  + the a list of names of the A1 .. An, Those are fresh in the ctxt*)
     1.6  
     1.7 -
     1.8  fun mk_congeq ctxt fs th =
     1.9    let
    1.10     val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
    1.11 @@ -79,6 +78,12 @@
    1.12  
    1.13  fun dest_listT (Type ("List.list", [T])) = T;
    1.14  
    1.15 +(* This modified version of divide_and_conquer allows the threading
    1.16 +   of a state variable *)
    1.17 +fun divide_and_conquer' decomp (s, x) =
    1.18 +  let val (ys, recomb) = decomp (s, x)
    1.19 +  in recomb (Library.foldl_map (divide_and_conquer' decomp) ys) end;
    1.20 +
    1.21  fun rearrange congs =
    1.22    let
    1.23      fun P (_, th) =
    1.24 @@ -89,23 +94,21 @@
    1.25  
    1.26  fun genreif ctxt raw_eqs t =
    1.27    let
    1.28 -    val bds = ref ([]: (typ * ((term list) * (term list))) list);
    1.29 -
    1.30 -    fun index_of t =
    1.31 +    fun index_of bds t =
    1.32        let
    1.33          val tt = HOLogic.listT (fastype_of t)
    1.34        in
    1.35 -       (case AList.lookup Type.could_unify (!bds) tt of
    1.36 +       (case AList.lookup Type.could_unify bds tt of
    1.37            NONE => error "index_of : type not found in environements!"
    1.38          | SOME (tbs,tats) =>
    1.39            let
    1.40              val i = find_index_eq t tats
    1.41              val j = find_index_eq t tbs
    1.42 -          in (if j= ~1 then
    1.43 -	      if i= ~1
    1.44 -              then (bds := AList.update Type.could_unify (tt,(tbs,tats@[t])) (!bds) ;
    1.45 +          in (if j = ~1 then
    1.46 +              if i = ~1
    1.47 +              then (AList.update Type.could_unify (tt,(tbs,tats@[t])) bds,
    1.48                      length tbs + length tats)
    1.49 -              else i else j)
    1.50 +              else (bds, i) else (bds, j))
    1.51            end)
    1.52        end;
    1.53  
    1.54 @@ -118,30 +121,31 @@
    1.55      (* da is the decomposition for atoms, ie. it returns ([],g) where g
    1.56         returns the right instance f (AtC n) = t , where AtC is the Atoms
    1.57         constructor and n is the number of the atom corresponding to t *)
    1.58 -    fun decomp_genreif da cgns (t,ctxt) =
    1.59 +    fun decomp_genreif da cgns (bds, (t,ctxt)) =
    1.60        let
    1.61          val thy = ProofContext.theory_of ctxt
    1.62          val cert = cterm_of thy
    1.63 -        fun tryabsdecomp (s,ctxt) =
    1.64 +        fun tryabsdecomp (bds, (s,ctxt)) =
    1.65            (case s of
    1.66               Abs(xn,xT,ta) => (
    1.67                 let
    1.68                   val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
    1.69                   val (xn,ta) = variant_abs (xn,xT,ta)
    1.70                   val x = Free(xn,xT)
    1.71 -                 val _ = (case AList.lookup Type.could_unify (!bds) (HOLogic.listT xT)
    1.72 +                 val bds = (case AList.lookup Type.could_unify bds (HOLogic.listT xT)
    1.73  		          of NONE => error "tryabsdecomp: Type not found in the Environement"
    1.74                             | SOME (bsT,atsT) =>
    1.75 -                             (bds := AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) (!bds)))
    1.76 -               in ([(ta, ctxt')] ,
    1.77 -                  fn [th] => ((let val (bsT,asT) = the(AList.lookup Type.could_unify (!bds) (HOLogic.listT xT))
    1.78 -		               in (bds := AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) (!bds))
    1.79 -		               end) ;
    1.80 -                              hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
    1.81 +                             (AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) bds))
    1.82 +               in ((bds, [(ta, ctxt')]),
    1.83 +                  fn (bds, [th]) => (
    1.84 +                    (let val (bsT,asT) = the(AList.lookup Type.could_unify bds (HOLogic.listT xT))
    1.85 +		     in AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) bds
    1.86 +		     end),
    1.87 +                    hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
    1.88                 end)
    1.89 -           | _ => da (s,ctxt))
    1.90 +           | _ => da (bds, (s,ctxt)))
    1.91        in (case cgns of
    1.92 -          [] => tryabsdecomp (t,ctxt)
    1.93 +          [] => tryabsdecomp (bds, (t,ctxt))
    1.94          | ((vns,cong)::congs) => ((let
    1.95              val cert = cterm_of thy
    1.96              val certy = ctyp_of thy
    1.97 @@ -154,13 +158,14 @@
    1.98  	      (map (snd o snd) fnvs,
    1.99                 map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
   1.100  	    val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
   1.101 -          in (fts ~~ (replicate (length fts) ctxt), FWD (instantiate (ctyenv, its) cong))
   1.102 +          in ((bds, fts ~~ (replicate (length fts) ctxt)),
   1.103 +              Library.apsnd (FWD (instantiate (ctyenv, its) cong)))
   1.104            end)
   1.105 -        handle MATCH => decomp_genreif da congs (t,ctxt)))
   1.106 +        handle MATCH => decomp_genreif da congs (bds, (t,ctxt))))
   1.107        end;
   1.108  
   1.109   (* looks for the atoms equation and instantiates it with the right number *)
   1.110 -    fun mk_decompatom eqs (t,ctxt) =
   1.111 +    fun mk_decompatom eqs (bds, (t,ctxt)) = ((bds, []), fn (bds, _) =>
   1.112        let
   1.113          val tT = fastype_of t
   1.114          fun isat eq =
   1.115 @@ -169,7 +174,7 @@
   1.116            in exists_Const
   1.117  	    (fn (n,ty) => n="List.nth"
   1.118                            andalso
   1.119 -			  AList.defined Type.could_unify (!bds) (domain_type ty)) rhs
   1.120 +			  AList.defined Type.could_unify bds (domain_type ty)) rhs
   1.121              andalso Type.could_unify (fastype_of rhs, tT)
   1.122            end
   1.123  
   1.124 @@ -181,8 +186,8 @@
   1.125            | _ => acc
   1.126  
   1.127          fun
   1.128 -           tryeqs [] = error "Can not find the atoms equation"
   1.129 -         | tryeqs (eq::eqs) = ((
   1.130 +           tryeqs bds [] = error "Can not find the atoms equation"
   1.131 +         | tryeqs bds (eq::eqs) = ((
   1.132            let
   1.133              val rhs = eq |> prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd
   1.134              val nths = get_nths rhs []
   1.135 @@ -206,10 +211,12 @@
   1.136                                 (Vartab.dest tyenv)
   1.137              val tml = Vartab.dest tmenv
   1.138              val t's = map (fn xn => snd (valOf (AList.lookup (op =) tml (xn,0)))) xns (* FIXME : Express with sbst*)
   1.139 -            val subst_ns = map (fn (Const _ $ vs $ n, Var (xn0,T)) =>
   1.140 -                                   (cert n, snd (valOf (AList.lookup (op =) tml xn0))
   1.141 -                                      |> (index_of #> HOLogic.mk_nat #> cert)))
   1.142 -                               subst
   1.143 +            val (bds, subst_ns) = Library.foldl_map
   1.144 +                (fn (bds, (Const _ $ vs $ n, Var (xn0,T))) =>
   1.145 +                  let
   1.146 +                    val name = snd (valOf (AList.lookup (op =) tml xn0))
   1.147 +                    val (bds, idx) = index_of bds name
   1.148 +                  in (bds, (cert n, idx |> (HOLogic.mk_nat #> cert))) end) (bds, subst)
   1.149              val subst_vs =
   1.150                let
   1.151                  fun ty (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = (certT T, certT (sbsT T))
   1.152 @@ -217,7 +224,7 @@
   1.153                    let
   1.154                      val cns = sbst (Const("List.list.Cons", T --> lT --> lT))
   1.155                      val lT' = sbsT lT
   1.156 -                    val (bsT,asT) = the (AList.lookup Type.could_unify (!bds) lT)
   1.157 +                    val (bsT,asT) = the (AList.lookup Type.could_unify bds lT)
   1.158                      val vsn = valOf (AList.lookup (op =) vsns_map vs)
   1.159                      val cvs = cert (fold_rev (fn x => fn xs => cns$x$xs) bsT (Free (vsn, lT')))
   1.160                    in (cert vs, cvs) end
   1.161 @@ -229,10 +236,9 @@
   1.162                let val ih = Drule.cterm_rule (Thm.instantiate (subst_ty,[]))
   1.163                in map (fn (v,t) => (ih v, ih t)) (subst_ns@subst_vs@cts)  end
   1.164              val th = (instantiate (subst_ty, substt)  eq) RS sym
   1.165 -          in hd (Variable.export ctxt'' ctxt [th]) end)
   1.166 -          handle MATCH => tryeqs eqs)
   1.167 -      in ([], fn _ => tryeqs (filter isat eqs))
   1.168 -      end;
   1.169 +          in (bds, hd (Variable.export ctxt'' ctxt [th])) end)
   1.170 +          handle MATCH => tryeqs bds eqs)
   1.171 +      in tryeqs bds (filter isat eqs) end);
   1.172  
   1.173    (* Generic reification procedure: *)
   1.174    (* creates all needed cong rules and then just uses the theorem synthesis *)
   1.175 @@ -245,7 +251,6 @@
   1.176                             |> fst)) raw_eqs []
   1.177          val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)
   1.178                              ) fs []
   1.179 -        val _ = bds := AList.make (fn _ => ([],[])) tys
   1.180          val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
   1.181          val thy = ProofContext.theory_of ctxt'
   1.182          val cert = cterm_of thy
   1.183 @@ -259,27 +264,28 @@
   1.184            in Thm.instantiate ([],subst) eq
   1.185            end
   1.186  
   1.187 +        val bds = AList.make (fn _ => ([],[])) tys
   1.188          val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop
   1.189    	                           |> HOLogic.dest_eq |> fst |> strip_comb |> snd |> tl
   1.190                                     |> (insteq eq)) raw_eqs
   1.191          val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
   1.192 -      in ps ~~ (Variable.export ctxt' ctxt congs)
   1.193 +      in (bds, ps ~~ (Variable.export ctxt' ctxt congs))
   1.194        end
   1.195  
   1.196 -    val congs = rearrange (mk_congs ctxt raw_eqs)
   1.197 -    val th = divide_and_conquer (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt)
   1.198 +    val (bds, congs) = mk_congs ctxt raw_eqs
   1.199 +    val congs = rearrange congs
   1.200 +    val (bds, th) = divide_and_conquer' (decomp_genreif (mk_decompatom raw_eqs) congs) (bds, (t,ctxt))
   1.201      fun is_listVar (Var (_,t)) = can dest_listT t
   1.202           | is_listVar _ = false
   1.203      val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   1.204  	          |> strip_comb |> snd |> filter is_listVar
   1.205      val cert = cterm_of (ProofContext.theory_of ctxt)
   1.206      val cvs = map (fn (v as Var(n,t)) => (cert v,
   1.207 -                  the (AList.lookup Type.could_unify (!bds) t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   1.208 +                  the (AList.lookup Type.could_unify bds t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   1.209      val th' = instantiate ([], cvs) th
   1.210      val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
   1.211      val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
   1.212  	       (fn _ => simp_tac (local_simpset_of ctxt) 1)
   1.213 -    val _ = bds := []
   1.214    in FWD trans [th'',th']
   1.215    end
   1.216