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