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