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