| 
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.
  | 
| 
14074
 | 
     7  | 
Already builds on the theory for list abstractions.
  | 
| 
 | 
     8  | 
  | 
| 
 | 
     9  | 
If we suppress the H parameter for "List", we have to hardwired this
  | 
| 
 | 
    10  | 
into parser and pretty printer, which is not very modular.
  | 
| 
 | 
    11  | 
Alternative: some syntax like <P> which stands for P H. No more
  | 
| 
 | 
    12  | 
compact, but avoids the funny H.
  | 
| 
 | 
    13  | 
  | 
| 
13903
 | 
    14  | 
*)
  | 
| 
 | 
    15  | 
  | 
| 
16417
 | 
    16  | 
theory Separation imports HoareAbort SepLogHeap begin
  | 
| 
13857
 | 
    17  | 
  | 
| 
 | 
    18  | 
text{* The semantic definition of a few connectives: *}
 | 
| 
 | 
    19  | 
  | 
| 
 | 
    20  | 
constdefs
  | 
| 
13875
 | 
    21  | 
 ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
  | 
| 
 | 
    22  | 
"h1 \<bottom> h2 == dom h1 \<inter> dom h2 = {}"
 | 
| 
13857
 | 
    23  | 
  | 
| 
13875
 | 
    24  | 
 is_empty :: "heap \<Rightarrow> bool"
  | 
| 
 | 
    25  | 
"is_empty h == h = empty"
  | 
| 
13857
 | 
    26  | 
  | 
| 
 | 
    27  | 
 singl:: "heap \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
  | 
| 
 | 
    28  | 
"singl h x y == dom h = {x} & h x = Some y"
 | 
| 
 | 
    29  | 
  | 
| 
13875
 | 
    30  | 
 star:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
  | 
| 
 | 
    31  | 
"star P Q == \<lambda>h. \<exists>h1 h2. h = h1++h2 \<and> h1 \<bottom> h2 \<and> P h1 \<and> Q h2"
  | 
| 
 | 
    32  | 
  | 
| 
 | 
    33  | 
 wand:: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> bool)"
  | 
| 
 | 
    34  | 
"wand P Q == \<lambda>h. \<forall>h'. h' \<bottom> h \<and> P h' \<longrightarrow> Q(h++h')"
  | 
| 
 | 
    35  | 
  | 
| 
13903
 | 
    36  | 
text{*This is what assertions look like without any syntactic sugar: *}
 | 
| 
 | 
    37  | 
  | 
| 
13857
 | 
    38  | 
lemma "VARS x y z w h
  | 
| 
 | 
    39  | 
 {star (%h. singl h x y) (%h. singl h z w) h}
 | 
| 
 | 
    40  | 
 SKIP
  | 
| 
 | 
    41  | 
 {x \<noteq> z}"
 | 
| 
 | 
    42  | 
apply vcg
  | 
| 
13875
 | 
    43  | 
apply(auto simp:star_def ortho_def singl_def)
  | 
| 
13857
 | 
    44  | 
done
  | 
| 
 | 
    45  | 
  | 
| 
13903
 | 
    46  | 
text{* Now we add nice input syntax.  To suppress the heap parameter
 | 
| 
 | 
    47  | 
of the connectives, we assume it is always called H and add/remove it
  | 
| 
 | 
    48  | 
upon parsing/printing. Thus every pointer program needs to have a
  | 
| 
 | 
    49  | 
program variable H, and assertions should not contain any locally
  | 
| 
 | 
    50  | 
bound Hs - otherwise they may bind the implicit H. *}
  | 
| 
13857
 | 
    51  | 
  | 
| 
 | 
    52  | 
syntax
  | 
| 
13875
 | 
    53  | 
 "@emp" :: "bool" ("emp")
 | 
| 
13857
 | 
    54  | 
 "@singl" :: "nat \<Rightarrow> nat \<Rightarrow> bool" ("[_ \<mapsto> _]")
 | 
| 
 | 
    55  | 
 "@star" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "**" 60)
  | 
| 
13903
 | 
    56  | 
 "@wand" :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "-*" 60)
  | 
| 
13857
 | 
    57  | 
  | 
| 
17781
 | 
    58  | 
(* FIXME does not handle "_idtdummy" *)
  | 
| 
13857
 | 
    59  | 
ML{*
 | 
| 
13875
 | 
    60  | 
(* free_tr takes care of free vars in the scope of sep. logic connectives:
  | 
| 
 | 
    61  | 
   they are implicitly applied to the heap *)
  | 
| 
 | 
    62  | 
fun free_tr(t as Free _) = t $ Syntax.free "H"
  | 
| 
14074
 | 
    63  | 
(*
  | 
| 
 | 
    64  | 
  | free_tr((list as Free("List",_))$ p $ ps) = list $ Syntax.free "H" $ p $ ps
 | 
| 
 | 
    65  | 
*)
  | 
| 
13875
 | 
    66  | 
  | free_tr t = t
  | 
| 
 | 
    67  | 
  | 
| 
 | 
    68  | 
fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
  | 
| 
 | 
    69  | 
  | emp_tr ts = raise TERM ("emp_tr", ts);
 | 
| 
13857
 | 
    70  | 
fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
  | 
| 
 | 
    71  | 
  | singl_tr ts = raise TERM ("singl_tr", ts);
 | 
| 
 | 
    72  | 
fun star_tr [P,Q] = Syntax.const "star" $
  | 
| 
13875
 | 
    73  | 
      absfree("H",dummyT,free_tr P) $ absfree("H",dummyT,free_tr Q) $
 | 
| 
 | 
    74  | 
      Syntax.free "H"
  | 
| 
 | 
    75  | 
  | star_tr ts = raise TERM ("star_tr", ts);
 | 
| 
 | 
    76  | 
fun wand_tr [P,Q] = Syntax.const "wand" $
  | 
| 
13857
 | 
    77  | 
      absfree("H",dummyT,P) $ absfree("H",dummyT,Q) $ Syntax.free "H"
 | 
| 
13875
 | 
    78  | 
  | wand_tr ts = raise TERM ("wand_tr", ts);
 | 
| 
13857
 | 
    79  | 
*}
  | 
| 
 | 
    80  | 
  | 
| 
13875
 | 
    81  | 
parse_translation
  | 
| 
 | 
    82  | 
 {* [("@emp", emp_tr), ("@singl", singl_tr),
 | 
| 
 | 
    83  | 
     ("@star", star_tr), ("@wand", wand_tr)] *}
 | 
| 
13857
 | 
    84  | 
  | 
| 
13903
 | 
    85  | 
text{* Now it looks much better: *}
 | 
| 
 | 
    86  | 
  | 
| 
13857
 | 
    87  | 
lemma "VARS H x y z w
  | 
| 
 | 
    88  | 
 {[x\<mapsto>y] ** [z\<mapsto>w]}
 | 
| 
 | 
    89  | 
 SKIP
  | 
| 
 | 
    90  | 
 {x \<noteq> z}"
 | 
| 
 | 
    91  | 
apply vcg
  | 
| 
13875
 | 
    92  | 
apply(auto simp:star_def ortho_def singl_def)
  | 
| 
 | 
    93  | 
done
  | 
| 
 | 
    94  | 
  | 
| 
 | 
    95  | 
lemma "VARS H x y z w
  | 
| 
 | 
    96  | 
 {emp ** emp}
 | 
| 
 | 
    97  | 
 SKIP
  | 
| 
 | 
    98  | 
 {emp}"
 | 
| 
 | 
    99  | 
apply vcg
  | 
| 
 | 
   100  | 
apply(auto simp:star_def ortho_def is_empty_def)
  | 
| 
13857
 | 
   101  | 
done
  | 
| 
 | 
   102  | 
  | 
| 
13903
 | 
   103  | 
text{* But the output is still unreadable. Thus we also strip the heap
 | 
| 
 | 
   104  | 
parameters upon output: *}
  | 
| 
 | 
   105  | 
  | 
| 
 | 
   106  | 
(* debugging code:
  | 
| 
 | 
   107  | 
fun sot(Free(s,_)) = s
  | 
| 
 | 
   108  | 
  | sot(Var((s,i),_)) = "?" ^ s ^ string_of_int i
  | 
| 
 | 
   109  | 
  | sot(Const(s,_)) = s
  | 
| 
 | 
   110  | 
  | sot(Bound i) = "B." ^ string_of_int i
  | 
| 
 | 
   111  | 
  | sot(s $ t) = "(" ^ sot s ^ " " ^ sot t ^ ")"
 | 
| 
 | 
   112  | 
  | sot(Abs(_,_,t)) = "(% " ^ sot t ^ ")";
  | 
| 
 | 
   113  | 
*)
  | 
| 
13857
 | 
   114  | 
  | 
| 
 | 
   115  | 
ML{*
 | 
| 
13875
 | 
   116  | 
local
  | 
| 
13903
 | 
   117  | 
fun strip (Abs(_,_,(t as Const("_free",_) $ Free _) $ Bound 0)) = t
 | 
| 
 | 
   118  | 
  | strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
  | 
| 
14074
 | 
   119  | 
(*
  | 
| 
 | 
   120  | 
  | strip (Abs(_,_,((list as Const("List",_))$ Bound 0 $ p $ ps))) = list$p$ps
 | 
| 
 | 
   121  | 
*)
  | 
| 
13903
 | 
   122  | 
  | strip (Abs(_,_,(t as Const("_var",_) $ Var _) $ Bound 0)) = t
 | 
| 
13875
 | 
   123  | 
  | strip (Abs(_,_,P)) = P
  | 
| 
 | 
   124  | 
  | strip (Const("is_empty",_)) = Syntax.const "@emp"
 | 
| 
 | 
   125  | 
  | strip t = t;
  | 
| 
 | 
   126  | 
in
  | 
| 
 | 
   127  | 
fun is_empty_tr' [_] = Syntax.const "@emp"
  | 
| 
13857
 | 
   128  | 
fun singl_tr' [_,p,q] = Syntax.const "@singl" $ p $ q
  | 
| 
13875
 | 
   129  | 
fun star_tr' [P,Q,_] = Syntax.const "@star" $ strip P $ strip Q
  | 
| 
 | 
   130  | 
fun wand_tr' [P,Q,_] = Syntax.const "@wand" $ strip P $ strip Q
  | 
| 
 | 
   131  | 
end
  | 
| 
13857
 | 
   132  | 
*}
  | 
| 
 | 
   133  | 
  | 
| 
13875
 | 
   134  | 
print_translation
  | 
| 
13903
 | 
   135  | 
 {* [("is_empty", is_empty_tr'),("singl", singl_tr'),
 | 
| 
 | 
   136  | 
     ("star", star_tr'),("wand", wand_tr')] *}
 | 
| 
 | 
   137  | 
  | 
| 
 | 
   138  | 
text{* Now the intermediate proof states are also readable: *}
 | 
| 
13857
 | 
   139  | 
  | 
| 
 | 
   140  | 
lemma "VARS H x y z w
  | 
| 
 | 
   141  | 
 {[x\<mapsto>y] ** [z\<mapsto>w]}
 | 
| 
13867
 | 
   142  | 
 y := w
  | 
| 
13857
 | 
   143  | 
 {x \<noteq> z}"
 | 
| 
 | 
   144  | 
apply vcg
  | 
| 
13875
 | 
   145  | 
apply(auto simp:star_def ortho_def singl_def)
  | 
| 
 | 
   146  | 
done
  | 
| 
 | 
   147  | 
  | 
| 
 | 
   148  | 
lemma "VARS H x y z w
  | 
| 
 | 
   149  | 
 {emp ** emp}
 | 
| 
 | 
   150  | 
 SKIP
  | 
| 
 | 
   151  | 
 {emp}"
 | 
| 
 | 
   152  | 
apply vcg
  | 
| 
 | 
   153  | 
apply(auto simp:star_def ortho_def is_empty_def)
  | 
| 
 | 
   154  | 
done
  | 
| 
 | 
   155  | 
  | 
| 
13903
 | 
   156  | 
text{* So far we have unfolded the separation logic connectives in
 | 
| 
 | 
   157  | 
proofs. Here comes a simple example of a program proof that uses a law
  | 
| 
 | 
   158  | 
of separation logic instead. *}
  | 
| 
 | 
   159  | 
  | 
| 
13875
 | 
   160  | 
(* a law of separation logic *)
  | 
| 
 | 
   161  | 
lemma star_comm: "P ** Q = Q ** P"
  | 
| 
18447
 | 
   162  | 
  by(auto simp add:star_def ortho_def dest: map_add_comm)
  | 
| 
13875
 | 
   163  | 
  | 
| 
 | 
   164  | 
lemma "VARS H x y z w
  | 
| 
 | 
   165  | 
 {P ** Q}
 | 
| 
 | 
   166  | 
 SKIP
  | 
| 
 | 
   167  | 
 {Q ** P}"
 | 
| 
 | 
   168  | 
apply vcg
  | 
| 
 | 
   169  | 
apply(simp add: star_comm)
  | 
| 
 | 
   170  | 
done
  | 
| 
 | 
   171  | 
  | 
| 
14074
 | 
   172  | 
  | 
| 
 | 
   173  | 
lemma "VARS H
  | 
| 
 | 
   174  | 
 {p\<noteq>0 \<and> [p \<mapsto> x] ** List H q qs}
 | 
| 
 | 
   175  | 
 H := H(p \<mapsto> q)
  | 
| 
 | 
   176  | 
 {List H p (p#qs)}"
 | 
| 
 | 
   177  | 
apply vcg
  | 
| 
 | 
   178  | 
apply(simp add: star_def ortho_def singl_def)
  | 
| 
 | 
   179  | 
apply clarify
  | 
| 
 | 
   180  | 
apply(subgoal_tac "p \<notin> set qs")
  | 
| 
 | 
   181  | 
 prefer 2
  | 
| 
 | 
   182  | 
 apply(blast dest:list_in_heap)
  | 
| 
 | 
   183  | 
apply simp
  | 
| 
 | 
   184  | 
done
  | 
| 
 | 
   185  | 
  | 
| 
 | 
   186  | 
lemma "VARS H p q r
  | 
| 
 | 
   187  | 
  {List H p Ps ** List H q Qs}
 | 
| 
 | 
   188  | 
  WHILE p \<noteq> 0
  | 
| 
 | 
   189  | 
  INV {\<exists>ps qs. (List H p ps ** List H q qs) \<and> rev ps @ qs = rev Ps @ Qs}
 | 
| 
 | 
   190  | 
  DO r := p; p := the(H p); H := H(r \<mapsto> q); q := r OD
  | 
| 
 | 
   191  | 
  {List H q (rev Ps @ Qs)}"
 | 
| 
 | 
   192  | 
apply vcg
  | 
| 
 | 
   193  | 
apply(simp_all add: star_def ortho_def singl_def)
  | 
| 
 | 
   194  | 
  | 
| 
 | 
   195  | 
apply fastsimp
  | 
| 
 | 
   196  | 
  | 
| 
 | 
   197  | 
apply (clarsimp simp add:List_non_null)
  | 
| 
 | 
   198  | 
apply(rename_tac ps')
  | 
| 
 | 
   199  | 
apply(rule_tac x = ps' in exI)
  | 
| 
 | 
   200  | 
apply(rule_tac x = "p#qs" in exI)
  | 
| 
 | 
   201  | 
apply simp
  | 
| 
 | 
   202  | 
apply(rule_tac x = "h1(p:=None)" in exI)
  | 
| 
 | 
   203  | 
apply(rule_tac x = "h2(p\<mapsto>q)" in exI)
  | 
| 
 | 
   204  | 
apply simp
  | 
| 
 | 
   205  | 
apply(rule conjI)
  | 
| 
 | 
   206  | 
 apply(rule ext)
  | 
| 
 | 
   207  | 
 apply(simp add:map_add_def split:option.split)
  | 
| 
 | 
   208  | 
apply(rule conjI)
  | 
| 
 | 
   209  | 
 apply blast
  | 
| 
 | 
   210  | 
apply(simp add:map_add_def split:option.split)
  | 
| 
 | 
   211  | 
apply(rule conjI)
  | 
| 
 | 
   212  | 
apply(subgoal_tac "p \<notin> set qs")
  | 
| 
 | 
   213  | 
 prefer 2
  | 
| 
 | 
   214  | 
 apply(blast dest:list_in_heap)
  | 
| 
 | 
   215  | 
apply(simp)
  | 
| 
 | 
   216  | 
apply fast
  | 
| 
 | 
   217  | 
  | 
| 
 | 
   218  | 
apply(fastsimp)
  | 
| 
 | 
   219  | 
done
  | 
| 
 | 
   220  | 
  | 
| 
13875
 | 
   221  | 
end
  |