13857
|
1 |
theory Separation = HoareAbort:
|
|
2 |
|
|
3 |
types heap = "(nat \<Rightarrow> nat option)"
|
|
4 |
|
|
5 |
|
|
6 |
text{* The semantic definition of a few connectives: *}
|
|
7 |
|
|
8 |
constdefs
|
13875
|
9 |
ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
|
|
10 |
"h1 \<bottom> h2 == dom h1 \<inter> dom h2 = {}"
|
13857
|
11 |
|
13875
|
12 |
is_empty :: "heap \<Rightarrow> bool"
|
|
13 |
"is_empty h == h = empty"
|
13857
|
14 |
|
|
15 |
singl:: "heap \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
|
|
16 |
"singl h x y == dom h = {x} & h x = Some y"
|
|
17 |
|
13875
|
18 |
star:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
19 |
"star P Q == \<lambda>h. \<exists>h1 h2. h = h1++h2 \<and> h1 \<bottom> h2 \<and> P h1 \<and> Q h2"
|
|
20 |
|
|
21 |
wand:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
22 |
"wand P Q == \<lambda>h. \<forall>h'. h' \<bottom> h \<and> P h' \<longrightarrow> Q(h++h')"
|
|
23 |
|
13857
|
24 |
lemma "VARS x y z w h
|
|
25 |
{star (%h. singl h x y) (%h. singl h z w) h}
|
|
26 |
SKIP
|
|
27 |
{x \<noteq> z}"
|
|
28 |
apply vcg
|
13875
|
29 |
apply(auto simp:star_def ortho_def singl_def)
|
13857
|
30 |
done
|
|
31 |
|
|
32 |
text{* To suppress the heap parameter of the connectives, we assume it
|
|
33 |
is always called H and add/remove it upon parsing/printing. Thus
|
|
34 |
every pointer program needs to have a program variable H, and
|
|
35 |
assertions should not contain any locally bound Hs - otherwise they
|
|
36 |
may bind the implicit H. *}
|
|
37 |
|
|
38 |
text{* Nice input syntax: *}
|
|
39 |
|
|
40 |
syntax
|
13875
|
41 |
"@emp" :: "bool" ("emp")
|
13857
|
42 |
"@singl" :: "nat \<Rightarrow> nat \<Rightarrow> bool" ("[_ \<mapsto> _]")
|
|
43 |
"@star" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "**" 60)
|
13875
|
44 |
"@wand" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "-o" 60)
|
13857
|
45 |
|
|
46 |
ML{*
|
13875
|
47 |
(* free_tr takes care of free vars in the scope of sep. logic connectives:
|
|
48 |
they are implicitly applied to the heap *)
|
|
49 |
fun free_tr(t as Free _) = t $ Syntax.free "H"
|
|
50 |
| free_tr t = t
|
|
51 |
|
|
52 |
fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
|
|
53 |
| emp_tr ts = raise TERM ("emp_tr", ts);
|
13857
|
54 |
fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
|
|
55 |
| singl_tr ts = raise TERM ("singl_tr", ts);
|
|
56 |
fun star_tr [P,Q] = Syntax.const "star" $
|
13875
|
57 |
absfree("H",dummyT,free_tr P) $ absfree("H",dummyT,free_tr Q) $
|
|
58 |
Syntax.free "H"
|
|
59 |
| star_tr ts = raise TERM ("star_tr", ts);
|
|
60 |
fun wand_tr [P,Q] = Syntax.const "wand" $
|
13857
|
61 |
absfree("H",dummyT,P) $ absfree("H",dummyT,Q) $ Syntax.free "H"
|
13875
|
62 |
| wand_tr ts = raise TERM ("wand_tr", ts);
|
13857
|
63 |
*}
|
|
64 |
|
13875
|
65 |
parse_translation
|
|
66 |
{* [("@emp", emp_tr), ("@singl", singl_tr),
|
|
67 |
("@star", star_tr), ("@wand", wand_tr)] *}
|
13857
|
68 |
|
|
69 |
lemma "VARS H x y z w
|
|
70 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
|
71 |
SKIP
|
|
72 |
{x \<noteq> z}"
|
|
73 |
apply vcg
|
13875
|
74 |
apply(auto simp:star_def ortho_def singl_def)
|
|
75 |
done
|
|
76 |
|
|
77 |
lemma "VARS H x y z w
|
|
78 |
{emp ** emp}
|
|
79 |
SKIP
|
|
80 |
{emp}"
|
|
81 |
apply vcg
|
|
82 |
apply(auto simp:star_def ortho_def is_empty_def)
|
13857
|
83 |
done
|
|
84 |
|
|
85 |
text{* Nice output syntax: *}
|
|
86 |
|
|
87 |
ML{*
|
13875
|
88 |
local
|
|
89 |
fun strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
|
|
90 |
| strip (Abs(_,_,(t as Var _) $ Bound 0)) = t
|
|
91 |
| strip (Abs(_,_,P)) = P
|
|
92 |
| strip (Const("is_empty",_)) = Syntax.const "@emp"
|
|
93 |
| strip t = t;
|
|
94 |
in
|
|
95 |
fun is_empty_tr' [_] = Syntax.const "@emp"
|
13857
|
96 |
fun singl_tr' [_,p,q] = Syntax.const "@singl" $ p $ q
|
13875
|
97 |
fun star_tr' [P,Q,_] = Syntax.const "@star" $ strip P $ strip Q
|
|
98 |
fun wand_tr' [P,Q,_] = Syntax.const "@wand" $ strip P $ strip Q
|
|
99 |
end
|
13857
|
100 |
*}
|
|
101 |
|
13875
|
102 |
print_translation
|
|
103 |
{* [("is_empty", is_empty_tr'),("singl", singl_tr'),("star", star_tr')] *}
|
13857
|
104 |
|
|
105 |
lemma "VARS H x y z w
|
|
106 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
13867
|
107 |
y := w
|
13857
|
108 |
{x \<noteq> z}"
|
|
109 |
apply vcg
|
13875
|
110 |
apply(auto simp:star_def ortho_def singl_def)
|
|
111 |
done
|
|
112 |
|
|
113 |
lemma "VARS H x y z w
|
|
114 |
{emp ** emp}
|
|
115 |
SKIP
|
|
116 |
{emp}"
|
|
117 |
apply vcg
|
|
118 |
apply(auto simp:star_def ortho_def is_empty_def)
|
|
119 |
done
|
|
120 |
|
|
121 |
(* move to Map.thy *)
|
|
122 |
lemma override_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1"
|
|
123 |
apply(rule ext)
|
|
124 |
apply(fastsimp simp:override_def split:option.split)
|
13857
|
125 |
done
|
|
126 |
|
13875
|
127 |
(* a law of separation logic *)
|
|
128 |
(* something is wrong with the pretty printer, but I cannot figure out what. *)
|
13857
|
129 |
|
13875
|
130 |
lemma star_comm: "P ** Q = Q ** P"
|
|
131 |
apply(simp add:star_def ortho_def)
|
|
132 |
apply(blast intro:override_comm)
|
|
133 |
done
|
|
134 |
|
|
135 |
lemma "VARS H x y z w
|
|
136 |
{P ** Q}
|
|
137 |
SKIP
|
|
138 |
{Q ** P}"
|
|
139 |
apply vcg
|
|
140 |
apply(simp add: star_comm)
|
|
141 |
done
|
|
142 |
|
|
143 |
end
|
|
144 |
(*
|
13857
|
145 |
consts llist :: "(heap * nat)set"
|
|
146 |
inductive llist
|
|
147 |
intros
|
|
148 |
empty: "(%n. None, 0) : llist"
|
|
149 |
cons: "\<lbrakk> R h h1 h2; pto h1 p q; (h2,q):llist \<rbrakk> \<Longrightarrow> (h,p):llist"
|
|
150 |
|
|
151 |
lemma "VARS p q h
|
|
152 |
{(h,p) : llist}
|
|
153 |
h := h(q \<mapsto> p)
|
|
154 |
{(h,q) : llist}"
|
|
155 |
apply vcg
|
|
156 |
apply(rule_tac "h1.0" = "%n. if n=q then Some p else None" in llist.cons)
|
|
157 |
prefer 3 apply assumption
|
|
158 |
prefer 2 apply(simp add:singl_def dom_def)
|
|
159 |
apply(simp add:R_def dom_def)
|
13875
|
160 |
*) |