src/HOL/Hoare/SchorrWaite.thy
author wenzelm
Mon Mar 22 20:58:52 2010 +0100 (2010-03-22)
changeset 35898 c890a3835d15
parent 32960 69916a850301
child 36866 426d5781bb25
permissions -rw-r--r--
recovered header;
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
haftmann@16417
    10
theory SchorrWaite imports HeapSyntax begin
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
wenzelm@32960
   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)
wenzelm@32960
   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>
wenzelm@32960
   258
              (\<not>?ifB1 \<longrightarrow> (\<exists>stack.?puInv stack))"
mehta@13820
   259
      proof - 
wenzelm@32960
   260
        {
wenzelm@32960
   261
          assume ifB1: "t = Null \<or> t^.m" and ifB2: "p^.c"
wenzelm@32960
   262
          from ifB1 whileB have pNotNull: "p \<noteq> Null" by auto
wenzelm@32960
   263
          then obtain addr_p where addr_p_eq: "p = Ref addr_p" by auto
wenzelm@32960
   264
          with i1 obtain stack_tl where stack_eq: "stack = (addr p) # stack_tl"
wenzelm@32960
   265
            by auto
wenzelm@32960
   266
          with i2 have m_addr_p: "p^.m" by auto
wenzelm@32960
   267
          have stackDist: "distinct (stack)" using i1 by (rule List_distinct)
wenzelm@32960
   268
          from stack_eq stackDist have p_notin_stack_tl: "addr p \<notin> set stack_tl" by simp
wenzelm@32960
   269
          let "?poI1\<and> ?poI2\<and> ?poI3\<and> ?poI4\<and> ?poI5\<and> ?poI6\<and> ?poI7" = "?popInv stack_tl"
wenzelm@32960
   270
          have "?popInv stack_tl"
wenzelm@32960
   271
          proof -
mehta@13820
   272
wenzelm@32960
   273
            -- {*List property is maintained:*}
wenzelm@32960
   274
            from i1 p_notin_stack_tl ifB2
wenzelm@32960
   275
            have poI1: "List (S c l (r(p \<rightarrow> t))) (p^.r) stack_tl" 
wenzelm@32960
   276
              by(simp add: addr_p_eq stack_eq, simp add: S_def)
mehta@13820
   277
wenzelm@32960
   278
            moreover
wenzelm@32960
   279
            -- {*Everything on the stack is marked:*}
wenzelm@32960
   280
            from i2 have poI2: "\<forall> x \<in> set stack_tl. m x" by (simp add:stack_eq)
wenzelm@32960
   281
            moreover
mehta@13820
   282
wenzelm@32960
   283
            -- {*Everything is still reachable:*}
wenzelm@32960
   284
            let "(R = reachable ?Ra ?A)" = "?I3"
wenzelm@32960
   285
            let "?Rb" = "(relS {l, r(p \<rightarrow> t)})"
wenzelm@32960
   286
            let "?B" = "{p, p^.r}"
wenzelm@32960
   287
            -- {*Our goal is @{text"R = reachable ?Rb ?B"}.*}
wenzelm@32960
   288
            have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B" (is "?L = ?R")
wenzelm@32960
   289
            proof
wenzelm@32960
   290
              show "?L \<subseteq> ?R"
wenzelm@32960
   291
              proof (rule still_reachable)
wenzelm@32960
   292
                show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B" by(fastsimp simp:addrs_def relS_def rel_def addr_p_eq 
wenzelm@32960
   293
                     intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   294
                show "\<forall>(x,y) \<in> ?Ra-?Rb. y \<in> (?Rb\<^sup>* `` addrs ?B)" by (clarsimp simp:relS_def) 
wenzelm@32960
   295
                     (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
wenzelm@32960
   296
              qed
wenzelm@32960
   297
              show "?R \<subseteq> ?L"
wenzelm@32960
   298
              proof (rule still_reachable)
wenzelm@32960
   299
                show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
wenzelm@32960
   300
                  by(fastsimp simp:addrs_def rel_defs addr_p_eq 
wenzelm@32960
   301
                      intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   302
              next
wenzelm@32960
   303
                show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
wenzelm@32960
   304
                  by (clarsimp simp:relS_def) 
wenzelm@32960
   305
                     (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd2)
wenzelm@32960
   306
              qed
wenzelm@32960
   307
            qed
wenzelm@32960
   308
            with i3 have poI3: "R = reachable ?Rb ?B"  by (simp add:reachable_def) 
wenzelm@32960
   309
            moreover
mehta@13820
   310
wenzelm@32960
   311
            -- "If it is reachable and not marked, it is still reachable using..."
wenzelm@32960
   312
            let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A"  =  ?I4        
wenzelm@32960
   313
            let "?Rb" = "relS {l, r(p \<rightarrow> t)} | m"
wenzelm@32960
   314
            let "?B" = "{p} \<union> set (map (r(p \<rightarrow> t)) stack_tl)"
wenzelm@32960
   315
            -- {*Our goal is @{text"\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B"}.*}
wenzelm@32960
   316
            let ?T = "{t, p^.r}"
mehta@13820
   317
wenzelm@32960
   318
            have "?Ra\<^sup>* `` addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
wenzelm@32960
   319
            proof (rule still_reachable)
wenzelm@32960
   320
              have rewrite: "\<forall>s\<in>set stack_tl. (r(p \<rightarrow> t)) s = r s"
wenzelm@32960
   321
                by (auto simp add:p_notin_stack_tl intro:fun_upd_other) 
wenzelm@32960
   322
              show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
wenzelm@32960
   323
                by (fastsimp cong:map_cong simp:stack_eq addrs_def rewrite intro:self_reachable)
wenzelm@32960
   324
              show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
wenzelm@32960
   325
                by (clarsimp simp:restr_def relS_def) 
wenzelm@32960
   326
                  (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
wenzelm@32960
   327
            qed
wenzelm@32960
   328
            -- "We now bring a term from the right to the left of the subset relation."
wenzelm@32960
   329
            hence subset: "?Ra\<^sup>* `` addrs ?A - ?Rb\<^sup>* `` addrs ?T \<subseteq> ?Rb\<^sup>* `` addrs ?B"
wenzelm@32960
   330
              by blast
wenzelm@32960
   331
            have poI4: "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B"
wenzelm@32960
   332
            proof (rule allI, rule impI)
wenzelm@32960
   333
              fix x
wenzelm@32960
   334
              assume a: "x \<in> R \<and> \<not> m x"
wenzelm@32960
   335
              -- {*First, a disjunction on @{term"p^.r"} used later in the proof*}
wenzelm@32960
   336
              have pDisj:"p^.r = Null \<or> (p^.r \<noteq> Null \<and> p^.r^.m)" using poI1 poI2 
wenzelm@32960
   337
                by auto
wenzelm@32960
   338
              -- {*@{term x} belongs to the left hand side of @{thm[source] subset}:*}
wenzelm@32960
   339
              have incl: "x \<in> ?Ra\<^sup>*``addrs ?A" using  a i4 by (simp only:reachable_def, clarsimp)
wenzelm@32960
   340
              have excl: "x \<notin> ?Rb\<^sup>*`` addrs ?T" using pDisj ifB1 a by (auto simp add:addrs_def)
wenzelm@32960
   341
              -- {*And therefore also belongs to the right hand side of @{thm[source]subset},*}
wenzelm@32960
   342
              -- {*which corresponds to our goal.*}
wenzelm@32960
   343
              from incl excl subset  show "x \<in> reachable ?Rb ?B" by (auto simp add:reachable_def)
wenzelm@32960
   344
            qed
wenzelm@32960
   345
            moreover
mehta@13820
   346
wenzelm@32960
   347
            -- "If it is marked, then it is reachable"
wenzelm@32960
   348
            from i5 have poI5: "\<forall>x. m x \<longrightarrow> x \<in> R" .
wenzelm@32960
   349
            moreover
mehta@13820
   350
wenzelm@32960
   351
            -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
wenzelm@32960
   352
            from i7 i6 ifB2 
wenzelm@32960
   353
            have poI6: "\<forall>x. x \<notin> set stack_tl \<longrightarrow> (r(p \<rightarrow> t)) x = iR x \<and> l x = iL x" 
wenzelm@32960
   354
              by(auto simp: addr_p_eq stack_eq fun_upd_apply)
mehta@13820
   355
wenzelm@32960
   356
            moreover
mehta@13820
   357
wenzelm@32960
   358
            -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
wenzelm@32960
   359
            from p_notin_stack_tl i7 have poI7: "stkOk c l (r(p \<rightarrow> t)) iL iR p stack_tl"
wenzelm@32960
   360
              by (clarsimp simp:stack_eq addr_p_eq)
mehta@13820
   361
wenzelm@32960
   362
            ultimately show "?popInv stack_tl" by simp
wenzelm@32960
   363
          qed
wenzelm@32960
   364
          hence "\<exists>stack. ?popInv stack" ..
wenzelm@32960
   365
        }
wenzelm@32960
   366
        moreover
mehta@13820
   367
wenzelm@32960
   368
        -- "Proofs of the Swing and Push arm follow."
wenzelm@32960
   369
        -- "Since they are in principle simmilar to the Pop arm proof,"
wenzelm@32960
   370
        -- "we show fewer comments and use frequent pattern matching."
wenzelm@32960
   371
        {
wenzelm@32960
   372
          -- "Swing arm"
wenzelm@32960
   373
          assume ifB1: "?ifB1" and nifB2: "\<not>?ifB2"
wenzelm@32960
   374
          from ifB1 whileB have pNotNull: "p \<noteq> Null" by clarsimp
wenzelm@32960
   375
          then obtain addr_p where addr_p_eq: "p = Ref addr_p" by clarsimp
wenzelm@32960
   376
          with i1 obtain stack_tl where stack_eq: "stack = (addr p) # stack_tl" by clarsimp
wenzelm@32960
   377
          with i2 have m_addr_p: "p^.m" by clarsimp
wenzelm@32960
   378
          from stack_eq stackDist have p_notin_stack_tl: "(addr p) \<notin> set stack_tl"
wenzelm@32960
   379
            by simp
wenzelm@32960
   380
          let "?swI1\<and>?swI2\<and>?swI3\<and>?swI4\<and>?swI5\<and>?swI6\<and>?swI7" = "?swInv stack"
wenzelm@32960
   381
          have "?swInv stack"
wenzelm@32960
   382
          proof -
wenzelm@32960
   383
            
wenzelm@32960
   384
            -- {*List property is maintained:*}
wenzelm@32960
   385
            from i1 p_notin_stack_tl nifB2
wenzelm@32960
   386
            have swI1: "?swI1"
wenzelm@32960
   387
              by (simp add:addr_p_eq stack_eq, simp add:S_def)
wenzelm@32960
   388
            moreover
wenzelm@32960
   389
            
wenzelm@32960
   390
            -- {*Everything on the stack is marked:*}
wenzelm@32960
   391
            from i2
wenzelm@32960
   392
            have swI2: "?swI2" .
wenzelm@32960
   393
            moreover
wenzelm@32960
   394
            
wenzelm@32960
   395
            -- {*Everything is still reachable:*}
wenzelm@32960
   396
            let "R = reachable ?Ra ?A" = "?I3"
wenzelm@32960
   397
            let "R = reachable ?Rb ?B" = "?swI3"
wenzelm@32960
   398
            have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B"
wenzelm@32960
   399
            proof (rule still_reachable_eq)
wenzelm@32960
   400
              show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B"
wenzelm@32960
   401
                by(fastsimp simp:addrs_def rel_defs addr_p_eq intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   402
            next
wenzelm@32960
   403
              show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
wenzelm@32960
   404
                by(fastsimp simp:addrs_def rel_defs addr_p_eq intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   405
            next
wenzelm@32960
   406
              show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``addrs ?B)"
wenzelm@32960
   407
                by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd1)
wenzelm@32960
   408
            next
wenzelm@32960
   409
              show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
wenzelm@32960
   410
                by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd2)
wenzelm@32960
   411
            qed
wenzelm@32960
   412
            with i3
wenzelm@32960
   413
            have swI3: "?swI3" by (simp add:reachable_def) 
wenzelm@32960
   414
            moreover
mehta@13820
   415
wenzelm@32960
   416
            -- "If it is reachable and not marked, it is still reachable using..."
wenzelm@32960
   417
            let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A" = ?I4
wenzelm@32960
   418
            let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Rb ?B" = ?swI4
wenzelm@32960
   419
            let ?T = "{t}"
wenzelm@32960
   420
            have "?Ra\<^sup>*``addrs ?A \<subseteq> ?Rb\<^sup>*``(addrs ?B \<union> addrs ?T)"
wenzelm@32960
   421
            proof (rule still_reachable)
wenzelm@32960
   422
              have rewrite: "(\<forall>s\<in>set stack_tl. (r(addr p := l(addr p))) s = r s)"
wenzelm@32960
   423
                by (auto simp add:p_notin_stack_tl intro:fun_upd_other)
wenzelm@32960
   424
              show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
wenzelm@32960
   425
                by (fastsimp cong:map_cong simp:stack_eq addrs_def rewrite intro:self_reachable)
wenzelm@32960
   426
            next
wenzelm@32960
   427
              show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
wenzelm@32960
   428
                by (clarsimp simp:relS_def restr_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd1)
wenzelm@32960
   429
            qed
wenzelm@32960
   430
            then have subset: "?Ra\<^sup>*``addrs ?A - ?Rb\<^sup>*``addrs ?T \<subseteq> ?Rb\<^sup>*``addrs ?B"
wenzelm@32960
   431
              by blast
wenzelm@32960
   432
            have ?swI4
wenzelm@32960
   433
            proof (rule allI, rule impI)
wenzelm@32960
   434
              fix x
wenzelm@32960
   435
              assume a: "x \<in> R \<and>\<not> m x"
wenzelm@32960
   436
              with i4 addr_p_eq stack_eq  have inc: "x \<in> ?Ra\<^sup>*``addrs ?A" 
wenzelm@32960
   437
                by (simp only:reachable_def, clarsimp)
wenzelm@32960
   438
              with ifB1 a 
wenzelm@32960
   439
              have exc: "x \<notin> ?Rb\<^sup>*`` addrs ?T" 
wenzelm@32960
   440
                by (auto simp add:addrs_def)
wenzelm@32960
   441
              from inc exc subset  show "x \<in> reachable ?Rb ?B" 
wenzelm@32960
   442
                by (auto simp add:reachable_def)
wenzelm@32960
   443
            qed
wenzelm@32960
   444
            moreover
wenzelm@32960
   445
            
wenzelm@32960
   446
            -- "If it is marked, then it is reachable"
wenzelm@32960
   447
            from i5
wenzelm@32960
   448
            have "?swI5" .
wenzelm@32960
   449
            moreover
mehta@13820
   450
wenzelm@32960
   451
            -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
wenzelm@32960
   452
            from i6 stack_eq
wenzelm@32960
   453
            have "?swI6"
wenzelm@32960
   454
              by clarsimp           
wenzelm@32960
   455
            moreover
mehta@13820
   456
wenzelm@32960
   457
            -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
wenzelm@32960
   458
            from stackDist i7 nifB2 
wenzelm@32960
   459
            have "?swI7"
wenzelm@32960
   460
              by (clarsimp simp:addr_p_eq stack_eq)
mehta@13820
   461
wenzelm@32960
   462
            ultimately show ?thesis by auto
wenzelm@32960
   463
          qed
wenzelm@32960
   464
          then have "\<exists>stack. ?swInv stack" by blast
wenzelm@32960
   465
        }
wenzelm@32960
   466
        moreover
mehta@13820
   467
wenzelm@32960
   468
        {
wenzelm@32960
   469
          -- "Push arm"
wenzelm@32960
   470
          assume nifB1: "\<not>?ifB1"
wenzelm@32960
   471
          from nifB1 whileB have tNotNull: "t \<noteq> Null" by clarsimp
wenzelm@32960
   472
          then obtain addr_t where addr_t_eq: "t = Ref addr_t" by clarsimp
wenzelm@32960
   473
          with i1 obtain new_stack where new_stack_eq: "new_stack = (addr t) # stack" by clarsimp
wenzelm@32960
   474
          from tNotNull nifB1 have n_m_addr_t: "\<not> (t^.m)" by clarsimp
wenzelm@32960
   475
          with i2 have t_notin_stack: "(addr t) \<notin> set stack" by blast
wenzelm@32960
   476
          let "?puI1\<and>?puI2\<and>?puI3\<and>?puI4\<and>?puI5\<and>?puI6\<and>?puI7" = "?puInv new_stack"
wenzelm@32960
   477
          have "?puInv new_stack"
wenzelm@32960
   478
          proof -
wenzelm@32960
   479
            
wenzelm@32960
   480
            -- {*List property is maintained:*}
wenzelm@32960
   481
            from i1 t_notin_stack
wenzelm@32960
   482
            have puI1: "?puI1"
wenzelm@32960
   483
              by (simp add:addr_t_eq new_stack_eq, simp add:S_def)
wenzelm@32960
   484
            moreover
wenzelm@32960
   485
            
wenzelm@32960
   486
            -- {*Everything on the stack is marked:*}
wenzelm@32960
   487
            from i2
wenzelm@32960
   488
            have puI2: "?puI2" 
wenzelm@32960
   489
              by (simp add:new_stack_eq fun_upd_apply)
wenzelm@32960
   490
            moreover
wenzelm@32960
   491
            
wenzelm@32960
   492
            -- {*Everything is still reachable:*}
wenzelm@32960
   493
            let "R = reachable ?Ra ?A" = "?I3"
wenzelm@32960
   494
            let "R = reachable ?Rb ?B" = "?puI3"
wenzelm@32960
   495
            have "?Ra\<^sup>* `` addrs ?A = ?Rb\<^sup>* `` addrs ?B"
wenzelm@32960
   496
            proof (rule still_reachable_eq)
wenzelm@32960
   497
              show "addrs ?A \<subseteq> ?Rb\<^sup>* `` addrs ?B"
wenzelm@32960
   498
                by(fastsimp simp:addrs_def rel_defs addr_t_eq intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   499
            next
wenzelm@32960
   500
              show "addrs ?B \<subseteq> ?Ra\<^sup>* `` addrs ?A"
wenzelm@32960
   501
                by(fastsimp simp:addrs_def rel_defs addr_t_eq intro:oneStep_reachable Image_iff[THEN iffD2])
wenzelm@32960
   502
            next
wenzelm@32960
   503
              show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``addrs ?B)"
wenzelm@32960
   504
                by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def dest:rel_upd1)
wenzelm@32960
   505
            next
wenzelm@32960
   506
              show "\<forall>(x, y)\<in>?Rb-?Ra. y\<in>(?Ra\<^sup>*``addrs ?A)"
wenzelm@32960
   507
                by (clarsimp simp:relS_def) (fastsimp simp add:rel_def Image_iff addrs_def fun_upd_apply dest:rel_upd2)
wenzelm@32960
   508
            qed
wenzelm@32960
   509
            with i3
wenzelm@32960
   510
            have puI3: "?puI3" by (simp add:reachable_def) 
wenzelm@32960
   511
            moreover
wenzelm@32960
   512
            
wenzelm@32960
   513
            -- "If it is reachable and not marked, it is still reachable using..."
wenzelm@32960
   514
            let "\<forall>x. x \<in> R \<and> \<not> m x \<longrightarrow> x \<in> reachable ?Ra ?A" = ?I4
wenzelm@32960
   515
            let "\<forall>x. x \<in> R \<and> \<not> ?new_m x \<longrightarrow> x \<in> reachable ?Rb ?B" = ?puI4
wenzelm@32960
   516
            let ?T = "{t}"
wenzelm@32960
   517
            have "?Ra\<^sup>*``addrs ?A \<subseteq> ?Rb\<^sup>*``(addrs ?B \<union> addrs ?T)"
wenzelm@32960
   518
            proof (rule still_reachable)
wenzelm@32960
   519
              show "addrs ?A \<subseteq> ?Rb\<^sup>* `` (addrs ?B \<union> addrs ?T)"
wenzelm@32960
   520
                by (fastsimp simp:new_stack_eq addrs_def intro:self_reachable)
wenzelm@32960
   521
            next
wenzelm@32960
   522
              show "\<forall>(x, y)\<in>?Ra-?Rb. y\<in>(?Rb\<^sup>*``(addrs ?B \<union> addrs ?T))"
wenzelm@32960
   523
                by (clarsimp simp:relS_def new_stack_eq restr_un restr_upd) 
wenzelm@32960
   524
                   (fastsimp simp add:rel_def Image_iff restr_def addrs_def fun_upd_apply addr_t_eq dest:rel_upd3)
wenzelm@32960
   525
            qed
wenzelm@32960
   526
            then have subset: "?Ra\<^sup>*``addrs ?A - ?Rb\<^sup>*``addrs ?T \<subseteq> ?Rb\<^sup>*``addrs ?B"
wenzelm@32960
   527
              by blast
wenzelm@32960
   528
            have ?puI4
wenzelm@32960
   529
            proof (rule allI, rule impI)
wenzelm@32960
   530
              fix x
wenzelm@32960
   531
              assume a: "x \<in> R \<and> \<not> ?new_m x"
wenzelm@32960
   532
              have xDisj: "x=(addr t) \<or> x\<noteq>(addr t)" by simp
wenzelm@32960
   533
              with i4 a have inc: "x \<in> ?Ra\<^sup>*``addrs ?A"
wenzelm@32960
   534
                by (fastsimp simp:addr_t_eq addrs_def reachable_def intro:self_reachable)
wenzelm@32960
   535
              have exc: "x \<notin> ?Rb\<^sup>*`` addrs ?T"
wenzelm@32960
   536
                using xDisj a n_m_addr_t
wenzelm@32960
   537
                by (clarsimp simp add:addrs_def addr_t_eq) 
wenzelm@32960
   538
              from inc exc subset  show "x \<in> reachable ?Rb ?B" 
wenzelm@32960
   539
                by (auto simp add:reachable_def)
wenzelm@32960
   540
            qed  
wenzelm@32960
   541
            moreover
wenzelm@32960
   542
            
wenzelm@32960
   543
            -- "If it is marked, then it is reachable"
wenzelm@32960
   544
            from i5
wenzelm@32960
   545
            have "?puI5"
wenzelm@32960
   546
              by (auto simp:addrs_def i3 reachable_def addr_t_eq fun_upd_apply intro:self_reachable)
wenzelm@32960
   547
            moreover
wenzelm@32960
   548
            
wenzelm@32960
   549
            -- {*If it is not on the stack, then its @{term l} and @{term r} fields are unchanged*}
wenzelm@32960
   550
            from i6 
wenzelm@32960
   551
            have "?puI6"
wenzelm@32960
   552
              by (simp add:new_stack_eq)
wenzelm@32960
   553
            moreover
mehta@13820
   554
wenzelm@32960
   555
            -- {*If it is on the stack, then its @{term l} and @{term r} fields can be reconstructed*}
wenzelm@32960
   556
            from stackDist i6 t_notin_stack i7
wenzelm@32960
   557
            have "?puI7" by (clarsimp simp:addr_t_eq new_stack_eq)
mehta@13820
   558
wenzelm@32960
   559
            ultimately show ?thesis by auto
wenzelm@32960
   560
          qed
wenzelm@32960
   561
          then have "\<exists>stack. ?puInv stack" by blast
mehta@13820
   562
wenzelm@32960
   563
        }
wenzelm@32960
   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