| 14074 |      1 | (*  Title:      HOL/Hoare/Heap.thy
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Tobias Nipkow
 | 
|  |      4 |     Copyright   2002 TUM
 | 
|  |      5 | 
 | 
|  |      6 | Heap abstractions (at the moment only Path and List)
 | 
|  |      7 | for Separation Logic.
 | 
|  |      8 | *)
 | 
|  |      9 | 
 | 
| 18576 |     10 | theory SepLogHeap
 | 
|  |     11 | imports Main
 | 
|  |     12 | begin
 | 
| 18447 |     13 | 
 | 
| 14074 |     14 | types heap = "(nat \<Rightarrow> nat option)"
 | 
|  |     15 | 
 | 
| 16972 |     16 | text{* @{text "Some"} means allocated, @{text "None"} means
 | 
|  |     17 | free. Address @{text "0"} serves as the null reference. *}
 | 
| 14074 |     18 | 
 | 
|  |     19 | subsection "Paths in the heap"
 | 
|  |     20 | 
 | 
|  |     21 | consts
 | 
|  |     22 |  Path :: "heap \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> nat \<Rightarrow> bool"
 | 
|  |     23 | primrec
 | 
|  |     24 | "Path h x [] y = (x = y)"
 | 
|  |     25 | "Path h x (a#as) y = (x\<noteq>0 \<and> a=x \<and> (\<exists>b. h x = Some b \<and> Path h b as y))"
 | 
|  |     26 | 
 | 
|  |     27 | lemma [iff]: "Path h 0 xs y = (xs = [] \<and> y = 0)"
 | 
| 16972 |     28 | by (cases xs) simp_all
 | 
| 14074 |     29 | 
 | 
|  |     30 | lemma [simp]: "x\<noteq>0 \<Longrightarrow> Path h x as z =
 | 
|  |     31 |  (as = [] \<and> z = x  \<or>  (\<exists>y bs. as = x#bs \<and> h x = Some y & Path h y bs z))"
 | 
| 16972 |     32 | by (cases as) auto
 | 
| 14074 |     33 | 
 | 
|  |     34 | lemma [simp]: "\<And>x. Path f x (as@bs) z = (\<exists>y. Path f x as y \<and> Path f y bs z)"
 | 
| 16972 |     35 | by (induct as) auto
 | 
| 14074 |     36 | 
 | 
|  |     37 | lemma Path_upd[simp]:
 | 
|  |     38 |  "\<And>x. u \<notin> set as \<Longrightarrow> Path (f(u := v)) x as y = Path f x as y"
 | 
| 16972 |     39 | by (induct as) simp_all
 | 
| 14074 |     40 | 
 | 
|  |     41 | 
 | 
|  |     42 | subsection "Lists on the heap"
 | 
|  |     43 | 
 | 
|  |     44 | constdefs
 | 
|  |     45 |  List :: "heap \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> bool"
 | 
|  |     46 | "List h x as == Path h x as 0"
 | 
|  |     47 | 
 | 
|  |     48 | lemma [simp]: "List h x [] = (x = 0)"
 | 
| 16972 |     49 | by (simp add: List_def)
 | 
| 14074 |     50 | 
 | 
|  |     51 | lemma [simp]:
 | 
|  |     52 |  "List h x (a#as) = (x\<noteq>0 \<and> a=x \<and> (\<exists>y. h x = Some y \<and> List h y as))"
 | 
| 16972 |     53 | by (simp add: List_def)
 | 
| 14074 |     54 | 
 | 
|  |     55 | lemma [simp]: "List h 0 as = (as = [])"
 | 
| 16972 |     56 | by (cases as) simp_all
 | 
| 14074 |     57 | 
 | 
|  |     58 | lemma List_non_null: "a\<noteq>0 \<Longrightarrow>
 | 
|  |     59 |  List h a as = (\<exists>b bs. as = a#bs \<and> h a = Some b \<and> List h b bs)"
 | 
| 16972 |     60 | by (cases as) simp_all
 | 
| 14074 |     61 | 
 | 
|  |     62 | theorem notin_List_update[simp]:
 | 
|  |     63 |  "\<And>x. a \<notin> set as \<Longrightarrow> List (h(a := y)) x as = List h x as"
 | 
| 16972 |     64 | by (induct as) simp_all
 | 
| 14074 |     65 | 
 | 
|  |     66 | lemma List_unique: "\<And>x bs. List h x as \<Longrightarrow> List h x bs \<Longrightarrow> as = bs"
 | 
| 16972 |     67 | by (induct as) (auto simp add:List_non_null)
 | 
| 14074 |     68 | 
 | 
|  |     69 | lemma List_unique1: "List h p as \<Longrightarrow> \<exists>!as. List h p as"
 | 
| 16972 |     70 | by (blast intro: List_unique)
 | 
| 14074 |     71 | 
 | 
|  |     72 | lemma List_app: "\<And>x. List h x (as@bs) = (\<exists>y. Path h x as y \<and> List h y bs)"
 | 
| 16972 |     73 | by (induct as) auto
 | 
| 14074 |     74 | 
 | 
|  |     75 | lemma List_hd_not_in_tl[simp]: "List h b as \<Longrightarrow> h a = Some b \<Longrightarrow> a \<notin> set as"
 | 
|  |     76 | apply (clarsimp simp add:in_set_conv_decomp)
 | 
|  |     77 | apply(frule List_app[THEN iffD1])
 | 
|  |     78 | apply(fastsimp dest: List_unique)
 | 
|  |     79 | done
 | 
|  |     80 | 
 | 
|  |     81 | lemma List_distinct[simp]: "\<And>x. List h x as \<Longrightarrow> distinct as"
 | 
| 16972 |     82 | by (induct as) (auto dest:List_hd_not_in_tl)
 | 
| 14074 |     83 | 
 | 
|  |     84 | lemma list_in_heap: "\<And>p. List h p ps \<Longrightarrow> set ps \<subseteq> dom h"
 | 
| 16972 |     85 | by (induct ps) auto
 | 
| 14074 |     86 | 
 | 
|  |     87 | lemma list_ortho_sum1[simp]:
 | 
|  |     88 |  "\<And>p. \<lbrakk> List h1 p ps; dom h1 \<inter> dom h2 = {}\<rbrakk> \<Longrightarrow> List (h1++h2) p ps"
 | 
| 16972 |     89 | by (induct ps) (auto simp add:map_add_def split:option.split)
 | 
| 14074 |     90 | 
 | 
| 18447 |     91 | 
 | 
| 14074 |     92 | lemma list_ortho_sum2[simp]:
 | 
|  |     93 |  "\<And>p. \<lbrakk> List h2 p ps; dom h1 \<inter> dom h2 = {}\<rbrakk> \<Longrightarrow> List (h1++h2) p ps"
 | 
| 16972 |     94 | by (induct ps) (auto simp add:map_add_def split:option.split)
 | 
| 14074 |     95 | 
 | 
|  |     96 | end
 |