src/HOL/Data_Structures/AVL_Set.thy
author wenzelm
Fri, 09 Oct 2015 21:16:00 +0200
changeset 61379 c57820ceead3
parent 61232 c46faf9762f7
child 61428 5e1938107371
permissions -rw-r--r--
more direct HTML presentation, without print mode;
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)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   133
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   134
  case 5 thus ?case ..
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   135
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   136
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   137
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   138
subsection {* AVL invariants *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   139
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   140
text{* Essentially the AFP/AVL proofs *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   141
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   142
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   143
subsubsection {* Insertion maintains AVL balance *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   144
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   145
declare Let_def [simp]
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   146
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   147
lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   148
by (induct t) simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   149
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   150
lemma height_node_bal_l:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   151
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   152
   height (node_bal_l l a r) = height r + 2 \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   153
   height (node_bal_l l a r) = height r + 3"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   154
by (cases l) (auto simp:node_def node_bal_l_def split:tree.split)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   155
       
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   156
lemma height_node_bal_r:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   157
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   158
   height (node_bal_r l a r) = height l + 2 \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   159
   height (node_bal_r l a r) = height l + 3"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   160
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
   161
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   162
lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   163
by (simp add: node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   164
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   165
lemma avl_node:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   166
  "\<lbrakk> avl l; avl r;
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   167
     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
   168
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   169
by (auto simp add:max_def node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   170
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   171
lemma height_node_bal_l2:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   172
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   173
   height (node_bal_l l a r) = (1 + max (height l) (height r))"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   174
by (cases l, cases r) (simp_all add: node_bal_l_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   175
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   176
lemma height_node_bal_r2:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   177
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   178
   height (node_bal_r l a r) = (1 + max (height l) (height r))"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   179
by (cases l, cases r) (simp_all add: node_bal_r_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   180
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   181
lemma avl_node_bal_l: 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   182
  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
   183
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   184
  shows "avl(node_bal_l l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   185
proof(cases l)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   186
  case Leaf
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   187
  with assms show ?thesis by (simp add: node_def node_bal_l_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   188
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   189
  case (Node ln ll lr lh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   190
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   191
  proof(cases "height l = height r + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   192
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   193
    from True Node assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   194
      by (auto simp: node_bal_l_def intro!: avl_node split: tree.split) arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   195
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   196
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   197
    with assms show ?thesis by (simp add: avl_node node_bal_l_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   198
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   199
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   200
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   201
lemma avl_node_bal_r: 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   202
  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
   203
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   204
  shows "avl(node_bal_r l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   205
proof(cases r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   206
  case Leaf
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   207
  with assms show ?thesis by (simp add: node_def node_bal_r_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   208
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   209
  case (Node rn rl rr rh)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   210
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   211
  proof(cases "height r = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   212
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   213
      from True Node assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   214
        by (auto simp: node_bal_r_def intro!: avl_node split: tree.split) arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   215
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   216
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   217
    with assms show ?thesis by (simp add: node_bal_r_def avl_node)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   218
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   219
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   220
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   221
(* It appears that these two properties need to be proved simultaneously: *)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   222
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   223
text{* Insertion maintains the AVL property: *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   224
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   225
theorem avl_insert_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   226
  assumes "avl t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   227
  shows "avl(insert x t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   228
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   229
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   230
proof (induction t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   231
  case (Node h l a r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   232
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   233
  with Node show ?case
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
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   236
    with Node 1 show ?thesis by simp
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
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   239
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   240
    proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   241
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   242
      with Node 1 show ?thesis by (auto simp add:avl_node_bal_l)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   243
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   244
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   245
      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
   246
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   247
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   248
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   249
  from 2 Node show ?case
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 1 show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   253
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   254
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   255
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   256
     proof(cases "x<a")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   257
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   258
      with Node 2 show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   259
      proof(cases "height (insert x l) = height r + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   260
        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
   261
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   262
        case True 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   263
        hence "(height (node_bal_l (insert x l) a r) = height r + 2) \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   264
          (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
   265
          using Node 2 by (intro height_node_bal_l) simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   266
        thus ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   267
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   268
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   269
          with 2 `x < a` show ?thesis by (auto)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   270
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   271
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   272
          with True 1 Node(2) `x < a` show ?thesis by (simp) arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   273
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   274
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   275
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   276
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   277
      with Node 2 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   278
      proof(cases "height (insert x r) = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   279
        case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   280
        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
   281
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   282
        case True 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   283
        hence "(height (node_bal_r l a (insert x r)) = height l + 2) \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   284
          (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
   285
          using Node 2 by (intro height_node_bal_r) simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   286
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   287
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   288
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   289
          with 2 `\<not>x < a` show ?thesis by (auto)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   290
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   291
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   292
          with True 1 Node(4) `\<not>x < a` show ?thesis by (simp) arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   293
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   294
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   295
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   296
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   297
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   298
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   299
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   300
subsubsection {* Deletion maintains AVL balance *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   301
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   302
lemma avl_delete_max:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   303
  assumes "avl x" and "x \<noteq> Leaf"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   304
  shows "avl (fst (delete_max x))" "height x = height(fst (delete_max x)) \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   305
         height x = height(fst (delete_max x)) + 1"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   306
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   307
proof (induct x rule: delete_max_induct)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   308
  case (Node h l a rh rl b rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   309
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   310
  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
   311
  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
   312
    by (intro avl_node_bal_l) fastforce+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   313
  thus ?case 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   314
    by (auto simp: height_node_bal_l height_node_bal_l2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   315
      linorder_class.max.absorb1 linorder_class.max.absorb2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   316
      split:prod.split)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   317
next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   318
  case (Node h l a rh rl b rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   319
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   320
  let ?r = "Node rh rl b rr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   321
  let ?r' = "fst (delete_max ?r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   322
  from `avl x` Node 2 have "avl l" and "avl ?r" by simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   323
  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
   324
    apply (auto split:prod.splits simp del:avl.simps) by arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   325
qed auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   326
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   327
lemma avl_delete_root:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   328
  assumes "avl t" and "t \<noteq> Leaf"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   329
  shows "avl(delete_root t)" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   330
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   331
proof (cases t rule:delete_root_cases)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   332
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   333
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   334
  let ?r = "Node rh rl rn rr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   335
  let ?l' = "fst (delete_max ?l)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   336
  from `avl t` and Node_Node have "avl ?r" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   337
  from `avl t` and Node_Node have "avl ?l" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   338
  hence "avl(?l')" "height ?l = height(?l') \<or>
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   339
         height ?l = height(?l') + 1" by (rule avl_delete_max,simp)+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   340
  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
   341
            \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   342
  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
   343
    by (rule avl_node_bal_r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   344
  with Node_Node show ?thesis by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   345
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   346
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   347
lemma height_delete_root:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   348
  assumes "avl t" and "t \<noteq> Leaf" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   349
  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
   350
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   351
proof (cases t rule: delete_root_cases)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   352
  case (Node_Node h lh ll ln lr n rh rl rn rr)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   353
  let ?l = "Node lh ll ln lr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   354
  let ?r = "Node rh rl rn rr"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   355
  let ?l' = "fst (delete_max ?l)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   356
  let ?t' = "node_bal_r ?l' (snd(delete_max ?l)) ?r"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   357
  from `avl t` and Node_Node have "avl ?r" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   358
  from `avl t` and Node_Node have "avl ?l" by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   359
  hence "avl(?l')"  by (rule avl_delete_max,simp)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   360
  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
   361
  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
   362
  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
   363
  proof(cases "height ?r = height ?l' + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   364
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   365
    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
   366
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   367
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   368
    show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   369
    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
   370
      case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   371
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   372
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   373
      case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   374
      thus ?thesis using l'_height t_height True by arith
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   375
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   376
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   377
  thus ?thesis using Node_Node by (auto split:prod.splits)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   378
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   379
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   380
text{* Deletion maintains the AVL property: *}
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   381
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   382
theorem avl_delete_aux:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   383
  assumes "avl t" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   384
  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
   385
using assms
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   386
proof (induct t)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   387
  case (Node h l n r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   388
  case 1
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   389
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   390
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   391
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   392
    with Node 1 show ?thesis by (auto simp:avl_delete_root)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   393
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   394
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   395
    with Node 1 show ?thesis 
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
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   398
      with Node 1 show ?thesis by (auto simp add:avl_node_bal_r)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   399
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   400
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   401
      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
   402
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   403
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   404
  case 2
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   405
  with Node show ?case
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   406
  proof(cases "x = n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   407
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   408
    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
   409
      \<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
   410
      by (subst height_delete_root,simp_all)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   411
    with True show ?thesis by simp
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   412
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   413
    case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   414
    with Node 1 show ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   415
     proof(cases "x<n")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   416
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   417
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   418
      proof(cases "height r = height (delete x l) + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   419
        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
   420
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   421
        case True 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   422
        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
   423
          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
   424
          using Node 2 by (intro height_node_bal_r) auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   425
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   426
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   427
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   428
          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   429
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   430
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   431
          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   432
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   433
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   434
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   435
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   436
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   437
      proof(cases "height l = height (delete x r) + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   438
        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
   439
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   440
        case True 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   441
        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
   442
          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
   443
          using Node 2 by (intro height_node_bal_l) auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   444
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   445
        proof
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   446
          assume ?A
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   447
          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
   448
        next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   449
          assume ?B
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   450
          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
   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
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   454
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   455
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   456
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   457
end