13903
|
1 |
(* Title: HOL/Hoare/Separation.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Tobias Nipkow
|
|
4 |
Copyright 2003 TUM
|
|
5 |
|
|
6 |
A first attempt at a nice syntactic embedding of separation logic.
|
|
7 |
*)
|
|
8 |
|
13857
|
9 |
theory Separation = HoareAbort:
|
|
10 |
|
|
11 |
types heap = "(nat \<Rightarrow> nat option)"
|
|
12 |
|
|
13 |
text{* The semantic definition of a few connectives: *}
|
|
14 |
|
|
15 |
constdefs
|
13875
|
16 |
ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
|
|
17 |
"h1 \<bottom> h2 == dom h1 \<inter> dom h2 = {}"
|
13857
|
18 |
|
13875
|
19 |
is_empty :: "heap \<Rightarrow> bool"
|
|
20 |
"is_empty h == h = empty"
|
13857
|
21 |
|
|
22 |
singl:: "heap \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
|
|
23 |
"singl h x y == dom h = {x} & h x = Some y"
|
|
24 |
|
13875
|
25 |
star:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
26 |
"star P Q == \<lambda>h. \<exists>h1 h2. h = h1++h2 \<and> h1 \<bottom> h2 \<and> P h1 \<and> Q h2"
|
|
27 |
|
|
28 |
wand:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
|
|
29 |
"wand P Q == \<lambda>h. \<forall>h'. h' \<bottom> h \<and> P h' \<longrightarrow> Q(h++h')"
|
|
30 |
|
13903
|
31 |
text{*This is what assertions look like without any syntactic sugar: *}
|
|
32 |
|
13857
|
33 |
lemma "VARS x y z w h
|
|
34 |
{star (%h. singl h x y) (%h. singl h z w) h}
|
|
35 |
SKIP
|
|
36 |
{x \<noteq> z}"
|
|
37 |
apply vcg
|
13875
|
38 |
apply(auto simp:star_def ortho_def singl_def)
|
13857
|
39 |
done
|
|
40 |
|
13903
|
41 |
text{* Now we add nice input syntax. To suppress the heap parameter
|
|
42 |
of the connectives, we assume it is always called H and add/remove it
|
|
43 |
upon parsing/printing. Thus every pointer program needs to have a
|
|
44 |
program variable H, and assertions should not contain any locally
|
|
45 |
bound Hs - otherwise they may bind the implicit H. *}
|
13857
|
46 |
|
|
47 |
syntax
|
13875
|
48 |
"@emp" :: "bool" ("emp")
|
13857
|
49 |
"@singl" :: "nat \<Rightarrow> nat \<Rightarrow> bool" ("[_ \<mapsto> _]")
|
|
50 |
"@star" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "**" 60)
|
13903
|
51 |
"@wand" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "-*" 60)
|
13857
|
52 |
|
|
53 |
ML{*
|
13875
|
54 |
(* free_tr takes care of free vars in the scope of sep. logic connectives:
|
|
55 |
they are implicitly applied to the heap *)
|
|
56 |
fun free_tr(t as Free _) = t $ Syntax.free "H"
|
|
57 |
| free_tr t = t
|
|
58 |
|
|
59 |
fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
|
|
60 |
| emp_tr ts = raise TERM ("emp_tr", ts);
|
13857
|
61 |
fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
|
|
62 |
| singl_tr ts = raise TERM ("singl_tr", ts);
|
|
63 |
fun star_tr [P,Q] = Syntax.const "star" $
|
13875
|
64 |
absfree("H",dummyT,free_tr P) $ absfree("H",dummyT,free_tr Q) $
|
|
65 |
Syntax.free "H"
|
|
66 |
| star_tr ts = raise TERM ("star_tr", ts);
|
|
67 |
fun wand_tr [P,Q] = Syntax.const "wand" $
|
13857
|
68 |
absfree("H",dummyT,P) $ absfree("H",dummyT,Q) $ Syntax.free "H"
|
13875
|
69 |
| wand_tr ts = raise TERM ("wand_tr", ts);
|
13857
|
70 |
*}
|
|
71 |
|
13875
|
72 |
parse_translation
|
|
73 |
{* [("@emp", emp_tr), ("@singl", singl_tr),
|
|
74 |
("@star", star_tr), ("@wand", wand_tr)] *}
|
13857
|
75 |
|
13903
|
76 |
text{* Now it looks much better: *}
|
|
77 |
|
13857
|
78 |
lemma "VARS H x y z w
|
|
79 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
|
80 |
SKIP
|
|
81 |
{x \<noteq> z}"
|
|
82 |
apply vcg
|
13875
|
83 |
apply(auto simp:star_def ortho_def singl_def)
|
|
84 |
done
|
|
85 |
|
|
86 |
lemma "VARS H x y z w
|
|
87 |
{emp ** emp}
|
|
88 |
SKIP
|
|
89 |
{emp}"
|
|
90 |
apply vcg
|
|
91 |
apply(auto simp:star_def ortho_def is_empty_def)
|
13857
|
92 |
done
|
|
93 |
|
13903
|
94 |
text{* But the output is still unreadable. Thus we also strip the heap
|
|
95 |
parameters upon output: *}
|
|
96 |
|
|
97 |
(* debugging code:
|
|
98 |
fun sot(Free(s,_)) = s
|
|
99 |
| sot(Var((s,i),_)) = "?" ^ s ^ string_of_int i
|
|
100 |
| sot(Const(s,_)) = s
|
|
101 |
| sot(Bound i) = "B." ^ string_of_int i
|
|
102 |
| sot(s $ t) = "(" ^ sot s ^ " " ^ sot t ^ ")"
|
|
103 |
| sot(Abs(_,_,t)) = "(% " ^ sot t ^ ")";
|
|
104 |
*)
|
13857
|
105 |
|
|
106 |
ML{*
|
13875
|
107 |
local
|
13903
|
108 |
fun strip (Abs(_,_,(t as Const("_free",_) $ Free _) $ Bound 0)) = t
|
|
109 |
| strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
|
|
110 |
| strip (Abs(_,_,(t as Const("_var",_) $ Var _) $ Bound 0)) = t
|
13875
|
111 |
| strip (Abs(_,_,P)) = P
|
|
112 |
| strip (Const("is_empty",_)) = Syntax.const "@emp"
|
|
113 |
| strip t = t;
|
|
114 |
in
|
|
115 |
fun is_empty_tr' [_] = Syntax.const "@emp"
|
13857
|
116 |
fun singl_tr' [_,p,q] = Syntax.const "@singl" $ p $ q
|
13875
|
117 |
fun star_tr' [P,Q,_] = Syntax.const "@star" $ strip P $ strip Q
|
|
118 |
fun wand_tr' [P,Q,_] = Syntax.const "@wand" $ strip P $ strip Q
|
|
119 |
end
|
13857
|
120 |
*}
|
|
121 |
|
13875
|
122 |
print_translation
|
13903
|
123 |
{* [("is_empty", is_empty_tr'),("singl", singl_tr'),
|
|
124 |
("star", star_tr'),("wand", wand_tr')] *}
|
|
125 |
|
|
126 |
text{* Now the intermediate proof states are also readable: *}
|
13857
|
127 |
|
|
128 |
lemma "VARS H x y z w
|
|
129 |
{[x\<mapsto>y] ** [z\<mapsto>w]}
|
13867
|
130 |
y := w
|
13857
|
131 |
{x \<noteq> z}"
|
|
132 |
apply vcg
|
13875
|
133 |
apply(auto simp:star_def ortho_def singl_def)
|
|
134 |
done
|
|
135 |
|
|
136 |
lemma "VARS H x y z w
|
|
137 |
{emp ** emp}
|
|
138 |
SKIP
|
|
139 |
{emp}"
|
|
140 |
apply vcg
|
|
141 |
apply(auto simp:star_def ortho_def is_empty_def)
|
|
142 |
done
|
|
143 |
|
13903
|
144 |
text{* So far we have unfolded the separation logic connectives in
|
|
145 |
proofs. Here comes a simple example of a program proof that uses a law
|
|
146 |
of separation logic instead. *}
|
|
147 |
|
13875
|
148 |
(* move to Map.thy *)
|
|
149 |
lemma override_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1"
|
|
150 |
apply(rule ext)
|
|
151 |
apply(fastsimp simp:override_def split:option.split)
|
13857
|
152 |
done
|
|
153 |
|
13875
|
154 |
(* a law of separation logic *)
|
|
155 |
lemma star_comm: "P ** Q = Q ** P"
|
|
156 |
apply(simp add:star_def ortho_def)
|
|
157 |
apply(blast intro:override_comm)
|
|
158 |
done
|
|
159 |
|
|
160 |
lemma "VARS H x y z w
|
|
161 |
{P ** Q}
|
|
162 |
SKIP
|
|
163 |
{Q ** P}"
|
|
164 |
apply vcg
|
|
165 |
apply(simp add: star_comm)
|
|
166 |
done
|
|
167 |
|
|
168 |
end
|