src/HOL/Data_Structures/Tree23_Set.thy
author nipkow
Thu, 29 Aug 2019 13:07:56 +0200
changeset 70628 40b63f2655e8
parent 70274 7daa65d45462
child 72566 831f17da1aab
permissions -rw-r--r--
tuned proof
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
67965
aaa31cd0caef more name tuning
nipkow
parents: 67964
diff changeset
     9
  Set_Specs
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    10
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    11
68109
cebf36c14226 new def of sorted and sorted_wrt
nipkow
parents: 68020
diff changeset
    12
declare sorted_wrt.simps(2)[simp del]
cebf36c14226 new def of sorted and sorted_wrt
nipkow
parents: 68020
diff changeset
    13
68431
b294e095f64c more abstract naming
nipkow
parents: 68109
diff changeset
    14
definition empty :: "'a tree23" where
b294e095f64c more abstract naming
nipkow
parents: 68109
diff changeset
    15
"empty = Leaf"
b294e095f64c more abstract naming
nipkow
parents: 68109
diff changeset
    16
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    17
fun isin :: "'a::linorder tree23 \<Rightarrow> 'a \<Rightarrow> bool" where
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    18
"isin Leaf x = False" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    19
"isin (Node2 l a 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> isin r x)" |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    24
"isin (Node3 l a m b r) x =
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    25
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    26
     LT \<Rightarrow> isin l x |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    27
     EQ \<Rightarrow> True |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    28
     GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    29
       (case cmp x b of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    30
          LT \<Rightarrow> isin m x |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    31
          EQ \<Rightarrow> True |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    32
          GT \<Rightarrow> isin r x))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    33
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    34
datatype 'a upI = TI "'a tree23" | OF "'a tree23" 'a "'a tree23"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    35
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    36
fun treeI :: "'a upI \<Rightarrow> 'a tree23" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    37
"treeI (TI t) = t" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    38
"treeI (OF l a r) = Node2 l a r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    39
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    40
fun ins :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a upI" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    41
"ins x Leaf = OF Leaf x Leaf" |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    42
"ins x (Node2 l a r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    43
   (case cmp x a of
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    44
      LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    45
        (case ins x l of
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    46
           TI l' => TI (Node2 l' a r) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    47
           OF l1 b l2 => TI (Node3 l1 b l2 a r)) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    48
      EQ \<Rightarrow> TI (Node2 l x r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    49
      GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    50
        (case ins x r of
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    51
           TI r' => TI (Node2 l a r') |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    52
           OF r1 b r2 => TI (Node3 l a r1 b r2)))" |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    53
"ins x (Node3 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    54
   (case cmp x a of
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    55
      LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    56
        (case ins x l of
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    57
           TI l' => TI (Node3 l' a m b r) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    58
           OF l1 c l2 => OF (Node2 l1 c l2) a (Node2 m b r)) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    59
      EQ \<Rightarrow> TI (Node3 l a m b r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    60
      GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    61
        (case cmp x b of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    62
           GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    63
             (case ins x r of
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    64
                TI r' => TI (Node3 l a m b r') |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    65
                OF r1 c r2 => OF (Node2 l a m) b (Node2 r1 c r2)) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    66
           EQ \<Rightarrow> TI (Node3 l a m b r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    67
           LT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
    68
             (case ins x m of
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    69
                TI m' => TI (Node3 l a m' b r) |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    70
                OF m1 c m2 => OF (Node2 l a m1) c (Node2 m2 b r))))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    71
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    72
hide_const insert
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    73
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    74
definition insert :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    75
"insert x t = treeI(ins x t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    76
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    77
datatype 'a upD = TD "'a tree23" | UF "'a tree23"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    78
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    79
fun treeD :: "'a upD \<Rightarrow> 'a tree23" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    80
"treeD (TD t) = t" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    81
"treeD (UF t) = t"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    82
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    83
(* Variation: return None to signal no-change *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    84
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    85
fun node21 :: "'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    86
"node21 (TD t1) a t2 = TD(Node2 t1 a t2)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    87
"node21 (UF t1) a (Node2 t2 b t3) = UF(Node3 t1 a t2 b t3)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    88
"node21 (UF t1) a (Node3 t2 b t3 c t4) = TD(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    89
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    90
fun node22 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    91
"node22 t1 a (TD t2) = TD(Node2 t1 a t2)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    92
"node22 (Node2 t1 b t2) a (UF t3) = UF(Node3 t1 b t2 a t3)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    93
"node22 (Node3 t1 b t2 c t3) a (UF t4) = TD(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    94
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    95
fun node31 :: "'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    96
"node31 (TD t1) a t2 b t3 = TD(Node3 t1 a t2 b t3)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    97
"node31 (UF t1) a (Node2 t2 b t3) c t4 = TD(Node2 (Node3 t1 a t2 b t3) c t4)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
    98
"node31 (UF t1) a (Node3 t2 b t3 c t4) d t5 = TD(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    99
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   100
fun node32 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   101
"node32 t1 a (TD t2) b t3 = TD(Node3 t1 a t2 b t3)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   102
"node32 t1 a (UF t2) b (Node2 t3 c t4) = TD(Node2 t1 a (Node3 t2 b t3 c t4))" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   103
"node32 t1 a (UF t2) b (Node3 t3 c t4 d t5) = TD(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   104
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   105
fun node33 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a upD \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   106
"node33 l a m b (TD r) = TD(Node3 l a m b r)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   107
"node33 t1 a (Node2 t2 b t3) c (UF t4) = TD(Node2 t1 a (Node3 t2 b t3 c t4))" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   108
"node33 t1 a (Node3 t2 b t3 c t4) d (UF t5) = TD(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   109
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   110
fun split_min :: "'a tree23 \<Rightarrow> 'a * 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   111
"split_min (Node2 Leaf a Leaf) = (a, UF Leaf)" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   112
"split_min (Node3 Leaf a Leaf b Leaf) = (a, TD(Node2 Leaf b Leaf))" |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   113
"split_min (Node2 l a r) = (let (x,l') = split_min l in (x, node21 l' a r))" |
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   114
"split_min (Node3 l a m b r) = (let (x,l') = split_min l in (x, node31 l' a m b r))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   115
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   116
text \<open>In the base cases of \<open>split_min\<close> and \<open>del\<close> it is enough to check if one subtree is a \<open>Leaf\<close>,
67038
nipkow
parents: 63636
diff changeset
   117
in which case balancedness implies that so are the others. Exercise.\<close>
nipkow
parents: 63636
diff changeset
   118
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   119
fun del :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a upD" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   120
"del x Leaf = TD Leaf" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   121
"del x (Node2 Leaf a Leaf) =
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   122
  (if x = a then UF Leaf else TD(Node2 Leaf a Leaf))" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   123
"del x (Node3 Leaf a Leaf b Leaf) =
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   124
  TD(if x = a then Node2 Leaf b Leaf else
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   125
     if x = b then Node2 Leaf a Leaf
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   126
     else Node3 Leaf a Leaf b Leaf)" |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   127
"del x (Node2 l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   128
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   129
     LT \<Rightarrow> node21 (del x l) a r |
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   130
     GT \<Rightarrow> node22 l a (del x r) |
70272
nipkow
parents: 69597
diff changeset
   131
     EQ \<Rightarrow> let (a',r') = split_min r in node22 l a' r')" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   132
"del x (Node3 l a m b r) =
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   133
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   134
     LT \<Rightarrow> node31 (del x l) a m b r |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   135
     EQ \<Rightarrow> let (a',m') = split_min m in node32 l a' m' b r |
61678
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   136
     GT \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61640
diff changeset
   137
       (case cmp x b of
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   138
          LT \<Rightarrow> node32 l a (del x m) b r |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   139
          EQ \<Rightarrow> let (b',r') = split_min r in node33 l a m b' r' |
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   140
          GT \<Rightarrow> node33 l a m b (del x r)))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   141
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
   142
definition delete :: "'a::linorder \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   143
"delete x t = treeD(del x t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   144
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   145
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   146
subsection "Functional Correctness"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   147
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   148
subsubsection "Proofs for isin"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   149
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
   150
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set (inorder t))"
70628
40b63f2655e8 tuned proof
nipkow
parents: 70274
diff changeset
   151
by (induction t) (auto simp: isin_simps)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   152
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   153
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   154
subsubsection "Proofs for insert"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   155
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   156
lemma inorder_ins:
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   157
  "sorted(inorder t) \<Longrightarrow> inorder(treeI(ins x t)) = ins_list x (inorder t)"
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   158
by(induction t) (auto simp: ins_list_simps split: upI.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   159
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   160
lemma inorder_insert:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   161
  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   162
by(simp add: insert_def inorder_ins)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   163
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   164
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   165
subsubsection "Proofs for delete"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   166
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   167
lemma inorder_node21: "height r > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   168
  inorder (treeD (node21 l' a r)) = inorder (treeD l') @ a # inorder r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   169
by(induct l' a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   170
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   171
lemma inorder_node22: "height l > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   172
  inorder (treeD (node22 l a r')) = inorder l @ a # inorder (treeD r')"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   173
by(induct l a r' rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   174
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   175
lemma inorder_node31: "height m > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   176
  inorder (treeD (node31 l' a m b r)) = inorder (treeD l') @ a # inorder m @ b # inorder r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   177
by(induct l' a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   178
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   179
lemma inorder_node32: "height r > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   180
  inorder (treeD (node32 l a m' b r)) = inorder l @ a # inorder (treeD m') @ b # inorder r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   181
by(induct l a m' b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   182
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   183
lemma inorder_node33: "height m > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   184
  inorder (treeD (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (treeD r')"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   185
by(induct l a m b r' rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   186
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   187
lemmas inorder_nodes = inorder_node21 inorder_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   188
  inorder_node31 inorder_node32 inorder_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   189
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   190
lemma split_minD:
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   191
  "split_min t = (x,t') \<Longrightarrow> complete t \<Longrightarrow> height t > 0 \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   192
  x # inorder(treeD t') = inorder t"
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   193
by(induction t arbitrary: t' rule: split_min.induct)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   194
  (auto simp: inorder_nodes split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   195
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   196
lemma inorder_del: "\<lbrakk> complete t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   197
  inorder(treeD (del x t)) = del_list x (inorder t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   198
by(induction t rule: del.induct)
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   199
  (auto simp: del_list_simps inorder_nodes split_minD split!: if_split prod.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   200
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   201
lemma inorder_delete: "\<lbrakk> complete t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   202
  inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   203
by(simp add: delete_def inorder_del)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   204
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   205
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   206
subsection \<open>Balancedness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   207
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   208
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   209
subsubsection "Proofs for insert"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   210
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   211
text\<open>First a standard proof that \<^const>\<open>ins\<close> preserves \<^const>\<open>complete\<close>.\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   212
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   213
instantiation upI :: (type)height
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   214
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   215
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   216
fun height_upI :: "'a upI \<Rightarrow> nat" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   217
"height (TI t) = height t" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   218
"height (OF l a r) = height l"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   219
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   220
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   221
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   222
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   223
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   224
lemma complete_ins: "complete t \<Longrightarrow> complete (treeI(ins a t)) \<and> height(ins a t) = height t"
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   225
by (induct t) (auto split!: if_split upI.split) (* 15 secs in 2015 *)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   226
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67038
diff changeset
   227
text\<open>Now an alternative proof (by Brian Huffman) that runs faster because
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67038
diff changeset
   228
two properties (balance and height) are combined in one predicate.\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   229
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   230
inductive full :: "nat \<Rightarrow> 'a tree23 \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   231
"full 0 Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   232
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   233
"\<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
   234
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   235
inductive_cases full_elims:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   236
  "full n Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   237
  "full n (Node2 l p r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   238
  "full n (Node3 l p m q r)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   239
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   240
inductive_cases full_0_elim: "full 0 t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   241
inductive_cases full_Suc_elim: "full (Suc n) t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   242
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   243
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   244
  by (auto elim: full_0_elim intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   245
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   246
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   247
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   248
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   249
lemma full_Suc_Node2_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   250
  "full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   251
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   252
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   253
lemma full_Suc_Node3_iff [simp]:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   254
  "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
   255
  by (auto elim: full_elims intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   256
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   257
lemma full_imp_height: "full n t \<Longrightarrow> height t = n"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   258
  by (induct set: full, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   259
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   260
lemma full_imp_complete: "full n t \<Longrightarrow> complete t"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   261
  by (induct set: full, auto dest: full_imp_height)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   262
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   263
lemma complete_imp_full: "complete t \<Longrightarrow> full (height t) t"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   264
  by (induct t, simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   265
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   266
lemma complete_iff_full: "complete t \<longleftrightarrow> (\<exists>n. full n t)"
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   267
  by (auto elim!: complete_imp_full full_imp_complete)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   268
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   269
text \<open>The \<^const>\<open>insert\<close> function either preserves the height of the
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   270
tree, or increases it by one. The constructor returned by the \<^term>\<open>insert\<close> function determines which: A return value of the form \<^term>\<open>TI t\<close> indicates that the height will be the same. A value of the
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   271
form \<^term>\<open>OF l p r\<close> indicates an increase in height.\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   272
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   273
fun full\<^sub>i :: "nat \<Rightarrow> 'a upI \<Rightarrow> bool" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   274
"full\<^sub>i n (TI t) \<longleftrightarrow> full n t" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   275
"full\<^sub>i n (OF l p r) \<longleftrightarrow> full n l \<and> full n r"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   276
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   277
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)"
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   278
by (induct rule: full.induct) (auto split: upI.split)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   279
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   280
text \<open>The \<^const>\<open>insert\<close> operation preserves completeance.\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   281
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   282
lemma complete_insert: "complete t \<Longrightarrow> complete (insert a t)"
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   283
unfolding complete_iff_full insert_def
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   284
apply (erule exE)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   285
apply (drule full\<^sub>i_ins [of _ _ a])
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   286
apply (cases "ins a t")
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   287
apply (auto intro: full.intros)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   288
done
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   289
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   290
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   291
subsection "Proofs for delete"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   292
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   293
instantiation upD :: (type)height
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   294
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   295
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   296
fun height_upD :: "'a upD \<Rightarrow> nat" where
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   297
"height (TD t) = height t" |
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   298
"height (UF t) = height t + 1"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   299
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   300
instance ..
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   301
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   302
end
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   303
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   304
lemma complete_treeD_node21:
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   305
  "\<lbrakk>complete r; complete (treeD l'); height r = height l' \<rbrakk> \<Longrightarrow> complete (treeD (node21 l' a r))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   306
by(induct l' a r rule: node21.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   307
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   308
lemma complete_treeD_node22:
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   309
  "\<lbrakk>complete(treeD r'); complete l; height r' = height l \<rbrakk> \<Longrightarrow> complete (treeD (node22 l a r'))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   310
by(induct l a r' rule: node22.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   311
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   312
lemma complete_treeD_node31:
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   313
  "\<lbrakk> complete (treeD l'); complete m; complete r; height l' = height r; height m = height r \<rbrakk>
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   314
  \<Longrightarrow> complete (treeD (node31 l' a m b r))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   315
by(induct l' a m b r rule: node31.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   316
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   317
lemma complete_treeD_node32:
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   318
  "\<lbrakk> complete l; complete (treeD m'); complete r; height l = height r; height m' = height r \<rbrakk>
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   319
  \<Longrightarrow> complete (treeD (node32 l a m' b r))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   320
by(induct l a m' b r rule: node32.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   321
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   322
lemma complete_treeD_node33:
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   323
  "\<lbrakk> complete l; complete m; complete(treeD r'); height l = height r'; height m = height r' \<rbrakk>
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   324
  \<Longrightarrow> complete (treeD (node33 l a m b r'))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   325
by(induct l a m b r' rule: node33.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   326
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   327
lemmas completes = complete_treeD_node21 complete_treeD_node22
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   328
  complete_treeD_node31 complete_treeD_node32 complete_treeD_node33
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   329
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   330
lemma height'_node21:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   331
   "height r > 0 \<Longrightarrow> height(node21 l' a r) = max (height l') (height r) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   332
by(induct l' a r rule: node21.induct)(simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   333
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   334
lemma height'_node22:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   335
   "height l > 0 \<Longrightarrow> height(node22 l a r') = max (height l) (height r') + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   336
by(induct l a r' rule: node22.induct)(simp_all)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   337
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   338
lemma height'_node31:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   339
  "height m > 0 \<Longrightarrow> height(node31 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   340
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   341
by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   342
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   343
lemma height'_node32:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   344
  "height r > 0 \<Longrightarrow> height(node32 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   345
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   346
by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   347
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   348
lemma height'_node33:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   349
  "height m > 0 \<Longrightarrow> height(node33 l a m b r) =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   350
   max (height l) (max (height m) (height r)) + 1"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   351
by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   352
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   353
lemmas heights = height'_node21 height'_node22
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   354
  height'_node31 height'_node32 height'_node33
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   355
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   356
lemma height_split_min:
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   357
  "split_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> complete t \<Longrightarrow> height t' = height t"
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   358
by(induct t arbitrary: x t' rule: split_min.induct)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   359
  (auto simp: heights split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   360
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   361
lemma height_del: "complete t \<Longrightarrow> height(del x t) = height t"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   362
by(induction x t rule: del.induct)
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   363
  (auto simp: heights max_def height_split_min split: prod.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   364
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   365
lemma complete_split_min:
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   366
  "\<lbrakk> split_min t = (x, t'); complete t; height t > 0 \<rbrakk> \<Longrightarrow> complete (treeD t')"
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   367
by(induct t arbitrary: x t' rule: split_min.induct)
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   368
  (auto simp: heights height_split_min completes split: prod.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   369
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   370
lemma complete_treeD_del: "complete t \<Longrightarrow> complete(treeD(del x t))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   371
by(induction x t rule: del.induct)
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   372
  (auto simp: completes complete_split_min height_del height_split_min split: prod.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   373
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   374
corollary complete_delete: "complete t \<Longrightarrow> complete(delete x t)"
70274
7daa65d45462 tuned names
nipkow
parents: 70273
diff changeset
   375
by(simp add: delete_def complete_treeD_del)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   376
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   377
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   378
subsection \<open>Overall Correctness\<close>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   379
68440
6826718f732d qualify interpretations to avoid clashes
nipkow
parents: 68431
diff changeset
   380
interpretation S: Set_by_Ordered
68431
b294e095f64c more abstract naming
nipkow
parents: 68109
diff changeset
   381
where empty = empty and isin = isin and insert = insert and delete = delete
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   382
and inorder = inorder and inv = complete
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   383
proof (standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   384
  case 2 thus ?case by(simp add: isin_set)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   385
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   386
  case 3 thus ?case by(simp add: inorder_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   387
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   388
  case 4 thus ?case by(simp add: inorder_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   389
next
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   390
  case 6 thus ?case by(simp add: complete_insert)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   391
next
70273
acc1749c2be9 tuned name
nipkow
parents: 70272
diff changeset
   392
  case 7 thus ?case by(simp add: complete_delete)
68431
b294e095f64c more abstract naming
nipkow
parents: 68109
diff changeset
   393
qed (simp add: empty_def)+
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   394
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
   395
end