src/HOL/Imperative_HOL/Relational.thy
author haftmann
Fri, 09 Jul 2010 10:08:10 +0200
changeset 37756 59caa6180fff
parent 37755 7086b7feaaa5
child 37758 bf86a65403a8
permissions -rw-r--r--
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     1
theory Relational 
28744
9257bb7bcd2d removed Assert.thy
haftmann
parents: 28145
diff changeset
     2
imports Array Ref
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     3
begin
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     4
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     5
section{* Definition of the Relational framework *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     6
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     7
text {* The crel predicate states that when a computation c runs with the heap h
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     8
  will result in return value r and a heap h' (if no exception occurs). *}  
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
     9
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    10
definition crel :: "'a Heap \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    11
where
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
    12
  crel_def': "crel c h h' r \<longleftrightarrow> Heap_Monad.execute c h = Some (r, h')"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    13
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    14
lemma crel_def: -- FIXME
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
    15
  "crel c h h' r \<longleftrightarrow> Some (r, h') = Heap_Monad.execute c h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    16
  unfolding crel_def' by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    17
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    18
lemma crel_deterministic: "\<lbrakk> crel f h h' a; crel f h h'' b \<rbrakk> \<Longrightarrow> (a = b) \<and> (h' = h'')"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    19
unfolding crel_def' by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    20
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    21
section {* Elimination rules *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    22
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    23
text {* For all commands, we define simple elimination rules. *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    24
(* FIXME: consumes 1 necessary ?? *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    25
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    26
lemma crel_heap:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    27
  assumes "crel (Heap_Monad.heap f) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    28
  obtains "h' = snd (f h)" "r = fst (f h)"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    29
  using assms by (cases "f h") (simp add: crel_def)
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    30
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    31
lemma crel_guard:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    32
  assumes "crel (Heap_Monad.guard P f) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    33
  obtains "h' = snd (f h)" "r = fst (f h)" "P h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    34
  using assms by (cases "f h", cases "P h") (simp_all add: crel_def)
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    35
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
    36
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    37
subsection {* Elimination rules for basic monadic commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    38
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    39
lemma crelE[consumes 1]:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    40
  assumes "crel (f >>= g) h h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    41
  obtains h' r where "crel f h h' r" "crel (g r) h' h'' r'"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    42
  using assms by (auto simp add: crel_def bind_def split: option.split_asm)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    43
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    44
lemma crelE'[consumes 1]:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    45
  assumes "crel (f >> g) h h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    46
  obtains h' r where "crel f h h' r" "crel g h' h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    47
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    48
  by (elim crelE) auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    49
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    50
lemma crel_return[consumes 1]:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    51
  assumes "crel (return x) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    52
  obtains "r = x" "h = h'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    53
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    54
  unfolding crel_def return_def by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    55
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    56
lemma crel_raise[consumes 1]:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    57
  assumes "crel (raise x) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    58
  obtains "False"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    59
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    60
  unfolding crel_def raise_def by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    61
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    62
lemma crel_if:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    63
  assumes "crel (if c then t else e) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    64
  obtains "c" "crel t h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    65
        | "\<not>c" "crel e h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    66
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    67
  unfolding crel_def by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    68
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    69
lemma crel_option_case:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    70
  assumes "crel (case x of None \<Rightarrow> n | Some y \<Rightarrow> s y) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    71
  obtains "x = None" "crel n h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    72
        | y where "x = Some y" "crel (s y) h h' r" 
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    73
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    74
  unfolding crel_def by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    75
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    76
lemma crel_fold_map:
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    77
  assumes "crel (Heap_Monad.fold_map f xs) h h' r"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    78
  assumes "\<And>h h'. P f [] h h' []"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    79
  assumes "\<And>h h1 h' x xs y ys. \<lbrakk> crel (f x) h h1 y; crel (Heap_Monad.fold_map f xs) h1 h' ys; P f xs h1 h' ys \<rbrakk> \<Longrightarrow> P f (x#xs) h h' (y#ys)"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    80
  shows "P f xs h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    81
using assms(1)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    82
proof (induct xs arbitrary: h h' r)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    83
  case Nil  with assms(2) show ?case
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    84
    by (auto elim: crel_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    85
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    86
  case (Cons x xs)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    87
  from Cons(2) obtain h1 y ys where crel_f: "crel (f x) h h1 y"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    88
    and crel_fold_map: "crel (Heap_Monad.fold_map f xs) h1 h' ys"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    89
    and r_def: "r = y#ys"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    90
    unfolding fold_map.simps
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    91
    by (auto elim!: crelE crel_return)
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
    92
  from Cons(1)[OF crel_fold_map] crel_fold_map crel_f assms(3) r_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    93
  show ?case by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    94
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    95
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    96
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    97
subsection {* Elimination rules for array commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    98
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
    99
(* Strong version of the lemma for this operation is missing *) 
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   100
lemma crel_new_weak:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   101
  assumes "crel (Array.new n v) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   102
  obtains "get_array r h' = List.replicate n v"
37716
24bb91462892 remove primitive operation Heap.array in favour of Heap.array_of_list
haftmann
parents: 37709
diff changeset
   103
  using assms unfolding Array.new_def
24bb91462892 remove primitive operation Heap.array in favour of Heap.array_of_list
haftmann
parents: 37709
diff changeset
   104
  by (elim crel_heap) (auto simp: array_def Let_def split_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   105
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   106
(* Strong version of the lemma for this operation is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   107
lemma crel_of_list_weak:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   108
  assumes "crel (Array.of_list xs) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   109
  obtains "get_array r h' = xs"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   110
  using assms unfolding of_list_def 
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   111
  by (elim crel_heap) (simp add: get_array_init_array_list)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   112
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   113
(* Strong version of the lemma for this operation is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   114
lemma crel_make_weak:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   115
  assumes "crel (Array.make n f) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   116
  obtains "i < n \<Longrightarrow> get_array r h' ! i = f i"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   117
  using assms unfolding Array.make_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   118
  by (elim crel_heap) (auto simp: array_def Let_def split_def)
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   119
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   120
lemma crel_length:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   121
  assumes "crel (len a) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   122
  obtains "h = h'" "r = Array.length a h'"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   123
  using assms unfolding Array.len_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   124
  by (elim crel_heap) simp
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   125
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   126
lemma crel_nth[consumes 1]:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   127
  assumes "crel (nth a i) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   128
  obtains "r = get_array a h ! i" "h = h'" "i < Array.length a h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   129
  using assms unfolding nth_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   130
  by (elim crel_guard) (clarify, simp)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   131
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   132
lemma crel_upd[consumes 1]:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   133
  assumes "crel (upd i v a) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   134
  obtains "r = a" "h' = Array.change a i v h" "i < Array.length a h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   135
  using assms unfolding upd_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   136
  by (elim crel_guard) simp
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   137
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   138
lemma crel_map_entry:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   139
  assumes "crel (Array.map_entry i f a) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   140
  obtains "r = a" "h' = Array.change a i (f (get_array a h ! i)) h" "i < Array.length a h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   141
  using assms unfolding Array.map_entry_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   142
  by (elim crel_guard) simp
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   143
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   144
lemma crel_swap:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   145
  assumes "crel (Array.swap i x a) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   146
  obtains "r = get_array a h ! i" "h' = Array.change a i x h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   147
  using assms unfolding Array.swap_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   148
  by (elim crel_guard) simp
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   149
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   150
lemma upt_conv_Cons': (*FIXME move*)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   151
  assumes "Suc a \<le> b"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   152
  shows "[b - Suc a..<b] = (b - Suc a)#[b - a..<b]"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   153
proof -
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   154
  from assms have l: "b - Suc a < b" by arith
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   155
  from assms have "Suc (b - Suc a) = b - a" by arith
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   156
  with l show ?thesis by (simp add: upt_conv_Cons)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   157
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   158
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   159
lemma crel_fold_map_nth:
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   160
  assumes
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   161
  "crel (Heap_Monad.fold_map (Array.nth a) [Array.length a h - n..<Array.length a h]) h h' xs"
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   162
  assumes "n \<le> Array.length a h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   163
  shows "h = h' \<and> xs = drop (Array.length a h - n) (get_array a h)"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   164
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   165
proof (induct n arbitrary: xs h h')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   166
  case 0 thus ?case
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   167
    by (auto elim!: crel_return simp add: Array.length_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   168
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   169
  case (Suc n)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   170
  from Suc(3) have "[Array.length a h - Suc n..<Array.length a h] = (Array.length a h - Suc n)#[Array.length a h - n..<Array.length a h]"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   171
    by (simp add: upt_conv_Cons')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   172
  with Suc(2) obtain r where
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   173
    crel_fold_map: "crel (Heap_Monad.fold_map (Array.nth a) [Array.length a h - n..<Array.length a h]) h h' r"
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   174
    and xs_def: "xs = get_array a h ! (Array.length a h - Suc n) # r"
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   175
    by (auto elim!: crelE crel_nth crel_return)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   176
  from Suc(3) have "Array.length a h - n = Suc (Array.length a h - Suc n)" 
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   177
    by arith
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   178
  with Suc.hyps[OF crel_fold_map] xs_def show ?case
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   179
    unfolding Array.length_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   180
    by (auto simp add: nth_drop')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   181
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   182
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   183
lemma crel_freeze:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   184
  assumes "crel (Array.freeze a) h h' xs"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   185
  obtains "h = h'" "xs = get_array a h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   186
  using assms unfolding freeze_def
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   187
  by (elim crel_heap) simp
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   188
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   189
lemma crel_fold_map_map_entry_remains:
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   190
  assumes "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) h h' r"
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   191
  assumes "i < Array.length a h - n"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   192
  shows "get_array a h ! i = get_array a h' ! i"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   193
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   194
proof (induct n arbitrary: h h' r)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   195
  case 0
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   196
  thus ?case
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   197
    by (auto elim: crel_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   198
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   199
  case (Suc n)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   200
  let ?h1 = "Array.change a (Array.length a h - Suc n) (f (get_array a h ! (Array.length a h - Suc n))) h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   201
  from Suc(3) have "[Array.length a h - Suc n..<Array.length a h] = (Array.length a h - Suc n)#[Array.length a h - n..<Array.length a h]"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   202
    by (simp add: upt_conv_Cons')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   203
  from Suc(2) this obtain r where
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   204
    crel_fold_map: "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) ?h1 h' r"
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   205
    by (auto simp add: elim!: crelE crel_map_entry crel_return)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   206
  have length_remains: "Array.length a ?h1 = Array.length a h" by simp
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   207
  from crel_fold_map have crel_fold_map': "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a ?h1 - n..<Array.length a ?h1]) ?h1 h' r"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   208
    by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   209
  from Suc(1)[OF this] length_remains Suc(3) show ?case by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   210
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   211
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   212
lemma crel_fold_map_map_entry_changes:
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   213
  assumes "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) h h' r"
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   214
  assumes "n \<le> Array.length a h"  
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   215
  assumes "i \<ge> Array.length a h - n"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   216
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   217
  shows "get_array a h' ! i = f (get_array a h ! i)"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   218
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   219
proof (induct n arbitrary: h h' r)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   220
  case 0
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   221
  thus ?case
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   222
    by (auto elim!: crel_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   223
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   224
  case (Suc n)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   225
  let ?h1 = "Array.change a (Array.length a h - Suc n) (f (get_array a h ! (Array.length a h - Suc n))) h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   226
  from Suc(3) have "[Array.length a h - Suc n..<Array.length a h] = (Array.length a h - Suc n)#[Array.length a h - n..<Array.length a h]"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   227
    by (simp add: upt_conv_Cons')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   228
  from Suc(2) this obtain r where
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   229
    crel_fold_map: "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) ?h1 h' r"
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   230
    by (auto simp add: elim!: crelE crel_map_entry crel_return)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   231
  have length_remains: "Array.length a ?h1 = Array.length a h" by simp
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   232
  from Suc(3) have less: "Array.length a h - Suc n < Array.length a h - n" by arith
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   233
  from Suc(3) have less2: "Array.length a h - Suc n < Array.length a h" by arith
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   234
  from Suc(4) length_remains have cases: "i = Array.length a ?h1 - Suc n \<or> i \<ge> Array.length a ?h1 - n" by arith
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   235
  from crel_fold_map have crel_fold_map': "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a ?h1 - n..<Array.length a ?h1]) ?h1 h' r"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   236
    by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   237
  from Suc(1)[OF this] cases Suc(3) Suc(5) length_remains
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   238
    crel_fold_map_map_entry_remains[OF this, of "Array.length a h - Suc n", symmetric] less less2
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   239
  show ?case
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   240
    by (auto simp add: nth_list_update_eq Array.length_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   241
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   242
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   243
lemma crel_fold_map_map_entry_length:
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   244
  assumes "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) h h' r"
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   245
  assumes "n \<le> Array.length a h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   246
  shows "Array.length a h' = Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   247
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   248
proof (induct n arbitrary: h h' r)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   249
  case 0
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   250
  thus ?case by (auto elim!: crel_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   251
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   252
  case (Suc n)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   253
  let ?h1 = "Array.change a (Array.length a h - Suc n) (f (get_array a h ! (Array.length a h - Suc n))) h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   254
  from Suc(3) have "[Array.length a h - Suc n..<Array.length a h] = (Array.length a h - Suc n)#[Array.length a h - n..<Array.length a h]"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   255
    by (simp add: upt_conv_Cons')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   256
  from Suc(2) this obtain r where 
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   257
    crel_fold_map: "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) ?h1 h' r"
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   258
    by (auto elim!: crelE crel_map_entry crel_return)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   259
  have length_remains: "Array.length a ?h1 = Array.length a h" by simp
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   260
  from crel_fold_map have crel_fold_map': "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a ?h1 - n..<Array.length a ?h1]) ?h1 h' r"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   261
    by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   262
  from Suc(1)[OF this] length_remains Suc(3) show ?case by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   263
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   264
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   265
lemma crel_fold_map_map_entry:
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   266
assumes "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [0..<Array.length a h]) h h' r"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   267
  shows "get_array a h' = List.map f (get_array a h)"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   268
proof -
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   269
  from assms have "crel (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - Array.length a h..<Array.length a h]) h h' r" by simp
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   270
  from crel_fold_map_map_entry_length[OF this]
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   271
  crel_fold_map_map_entry_changes[OF this] show ?thesis
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   272
    unfolding Array.length_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   273
    by (auto intro: nth_equalityI)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   274
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   275
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   276
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   277
subsection {* Elimination rules for reference commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   278
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   279
(* TODO:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   280
maybe introduce a new predicate "extends h' h x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   281
which means h' extends h with a new reference x.
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   282
Then crel_new: would be
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   283
assumes "crel (Ref.new v) h h' x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   284
obtains "get_ref x h' = v"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   285
and "extends h' h x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   286
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   287
and we would need further rules for extends:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   288
extends h' h x \<Longrightarrow> \<not> ref_present x h
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   289
extends h' h x \<Longrightarrow>  ref_present x h'
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   290
extends h' h x \<Longrightarrow> ref_present y h \<Longrightarrow> ref_present y h'
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   291
extends h' h x \<Longrightarrow> ref_present y h \<Longrightarrow> get_ref y h = get_ref y h'
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   292
extends h' h x \<Longrightarrow> lim h' = Suc (lim h)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   293
*)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   294
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   295
lemma crel_ref:
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   296
  assumes "crel (ref v) h h' x"
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   297
  obtains "Ref.get h' x = v"
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   298
  and "\<not> Ref.present h x"
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   299
  and "Ref.present h' x"
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   300
  and "\<forall>y. Ref.present h y \<longrightarrow> Ref.get h y = Ref.get h' y"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   301
 (* and "lim h' = Suc (lim h)" *)
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   302
  and "\<forall>y. Ref.present h y \<longrightarrow> Ref.present h' y"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   303
  using assms
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   304
  unfolding Ref.ref_def
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   305
  apply (elim crel_heap)
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   306
  unfolding Ref.alloc_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   307
  apply (simp add: Let_def)
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   308
  unfolding Ref.present_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   309
  apply auto
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   310
  unfolding Ref.get_def Ref.set_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   311
  apply auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   312
  done
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   313
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   314
lemma crel_lookup:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   315
  assumes "crel (!r') h h' r"
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   316
  obtains "h = h'" "r = Ref.get h r'"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   317
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   318
unfolding Ref.lookup_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   319
by (auto elim: crel_heap)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   320
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   321
lemma crel_update:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   322
  assumes "crel (r' := v) h h' r"
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   323
  obtains "h' = Ref.set r' v h" "r = ()"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   324
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   325
unfolding Ref.update_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   326
by (auto elim: crel_heap)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   327
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   328
lemma crel_change:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   329
  assumes "crel (Ref.change f r') h h' r"
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   330
  obtains "h' = Ref.set r' (f (Ref.get h r')) h" "r = f (Ref.get h r')"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   331
using assms
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   332
unfolding Ref.change_def Let_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   333
by (auto elim!: crelE crel_lookup crel_update crel_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   334
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   335
subsection {* Elimination rules for the assert command *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   336
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   337
lemma crel_assert[consumes 1]:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   338
  assumes "crel (assert P x) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   339
  obtains "P x" "r = x" "h = h'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   340
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   341
  unfolding assert_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   342
  by (elim crel_if crel_return crel_raise) auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   343
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   344
lemma crel_assert_eq: "(\<And>h h' r. crel f h h' r \<Longrightarrow> P r) \<Longrightarrow> f \<guillemotright>= assert P = f"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   345
unfolding crel_def bind_def Let_def assert_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   346
  raise_def return_def prod_case_beta
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   347
apply (cases f)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   348
apply simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   349
apply (simp add: expand_fun_eq split_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   350
apply (auto split: option.split)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   351
apply (erule_tac x="x" in meta_allE)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   352
apply auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   353
done
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   354
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   355
section {* Introduction rules *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   356
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   357
subsection {* Introduction rules for basic monadic commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   358
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   359
lemma crelI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   360
  assumes "crel f h h' r" "crel (g r) h' h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   361
  shows "crel (f >>= g) h h'' r'"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   362
  using assms by (simp add: crel_def' bind_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   363
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   364
lemma crelI':
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   365
  assumes "crel f h h' r" "crel g h' h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   366
  shows "crel (f >> g) h h'' r'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   367
  using assms by (intro crelI) auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   368
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   369
lemma crel_returnI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   370
  shows "crel (return x) h h x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   371
  unfolding crel_def return_def by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   372
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   373
lemma crel_raiseI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   374
  shows "\<not> (crel (raise x) h h' r)"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   375
  unfolding crel_def raise_def by simp
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   376
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   377
lemma crel_ifI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   378
  assumes "c \<longrightarrow> crel t h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   379
  "\<not>c \<longrightarrow> crel e h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   380
  shows "crel (if c then t else e) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   381
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   382
  unfolding crel_def by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   383
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   384
lemma crel_option_caseI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   385
  assumes "\<And>y. x = Some y \<Longrightarrow> crel (s y) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   386
  assumes "x = None \<Longrightarrow> crel n h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   387
  shows "crel (case x of None \<Rightarrow> n | Some y \<Rightarrow> s y) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   388
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   389
by (auto split: option.split)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   390
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   391
lemma crel_heapI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   392
  shows "crel (Heap_Monad.heap f) h (snd (f h)) (fst (f h))"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   393
  by (simp add: crel_def apfst_def split_def prod_fun_def)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   394
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   395
lemma crel_heapI': (*FIXME keep only this version*)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   396
  assumes "h' = snd (f h)" "r = fst (f h)"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   397
  shows "crel (Heap_Monad.heap f) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   398
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   399
  by (simp add: crel_def split_def apfst_def prod_fun_def)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   400
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   401
lemma crel_guardI:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   402
  assumes "P h" "h' = snd (f h)" "r = fst (f h)"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   403
  shows "crel (Heap_Monad.guard P f) h h' r"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   404
  using assms by (simp add: crel_def)
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   405
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   406
lemma crelI2:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   407
  assumes "\<exists>h' rs'. crel f h h' rs' \<and> (\<exists>h'' rs. crel (g rs') h' h'' rs)"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   408
  shows "\<exists>h'' rs. crel (f\<guillemotright>= g) h h'' rs"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   409
  oops
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   410
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   411
lemma crel_ifI2:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   412
  assumes "c \<Longrightarrow> \<exists>h' r. crel t h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   413
  "\<not> c \<Longrightarrow> \<exists>h' r. crel e h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   414
  shows "\<exists> h' r. crel (if c then t else e) h h' r"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   415
  oops
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   416
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   417
subsection {* Introduction rules for array commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   418
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   419
lemma crel_lengthI:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   420
  shows "crel (Array.len a) h h (Array.length a h)"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   421
  unfolding len_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   422
  by (rule crel_heapI') auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   423
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   424
(* thm crel_newI for Array.new is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   425
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   426
lemma crel_nthI:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   427
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   428
  shows "crel (nth a i) h h ((get_array a h) ! i)"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   429
  using assms unfolding nth_def by (auto intro: crel_guardI)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   430
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   431
lemma crel_updI:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   432
  assumes "i < Array.length a h"
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   433
  shows "crel (upd i v a) h (Array.change a i v h) a"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   434
  using assms unfolding upd_def by (auto intro: crel_guardI)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   435
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   436
(* thm crel_of_listI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   437
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   438
(* thm crel_map_entryI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   439
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   440
(* thm crel_swapI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   441
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   442
(* thm crel_makeI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   443
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   444
(* thm crel_freezeI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   445
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   446
(* thm crel_mapI is missing *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   447
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   448
subsubsection {* Introduction rules for reference commands *}
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   449
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   450
lemma crel_lookupI:
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   451
  shows "crel (!r) h h (Ref.get h r)"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   452
  unfolding lookup_def by (auto intro!: crel_heapI')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   453
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   454
lemma crel_updateI:
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   455
  shows "crel (r := v) h (Ref.set r v h) ()"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   456
  unfolding update_def by (auto intro!: crel_heapI')
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   457
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   458
lemma crel_changeI: 
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   459
  shows "crel (Ref.change f r) h (Ref.set r (f (Ref.get h r)) h) (f (Ref.get h r))"
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   460
unfolding change_def Let_def by (auto intro!: crelI crel_returnI crel_lookupI crel_updateI)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   461
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   462
subsubsection {* Introduction rules for the assert command *}
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   463
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   464
lemma crel_assertI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   465
  assumes "P x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   466
  shows "crel (assert P x) h h x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   467
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   468
  unfolding assert_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   469
  by (auto intro!: crel_ifI crel_returnI crel_raiseI)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   470
 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   471
subsection {* Induction rule for the MREC combinator *}
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   472
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   473
lemma MREC_induct:
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   474
  assumes "crel (MREC f g x) h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   475
  assumes "\<And> x h h' r. crel (f x) h h' (Inl r) \<Longrightarrow> P x h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   476
  assumes "\<And> x h h1 h2 h' s z r. crel (f x) h h1 (Inr s) \<Longrightarrow> crel (MREC f g s) h1 h2 z \<Longrightarrow> P s h1 h2 z
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   477
    \<Longrightarrow> crel (g x s z) h2 h' r \<Longrightarrow> P x h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   478
  shows "P x h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   479
proof (rule MREC_pinduct[OF assms(1)[unfolded crel_def, symmetric]])
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   480
  fix x h h1 h2 h' s z r
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   481
  assume "Heap_Monad.execute (f x) h = Some (Inr s, h1)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   482
    "Heap_Monad.execute (MREC f g s) h1 = Some (z, h2)"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   483
    "P s h1 h2 z"
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   484
    "Heap_Monad.execute (g x s z) h2 = Some (r, h')"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   485
  from assms(3)[unfolded crel_def, OF this(1)[symmetric] this(2)[symmetric] this(3) this(4)[symmetric]]
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   486
  show "P x h h' r" .
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   487
next
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   488
qed (auto simp add: assms(2)[unfolded crel_def])
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   489
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 29399
diff changeset
   490
section {* Definition of the noError predicate *}
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   491
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   492
text {* We add a simple definitional setting for crel intro rules
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   493
  where we only would like to show that the computation does not result in a exception for heap h,
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   494
  but we do not care about statements about the resulting heap and return value.*}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   495
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   496
definition noError :: "'a Heap \<Rightarrow> heap \<Rightarrow> bool"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   497
where
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   498
  "noError c h \<longleftrightarrow> (\<exists>r h'. Some (r, h') = Heap_Monad.execute c h)"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   499
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   500
lemma noError_def': -- FIXME
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   501
  "noError c h \<longleftrightarrow> (\<exists>r h'. Heap_Monad.execute c h = Some (r, h'))"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   502
  unfolding noError_def apply auto proof -
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   503
  fix r h'
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   504
  assume "Some (r, h') = Heap_Monad.execute c h"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   505
  then have "Heap_Monad.execute c h = Some (r, h')" ..
70fafefbcc98 simplified representation of monad type
haftmann
parents: 36057
diff changeset
   506
  then show "\<exists>r h'. Heap_Monad.execute c h = Some (r, h')" by blast
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   507
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   508
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   509
subsection {* Introduction rules for basic monadic commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   510
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   511
lemma noErrorI:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   512
  assumes "noError f h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   513
  assumes "\<And>h' r. crel f h h' r \<Longrightarrow> noError (g r) h'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   514
  shows "noError (f \<guillemotright>= g) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   515
  using assms
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   516
  by (auto simp add: noError_def' crel_def' bind_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   517
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   518
lemma noErrorI':
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   519
  assumes "noError f h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   520
  assumes "\<And>h' r. crel f h h' r \<Longrightarrow> noError g h'"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   521
  shows "noError (f \<guillemotright> g) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   522
  using assms
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   523
  by (auto simp add: noError_def' crel_def' bind_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   524
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   525
lemma noErrorI2:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   526
"\<lbrakk>crel f h h' r ; noError f h; noError (g r) h'\<rbrakk>
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   527
\<Longrightarrow> noError (f \<guillemotright>= g) h"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   528
by (auto simp add: noError_def' crel_def' bind_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   529
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   530
lemma noError_return: 
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   531
  shows "noError (return x) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   532
  unfolding noError_def return_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   533
  by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   534
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   535
lemma noError_if:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   536
  assumes "c \<Longrightarrow> noError t h" "\<not> c \<Longrightarrow> noError e h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   537
  shows "noError (if c then t else e) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   538
  using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   539
  unfolding noError_def
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   540
  by auto
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   541
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   542
lemma noError_option_case:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   543
  assumes "\<And>y. x = Some y \<Longrightarrow> noError (s y) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   544
  assumes "noError n h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   545
  shows "noError (case x of None \<Rightarrow> n | Some y \<Rightarrow> s y) h"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   546
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   547
by (auto split: option.split)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   548
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   549
lemma noError_fold_map: 
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   550
assumes "\<forall>x \<in> set xs. noError (f x) h \<and> crel (f x) h h (r x)" 
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   551
shows "noError (Heap_Monad.fold_map f xs) h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   552
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   553
proof (induct xs)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   554
  case Nil
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   555
  thus ?case
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   556
    unfolding fold_map.simps by (intro noError_return)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   557
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   558
  case (Cons x xs)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   559
  thus ?case
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   560
    unfolding fold_map.simps
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   561
    by (auto intro: noErrorI2[of "f x"] noErrorI noError_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   562
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   563
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   564
lemma noError_heap [simp]:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   565
  "noError (Heap_Monad.heap f) h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   566
  by (simp add: noError_def')
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   567
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   568
lemma noError_guard [simp]:
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   569
  "P h \<Longrightarrow> noError (Heap_Monad.guard P f) h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   570
  by (simp add: noError_def')
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   571
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   572
subsection {* Introduction rules for array commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   573
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   574
lemma noError_length:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   575
  shows "noError (Array.len a) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   576
  by (simp add: len_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   577
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   578
lemma noError_new:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   579
  shows "noError (Array.new n v) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   580
  by (simp add: Array.new_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   581
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   582
lemma noError_upd:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   583
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   584
  shows "noError (Array.upd i v a) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   585
  using assms by (simp add: upd_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   586
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   587
lemma noError_nth:
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   588
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   589
  shows "noError (Array.nth a i) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   590
  using assms by (simp add: nth_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   591
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   592
lemma noError_of_list:
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   593
  "noError (of_list ls) h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   594
  by (simp add: of_list_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   595
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   596
lemma noError_map_entry:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   597
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   598
  shows "noError (map_entry i f a) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   599
  using assms by (simp add: map_entry_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   600
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   601
lemma noError_swap:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   602
  assumes "i < Array.length a h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   603
  shows "noError (swap i x a) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   604
  using assms by (simp add: swap_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   605
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   606
lemma noError_make:
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   607
  "noError (make n f) h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   608
  by (simp add: make_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   609
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   610
lemma noError_freeze:
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   611
  "noError (freeze a) h"
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   612
  by (simp add: freeze_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   613
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   614
lemma noError_fold_map_map_entry:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   615
  assumes "n \<le> Array.length a h"
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37755
diff changeset
   616
  shows "noError (Heap_Monad.fold_map (\<lambda>n. map_entry n f a) [Array.length a h - n..<Array.length a h]) h"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   617
using assms
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   618
proof (induct n arbitrary: h)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   619
  case 0
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   620
  thus ?case by (auto intro: noError_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   621
next
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   622
  case (Suc n)
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   623
  from Suc.prems have "[Array.length a h - Suc n..<Array.length a h] = (Array.length a h - Suc n)#[Array.length a h - n..<Array.length a h]"
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   624
    by (simp add: upt_conv_Cons')
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   625
  with Suc.hyps[of "(Array.change a (Array.length a h - Suc n) (f (get_array a h ! (Array.length a h - Suc n))) h)"] Suc.prems show ?case
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 27656
diff changeset
   626
    by (auto simp add: intro!: noErrorI noError_return noError_map_entry elim: crel_map_entry)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   627
qed
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   628
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   629
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   630
subsection {* Introduction rules for the reference commands *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   631
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   632
lemma noError_Ref_new:
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   633
  shows "noError (ref v) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   634
  by (simp add: Ref.ref_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   635
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   636
lemma noError_lookup:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   637
  shows "noError (!r) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   638
  by (simp add: lookup_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   639
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   640
lemma noError_update:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   641
  shows "noError (r := v) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   642
  by (simp add: update_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   643
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   644
lemma noError_change:
37719
271ecd4fb9f9 moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents: 37718
diff changeset
   645
  shows "noError (Ref.change f r) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   646
  by (simp add: change_def)
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   647
    (auto intro!: noErrorI2 noError_lookup noError_update noError_return crel_lookupI crel_updateI simp add: Let_def)
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   648
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   649
subsection {* Introduction rules for the assert command *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   650
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   651
lemma noError_assert:
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   652
  assumes "P x"
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   653
  shows "noError (assert P x) h"
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   654
  using assms unfolding assert_def
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   655
  by (auto intro: noError_if noError_return)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   657
section {* Cumulative lemmas *}
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   658
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   659
lemmas crel_elim_all =
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   660
  crelE crelE' crel_return crel_raise crel_if crel_option_case
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   661
  crel_length crel_new_weak crel_nth crel_upd crel_of_list_weak crel_map_entry crel_swap crel_make_weak crel_freeze
37725
6d28a2aea936 refactored reference operations
haftmann
parents: 37719
diff changeset
   662
  crel_ref crel_lookup crel_update crel_change
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   663
  crel_assert
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   664
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   665
lemmas crel_intro_all =
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   666
  crelI crelI' crel_returnI crel_raiseI crel_ifI crel_option_caseI
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   667
  crel_lengthI (* crel_newI *) crel_nthI crel_updI (* crel_of_listI crel_map_entryI crel_swapI crel_makeI crel_freezeI crel_mapI *)
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   668
  (* crel_Ref_newI *) crel_lookupI crel_updateI crel_changeI
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   669
  crel_assert
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   670
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   671
lemmas noError_intro_all = 
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   672
  noErrorI noErrorI' noError_return noError_if noError_option_case
37755
7086b7feaaa5 adapted to changes
haftmann
parents: 37725
diff changeset
   673
  noError_length noError_new noError_nth noError_upd noError_of_list noError_map_entry noError_swap noError_make noError_freeze
27656
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   674
  noError_Ref_new noError_lookup noError_update noError_change
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   675
  noError_assert
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   676
d4f6e64ee7cc added verification framework for the HeapMonad and quicksort as example for this framework
bulwahn
parents:
diff changeset
   677
end