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