src/HOL/Data_Structures/Tree23_Set.thy
author nipkow
Mon, 11 Jan 2016 20:51:13 +0100
changeset 62130 90a3016a6c12
parent 61709 47f7263870a0
child 63411 e051eea34990
permissions -rw-r--r--
added AA_Map; tuned titles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     2
62130
90a3016a6c12 added AA_Map; tuned titles
nipkow
parents: 61709
diff changeset
     3
section \<open>2-3 Tree Implementation of Sets\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     5
theory Tree23_Set
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     6
imports
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     7
  Tree23
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     8
  Cmp
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     9
  Set_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    10
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    11
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    12
fun isin :: "'a::cmp tree23 \<Rightarrow> 'a \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    13
"isin Leaf x = False" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    14
"isin (Node2 l a r) x =
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    15
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    16
     LT \<Rightarrow> isin l x |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    17
     EQ \<Rightarrow> True |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    18
     GT \<Rightarrow> isin r x)" |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    19
"isin (Node3 l a m b r) x =
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    20
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    21
     LT \<Rightarrow> isin l x |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    22
     EQ \<Rightarrow> True |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    23
     GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    24
       (case cmp x b of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    25
          LT \<Rightarrow> isin m x |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    26
          EQ \<Rightarrow> True |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    27
          GT \<Rightarrow> isin r x))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    28
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    29
datatype 'a up\<^sub>i = T\<^sub>i "'a tree23" | Up\<^sub>i "'a tree23" 'a "'a tree23"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    30
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    31
fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree23" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    32
"tree\<^sub>i (T\<^sub>i t) = t" |
61709
nipkow
parents: 61678
diff changeset
    33
"tree\<^sub>i (Up\<^sub>i l a r) = Node2 l a r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    34
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    35
fun ins :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    36
"ins x Leaf = Up\<^sub>i Leaf x Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    37
"ins x (Node2 l a r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    38
   (case cmp x a of
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    39
      LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    40
        (case ins x l of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    41
           T\<^sub>i l' => T\<^sub>i (Node2 l' a r) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    42
           Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    43
      EQ \<Rightarrow> T\<^sub>i (Node2 l x r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    44
      GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    45
        (case ins x r of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    46
           T\<^sub>i r' => T\<^sub>i (Node2 l a r') |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    47
           Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    48
"ins x (Node3 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    49
   (case cmp x a of
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    50
      LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    51
        (case ins x l of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    52
           T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    53
           Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    54
      EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    55
      GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    56
        (case cmp x b of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    57
           GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    58
             (case ins x r of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    59
                T\<^sub>i r' => T\<^sub>i (Node3 l a m b r') |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    60
                Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    61
           EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    62
           LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    63
             (case ins x m of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    64
                T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    65
                Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    66
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    67
hide_const insert
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    68
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    69
definition insert :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    70
"insert x t = tree\<^sub>i(ins x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    71
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    72
datatype 'a up\<^sub>d = T\<^sub>d "'a tree23" | Up\<^sub>d "'a tree23"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    73
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    74
fun tree\<^sub>d :: "'a up\<^sub>d \<Rightarrow> 'a tree23" where
61709
nipkow
parents: 61678
diff changeset
    75
"tree\<^sub>d (T\<^sub>d t) = t" |
nipkow
parents: 61678
diff changeset
    76
"tree\<^sub>d (Up\<^sub>d t) = t"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    77
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    78
(* Variation: return None to signal no-change *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    79
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    80
fun node21 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    81
"node21 (T\<^sub>d t1) a t2 = T\<^sub>d(Node2 t1 a t2)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    82
"node21 (Up\<^sub>d t1) a (Node2 t2 b t3) = Up\<^sub>d(Node3 t1 a t2 b t3)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    83
"node21 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    84
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    85
fun node22 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    86
"node22 t1 a (T\<^sub>d t2) = T\<^sub>d(Node2 t1 a t2)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    87
"node22 (Node2 t1 b t2) a (Up\<^sub>d t3) = Up\<^sub>d(Node3 t1 b t2 a t3)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    88
"node22 (Node3 t1 b t2 c t3) a (Up\<^sub>d t4) = T\<^sub>d(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    89
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    90
fun node31 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    91
"node31 (T\<^sub>d t1) a t2 b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    92
"node31 (Up\<^sub>d t1) a (Node2 t2 b t3) c t4 = T\<^sub>d(Node2 (Node3 t1 a t2 b t3) c t4)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    93
"node31 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) d t5 = T\<^sub>d(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    94
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    95
fun node32 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    96
"node32 t1 a (T\<^sub>d t2) b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    97
"node32 t1 a (Up\<^sub>d t2) b (Node2 t3 c t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    98
"node32 t1 a (Up\<^sub>d t2) b (Node3 t3 c t4 d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    99
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   100
fun node33 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   101
"node33 l a m b (T\<^sub>d r) = T\<^sub>d(Node3 l a m b r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   102
"node33 t1 a (Node2 t2 b t3) c (Up\<^sub>d t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   103
"node33 t1 a (Node3 t2 b t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   104
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   105
fun del_min :: "'a tree23 \<Rightarrow> 'a * 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   106
"del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   107
"del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   108
"del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   109
"del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   110
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   111
fun del :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   112
"del x Leaf = T\<^sub>d Leaf" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   113
"del x (Node2 Leaf a Leaf) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   114
  (if x = a then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf a Leaf))" |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   115
"del x (Node3 Leaf a Leaf b Leaf) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   116
  T\<^sub>d(if x = a then Node2 Leaf b Leaf else
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   117
     if x = b then Node2 Leaf a Leaf
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   118
     else Node3 Leaf a Leaf b Leaf)" |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   119
"del x (Node2 l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   120
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   121
     LT \<Rightarrow> node21 (del x l) a r |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   122
     GT \<Rightarrow> node22 l a (del x r) |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   123
     EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   124
"del x (Node3 l a m b r) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   125
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   126
     LT \<Rightarrow> node31 (del x l) a m b r |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   127
     EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   128
     GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   129
       (case cmp x b of
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   130
          LT \<Rightarrow> node32 l a (del x m) b r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   131
          EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   132
          GT \<Rightarrow> node33 l a m b (del x r)))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   133
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   134
definition delete :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   135
"delete x t = tree\<^sub>d(del x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   136
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   137
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   138
subsection "Functional Correctness"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   139
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   140
subsubsection "Proofs for isin"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   141
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   142
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   143
by (induction t) (auto simp: elems_simps1 ball_Un)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   144
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   145
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   146
by (induction t) (auto simp: elems_simps2)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   147
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   148
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   149
subsubsection "Proofs for insert"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   150
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   151
lemma inorder_ins:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   152
  "sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   153
by(induction t) (auto simp: ins_list_simps split: up\<^sub>i.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   154
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   155
lemma inorder_insert:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   156
  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   157
by(simp add: insert_def inorder_ins)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   158
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   159
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   160
subsubsection "Proofs for delete"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   161
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   162
lemma inorder_node21: "height r > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   163
  inorder (tree\<^sub>d (node21 l' a r)) = inorder (tree\<^sub>d l') @ a # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   164
by(induct l' a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   165
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   166
lemma inorder_node22: "height l > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   167
  inorder (tree\<^sub>d (node22 l a r')) = inorder l @ a # inorder (tree\<^sub>d r')"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   168
by(induct l a r' rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   169
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   170
lemma inorder_node31: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   171
  inorder (tree\<^sub>d (node31 l' a m b r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   172
by(induct l' a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   173
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   174
lemma inorder_node32: "height r > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   175
  inorder (tree\<^sub>d (node32 l a m' b r)) = inorder l @ a # inorder (tree\<^sub>d m') @ b # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   176
by(induct l a m' b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   177
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   178
lemma inorder_node33: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   179
  inorder (tree\<^sub>d (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (tree\<^sub>d r')"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   180
by(induct l a m b r' rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   181
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   182
lemmas inorder_nodes = inorder_node21 inorder_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   183
  inorder_node31 inorder_node32 inorder_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   184
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   185
lemma del_minD:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   186
  "del_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   187
  x # inorder(tree\<^sub>d t') = inorder t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   188
by(induction t arbitrary: t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   189
  (auto simp: inorder_nodes split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   190
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   191
lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   192
  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   193
by(induction t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   194
  (auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   195
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   196
lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   197
  inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   198
by(simp add: delete_def inorder_del)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   199
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   200
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   201
subsection \<open>Balancedness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   202
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   203
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   204
subsubsection "Proofs for insert"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   205
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   206
text{* First a standard proof that @{const ins} preserves @{const bal}. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   207
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   208
instantiation up\<^sub>i :: (type)height
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   209
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   210
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   211
fun height_up\<^sub>i :: "'a up\<^sub>i \<Rightarrow> nat" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   212
"height (T\<^sub>i t) = height t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   213
"height (Up\<^sub>i l a r) = height l"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   214
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   215
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   216
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   217
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   218
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   219
lemma bal_ins: "bal t \<Longrightarrow> bal (tree\<^sub>i(ins a t)) \<and> height(ins a t) = height t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   220
by (induct t) (auto split: up\<^sub>i.split) (* 15 secs in 2015 *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   221
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   222
text{* Now an alternative proof (by Brian Huffman) that runs faster because
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   223
two properties (balance and height) are combined in one predicate. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   224
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   225
inductive full :: "nat \<Rightarrow> 'a tree23 \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   226
"full 0 Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   227
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   228
"\<lbrakk>full n l; full n m; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node3 l p m q r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   229
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   230
inductive_cases full_elims:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   231
  "full n Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   232
  "full n (Node2 l p r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   233
  "full n (Node3 l p m q r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   234
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   235
inductive_cases full_0_elim: "full 0 t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   236
inductive_cases full_Suc_elim: "full (Suc n) t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   237
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   238
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   239
  by (auto elim: full_0_elim intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   240
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   241
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   242
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   243
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   244
lemma full_Suc_Node2_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   245
  "full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   246
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   247
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   248
lemma full_Suc_Node3_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   249
  "full (Suc n) (Node3 l p m q r) \<longleftrightarrow> full n l \<and> full n m \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   250
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   251
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   252
lemma full_imp_height: "full n t \<Longrightarrow> height t = n"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   253
  by (induct set: full, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   254
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   255
lemma full_imp_bal: "full n t \<Longrightarrow> bal t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   256
  by (induct set: full, auto dest: full_imp_height)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   257
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   258
lemma bal_imp_full: "bal t \<Longrightarrow> full (height t) t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   259
  by (induct t, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   260
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   261
lemma bal_iff_full: "bal t \<longleftrightarrow> (\<exists>n. full n t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   262
  by (auto elim!: bal_imp_full full_imp_bal)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   263
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   264
text {* The @{const "insert"} function either preserves the height of the
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   265
tree, or increases it by one. The constructor returned by the @{term
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   266
"insert"} function determines which: A return value of the form @{term
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   267
"T\<^sub>i t"} indicates that the height will be the same. A value of the
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   268
form @{term "Up\<^sub>i l p r"} indicates an increase in height. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   269
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   270
fun full\<^sub>i :: "nat \<Rightarrow> 'a up\<^sub>i \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   271
"full\<^sub>i n (T\<^sub>i t) \<longleftrightarrow> full n t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   272
"full\<^sub>i n (Up\<^sub>i l p r) \<longleftrightarrow> full n l \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   273
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   274
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   275
by (induct rule: full.induct) (auto split: up\<^sub>i.split)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   276
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   277
text {* The @{const insert} operation preserves balance. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   278
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   279
lemma bal_insert: "bal t \<Longrightarrow> bal (insert a t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   280
unfolding bal_iff_full insert_def
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   281
apply (erule exE)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   282
apply (drule full\<^sub>i_ins [of _ _ a])
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   283
apply (cases "ins a t")
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   284
apply (auto intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   285
done
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   286
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   287
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   288
subsection "Proofs for delete"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   289
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   290
instantiation up\<^sub>d :: (type)height
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   291
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   292
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   293
fun height_up\<^sub>d :: "'a up\<^sub>d \<Rightarrow> nat" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   294
"height (T\<^sub>d t) = height t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   295
"height (Up\<^sub>d t) = height t + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   296
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   297
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   298
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   299
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   300
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   301
lemma bal_tree\<^sub>d_node21:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   302
  "\<lbrakk>bal r; bal (tree\<^sub>d l'); height r = height l' \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node21 l' a r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   303
by(induct l' a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   304
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   305
lemma bal_tree\<^sub>d_node22:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   306
  "\<lbrakk>bal(tree\<^sub>d r'); bal l; height r' = height l \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node22 l a r'))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   307
by(induct l a r' rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   308
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   309
lemma bal_tree\<^sub>d_node31:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   310
  "\<lbrakk> bal (tree\<^sub>d l'); bal m; bal r; height l' = height r; height m = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   311
  \<Longrightarrow> bal (tree\<^sub>d (node31 l' a m b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   312
by(induct l' a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   313
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   314
lemma bal_tree\<^sub>d_node32:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   315
  "\<lbrakk> bal l; bal (tree\<^sub>d m'); bal r; height l = height r; height m' = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   316
  \<Longrightarrow> bal (tree\<^sub>d (node32 l a m' b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   317
by(induct l a m' b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   318
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   319
lemma bal_tree\<^sub>d_node33:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   320
  "\<lbrakk> bal l; bal m; bal(tree\<^sub>d r'); height l = height r'; height m = height r' \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   321
  \<Longrightarrow> bal (tree\<^sub>d (node33 l a m b r'))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   322
by(induct l a m b r' rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   323
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   324
lemmas bals = bal_tree\<^sub>d_node21 bal_tree\<^sub>d_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   325
  bal_tree\<^sub>d_node31 bal_tree\<^sub>d_node32 bal_tree\<^sub>d_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   326
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   327
lemma height'_node21:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   328
   "height r > 0 \<Longrightarrow> height(node21 l' a r) = max (height l') (height r) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   329
by(induct l' a r rule: node21.induct)(simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   330
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   331
lemma height'_node22:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   332
   "height l > 0 \<Longrightarrow> height(node22 l a r') = max (height l) (height r') + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   333
by(induct l a r' rule: node22.induct)(simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   334
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   335
lemma height'_node31:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   336
  "height m > 0 \<Longrightarrow> height(node31 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   337
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   338
by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   339
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   340
lemma height'_node32:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   341
  "height r > 0 \<Longrightarrow> height(node32 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   342
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   343
by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   344
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   345
lemma height'_node33:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   346
  "height m > 0 \<Longrightarrow> height(node33 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   347
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   348
by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   349
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   350
lemmas heights = height'_node21 height'_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   351
  height'_node31 height'_node32 height'_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   352
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   353
lemma height_del_min:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   354
  "del_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> bal t \<Longrightarrow> height t' = height t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   355
by(induct t arbitrary: x t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   356
  (auto simp: heights split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   357
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   358
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   359
by(induction x t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   360
  (auto simp: heights max_def height_del_min split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   361
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   362
lemma bal_del_min:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   363
  "\<lbrakk> del_min t = (x, t'); bal t; height t > 0 \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d t')"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   364
by(induct t arbitrary: x t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   365
  (auto simp: heights height_del_min bals split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   366
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   367
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   368
by(induction x t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   369
  (auto simp: bals bal_del_min height_del height_del_min split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   370
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   371
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   372
by(simp add: delete_def bal_tree\<^sub>d_del)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   373
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   374
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   375
subsection \<open>Overall Correctness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   376
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   377
interpretation Set_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   378
where empty = Leaf and isin = isin and insert = insert and delete = delete
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   379
and inorder = inorder and inv = bal
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   380
proof (standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   381
  case 2 thus ?case by(simp add: isin_set)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   382
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   383
  case 3 thus ?case by(simp add: inorder_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   384
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   385
  case 4 thus ?case by(simp add: inorder_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   386
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   387
  case 6 thus ?case by(simp add: bal_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   388
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   389
  case 7 thus ?case by(simp add: bal_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   390
qed simp+
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   391
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   392
end