author  haftmann 
Fri, 09 Jul 2010 10:08:10 +0200  
changeset 37756  59caa6180fff 
parent 37752  d0a384c84d69 
child 37758  bf86a65403a8 
permissions  rwrr 
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 

37752  38 
definition (*FIXME update*) 
39 
change :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where 

37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

40 
"change a i x h = set_array a ((get_array a h)[i:=x]) h" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

41 

37752  42 
definition (*FIXME noteq*) 
43 
noteq_arrs :: "'a\<Colon>heap array \<Rightarrow> 'b\<Colon>heap array \<Rightarrow> bool" (infix "=!!=" 70) where 

44 
"r =!!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_array r \<noteq> addr_of_array s" 

45 

46 

47 
subsection {* Monad operations *} 

48 

49 
definition new :: "nat \<Rightarrow> 'a\<Colon>heap \<Rightarrow> 'a array Heap" where 

50 
[code del]: "new n x = Heap_Monad.heap (array (replicate n x))" 

51 

52 
definition of_list :: "'a\<Colon>heap list \<Rightarrow> 'a array Heap" where 

53 
[code del]: "of_list xs = Heap_Monad.heap (array xs)" 

54 

55 
definition make :: "nat \<Rightarrow> (nat \<Rightarrow> 'a\<Colon>heap) \<Rightarrow> 'a array Heap" where 

56 
[code del]: "make n f = Heap_Monad.heap (array (map f [0 ..< n]))" 

57 

58 
definition len :: "'a\<Colon>heap array \<Rightarrow> nat Heap" where 

59 
[code del]: "len a = Heap_Monad.heap (\<lambda>h. (length a h, h))" 

60 

61 
definition nth :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> 'a Heap" where 

62 
[code del]: "nth a i = Heap_Monad.guard (\<lambda>h. i < length a h) 

63 
(\<lambda>h. (get_array a h ! i, h))" 

64 

65 
definition upd :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a\<Colon>heap array Heap" where 

66 
[code del]: "upd i x a = Heap_Monad.guard (\<lambda>h. i < length a h) 

67 
(\<lambda>h. (a, change a i x h))" 

68 

69 
definition map_entry :: "nat \<Rightarrow> ('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a array \<Rightarrow> 'a array Heap" where 

70 
[code del]: "map_entry i f a = Heap_Monad.guard (\<lambda>h. i < length a h) 

71 
(\<lambda>h. (a, change a i (f (get_array a h ! i)) h))" 

72 

73 
definition swap :: "nat \<Rightarrow> 'a \<Rightarrow> 'a\<Colon>heap array \<Rightarrow> 'a Heap" where 

74 
[code del]: "swap i x a = Heap_Monad.guard (\<lambda>h. i < length a h) 

75 
(\<lambda>h. (get_array a h ! i, change a i x h))" 

76 

77 
definition freeze :: "'a\<Colon>heap array \<Rightarrow> 'a list Heap" where 

78 
[code del]: "freeze a = Heap_Monad.heap (\<lambda>h. (get_array a h, h))" 

79 

80 

81 
subsection {* Properties *} 

37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

82 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

83 
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

84 
the literature? *} 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

85 

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 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

110 
lemma get_array_change_eq [simp]: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

111 
"get_array a (change a i v h) = (get_array a h) [i := v]" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

112 
by (simp add: change_def) 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

113 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

114 
lemma nth_change_array_neq_array [simp]: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

115 
"a =!!= b \<Longrightarrow> get_array a (change b j v h) ! i = get_array a h ! i" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

116 
by (simp add: change_def noteq_arrs_def) 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

117 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

118 
lemma get_arry_array_change_elem_neqIndex [simp]: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

119 
"i \<noteq> j \<Longrightarrow> get_array a (change a j v h) ! i = get_array a h ! i" 
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 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

122 
lemma length_change [simp]: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

123 
"length a (change b i v h) = length a h" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

124 
by (simp add: change_def length_def set_array_def get_array_def) 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

125 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

126 
lemma change_swap_neqArray: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

127 
"a =!!= a' \<Longrightarrow> 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

128 
change a i v (change a' i' v' h) 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

129 
= change a' i' v' (change a i v h)" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

130 
apply (unfold change_def) 
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 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

138 
lemma change_swap_neqIndex: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

139 
"\<lbrakk> i \<noteq> i' \<rbrakk> \<Longrightarrow> change a i v (change a i' v' h) = change a i' v' (change a i v h)" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

140 
by (auto simp add: change_def array_set_set_swap list_update_swap) 
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 

271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

152 
lemma array_present_change [simp]: 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

153 
"array_present a (change b i v h) = array_present a h" 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

154 
by (simp add: change_def array_present_def set_array_def get_array_def) 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

155 

37752  156 
lemma execute_new [simp]: 
157 
"Heap_Monad.execute (new n x) h = Some (array (replicate n x) h)" 

158 
by (simp add: new_def) 

26170  159 

37752  160 
lemma execute_of_list [simp]: 
161 
"Heap_Monad.execute (of_list xs) h = Some (array xs h)" 

162 
by (simp add: of_list_def) 

26170  163 

37752  164 
lemma execute_make [simp]: 
165 
"Heap_Monad.execute (make n f) h = Some (array (map f [0 ..< n]) h)" 

166 
by (simp add: make_def) 

26170  167 

37752  168 
lemma execute_len [simp]: 
169 
"Heap_Monad.execute (len a) h = Some (length a h, h)" 

170 
by (simp add: len_def) 

171 

172 
lemma execute_nth [simp]: 

173 
"i < length a h \<Longrightarrow> 

174 
Heap_Monad.execute (nth a i) h = Some (get_array a h ! i, h)" 

175 
"i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None" 

176 
by (simp_all add: nth_def) 

26170  177 

37752  178 
lemma execute_upd [simp]: 
179 
"i < length a h \<Longrightarrow> 

180 
Heap_Monad.execute (upd i x a) h = Some (a, change a i x h)" 

181 
"i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None" 

182 
by (simp_all add: upd_def) 

26170  183 

37752  184 
lemma execute_map_entry [simp]: 
185 
"i < length a h \<Longrightarrow> 

186 
Heap_Monad.execute (map_entry i f a) h = 

187 
Some (a, change a i (f (get_array a h ! i)) h)" 

188 
"i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None" 

189 
by (simp_all add: map_entry_def) 

190 

191 
lemma execute_swap [simp]: 

192 
"i < length a h \<Longrightarrow> 

193 
Heap_Monad.execute (swap i x a) h = 

194 
Some (get_array a h ! i, change a i x h)" 

195 
"i \<ge> length a h \<Longrightarrow> Heap_Monad.execute (nth a i) h = None" 

196 
by (simp_all add: swap_def) 

197 

198 
lemma execute_freeze [simp]: 

199 
"Heap_Monad.execute (freeze a) h = Some (get_array a h, h)" 

200 
by (simp add: freeze_def) 

26170  201 

202 
lemma upd_return: 

203 
"upd i x a \<guillemotright> return a = upd i x 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

204 
by (rule Heap_eqI) (simp add: bind_def guard_def upd_def) 
26170  205 

37752  206 
lemma array_make: 
207 
"new n x = make n (\<lambda>_. x)" 

208 
by (rule Heap_eqI) (simp add: map_replicate_trivial) 

26170  209 

37752  210 
lemma array_of_list_make: 
211 
"of_list xs = make (List.length xs) (\<lambda>n. xs ! n)" 

212 
by (rule Heap_eqI) (simp add: map_nth) 

26170  213 

37752  214 
hide_const (open) new map 
26170  215 

26182  216 

217 
subsection {* Code generator setup *} 

218 

219 
subsubsection {* Logical intermediate layer *} 

220 

221 
definition new' where 

31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset

222 
[code del]: "new' = Array.new o Code_Numeral.nat_of" 
37752  223 

28562  224 
lemma [code]: 
37752  225 
"Array.new = new' o Code_Numeral.of_nat" 
26182  226 
by (simp add: new'_def o_def) 
227 

228 
definition of_list' where 

31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset

229 
[code del]: "of_list' i xs = Array.of_list (take (Code_Numeral.nat_of i) xs)" 
37752  230 

28562  231 
lemma [code]: 
37752  232 
"Array.of_list xs = of_list' (Code_Numeral.of_nat (List.length xs)) xs" 
26182  233 
by (simp add: of_list'_def) 
234 

235 
definition make' where 

31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset

236 
[code del]: "make' i f = Array.make (Code_Numeral.nat_of i) (f o Code_Numeral.of_nat)" 
37752  237 

28562  238 
lemma [code]: 
37752  239 
"Array.make n f = make' (Code_Numeral.of_nat n) (f o Code_Numeral.nat_of)" 
26182  240 
by (simp add: make'_def o_def) 
241 

37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

242 
definition len' where 
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37716
diff
changeset

243 
[code del]: "len' a = Array.len a \<guillemotright>= (\<lambda>n. return (Code_Numeral.of_nat n))" 
37752  244 

28562  245 
lemma [code]: 
37752  246 
"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

247 
by (simp add: len'_def) 
26182  248 

249 
definition nth' where 

31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset

250 
[code del]: "nth' a = Array.nth a o Code_Numeral.nat_of" 
37752  251 

28562  252 
lemma [code]: 
37752  253 
"Array.nth a n = nth' a (Code_Numeral.of_nat n)" 
26182  254 
by (simp add: nth'_def) 
255 

256 
definition upd' where 

31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset

257 
[code del]: "upd' a i x = Array.upd (Code_Numeral.nat_of i) x a \<guillemotright> return ()" 
37752  258 

28562  259 
lemma [code]: 
37752  260 
"Array.upd i x a = upd' a (Code_Numeral.of_nat i) x \<guillemotright> return a" 
37709  261 
by (simp add: upd'_def upd_return) 
26182  262 

37752  263 
lemma [code]: 
264 
"map_entry i f a = (do 

265 
x \<leftarrow> nth a i; 

266 
upd i (f x) a 

267 
done)" 

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

268 
by (rule Heap_eqI) (simp add: bind_def guard_def map_entry_def) 
26182  269 

37752  270 
lemma [code]: 
271 
"swap i x a = (do 

272 
y \<leftarrow> nth a i; 

273 
upd i x a; 

274 
return y 

275 
done)" 

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

276 
by (rule Heap_eqI) (simp add: bind_def guard_def swap_def) 
37752  277 

278 
lemma [code]: 

279 
"freeze a = (do 

280 
n \<leftarrow> 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

281 
Heap_Monad.fold_map (\<lambda>i. nth a i) [0..<n] 
37752  282 
done)" 
283 
proof (rule Heap_eqI) 

284 
fix h 

285 
have *: "List.map 

286 
(\<lambda>x. fst (the (if x < length a h 

287 
then Some (get_array a h ! x, h) else None))) 

288 
[0..<length a h] = 

289 
List.map (List.nth (get_array a h)) [0..<length a h]" 

290 
by simp 

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

291 
have "Heap_Monad.execute (Heap_Monad.fold_map (Array.nth a) [0..<length a h]) h = 
37752  292 
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

293 
apply (subst execute_fold_map_unchanged_heap) 
37752  294 
apply (simp_all add: nth_def guard_def *) 
295 
apply (simp add: length_def map_nth) 

296 
done 

297 
then have "Heap_Monad.execute (do 

298 
n \<leftarrow> 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

299 
Heap_Monad.fold_map (Array.nth a) [0..<n] 
37752  300 
done) h = Some (get_array a h, h)" 
301 
by (auto intro: execute_eq_SomeI) 

302 
then show "Heap_Monad.execute (freeze a) h = Heap_Monad.execute (do 

303 
n \<leftarrow> 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

304 
Heap_Monad.fold_map (Array.nth a) [0..<n] 
37752  305 
done) h" by simp 
306 
qed 

307 

308 
hide_const (open) new' of_list' make' len' nth' upd' 

309 

310 

311 
text {* SML *} 

26182  312 

313 
code_type array (SML "_/ array") 

314 
code_const Array (SML "raise/ (Fail/ \"bare Array\")") 

26752  315 
code_const Array.new' (SML "(fn/ ()/ =>/ Array.array/ ((_),/ (_)))") 
35846  316 
code_const Array.of_list' (SML "(fn/ ()/ =>/ Array.fromList/ _)") 
26752  317 
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

318 
code_const Array.len' (SML "(fn/ ()/ =>/ Array.length/ _)") 
26752  319 
code_const Array.nth' (SML "(fn/ ()/ =>/ Array.sub/ ((_),/ (_)))") 
320 
code_const Array.upd' (SML "(fn/ ()/ =>/ Array.update/ ((_),/ (_),/ (_)))") 

26182  321 

322 
code_reserved SML Array 

323 

324 

37752  325 
text {* OCaml *} 
26182  326 

327 
code_type array (OCaml "_/ array") 

328 
code_const Array (OCaml "failwith/ \"bare Array\"") 

32580  329 
code_const Array.new' (OCaml "(fun/ ()/ >/ Array.make/ (Big'_int.int'_of'_big'_int/ _)/ _)") 
35846  330 
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

331 
code_const Array.len' (OCaml "(fun/ ()/ >/ Big'_int.big'_int'_of'_int/ (Array.length/ _))") 
32580  332 
code_const Array.nth' (OCaml "(fun/ ()/ >/ Array.get/ _/ (Big'_int.int'_of'_big'_int/ _))") 
333 
code_const Array.upd' (OCaml "(fun/ ()/ >/ Array.set/ _/ (Big'_int.int'_of'_big'_int/ _)/ _)") 

26182  334 

335 
code_reserved OCaml Array 

336 

337 

37752  338 
text {* Haskell *} 
26182  339 

29793  340 
code_type array (Haskell "Heap.STArray/ Heap.RealWorld/ _") 
26182  341 
code_const Array (Haskell "error/ \"bare Array\"") 
29793  342 
code_const Array.new' (Haskell "Heap.newArray/ (0,/ _)") 
343 
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

344 
code_const Array.len' (Haskell "Heap.lengthArray") 
29793  345 
code_const Array.nth' (Haskell "Heap.readArray") 
346 
code_const Array.upd' (Haskell "Heap.writeArray") 

26182  347 

26170  348 
end 