added theories for imperative HOL
authorhaftmann
Wed Feb 27 21:41:08 2008 +0100 (2008-02-27)
changeset 2617066e6b967ccf1
parent 26169 73027318f9ba
child 26171 5426a823455c
added theories for imperative HOL
src/HOL/IsaMakefile
src/HOL/Library/Array.thy
src/HOL/Library/Heap.thy
src/HOL/Library/Heap_Monad.thy
src/HOL/Library/Imperative_HOL.thy
src/HOL/Library/Library.thy
src/HOL/Library/Ref.thy
     1.1 --- a/src/HOL/IsaMakefile	Wed Feb 27 21:41:07 2008 +0100
     1.2 +++ b/src/HOL/IsaMakefile	Wed Feb 27 21:41:08 2008 +0100
     1.3 @@ -233,7 +233,9 @@
     1.4    Library/Code_Index.thy Library/Code_Char.thy Library/Code_Char_chr.thy \
     1.5    Library/Code_Integer.thy Library/Code_Message.thy \
     1.6    Library/Abstract_Rat.thy Library/Univ_Poly.thy\
     1.7 -  Library/Numeral_Type.thy Library/Boolean_Algebra.thy
     1.8 +  Library/Numeral_Type.thy Library/Boolean_Algebra.thy Library/Countable.thy \
     1.9 +  Library/RType.thy Library/Heap.thy Library/Heap_Monad.thy Library/Array.thy \
    1.10 +  Library/Ref.thy Library/Imperative_HOL.thy
    1.11  	@cd Library; $(ISATOOL) usedir $(OUT)/HOL Library
    1.12  
    1.13  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Library/Array.thy	Wed Feb 27 21:41:08 2008 +0100
     2.3 @@ -0,0 +1,130 @@
     2.4 +(*  Title:      HOL/Library/Array.thy
     2.5 +    ID:         $Id$
     2.6 +    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     2.7 +*)
     2.8 +
     2.9 +header {* Monadic arrays *}
    2.10 +
    2.11 +theory Array
    2.12 +imports Heap_Monad
    2.13 +begin
    2.14 +
    2.15 +subsection {* Primitives *}
    2.16 +
    2.17 +definition
    2.18 +  new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where
    2.19 +  [code del]: "new n x = Heap_Monad.heap (Heap.array n x)"
    2.20 +
    2.21 +definition
    2.22 +  of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where
    2.23 +  [code del]: "of_list xs = Heap_Monad.heap (Heap.array_of_list xs)"
    2.24 +
    2.25 +definition
    2.26 +  length :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where
    2.27 +  [code del]: "length arr = Heap_Monad.heap (\<lambda>h. (Heap.length arr h, h))"
    2.28 +
    2.29 +definition
    2.30 +  nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap"
    2.31 +where
    2.32 +  [code del]: "nth a i = (do len \<leftarrow> length a;
    2.33 +                 (if i < len
    2.34 +                     then Heap_Monad.heap (\<lambda>h. (get_array a h ! i, h))
    2.35 +                     else raise (''array lookup: index out of range''))
    2.36 +              done)"
    2.37 +
    2.38 +-- {* FIXME adjustion for List theory *}
    2.39 +no_syntax
    2.40 +  nth  :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!" 100)
    2.41 +
    2.42 +abbreviation
    2.43 +  nth_list :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!" 100)
    2.44 +where
    2.45 +  "nth_list \<equiv> List.nth"
    2.46 +
    2.47 +definition
    2.48 +  upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap"
    2.49 +where
    2.50 +  [code del]: "upd i x a = (do len \<leftarrow> length a;
    2.51 +                      (if i < len
    2.52 +                           then Heap_Monad.heap (\<lambda>h. ((), Heap.upd a i x h))
    2.53 +                           else raise (''array update: index out of range''));
    2.54 +                      return a
    2.55 +                   done)" 
    2.56 +
    2.57 +lemma upd_return:
    2.58 +  "upd i x a \<guillemotright> return a = upd i x a"
    2.59 +  unfolding upd_def by (simp add: monad_simp)
    2.60 +
    2.61 +
    2.62 +subsection {* Derivates *}
    2.63 +
    2.64 +definition
    2.65 +  map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
    2.66 +where
    2.67 +  "map_entry i f a = (do
    2.68 +     x \<leftarrow> nth a i;
    2.69 +     upd i (f x) a
    2.70 +   done)"
    2.71 +
    2.72 +definition
    2.73 +  swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap"
    2.74 +where
    2.75 +  "swap i x a = (do
    2.76 +     y \<leftarrow> nth a i;
    2.77 +     upd i x a;
    2.78 +     return x
    2.79 +   done)"
    2.80 +
    2.81 +definition
    2.82 +  make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap"
    2.83 +where
    2.84 +  "make n f = of_list (map f [0 ..< n])"
    2.85 +
    2.86 +definition
    2.87 +  freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap"
    2.88 +where
    2.89 +  "freeze a = (do
    2.90 +     n \<leftarrow> length a;
    2.91 +     mapM (nth a) [0..<n]
    2.92 +   done)"
    2.93 +
    2.94 +definition
    2.95 +  map :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap"
    2.96 +where
    2.97 +  "map f a = (do
    2.98 +     n \<leftarrow> length a;
    2.99 +     foldM (\<lambda>n. map_entry n f) [0..<n] a
   2.100 +   done)"
   2.101 +
   2.102 +hide (open) const new map -- {* avoid clashed with some popular names *}
   2.103 +
   2.104 +
   2.105 +subsection {* Converting arrays to lists *}
   2.106 +
   2.107 +primrec list_of_aux :: "nat \<Rightarrow> ('a\<Colon>heap) array \<Rightarrow> 'a list \<Rightarrow> 'a list Heap" where
   2.108 +  "list_of_aux 0 a xs = return xs"
   2.109 +  | "list_of_aux (Suc n) a xs = (do
   2.110 +        x \<leftarrow> Array.nth a n;
   2.111 +        list_of_aux n a (x#xs)
   2.112 +     done)"
   2.113 +
   2.114 +definition list_of :: "('a\<Colon>heap) array \<Rightarrow> 'a list Heap" where
   2.115 +  "list_of a = (do n \<leftarrow> Array.length a;
   2.116 +                   list_of_aux n a []
   2.117 +                done)"
   2.118 +
   2.119 +
   2.120 +subsection {* Properties *}
   2.121 +
   2.122 +lemma array_make [code func]:
   2.123 +  "Array.new n x = make n (\<lambda>_. x)"
   2.124 +  by (induct n) (simp_all add: make_def new_def Heap_Monad.heap_def
   2.125 +    monad_simp array_of_list_replicate [symmetric]
   2.126 +    map_replicate_trivial replicate_append_same
   2.127 +    of_list_def)
   2.128 +
   2.129 +lemma array_of_list_make [code func]:
   2.130 +  "of_list xs = make (List.length xs) (\<lambda>n. xs ! n)"
   2.131 +  unfolding make_def map_nth ..
   2.132 +
   2.133 +end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Library/Heap.thy	Wed Feb 27 21:41:08 2008 +0100
     3.3 @@ -0,0 +1,450 @@
     3.4 +(*  Title:      HOL/Library/Heap.thy
     3.5 +    ID:         $Id$
     3.6 +    Author:     John Matthews, Galois Connections; Alexander Krauss, TU Muenchen
     3.7 +*)
     3.8 +
     3.9 +header {* A polymorphic heap based on cantor encodings *}
    3.10 +
    3.11 +theory Heap
    3.12 +imports Main Countable RType
    3.13 +begin
    3.14 +
    3.15 +subsection {* Representable types *}
    3.16 +
    3.17 +text {* The type class of representable types *}
    3.18 +
    3.19 +class heap = rtype + countable
    3.20 +
    3.21 +text {* Instances for common HOL types *}
    3.22 +
    3.23 +instance nat :: heap ..
    3.24 +
    3.25 +instance "*" :: (heap, heap) heap ..
    3.26 +
    3.27 +instance "+" :: (heap, heap) heap ..
    3.28 +
    3.29 +instance list :: (heap) heap ..
    3.30 +
    3.31 +instance option :: (heap) heap ..
    3.32 +
    3.33 +instance int :: heap ..
    3.34 +
    3.35 +instance set :: ("{heap, finite}") heap ..
    3.36 +
    3.37 +instance message_string :: countable
    3.38 +  by (rule countable_classI [of "message_string_case to_nat"])
    3.39 +   (auto split: message_string.splits)
    3.40 +
    3.41 +instance message_string :: heap ..
    3.42 +
    3.43 +text {* Reflected types themselves are heap-representable *}
    3.44 +
    3.45 +instantiation rtype :: countable
    3.46 +begin
    3.47 +
    3.48 +lemma list_size_size_append:
    3.49 +  "list_size size (xs @ ys) = list_size size xs + list_size size ys"
    3.50 +  by (induct xs, auto)
    3.51 +
    3.52 +lemma rtype_size: "t = RType.RType c ts \<Longrightarrow> t' \<in> set ts \<Longrightarrow> size t' < size t"
    3.53 +  by (frule split_list) (auto simp add: list_size_size_append)
    3.54 +
    3.55 +function to_nat_rtype :: "rtype \<Rightarrow> nat" where
    3.56 +  "to_nat_rtype (RType.RType c ts) = to_nat (to_nat c, to_nat (map to_nat_rtype ts))"
    3.57 +by pat_completeness auto
    3.58 +
    3.59 +termination by (relation "measure (\<lambda>x. size x)")
    3.60 +  (simp, simp only: in_measure rtype_size)
    3.61 +
    3.62 +instance proof (rule countable_classI)
    3.63 +  fix t t' :: rtype
    3.64 +  have "(\<forall>t'. to_nat_rtype t = to_nat_rtype t' \<longrightarrow> t = t')
    3.65 +    \<and> (\<forall>ts'. map to_nat_rtype ts = map to_nat_rtype ts' \<longrightarrow> ts = ts')"
    3.66 +  proof (induct rule: rtype.induct)
    3.67 +    case (RType c ts) show ?case
    3.68 +    proof (rule allI, rule impI)
    3.69 +      fix t'
    3.70 +      assume hyp: "to_nat_rtype (rtype.RType c ts) = to_nat_rtype t'"
    3.71 +      then obtain c' ts' where t': "t' = (rtype.RType c' ts')"
    3.72 +        by (cases t') auto
    3.73 +      with RType hyp have "c = c'" and "ts = ts'" by simp_all
    3.74 +      with t' show "rtype.RType c ts = t'" by simp
    3.75 +    qed
    3.76 +  next
    3.77 +    case Nil_rtype then show ?case by simp
    3.78 +  next
    3.79 +    case (Cons_rtype t ts) then show ?case by auto
    3.80 +  qed
    3.81 +  then have "to_nat_rtype t = to_nat_rtype t' \<Longrightarrow> t = t'" by auto
    3.82 +  moreover assume "to_nat_rtype t = to_nat_rtype t'"
    3.83 +  ultimately show "t = t'" by simp
    3.84 +qed
    3.85 +
    3.86 +end
    3.87 +
    3.88 +instance rtype :: heap ..
    3.89 +
    3.90 +
    3.91 +subsection {* A polymorphic heap with dynamic arrays and references *}
    3.92 +
    3.93 +types addr = nat -- "untyped heap references"
    3.94 +
    3.95 +datatype 'a array = Array addr
    3.96 +datatype 'a ref = Ref addr -- "note the phantom type 'a "
    3.97 +
    3.98 +primrec addr_of_array :: "'a array \<Rightarrow> addr" where
    3.99 +  "addr_of_array (Array x) = x"
   3.100 +
   3.101 +primrec addr_of_ref :: "'a ref \<Rightarrow> addr" where
   3.102 +  "addr_of_ref (Ref x) = x"
   3.103 +
   3.104 +lemma addr_of_array_inj [simp]:
   3.105 +  "addr_of_array a = addr_of_array a' \<longleftrightarrow> a = a'"
   3.106 +  by (cases a, cases a') simp_all
   3.107 +
   3.108 +lemma addr_of_ref_inj [simp]:
   3.109 +  "addr_of_ref r = addr_of_ref r' \<longleftrightarrow> r = r'"
   3.110 +  by (cases r, cases r') simp_all
   3.111 +
   3.112 +instance array :: (type) countable
   3.113 +  by (rule countable_classI [of addr_of_array]) simp
   3.114 +
   3.115 +instance ref :: (type) countable
   3.116 +  by (rule countable_classI [of addr_of_ref]) simp
   3.117 +
   3.118 +setup {*
   3.119 +  Sign.add_const_constraint (@{const_name Array}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap array"})
   3.120 +  #> Sign.add_const_constraint (@{const_name Ref}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap ref"})
   3.121 +  #> Sign.add_const_constraint (@{const_name addr_of_array}, SOME @{typ "'a\<Colon>heap array \<Rightarrow> nat"})
   3.122 +  #> Sign.add_const_constraint (@{const_name addr_of_ref}, SOME @{typ "'a\<Colon>heap ref \<Rightarrow> nat"})
   3.123 +*}
   3.124 +
   3.125 +types heap_rep = nat -- "representable values"
   3.126 +
   3.127 +record heap =
   3.128 +  arrays :: "rtype \<Rightarrow> addr \<Rightarrow> heap_rep list"
   3.129 +  refs :: "rtype \<Rightarrow> addr \<Rightarrow> heap_rep"
   3.130 +  lim  :: addr
   3.131 +
   3.132 +definition empty :: heap where
   3.133 +  "empty = \<lparr>arrays = (\<lambda>_. arbitrary), refs = (\<lambda>_. arbitrary), lim = 0\<rparr>" -- "why arbitrary?"
   3.134 +
   3.135 +
   3.136 +subsection {* Imperative references and arrays *}
   3.137 +
   3.138 +text {*
   3.139 +  References and arrays are developed in parallel,
   3.140 +  but keeping them seperate makes some later proofs simpler.
   3.141 +*}
   3.142 +
   3.143 +subsubsection {* Primitive operations *}
   3.144 +
   3.145 +definition
   3.146 +  new_ref :: "heap \<Rightarrow> ('a\<Colon>heap) ref \<times> heap" where
   3.147 +  "new_ref h = (let l = lim h in (Ref l, h\<lparr>lim := l + 1\<rparr>))"
   3.148 +
   3.149 +definition
   3.150 +  new_array :: "heap \<Rightarrow> ('a\<Colon>heap) array \<times> heap" where
   3.151 +  "new_array h = (let l = lim h in (Array l, h\<lparr>lim := l + 1\<rparr>))"
   3.152 +
   3.153 +definition
   3.154 +  ref_present :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> bool" where
   3.155 +  "ref_present r h \<longleftrightarrow> addr_of_ref r < lim h"
   3.156 +
   3.157 +definition 
   3.158 +  array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where
   3.159 +  "array_present a h \<longleftrightarrow> addr_of_array a < lim h"
   3.160 +
   3.161 +definition
   3.162 +  get_ref :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> 'a" where
   3.163 +  "get_ref r h = from_nat (refs h (RTYPE('a)) (addr_of_ref r))"
   3.164 +
   3.165 +definition
   3.166 +  get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
   3.167 +  "get_array a h = map from_nat (arrays h (RTYPE('a)) (addr_of_array a))"
   3.168 +
   3.169 +definition
   3.170 +  set_ref :: "'a\<Colon>heap ref \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
   3.171 +  "set_ref r x = 
   3.172 +  refs_update (\<lambda>h. h( RTYPE('a) := ((h (RTYPE('a))) (addr_of_ref r:=to_nat x))))"
   3.173 +
   3.174 +definition
   3.175 +  set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
   3.176 +  "set_array a x = 
   3.177 +  arrays_update (\<lambda>h. h( RTYPE('a) := ((h (RTYPE('a))) (addr_of_array a:=map to_nat x))))"
   3.178 +
   3.179 +subsubsection {* Interface operations *}
   3.180 +
   3.181 +definition
   3.182 +  ref :: "'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap ref \<times> heap" where
   3.183 +  "ref x h = (let (r, h') = new_ref h;
   3.184 +                   h''    = set_ref r x h'
   3.185 +         in (r, h''))"
   3.186 +
   3.187 +definition
   3.188 +  array :: "nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
   3.189 +  "array n x h = (let (r, h') = new_array h;
   3.190 +                       h'' = set_array r (replicate n x) h'
   3.191 +        in (r, h''))"
   3.192 +
   3.193 +definition
   3.194 +  array_of_list :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
   3.195 +  "array_of_list xs h = (let (r, h') = new_array h;
   3.196 +           h'' = set_array r xs h'
   3.197 +        in (r, h''))"  
   3.198 +
   3.199 +definition
   3.200 +  upd :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
   3.201 +  "upd a i x h = set_array a ((get_array a h)[i:=x]) h"
   3.202 +
   3.203 +definition
   3.204 +  length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
   3.205 +  "length a h = size (get_array a h)"
   3.206 +
   3.207 +definition
   3.208 +  array_ran :: "('a\<Colon>heap) option array \<Rightarrow> heap \<Rightarrow> 'a set" where
   3.209 +  "array_ran a h = {e. Some e \<in> set (get_array a h)}"
   3.210 +    -- {*FIXME*}
   3.211 +
   3.212 +
   3.213 +subsubsection {* Reference equality *}
   3.214 +
   3.215 +text {* 
   3.216 +  The following relations are useful for comparing arrays and references.
   3.217 +*}
   3.218 +
   3.219 +definition
   3.220 +  noteq_refs :: "('a\<Colon>heap) ref \<Rightarrow> ('b\<Colon>heap) ref \<Rightarrow> bool" (infix "=!=" 70)
   3.221 +where
   3.222 +  "r =!= s \<longleftrightarrow> RTYPE('a) \<noteq> RTYPE('b) \<or> addr_of_ref r \<noteq> addr_of_ref s"
   3.223 +
   3.224 +definition
   3.225 +  noteq_arrs :: "('a\<Colon>heap) array \<Rightarrow> ('b\<Colon>heap) array \<Rightarrow> bool" (infix "=!!=" 70)
   3.226 +where
   3.227 +  "r =!!= s \<longleftrightarrow> RTYPE('a) \<noteq> RTYPE('b) \<or> addr_of_array r \<noteq> addr_of_array s"
   3.228 +
   3.229 +lemma noteq_refs_sym: "r =!= s \<Longrightarrow> s =!= r"
   3.230 +  and noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
   3.231 +  and unequal_refs [simp]: "r \<noteq> r' \<longleftrightarrow> r =!= r'" -- "same types!"
   3.232 +  and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
   3.233 +unfolding noteq_refs_def noteq_arrs_def by auto
   3.234 +
   3.235 +lemma present_new_ref: "ref_present r h \<Longrightarrow> r =!= fst (ref v h)"
   3.236 +  by (simp add: ref_present_def new_ref_def ref_def Let_def noteq_refs_def)
   3.237 +
   3.238 +lemma present_new_arr: "array_present a h \<Longrightarrow> a =!!= fst (array v x h)"
   3.239 +  by (simp add: array_present_def noteq_arrs_def new_array_def array_def Let_def)
   3.240 +
   3.241 +
   3.242 +subsubsection {* Properties of heap containers *}
   3.243 +
   3.244 +text {* Properties of imperative arrays *}
   3.245 +
   3.246 +text {* FIXME: Does there exist a "canonical" array axiomatisation in
   3.247 +the literature?  *}
   3.248 +
   3.249 +lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x"
   3.250 +  by (simp add: get_array_def set_array_def)
   3.251 +
   3.252 +lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h"
   3.253 +  by (simp add: noteq_arrs_def get_array_def set_array_def)
   3.254 +
   3.255 +lemma set_array_same [simp]:
   3.256 +  "set_array r x (set_array r y h) = set_array r x h"
   3.257 +  by (simp add: set_array_def)
   3.258 +
   3.259 +lemma array_set_set_swap:
   3.260 +  "r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)"
   3.261 +  by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def)
   3.262 +
   3.263 +lemma array_ref_set_set_swap:
   3.264 +  "set_array r x (set_ref r' x' h) = set_ref r' x' (set_array r x h)"
   3.265 +  by (simp add: Let_def expand_fun_eq set_array_def set_ref_def)
   3.266 +
   3.267 +lemma get_array_upd_eq [simp]:
   3.268 +  "get_array a (upd a i v h) = (get_array a h) [i := v]"
   3.269 +  by (simp add: upd_def)
   3.270 +
   3.271 +lemma nth_upd_array_neq_array [simp]:
   3.272 +  "a =!!= b \<Longrightarrow> get_array a (upd b j v h) ! i = get_array a h ! i"
   3.273 +  by (simp add: upd_def noteq_arrs_def)
   3.274 +
   3.275 +lemma get_arry_array_upd_elem_neqIndex [simp]:
   3.276 +  "i \<noteq> j \<Longrightarrow> get_array a (upd a j v h) ! i = get_array a h ! i"
   3.277 +  by simp
   3.278 +
   3.279 +lemma length_upd_eq [simp]: 
   3.280 +  "length a (upd a i v h) = length a h" 
   3.281 +  by (simp add: length_def upd_def)
   3.282 +
   3.283 +lemma length_upd_neq [simp]: 
   3.284 +  "length a (upd b i v h) = length a h"
   3.285 +  by (simp add: upd_def length_def set_array_def get_array_def)
   3.286 +
   3.287 +lemma upd_swap_neqArray:
   3.288 +  "a =!!= a' \<Longrightarrow> 
   3.289 +  upd a i v (upd a' i' v' h) 
   3.290 +  = upd a' i' v' (upd a i v h)"
   3.291 +apply (unfold upd_def)
   3.292 +apply simp
   3.293 +apply (subst array_set_set_swap, assumption)
   3.294 +apply (subst array_get_set_neq)
   3.295 +apply (erule noteq_arrs_sym)
   3.296 +apply (simp)
   3.297 +done
   3.298 +
   3.299 +lemma upd_swap_neqIndex:
   3.300 +  "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> upd a i v (upd a i' v' h) = upd a i' v' (upd a i v h)"
   3.301 +by (auto simp add: upd_def array_set_set_swap list_update_swap)
   3.302 +
   3.303 +lemma get_array_init_array_list:
   3.304 +  "get_array (fst (array_of_list ls h)) (snd (array_of_list ls' h)) = ls'"
   3.305 +  by (simp add: Let_def split_def array_of_list_def)
   3.306 +
   3.307 +lemma set_array:
   3.308 +  "set_array (fst (array_of_list ls h))
   3.309 +     new_ls (snd (array_of_list ls h))
   3.310 +       = snd (array_of_list new_ls h)"
   3.311 +  by (simp add: Let_def split_def array_of_list_def)
   3.312 +
   3.313 +lemma array_present_upd [simp]: 
   3.314 +  "array_present a (upd b i v h) = array_present a h"
   3.315 +  by (simp add: upd_def array_present_def set_array_def get_array_def)
   3.316 +
   3.317 +lemma array_of_list_replicate:
   3.318 +  "array_of_list (replicate n x) = array n x"
   3.319 +  by (simp add: expand_fun_eq array_of_list_def array_def)
   3.320 +
   3.321 +
   3.322 +text {* Properties of imperative references *}
   3.323 +
   3.324 +lemma next_ref_fresh [simp]:
   3.325 +  assumes "(r, h') = new_ref h"
   3.326 +  shows "\<not> ref_present r h"
   3.327 +  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
   3.328 +
   3.329 +lemma next_ref_present [simp]:
   3.330 +  assumes "(r, h') = new_ref h"
   3.331 +  shows "ref_present r h'"
   3.332 +  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
   3.333 +
   3.334 +lemma ref_get_set_eq [simp]: "get_ref r (set_ref r x h) = x"
   3.335 +  by (simp add: get_ref_def set_ref_def)
   3.336 +
   3.337 +lemma ref_get_set_neq [simp]: "r =!= s \<Longrightarrow> get_ref r (set_ref s x h) = get_ref r h"
   3.338 +  by (simp add: noteq_refs_def get_ref_def set_ref_def)
   3.339 +
   3.340 +(* FIXME: We need some infrastructure to infer that locally generated
   3.341 +  new refs (by new_ref(_no_init), new_array(')) are distinct
   3.342 +  from all existing refs.
   3.343 +*)
   3.344 +
   3.345 +lemma ref_set_get: "set_ref r (get_ref r h) h = h"
   3.346 +apply (simp add: set_ref_def get_ref_def)
   3.347 +oops
   3.348 +
   3.349 +lemma set_ref_same[simp]:
   3.350 +  "set_ref r x (set_ref r y h) = set_ref r x h"
   3.351 +  by (simp add: set_ref_def)
   3.352 +
   3.353 +lemma ref_set_set_swap:
   3.354 +  "r =!= r' \<Longrightarrow> set_ref r x (set_ref r' x' h) = set_ref r' x' (set_ref r x h)"
   3.355 +  by (simp add: Let_def expand_fun_eq noteq_refs_def set_ref_def)
   3.356 +
   3.357 +lemma ref_new_set: "fst (ref v (set_ref r v' h)) = fst (ref v h)"
   3.358 +  by (simp add: ref_def new_ref_def set_ref_def Let_def)
   3.359 +
   3.360 +lemma ref_get_new [simp]:
   3.361 +  "get_ref (fst (ref v h)) (snd (ref v' h)) = v'"
   3.362 +  by (simp add: ref_def Let_def split_def)
   3.363 +
   3.364 +lemma ref_set_new [simp]:
   3.365 +  "set_ref (fst (ref v h)) new_v (snd (ref v h)) = snd (ref new_v h)"
   3.366 +  by (simp add: ref_def Let_def split_def)
   3.367 +
   3.368 +lemma ref_get_new_neq: "r =!= (fst (ref v h)) \<Longrightarrow> 
   3.369 +  get_ref r (snd (ref v h)) = get_ref r h"
   3.370 +  by (simp add: get_ref_def set_ref_def ref_def Let_def new_ref_def noteq_refs_def)
   3.371 +
   3.372 +lemma lim_set_ref [simp]:
   3.373 +  "lim (set_ref r v h) = lim h"
   3.374 +  by (simp add: set_ref_def)
   3.375 +
   3.376 +lemma ref_present_new_ref [simp]: 
   3.377 +  "ref_present r h \<Longrightarrow> ref_present r (snd (ref v h))"
   3.378 +  by (simp add: new_ref_def ref_present_def ref_def Let_def)
   3.379 +
   3.380 +lemma ref_present_set_ref [simp]:
   3.381 +  "ref_present r (set_ref r' v h) = ref_present r h"
   3.382 +  by (simp add: set_ref_def ref_present_def)
   3.383 +
   3.384 +lemma array_ranI: "\<lbrakk> Some b = get_array a h ! i; i < Heap.length a h \<rbrakk> \<Longrightarrow> b \<in> array_ran a h"
   3.385 +unfolding array_ran_def Heap.length_def by simp
   3.386 +
   3.387 +lemma array_ran_upd_array_Some:
   3.388 +  assumes "cl \<in> array_ran a (Heap.upd a i (Some b) h)"
   3.389 +  shows "cl \<in> array_ran a h \<or> cl = b"
   3.390 +proof -
   3.391 +  have "set (get_array a h[i := Some b]) \<subseteq> insert (Some b) (set (get_array a h))" by (rule set_update_subset_insert)
   3.392 +  with assms show ?thesis 
   3.393 +    unfolding array_ran_def Heap.upd_def by fastsimp
   3.394 +qed
   3.395 +
   3.396 +lemma array_ran_upd_array_None:
   3.397 +  assumes "cl \<in> array_ran a (Heap.upd a i None h)"
   3.398 +  shows "cl \<in> array_ran a h"
   3.399 +proof -
   3.400 +  have "set (get_array a h[i := None]) \<subseteq> insert None (set (get_array a h))" by (rule set_update_subset_insert)
   3.401 +  with assms show ?thesis
   3.402 +    unfolding array_ran_def Heap.upd_def by auto
   3.403 +qed
   3.404 +
   3.405 +
   3.406 +text {* Non-interaction between imperative array and imperative references *}
   3.407 +
   3.408 +lemma get_array_set_ref [simp]: "get_array a (set_ref r v h) = get_array a h"
   3.409 +  by (simp add: get_array_def set_ref_def)
   3.410 +
   3.411 +lemma nth_set_ref [simp]: "get_array a (set_ref r v h) ! i = get_array a h ! i"
   3.412 +  by simp
   3.413 +
   3.414 +lemma get_ref_upd [simp]: "get_ref r (upd a i v h) = get_ref r h"
   3.415 +  by (simp add: get_ref_def set_array_def upd_def)
   3.416 +
   3.417 +lemma new_ref_upd: "fst (ref v (upd a i v' h)) = fst (ref v h)"
   3.418 +  by (simp add: set_array_def get_array_def Let_def ref_new_set upd_def ref_def  new_ref_def)
   3.419 +
   3.420 +(*not actually true ???
   3.421 +lemma upd_set_ref_swap: "upd a i v (set_ref r v' h) = set_ref r v' (upd a i v h)"
   3.422 +apply (case_tac a)
   3.423 +apply (simp add: Let_def upd_def)
   3.424 +apply auto
   3.425 +done*)
   3.426 +
   3.427 +lemma length_new_ref[simp]: 
   3.428 +  "length a (snd (ref v h)) = length a h"
   3.429 +  by (simp add: get_array_def set_ref_def length_def new_ref_def ref_def Let_def)
   3.430 +
   3.431 +lemma get_array_new_ref [simp]: 
   3.432 +  "get_array a (snd (ref v h)) = get_array a h"
   3.433 +  by (simp add: new_ref_def ref_def set_ref_def get_array_def Let_def)
   3.434 +
   3.435 +lemma get_array_new_ref [simp]:
   3.436 +  "get_array a (snd (ref v h)) ! i = get_array a h ! i"
   3.437 +  by (simp add: get_array_def new_ref_def ref_def set_ref_def Let_def)
   3.438 +
   3.439 +lemma ref_present_upd [simp]: 
   3.440 +  "ref_present r (upd a i v h) = ref_present r h"
   3.441 +  by (simp add: upd_def ref_present_def set_array_def get_array_def)
   3.442 +
   3.443 +lemma array_present_set_ref [simp]:
   3.444 +  "array_present a (set_ref r v h) = array_present a h"
   3.445 +  by (simp add: array_present_def set_ref_def)
   3.446 +
   3.447 +lemma array_present_new_ref [simp]:
   3.448 +  "array_present a h \<Longrightarrow> array_present a (snd (ref v h))"
   3.449 +  by (simp add: array_present_def new_ref_def ref_def Let_def)
   3.450 +
   3.451 +hide (open) const empty array array_of_list upd length ref
   3.452 +
   3.453 +end
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Library/Heap_Monad.thy	Wed Feb 27 21:41:08 2008 +0100
     4.3 @@ -0,0 +1,277 @@
     4.4 +(*  Title:      HOL/Library/Heap_Monad.thy
     4.5 +    ID:         $Id$
     4.6 +    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     4.7 +*)
     4.8 +
     4.9 +header {* A monad with a polymorphic heap *}
    4.10 +
    4.11 +theory Heap_Monad
    4.12 +imports Heap
    4.13 +begin
    4.14 +
    4.15 +subsection {* The monad *}
    4.16 +
    4.17 +subsubsection {* Monad combinators *}
    4.18 +
    4.19 +datatype exception = Exn
    4.20 +
    4.21 +text {* Monadic heap actions either produce values
    4.22 +  and transform the heap, or fail *}
    4.23 +datatype 'a Heap = Heap "heap \<Rightarrow> ('a + exception) \<times> heap"
    4.24 +
    4.25 +primrec
    4.26 +  execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a + exception) \<times> heap" where
    4.27 +  "execute (Heap f) = f"
    4.28 +lemmas [code del] = execute.simps
    4.29 +
    4.30 +lemma Heap_execute [simp]:
    4.31 +  "Heap (execute f) = f" by (cases f) simp_all
    4.32 +
    4.33 +lemma Heap_eqI:
    4.34 +  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
    4.35 +    by (cases f, cases g) (auto simp: expand_fun_eq)
    4.36 +
    4.37 +lemma Heap_eqI':
    4.38 +  "(\<And>h. (\<lambda>x. execute (f x) h) = (\<lambda>y. execute (g y) h)) \<Longrightarrow> f = g"
    4.39 +    by (auto simp: expand_fun_eq intro: Heap_eqI)
    4.40 +
    4.41 +lemma Heap_strip: "(\<And>f. PROP P f) \<equiv> (\<And>g. PROP P (Heap g))"
    4.42 +proof
    4.43 +  fix g :: "heap \<Rightarrow> ('a + exception) \<times> heap" 
    4.44 +  assume "\<And>f. PROP P f"
    4.45 +  then show "PROP P (Heap g)" .
    4.46 +next
    4.47 +  fix f :: "'a Heap" 
    4.48 +  assume assm: "\<And>g. PROP P (Heap g)"
    4.49 +  then have "PROP P (Heap (execute f))" .
    4.50 +  then show "PROP P f" by simp
    4.51 +qed
    4.52 +
    4.53 +definition
    4.54 +  heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
    4.55 +  [code del]: "heap f = Heap (\<lambda>h. apfst Inl (f h))"
    4.56 +
    4.57 +lemma execute_heap [simp]:
    4.58 +  "execute (heap f) h = apfst Inl (f h)"
    4.59 +  by (simp add: heap_def)
    4.60 +
    4.61 +definition
    4.62 +  run :: "'a Heap \<Rightarrow> 'a Heap" where
    4.63 +  run_drop [code del]: "run f = f"
    4.64 +
    4.65 +definition
    4.66 +  bindM :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
    4.67 +  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
    4.68 +                  (Inl x, h') \<Rightarrow> execute (g x) h'
    4.69 +                | r \<Rightarrow> r)"
    4.70 +
    4.71 +notation
    4.72 +  bindM (infixl "\<guillemotright>=" 54)
    4.73 +
    4.74 +abbreviation
    4.75 +  chainM :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
    4.76 +  "f >> g \<equiv> f >>= (\<lambda>_. g)"
    4.77 +
    4.78 +notation
    4.79 +  chainM (infixl "\<guillemotright>" 54)
    4.80 +
    4.81 +definition
    4.82 +  return :: "'a \<Rightarrow> 'a Heap" where
    4.83 +  [code del]: "return x = heap (Pair x)"
    4.84 +
    4.85 +lemma execute_return [simp]:
    4.86 +  "execute (return x) h = apfst Inl (x, h)"
    4.87 +  by (simp add: return_def)
    4.88 +
    4.89 +definition
    4.90 +  raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
    4.91 +  [code del]: "raise s = Heap (Pair (Inr Exn))"
    4.92 +
    4.93 +notation (latex output)
    4.94 +  "raise" ("\<^raw:{\textsf{raise}}>")
    4.95 +
    4.96 +lemma execute_raise [simp]:
    4.97 +  "execute (raise s) h = (Inr Exn, h)"
    4.98 +  by (simp add: raise_def)
    4.99 +
   4.100 +
   4.101 +subsubsection {* do-syntax *}
   4.102 +
   4.103 +text {*
   4.104 +  We provide a convenient do-notation for monadic expressions
   4.105 +  well-known from Haskell.  @{const Let} is printed
   4.106 +  specially in do-expressions.
   4.107 +*}
   4.108 +
   4.109 +nonterminals do_expr
   4.110 +
   4.111 +syntax
   4.112 +  "_do" :: "do_expr \<Rightarrow> 'a"
   4.113 +    ("(do (_)//done)" [12] 100)
   4.114 +  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
   4.115 +    ("_ <- _;//_" [1000, 13, 12] 12)
   4.116 +  "_chainM" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
   4.117 +    ("_;//_" [13, 12] 12)
   4.118 +  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
   4.119 +    ("let _ = _;//_" [1000, 13, 12] 12)
   4.120 +  "_nil" :: "'a \<Rightarrow> do_expr"
   4.121 +    ("_" [12] 12)
   4.122 +
   4.123 +syntax (xsymbols)
   4.124 +  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
   4.125 +    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
   4.126 +syntax (latex output)
   4.127 +  "_do" :: "do_expr \<Rightarrow> 'a"
   4.128 +    ("(\<^raw:{\textsf{do}}> (_))" [12] 100)
   4.129 +  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
   4.130 +    ("\<^raw:\textsf{let}> _ = _;//_" [1000, 13, 12] 12)
   4.131 +notation (latex output)
   4.132 +  "return" ("\<^raw:{\textsf{return}}>")
   4.133 +
   4.134 +translations
   4.135 +  "_do f" => "CONST run f"
   4.136 +  "_bindM x f g" => "f \<guillemotright>= (\<lambda>x. g)"
   4.137 +  "_chainM f g" => "f \<guillemotright> g"
   4.138 +  "_let x t f" => "CONST Let t (\<lambda>x. f)"
   4.139 +  "_nil f" => "f"
   4.140 +
   4.141 +print_translation {*
   4.142 +let
   4.143 +  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
   4.144 +        let
   4.145 +          val (v, t) = Syntax.variant_abs abs;
   4.146 +        in ((v, ty), t) end
   4.147 +    | dest_abs_eta t =
   4.148 +        let
   4.149 +          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
   4.150 +        in ((v, dummyT), t) end
   4.151 +  fun unfold_monad (Const (@{const_syntax bindM}, _) $ f $ g) =
   4.152 +        let
   4.153 +          val ((v, ty), g') = dest_abs_eta g;
   4.154 +          val v_used = fold_aterms
   4.155 +            (fn Free (w, _) => (fn s => s orelse v = w) | _ => I) g' false;
   4.156 +        in if v_used then
   4.157 +          Const ("_bindM", dummyT) $ Free (v, ty) $ f $ unfold_monad g'
   4.158 +        else
   4.159 +          Const ("_chainM", dummyT) $ f $ unfold_monad g'
   4.160 +        end
   4.161 +    | unfold_monad (Const (@{const_syntax chainM}, _) $ f $ g) =
   4.162 +        Const ("_chainM", dummyT) $ f $ unfold_monad g
   4.163 +    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
   4.164 +        let
   4.165 +          val ((v, ty), g') = dest_abs_eta g;
   4.166 +        in Const ("_let", dummyT) $ Free (v, ty) $ f $ unfold_monad g' end
   4.167 +    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
   4.168 +        Const ("return", dummyT) $ f
   4.169 +    | unfold_monad f = f;
   4.170 +  fun tr' (f::ts) =
   4.171 +    list_comb (Const ("_do", dummyT) $ unfold_monad f, ts)
   4.172 +in [(@{const_syntax "run"}, tr')] end;
   4.173 +*}
   4.174 +
   4.175 +subsubsection {* Plain evaluation *}
   4.176 +
   4.177 +definition
   4.178 +  evaluate :: "'a Heap \<Rightarrow> 'a"
   4.179 +where
   4.180 +  [code del]: "evaluate f = (case execute f Heap.empty
   4.181 +    of (Inl x, _) \<Rightarrow> x)"
   4.182 +
   4.183 +
   4.184 +subsection {* Monad properties *}
   4.185 +
   4.186 +subsubsection {* Superfluous runs *}
   4.187 +
   4.188 +text {* @{term run} is just a doodle *}
   4.189 +
   4.190 +lemma run_simp [simp]:
   4.191 +  "\<And>f. run (run f) = run f"
   4.192 +  "\<And>f g. run f \<guillemotright>= g = f \<guillemotright>= g"
   4.193 +  "\<And>f g. run f \<guillemotright> g = f \<guillemotright> g"
   4.194 +  "\<And>f g. f \<guillemotright>= (\<lambda>x. run g) = f \<guillemotright>= (\<lambda>x. g)"
   4.195 +  "\<And>f g. f \<guillemotright> run g = f \<guillemotright> g"
   4.196 +  "\<And>f. f = run g \<longleftrightarrow> f = g"
   4.197 +  "\<And>f. run f = g \<longleftrightarrow> f = g"
   4.198 +  unfolding run_drop by rule+
   4.199 +
   4.200 +subsubsection {* Monad laws *}
   4.201 +
   4.202 +lemma return_bind: "return x \<guillemotright>= f = f x"
   4.203 +  by (simp add: bindM_def return_def)
   4.204 +
   4.205 +lemma bind_return: "f \<guillemotright>= return = f"
   4.206 +proof (rule Heap_eqI)
   4.207 +  fix h
   4.208 +  show "execute (f \<guillemotright>= return) h = execute f h"
   4.209 +    by (auto simp add: bindM_def return_def split: sum.splits prod.splits)
   4.210 +qed
   4.211 +
   4.212 +lemma bind_bind: "(f \<guillemotright>= g) \<guillemotright>= h = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= h)"
   4.213 +  by (rule Heap_eqI) (auto simp add: bindM_def split: split: sum.splits prod.splits)
   4.214 +
   4.215 +lemma bind_bind': "f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= h x) = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= (\<lambda>y. return (x, y))) \<guillemotright>= (\<lambda>(x, y). h x y)"
   4.216 +  by (rule Heap_eqI) (auto simp add: bindM_def split: split: sum.splits prod.splits)
   4.217 +
   4.218 +lemma raise_bind: "raise e \<guillemotright>= f = raise e"
   4.219 +  by (simp add: raise_def bindM_def)
   4.220 +
   4.221 +
   4.222 +lemmas monad_simp = return_bind bind_return bind_bind raise_bind
   4.223 +
   4.224 +
   4.225 +subsection {* Generic combinators *}
   4.226 +
   4.227 +definition
   4.228 +  liftM :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap"
   4.229 +where
   4.230 +  "liftM f = return o f"
   4.231 +
   4.232 +definition
   4.233 +  compM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> ('b \<Rightarrow> 'c Heap) \<Rightarrow> 'a \<Rightarrow> 'c Heap" (infixl ">>==" 54)
   4.234 +where
   4.235 +  "(f >>== g) = (\<lambda>x. f x \<guillemotright>= g)"
   4.236 +
   4.237 +notation
   4.238 +  compM (infixl "\<guillemotright>==" 54)
   4.239 +
   4.240 +lemma liftM_collapse: "liftM f x = return (f x)"
   4.241 +  by (simp add: liftM_def)
   4.242 +
   4.243 +lemma liftM_compM: "liftM f \<guillemotright>== g = g o f"
   4.244 +  by (auto intro: Heap_eqI' simp add: expand_fun_eq liftM_def compM_def bindM_def)
   4.245 +
   4.246 +lemma compM_return: "f \<guillemotright>== return = f"
   4.247 +  by (simp add: compM_def monad_simp)
   4.248 +
   4.249 +lemma compM_compM: "(f \<guillemotright>== g) \<guillemotright>== h = f \<guillemotright>== (g \<guillemotright>== h)"
   4.250 +  by (simp add: compM_def monad_simp)
   4.251 +
   4.252 +lemma liftM_bind:
   4.253 +  "(\<lambda>x. liftM f x \<guillemotright>= liftM g) = liftM (\<lambda>x. g (f x))"
   4.254 +  by (rule Heap_eqI') (simp add: monad_simp liftM_def bindM_def)
   4.255 +
   4.256 +lemma liftM_comp:
   4.257 +  "liftM f o g = liftM (f o g)"
   4.258 +  by (rule Heap_eqI') (simp add: liftM_def)
   4.259 +
   4.260 +lemmas monad_simp' = monad_simp liftM_compM compM_return
   4.261 +  compM_compM liftM_bind liftM_comp
   4.262 +
   4.263 +primrec 
   4.264 +  mapM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap"
   4.265 +where
   4.266 +  "mapM f [] = return []"
   4.267 +  | "mapM f (x#xs) = do y \<leftarrow> f x;
   4.268 +                        ys \<leftarrow> mapM f xs;
   4.269 +                        return (y # ys)
   4.270 +                     done"
   4.271 +
   4.272 +primrec
   4.273 +  foldM :: "('a \<Rightarrow> 'b \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b Heap"
   4.274 +where
   4.275 +  "foldM f [] s = return s"
   4.276 +  | "foldM f (x#xs) s = f x s \<guillemotright>= foldM f xs"
   4.277 +
   4.278 +hide (open) const heap execute
   4.279 +
   4.280 +end
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/HOL/Library/Imperative_HOL.thy	Wed Feb 27 21:41:08 2008 +0100
     5.3 @@ -0,0 +1,12 @@
     5.4 +(*  Title:      HOL/Library/Imperative_HOL.thy
     5.5 +    ID:         $Id$
     5.6 +    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     5.7 +*)
     5.8 +
     5.9 +header {* Entry point into monadic imperative HOL *}
    5.10 +
    5.11 +theory Imperative_HOL
    5.12 +imports Array Ref
    5.13 +begin
    5.14 +
    5.15 +end
     6.1 --- a/src/HOL/Library/Library.thy	Wed Feb 27 21:41:07 2008 +0100
     6.2 +++ b/src/HOL/Library/Library.thy	Wed Feb 27 21:41:08 2008 +0100
     6.3 @@ -8,18 +8,22 @@
     6.4    Binomial
     6.5    Boolean_Algebra
     6.6    Char_ord
     6.7 +  Code_Char_chr
     6.8    Code_Index
     6.9 +  Code_Integer
    6.10    Code_Message
    6.11    Coinductive_List
    6.12    Commutative_Ring
    6.13    Continuity
    6.14 +  Countable
    6.15    Dense_Linear_Order
    6.16    Efficient_Nat
    6.17 -  (*Eval*)
    6.18 +  Eval
    6.19    Eval_Witness
    6.20    Executable_Set
    6.21    FuncSet
    6.22    GCD
    6.23 +  Imperative_HOL
    6.24    Infinite_Set
    6.25    ListSpace
    6.26    Multiset
    6.27 @@ -30,8 +34,6 @@
    6.28    OptionalSugar
    6.29    Parity
    6.30    Permutation
    6.31 -  Code_Integer
    6.32 -  Code_Char_chr
    6.33    Primes
    6.34    Quicksort
    6.35    Quotient
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/HOL/Library/Ref.thy	Wed Feb 27 21:41:08 2008 +0100
     7.3 @@ -0,0 +1,56 @@
     7.4 +(*  Title:      HOL/Library/Ref.thy
     7.5 +    ID:         $Id$
     7.6 +    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
     7.7 +*)
     7.8 +
     7.9 +header {* Monadic references *}
    7.10 +
    7.11 +theory Ref
    7.12 +imports Heap_Monad
    7.13 +begin
    7.14 +
    7.15 +text {*
    7.16 +  Imperative reference operations; modeled after their ML counterparts.
    7.17 +  See http://caml.inria.fr/pub/docs/manual-caml-light/node14.15.html
    7.18 +  and http://www.smlnj.org/doc/Conversion/top-level-comparison.html
    7.19 +*}
    7.20 +
    7.21 +subsection {* Primitives *}
    7.22 +
    7.23 +definition
    7.24 +  new :: "'a\<Colon>heap \<Rightarrow> 'a ref Heap" where
    7.25 +  [code del]: "new v = Heap_Monad.heap (Heap.ref v)"
    7.26 +
    7.27 +definition
    7.28 +  lookup :: "'a\<Colon>heap ref \<Rightarrow> 'a Heap" ("!_" 61) where
    7.29 +  [code del]: "lookup r = Heap_Monad.heap (\<lambda>h. (get_ref r h, h))"
    7.30 +
    7.31 +definition
    7.32 +  update :: "'a ref \<Rightarrow> ('a\<Colon>heap) \<Rightarrow> unit Heap" ("_ := _" 62) where
    7.33 +  [code del]: "update r e = Heap_Monad.heap (\<lambda>h. ((), set_ref r e h))"
    7.34 +
    7.35 +subsection {* Derivates *}
    7.36 +
    7.37 +definition
    7.38 +  change :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a ref \<Rightarrow> 'a Heap"
    7.39 +where
    7.40 +  "change f r = (do x \<leftarrow> ! r;
    7.41 +                    let y = f x;
    7.42 +                    r := y;
    7.43 +                    return y
    7.44 +                 done)"
    7.45 +
    7.46 +hide (open) const new lookup update change
    7.47 +
    7.48 +subsection {* Properties *}
    7.49 +
    7.50 +lemma lookup_chain:
    7.51 +  "(!r \<guillemotright> f) = f"
    7.52 +  by (cases f)
    7.53 +    (auto simp add: Let_def bindM_def lookup_def expand_fun_eq)
    7.54 +
    7.55 +lemma update_change [code func]:
    7.56 +  "r := e = Ref.change (\<lambda>_. e) r \<guillemotright> return ()"
    7.57 +  by (auto simp add: monad_simp change_def lookup_chain)
    7.58 +
    7.59 +end
    7.60 \ No newline at end of file