author | haftmann |
Tue, 13 Jul 2010 12:01:34 +0200 | |
changeset 37796 | 08bd610b2583 |
parent 37787 | 30dc3abf4a58 |
child 37797 | 96551d6b1414 |
permissions | -rw-r--r-- |
31870 | 1 |
(* Title: HOL/Imperative_HOL/Array.thy |
26170 | 2 |
Author: John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen |
3 |
*) |
|
4 |
||
5 |
header {* Monadic arrays *} |
|
6 |
||
7 |
theory Array |
|
31203
5c8fb4fd67e0
moved Code_Index, Random and Quickcheck before Main
haftmann
parents:
29822
diff
changeset
|
8 |
imports Heap_Monad |
26170 | 9 |
begin |
10 |
||
37752 | 11 |
subsection {* Primitives *} |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
12 |
|
37752 | 13 |
definition (*FIXME present :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> bool" where*) |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
14 |
array_present :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> bool" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
15 |
"array_present a h \<longleftrightarrow> addr_of_array a < lim h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
16 |
|
37752 | 17 |
definition (*FIXME get :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a list" where*) |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
18 |
get_array :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> 'a list" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
19 |
"get_array a h = map from_nat (arrays h (TYPEREP('a)) (addr_of_array a))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
20 |
|
37752 | 21 |
definition (*FIXME set*) |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
22 |
set_array :: "'a\<Colon>heap array \<Rightarrow> 'a list \<Rightarrow> heap \<Rightarrow> heap" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
23 |
"set_array a x = |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
24 |
arrays_update (\<lambda>h. h(TYPEREP('a) := ((h(TYPEREP('a))) (addr_of_array a:=map to_nat x))))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
25 |
|
37752 | 26 |
definition (*FIXME alloc*) |
27 |
array :: "'a list \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap array \<times> heap" where |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
28 |
"array xs h = (let |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
29 |
l = lim h; |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
30 |
r = Array l; |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
31 |
h'' = set_array r xs (h\<lparr>lim := l + 1\<rparr>) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
32 |
in (r, h''))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
33 |
|
37752 | 34 |
definition (*FIXME length :: "heap \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> nat" where*) |
35 |
length :: "'a\<Colon>heap array \<Rightarrow> heap \<Rightarrow> nat" where |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
36 |
"length a h = List.length (get_array a h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
37 |
|
37796 | 38 |
definition update :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where |
39 |
"update a i x h = set_array a ((get_array a h)[i:=x]) h" |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
40 |
|
37752 | 41 |
definition (*FIXME noteq*) |
42 |
noteq_arrs :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where |
|
43 |
"r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s" |
|
44 |
||
45 |
||
46 |
subsection {* Monad operations *} |
|
47 |
||
48 |
definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where |
|
49 |
[code del]: "new n x = Heap_Monad.heap (array (replicate n x))" |
|
50 |
||
51 |
definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where |
|
52 |
[code del]: "of_list xs = Heap_Monad.heap (array xs)" |
|
53 |
||
54 |
definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap" where |
|
55 |
[code del]: "make n f = Heap_Monad.heap (array (map f [0 ..< n]))" |
|
56 |
||
57 |
definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where |
|
37758 | 58 |
[code del]: "len a = Heap_Monad.tap (\<lambda>h. length a h)" |
37752 | 59 |
|
60 |
definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where |
|
61 |
[code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length a h) |
|
62 |
(\<lambda>h. (get_array a h ! i, h))" |
|
63 |
||
64 |
definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where |
|
65 |
[code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length a h) |
|
37796 | 66 |
(\<lambda>h. (a, update a i x h))" |
37752 | 67 |
|
68 |
definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where |
|
69 |
[code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length a h) |
|
37796 | 70 |
(\<lambda>h. (a, update a i (f (get_array a h ! i)) h))" |
37752 | 71 |
|
72 |
definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where |
|
73 |
[code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length a h) |
|
37796 | 74 |
(\<lambda>h. (get_array a h ! i, update a i x h))" |
37752 | 75 |
|
76 |
definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where |
|
37758 | 77 |
[code del]: "freeze a = Heap_Monad.tap (\<lambda>h. get_array a h)" |
37752 | 78 |
|
79 |
||
80 |
subsection {* Properties *} |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
81 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
82 |
text {* FIXME: Does there exist a "canonical" array axiomatisation in |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
83 |
the literature? *} |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
84 |
|
37758 | 85 |
text {* Primitives *} |
86 |
||
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
87 |
lemma noteq_arrs_sym: "a =!!= b \<Longrightarrow> b =!!= a" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
88 |
and unequal_arrs [simp]: "a \<noteq> a' \<longleftrightarrow> a =!!= a'" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
89 |
unfolding noteq_arrs_def by auto |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
90 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
91 |
lemma noteq_arrs_irrefl: "r =!!= r \<Longrightarrow> False" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
92 |
unfolding noteq_arrs_def by auto |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
93 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
94 |
lemma present_new_arr: "array_present a h \<Longrightarrow> a =!!= fst (array xs h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
95 |
by (simp add: array_present_def noteq_arrs_def array_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
96 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
97 |
lemma array_get_set_eq [simp]: "get_array r (set_array r x h) = x" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
98 |
by (simp add: get_array_def set_array_def o_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
99 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
100 |
lemma array_get_set_neq [simp]: "r =!!= s \<Longrightarrow> get_array r (set_array s x h) = get_array r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
101 |
by (simp add: noteq_arrs_def get_array_def set_array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
102 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
103 |
lemma set_array_same [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
104 |
"set_array r x (set_array r y h) = set_array r x h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
105 |
by (simp add: set_array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
106 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
107 |
lemma array_set_set_swap: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
108 |
"r =!!= r' \<Longrightarrow> set_array r x (set_array r' x' h) = set_array r' x' (set_array r x h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
109 |
by (simp add: Let_def expand_fun_eq noteq_arrs_def set_array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
110 |
|
37796 | 111 |
lemma get_array_update_eq [simp]: |
112 |
"get_array a (update a i v h) = (get_array a h) [i := v]" |
|
113 |
by (simp add: update_def) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
114 |
|
37796 | 115 |
lemma nth_update_array_neq_array [simp]: |
116 |
"a =!!= b \<Longrightarrow> get_array a (update b j v h) ! i = get_array a h ! i" |
|
117 |
by (simp add: update_def noteq_arrs_def) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
118 |
|
37796 | 119 |
lemma get_arry_array_update_elem_neqIndex [simp]: |
120 |
"i \<noteq> j \<Longrightarrow> get_array a (update a j v h) ! i = get_array a h ! i" |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
121 |
by simp |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
122 |
|
37796 | 123 |
lemma length_update [simp]: |
124 |
"length a (update b i v h) = length a h" |
|
125 |
by (simp add: update_def length_def set_array_def get_array_def) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
126 |
|
37796 | 127 |
lemma update_swap_neqArray: |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
128 |
"a =!!= a' \<Longrightarrow> |
37796 | 129 |
update a i v (update a' i' v' h) |
130 |
= update a' i' v' (update a i v h)" |
|
131 |
apply (unfold update_def) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
132 |
apply simp |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
133 |
apply (subst array_set_set_swap, assumption) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
134 |
apply (subst array_get_set_neq) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
135 |
apply (erule noteq_arrs_sym) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
136 |
apply (simp) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
137 |
done |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
138 |
|
37796 | 139 |
lemma update_swap_neqIndex: |
140 |
"\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> update a i v (update a i' v' h) = update a i' v' (update a i v h)" |
|
141 |
by (auto simp add: update_def array_set_set_swap list_update_swap) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
142 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
143 |
lemma get_array_init_array_list: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
144 |
"get_array (fst (array ls h)) (snd (array ls' h)) = ls'" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
145 |
by (simp add: Let_def split_def array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
146 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
147 |
lemma set_array: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
148 |
"set_array (fst (array ls h)) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
149 |
new_ls (snd (array ls h)) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
150 |
= snd (array new_ls h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
151 |
by (simp add: Let_def split_def array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
152 |
|
37796 | 153 |
lemma array_present_update [simp]: |
154 |
"array_present a (update b i v h) = array_present a h" |
|
155 |
by (simp add: update_def array_present_def set_array_def get_array_def) |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
156 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
157 |
lemma array_present_array [simp]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
158 |
"array_present (fst (array xs h)) (snd (array xs h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
159 |
by (simp add: array_present_def array_def set_array_def Let_def) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
160 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
161 |
lemma not_array_present_array [simp]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
162 |
"\<not> array_present (fst (array xs h)) h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
163 |
by (simp add: array_present_def array_def Let_def) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
164 |
|
37758 | 165 |
|
166 |
text {* Monad operations *} |
|
167 |
||
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
168 |
lemma execute_new [execute_simps]: |
37758 | 169 |
"execute (new n x) h = Some (array (replicate n x) h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
170 |
by (simp add: new_def execute_simps) |
37758 | 171 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
172 |
lemma success_newI [success_intros]: |
37758 | 173 |
"success (new n x) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
174 |
by (auto intro: success_intros simp add: new_def) |
26170 | 175 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
176 |
lemma crel_newI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
177 |
assumes "(a, h') = array (replicate n x) h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
178 |
shows "crel (new n x) h h' a" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
179 |
by (rule crelI) (simp add: assms execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
180 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
181 |
lemma crel_newE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
182 |
assumes "crel (new n x) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
183 |
obtains "r = fst (array (replicate n x) h)" "h' = snd (array (replicate n x) h)" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
184 |
"get_array r h' = replicate n x" "array_present r h'" "\<not> array_present r h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
185 |
using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
186 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
187 |
lemma execute_of_list [execute_simps]: |
37758 | 188 |
"execute (of_list xs) h = Some (array xs h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
189 |
by (simp add: of_list_def execute_simps) |
37758 | 190 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
191 |
lemma success_of_listI [success_intros]: |
37758 | 192 |
"success (of_list xs) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
193 |
by (auto intro: success_intros simp add: of_list_def) |
26170 | 194 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
195 |
lemma crel_of_listI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
196 |
assumes "(a, h') = array xs h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
197 |
shows "crel (of_list xs) h h' a" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
198 |
by (rule crelI) (simp add: assms execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
199 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
200 |
lemma crel_of_listE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
201 |
assumes "crel (of_list xs) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
202 |
obtains "r = fst (array xs h)" "h' = snd (array xs h)" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
203 |
"get_array r h' = xs" "array_present r h'" "\<not> array_present r h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
204 |
using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
205 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
206 |
lemma execute_make [execute_simps]: |
37758 | 207 |
"execute (make n f) h = Some (array (map f [0 ..< n]) h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
208 |
by (simp add: make_def execute_simps) |
26170 | 209 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
210 |
lemma success_makeI [success_intros]: |
37758 | 211 |
"success (make n f) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
212 |
by (auto intro: success_intros simp add: make_def) |
37758 | 213 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
214 |
lemma crel_makeI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
215 |
assumes "(a, h') = array (map f [0 ..< n]) h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
216 |
shows "crel (make n f) h h' a" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
217 |
by (rule crelI) (simp add: assms execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
218 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
219 |
lemma crel_makeE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
220 |
assumes "crel (make n f) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
221 |
obtains "r = fst (array (map f [0 ..< n]) h)" "h' = snd (array (map f [0 ..< n]) h)" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
222 |
"get_array r h' = map f [0 ..< n]" "array_present r h'" "\<not> array_present r h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
223 |
using assms by (rule crelE) (simp add: get_array_init_array_list execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
224 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
225 |
lemma execute_len [execute_simps]: |
37758 | 226 |
"execute (len a) h = Some (length a h, h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
227 |
by (simp add: len_def execute_simps) |
37758 | 228 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
229 |
lemma success_lenI [success_intros]: |
37758 | 230 |
"success (len a) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
231 |
by (auto intro: success_intros simp add: len_def) |
37752 | 232 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
233 |
lemma crel_lengthI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
234 |
assumes "h' = h" "r = length a h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
235 |
shows "crel (len a) h h' r" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
236 |
by (rule crelI) (simp add: assms execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
237 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
238 |
lemma crel_lengthE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
239 |
assumes "crel (len a) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
240 |
obtains "r = length a h'" "h' = h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
241 |
using assms by (rule crelE) (simp add: execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
242 |
|
37758 | 243 |
lemma execute_nth [execute_simps]: |
37752 | 244 |
"i < length a h \<Longrightarrow> |
37758 | 245 |
execute (nth a i) h = Some (get_array a h ! i, h)" |
246 |
"i \<ge> length a h \<Longrightarrow> execute (nth a i) h = None" |
|
247 |
by (simp_all add: nth_def execute_simps) |
|
248 |
||
249 |
lemma success_nthI [success_intros]: |
|
250 |
"i < length a h \<Longrightarrow> success (nth a i) h" |
|
251 |
by (auto intro: success_intros simp add: nth_def) |
|
26170 | 252 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
253 |
lemma crel_nthI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
254 |
assumes "i < length a h" "h' = h" "r = get_array a h ! i" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
255 |
shows "crel (nth a i) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
256 |
by (rule crelI) (insert assms, simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
257 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
258 |
lemma crel_nthE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
259 |
assumes "crel (nth a i) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
260 |
obtains "i < length a h" "r = get_array a h ! i" "h' = h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
261 |
using assms by (rule crelE) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
262 |
(erule successE, cases "i < length a h", simp_all add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
263 |
|
37758 | 264 |
lemma execute_upd [execute_simps]: |
37752 | 265 |
"i < length a h \<Longrightarrow> |
37796 | 266 |
execute (upd i x a) h = Some (a, update a i x h)" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
267 |
"i \<ge> length a h \<Longrightarrow> execute (upd i x a) h = None" |
37758 | 268 |
by (simp_all add: upd_def execute_simps) |
26170 | 269 |
|
37758 | 270 |
lemma success_updI [success_intros]: |
271 |
"i < length a h \<Longrightarrow> success (upd i x a) h" |
|
272 |
by (auto intro: success_intros simp add: upd_def) |
|
273 |
||
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
274 |
lemma crel_updI [crel_intros]: |
37796 | 275 |
assumes "i < length a h" "h' = update a i v h" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
276 |
shows "crel (upd i v a) h h' a" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
277 |
by (rule crelI) (insert assms, simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
278 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
279 |
lemma crel_updE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
280 |
assumes "crel (upd i v a) h h' r" |
37796 | 281 |
obtains "r = a" "h' = update a i v h" "i < length a h" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
282 |
using assms by (rule crelE) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
283 |
(erule successE, cases "i < length a h", simp_all add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
284 |
|
37758 | 285 |
lemma execute_map_entry [execute_simps]: |
37752 | 286 |
"i < length a h \<Longrightarrow> |
37758 | 287 |
execute (map_entry i f a) h = |
37796 | 288 |
Some (a, update a i (f (get_array a h ! i)) h)" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
289 |
"i \<ge> length a h \<Longrightarrow> execute (map_entry i f a) h = None" |
37758 | 290 |
by (simp_all add: map_entry_def execute_simps) |
37752 | 291 |
|
37758 | 292 |
lemma success_map_entryI [success_intros]: |
293 |
"i < length a h \<Longrightarrow> success (map_entry i f a) h" |
|
294 |
by (auto intro: success_intros simp add: map_entry_def) |
|
295 |
||
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
296 |
lemma crel_map_entryI [crel_intros]: |
37796 | 297 |
assumes "i < length a h" "h' = update a i (f (get_array a h ! i)) h" "r = a" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
298 |
shows "crel (map_entry i f a) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
299 |
by (rule crelI) (insert assms, simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
300 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
301 |
lemma crel_map_entryE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
302 |
assumes "crel (map_entry i f a) h h' r" |
37796 | 303 |
obtains "r = a" "h' = update a i (f (get_array a h ! i)) h" "i < length a h" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
304 |
using assms by (rule crelE) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
305 |
(erule successE, cases "i < length a h", simp_all add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
306 |
|
37758 | 307 |
lemma execute_swap [execute_simps]: |
37752 | 308 |
"i < length a h \<Longrightarrow> |
37758 | 309 |
execute (swap i x a) h = |
37796 | 310 |
Some (get_array a h ! i, update a i x h)" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
311 |
"i \<ge> length a h \<Longrightarrow> execute (swap i x a) h = None" |
37758 | 312 |
by (simp_all add: swap_def execute_simps) |
313 |
||
314 |
lemma success_swapI [success_intros]: |
|
315 |
"i < length a h \<Longrightarrow> success (swap i x a) h" |
|
316 |
by (auto intro: success_intros simp add: swap_def) |
|
37752 | 317 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
318 |
lemma crel_swapI [crel_intros]: |
37796 | 319 |
assumes "i < length a h" "h' = update a i x h" "r = get_array a h ! i" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
320 |
shows "crel (swap i x a) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
321 |
by (rule crelI) (insert assms, simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
322 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
323 |
lemma crel_swapE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
324 |
assumes "crel (swap i x a) h h' r" |
37796 | 325 |
obtains "r = get_array a h ! i" "h' = update a i x h" "i < length a h" |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
326 |
using assms by (rule crelE) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
327 |
(erule successE, cases "i < length a h", simp_all add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
328 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
329 |
lemma execute_freeze [execute_simps]: |
37758 | 330 |
"execute (freeze a) h = Some (get_array a h, h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
331 |
by (simp add: freeze_def execute_simps) |
37758 | 332 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
333 |
lemma success_freezeI [success_intros]: |
37758 | 334 |
"success (freeze a) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
335 |
by (auto intro: success_intros simp add: freeze_def) |
26170 | 336 |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
337 |
lemma crel_freezeI [crel_intros]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
338 |
assumes "h' = h" "r = get_array a h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
339 |
shows "crel (freeze a) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
340 |
by (rule crelI) (insert assms, simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
341 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
342 |
lemma crel_freezeE [crel_elims]: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
343 |
assumes "crel (freeze a) h h' r" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
344 |
obtains "h' = h" "r = get_array a h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
345 |
using assms by (rule crelE) (simp add: execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
346 |
|
26170 | 347 |
lemma upd_return: |
348 |
"upd i x a \<guillemotright> return a = upd i x a" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
349 |
by (rule Heap_eqI) (simp add: bind_def guard_def upd_def execute_simps) |
26170 | 350 |
|
37752 | 351 |
lemma array_make: |
352 |
"new n x = make n (\<lambda>_. x)" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
353 |
by (rule Heap_eqI) (simp add: map_replicate_trivial execute_simps) |
26170 | 354 |
|
37752 | 355 |
lemma array_of_list_make: |
356 |
"of_list xs = make (List.length xs) (\<lambda>n. xs ! n)" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
357 |
by (rule Heap_eqI) (simp add: map_nth execute_simps) |
26170 | 358 |
|
37796 | 359 |
hide_const (open) update new of_list make len nth upd map_entry swap freeze |
26170 | 360 |
|
26182 | 361 |
|
362 |
subsection {* Code generator setup *} |
|
363 |
||
364 |
subsubsection {* Logical intermediate layer *} |
|
365 |
||
366 |
definition new' where |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
367 |
[code del]: "new' = Array.new o Code_Numeral.nat_of" |
37752 | 368 |
|
28562 | 369 |
lemma [code]: |
37752 | 370 |
"Array.new = new' o Code_Numeral.of_nat" |
26182 | 371 |
by (simp add: new'_def o_def) |
372 |
||
373 |
definition of_list' where |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
374 |
[code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)" |
37752 | 375 |
|
28562 | 376 |
lemma [code]: |
37752 | 377 |
"Array.of_list xs = of_list' (Code_Numeral.of_nat (List.length xs)) xs" |
26182 | 378 |
by (simp add: of_list'_def) |
379 |
||
380 |
definition make' where |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
381 |
[code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)" |
37752 | 382 |
|
28562 | 383 |
lemma [code]: |
37752 | 384 |
"Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)" |
26182 | 385 |
by (simp add: make'_def o_def) |
386 |
||
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
387 |
definition len' where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
388 |
[code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))" |
37752 | 389 |
|
28562 | 390 |
lemma [code]: |
37752 | 391 |
"Array.len a = len' a \<guillemotright>= (\<lambda>i. return (Code_Numeral.nat_of i))" |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
392 |
by (simp add: len'_def) |
26182 | 393 |
|
394 |
definition nth' where |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
395 |
[code del]: "nth' a = Array.nth a o Code_Numeral.nat_of" |
37752 | 396 |
|
28562 | 397 |
lemma [code]: |
37752 | 398 |
"Array.nth a n = nth' a (Code_Numeral.of_nat n)" |
26182 | 399 |
by (simp add: nth'_def) |
400 |
||
401 |
definition upd' where |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
402 |
[code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()" |
37752 | 403 |
|
28562 | 404 |
lemma [code]: |
37752 | 405 |
"Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a" |
37709 | 406 |
by (simp add: upd'_def upd_return) |
26182 | 407 |
|
37752 | 408 |
lemma [code]: |
37796 | 409 |
"Array.map_entry i f a = (do |
410 |
x \<leftarrow> Array.nth a i; |
|
411 |
Array.upd i (f x) a |
|
37752 | 412 |
done)" |
37758 | 413 |
by (rule Heap_eqI) (simp add: bind_def guard_def map_entry_def execute_simps) |
26182 | 414 |
|
37752 | 415 |
lemma [code]: |
37796 | 416 |
"Array.swap i x a = (do |
417 |
y \<leftarrow> Array.nth a i; |
|
418 |
Array.upd i x a; |
|
37752 | 419 |
return y |
420 |
done)" |
|
37758 | 421 |
by (rule Heap_eqI) (simp add: bind_def guard_def swap_def execute_simps) |
37752 | 422 |
|
423 |
lemma [code]: |
|
37796 | 424 |
"Array.freeze a = (do |
425 |
n \<leftarrow> Array.len a; |
|
426 |
Heap_Monad.fold_map (\<lambda>i. Array.nth a i) [0..<n] |
|
37752 | 427 |
done)" |
428 |
proof (rule Heap_eqI) |
|
429 |
fix h |
|
430 |
have *: "List.map |
|
431 |
(\<lambda>x. fst (the (if x < length a h |
|
432 |
then Some (get_array a h ! x, h) else None))) |
|
433 |
[0..<length a h] = |
|
434 |
List.map (List.nth (get_array a h)) [0..<length a h]" |
|
435 |
by simp |
|
37758 | 436 |
have "execute (Heap_Monad.fold_map (Array.nth a) [0..<length a h]) h = |
37752 | 437 |
Some (get_array a h, h)" |
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37752
diff
changeset
|
438 |
apply (subst execute_fold_map_unchanged_heap) |
37752 | 439 |
apply (simp_all add: nth_def guard_def *) |
440 |
apply (simp add: length_def map_nth) |
|
441 |
done |
|
37758 | 442 |
then have "execute (do |
37796 | 443 |
n \<leftarrow> Array.len a; |
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37752
diff
changeset
|
444 |
Heap_Monad.fold_map (Array.nth a) [0..<n] |
37752 | 445 |
done) h = Some (get_array a h, h)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
446 |
by (auto intro: execute_bind_eq_SomeI simp add: execute_simps) |
37796 | 447 |
then show "execute (Array.freeze a) h = execute (do |
448 |
n \<leftarrow> Array.len a; |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37752
diff
changeset
|
449 |
Heap_Monad.fold_map (Array.nth a) [0..<n] |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37771
diff
changeset
|
450 |
done) h" by (simp add: execute_simps) |
37752 | 451 |
qed |
452 |
||
453 |
hide_const (open) new' of_list' make' len' nth' upd' |
|
454 |
||
455 |
||
456 |
text {* SML *} |
|
26182 | 457 |
|
458 |
code_type array (SML "_/ array") |
|
459 |
code_const Array (SML "raise/ (Fail/ \"bare Array\")") |
|
26752 | 460 |
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))") |
35846 | 461 |
code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)") |
26752 | 462 |
code_const Array.make' (SML "(fn/ ()/ =>/ Array.tabulate/ ((_),/ (_)))") |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
463 |
code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)") |
26752 | 464 |
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))") |
465 |
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))") |
|
26182 | 466 |
|
467 |
code_reserved SML Array |
|
468 |
||
469 |
||
37752 | 470 |
text {* OCaml *} |
26182 | 471 |
|
472 |
code_type array (OCaml "_/ array") |
|
473 |
code_const Array (OCaml "failwith/ \"bare Array\"") |
|
32580 | 474 |
code_const Array.new' (OCaml "(fun/ ()/ ->/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)") |
35846 | 475 |
code_const Array.of_list' (OCaml "(fun/ ()/ ->/ Array.of'_list/ _)") |
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
476 |
code_const Array.len' (OCaml "(fun/ ()/ ->/ Big'_int.big'_int'_of'_int/ (Array.length/ _))") |
32580 | 477 |
code_const Array.nth' (OCaml "(fun/ ()/ ->/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))") |
478 |
code_const Array.upd' (OCaml "(fun/ ()/ ->/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)") |
|
26182 | 479 |
|
480 |
code_reserved OCaml Array |
|
481 |
||
482 |
||
37752 | 483 |
text {* Haskell *} |
26182 | 484 |
|
29793 | 485 |
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _") |
26182 | 486 |
code_const Array (Haskell "error/ \"bare Array\"") |
29793 | 487 |
code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)") |
488 |
code_const Array.of_list' (Haskell "Heap.newListArray/ (0,/ _)") |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset
|
489 |
code_const Array.len' (Haskell "Heap.lengthArray") |
29793 | 490 |
code_const Array.nth' (Haskell "Heap.readArray") |
491 |
code_const Array.upd' (Haskell "Heap.writeArray") |
|
26182 | 492 |
|
26170 | 493 |
end |