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