src/HOL/Imperative_HOL/Heap.thy
author haftmann
Wed, 21 Jan 2009 16:47:31 +0100
changeset 29580 117b88da143c
parent 29399 ebcd69a00872
child 30738 0842e906300c
permissions -rw-r--r--
dropped ID
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Heap.thy
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     2
    ID:         $Id$
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     3
    Author:     John Matthews, Galois Connections; Alexander Krauss, TU Muenchen
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     4
*)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     5
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     6
header {* A polymorphic heap based on cantor encodings *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     7
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     8
theory Heap
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28524
diff changeset
     9
imports Plain "~~/src/HOL/List" Countable Typerep
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    10
begin
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    11
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    12
subsection {* Representable types *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    13
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    14
text {* The type class of representable types *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    15
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    16
class heap = typerep + countable
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    17
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    18
text {* Instances for common HOL types *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    19
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    20
instance nat :: heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    21
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    22
instance "*" :: (heap, heap) heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    23
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    24
instance "+" :: (heap, heap) heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    25
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    26
instance list :: (heap) heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    27
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    28
instance option :: (heap) heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    29
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    30
instance int :: heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    31
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    32
instance message_string :: countable
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    33
  by (rule countable_classI [of "message_string_case to_nat"])
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    34
   (auto split: message_string.splits)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    35
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    36
instance message_string :: heap ..
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    37
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    38
text {* Reflected types themselves are heap-representable *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    39
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    40
instantiation typerep :: countable
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    41
begin
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    42
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    43
fun to_nat_typerep :: "typerep \<Rightarrow> nat" where
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28524
diff changeset
    44
  "to_nat_typerep (Typerep.Typerep c ts) = to_nat (to_nat c, to_nat (map to_nat_typerep ts))"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    45
26932
c398a3866082 avoid undeclared variables within proofs;
wenzelm
parents: 26817
diff changeset
    46
instance
c398a3866082 avoid undeclared variables within proofs;
wenzelm
parents: 26817
diff changeset
    47
proof (rule countable_classI)
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    48
  fix t t' :: typerep and ts
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    49
  have "(\<forall>t'. to_nat_typerep t = to_nat_typerep t' \<longrightarrow> t = t')
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    50
    \<and> (\<forall>ts'. map to_nat_typerep ts = map to_nat_typerep ts' \<longrightarrow> ts = ts')"
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    51
  proof (induct rule: typerep.induct)
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    52
    case (Typerep c ts) show ?case
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    53
    proof (rule allI, rule impI)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    54
      fix t'
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28524
diff changeset
    55
      assume hyp: "to_nat_typerep (Typerep.Typerep c ts) = to_nat_typerep t'"
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28524
diff changeset
    56
      then obtain c' ts' where t': "t' = (Typerep.Typerep c' ts')"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    57
        by (cases t') auto
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    58
      with Typerep hyp have "c = c'" and "ts = ts'" by simp_all
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28524
diff changeset
    59
      with t' show "Typerep.Typerep c ts = t'" by simp
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    60
    qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    61
  next
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    62
    case Nil_typerep then show ?case by simp
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    63
  next
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    64
    case (Cons_typerep t ts) then show ?case by auto
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    65
  qed
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    66
  then have "to_nat_typerep t = to_nat_typerep t' \<Longrightarrow> t = t'" by auto
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    67
  moreover assume "to_nat_typerep t = to_nat_typerep t'"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    68
  ultimately show "t = t'" by simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    69
qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    70
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    71
end
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    72
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
    73
instance typerep :: heap ..
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    74
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    75
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    76
subsection {* A polymorphic heap with dynamic arrays and references *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    77
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    78
types addr = nat -- "untyped heap references"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    79
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    80
datatype 'a array = Array addr
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    81
datatype 'a ref = Ref addr -- "note the phantom type 'a "
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    82
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    83
primrec addr_of_array :: "'a array \<Rightarrow> addr" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    84
  "addr_of_array (Array x) = x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    85
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    86
primrec addr_of_ref :: "'a ref \<Rightarrow> addr" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    87
  "addr_of_ref (Ref x) = x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    88
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    89
lemma addr_of_array_inj [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    90
  "addr_of_array a = addr_of_array a' \<longleftrightarrow> a = a'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    91
  by (cases a, cases a') simp_all
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    92
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    93
lemma addr_of_ref_inj [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    94
  "addr_of_ref r = addr_of_ref r' \<longleftrightarrow> r = r'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    95
  by (cases r, cases r') simp_all
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    96
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    97
instance array :: (type) countable
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    98
  by (rule countable_classI [of addr_of_array]) simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    99
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   100
instance ref :: (type) countable
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   101
  by (rule countable_classI [of addr_of_ref]) simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   102
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   103
setup {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   104
  Sign.add_const_constraint (@{const_name Array}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap array"})
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   105
  #> Sign.add_const_constraint (@{const_name Ref}, SOME @{typ "nat \<Rightarrow> 'a\<Colon>heap ref"})
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   106
  #> Sign.add_const_constraint (@{const_name addr_of_array}, SOME @{typ "'a\<Colon>heap array \<Rightarrow> nat"})
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   107
  #> Sign.add_const_constraint (@{const_name addr_of_ref}, SOME @{typ "'a\<Colon>heap ref \<Rightarrow> nat"})
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   108
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   109
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   110
types heap_rep = nat -- "representable values"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   111
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   112
record heap =
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   113
  arrays :: "typerep \<Rightarrow> addr \<Rightarrow> heap_rep list"
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   114
  refs :: "typerep \<Rightarrow> addr \<Rightarrow> heap_rep"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   115
  lim  :: addr
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   116
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   117
definition empty :: heap where
28524
644b62cf678f arbitrary is undefined
haftmann
parents: 28335
diff changeset
   118
  "empty = \<lparr>arrays = (\<lambda>_. undefined), refs = (\<lambda>_. undefined), lim = 0\<rparr>" -- "why undefined?"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   119
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   120
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   121
subsection {* Imperative references and arrays *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   122
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   123
text {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   124
  References and arrays are developed in parallel,
26586
a2255b130fd9 fix spelling
huffman
parents: 26300
diff changeset
   125
  but keeping them separate makes some later proofs simpler.
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   126
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   127
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   128
subsubsection {* Primitive operations *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   129
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   130
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   131
  new_ref :: "heap \<Rightarrow> ('a\<Colon>heap) ref \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   132
  "new_ref h = (let l = lim h in (Ref l, h\<lparr>lim := l + 1\<rparr>))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   133
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   134
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   135
  new_array :: "heap \<Rightarrow> ('a\<Colon>heap) array \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   136
  "new_array h = (let l = lim h in (Array l, h\<lparr>lim := l + 1\<rparr>))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   137
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   138
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   139
  ref_present :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> bool" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   140
  "ref_present r h \<longleftrightarrow> addr_of_ref r < lim h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   141
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   142
definition 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   143
  array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   144
  "array_present a h \<longleftrightarrow> addr_of_array a < lim h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   145
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   146
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   147
  get_ref :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> 'a" where
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   148
  "get_ref r h = from_nat (refs h (TYPEREP('a)) (addr_of_ref r))"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   149
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   150
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   151
  get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   152
  "get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   153
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   154
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   155
  set_ref :: "'a\<Colon>heap ref \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   156
  "set_ref r x = 
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   157
  refs_update (\<lambda>h. h(TYPEREP('a) := ((h (TYPEREP('a))) (addr_of_ref r:=to_nat x))))"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   158
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   159
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   160
  set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   161
  "set_array a x = 
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   162
  arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   163
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   164
subsubsection {* Interface operations *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   165
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   166
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   167
  ref :: "'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap ref \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   168
  "ref x h = (let (r, h') = new_ref h;
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   169
                   h''    = set_ref r x h'
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   170
         in (r, h''))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   171
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   172
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   173
  array :: "nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   174
  "array n x h = (let (r, h') = new_array h;
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   175
                       h'' = set_array r (replicate n x) h'
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   176
        in (r, h''))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   177
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   178
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   179
  array_of_list :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   180
  "array_of_list xs h = (let (r, h') = new_array h;
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   181
           h'' = set_array r xs h'
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   182
        in (r, h''))"  
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   183
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   184
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   185
  upd :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   186
  "upd a i x h = set_array a ((get_array a h)[i:=x]) h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   187
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   188
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   189
  length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   190
  "length a h = size (get_array a h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   191
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   192
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   193
  array_ran :: "('a\<Colon>heap) option array \<Rightarrow> heap \<Rightarrow> 'a set" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   194
  "array_ran a h = {e. Some e \<in> set (get_array a h)}"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   195
    -- {*FIXME*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   196
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   197
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   198
subsubsection {* Reference equality *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   199
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   200
text {* 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   201
  The following relations are useful for comparing arrays and references.
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   202
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   203
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   204
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   205
  noteq_refs :: "('a\<Colon>heap) ref \<Rightarrow> ('b\<Colon>heap) ref \<Rightarrow> bool" (infix "=!=" 70)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   206
where
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   207
  "r =!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_ref r \<noteq> addr_of_ref s"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   208
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   209
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   210
  noteq_arrs :: "('a\<Colon>heap) array \<Rightarrow> ('b\<Colon>heap) array \<Rightarrow> bool" (infix "=!!=" 70)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   211
where
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28042
diff changeset
   212
  "r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   213
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   214
lemma noteq_refs_sym: "r =!= s \<Longrightarrow> s =!= r"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   215
  and noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   216
  and unequal_refs [simp]: "r \<noteq> r' \<longleftrightarrow> r =!= r'" -- "same types!"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   217
  and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   218
unfolding noteq_refs_def noteq_arrs_def by auto
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   219
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   220
lemma present_new_ref: "ref_present r h \<Longrightarrow> r =!= fst (ref v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   221
  by (simp add: ref_present_def new_ref_def ref_def Let_def noteq_refs_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   222
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   223
lemma present_new_arr: "array_present a h \<Longrightarrow> a =!!= fst (array v x h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   224
  by (simp add: array_present_def noteq_arrs_def new_array_def array_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   225
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   226
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   227
subsubsection {* Properties of heap containers *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   228
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   229
text {* Properties of imperative arrays *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   230
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   231
text {* FIXME: Does there exist a "canonical" array axiomatisation in
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   232
the literature?  *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   233
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   234
lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   235
  by (simp add: get_array_def set_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   236
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   237
lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   238
  by (simp add: noteq_arrs_def get_array_def set_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   239
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   240
lemma set_array_same [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   241
  "set_array r x (set_array r y h) = set_array r x h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   242
  by (simp add: set_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   243
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   244
lemma array_set_set_swap:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   245
  "r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   246
  by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   247
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   248
lemma array_ref_set_set_swap:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   249
  "set_array r x (set_ref r' x' h) = set_ref r' x' (set_array r x h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   250
  by (simp add: Let_def expand_fun_eq set_array_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   251
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   252
lemma get_array_upd_eq [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   253
  "get_array a (upd a i v h) = (get_array a h) [i := v]"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   254
  by (simp add: upd_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   255
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   256
lemma nth_upd_array_neq_array [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   257
  "a =!!= b \<Longrightarrow> get_array a (upd b j v h) ! i = get_array a h ! i"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   258
  by (simp add: upd_def noteq_arrs_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   259
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   260
lemma get_arry_array_upd_elem_neqIndex [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   261
  "i \<noteq> j \<Longrightarrow> get_array a (upd a j v h) ! i = get_array a h ! i"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   262
  by simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   263
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   264
lemma length_upd_eq [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   265
  "length a (upd a i v h) = length a h" 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   266
  by (simp add: length_def upd_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   267
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   268
lemma length_upd_neq [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   269
  "length a (upd b i v h) = length a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   270
  by (simp add: upd_def length_def set_array_def get_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   271
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   272
lemma upd_swap_neqArray:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   273
  "a =!!= a' \<Longrightarrow> 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   274
  upd a i v (upd a' i' v' h) 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   275
  = upd a' i' v' (upd a i v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   276
apply (unfold upd_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   277
apply simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   278
apply (subst array_set_set_swap, assumption)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   279
apply (subst array_get_set_neq)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   280
apply (erule noteq_arrs_sym)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   281
apply (simp)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   282
done
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   283
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   284
lemma upd_swap_neqIndex:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   285
  "\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> upd a i v (upd a i' v' h) = upd a i' v' (upd a i v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   286
by (auto simp add: upd_def array_set_set_swap list_update_swap)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   287
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   288
lemma get_array_init_array_list:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   289
  "get_array (fst (array_of_list ls h)) (snd (array_of_list ls' h)) = ls'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   290
  by (simp add: Let_def split_def array_of_list_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   291
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   292
lemma set_array:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   293
  "set_array (fst (array_of_list ls h))
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   294
     new_ls (snd (array_of_list ls h))
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   295
       = snd (array_of_list new_ls h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   296
  by (simp add: Let_def split_def array_of_list_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   297
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   298
lemma array_present_upd [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   299
  "array_present a (upd b i v h) = array_present a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   300
  by (simp add: upd_def array_present_def set_array_def get_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   301
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   302
lemma array_of_list_replicate:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   303
  "array_of_list (replicate n x) = array n x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   304
  by (simp add: expand_fun_eq array_of_list_def array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   305
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   306
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   307
text {* Properties of imperative references *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   308
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   309
lemma next_ref_fresh [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   310
  assumes "(r, h') = new_ref h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   311
  shows "\<not> ref_present r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   312
  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   313
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   314
lemma next_ref_present [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   315
  assumes "(r, h') = new_ref h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   316
  shows "ref_present r h'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   317
  using assms by (cases h) (auto simp add: new_ref_def ref_present_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   318
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   319
lemma ref_get_set_eq [simp]: "get_ref r (set_ref r x h) = x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   320
  by (simp add: get_ref_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   321
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   322
lemma ref_get_set_neq [simp]: "r =!= s \<Longrightarrow> get_ref r (set_ref s x h) = get_ref r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   323
  by (simp add: noteq_refs_def get_ref_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   324
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   325
(* FIXME: We need some infrastructure to infer that locally generated
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   326
  new refs (by new_ref(_no_init), new_array(')) are distinct
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   327
  from all existing refs.
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   328
*)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   329
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   330
lemma ref_set_get: "set_ref r (get_ref r h) h = h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   331
apply (simp add: set_ref_def get_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   332
oops
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   333
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   334
lemma set_ref_same[simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   335
  "set_ref r x (set_ref r y h) = set_ref r x h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   336
  by (simp add: set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   337
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   338
lemma ref_set_set_swap:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   339
  "r =!= r' \<Longrightarrow> set_ref r x (set_ref r' x' h) = set_ref r' x' (set_ref r x h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   340
  by (simp add: Let_def expand_fun_eq noteq_refs_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   341
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   342
lemma ref_new_set: "fst (ref v (set_ref r v' h)) = fst (ref v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   343
  by (simp add: ref_def new_ref_def set_ref_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   344
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   345
lemma ref_get_new [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   346
  "get_ref (fst (ref v h)) (snd (ref v' h)) = v'"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   347
  by (simp add: ref_def Let_def split_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   348
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   349
lemma ref_set_new [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   350
  "set_ref (fst (ref v h)) new_v (snd (ref v h)) = snd (ref new_v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   351
  by (simp add: ref_def Let_def split_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   352
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   353
lemma ref_get_new_neq: "r =!= (fst (ref v h)) \<Longrightarrow> 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   354
  get_ref r (snd (ref v h)) = get_ref r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   355
  by (simp add: get_ref_def set_ref_def ref_def Let_def new_ref_def noteq_refs_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   356
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   357
lemma lim_set_ref [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   358
  "lim (set_ref r v h) = lim h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   359
  by (simp add: set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   360
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   361
lemma ref_present_new_ref [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   362
  "ref_present r h \<Longrightarrow> ref_present r (snd (ref v h))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   363
  by (simp add: new_ref_def ref_present_def ref_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   364
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   365
lemma ref_present_set_ref [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   366
  "ref_present r (set_ref r' v h) = ref_present r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   367
  by (simp add: set_ref_def ref_present_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   368
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   369
lemma array_ranI: "\<lbrakk> Some b = get_array a h ! i; i < Heap.length a h \<rbrakk> \<Longrightarrow> b \<in> array_ran a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   370
unfolding array_ran_def Heap.length_def by simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   371
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   372
lemma array_ran_upd_array_Some:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   373
  assumes "cl \<in> array_ran a (Heap.upd a i (Some b) h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   374
  shows "cl \<in> array_ran a h \<or> cl = b"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   375
proof -
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   376
  have "set (get_array a h[i := Some b]) \<subseteq> insert (Some b) (set (get_array a h))" by (rule set_update_subset_insert)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   377
  with assms show ?thesis 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   378
    unfolding array_ran_def Heap.upd_def by fastsimp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   379
qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   380
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   381
lemma array_ran_upd_array_None:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   382
  assumes "cl \<in> array_ran a (Heap.upd a i None h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   383
  shows "cl \<in> array_ran a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   384
proof -
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   385
  have "set (get_array a h[i := None]) \<subseteq> insert None (set (get_array a h))" by (rule set_update_subset_insert)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   386
  with assms show ?thesis
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   387
    unfolding array_ran_def Heap.upd_def by auto
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   388
qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   389
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   390
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   391
text {* Non-interaction between imperative array and imperative references *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   392
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   393
lemma get_array_set_ref [simp]: "get_array a (set_ref r v h) = get_array a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   394
  by (simp add: get_array_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   395
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   396
lemma nth_set_ref [simp]: "get_array a (set_ref r v h) ! i = get_array a h ! i"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   397
  by simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   398
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   399
lemma get_ref_upd [simp]: "get_ref r (upd a i v h) = get_ref r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   400
  by (simp add: get_ref_def set_array_def upd_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   401
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   402
lemma new_ref_upd: "fst (ref v (upd a i v' h)) = fst (ref v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   403
  by (simp add: set_array_def get_array_def Let_def ref_new_set upd_def ref_def  new_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   404
26300
03def556e26e removed duplicate lemmas;
wenzelm
parents: 26170
diff changeset
   405
text {*not actually true ???*}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   406
lemma upd_set_ref_swap: "upd a i v (set_ref r v' h) = set_ref r v' (upd a i v h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   407
apply (case_tac a)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   408
apply (simp add: Let_def upd_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   409
apply auto
26300
03def556e26e removed duplicate lemmas;
wenzelm
parents: 26170
diff changeset
   410
oops
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   411
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   412
lemma length_new_ref[simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   413
  "length a (snd (ref v h)) = length a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   414
  by (simp add: get_array_def set_ref_def length_def new_ref_def ref_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   415
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   416
lemma get_array_new_ref [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   417
  "get_array a (snd (ref v h)) = get_array a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   418
  by (simp add: new_ref_def ref_def set_ref_def get_array_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   419
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   420
lemma ref_present_upd [simp]: 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   421
  "ref_present r (upd a i v h) = ref_present r h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   422
  by (simp add: upd_def ref_present_def set_array_def get_array_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   423
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   424
lemma array_present_set_ref [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   425
  "array_present a (set_ref r v h) = array_present a h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   426
  by (simp add: array_present_def set_ref_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   427
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   428
lemma array_present_new_ref [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   429
  "array_present a h \<Longrightarrow> array_present a (snd (ref v h))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   430
  by (simp add: array_present_def new_ref_def ref_def Let_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   431
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   432
hide (open) const empty array array_of_list upd length ref
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   433
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   434
end