src/HOL/Data_Structures/Tree234_Set.thy
author nipkow
Mon, 16 Nov 2015 15:59:47 +0100
changeset 61688 d04b1b4fb015
parent 61640 44c9198f210c
child 61703 1f1354ca7ea7
permissions -rw-r--r--
corrected inefficient implementation
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
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     3
section \<open>A 2-3-4 Tree Implementation of Sets\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     5
theory Tree234_Set
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     6
imports
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     7
  Tree234
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     8
  Cmp
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     9
  "../Data_Structures/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
subsection \<open>Set operations on 2-3-4 trees\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    13
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    14
fun isin :: "'a::cmp tree234 \<Rightarrow> 'a \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    15
"isin Leaf x = False" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    16
"isin (Node2 l a r) x =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    17
  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    18
"isin (Node3 l a m b r) x =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    19
  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> (case cmp x b of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    20
   LT \<Rightarrow> isin m x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    21
"isin (Node4 t1 a t2 b t3 c t4) x = (case cmp x b of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    22
  LT \<Rightarrow> (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    23
          LT \<Rightarrow> isin t1 x |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    24
          EQ \<Rightarrow> True |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    25
          GT \<Rightarrow> isin t2 x) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    26
  EQ \<Rightarrow> True |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    27
  GT \<Rightarrow> (case cmp x c of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    28
          LT \<Rightarrow> isin t3 x |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    29
          EQ \<Rightarrow> True |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    30
          GT \<Rightarrow> isin t4 x))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    31
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    32
datatype 'a up\<^sub>i = T\<^sub>i "'a tree234" | Up\<^sub>i "'a tree234" 'a "'a tree234"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    34
fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree234" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    35
"tree\<^sub>i (T\<^sub>i t) = t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    36
"tree\<^sub>i (Up\<^sub>i l p r) = Node2 l p r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    37
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    38
fun ins :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>i" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    39
"ins x Leaf = Up\<^sub>i Leaf x Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    40
"ins x (Node2 l a r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    41
   (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    42
      LT \<Rightarrow> (case ins x l of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    43
              T\<^sub>i l' => T\<^sub>i (Node2 l' a r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    44
            | Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    45
      EQ \<Rightarrow> T\<^sub>i (Node2 l x r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    46
      GT \<Rightarrow> (case ins x r of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    47
              T\<^sub>i r' => T\<^sub>i (Node2 l a r')
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    48
            | Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    49
"ins x (Node3 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    50
   (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    51
      LT \<Rightarrow> (case ins x l of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    52
              T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    53
            | Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    54
      EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    55
      GT \<Rightarrow> (case cmp x b of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    56
               GT \<Rightarrow> (case ins x r of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    57
                       T\<^sub>i r' => T\<^sub>i (Node3 l a m b r')
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    58
                     | Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    59
               EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    60
               LT \<Rightarrow> (case ins x m of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    61
                       T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    62
                     | Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    63
"ins a (Node4 l x1 m x2 n x3 r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    64
   (if a < x2 then
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    65
      if a < x1 then
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    66
        (case ins a l of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    67
           T\<^sub>i l' => T\<^sub>i (Node4 l' x1 m x2 n x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    68
         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) x1 (Node3 m x2 n x3 r))
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    69
      else if a=x1 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    70
      else (case ins a m of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    71
                T\<^sub>i m' => T\<^sub>i (Node4 l x1 m' x2 n x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    72
              | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l x1 m1) q (Node3 m2 x2 n x3 r))
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    73
    else if a=x2 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    74
    else if a < x3 then
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    75
           (case ins a n of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    76
              T\<^sub>i n' => T\<^sub>i (Node4 l x1 m x2 n' x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    77
            | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l x1 m) x2 (Node3 n1 q n2 x3 r))
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    78
         else if a=x3 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    79
         else (case ins a r of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    80
              T\<^sub>i r' => T\<^sub>i (Node4 l x1 m x2 n x3 r')
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    81
            | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l x1 m) x2 (Node3 n x3 r1 q r2))
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    82
)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    83
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    84
hide_const insert
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    85
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    86
definition insert :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    87
"insert x t = tree\<^sub>i(ins x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    88
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    89
datatype 'a up\<^sub>d = T\<^sub>d "'a tree234" | Up\<^sub>d "'a tree234"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    90
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    91
fun tree\<^sub>d :: "'a up\<^sub>d \<Rightarrow> 'a tree234" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    92
"tree\<^sub>d (T\<^sub>d x) = x" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    93
"tree\<^sub>d (Up\<^sub>d x) = x"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    94
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    95
fun node21 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    96
"node21 (T\<^sub>d l) a r = T\<^sub>d(Node2 l a r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    97
"node21 (Up\<^sub>d l) a (Node2 lr b rr) = Up\<^sub>d(Node3 l a lr b rr)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    98
"node21 (Up\<^sub>d l) a (Node3 lr b mr c rr) = T\<^sub>d(Node2 (Node2 l a lr) b (Node2 mr c rr))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    99
"node21 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node3 t3 c t4 d t5))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   100
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   101
fun node22 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   102
"node22 l a (T\<^sub>d r) = T\<^sub>d(Node2 l a r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   103
"node22 (Node2 ll b rl) a (Up\<^sub>d r) = Up\<^sub>d(Node3 ll b rl a r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   104
"node22 (Node3 ll b ml c rl) a (Up\<^sub>d r) = T\<^sub>d(Node2 (Node2 ll b ml) c (Node2 rl a r))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   105
"node22 (Node4 t1 a t2 b t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node3 t3 c t4 d t5))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   106
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   107
fun node31 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   108
"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
   109
"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
   110
"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
   111
"node31 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) e t6 = T\<^sub>d(Node3 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   112
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   113
fun node32 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   114
"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
   115
"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
   116
"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
   117
"node32 t1 a (Up\<^sub>d t2) b (Node4 t3 c t4 d t5 e t6) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   118
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   119
fun node33 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   120
"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
   121
"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
   122
"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
   123
"node33 t1 a (Node4 t2 b t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   124
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   125
fun node41 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   126
"node41 (T\<^sub>d t1) a t2 b t3 c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   127
"node41 (Up\<^sub>d t1) a (Node2 t2 b t3) c t4 d t5 = T\<^sub>d(Node3 (Node3 t1 a t2 b t3) c t4 d t5)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   128
"node41 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) d t5 e t6 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node2 t3 c t4) d t5 e t6)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   129
"node41 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) e t6 f t7 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6 f t7)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   130
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   131
fun node42 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   132
"node42 t1 a (T\<^sub>d t2) b t3 c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   133
"node42 (Node2 t1 a t2) b (Up\<^sub>d t3) c t4 d t5 = T\<^sub>d(Node3 (Node3 t1 a t2 b t3) c t4 d t5)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   134
"node42 (Node3 t1 a t2 b t3) c (Up\<^sub>d t4) d t5 e t6 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node2 t3 c t4) d t5 e t6)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   135
"node42 (Node4 t1 a t2 b t3 c t4) d (Up\<^sub>d t5) e t6 f t7 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6 f t7)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   136
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   137
fun node43 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   138
"node43 t1 a t2 b (T\<^sub>d t3) c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   139
"node43 t1 a (Node2 t2 b t3) c (Up\<^sub>d t4) d t5 = T\<^sub>d(Node3 t1 a (Node3 t2 b t3 c t4) d t5)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   140
"node43 t1 a (Node3 t2 b t3 c t4) d (Up\<^sub>d t5) e t6 = T\<^sub>d(Node4 t1 a (Node2 t2 b t3) c (Node2 t4 d t5) e t6)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   141
"node43 t1 a (Node4 t2 b t3 c t4 d t5) e (Up\<^sub>d t6) f t7 = T\<^sub>d(Node4 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6) f t7)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   142
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   143
fun node44 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   144
"node44 t1 a t2 b t3 c (T\<^sub>d t4) = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   145
"node44 t1 a t2 b (Node2 t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a t2 b (Node3 t3 c t4 d t5))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   146
"node44 t1 a t2 b (Node3 t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node2 t5 e t6))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   147
"node44 t1 a t2 b (Node4 t3 c t4 d t5 e t6) f (Up\<^sub>d t7) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node3 t5 e t6 f t7))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   148
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   149
fun del_min :: "'a tree234 \<Rightarrow> 'a * 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   150
"del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   151
"del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   152
"del_min (Node4 Leaf a Leaf b Leaf c Leaf) = (a, T\<^sub>d(Node3 Leaf b Leaf c Leaf))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   153
"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
   154
"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
   155
"del_min (Node4 l a m b n c r) = (let (x,l') = del_min l in (x, node41 l' a m b n c r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   156
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   157
fun del :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   158
"del k Leaf = T\<^sub>d Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   159
"del k (Node2 Leaf p Leaf) = (if k=p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   160
"del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=p then Node2 Leaf q Leaf
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   161
  else if k=q then Node2 Leaf p Leaf else Node3 Leaf p Leaf q Leaf)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   162
"del k (Node4 Leaf a Leaf b Leaf c Leaf) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   163
  T\<^sub>d(if k=a then Node3 Leaf b Leaf c Leaf else
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   164
     if k=b then Node3 Leaf a Leaf c Leaf else
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   165
     if k=c then Node3 Leaf a Leaf b Leaf
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   166
     else Node4 Leaf a Leaf b Leaf c Leaf)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   167
"del k (Node2 l a r) = (case cmp k a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   168
  LT \<Rightarrow> node21 (del k l) a r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   169
  GT \<Rightarrow> node22 l a (del k r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   170
  EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   171
"del k (Node3 l a m b r) = (case cmp k a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   172
  LT \<Rightarrow> node31 (del k l) a m b r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   173
  EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   174
  GT \<Rightarrow> (case cmp k b of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   175
           LT \<Rightarrow> node32 l a (del k m) b r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   176
           EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   177
           GT \<Rightarrow> node33 l a m b (del k r)))" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   178
"del k (Node4 l a m b n c r) = (case cmp k b of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   179
  LT \<Rightarrow> (case cmp k a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   180
          LT \<Rightarrow> node41 (del k l) a m b n c r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   181
          EQ \<Rightarrow> let (a',m') = del_min m in node42 l a' m' b n c r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   182
          GT \<Rightarrow> node42 l a (del k m) b n c r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   183
  EQ \<Rightarrow> let (b',n') = del_min n in node43 l a m b' n' c r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   184
  GT \<Rightarrow> (case cmp k c of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   185
           LT \<Rightarrow> node43 l a m b (del k n) c r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   186
           EQ \<Rightarrow> let (c',r') = del_min r in node44 l a m b n c' r' |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   187
           GT \<Rightarrow> node44 l a m b n c (del k r)))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   188
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   189
definition delete :: "'a::cmp \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   190
"delete x t = tree\<^sub>d(del x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   191
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   192
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   193
subsection "Functional correctness"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   194
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   195
subsubsection \<open>Functional correctness of isin:\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   196
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   197
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   198
by (induction t) (auto simp: elems_simps1 ball_Un)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   199
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   200
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   201
by (induction t) (auto simp: elems_simps2)
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 \<open>Functional correctness of insert:\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   205
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   206
lemma inorder_ins:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   207
  "sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   208
by(induction t) (auto, auto simp: ins_list_simps split: up\<^sub>i.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   209
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   210
lemma inorder_insert:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   211
  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   212
by(simp add: insert_def inorder_ins)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   213
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   214
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   215
subsubsection \<open>Functional correctness of delete\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   216
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   217
lemma inorder_node21: "height r > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   218
  inorder (tree\<^sub>d (node21 l' a r)) = inorder (tree\<^sub>d l') @ a # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   219
by(induct l' a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   220
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   221
lemma inorder_node22: "height l > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   222
  inorder (tree\<^sub>d (node22 l a r')) = inorder l @ a # inorder (tree\<^sub>d r')"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   223
by(induct l a r' rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   224
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   225
lemma inorder_node31: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   226
  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
   227
by(induct l' a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   228
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   229
lemma inorder_node32: "height r > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   230
  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
   231
by(induct l a m' b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   232
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   233
lemma inorder_node33: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   234
  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
   235
by(induct l a m b r' rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   236
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   237
lemma inorder_node41: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   238
  inorder (tree\<^sub>d (node41 l' a m b n c r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder n @ c # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   239
by(induct l' a m b n c r rule: node41.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   240
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   241
lemma inorder_node42: "height l > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   242
  inorder (tree\<^sub>d (node42 l a m b n c r)) = inorder l @ a # inorder (tree\<^sub>d m) @ b # inorder n @ c # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   243
by(induct l a m b n c r rule: node42.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   244
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   245
lemma inorder_node43: "height m > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   246
  inorder (tree\<^sub>d (node43 l a m b n c r)) = inorder l @ a # inorder m @ b # inorder(tree\<^sub>d n) @ c # inorder r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   247
by(induct l a m b n c r rule: node43.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   248
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   249
lemma inorder_node44: "height n > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   250
  inorder (tree\<^sub>d (node44 l a m b n c r)) = inorder l @ a # inorder m @ b # inorder n @ c # inorder (tree\<^sub>d r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   251
by(induct l a m b n c r rule: node44.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   252
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   253
lemmas inorder_nodes = inorder_node21 inorder_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   254
  inorder_node31 inorder_node32 inorder_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   255
  inorder_node41 inorder_node42 inorder_node43 inorder_node44
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   256
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   257
lemma del_minD:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   258
  "del_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   259
  x # inorder(tree\<^sub>d t') = inorder t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   260
by(induction t arbitrary: t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   261
  (auto simp: inorder_nodes split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   262
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   263
lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   264
  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   265
by(induction t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   266
  (auto simp: inorder_nodes del_list_simps del_minD split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   267
  (* 150 secs (2015) *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   268
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   269
lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   270
  inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   271
by(simp add: delete_def inorder_del)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   272
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   273
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   274
subsection \<open>Balancedness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   275
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   276
subsubsection "Proofs for insert"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   277
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   278
text{* First a standard proof that @{const ins} preserves @{const bal}. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   279
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   280
instantiation up\<^sub>i :: (type)height
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   281
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   282
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   283
fun height_up\<^sub>i :: "'a up\<^sub>i \<Rightarrow> nat" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   284
"height (T\<^sub>i t) = height t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   285
"height (Up\<^sub>i l a r) = height l"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   286
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   287
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   288
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   289
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   290
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   291
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
   292
by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   293
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   294
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   295
text{* Now an alternative proof (by Brian Huffman) that runs faster because
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   296
two properties (balance and height) are combined in one predicate. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   297
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   298
inductive full :: "nat \<Rightarrow> 'a tree234 \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   299
"full 0 Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   300
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   301
"\<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
   302
"\<lbrakk>full n l; full n m; full n m'; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node4 l p m q m' q' r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   303
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   304
inductive_cases full_elims:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   305
  "full n Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   306
  "full n (Node2 l p r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   307
  "full n (Node3 l p m q r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   308
  "full n (Node4 l p m q m' q' r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   309
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   310
inductive_cases full_0_elim: "full 0 t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   311
inductive_cases full_Suc_elim: "full (Suc n) t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   312
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   313
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   314
  by (auto elim: full_0_elim intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   315
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   316
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   317
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   318
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   319
lemma full_Suc_Node2_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   320
  "full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   321
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   322
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   323
lemma full_Suc_Node3_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   324
  "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
   325
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   326
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   327
lemma full_Suc_Node4_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   328
  "full (Suc n) (Node4 l p m q m' q' r) \<longleftrightarrow> full n l \<and> full n m \<and> full n m' \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   329
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   330
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   331
lemma full_imp_height: "full n t \<Longrightarrow> height t = n"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   332
  by (induct set: full, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   333
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   334
lemma full_imp_bal: "full n t \<Longrightarrow> bal t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   335
  by (induct set: full, auto dest: full_imp_height)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   336
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   337
lemma bal_imp_full: "bal t \<Longrightarrow> full (height t) t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   338
  by (induct t, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   339
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   340
lemma bal_iff_full: "bal t \<longleftrightarrow> (\<exists>n. full n t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   341
  by (auto elim!: bal_imp_full full_imp_bal)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   342
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   343
text {* The @{const "insert"} function either preserves the height of the
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   344
tree, or increases it by one. The constructor returned by the @{term
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   345
"insert"} function determines which: A return value of the form @{term
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   346
"T\<^sub>i t"} indicates that the height will be the same. A value of the
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   347
form @{term "Up\<^sub>i l p r"} indicates an increase in height. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   348
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   349
primrec full\<^sub>i :: "nat \<Rightarrow> 'a up\<^sub>i \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   350
"full\<^sub>i n (T\<^sub>i t) \<longleftrightarrow> full n t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   351
"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
   352
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   353
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   354
by (induct rule: full.induct) (auto, auto split: up\<^sub>i.split)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   355
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   356
text {* The @{const insert} operation preserves balance. *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   357
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   358
lemma bal_insert: "bal t \<Longrightarrow> bal (insert a t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   359
unfolding bal_iff_full insert_def
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   360
apply (erule exE)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   361
apply (drule full\<^sub>i_ins [of _ _ a])
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   362
apply (cases "ins a t")
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   363
apply (auto intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   364
done
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   365
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   366
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   367
subsubsection "Proofs for delete"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   368
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   369
instantiation up\<^sub>d :: (type)height
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   370
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   371
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   372
fun height_up\<^sub>d :: "'a up\<^sub>d \<Rightarrow> nat" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   373
"height (T\<^sub>d t) = height t" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   374
"height (Up\<^sub>d t) = height t + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   375
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   376
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   377
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   378
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   379
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   380
lemma bal_tree\<^sub>d_node21:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   381
  "\<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
   382
by(induct l a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   383
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   384
lemma bal_tree\<^sub>d_node22:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   385
  "\<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
   386
by(induct l a r rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   387
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   388
lemma bal_tree\<^sub>d_node31:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   389
  "\<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
   390
  \<Longrightarrow> bal (tree\<^sub>d (node31 l a m b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   391
by(induct l a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   392
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   393
lemma bal_tree\<^sub>d_node32:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   394
  "\<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
   395
  \<Longrightarrow> bal (tree\<^sub>d (node32 l a m b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   396
by(induct l a m b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   397
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   398
lemma bal_tree\<^sub>d_node33:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   399
  "\<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
   400
  \<Longrightarrow> bal (tree\<^sub>d (node33 l a m b r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   401
by(induct l a m b r rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   402
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   403
lemma bal_tree\<^sub>d_node41:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   404
  "\<lbrakk> bal (tree\<^sub>d l); bal m; bal n; bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   405
  \<Longrightarrow> bal (tree\<^sub>d (node41 l a m b n c r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   406
by(induct l a m b n c r rule: node41.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   407
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   408
lemma bal_tree\<^sub>d_node42:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   409
  "\<lbrakk> bal l; bal (tree\<^sub>d m); bal n; bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   410
  \<Longrightarrow> bal (tree\<^sub>d (node42 l a m b n c r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   411
by(induct l a m b n c r rule: node42.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   412
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   413
lemma bal_tree\<^sub>d_node43:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   414
  "\<lbrakk> bal l; bal m; bal (tree\<^sub>d n); bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   415
  \<Longrightarrow> bal (tree\<^sub>d (node43 l a m b n c r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   416
by(induct l a m b n c r rule: node43.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   417
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   418
lemma bal_tree\<^sub>d_node44:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   419
  "\<lbrakk> bal l; bal m; bal n; bal (tree\<^sub>d r); height l = height r; height m = height r; height n = height r \<rbrakk>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   420
  \<Longrightarrow> bal (tree\<^sub>d (node44 l a m b n c r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   421
by(induct l a m b n c r rule: node44.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   422
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   423
lemmas bals = bal_tree\<^sub>d_node21 bal_tree\<^sub>d_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   424
  bal_tree\<^sub>d_node31 bal_tree\<^sub>d_node32 bal_tree\<^sub>d_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   425
  bal_tree\<^sub>d_node41 bal_tree\<^sub>d_node42 bal_tree\<^sub>d_node43 bal_tree\<^sub>d_node44
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   426
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   427
lemma height_node21:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   428
   "height r > 0 \<Longrightarrow> height(node21 l a r) = max (height l) (height r) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   429
by(induct l a r rule: node21.induct)(simp_all add: max.assoc)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   430
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   431
lemma height_node22:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   432
   "height l > 0 \<Longrightarrow> height(node22 l a r) = max (height l) (height r) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   433
by(induct l a r rule: node22.induct)(simp_all add: max.assoc)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   434
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   435
lemma height_node31:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   436
  "height m > 0 \<Longrightarrow> height(node31 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   437
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   438
by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   439
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   440
lemma height_node32:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   441
  "height r > 0 \<Longrightarrow> height(node32 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   442
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   443
by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   444
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   445
lemma height_node33:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   446
  "height m > 0 \<Longrightarrow> height(node33 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   447
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   448
by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   449
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   450
lemma height_node41:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   451
  "height m > 0 \<Longrightarrow> height(node41 l a m b n c r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   452
   max (height l) (max (height m) (max (height n) (height r))) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   453
by(induct l a m b n c r rule: node41.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   454
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   455
lemma height_node42:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   456
  "height l > 0 \<Longrightarrow> height(node42 l a m b n c r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   457
   max (height l) (max (height m) (max (height n) (height r))) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   458
by(induct l a m b n c r rule: node42.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   459
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   460
lemma height_node43:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   461
  "height m > 0 \<Longrightarrow> height(node43 l a m b n c r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   462
   max (height l) (max (height m) (max (height n) (height r))) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   463
by(induct l a m b n c r rule: node43.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   464
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   465
lemma height_node44:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   466
  "height n > 0 \<Longrightarrow> height(node44 l a m b n c r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   467
   max (height l) (max (height m) (max (height n) (height r))) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   468
by(induct l a m b n c r rule: node44.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   469
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   470
lemmas heights = height_node21 height_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   471
  height_node31 height_node32 height_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   472
  height_node41 height_node42 height_node43 height_node44
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   473
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   474
lemma height_del_min:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   475
  "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
   476
by(induct t arbitrary: x t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   477
  (auto simp: heights split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   478
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   479
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   480
by(induction x t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   481
  (auto simp add: heights height_del_min split: prod.split)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   482
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   483
lemma bal_del_min:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   484
  "\<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
   485
by(induct t arbitrary: x t' rule: del_min.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   486
  (auto simp: heights height_del_min bals split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   487
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   488
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   489
by(induction x t rule: del.induct)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   490
  (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   491
(* 60 secs (2015) *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   492
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   493
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   494
by(simp add: delete_def bal_tree\<^sub>d_del)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   495
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   496
subsection \<open>Overall Correctness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   497
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   498
interpretation Set_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   499
where empty = Leaf and isin = isin and insert = insert and delete = delete
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   500
and inorder = inorder and inv = bal
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   501
proof (standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   502
  case 2 thus ?case by(simp add: isin_set)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   503
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   504
  case 3 thus ?case by(simp add: inorder_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   505
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   506
  case 4 thus ?case by(simp add: inorder_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   507
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   508
  case 6 thus ?case by(simp add: bal_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   509
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   510
  case 7 thus ?case by(simp add: bal_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   511
qed simp+
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   512
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   513
end