Removed usage of reference in reification
authorhoelzl
Wed Jun 03 11:33:16 2009 +0200 (2009-06-03)
changeset 31387c4a3c3e9dc8e
parent 31386 8624b75a7784
child 31408 9f2ca03ae7b7
Removed usage of reference in reification
src/HOL/Decision_Procs/ex/Approximation_Ex.thy
src/HOL/Library/reflection.ML
     1.1 --- a/src/HOL/Decision_Procs/ex/Approximation_Ex.thy	Tue Jun 02 18:38:13 2009 +0200
     1.2 +++ b/src/HOL/Decision_Procs/ex/Approximation_Ex.thy	Wed Jun 03 11:33:16 2009 +0200
     1.3 @@ -28,7 +28,7 @@
     1.4  
     1.5  lemma "\<bar> sqrt 2 - 1.4142135623730951 \<bar> < inverse 10 ^ 16"
     1.6    by (approximation 80)
     1.7 -   
     1.8 +
     1.9  lemma "\<bar> pi - 3.1415926535897932385 \<bar> < inverse 10 ^ 18"
    1.10    by (approximation 80)
    1.11  
     2.1 --- a/src/HOL/Library/reflection.ML	Tue Jun 02 18:38:13 2009 +0200
     2.2 +++ b/src/HOL/Library/reflection.ML	Wed Jun 03 11:33:16 2009 +0200
     2.3 @@ -28,7 +28,6 @@
     2.4        [|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn) *)
     2.5    (*  + the a list of names of the A1 .. An, Those are fresh in the ctxt*)
     2.6  
     2.7 -
     2.8  fun mk_congeq ctxt fs th =
     2.9    let
    2.10     val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
    2.11 @@ -79,6 +78,12 @@
    2.12  
    2.13  fun dest_listT (Type ("List.list", [T])) = T;
    2.14  
    2.15 +(* This modified version of divide_and_conquer allows the threading
    2.16 +   of a state variable *)
    2.17 +fun divide_and_conquer' decomp (s, x) =
    2.18 +  let val (ys, recomb) = decomp (s, x)
    2.19 +  in recomb (Library.foldl_map (divide_and_conquer' decomp) ys) end;
    2.20 +
    2.21  fun rearrange congs =
    2.22    let
    2.23      fun P (_, th) =
    2.24 @@ -89,23 +94,21 @@
    2.25  
    2.26  fun genreif ctxt raw_eqs t =
    2.27    let
    2.28 -    val bds = ref ([]: (typ * ((term list) * (term list))) list);
    2.29 -
    2.30 -    fun index_of t =
    2.31 +    fun index_of bds t =
    2.32        let
    2.33          val tt = HOLogic.listT (fastype_of t)
    2.34        in
    2.35 -       (case AList.lookup Type.could_unify (!bds) tt of
    2.36 +       (case AList.lookup Type.could_unify bds tt of
    2.37            NONE => error "index_of : type not found in environements!"
    2.38          | SOME (tbs,tats) =>
    2.39            let
    2.40              val i = find_index_eq t tats
    2.41              val j = find_index_eq t tbs
    2.42 -          in (if j= ~1 then
    2.43 -	      if i= ~1
    2.44 -              then (bds := AList.update Type.could_unify (tt,(tbs,tats@[t])) (!bds) ;
    2.45 +          in (if j = ~1 then
    2.46 +              if i = ~1
    2.47 +              then (AList.update Type.could_unify (tt,(tbs,tats@[t])) bds,
    2.48                      length tbs + length tats)
    2.49 -              else i else j)
    2.50 +              else (bds, i) else (bds, j))
    2.51            end)
    2.52        end;
    2.53  
    2.54 @@ -118,30 +121,31 @@
    2.55      (* da is the decomposition for atoms, ie. it returns ([],g) where g
    2.56         returns the right instance f (AtC n) = t , where AtC is the Atoms
    2.57         constructor and n is the number of the atom corresponding to t *)
    2.58 -    fun decomp_genreif da cgns (t,ctxt) =
    2.59 +    fun decomp_genreif da cgns (bds, (t,ctxt)) =
    2.60        let
    2.61          val thy = ProofContext.theory_of ctxt
    2.62          val cert = cterm_of thy
    2.63 -        fun tryabsdecomp (s,ctxt) =
    2.64 +        fun tryabsdecomp (bds, (s,ctxt)) =
    2.65            (case s of
    2.66               Abs(xn,xT,ta) => (
    2.67                 let
    2.68                   val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
    2.69                   val (xn,ta) = variant_abs (xn,xT,ta)
    2.70                   val x = Free(xn,xT)
    2.71 -                 val _ = (case AList.lookup Type.could_unify (!bds) (HOLogic.listT xT)
    2.72 +                 val bds = (case AList.lookup Type.could_unify bds (HOLogic.listT xT)
    2.73  		          of NONE => error "tryabsdecomp: Type not found in the Environement"
    2.74                             | SOME (bsT,atsT) =>
    2.75 -                             (bds := AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) (!bds)))
    2.76 -               in ([(ta, ctxt')] ,
    2.77 -                  fn [th] => ((let val (bsT,asT) = the(AList.lookup Type.could_unify (!bds) (HOLogic.listT xT))
    2.78 -		               in (bds := AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) (!bds))
    2.79 -		               end) ;
    2.80 -                              hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
    2.81 +                             (AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) bds))
    2.82 +               in ((bds, [(ta, ctxt')]),
    2.83 +                  fn (bds, [th]) => (
    2.84 +                    (let val (bsT,asT) = the(AList.lookup Type.could_unify bds (HOLogic.listT xT))
    2.85 +		     in AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) bds
    2.86 +		     end),
    2.87 +                    hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
    2.88                 end)
    2.89 -           | _ => da (s,ctxt))
    2.90 +           | _ => da (bds, (s,ctxt)))
    2.91        in (case cgns of
    2.92 -          [] => tryabsdecomp (t,ctxt)
    2.93 +          [] => tryabsdecomp (bds, (t,ctxt))
    2.94          | ((vns,cong)::congs) => ((let
    2.95              val cert = cterm_of thy
    2.96              val certy = ctyp_of thy
    2.97 @@ -154,13 +158,14 @@
    2.98  	      (map (snd o snd) fnvs,
    2.99                 map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
   2.100  	    val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
   2.101 -          in (fts ~~ (replicate (length fts) ctxt), FWD (instantiate (ctyenv, its) cong))
   2.102 +          in ((bds, fts ~~ (replicate (length fts) ctxt)),
   2.103 +              Library.apsnd (FWD (instantiate (ctyenv, its) cong)))
   2.104            end)
   2.105 -        handle MATCH => decomp_genreif da congs (t,ctxt)))
   2.106 +        handle MATCH => decomp_genreif da congs (bds, (t,ctxt))))
   2.107        end;
   2.108  
   2.109   (* looks for the atoms equation and instantiates it with the right number *)
   2.110 -    fun mk_decompatom eqs (t,ctxt) =
   2.111 +    fun mk_decompatom eqs (bds, (t,ctxt)) = ((bds, []), fn (bds, _) =>
   2.112        let
   2.113          val tT = fastype_of t
   2.114          fun isat eq =
   2.115 @@ -169,7 +174,7 @@
   2.116            in exists_Const
   2.117  	    (fn (n,ty) => n="List.nth"
   2.118                            andalso
   2.119 -			  AList.defined Type.could_unify (!bds) (domain_type ty)) rhs
   2.120 +			  AList.defined Type.could_unify bds (domain_type ty)) rhs
   2.121              andalso Type.could_unify (fastype_of rhs, tT)
   2.122            end
   2.123  
   2.124 @@ -181,8 +186,8 @@
   2.125            | _ => acc
   2.126  
   2.127          fun
   2.128 -           tryeqs [] = error "Can not find the atoms equation"
   2.129 -         | tryeqs (eq::eqs) = ((
   2.130 +           tryeqs bds [] = error "Can not find the atoms equation"
   2.131 +         | tryeqs bds (eq::eqs) = ((
   2.132            let
   2.133              val rhs = eq |> prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd
   2.134              val nths = get_nths rhs []
   2.135 @@ -206,10 +211,12 @@
   2.136                                 (Vartab.dest tyenv)
   2.137              val tml = Vartab.dest tmenv
   2.138              val t's = map (fn xn => snd (valOf (AList.lookup (op =) tml (xn,0)))) xns (* FIXME : Express with sbst*)
   2.139 -            val subst_ns = map (fn (Const _ $ vs $ n, Var (xn0,T)) =>
   2.140 -                                   (cert n, snd (valOf (AList.lookup (op =) tml xn0))
   2.141 -                                      |> (index_of #> HOLogic.mk_nat #> cert)))
   2.142 -                               subst
   2.143 +            val (bds, subst_ns) = Library.foldl_map
   2.144 +                (fn (bds, (Const _ $ vs $ n, Var (xn0,T))) =>
   2.145 +                  let
   2.146 +                    val name = snd (valOf (AList.lookup (op =) tml xn0))
   2.147 +                    val (bds, idx) = index_of bds name
   2.148 +                  in (bds, (cert n, idx |> (HOLogic.mk_nat #> cert))) end) (bds, subst)
   2.149              val subst_vs =
   2.150                let
   2.151                  fun ty (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = (certT T, certT (sbsT T))
   2.152 @@ -217,7 +224,7 @@
   2.153                    let
   2.154                      val cns = sbst (Const("List.list.Cons", T --> lT --> lT))
   2.155                      val lT' = sbsT lT
   2.156 -                    val (bsT,asT) = the (AList.lookup Type.could_unify (!bds) lT)
   2.157 +                    val (bsT,asT) = the (AList.lookup Type.could_unify bds lT)
   2.158                      val vsn = valOf (AList.lookup (op =) vsns_map vs)
   2.159                      val cvs = cert (fold_rev (fn x => fn xs => cns$x$xs) bsT (Free (vsn, lT')))
   2.160                    in (cert vs, cvs) end
   2.161 @@ -229,10 +236,9 @@
   2.162                let val ih = Drule.cterm_rule (Thm.instantiate (subst_ty,[]))
   2.163                in map (fn (v,t) => (ih v, ih t)) (subst_ns@subst_vs@cts)  end
   2.164              val th = (instantiate (subst_ty, substt)  eq) RS sym
   2.165 -          in hd (Variable.export ctxt'' ctxt [th]) end)
   2.166 -          handle MATCH => tryeqs eqs)
   2.167 -      in ([], fn _ => tryeqs (filter isat eqs))
   2.168 -      end;
   2.169 +          in (bds, hd (Variable.export ctxt'' ctxt [th])) end)
   2.170 +          handle MATCH => tryeqs bds eqs)
   2.171 +      in tryeqs bds (filter isat eqs) end);
   2.172  
   2.173    (* Generic reification procedure: *)
   2.174    (* creates all needed cong rules and then just uses the theorem synthesis *)
   2.175 @@ -245,7 +251,6 @@
   2.176                             |> fst)) raw_eqs []
   2.177          val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)
   2.178                              ) fs []
   2.179 -        val _ = bds := AList.make (fn _ => ([],[])) tys
   2.180          val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
   2.181          val thy = ProofContext.theory_of ctxt'
   2.182          val cert = cterm_of thy
   2.183 @@ -259,27 +264,28 @@
   2.184            in Thm.instantiate ([],subst) eq
   2.185            end
   2.186  
   2.187 +        val bds = AList.make (fn _ => ([],[])) tys
   2.188          val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop
   2.189    	                           |> HOLogic.dest_eq |> fst |> strip_comb |> snd |> tl
   2.190                                     |> (insteq eq)) raw_eqs
   2.191          val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
   2.192 -      in ps ~~ (Variable.export ctxt' ctxt congs)
   2.193 +      in (bds, ps ~~ (Variable.export ctxt' ctxt congs))
   2.194        end
   2.195  
   2.196 -    val congs = rearrange (mk_congs ctxt raw_eqs)
   2.197 -    val th = divide_and_conquer (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt)
   2.198 +    val (bds, congs) = mk_congs ctxt raw_eqs
   2.199 +    val congs = rearrange congs
   2.200 +    val (bds, th) = divide_and_conquer' (decomp_genreif (mk_decompatom raw_eqs) congs) (bds, (t,ctxt))
   2.201      fun is_listVar (Var (_,t)) = can dest_listT t
   2.202           | is_listVar _ = false
   2.203      val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
   2.204  	          |> strip_comb |> snd |> filter is_listVar
   2.205      val cert = cterm_of (ProofContext.theory_of ctxt)
   2.206      val cvs = map (fn (v as Var(n,t)) => (cert v,
   2.207 -                  the (AList.lookup Type.could_unify (!bds) t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   2.208 +                  the (AList.lookup Type.could_unify bds t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
   2.209      val th' = instantiate ([], cvs) th
   2.210      val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
   2.211      val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
   2.212  	       (fn _ => simp_tac (local_simpset_of ctxt) 1)
   2.213 -    val _ = bds := []
   2.214    in FWD trans [th'',th']
   2.215    end
   2.216