64
64
fun domain :: "('b::bot) state_map \<Rightarrow> addr set" where
65
65
"domain (SM m) = {a. m a \<noteq> \<bottom>}"
66
66
67
- fun deepen :: "(addr * 'a) set \<Rightarrow> 'a set state_map" where
68
- "deepen states = SM (\<lambda>pc. {st. (\<exists>(spc, st) \<in> states. pc = spc)})"
69
-
70
- fun flatten :: "'a set state_map \<Rightarrow> (addr * 'a) set" where
71
- "flatten sm = {(pc, st). st \<in> lookup sm pc}"
72
-
73
67
lemma state_map_eq_fwd : "(\<forall>p. lookup m p = lookup n p) \<Longrightarrow> m = n"
74
68
proof -
75
69
assume lookeq : "\<forall>p. lookup m p = lookup n p"
@@ -184,6 +178,24 @@ instance proof standard
184
178
qed
185
179
end
186
180
181
+ inductive_set states_at for states pc where
182
+ "(pc, s) \<in> states \<Longrightarrow> s \<in> states_at states pc"
183
+
184
+ fun deepen :: "(addr * 'a) set \<Rightarrow> 'a set state_map" where
185
+ "deepen states = SM (states_at states)"
186
+
187
+ lemma deepen_fwd : "(pc, st) \<in> flat \<Longrightarrow> st \<in> lookup (deepen flat) pc" by ( simp add : states_at.intros )
188
+ lemma deepen_bwd : "st \<in> lookup (deepen flat) pc \<Longrightarrow> (pc, st) \<in> flat" by ( simp add : states_at.simps )
189
+
190
+ (*fun flatten :: "'a set state_map \<Rightarrow> (addr * 'a) set" where
191
+ "flatten sm = {(pc, st). st \<in> lookup sm pc}"*)
192
+
193
+ inductive_set flatten for sm where
194
+ "st \<in> lookup sm pc \<Longrightarrow> (pc, st) \<in> flatten sm"
195
+
196
+ lemma flatten_fwd : "st \<in> lookup deep pc \<Longrightarrow> (pc, st) \<in> flatten deep" by ( simp add : flatten.intros )
197
+ lemma flatten_bwd : "(pc, st) \<in> flatten deep \<Longrightarrow> st \<in> lookup deep pc" by ( meson flatten.cases )
198
+
187
199
subsection "Collecting Semantics"
188
200
189
201
type_synonym collect_state = "stack * rstate * flag * nat list"
@@ -192,69 +204,46 @@ type_synonym collect_ctx = "collect_state set state_map"
192
204
fun states_domain :: "(addr * 'a) set \<Rightarrow> addr set" where
193
205
"states_domain states = fst ` states"
194
206
195
- fun states_at :: "(addr * 'a) set \<Rightarrow> addr \<Rightarrow> 'a set" where
196
- "states_at states pc = snd ` {s\<in>states. fst s = pc}"
197
-
198
207
fun propagate :: "'a set state_map \<Rightarrow> (addr * 'a) set \<Rightarrow> 'a set state_map" where
199
- "propagate (SM oldmap) ss =
200
- (let newmap = (\<lambda>pc. oldmap pc \<union> (states_at ss pc))
201
- in (SM newmap))"
208
+ "propagate oldmap ss = oldmap \<squnion> deepen ss"
202
209
203
- lemma propagate_preserve : "inm \<le> propagate inm sts" sorry
210
+ lemma propagate_preserve : "inm \<le> propagate inm sts" by simp
204
211
205
- lemma mono_inside : "a \<le> b \<Longrightarrow> x \<in> lookup_def a pc \<Longrightarrow> x \<in> lookup_def b pc" sorry
206
- lemma inside_mono : "x \<in> lookup_def a pc \<Longrightarrow> x \<in> lookup_def b pc \<Longrightarrow> a \<le> b" sorry
212
+ inductive_set stepped_to for prog ctx pc where
213
+ "ist \<in> lookup ctx ipc
214
+ \<Longrightarrow> program ipc = Some op
215
+ \<Longrightarrow> step op (ipc, ist) = Some (pc, st)
216
+ \<Longrightarrow> st \<in> stepped_to prog ctx pc"
207
217
208
- definition step_all :: "instr \<Rightarrow> addr \<Rightarrow> collect_state set \<Rightarrow> (state set * interpret_error set)" where
209
- "step_all op pc instates =
210
- ({outs. (\<exists>ins\<in>instates. step op (pc, ins) = Some outs)},
211
- (if (\<exists>ins\<in>instates. step op (pc, ins) = None) then {StepFailed pc} else {}))"
212
-
213
- fun collect_step_single :: "program \<Rightarrow> addr \<Rightarrow> (collect_ctx * interpret_error set) \<Rightarrow> (collect_ctx * interpret_error set)" where
214
- "collect_step_single prog pc (ctx, inerrs) =
215
- (case prog pc of
216
- Some op \<Rightarrow>
217
- let res = step_all op pc (lookup ctx pc) in
218
- (propagate ctx (fst res), snd res \<union> inerrs) |
219
- None \<Rightarrow> (ctx, { InvalAddr pc } \<union> inerrs))"
220
-
221
- lemma collect_step_single_preserve :
222
- assumes "collect_step_single prog pc (inctx, inerrs) = (outctx, errors)"
223
- shows "inctx \<le> outctx"
224
- proof ( cases "prog pc" )
225
- case None then show ?thesis using assms by simp
226
- next
227
- case ( Some op )
228
- from this assms have "outctx = propagate inctx (fst (step_all op pc (lookup inctx pc)))" unfolding Let_def
229
- by ( metis ( no_types , lifting ) collect_step_single.simps fstI option.simps ( 5 ))
230
- then show ?thesis using propagate_preserve by blast
231
- qed
218
+ (*lemma "stepped_to prog ctx pc = {st. \<exists>ipc ist op. (ist \<in> lookup ctx ipc) \<and> (program ipc = Some op) \<and> (step op (ipc, ist) = Some (pc, st))}"*)
232
219
233
- fun collect_step :: "program \<Rightarrow> collect_ctx \<Rightarrow> collect_ctx" where
234
- "collect_step prog ctx =
235
- fst (fold (collect_step_single prog) (sorted_list_of_set (domain ctx)) (ctx, {}))"
236
-
237
- lemma fold_preserve : "(\<forall>x acc. acc \<le> f x acc) \<Longrightarrow> (a::'a::order) \<le> fold f l a"
238
- proof ( induction l arbitrary : a )
239
- case Nil
240
- have "fold f [] a = a" by simp
241
- have "a \<le> a" by auto
242
- then show ?case by auto
243
- next
244
- case ( Cons el l )
245
- hence unf : "f el a \<le> fold f (el # l) a" by simp
246
- from Cons have "a \<le> f el a" by simp
247
- from unf this Cons ( 2 ) show ?case using order.trans by blast
248
- qed
220
+ fun step_all :: "program \<Rightarrow> collect_ctx \<Rightarrow> collect_ctx" where
221
+ "step_all prog ctx = SM (stepped_to prog ctx)"
249
222
250
- lemma fold_preserve_option : "(\<forall>x acc accy. (f x (Some acc) = Some accy) \<longrightarrow> (acc \<le> accy)) \<Longrightarrow> (\<forall>x. f x None = None) \<Longrightarrow> fold f l (Some a) = Some ay \<Longrightarrow> (a::'a::order) \<le> ay" sorry
223
+ (*definition step_all :: "instr \<Rightarrow> addr \<Rightarrow> collect_state set \<Rightarrow> state set" where
224
+ "step_all op pc instates =
225
+ {outs. (\<exists>ins\<in>instates. step op (pc, ins) = Some outs)}"*)
251
226
252
- lemma collect_step_preserve : "collect_step prog inctx = outctx \<Longrightarrow> inctx \<le> outctx"
253
- proof -
254
- assume "collect_step prog inctx = outctx"
255
- show "inctx \<le> outctx" sorry
227
+ lemma step_all_correct : "flatten (step_all prog (deepen flat)) = step_all_flat prog flat"
228
+ proof standard
229
+ show "flatten (step_all prog (deepen flat)) \<subseteq> step_all_flat prog flat"
230
+ proof standard
231
+ fix x assume ass : "x \<in> flatten (step_all prog (deepen flat))"
232
+ then obtain pc st where splitx : "x = (pc, st)" using prod.exhaust_sel by blast
233
+ from this ass have "st \<in> lookup (step_all prog (deepen flat)) pc" using flatten_bwd by fastforce
234
+ hence "st \<in> stepped_to prog (deepen flat) pc" by simp
235
+ have "x \<in> step_all_flat_induct prog flat" sorry
236
+ thus "x \<in> step_all_flat prog flat" using step_all_flat_eq by blast
237
+ qed
238
+ show "step_all_flat prog flat \<subseteq> flatten (step_all prog (deepen flat))" sorry
256
239
qed
257
240
241
+ definition error_all :: "instr \<Rightarrow> addr \<Rightarrow> collect_state set \<Rightarrow> interpret_error set" where
242
+ "error_all op pc instates =
243
+ (if (\<exists>ins\<in>instates. step op (pc, ins) = None) then {StepFailed pc} else {})"
244
+
245
+ fun collect_step :: "program \<Rightarrow> collect_ctx \<Rightarrow> collect_ctx" where
246
+ "collect_step prog ctx = ctx \<squnion> step_all prog ctx"
258
247
259
248
fun collect_loop :: "program \<Rightarrow> fuel \<Rightarrow> collect_ctx \<Rightarrow> collect_ctx" where
260
249
"collect_loop prog 0 st = st" |
0 commit comments