src/HOL/Data_Structures/AVL_Set.thy
author wenzelm
Mon, 15 Feb 2016 14:55:44 +0100
changeset 62337 d3996d5873dd
parent 61678 b594e9277be3
child 62526 347150095fd2
permissions -rw-r--r--
proper syntax;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     1
(*
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     2
Author:     Tobias Nipkow
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     3
Derived from AFP entry AVL.
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     4
*)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     5
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     6
section "AVL Tree Implementation of Sets"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     7
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     8
theory AVL_Set
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
     9
imports Cmp Isin2
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    10
begin
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    11
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    12
type_synonym 'a avl_tree = "('a,nat) tree"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    13
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    14
text {* Invariant: *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    15
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    16
fun avl :: "'a avl_tree \<Rightarrow> bool" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    17
"avl Leaf = True" |
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    18
"avl (Node h l a r) =
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    19
 ((height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1) \<and> 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    20
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    21
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    22
fun ht :: "'a avl_tree \<Rightarrow> nat" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    23
"ht Leaf = 0" |
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    24
"ht (Node h l a r) = h"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    25
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    26
definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    27
"node l a r = Node (max (ht l) (ht r) + 1) l a r"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    28
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    29
definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    30
"balL l a r =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    31
  (if ht l = ht r + 2 then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    32
     case l of 
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    33
       Node _ bl b br \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    34
         if ht bl < ht br then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    35
           case br of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    36
             Node _ cl c cr \<Rightarrow> node (node bl b cl) c (node cr a r)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    37
         else node bl b (node br a r)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    38
   else node l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    39
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    40
definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    41
"balR l a r =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    42
   (if ht r = ht l + 2 then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    43
      case r of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    44
        Node _ bl b br \<Rightarrow>
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    45
          if ht bl > ht br then
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    46
            case bl of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    47
              Node _ cl c cr \<Rightarrow> node (node l a cl) c (node cr b br)
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    48
          else node (node l a bl) b br
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    49
  else node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    50
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    51
fun insert :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    52
"insert x Leaf = Node 1 Leaf x Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    53
"insert x (Node h l a r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    54
   EQ \<Rightarrow> Node h l a r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    55
   LT \<Rightarrow> balL (insert x l) a r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    56
   GT \<Rightarrow> balR l a (insert x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    57
61647
nipkow
parents: 61588
diff changeset
    58
fun del_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    59
"del_max (Node _ l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    60
  (if r = Leaf then (l,a) else let (r',a') = del_max r in (balL l a r', a'))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    61
61647
nipkow
parents: 61588
diff changeset
    62
lemmas del_max_induct = del_max.induct[case_names Node Leaf]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    63
61647
nipkow
parents: 61588
diff changeset
    64
fun del_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow
parents: 61588
diff changeset
    65
"del_root (Node h Leaf a r) = r" |
nipkow
parents: 61588
diff changeset
    66
"del_root (Node h l a Leaf) = l" |
nipkow
parents: 61588
diff changeset
    67
"del_root (Node h l a r) = (let (l', a') = del_max l in balR l' a' r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    68
61647
nipkow
parents: 61588
diff changeset
    69
lemmas del_root_cases = del_root.cases[case_names Leaf_t Node_Leaf Node_Node]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    70
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    71
fun delete :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    72
"delete _ Leaf = Leaf" |
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    73
"delete x (Node h l a r) =
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    74
  (case cmp x a of
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    75
     EQ \<Rightarrow> del_root (Node h l a r) |
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    76
     LT \<Rightarrow> balR (delete x l) a r |
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    77
     GT \<Rightarrow> balL l a (delete x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    78
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    79
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    80
subsection {* Functional Correctness Proofs *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    81
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    82
text{* Very different from the AFP/AVL proofs *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    83
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    84
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    85
subsubsection "Proofs for insert"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    86
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    87
lemma inorder_balL:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    88
  "inorder (balL l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    89
by (auto simp: node_def balL_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    90
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    91
lemma inorder_balR:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    92
  "inorder (balR l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    93
by (auto simp: node_def balR_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    94
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    95
theorem inorder_insert:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    96
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    97
by (induct t) 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    98
   (auto simp: ins_list_simps inorder_balL inorder_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    99
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   100
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   101
subsubsection "Proofs for delete"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   102
61647
nipkow
parents: 61588
diff changeset
   103
lemma inorder_del_maxD:
nipkow
parents: 61588
diff changeset
   104
  "\<lbrakk> del_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   105
   inorder t' @ [a] = inorder t"
61647
nipkow
parents: 61588
diff changeset
   106
by(induction t arbitrary: t' rule: del_max.induct)
nipkow
parents: 61588
diff changeset
   107
  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   108
61647
nipkow
parents: 61588
diff changeset
   109
lemma inorder_del_root:
nipkow
parents: 61588
diff changeset
   110
  "inorder (del_root (Node h l a r)) = inorder l @ inorder r"
nipkow
parents: 61588
diff changeset
   111
by(induction "Node h l a r" arbitrary: l a r h rule: del_root.induct)
nipkow
parents: 61588
diff changeset
   112
  (auto simp: inorder_balL inorder_balR inorder_del_maxD split: if_splits prod.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   113
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   114
theorem inorder_delete:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   115
  "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   116
by(induction t)
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   117
  (auto simp: del_list_simps inorder_balL inorder_balR
61647
nipkow
parents: 61588
diff changeset
   118
    inorder_del_root inorder_del_maxD split: prod.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   119
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   120
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   121
subsubsection "Overall functional correctness"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   122
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   123
interpretation Set_by_Ordered
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   124
where empty = Leaf and isin = isin and insert = insert and delete = delete
61588
nipkow
parents: 61581
diff changeset
   125
and inorder = inorder and inv = "\<lambda>_. True"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   126
proof (standard, goal_cases)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   127
  case 1 show ?case by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   128
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   129
  case 2 thus ?case by(simp add: isin_set)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   130
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   131
  case 3 thus ?case by(simp add: inorder_insert)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   132
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   133
  case 4 thus ?case by(simp add: inorder_delete)
61428
5e1938107371 added invar empty
nipkow
parents: 61232
diff changeset
   134
qed (rule TrueI)+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   135
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   136
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   137
subsection {* AVL invariants *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   138
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   139
text{* Essentially the AFP/AVL proofs *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   140
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   141
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   142
subsubsection {* Insertion maintains AVL balance *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   143
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   144
declare Let_def [simp]
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   145
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   146
lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   147
by (induct t) simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   148
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   149
lemma height_balL:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   150
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   151
   height (balL l a r) = height r + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   152
   height (balL l a r) = height r + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   153
by (cases l) (auto simp:node_def balL_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   154
       
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   155
lemma height_balR:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   156
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   157
   height (balR l a r) = height l + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   158
   height (balR l a r) = height l + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   159
by (cases r) (auto simp add:node_def balR_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   160
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   161
lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   162
by (simp add: node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   163
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   164
lemma avl_node:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   165
  "\<lbrakk> avl l; avl r;
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   166
     height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   167
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   168
by (auto simp add:max_def node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   169
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   170
lemma height_balL2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   171
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   172
   height (balL l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   173
by (cases l, cases r) (simp_all add: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   174
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   175
lemma height_balR2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   176
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   177
   height (balR l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   178
by (cases l, cases r) (simp_all add: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   179
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   180
lemma avl_balL: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   181
  assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   182
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   183
  shows "avl(balL l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   184
proof(cases l)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   185
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   186
  with assms show ?thesis by (simp add: node_def balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   187
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   188
  case (Node ln ll lr lh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   189
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   190
  proof(cases "height l = height r + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   191
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   192
    from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   193
      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   194
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   195
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   196
    with assms show ?thesis by (simp add: avl_node balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   197
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   198
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   199
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   200
lemma avl_balR: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   201
  assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   202
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   203
  shows "avl(balR l a r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   204
proof(cases r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   205
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   206
  with assms show ?thesis by (simp add: node_def balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   207
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   208
  case (Node rn rl rr rh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   209
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   210
  proof(cases "height r = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   211
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   212
      from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   213
        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   214
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   215
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   216
    with assms show ?thesis by (simp add: balR_def avl_node)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   217
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   218
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   219
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   220
(* It appears that these two properties need to be proved simultaneously: *)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   221
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   222
text{* Insertion maintains the AVL property: *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   223
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   224
theorem avl_insert_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   225
  assumes "avl t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   226
  shows "avl(insert x t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   227
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   228
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   229
proof (induction t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   230
  case (Node h l a r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   231
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   232
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   233
  proof(cases "x = a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   234
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   235
    with Node 1 show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   236
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   237
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   238
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   239
    proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   240
      case True
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   241
      with Node 1 show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   242
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   243
      case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   244
      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   245
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   246
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   247
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   248
  from 2 Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   249
  proof(cases "x = a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   250
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   251
    with Node 1 show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   252
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   253
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   254
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   255
     proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   256
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   257
      with Node 2 show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   258
      proof(cases "height (insert x l) = height r + 2")
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   259
        case False with Node 2 `x < a` show ?thesis by (auto simp: height_balL2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   260
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   261
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   262
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   263
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   264
          using Node 2 by (intro height_balL) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   265
        thus ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   266
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   267
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   268
          with 2 `x < a` show ?thesis by (auto)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   269
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   270
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   271
          with True 1 Node(2) `x < a` show ?thesis by (simp) arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   272
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   273
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   274
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   275
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   276
      with Node 2 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   277
      proof(cases "height (insert x r) = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   278
        case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   279
        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_balR2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   280
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   281
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   282
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   283
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   284
          using Node 2 by (intro height_balR) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   285
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   286
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   287
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   288
          with 2 `\<not>x < a` show ?thesis by (auto)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   289
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   290
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   291
          with True 1 Node(4) `\<not>x < a` show ?thesis by (simp) arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   292
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   293
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   294
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   295
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   296
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   297
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   298
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   299
subsubsection {* Deletion maintains AVL balance *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   300
61647
nipkow
parents: 61588
diff changeset
   301
lemma avl_del_max:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   302
  assumes "avl x" and "x \<noteq> Leaf"
61647
nipkow
parents: 61588
diff changeset
   303
  shows "avl (fst (del_max x))" "height x = height(fst (del_max x)) \<or>
nipkow
parents: 61588
diff changeset
   304
         height x = height(fst (del_max x)) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   305
using assms
61647
nipkow
parents: 61588
diff changeset
   306
proof (induct x rule: del_max_induct)
nipkow
parents: 61588
diff changeset
   307
  case (Node h l a r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   308
  case 1
61647
nipkow
parents: 61588
diff changeset
   309
  thus ?case using Node
nipkow
parents: 61588
diff changeset
   310
    by (auto simp: height_balL height_balL2 avl_balL
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   311
      linorder_class.max.absorb1 linorder_class.max.absorb2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   312
      split:prod.split)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   313
next
61647
nipkow
parents: 61588
diff changeset
   314
  case (Node h l a r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   315
  case 2
61647
nipkow
parents: 61588
diff changeset
   316
  let ?r' = "fst (del_max r)"
nipkow
parents: 61588
diff changeset
   317
  from `avl x` Node 2 have "avl l" and "avl r" by simp_all
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   318
  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   319
    apply (auto split:prod.splits simp del:avl.simps) by arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   320
qed auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   321
61647
nipkow
parents: 61588
diff changeset
   322
lemma avl_del_root:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   323
  assumes "avl t" and "t \<noteq> Leaf"
61647
nipkow
parents: 61588
diff changeset
   324
  shows "avl(del_root t)" 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   325
using assms
61647
nipkow
parents: 61588
diff changeset
   326
proof (cases t rule:del_root_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   327
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   328
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   329
  let ?r = "Node rh rl rn rr"
61647
nipkow
parents: 61588
diff changeset
   330
  let ?l' = "fst (del_max ?l)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   331
  from `avl t` and Node_Node have "avl ?r" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   332
  from `avl t` and Node_Node have "avl ?l" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   333
  hence "avl(?l')" "height ?l = height(?l') \<or>
61647
nipkow
parents: 61588
diff changeset
   334
         height ?l = height(?l') + 1" by (rule avl_del_max,simp)+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   335
  with `avl t` Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   336
            \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
61647
nipkow
parents: 61588
diff changeset
   337
  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(del_max ?l)) ?r)"
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   338
    by (rule avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   339
  with Node_Node show ?thesis by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   340
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   341
61647
nipkow
parents: 61588
diff changeset
   342
lemma height_del_root:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   343
  assumes "avl t" and "t \<noteq> Leaf" 
61647
nipkow
parents: 61588
diff changeset
   344
  shows "height t = height(del_root t) \<or> height t = height(del_root t) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   345
using assms
61647
nipkow
parents: 61588
diff changeset
   346
proof (cases t rule: del_root_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   347
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   348
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   349
  let ?r = "Node rh rl rn rr"
61647
nipkow
parents: 61588
diff changeset
   350
  let ?l' = "fst (del_max ?l)"
nipkow
parents: 61588
diff changeset
   351
  let ?t' = "balR ?l' (snd(del_max ?l)) ?r"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   352
  from `avl t` and Node_Node have "avl ?r" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   353
  from `avl t` and Node_Node have "avl ?l" by simp
61647
nipkow
parents: 61588
diff changeset
   354
  hence "avl(?l')"  by (rule avl_del_max,simp)
nipkow
parents: 61588
diff changeset
   355
  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using `avl ?l` by (intro avl_del_max) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   356
  have t_height: "height t = 1 + max (height ?l) (height ?r)" using `avl t` Node_Node by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   357
  have "height t = height ?t' \<or> height t = height ?t' + 1" using  `avl t` Node_Node
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   358
  proof(cases "height ?r = height ?l' + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   359
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   360
    show ?thesis using l'_height t_height False by (subst  height_balR2[OF `avl ?l'` `avl ?r` False])+ arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   361
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   362
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   363
    show ?thesis
61647
nipkow
parents: 61588
diff changeset
   364
    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (del_max ?l)"]])
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   365
      case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   366
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   367
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   368
      case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   369
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   370
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   371
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   372
  thus ?thesis using Node_Node by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   373
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   374
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   375
text{* Deletion maintains the AVL property: *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   376
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   377
theorem avl_delete_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   378
  assumes "avl t" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   379
  shows "avl(delete x t)" and "height t = (height (delete x t)) \<or> height t = height (delete x t) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   380
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   381
proof (induct t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   382
  case (Node h l n r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   383
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   384
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   385
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   386
    case True
61647
nipkow
parents: 61588
diff changeset
   387
    with Node 1 show ?thesis by (auto simp:avl_del_root)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   388
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   389
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   390
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   391
    proof(cases "x<n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   392
      case True
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   393
      with Node 1 show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   394
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   395
      case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   396
      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   397
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   398
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   399
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   400
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   401
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   402
    case True
61647
nipkow
parents: 61588
diff changeset
   403
    with 1 have "height (Node h l n r) = height(del_root (Node h l n r))
nipkow
parents: 61588
diff changeset
   404
      \<or> height (Node h l n r) = height(del_root (Node h l n r)) + 1"
nipkow
parents: 61588
diff changeset
   405
      by (subst height_del_root,simp_all)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   406
    with True show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   407
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   408
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   409
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   410
     proof(cases "x<n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   411
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   412
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   413
      proof(cases "height r = height (delete x l) + 2")
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   414
        case False with Node 1 `x < n` show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   415
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   416
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   417
        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   418
          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   419
          using Node 2 by (intro height_balR) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   420
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   421
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   422
          assume ?A
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   423
          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   424
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   425
          assume ?B
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   426
          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   427
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   428
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   429
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   430
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   431
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   432
      proof(cases "height l = height (delete x r) + 2")
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   433
        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   434
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   435
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   436
        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   437
          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   438
          using Node 2 by (intro height_balL) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   439
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   440
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   441
          assume ?A
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   442
          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   443
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   444
          assume ?B
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   445
          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   446
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   447
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   448
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   449
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   450
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   451
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   452
end