src/HOL/Hoare/SchorrWaite.thy
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 13875 12997e3ddd8d
child 16417 9bc16273c2d4
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
mehta@13820
     1
(*  Title:      HOL/Hoare/SchorrWaite.thy
mehta@13820
     2
    ID:         $Id$
mehta@13820
     3
    Author:     Farhad Mehta
mehta@13820
     4
    Copyright   2003 TUM
mehta@13820
     5
mehta@13820
     6
Proof of the Schorr-Waite graph marking algorithm.
mehta@13820
     7
*)
mehta@13820
     8
mehta@13820
     9
nipkow@13875
    10
theory SchorrWaite = HeapSyntax:
mehta@13820
    11
mehta@13820
    12
section {* Machinery for the Schorr-Waite proof*}
mehta@13820
    13
mehta@13820
    14
constdefs
mehta@13820
    15
  -- "Relations induced by a mapping"
mehta@13820
    16
  rel :: "('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<times> 'a) set"
mehta@13820
    17
  "rel m == {(x,y). m x = Ref y}"
mehta@13820
    18
  relS :: "('a \<Rightarrow> 'a ref) set \<Rightarrow> ('a \<times> 'a) set"
mehta@13820
    19
  "relS M == (\<Union> m \<in> M. rel m)"
mehta@13820
    20
  addrs :: "'a ref set \<Rightarrow> 'a set"
mehta@13820
    21
  "addrs P == {a. Ref a \<in> P}"
mehta@13820
    22
  reachable :: "('a \<times> 'a) set \<Rightarrow> 'a ref set \<Rightarrow> 'a set"
mehta@13820
    23
  "reachable r P == (r\<^sup>* `` addrs P)"
mehta@13820
    24
mehta@13820
    25
lemmas rel_defs = relS_def rel_def
mehta@13820
    26
mehta@13820
    27
text {* Rewrite rules for relations induced by a mapping*}
mehta@13820
    28
mehta@13820
    29
lemma self_reachable: "b \<in> B \<Longrightarrow> b \<in> R\<^sup>* `` B"
mehta@13820
    30
apply blast
mehta@13820
    31
done
mehta@13820
    32
mehta@13820
    33
lemma oneStep_reachable: "b \<in> R``B \<Longrightarrow> b \<in> R\<^sup>* `` B"
mehta@13820
    34
apply blast
mehta@13820
    35
done
mehta@13820
    36
mehta@13820
    37
lemma still_reachable: "\<lbrakk>B\<subseteq>Ra\<^sup>*``A; \<forall> (x,y) \<in> Rb-Ra. y\<in> (Ra\<^sup>*``A)\<rbrakk> \<Longrightarrow> Rb\<^sup>* `` B \<subseteq> Ra\<^sup>* `` A "
mehta@13820
    38
apply (clarsimp simp only:Image_iff intro:subsetI)
mehta@13820
    39
apply (erule rtrancl_induct)
mehta@13820
    40
 apply blast
mehta@13820
    41
apply (subgoal_tac "(y, z) \<in> Ra\<union>(Rb-Ra)")
mehta@13820
    42
 apply (erule UnE)
mehta@13820
    43
 apply (auto intro:rtrancl_into_rtrancl)
mehta@13820
    44
apply blast
mehta@13820
    45
done
mehta@13820
    46
mehta@13820
    47
lemma still_reachable_eq: "\<lbrakk> A\<subseteq>Rb\<^sup>*``B; B\<subseteq>Ra\<^sup>*``A; \<forall> (x,y) \<in> Ra-Rb. y \<in>(Rb\<^sup>*``B); \<forall> (x,y) \<in> Rb-Ra. y\<in> (Ra\<^sup>*``A)\<rbrakk> \<Longrightarrow> Ra\<^sup>*``A =  Rb\<^sup>*``B "
mehta@13820
    48
apply (rule equalityI)
mehta@13820
    49
 apply (erule still_reachable ,assumption)+
mehta@13820
    50
done
mehta@13820
    51
mehta@13820
    52
lemma reachable_null: "reachable mS {Null} = {}"
mehta@13820
    53
apply (simp add: reachable_def addrs_def)
mehta@13820
    54
done
mehta@13820
    55
mehta@13820
    56
lemma reachable_empty: "reachable mS {} = {}"
mehta@13820
    57
apply (simp add: reachable_def addrs_def)
mehta@13820
    58
done
mehta@13820
    59
mehta@13820
    60
lemma reachable_union: "(reachable mS aS \<union> reachable mS bS) = reachable mS (aS \<union> bS)"
mehta@13820
    61
apply (simp add: reachable_def rel_defs addrs_def)
mehta@13820
    62
apply blast
mehta@13820
    63
done
mehta@13820
    64
mehta@13820
    65
lemma reachable_union_sym: "reachable r (insert a aS) = (r\<^sup>* `` addrs {a}) \<union> reachable r aS"
mehta@13820
    66
apply (simp add: reachable_def rel_defs addrs_def)
mehta@13820
    67
apply blast
mehta@13820
    68
done
mehta@13820
    69
mehta@13820
    70
lemma rel_upd1: "(a,b) \<notin> rel (r(q:=t)) \<Longrightarrow> (a,b) \<in> rel r \<Longrightarrow> a=q"
mehta@13820
    71
apply (rule classical)
mehta@13820
    72
apply (simp add:rel_defs fun_upd_apply)
mehta@13820
    73
done
mehta@13820
    74
mehta@13820
    75
lemma rel_upd2: "(a,b)  \<notin> rel r \<Longrightarrow> (a,b) \<in> rel (r(q:=t)) \<Longrightarrow> a=q"
mehta@13820
    76
apply (rule classical)
mehta@13820
    77
apply (simp add:rel_defs fun_upd_apply)
mehta@13820
    78
done
mehta@13820
    79
mehta@13820
    80
constdefs
mehta@13820
    81
  -- "Restriction of a relation"
mehta@13820
    82
  restr ::"('a \<times> 'a) set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> ('a \<times> 'a) set"       ("(_/ | _)" [50, 51] 50)
mehta@13820
    83
  "restr r m == {(x,y). (x,y) \<in> r \<and> \<not> m x}"
mehta@13820
    84
mehta@13820
    85
text {* Rewrite rules for the restriction of a relation *}
mehta@13820
    86
mehta@13820
    87
lemma restr_identity[simp]:
mehta@13820
    88
 " (\<forall>x. \<not> m x) \<Longrightarrow> (R |m) = R"
mehta@13820
    89
by (auto simp add:restr_def)
mehta@13820
    90
mehta@13820
    91
lemma restr_rtrancl[simp]: " \<lbrakk>m l\<rbrakk> \<Longrightarrow> (R | m)\<^sup>* `` {l} = {l}"
mehta@13820
    92
by (auto simp add:restr_def elim:converse_rtranclE)
mehta@13820
    93
mehta@13820
    94
lemma [simp]: " \<lbrakk>m l\<rbrakk> \<Longrightarrow> (l,x) \<in> (R | m)\<^sup>* = (l=x)"
mehta@13820
    95
by (auto simp add:restr_def elim:converse_rtranclE)
mehta@13820
    96
mehta@13820
    97
lemma restr_upd: "((rel (r (q := t)))|(m(q := True))) = ((rel (r))|(m(q := True))) "
mehta@13820
    98
apply (auto simp:restr_def rel_def fun_upd_apply)
mehta@13820
    99
apply (rename_tac a b)
mehta@13820
   100
apply (case_tac "a=q")
mehta@13820
   101
 apply auto
mehta@13820
   102
done
mehta@13820
   103
	      
mehta@13820
   104
lemma restr_un: "((r \<union> s)|m) = (r|m) \<union> (s|m)"
mehta@13820
   105
  by (auto simp add:restr_def)
mehta@13820
   106
mehta@13820
   107
lemma rel_upd3: "(a, b) \<notin> (r|(m(q := t))) \<Longrightarrow> (a,b) \<in> (r|m) \<Longrightarrow> a = q "
mehta@13820
   108
apply (rule classical)
mehta@13820
   109
apply (simp add:restr_def fun_upd_apply)
mehta@13820
   110
done	
mehta@13820
   111
mehta@13820
   112
constdefs
mehta@13820
   113
  -- "A short form for the stack mapping function for List"
mehta@13820
   114
  S :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<Rightarrow> 'a ref)"
mehta@13820
   115
  "S c l r == (\<lambda>x. if c x then r x else l x)"
mehta@13820
   116
mehta@13820
   117
text {* Rewrite rules for Lists using S as their mapping *}
mehta@13820
   118
mehta@13820
   119
lemma [rule_format,simp]:
mehta@13820
   120
 "\<forall>p. a \<notin> set stack \<longrightarrow> List (S c l r) p stack = List (S (c(a:=x)) (l(a:=y)) (r(a:=z))) p stack"
mehta@13820
   121
apply(induct_tac stack)
mehta@13820
   122
 apply(simp add:fun_upd_apply S_def)+
mehta@13820
   123
done
mehta@13820
   124
mehta@13820
   125
lemma [rule_format,simp]:
mehta@13820
   126
 "\<forall>p. a \<notin> set stack \<longrightarrow> List (S c l (r(a:=z))) p stack = List (S c l r) p stack"
mehta@13820
   127
apply(induct_tac stack)
mehta@13820
   128
 apply(simp add:fun_upd_apply S_def)+
mehta@13820
   129
done
mehta@13820
   130
mehta@13820
   131
lemma [rule_format,simp]:
mehta@13820
   132
 "\<forall>p. a \<notin> set stack \<longrightarrow> List (S c (l(a:=z)) r) p stack = List (S c l r) p stack"
mehta@13820
   133
apply(induct_tac stack)
mehta@13820
   134
 apply(simp add:fun_upd_apply S_def)+
mehta@13820
   135
done
mehta@13820
   136
mehta@13820
   137
lemma [rule_format,simp]:
mehta@13820
   138
 "\<forall>p. a \<notin> set stack \<longrightarrow> List (S (c(a:=z)) l r) p stack = List (S c l r) p stack"
mehta@13820
   139
apply(induct_tac stack)
mehta@13820
   140
 apply(simp add:fun_upd_apply S_def)+
mehta@13820
   141
done
mehta@13820
   142
mehta@13820
   143
consts
mehta@13820
   144
  --"Recursive definition of what is means for a the graph/stack structure to be reconstructible"
mehta@13820
   145
  stkOk :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> ('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow>'a list \<Rightarrow>  bool"
mehta@13820
   146
primrec
mehta@13820
   147
stkOk_nil:  "stkOk c l r iL iR t [] = True"
mehta@13820
   148
stkOk_cons: "stkOk c l r iL iR t (p#stk) = (stkOk c l r iL iR (Ref p) (stk) \<and> 
mehta@13820
   149
                                  iL p = (if c p then l p else t) \<and>
mehta@13820
   150
                                  iR p = (if c p then t else r p))"
mehta@13820
   151
mehta@13820
   152
text {* Rewrite rules for stkOk *}
mehta@13820
   153
mehta@13820
   154
lemma [simp]: "\<And>t. \<lbrakk> x \<notin> set xs; Ref x\<noteq>t \<rbrakk> \<Longrightarrow>
mehta@13820
   155
  stkOk (c(x := f)) l r iL iR t xs = stkOk c l r iL iR t xs"
mehta@13820
   156
apply (induct xs)
mehta@13820
   157
 apply (auto simp:eq_sym_conv)
mehta@13820
   158
done
mehta@13820
   159
mehta@13820
   160
lemma [simp]: "\<And>t. \<lbrakk> x \<notin> set xs; Ref x\<noteq>t \<rbrakk> \<Longrightarrow>
mehta@13820
   161
 stkOk c (l(x := g)) r iL iR t xs = stkOk c l r iL iR t xs"
mehta@13820
   162
apply (induct xs)
mehta@13820
   163
 apply (auto simp:eq_sym_conv)
mehta@13820
   164
done
mehta@13820
   165
mehta@13820
   166
lemma [simp]: "\<And>t. \<lbrakk> x \<notin> set xs; Ref x\<noteq>t \<rbrakk> \<Longrightarrow>
mehta@13820
   167
 stkOk c l (r(x := g)) iL iR t xs = stkOk c l r iL iR t xs"
mehta@13820
   168
apply (induct xs)
mehta@13820
   169
 apply (auto simp:eq_sym_conv)
mehta@13820
   170
done
mehta@13820
   171
mehta@13820
   172
lemma stkOk_r_rewrite [simp]: "\<And>x. x \<notin> set xs \<Longrightarrow>
mehta@13820
   173
  stkOk c l (r(x := g)) iL iR (Ref x) xs = stkOk c l r iL iR (Ref x) xs"
mehta@13820
   174
apply (induct xs)
mehta@13820
   175
 apply (auto simp:eq_sym_conv)
mehta@13820
   176
done
mehta@13820
   177
mehta@13820
   178
lemma [simp]: "\<And>x. x \<notin> set xs \<Longrightarrow>
mehta@13820
   179
 stkOk c (l(x := g)) r iL iR (Ref x) xs = stkOk c l r iL iR (Ref x) xs"
mehta@13820
   180
apply (induct xs)
mehta@13820
   181
 apply (auto simp:eq_sym_conv)
mehta@13820
   182
done
mehta@13820
   183
mehta@13820
   184
lemma [simp]: "\<And>x. x \<notin> set xs \<Longrightarrow>
mehta@13820
   185
 stkOk (c(x := g)) l r iL iR (Ref x) xs = stkOk c l r iL iR (Ref x) xs"
mehta@13820
   186
apply (induct xs)
mehta@13820
   187
 apply (auto simp:eq_sym_conv)
mehta@13820
   188
done
mehta@13820
   189
mehta@13820
   190
mehta@13820
   191
section{*The Schorr-Waite algorithm*}
mehta@13820
   192
mehta@13820
   193
mehta@13820
   194
theorem SchorrWaiteAlgorithm: 
mehta@13820
   195
"VARS c m l r t p q root
mehta@13820
   196
 {R = reachable (relS {l, r}) {root} \<and> (\<forall>x. \<not> m x) \<and> iR = r \<and> iL = l} 
mehta@13820
   197
 t := root; p := Null;
mehta@13820
   198
 WHILE p \<noteq> Null \<or> t \<noteq> Null \<and> \<not> t^.m
mehta@13820
   199
 INV {\<exists>stack.
nipkow@13875
   200
          List (S c l r) p stack \<and>                                         (*i1*)
nipkow@13875
   201
          (\<forall>x \<in> set stack. m x) \<and>                                        (*i2*)
nipkow@13875
   202
          R = reachable (relS{l, r}) {t,p} \<and>                           (*i3*)
nipkow@13875
   203
          (\<forall>x. x \<in> R \<and> \<not>m x \<longrightarrow>                                        (*i4*)
mehta@13820
   204
                 x \<in> reachable (relS{l,r}|m) ({t}\<union>set(map r stack))) \<and>
nipkow@13875
   205
          (\<forall>x. m x \<longrightarrow> x \<in> R) \<and>                                         (*i5*)
nipkow@13875
   206
          (\<forall>x. x \<notin> set stack \<longrightarrow> r x = iR x \<and> l x = iL x) \<and>       (*i6*)
nipkow@13875
   207
          (stkOk c l r iL iR t stack)                                    (*i7*) }
mehta@13820
   208
 DO IF t = Null \<or> t^.m
mehta@13820
   209
      THEN IF p^.c
nipkow@13875
   210
               THEN q := t; t := p; p := p^.r; t^.r := q               (*pop*)
nipkow@13875
   211
               ELSE q := t; t := p^.r; p^.r := p^.l;                      (*swing*)
mehta@13820
   212
                        p^.l := q; p^.c := True          FI    
nipkow@13875
   213
      ELSE q := p; p := t; t := t^.l; p^.l := q;                         (*push*)
mehta@13820
   214
               p^.m := True; p^.c := False            FI       OD
mehta@13820
   215
 {(\<forall>x. (x \<in> R) = m x) \<and> (r = iR \<and> l = iL) }"
nipkow@13875
   216
  (is "VARS c m l r t p q root {?Pre c m l r root} (?c1; ?c2; ?c3) {?Post c m l r}")
mehta@13820
   217
proof (vcg)
mehta@13820
   218
  let "While {(c, m, l, r, t, p, q, root). ?whileB m t p}
mehta@13820
   219
    {(c, m, l, r, t, p, q, root). ?inv c m l r t p} ?body" = ?c3
mehta@13820
   220
  {
mehta@13820
   221
mehta@13820
   222
    fix c m l r t p q root
mehta@13820
   223
    assume "?Pre c m l r root"
mehta@13820
   224
    thus "?inv c m l r root Null"  by (auto simp add: reachable_def addrs_def)
mehta@13820
   225
  next
mehta@13820
   226
mehta@13820
   227
    fix c m l r t p q
mehta@13820
   228
    let "\<exists>stack. ?Inv stack"  =  "?inv c m l r t p"
mehta@13820
   229
    assume a: "?inv c m l r t p \<and> \<not>(p \<noteq> Null \<or> t \<noteq> Null \<and> \<not> t^.m)"  
mehta@13820
   230
    then obtain stack where inv: "?Inv stack" by blast
mehta@13820
   231
    from a have pNull: "p = Null" and tDisj: "t=Null \<or> (t\<noteq>Null \<and> t^.m )" by auto
mehta@13820
   232
    let "?I1 \<and> _ \<and> _ \<and> ?I4 \<and> ?I5 \<and> ?I6 \<and> _"  =  "?Inv stack"
mehta@13820
   233
    from inv have i1: "?I1" and i4: "?I4" and i5: "?I5" and i6: "?I6" by simp+
mehta@13820
   234
    from pNull i1 have stackEmpty: "stack = []" by simp
mehta@13820
   235
    from tDisj i4 have RisMarked[rule_format]: "\<forall>x.  x \<in> R \<longrightarrow> m x"  by(auto simp: reachable_def addrs_def stackEmpty)
mehta@13820
   236
    from i5 i6 show "(\<forall>x.(x \<in> R) = m x) \<and> r = iR \<and> l = iL"  by(auto simp: stackEmpty expand_fun_eq intro:RisMarked)
mehta@13820
   237
mehta@13820
   238
  next   
mehta@13820
   239
      fix c m l r t p q root
mehta@13820
   240
      let "\<exists>stack. ?Inv stack"  =  "?inv c m l r t p"
mehta@13820
   241
      let "\<exists>stack. ?popInv stack"  =  "?inv c m l (r(p \<rightarrow> t)) p (p^.r)"
mehta@13820
   242
      let "\<exists>stack. ?swInv stack"  =
mehta@13820
   243
        "?inv (c(p \<rightarrow> True)) m (l(p \<rightarrow> t)) (r(p \<rightarrow> p^.l)) (p^.r) p"
mehta@13820
   244
      let "\<exists>stack. ?puInv stack"  =
mehta@13820
   245
        "?inv (c(t \<rightarrow> False)) (m(t \<rightarrow> True)) (l(t \<rightarrow> p)) r (t^.l) t"
mehta@13820
   246
      let "?ifB1"  =  "(t = Null \<or> t^.m)"
mehta@13820
   247
      let "?ifB2"  =  "p^.c" 
mehta@13820
   248
mehta@13820
   249
      assume "(\<exists>stack.?Inv stack) \<and> (p \<noteq> Null \<or> t \<noteq> Null \<and> \<not> t^.m)" (is "_ \<and> ?whileB")
mehta@13820
   250
      then obtain stack where inv: "?Inv stack" and whileB: "?whileB" by blast
mehta@13820
   251
      let "?I1 \<and> ?I2 \<and> ?I3 \<and> ?I4 \<and> ?I5 \<and> ?I6 \<and> ?I7" = "?Inv stack"
mehta@13820
   252
      from inv have i1: "?I1" and i2: "?I2" and i3: "?I3" and i4: "?I4"
mehta@13820
   253
                  and i5: "?I5" and i6: "?I6" and i7: "?I7" by simp+        
mehta@13820
   254
      have stackDist: "distinct (stack)" using i1 by (rule List_distinct)
mehta@13820
   255
mehta@13820
   256
      show "(?ifB1 \<longrightarrow> (?ifB2 \<longrightarrow> (\<exists>stack.?popInv stack)) \<and> 
mehta@13820
   257
                            (\<not>?ifB2 \<longrightarrow> (\<exists>stack.?swInv stack)) ) \<and>
mehta@13820
   258
	      (\<not>?ifB1 \<longrightarrow> (\<exists>stack.?puInv stack))"
mehta@13820
   259
      proof - 
mehta@13820
   260
	{
mehta@13820
   261
	  assume ifB1: "t = Null \<or> t^.m" and ifB2: "p^.c"
mehta@13820
   262
	  from ifB1 whileB have pNotNull: "p \<noteq> Null" by auto
mehta@13820
   263
	  then obtain addr_p where addr_p_eq: "p = Ref addr_p" by auto
mehta@13820
   264
	  with i1 obtain stack_tl where stack_eq: "stack = (addr p) # stack_tl"
mehta@13820
   265
	    by auto
mehta@13820
   266
	  with i2 have m_addr_p: "p^.m" by auto
mehta@13820
   267
	  have stackDist: "distinct (stack)" using i1 by (rule List_distinct)
mehta@13820
   268
	  from stack_eq stackDist have p_notin_stack_tl: "addr p \<notin> set stack_tl" by simp
mehta@13820
   269
	  let "?poI1\<and> ?poI2\<and> ?poI3\<and> ?poI4\<and> ?poI5\<and> ?poI6\<and> ?poI7" = "?popInv stack_tl"
mehta@13820
   270
	  have "?popInv stack_tl"
mehta@13820
   271
	  proof -
mehta@13820
   272
mehta@13820
   273
	    -- {*List property is maintained:*}
mehta@13820
   274
	    from i1 p_notin_stack_tl ifB2
mehta@13820
   275
	    have poI1: "List (S c l (r(p \<rightarrow> t))) (p^.r) stack_tl" 
mehta@13820
   276
	      by(simp add: addr_p_eq stack_eq, simp add: S_def)
mehta@13820
   277
mehta@13820
   278
	    moreover
mehta@13820
   279
	    -- {*Everything on the stack is marked:*}
mehta@13820
   280
	    from i2 have poI2: "\<forall> x \<in> set stack_tl. m x" by (simp add:stack_eq)
mehta@13820
   281
	    moreover
mehta@13820
   282
mehta@13820
   283
	    -- {*Everything is still reachable:*}
mehta@13820
   284
	    let "(R = reachable ?Ra ?A)" = "?I3"
mehta@13820
   285
	    let "?Rb" = "(relS {l, r(p \<rightarrow> t)})"
mehta@13820
   286
	    let "?B" = "{p, p^.r}"
mehta@13820
   287
	    -- {*Our goal is @{text"R = reachable ?Rb ?B"}.*}
mehta@13820
   288
	    have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B" (is "?L = ?R")
mehta@13820
   289
	    proof
mehta@13820
   290
	      show "?L \<subseteq> ?R"
mehta@13820
   291
	      proof (rule still_reachable)
mehta@13820
   292
		show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B" by(fastsimp simp:addrs_def relS_def rel_def addr_p_eq 
mehta@13820
   293
		     intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   294
		show "\<forall>(x,y) \<in> ?Ra-?Rb. y \<in> (?Rb\<^sup>* `` addrs ?B)" by (clarsimp simp:relS_def) 
mehta@13820
   295
	             (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
mehta@13820
   296
	      qed
mehta@13820
   297
	      show "?R \<subseteq> ?L"
mehta@13820
   298
	      proof (rule still_reachable)
mehta@13820
   299
		show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
mehta@13820
   300
		  by(fastsimp simp:addrs_def rel_defs addr_p_eq 
mehta@13820
   301
		      intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   302
	      next
mehta@13820
   303
		show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
mehta@13820
   304
		  by (clarsimp simp:relS_def) 
mehta@13820
   305
	             (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd2)
mehta@13820
   306
	      qed
mehta@13820
   307
	    qed
mehta@13820
   308
	    with i3 have poI3: "R = reachable ?Rb ?B"  by (simp add:reachable_def) 
mehta@13820
   309
	    moreover
mehta@13820
   310
mehta@13820
   311
	    -- "If it is reachable and not marked, it is still reachable using..."
mehta@13820
   312
	    let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A"  =  ?I4	    
mehta@13820
   313
	    let "?Rb" = "relS {l, r(p \<rightarrow> t)} | m"
mehta@13820
   314
	    let "?B" = "{p} \<union> set (map (r(p \<rightarrow> t)) stack_tl)"
mehta@13820
   315
	    -- {*Our goal is @{text"\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B"}.*}
mehta@13820
   316
	    let ?T = "{t, p^.r}"
mehta@13820
   317
mehta@13820
   318
	    have "?Ra\<^sup>* `` addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
mehta@13820
   319
	    proof (rule still_reachable)
mehta@13820
   320
	      have rewrite: "\<forall>s\<in>set stack_tl. (r(p \<rightarrow> t)) s = r s"
mehta@13820
   321
		by (auto simp add:p_notin_stack_tl intro:fun_upd_other)	
mehta@13820
   322
	      show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
mehta@13820
   323
		by (fastsimp cong:map_cong simp:stack_eq addrs_def rewrite intro:self_reachable)
mehta@13820
   324
	      show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
mehta@13820
   325
		by (clarsimp simp:restr_def relS_def) 
mehta@13820
   326
	          (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
mehta@13820
   327
 	    qed
mehta@13820
   328
	    -- "We now bring a term from the right to the left of the subset relation."
mehta@13820
   329
	    hence subset: "?Ra\<^sup>* `` addrs ?A - ?Rb\<^sup>* `` addrs ?T \<subseteq> ?Rb\<^sup>* `` addrs ?B"
mehta@13820
   330
	      by blast
mehta@13820
   331
	    have poI4: "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B"
mehta@13820
   332
	    proof (rule allI, rule impI)
mehta@13820
   333
	      fix x
mehta@13820
   334
	      assume a: "x \<in> R \<and> \<not> m x"
mehta@13820
   335
	      -- {*First, a disjunction on @{term"p^.r"} used later in the proof*}
mehta@13820
   336
	      have pDisj:"p^.r = Null \<or> (p^.r \<noteq> Null \<and> p^.r^.m)" using poI1 poI2 
mehta@13820
   337
		by auto
mehta@13820
   338
	      -- {*@{term x} belongs to the left hand side of @{thm[source] subset}:*}
mehta@13820
   339
	      have incl: "x \<in> ?Ra\<^sup>*``addrs ?A" using  a i4 by (simp only:reachable_def, clarsimp)
mehta@13820
   340
	      have excl: "x \<notin> ?Rb\<^sup>*`` addrs ?T" using pDisj ifB1 a by (auto simp add:addrs_def)
mehta@13820
   341
	      -- {*And therefore also belongs to the right hand side of @{thm[source]subset},*}
mehta@13820
   342
	      -- {*which corresponds to our goal.*}
mehta@13820
   343
	      from incl excl subset  show "x \<in> reachable ?Rb ?B" by (auto simp add:reachable_def)
mehta@13820
   344
	    qed
mehta@13820
   345
	    moreover
mehta@13820
   346
mehta@13820
   347
	    -- "If it is marked, then it is reachable"
mehta@13820
   348
	    from i5 have poI5: "\<forall>x. m x \<longrightarrow> x \<in> R" .
mehta@13820
   349
	    moreover
mehta@13820
   350
mehta@13820
   351
	    -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
mehta@13820
   352
	    from i7 i6 ifB2 
mehta@13820
   353
	    have poI6: "\<forall>x. x \<notin> set stack_tl \<longrightarrow> (r(p \<rightarrow> t)) x = iR x \<and> l x = iL x" 
mehta@13820
   354
	      by(auto simp: addr_p_eq stack_eq fun_upd_apply)
mehta@13820
   355
mehta@13820
   356
	    moreover
mehta@13820
   357
mehta@13820
   358
	    -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
mehta@13820
   359
	    from p_notin_stack_tl i7 have poI7: "stkOk c l (r(p \<rightarrow> t)) iL iR p stack_tl"
mehta@13820
   360
	      by (clarsimp simp:stack_eq addr_p_eq)
mehta@13820
   361
mehta@13820
   362
	    ultimately show "?popInv stack_tl" by simp
mehta@13820
   363
	  qed
mehta@13820
   364
	  hence "\<exists>stack. ?popInv stack" ..
mehta@13820
   365
	}
mehta@13820
   366
	moreover
mehta@13820
   367
mehta@13820
   368
	-- "Proofs of the Swing and Push arm follow."
mehta@13820
   369
	-- "Since they are in principle simmilar to the Pop arm proof,"
mehta@13820
   370
	-- "we show fewer comments and use frequent pattern matching."
mehta@13820
   371
	{
mehta@13820
   372
	  -- "Swing arm"
mehta@13820
   373
	  assume ifB1: "?ifB1" and nifB2: "\<not>?ifB2"
mehta@13820
   374
	  from ifB1 whileB have pNotNull: "p \<noteq> Null" by clarsimp
mehta@13820
   375
	  then obtain addr_p where addr_p_eq: "p = Ref addr_p" by clarsimp
mehta@13820
   376
	  with i1 obtain stack_tl where stack_eq: "stack = (addr p) # stack_tl" by clarsimp
mehta@13820
   377
	  with i2 have m_addr_p: "p^.m" by clarsimp
mehta@13820
   378
	  from stack_eq stackDist have p_notin_stack_tl: "(addr p) \<notin> set stack_tl"
mehta@13820
   379
	    by simp
mehta@13820
   380
	  let "?swI1\<and>?swI2\<and>?swI3\<and>?swI4\<and>?swI5\<and>?swI6\<and>?swI7" = "?swInv stack"
mehta@13820
   381
	  have "?swInv stack"
mehta@13820
   382
	  proof -
mehta@13820
   383
	    
mehta@13820
   384
	    -- {*List property is maintained:*}
mehta@13820
   385
	    from i1 p_notin_stack_tl nifB2
mehta@13820
   386
	    have swI1: "?swI1"
mehta@13820
   387
	      by (simp add:addr_p_eq stack_eq, simp add:S_def)
mehta@13820
   388
	    moreover
mehta@13820
   389
	    
mehta@13820
   390
	    -- {*Everything on the stack is marked:*}
mehta@13820
   391
	    from i2
mehta@13820
   392
	    have swI2: "?swI2" .
mehta@13820
   393
	    moreover
mehta@13820
   394
	    
mehta@13820
   395
	    -- {*Everything is still reachable:*}
mehta@13820
   396
	    let "R = reachable ?Ra ?A" = "?I3"
mehta@13820
   397
	    let "R = reachable ?Rb ?B" = "?swI3"
mehta@13820
   398
	    have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B"
mehta@13820
   399
	    proof (rule still_reachable_eq)
mehta@13820
   400
	      show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B"
mehta@13820
   401
		by(fastsimp simp:addrs_def rel_defs addr_p_eq intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   402
	    next
mehta@13820
   403
	      show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
mehta@13820
   404
		by(fastsimp simp:addrs_def rel_defs addr_p_eq intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   405
	    next
mehta@13820
   406
	      show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``addrs ?B)"
mehta@13820
   407
		by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd1)
mehta@13820
   408
	    next
mehta@13820
   409
	      show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
mehta@13820
   410
		by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd2)
mehta@13820
   411
	    qed
mehta@13820
   412
	    with i3
mehta@13820
   413
	    have swI3: "?swI3" by (simp add:reachable_def) 
mehta@13820
   414
	    moreover
mehta@13820
   415
mehta@13820
   416
	    -- "If it is reachable and not marked, it is still reachable using..."
mehta@13820
   417
	    let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A" = ?I4
mehta@13820
   418
	    let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B" = ?swI4
mehta@13820
   419
	    let ?T = "{t}"
mehta@13820
   420
	    have "?Ra\<^sup>*``addrs ?A \<subseteq> ?Rb\<^sup>*``(addrs ?B \<union> addrs ?T)"
mehta@13820
   421
	    proof (rule still_reachable)
mehta@13820
   422
	      have rewrite: "(\<forall>s\<in>set stack_tl. (r(addr p := l(addr p))) s = r s)"
mehta@13820
   423
		by (auto simp add:p_notin_stack_tl intro:fun_upd_other)
mehta@13820
   424
	      show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
mehta@13820
   425
		by (fastsimp cong:map_cong simp:stack_eq addrs_def rewrite intro:self_reachable)
mehta@13820
   426
	    next
mehta@13820
   427
	      show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
mehta@13820
   428
		by (clarsimp simp:relS_def restr_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd1)
mehta@13820
   429
	    qed
mehta@13820
   430
	    then have subset: "?Ra\<^sup>*``addrs ?A - ?Rb\<^sup>*``addrs ?T \<subseteq> ?Rb\<^sup>*``addrs ?B"
mehta@13820
   431
	      by blast
mehta@13820
   432
	    have ?swI4
mehta@13820
   433
	    proof (rule allI, rule impI)
mehta@13820
   434
	      fix x
mehta@13820
   435
	      assume a: "x \<in> R \<and>\<not> m x"
mehta@13820
   436
	      with i4 addr_p_eq stack_eq  have inc: "x \<in> ?Ra\<^sup>*``addrs ?A" 
mehta@13820
   437
		by (simp only:reachable_def, clarsimp)
mehta@13820
   438
	      with ifB1 a 
mehta@13820
   439
	      have exc: "x \<notin> ?Rb\<^sup>*`` addrs ?T" 
mehta@13820
   440
		by (auto simp add:addrs_def)
mehta@13820
   441
	      from inc exc subset  show "x \<in> reachable ?Rb ?B" 
mehta@13820
   442
		by (auto simp add:reachable_def)
mehta@13820
   443
	    qed
mehta@13820
   444
	    moreover
mehta@13820
   445
	    
mehta@13820
   446
	    -- "If it is marked, then it is reachable"
mehta@13820
   447
	    from i5
mehta@13820
   448
	    have "?swI5" .
mehta@13820
   449
	    moreover
mehta@13820
   450
mehta@13820
   451
	    -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
mehta@13820
   452
	    from i6 stack_eq
mehta@13820
   453
	    have "?swI6"
mehta@13820
   454
	      by clarsimp 	    
mehta@13820
   455
	    moreover
mehta@13820
   456
mehta@13820
   457
	    -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
mehta@13820
   458
	    from stackDist i7 nifB2 
mehta@13820
   459
	    have "?swI7"
mehta@13820
   460
	      by (clarsimp simp:addr_p_eq stack_eq)
mehta@13820
   461
mehta@13820
   462
	    ultimately show ?thesis by auto
mehta@13820
   463
	  qed
mehta@13820
   464
	  then have "\<exists>stack. ?swInv stack" by blast
mehta@13820
   465
	}
mehta@13820
   466
	moreover
mehta@13820
   467
mehta@13820
   468
	{
mehta@13820
   469
	  -- "Push arm"
mehta@13820
   470
	  assume nifB1: "\<not>?ifB1"
mehta@13820
   471
	  from nifB1 whileB have tNotNull: "t \<noteq> Null" by clarsimp
mehta@13820
   472
	  then obtain addr_t where addr_t_eq: "t = Ref addr_t" by clarsimp
mehta@13820
   473
	  with i1 obtain new_stack where new_stack_eq: "new_stack = (addr t) # stack" by clarsimp
mehta@13820
   474
	  from tNotNull nifB1 have n_m_addr_t: "\<not> (t^.m)" by clarsimp
mehta@13820
   475
	  with i2 have t_notin_stack: "(addr t) \<notin> set stack" by blast
mehta@13820
   476
	  let "?puI1\<and>?puI2\<and>?puI3\<and>?puI4\<and>?puI5\<and>?puI6\<and>?puI7" = "?puInv new_stack"
mehta@13820
   477
	  have "?puInv new_stack"
mehta@13820
   478
	  proof -
mehta@13820
   479
	    
mehta@13820
   480
	    -- {*List property is maintained:*}
mehta@13820
   481
	    from i1 t_notin_stack
mehta@13820
   482
	    have puI1: "?puI1"
mehta@13820
   483
	      by (simp add:addr_t_eq new_stack_eq, simp add:S_def)
mehta@13820
   484
	    moreover
mehta@13820
   485
	    
mehta@13820
   486
	    -- {*Everything on the stack is marked:*}
mehta@13820
   487
	    from i2
mehta@13820
   488
	    have puI2: "?puI2" 
mehta@13820
   489
	      by (simp add:new_stack_eq fun_upd_apply)
mehta@13820
   490
	    moreover
mehta@13820
   491
	    
mehta@13820
   492
	    -- {*Everything is still reachable:*}
mehta@13820
   493
	    let "R = reachable ?Ra ?A" = "?I3"
mehta@13820
   494
	    let "R = reachable ?Rb ?B" = "?puI3"
mehta@13820
   495
	    have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B"
mehta@13820
   496
	    proof (rule still_reachable_eq)
mehta@13820
   497
	      show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B"
mehta@13820
   498
		by(fastsimp simp:addrs_def rel_defs addr_t_eq intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   499
	    next
mehta@13820
   500
	      show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
mehta@13820
   501
		by(fastsimp simp:addrs_def rel_defs addr_t_eq intro:oneStep_reachable Image_iff[THEN iffD2])
mehta@13820
   502
	    next
mehta@13820
   503
	      show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``addrs ?B)"
mehta@13820
   504
		by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
mehta@13820
   505
	    next
mehta@13820
   506
	      show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
mehta@13820
   507
		by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd2)
mehta@13820
   508
	    qed
mehta@13820
   509
	    with i3
mehta@13820
   510
	    have puI3: "?puI3" by (simp add:reachable_def) 
mehta@13820
   511
	    moreover
mehta@13820
   512
	    
mehta@13820
   513
	    -- "If it is reachable and not marked, it is still reachable using..."
mehta@13820
   514
	    let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A" = ?I4
mehta@13820
   515
	    let "\<forall>x. x \<in> R \<and> \<not> ?new_m x \<longrightarrow> x \<in> reachable ?Rb ?B" = ?puI4
mehta@13820
   516
	    let ?T = "{t}"
mehta@13820
   517
	    have "?Ra\<^sup>*``addrs ?A \<subseteq> ?Rb\<^sup>*``(addrs ?B \<union> addrs ?T)"
mehta@13820
   518
	    proof (rule still_reachable)
mehta@13820
   519
	      show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
mehta@13820
   520
		by (fastsimp simp:new_stack_eq addrs_def intro:self_reachable)
mehta@13820
   521
	    next
mehta@13820
   522
	      show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
mehta@13820
   523
		by (clarsimp simp:relS_def new_stack_eq restr_un restr_upd) 
mehta@13820
   524
	           (fastsimp simp add:rel_def Image_iff restr_def addrs_def fun_upd_apply addr_t_eq dest:rel_upd3)
mehta@13820
   525
	    qed
mehta@13820
   526
	    then have subset: "?Ra\<^sup>*``addrs ?A - ?Rb\<^sup>*``addrs ?T \<subseteq> ?Rb\<^sup>*``addrs ?B"
mehta@13820
   527
	      by blast
mehta@13820
   528
	    have ?puI4
mehta@13820
   529
	    proof (rule allI, rule impI)
mehta@13820
   530
	      fix x
mehta@13820
   531
	      assume a: "x \<in> R \<and> \<not> ?new_m x"
mehta@13820
   532
	      have xDisj: "x=(addr t) \<or> x\<noteq>(addr t)" by simp
mehta@13820
   533
	      with i4 a have inc: "x \<in> ?Ra\<^sup>*``addrs ?A"
mehta@13820
   534
		by (fastsimp simp:addr_t_eq addrs_def reachable_def intro:self_reachable)
mehta@13820
   535
	      have exc: "x \<notin> ?Rb\<^sup>*`` addrs ?T"
mehta@13820
   536
		using xDisj a n_m_addr_t
mehta@13820
   537
		by (clarsimp simp add:addrs_def addr_t_eq) 
mehta@13820
   538
	      from inc exc subset  show "x \<in> reachable ?Rb ?B" 
mehta@13820
   539
		by (auto simp add:reachable_def)
mehta@13820
   540
	    qed  
mehta@13820
   541
	    moreover
mehta@13820
   542
	    
mehta@13820
   543
	    -- "If it is marked, then it is reachable"
mehta@13820
   544
	    from i5
mehta@13820
   545
	    have "?puI5"
mehta@13820
   546
	      by (auto simp:addrs_def i3 reachable_def addr_t_eq fun_upd_apply intro:self_reachable)
mehta@13820
   547
	    moreover
mehta@13820
   548
	    
mehta@13820
   549
	    -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
mehta@13820
   550
	    from i6 
mehta@13820
   551
	    have "?puI6"
mehta@13820
   552
	      by (simp add:new_stack_eq)
mehta@13820
   553
	    moreover
mehta@13820
   554
mehta@13820
   555
	    -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
mehta@13820
   556
	    from stackDist i6 t_notin_stack i7
mehta@13820
   557
	    have "?puI7" by (clarsimp simp:addr_t_eq new_stack_eq)
mehta@13820
   558
mehta@13820
   559
	    ultimately show ?thesis by auto
mehta@13820
   560
	  qed
mehta@13820
   561
	  then have "\<exists>stack. ?puInv stack" by blast
mehta@13820
   562
mehta@13820
   563
	}
mehta@13820
   564
	ultimately show ?thesis by blast
mehta@13820
   565
      qed
mehta@13820
   566
    }
mehta@13820
   567
  qed
mehta@13820
   568
mehta@13820
   569
end
mehta@13820
   570