src/HOL/Hoare/Separation.thy
changeset 14074 93dfce3b6f86
parent 14028 ff6eb32b30a1
child 16417 9bc16273c2d4
equal deleted inserted replaced
14073:21e2ff495d81 14074:93dfce3b6f86
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     3     Author:     Tobias Nipkow
     4     Copyright   2003 TUM
     4     Copyright   2003 TUM
     5 
     5 
     6 A first attempt at a nice syntactic embedding of separation logic.
     6 A first attempt at a nice syntactic embedding of separation logic.
     7 *)
     7 Already builds on the theory for list abstractions.
     8 
     8 
     9 theory Separation = HoareAbort:
     9 If we suppress the H parameter for "List", we have to hardwired this
    10 
    10 into parser and pretty printer, which is not very modular.
    11 types heap = "(nat \<Rightarrow> nat option)"
    11 Alternative: some syntax like <P> which stands for P H. No more
       
    12 compact, but avoids the funny H.
       
    13 
       
    14 *)
       
    15 
       
    16 theory Separation = HoareAbort + SepLogHeap:
    12 
    17 
    13 text{* The semantic definition of a few connectives: *}
    18 text{* The semantic definition of a few connectives: *}
    14 
    19 
    15 constdefs
    20 constdefs
    16  ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
    21  ortho:: "heap \<Rightarrow> heap \<Rightarrow> bool" (infix "\<bottom>" 55)
    52 
    57 
    53 ML{*
    58 ML{*
    54 (* free_tr takes care of free vars in the scope of sep. logic connectives:
    59 (* free_tr takes care of free vars in the scope of sep. logic connectives:
    55    they are implicitly applied to the heap *)
    60    they are implicitly applied to the heap *)
    56 fun free_tr(t as Free _) = t $ Syntax.free "H"
    61 fun free_tr(t as Free _) = t $ Syntax.free "H"
       
    62 (*
       
    63   | free_tr((list as Free("List",_))$ p $ ps) = list $ Syntax.free "H" $ p $ ps
       
    64 *)
    57   | free_tr t = t
    65   | free_tr t = t
    58 
    66 
    59 fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
    67 fun emp_tr [] = Syntax.const "is_empty" $ Syntax.free "H"
    60   | emp_tr ts = raise TERM ("emp_tr", ts);
    68   | emp_tr ts = raise TERM ("emp_tr", ts);
    61 fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
    69 fun singl_tr [p,q] = Syntax.const "singl" $ Syntax.free "H" $ p $ q
   105 
   113 
   106 ML{*
   114 ML{*
   107 local
   115 local
   108 fun strip (Abs(_,_,(t as Const("_free",_) $ Free _) $ Bound 0)) = t
   116 fun strip (Abs(_,_,(t as Const("_free",_) $ Free _) $ Bound 0)) = t
   109   | strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
   117   | strip (Abs(_,_,(t as Free _) $ Bound 0)) = t
       
   118 (*
       
   119   | strip (Abs(_,_,((list as Const("List",_))$ Bound 0 $ p $ ps))) = list$p$ps
       
   120 *)
   110   | strip (Abs(_,_,(t as Const("_var",_) $ Var _) $ Bound 0)) = t
   121   | strip (Abs(_,_,(t as Const("_var",_) $ Var _) $ Bound 0)) = t
   111   | strip (Abs(_,_,P)) = P
   122   | strip (Abs(_,_,P)) = P
   112   | strip (Const("is_empty",_)) = Syntax.const "@emp"
   123   | strip (Const("is_empty",_)) = Syntax.const "@emp"
   113   | strip t = t;
   124   | strip t = t;
   114 in
   125 in
   157  {Q ** P}"
   168  {Q ** P}"
   158 apply vcg
   169 apply vcg
   159 apply(simp add: star_comm)
   170 apply(simp add: star_comm)
   160 done
   171 done
   161 
   172 
       
   173 
       
   174 lemma "VARS H
       
   175  {p\<noteq>0 \<and> [p \<mapsto> x] ** List H q qs}
       
   176  H := H(p \<mapsto> q)
       
   177  {List H p (p#qs)}"
       
   178 apply vcg
       
   179 apply(simp add: star_def ortho_def singl_def)
       
   180 apply clarify
       
   181 apply(subgoal_tac "p \<notin> set qs")
       
   182  prefer 2
       
   183  apply(blast dest:list_in_heap)
       
   184 apply simp
       
   185 done
       
   186 
       
   187 lemma "VARS H p q r
       
   188   {List H p Ps ** List H q Qs}
       
   189   WHILE p \<noteq> 0
       
   190   INV {\<exists>ps qs. (List H p ps ** List H q qs) \<and> rev ps @ qs = rev Ps @ Qs}
       
   191   DO r := p; p := the(H p); H := H(r \<mapsto> q); q := r OD
       
   192   {List H q (rev Ps @ Qs)}"
       
   193 apply vcg
       
   194 apply(simp_all add: star_def ortho_def singl_def)
       
   195 
       
   196 apply fastsimp
       
   197 
       
   198 apply (clarsimp simp add:List_non_null)
       
   199 apply(rename_tac ps')
       
   200 apply(rule_tac x = ps' in exI)
       
   201 apply(rule_tac x = "p#qs" in exI)
       
   202 apply simp
       
   203 apply(rule_tac x = "h1(p:=None)" in exI)
       
   204 apply(rule_tac x = "h2(p\<mapsto>q)" in exI)
       
   205 apply simp
       
   206 apply(rule conjI)
       
   207  apply(rule ext)
       
   208  apply(simp add:map_add_def split:option.split)
       
   209 apply(rule conjI)
       
   210  apply blast
       
   211 apply(simp add:map_add_def split:option.split)
       
   212 apply(rule conjI)
       
   213 apply(subgoal_tac "p \<notin> set qs")
       
   214  prefer 2
       
   215  apply(blast dest:list_in_heap)
       
   216 apply(simp)
       
   217 apply fast
       
   218 
       
   219 apply(fastsimp)
       
   220 done
       
   221 
   162 end
   222 end