| 
13875
 | 
     1  | 
(*  Title:      HOL/Hoare/Heap.thy
  | 
| 
 | 
     2  | 
    ID:         $Id$
  | 
| 
 | 
     3  | 
    Author:     Tobias Nipkow
  | 
| 
 | 
     4  | 
    Copyright   2002 TUM
  | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
Pointers, heaps and heap abstractions.
  | 
| 
 | 
     7  | 
See the paper by Mehta and Nipkow.
  | 
| 
 | 
     8  | 
*)
  | 
| 
 | 
     9  | 
  | 
| 
16417
 | 
    10  | 
theory Heap imports Main begin
  | 
| 
13875
 | 
    11  | 
  | 
| 
 | 
    12  | 
subsection "References"
  | 
| 
 | 
    13  | 
  | 
| 
 | 
    14  | 
datatype 'a ref = Null | Ref 'a
  | 
| 
 | 
    15  | 
  | 
| 
 | 
    16  | 
lemma not_Null_eq [iff]: "(x ~= Null) = (EX y. x = Ref y)"
  | 
| 
 | 
    17  | 
  by (induct x) auto
  | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
lemma not_Ref_eq [iff]: "(ALL y. x ~= Ref y) = (x = Null)"
  | 
| 
 | 
    20  | 
  by (induct x) auto
  | 
| 
 | 
    21  | 
  | 
| 
 | 
    22  | 
consts addr :: "'a ref \<Rightarrow> 'a"
  | 
| 
 | 
    23  | 
primrec "addr(Ref a) = a"
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
section "The heap"
  | 
| 
 | 
    27  | 
  | 
| 
 | 
    28  | 
subsection "Paths in the heap"
  | 
| 
 | 
    29  | 
  | 
| 
 | 
    30  | 
consts
  | 
| 
 | 
    31  | 
 Path :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a list \<Rightarrow> 'a ref \<Rightarrow> bool"
 | 
| 
 | 
    32  | 
primrec
  | 
| 
 | 
    33  | 
"Path h x [] y = (x = y)"
  | 
| 
 | 
    34  | 
"Path h x (a#as) y = (x = Ref a \<and> Path h (h a) as y)"
  | 
| 
 | 
    35  | 
  | 
| 
 | 
    36  | 
lemma [iff]: "Path h Null xs y = (xs = [] \<and> y = Null)"
  | 
| 
 | 
    37  | 
apply(case_tac xs)
  | 
| 
 | 
    38  | 
apply fastsimp
  | 
| 
 | 
    39  | 
apply fastsimp
  | 
| 
 | 
    40  | 
done
  | 
| 
 | 
    41  | 
  | 
| 
 | 
    42  | 
lemma [simp]: "Path h (Ref a) as z =
  | 
| 
 | 
    43  | 
 (as = [] \<and> z = Ref a  \<or>  (\<exists>bs. as = a#bs \<and> Path h (h a) bs z))"
  | 
| 
 | 
    44  | 
apply(case_tac as)
  | 
| 
 | 
    45  | 
apply fastsimp
  | 
| 
 | 
    46  | 
apply fastsimp
  | 
| 
 | 
    47  | 
done
  | 
| 
 | 
    48  | 
  | 
| 
 | 
    49  | 
lemma [simp]: "\<And>x. Path f x (as@bs) z = (\<exists>y. Path f x as y \<and> Path f y bs z)"
  | 
| 
 | 
    50  | 
by(induct as, simp+)
  | 
| 
 | 
    51  | 
  | 
| 
 | 
    52  | 
lemma Path_upd[simp]:
  | 
| 
 | 
    53  | 
 "\<And>x. u \<notin> set as \<Longrightarrow> Path (f(u := v)) x as y = Path f x as y"
  | 
| 
 | 
    54  | 
by(induct as, simp, simp add:eq_sym_conv)
  | 
| 
 | 
    55  | 
  | 
| 
 | 
    56  | 
lemma Path_snoc:
  | 
| 
 | 
    57  | 
 "Path (f(a := q)) p as (Ref a) \<Longrightarrow> Path (f(a := q)) p (as @ [a]) q"
  | 
| 
 | 
    58  | 
by simp
  | 
| 
 | 
    59  | 
  | 
| 
 | 
    60  | 
  | 
| 
19399
 | 
    61  | 
subsection "Non-repeating paths"
  | 
| 
 | 
    62  | 
  | 
| 
 | 
    63  | 
constdefs
  | 
| 
 | 
    64  | 
  distPath :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a list \<Rightarrow> 'a ref \<Rightarrow> bool"
 | 
| 
 | 
    65  | 
  "distPath h x as y   \<equiv>   Path h x as y  \<and>  distinct as"
  | 
| 
 | 
    66  | 
  | 
| 
 | 
    67  | 
text{* The term @{term"distPath h x as y"} expresses the fact that a
 | 
| 
 | 
    68  | 
non-repeating path @{term as} connects location @{term x} to location
 | 
| 
 | 
    69  | 
@{term y} by means of the @{term h} field. In the case where @{text "x
 | 
| 
 | 
    70  | 
= y"}, and there is a cycle from @{term x} to itself, @{term as} can
 | 
| 
 | 
    71  | 
be both @{term "[]"} and the non-repeating list of nodes in the
 | 
| 
 | 
    72  | 
cycle. *}
  | 
| 
 | 
    73  | 
  | 
| 
 | 
    74  | 
lemma neq_dP: "p \<noteq> q \<Longrightarrow> Path h p Ps q \<Longrightarrow> distinct Ps \<Longrightarrow>
  | 
| 
 | 
    75  | 
 EX a Qs. p = Ref a & Ps = a#Qs & a \<notin> set Qs"
  | 
| 
 | 
    76  | 
by (case_tac Ps, auto)
  | 
| 
 | 
    77  | 
  | 
| 
 | 
    78  | 
  | 
| 
 | 
    79  | 
lemma neq_dP_disp: "\<lbrakk> p \<noteq> q; distPath h p Ps q \<rbrakk> \<Longrightarrow>
  | 
| 
 | 
    80  | 
 EX a Qs. p = Ref a \<and> Ps = a#Qs \<and> a \<notin> set Qs"
  | 
| 
 | 
    81  | 
apply (simp only:distPath_def)
  | 
| 
 | 
    82  | 
by (case_tac Ps, auto)
  | 
| 
 | 
    83  | 
  | 
| 
 | 
    84  | 
  | 
| 
13875
 | 
    85  | 
subsection "Lists on the heap"
  | 
| 
 | 
    86  | 
  | 
| 
 | 
    87  | 
subsubsection "Relational abstraction"
  | 
| 
 | 
    88  | 
  | 
| 
 | 
    89  | 
constdefs
  | 
| 
 | 
    90  | 
 List :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a list \<Rightarrow> bool"
 | 
| 
 | 
    91  | 
"List h x as == Path h x as Null"
  | 
| 
 | 
    92  | 
  | 
| 
 | 
    93  | 
lemma [simp]: "List h x [] = (x = Null)"
  | 
| 
 | 
    94  | 
by(simp add:List_def)
  | 
| 
 | 
    95  | 
  | 
| 
 | 
    96  | 
lemma [simp]: "List h x (a#as) = (x = Ref a \<and> List h (h a) as)"
  | 
| 
 | 
    97  | 
by(simp add:List_def)
  | 
| 
 | 
    98  | 
  | 
| 
 | 
    99  | 
lemma [simp]: "List h Null as = (as = [])"
  | 
| 
 | 
   100  | 
by(case_tac as, simp_all)
  | 
| 
 | 
   101  | 
  | 
| 
 | 
   102  | 
lemma List_Ref[simp]: "List h (Ref a) as = (\<exists>bs. as = a#bs \<and> List h (h a) bs)"
  | 
| 
 | 
   103  | 
by(case_tac as, simp_all, fast)
  | 
| 
 | 
   104  | 
  | 
| 
 | 
   105  | 
theorem notin_List_update[simp]:
  | 
| 
 | 
   106  | 
 "\<And>x. a \<notin> set as \<Longrightarrow> List (h(a := y)) x as = List h x as"
  | 
| 
 | 
   107  | 
apply(induct as)
  | 
| 
 | 
   108  | 
apply simp
  | 
| 
 | 
   109  | 
apply(clarsimp simp add:fun_upd_apply)
  | 
| 
 | 
   110  | 
done
  | 
| 
 | 
   111  | 
  | 
| 
 | 
   112  | 
lemma List_unique: "\<And>x bs. List h x as \<Longrightarrow> List h x bs \<Longrightarrow> as = bs"
  | 
| 
 | 
   113  | 
by(induct as, simp, clarsimp)
  | 
| 
 | 
   114  | 
  | 
| 
 | 
   115  | 
lemma List_unique1: "List h p as \<Longrightarrow> \<exists>!as. List h p as"
  | 
| 
 | 
   116  | 
by(blast intro:List_unique)
  | 
| 
 | 
   117  | 
  | 
| 
 | 
   118  | 
lemma List_app: "\<And>x. List h x (as@bs) = (\<exists>y. Path h x as y \<and> List h y bs)"
  | 
| 
 | 
   119  | 
by(induct as, simp, clarsimp)
  | 
| 
 | 
   120  | 
  | 
| 
 | 
   121  | 
lemma List_hd_not_in_tl[simp]: "List h (h a) as \<Longrightarrow> a \<notin> set as"
  | 
| 
 | 
   122  | 
apply (clarsimp simp add:in_set_conv_decomp)
  | 
| 
 | 
   123  | 
apply(frule List_app[THEN iffD1])
  | 
| 
 | 
   124  | 
apply(fastsimp dest: List_unique)
  | 
| 
 | 
   125  | 
done
  | 
| 
 | 
   126  | 
  | 
| 
 | 
   127  | 
lemma List_distinct[simp]: "\<And>x. List h x as \<Longrightarrow> distinct as"
  | 
| 
 | 
   128  | 
apply(induct as, simp)
  | 
| 
 | 
   129  | 
apply(fastsimp dest:List_hd_not_in_tl)
  | 
| 
 | 
   130  | 
done
  | 
| 
 | 
   131  | 
  | 
| 
19399
 | 
   132  | 
lemma Path_is_List:
  | 
| 
 | 
   133  | 
  "\<lbrakk>Path h b Ps (Ref a); a \<notin> set Ps\<rbrakk> \<Longrightarrow> List (h(a := Null)) b (Ps @ [a])"
  | 
| 
20503
 | 
   134  | 
apply (induct Ps arbitrary: b)
  | 
| 
19399
 | 
   135  | 
apply (auto simp add:fun_upd_apply)
  | 
| 
 | 
   136  | 
done
  | 
| 
 | 
   137  | 
  | 
| 
 | 
   138  | 
  | 
| 
13875
 | 
   139  | 
subsection "Functional abstraction"
  | 
| 
 | 
   140  | 
  | 
| 
 | 
   141  | 
constdefs
  | 
| 
 | 
   142  | 
 islist :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> bool"
 | 
| 
 | 
   143  | 
"islist h p == \<exists>as. List h p as"
  | 
| 
 | 
   144  | 
 list :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a list"
 | 
| 
 | 
   145  | 
"list h p == SOME as. List h p as"
  | 
| 
 | 
   146  | 
  | 
| 
 | 
   147  | 
lemma List_conv_islist_list: "List h p as = (islist h p \<and> as = list h p)"
  | 
| 
 | 
   148  | 
apply(simp add:islist_def list_def)
  | 
| 
 | 
   149  | 
apply(rule iffI)
  | 
| 
 | 
   150  | 
apply(rule conjI)
  | 
| 
 | 
   151  | 
apply blast
  | 
| 
 | 
   152  | 
apply(subst some1_equality)
  | 
| 
 | 
   153  | 
  apply(erule List_unique1)
  | 
| 
 | 
   154  | 
 apply assumption
  | 
| 
 | 
   155  | 
apply(rule refl)
  | 
| 
 | 
   156  | 
apply simp
  | 
| 
 | 
   157  | 
apply(rule someI_ex)
  | 
| 
 | 
   158  | 
apply fast
  | 
| 
 | 
   159  | 
done
  | 
| 
 | 
   160  | 
  | 
| 
 | 
   161  | 
lemma [simp]: "islist h Null"
  | 
| 
 | 
   162  | 
by(simp add:islist_def)
  | 
| 
 | 
   163  | 
  | 
| 
 | 
   164  | 
lemma [simp]: "islist h (Ref a) = islist h (h a)"
  | 
| 
 | 
   165  | 
by(simp add:islist_def)
  | 
| 
 | 
   166  | 
  | 
| 
 | 
   167  | 
lemma [simp]: "list h Null = []"
  | 
| 
 | 
   168  | 
by(simp add:list_def)
  | 
| 
 | 
   169  | 
  | 
| 
 | 
   170  | 
lemma list_Ref_conv[simp]:
  | 
| 
 | 
   171  | 
 "islist h (h a) \<Longrightarrow> list h (Ref a) = a # list h (h a)"
  | 
| 
 | 
   172  | 
apply(insert List_Ref[of h])
  | 
| 
 | 
   173  | 
apply(fastsimp simp:List_conv_islist_list)
  | 
| 
 | 
   174  | 
done
  | 
| 
 | 
   175  | 
  | 
| 
 | 
   176  | 
lemma [simp]: "islist h (h a) \<Longrightarrow> a \<notin> set(list h (h a))"
  | 
| 
 | 
   177  | 
apply(insert List_hd_not_in_tl[of h])
  | 
| 
 | 
   178  | 
apply(simp add:List_conv_islist_list)
  | 
| 
 | 
   179  | 
done
  | 
| 
 | 
   180  | 
  | 
| 
 | 
   181  | 
lemma list_upd_conv[simp]:
  | 
| 
 | 
   182  | 
 "islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> list (h(y := q)) p = list h p"
  | 
| 
 | 
   183  | 
apply(drule notin_List_update[of _ _ h q p])
  | 
| 
 | 
   184  | 
apply(simp add:List_conv_islist_list)
  | 
| 
 | 
   185  | 
done
  | 
| 
 | 
   186  | 
  | 
| 
 | 
   187  | 
lemma islist_upd[simp]:
  | 
| 
 | 
   188  | 
 "islist h p \<Longrightarrow> y \<notin> set(list h p) \<Longrightarrow> islist (h(y := q)) p"
  | 
| 
 | 
   189  | 
apply(frule notin_List_update[of _ _ h q p])
  | 
| 
 | 
   190  | 
apply(simp add:List_conv_islist_list)
  | 
| 
 | 
   191  | 
done
  | 
| 
 | 
   192  | 
  | 
| 
 | 
   193  | 
end
  |