-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diff_Arr_Safe.thy
78 lines (68 loc) · 2.25 KB
/
Diff_Arr_Safe.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
theory Diff_Arr_Safe
imports Diff_Arr
begin
context
begin
qualified definition lookup where
"lookup arr i = do {
len \<leftarrow> Diff_Arr.length arr;
if i < len
then Diff_Arr.lookup arr i
else return (undefined(i - len))
}"
qualified definition update where
"update arr i v = do {
len \<leftarrow> Diff_Arr.length arr;
if i < len
then Diff_Arr.update arr i v
else return arr
}"
qualified definition update_tailrec where
"update_tailrec arr i v = do {
len \<leftarrow> Diff_Arr.length arr;
if i < len
then Diff_Arr.update_tailrec arr i v
else return arr
}"
lemma nth_undefined: "i \<ge> length xs \<Longrightarrow> xs ! i = undefined(i - length xs)"
unfolding List.nth_def
by(induction xs arbitrary: i)(auto split: nat.split)
lemma lookup_safe [sep_heap_rules]: "
<master_assn t * \<up>(t \<turnstile> xs \<sim> a)>
lookup a i
<\<lambda>r. master_assn t * \<up>(r = xs!i)>"
unfolding lookup_def
apply sep_auto
using length
apply sep_auto
apply sep_auto
using lookup
by(sep_auto simp: nth_undefined[of xs i])+
lemma update_safe [sep_heap_rules]: "
<master_assn t * \<up>(t \<turnstile> xs \<sim> diff_arr)>
update diff_arr i v
<\<lambda>diff_arr. \<exists>\<^sub>At'. master_assn t' *
\<up>((\<forall>xs' diff_arr'. t \<turnstile> xs' \<sim> diff_arr' \<longrightarrow> t' \<turnstile> xs' \<sim> diff_arr')
\<and> (t' \<turnstile> xs[i := v] \<sim> diff_arr))>"
unfolding update_def
apply sep_auto
apply(rule fi_rule[OF length, where F = emp])
apply sep_auto+
apply(rule cons_post_rule)
apply(rule fi_rule[OF update, where F = emp])
by sep_auto+
lemma update_tailrec_safe [sep_heap_rules]: "
<master_assn t * \<up>(t \<turnstile> xs \<sim> diff_arr)>
update_tailrec diff_arr i v
<\<lambda>diff_arr. \<exists>\<^sub>At'. master_assn t' *
\<up>((\<forall>xs' diff_arr'. t \<turnstile> xs' \<sim> diff_arr' \<longrightarrow> t' \<turnstile> xs' \<sim> diff_arr')
\<and> (t' \<turnstile> xs[i := v] \<sim> diff_arr))>"
unfolding update_tailrec_def
apply sep_auto
apply(rule fi_rule[OF length, where F = emp])
apply sep_auto+
apply(rule cons_post_rule)
apply(rule fi_rule[OF update_tailrec, where F = emp])
by sep_auto+
end
end