src/HOL/Hoare/Pointers0.thy
author haftmann
Tue, 23 Feb 2010 10:11:15 +0100
changeset 35316 870dfea4f9c0
parent 35101 6ce9177d6b38
child 35416 d8d7d1b785af
permissions -rw-r--r--
dropped axclass; dropped Id; session theory Hoare.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     1
(*  Title:      HOL/Hoare/Pointers.thy
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     2
    Author:     Tobias Nipkow
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     3
    Copyright   2002 TUM
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     4
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     5
This is like Pointers.thy, but instead of a type constructor 'a ref
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     6
that adjoins Null to a type, Null is simply a distinguished element of
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     7
the address type. This avoids the Ref constructor and thus simplifies
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     8
specifications (a bit). However, the proofs don't seem to get simpler
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
     9
- in fact in some case they appear to get (a bit) more complicated.
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    10
*)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    11
35316
870dfea4f9c0 dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents: 35101
diff changeset
    12
theory Pointers0 imports Hoare_Logic begin
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    13
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    14
subsection "References"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    15
35316
870dfea4f9c0 dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents: 35101
diff changeset
    16
class ref =
870dfea4f9c0 dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents: 35101
diff changeset
    17
  fixes Null :: 'a
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    18
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    19
subsection "Field access and update"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    20
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    21
syntax
35101
6ce9177d6b38 modernized translations;
wenzelm
parents: 17778
diff changeset
    22
  "_fassign"  :: "'a::ref => id => 'v => 's com"
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    23
   ("(2_^._ :=/ _)" [70,1000,65] 61)
35101
6ce9177d6b38 modernized translations;
wenzelm
parents: 17778
diff changeset
    24
  "_faccess"  :: "'a::ref => ('a::ref \<Rightarrow> 'v) => 'v"
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    25
   ("_^._" [65,1000] 65)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    26
translations
35101
6ce9177d6b38 modernized translations;
wenzelm
parents: 17778
diff changeset
    27
  "p^.f := e"  =>  "f := CONST fun_upd f p e"
13771
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    28
  "p^.f"       =>  "f p"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    29
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    30
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    31
text "An example due to Suzuki:"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    32
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    33
lemma "VARS v n
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    34
  {distinct[w,x,y,z]}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    35
  w^.v := (1::int); w^.n := x;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    36
  x^.v := 2; x^.n := y;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    37
  y^.v := 3; y^.n := z;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    38
  z^.v := 4; x^.n := z
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    39
  {w^.n^.n^.v = 4}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    40
by vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    41
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    42
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    43
section "The heap"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    44
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    45
subsection "Paths in the heap"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    46
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    47
consts
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    48
 Path :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> bool"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    49
primrec
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    50
"Path h x [] y = (x = y)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    51
"Path h x (a#as) y = (x \<noteq> Null \<and> x = a \<and> Path h (h a) as y)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    52
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    53
lemma [iff]: "Path h Null xs y = (xs = [] \<and> y = Null)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    54
apply(case_tac xs)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    55
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    56
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    57
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    58
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    59
lemma [simp]: "a \<noteq> Null \<Longrightarrow> Path h a as z =
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    60
 (as = [] \<and> z = a  \<or>  (\<exists>bs. as = a#bs \<and> Path h (h a) bs z))"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    61
apply(case_tac as)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    62
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    63
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    64
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    65
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    66
lemma [simp]: "\<And>x. Path f x (as@bs) z = (\<exists>y. Path f x as y \<and> Path f y bs z)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    67
by(induct as, simp+)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    68
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    69
lemma [simp]: "\<And>x. u \<notin> set as \<Longrightarrow> Path (f(u := v)) x as y = Path f x as y"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    70
by(induct as, simp, simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    71
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    72
subsection "Lists on the heap"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    73
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    74
subsubsection "Relational abstraction"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    75
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    76
constdefs
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    77
 List :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> bool"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    78
"List h x as == Path h x as Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    79
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    80
lemma [simp]: "List h x [] = (x = Null)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    81
by(simp add:List_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    82
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    83
lemma [simp]: "List h x (a#as) = (x \<noteq> Null \<and> x = a \<and> List h (h a) as)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    84
by(simp add:List_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    85
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    86
lemma [simp]: "List h Null as = (as = [])"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    87
by(case_tac as, simp_all)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    88
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    89
lemma List_Ref[simp]:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    90
 "a \<noteq> Null \<Longrightarrow> List h a as = (\<exists>bs. as = a#bs \<and> List h (h a) bs)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    91
by(case_tac as, simp_all, fast)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    92
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    93
theorem notin_List_update[simp]:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    94
 "\<And>x. a \<notin> set as \<Longrightarrow> List (h(a := y)) x as = List h x as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    95
apply(induct as)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    96
apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    97
apply(clarsimp simp add:fun_upd_apply)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    98
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
    99
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   100
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   101
declare fun_upd_apply[simp del]fun_upd_same[simp] fun_upd_other[simp]
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   102
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   103
lemma List_unique: "\<And>x bs. List h x as \<Longrightarrow> List h x bs \<Longrightarrow> as = bs"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   104
by(induct as, simp, clarsimp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   105
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   106
lemma List_unique1: "List h p as \<Longrightarrow> \<exists>!as. List h p as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   107
by(blast intro:List_unique)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   108
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   109
lemma List_app: "\<And>x. List h x (as@bs) = (\<exists>y. Path h x as y \<and> List h y bs)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   110
by(induct as, simp, clarsimp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   111
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   112
lemma List_hd_not_in_tl[simp]: "List h (h a) as \<Longrightarrow> a \<notin> set as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   113
apply (clarsimp simp add:in_set_conv_decomp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   114
apply(frule List_app[THEN iffD1])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   115
apply(fastsimp dest: List_unique)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   116
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   117
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   118
lemma List_distinct[simp]: "\<And>x. List h x as \<Longrightarrow> distinct as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   119
apply(induct as, simp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   120
apply(fastsimp dest:List_hd_not_in_tl)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   121
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   122
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   123
subsection "Functional abstraction"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   124
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   125
constdefs
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   126
 islist :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> bool"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   127
"islist h p == \<exists>as. List h p as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   128
 list :: "('a::ref \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a list"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   129
"list h p == SOME as. List h p as"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   130
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   131
lemma List_conv_islist_list: "List h p as = (islist h p \<and> as = list h p)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   132
apply(simp add:islist_def list_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   133
apply(rule iffI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   134
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   135
apply blast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   136
apply(subst some1_equality)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   137
  apply(erule List_unique1)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   138
 apply assumption
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   139
apply(rule refl)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   140
apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   141
apply(rule someI_ex)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   142
apply fast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   143
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   144
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   145
lemma [simp]: "islist h Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   146
by(simp add:islist_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   147
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   148
lemma [simp]: "a \<noteq> Null \<Longrightarrow> islist h a = islist h (h a)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   149
by(simp add:islist_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   150
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   151
lemma [simp]: "list h Null = []"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   152
by(simp add:list_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   153
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   154
lemma list_Ref_conv[simp]:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   155
 "\<lbrakk> a \<noteq> Null; islist h (h a) \<rbrakk> \<Longrightarrow> list h a = a # list h (h a)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   156
apply(insert List_Ref[of _ h])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   157
apply(fastsimp simp:List_conv_islist_list)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   158
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   159
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   160
lemma [simp]: "islist h (h a) \<Longrightarrow> a \<notin> set(list h (h a))"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   161
apply(insert List_hd_not_in_tl[of h])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   162
apply(simp add:List_conv_islist_list)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   163
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   164
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   165
lemma list_upd_conv[simp]:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   166
 "islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> list (h(y := q)) p = list h p"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   167
apply(drule notin_List_update[of _ _ h q p])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   168
apply(simp add:List_conv_islist_list)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   169
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   170
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   171
lemma islist_upd[simp]:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   172
 "islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> islist (h(y := q)) p"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   173
apply(frule notin_List_update[of _ _ h q p])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   174
apply(simp add:List_conv_islist_list)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   175
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   176
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   177
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   178
section "Verifications"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   179
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   180
subsection "List reversal"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   181
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   182
text "A short but unreadable proof:"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   183
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   184
lemma "VARS tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   185
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   186
  WHILE p \<noteq> Null
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   187
  INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   188
                 rev ps @ qs = rev Ps @ Qs}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   189
  DO r := p; p := p^.tl; r^.tl := q; q := r OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   190
  {List tl q (rev Ps @ Qs)}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   191
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   192
  apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   193
 apply(fastsimp intro:notin_List_update[THEN iffD2])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   194
(* explicily:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   195
 apply clarify
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   196
 apply(rename_tac ps qs)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   197
 apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   198
 apply(rename_tac ps')
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   199
 apply(rule_tac x = ps' in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   200
 apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   201
 apply(rule_tac x = "p#qs" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   202
 apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   203
*)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   204
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   205
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   206
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   207
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   208
text "A longer readable version:"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   209
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   210
lemma "VARS tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   211
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   212
  WHILE p \<noteq> Null
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   213
  INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   214
               rev ps @ qs = rev Ps @ Qs}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   215
  DO r := p; p := p^.tl; r^.tl := q; q := r OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   216
  {List tl q (rev Ps @ Qs)}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   217
proof vcg
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   218
  fix tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   219
  assume "List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   220
  thus "\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   221
                rev ps @ qs = rev Ps @ Qs" by fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   222
next
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   223
  fix tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   224
  assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   225
                   rev ps @ qs = rev Ps @ Qs) \<and> p \<noteq> Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   226
         (is "(\<exists>ps qs. ?I ps qs) \<and> _")
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   227
  then obtain ps qs where I: "?I ps qs \<and> p \<noteq> Null" by fast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   228
  then obtain ps' where "ps = p # ps'" by fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   229
  hence "List (tl(p := q)) (p^.tl) ps' \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   230
         List (tl(p := q)) p       (p#qs) \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   231
         set ps' \<inter> set (p#qs) = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   232
         rev ps' @ (p#qs) = rev Ps @ Qs"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   233
    using I by fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   234
  thus "\<exists>ps' qs'. List (tl(p := q)) (p^.tl) ps' \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   235
                  List (tl(p := q)) p       qs' \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   236
                  set ps' \<inter> set qs' = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   237
                  rev ps' @ qs' = rev Ps @ Qs" by fast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   238
next
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   239
  fix tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   240
  assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   241
                   rev ps @ qs = rev Ps @ Qs) \<and> \<not> p \<noteq> Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   242
  thus "List tl q (rev Ps @ Qs)" by fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   243
qed
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   244
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   245
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   246
text{* Finaly, the functional version. A bit more verbose, but automatic! *}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   247
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   248
lemma "VARS tl p q r
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   249
  {islist tl p \<and> islist tl q \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   250
   Ps = list tl p \<and> Qs = list tl q \<and> set Ps \<inter> set Qs = {}}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   251
  WHILE p \<noteq> Null
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   252
  INV {islist tl p \<and> islist tl q \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   253
       set(list tl p) \<inter> set(list tl q) = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   254
       rev(list tl p) @ (list tl q) = rev Ps @ Qs}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   255
  DO r := p; p := p^.tl; r^.tl := q; q := r OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   256
  {islist tl q \<and> list tl q = rev Ps @ Qs}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   257
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   258
  apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   259
 apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   260
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   261
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   262
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   263
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   264
subsection "Searching in a list"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   265
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   266
text{*What follows is a sequence of successively more intelligent proofs that
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   267
a simple loop finds an element in a linked list.
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   268
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   269
We start with a proof based on the @{term List} predicate. This means it only
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   270
works for acyclic lists. *}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   271
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   272
lemma "VARS tl p
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   273
  {List tl p Ps \<and> X \<in> set Ps}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   274
  WHILE p \<noteq> Null \<and> p \<noteq> X
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   275
  INV {p \<noteq> Null \<and> (\<exists>ps. List tl p ps \<and> X \<in> set ps)}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   276
  DO p := p^.tl OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   277
  {p = X}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   278
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   279
  apply(case_tac "p = Null")
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   280
   apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   281
  apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   282
 apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   283
 apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   284
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   285
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   286
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   287
text{*Using @{term Path} instead of @{term List} generalizes the correctness
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   288
statement to cyclic lists as well: *}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   289
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   290
lemma "VARS tl p
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   291
  {Path tl p Ps X}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   292
  WHILE p \<noteq> Null \<and> p \<noteq> X
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   293
  INV {\<exists>ps. Path tl p ps X}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   294
  DO p := p^.tl OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   295
  {p = X}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   296
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   297
  apply blast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   298
 apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   299
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   300
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   301
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   302
text{*Now it dawns on us that we do not need the list witness at all --- it
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   303
suffices to talk about reachability, i.e.\ we can use relations directly. *}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   304
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   305
lemma "VARS tl p
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   306
  {(p,X) \<in> {(x,y). y = tl x & x \<noteq> Null}^*}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   307
  WHILE p \<noteq> Null \<and> p \<noteq> X
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   308
  INV {(p,X) \<in> {(x,y). y = tl x & x \<noteq> Null}^*}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   309
  DO p := p^.tl OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   310
  {p = X}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   311
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   312
 apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   313
 apply(erule converse_rtranclE)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   314
  apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   315
 apply(simp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   316
apply(fastsimp elim:converse_rtranclE)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   317
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   318
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   319
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   320
subsection "Merging two lists"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   321
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   322
text"This is still a bit rough, especially the proof."
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   323
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   324
consts merge :: "'a list * 'a list * ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   325
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   326
recdef merge "measure(%(xs,ys,f). size xs + size ys)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   327
"merge(x#xs,y#ys,f) = (if f x y then x # merge(xs,y#ys,f)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   328
                                else y # merge(x#xs,ys,f))"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   329
"merge(x#xs,[],f) = x # merge(xs,[],f)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   330
"merge([],y#ys,f) = y # merge([],ys,f)"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   331
"merge([],[],f) = []"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   332
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   333
lemma imp_disjCL: "(P|Q \<longrightarrow> R) = ((P \<longrightarrow> R) \<and> (~P \<longrightarrow> Q \<longrightarrow> R))"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   334
by blast
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   335
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   336
declare disj_not1[simp del] imp_disjL[simp del] imp_disjCL[simp]
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   337
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   338
lemma "VARS hd tl p q r s
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   339
 {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {} \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   340
  (p \<noteq> Null \<or> q \<noteq> Null)}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   341
 IF if q = Null then True else p ~= Null & p^.hd \<le> q^.hd
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   342
 THEN r := p; p := p^.tl ELSE r := q; q := q^.tl FI;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   343
 s := r;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   344
 WHILE p \<noteq> Null \<or> q \<noteq> Null
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   345
 INV {EX rs ps qs. Path tl r rs s \<and> List tl p ps \<and> List tl q qs \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   346
      distinct(s # ps @ qs @ rs) \<and> s \<noteq> Null \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   347
      merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y) =
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   348
      rs @ s # merge(ps,qs,\<lambda>x y. hd x \<le> hd y) \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   349
      (tl s = p \<or> tl s = q)}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   350
 DO IF if q = Null then True else p \<noteq> Null \<and> p^.hd \<le> q^.hd
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   351
    THEN s^.tl := p; p := p^.tl ELSE s^.tl := q; q := q^.tl FI;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   352
    s := s^.tl
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   353
 OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   354
 {List tl r (merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y))}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   355
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   356
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   357
apply (fastsimp)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   358
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   359
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   360
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   361
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   362
apply(simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   363
apply(rule_tac x = "rs @ [s]" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   364
apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   365
apply(rule_tac x = "bs" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   366
apply (fastsimp simp:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   367
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   368
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   369
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   370
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   371
apply(rule_tac x = "rs @ [s]" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   372
apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   373
apply(rule_tac x = "bsa" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   374
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   375
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   376
apply(rule exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   377
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   378
apply(rule_tac x = bs in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   379
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   380
apply(rule refl)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   381
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   382
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   383
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   384
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   385
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   386
apply(rule_tac x = "rs @ [s]" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   387
apply simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   388
apply(rule_tac x = bs in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   389
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   390
apply clarsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   391
apply(rule_tac x = "rs @ [s]" in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   392
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   393
apply(rule exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   394
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   395
apply(rule_tac x = bsa in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   396
apply(rule conjI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   397
apply(rule refl)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   398
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   399
apply(rule_tac x = bs in exI)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   400
apply (simp add:eq_sym_conv)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   401
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   402
apply(clarsimp simp add:List_app)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   403
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   404
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   405
(* TODO: merging with islist/list instead of List: an improvement?
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   406
   needs (is)path, which is not so easy to prove either. *)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   407
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   408
subsection "Storage allocation"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   409
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   410
constdefs new :: "'a set \<Rightarrow> 'a::ref"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   411
"new A == SOME a. a \<notin> A & a \<noteq> Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   412
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   413
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   414
lemma new_notin:
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   415
 "\<lbrakk> ~finite(UNIV::('a::ref)set); finite(A::'a set); B \<subseteq> A \<rbrakk> \<Longrightarrow>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   416
  new (A) \<notin> B & new A \<noteq> Null"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   417
apply(unfold new_def)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   418
apply(rule someI2_ex)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   419
 apply (fast dest:ex_new_if_finite[of "insert Null A"])
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   420
apply (fast)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   421
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   422
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   423
lemma "~finite(UNIV::('a::ref)set) \<Longrightarrow>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   424
  VARS xs elem next alloc p q
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   425
  {Xs = xs \<and> p = (Null::'a)}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   426
  WHILE xs \<noteq> []
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   427
  INV {islist next p \<and> set(list next p) \<subseteq> set alloc \<and>
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   428
       map elem (rev(list next p)) @ xs = Xs}
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   429
  DO q := new(set alloc); alloc := q#alloc;
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   430
     q^.next := p; q^.elem := hd xs; xs := tl xs; p := q
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   431
  OD
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   432
  {islist next p \<and> map elem (rev(list next p)) = Xs}"
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   433
apply vcg_simp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   434
 apply (clarsimp simp: subset_insert_iff neq_Nil_conv fun_upd_apply new_notin)
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   435
apply fastsimp
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   436
done
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   437
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   438
6cd59cc885a1 *** empty log message ***
nipkow
parents:
diff changeset
   439
end