src/HOL/Data_Structures/AVL_Set.thy
author nipkow
Wed, 01 Apr 2020 21:48:39 +0200
changeset 71636 9d8fb1dbc368
parent 71635 b36f07d28867
child 71722 1cffe8f4d7b3
permissions -rw-r--r--
simplified code and proofs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     1
(*
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     2
Author:     Tobias Nipkow, Daniel Stüwe
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
     3
Based on the AFP entry AVL.
61232
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
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     9
imports
67964
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67406
diff changeset
    10
  Cmp
08cc5ab18c84 better name; added binary operations
nipkow
parents: 67406
diff changeset
    11
  Isin2
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 63411
diff changeset
    12
  "HOL-Number_Theory.Fib"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    13
begin
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    14
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
    15
type_synonym 'a avl_tree = "('a*nat) tree"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    16
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
    17
definition empty :: "'a avl_tree" where
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
    18
"empty = Leaf"
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
    19
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    20
text \<open>Invariant:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    21
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    22
fun avl :: "'a avl_tree \<Rightarrow> bool" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    23
"avl Leaf = True" |
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    24
"avl (Node l (a,n) r) =
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    25
 ((height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1) \<and> 
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    26
  n = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    27
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    28
fun ht :: "'a avl_tree \<Rightarrow> nat" where
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    29
"ht Leaf = 0" |
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    30
"ht (Node l (a,n) r) = n"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    31
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    32
definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
    33
"node l a r = Node l (a, max (ht l) (ht r) + 1) r"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    34
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    35
definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    36
"balL AB b C =
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    37
  (if ht AB = ht C + 2 then
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    38
     case AB of 
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    39
       Node A (a, _) B \<Rightarrow>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    40
         if ht A \<ge> ht B then node A a (node B b C)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    41
         else
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    42
           case B of
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    43
             Node B\<^sub>1 (ab, _) B\<^sub>2 \<Rightarrow> node (node A a B\<^sub>1) ab (node B\<^sub>2 b C)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    44
   else node AB b C)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    45
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    46
definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    47
"balR A a BC =
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    48
   (if ht BC = ht A + 2 then
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    49
      case BC of
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    50
        Node B (b, _) C \<Rightarrow>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    51
          if ht B \<le> ht C then node (node A a B) b C
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    52
          else
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    53
            case B of
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    54
              Node B\<^sub>1 (ab, _) B\<^sub>2 \<Rightarrow> node (node A a B\<^sub>1) ab (node B\<^sub>2 b C)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    55
  else node A a BC)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    56
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    57
fun insert :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
    58
"insert x Leaf = Node Leaf (x, 1) Leaf" |
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    59
"insert x (Node l (a, n) r) = (case cmp x a of
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    60
   EQ \<Rightarrow> Node l (a, n) r |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    61
   LT \<Rightarrow> balL (insert x l) a r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    62
   GT \<Rightarrow> balR l a (insert x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    63
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
    64
fun split_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
    65
"split_max (Node l (a, _) r) =
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
    66
  (if r = Leaf then (l,a) else let (r',a') = split_max r in (balL l a r', a'))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    67
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
    68
lemmas split_max_induct = split_max.induct[case_names Node Leaf]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    69
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    70
fun delete :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    71
"delete _ Leaf = Leaf" |
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    72
"delete x (Node l (a, n) r) =
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    73
  (case cmp x a of
71636
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
    74
     EQ \<Rightarrow> if l = Leaf then r
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
    75
           else let (l', a') = split_max l in balR l' a' r |
61678
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    76
     LT \<Rightarrow> balR (delete x l) a r |
b594e9277be3 tuned white space
nipkow
parents: 61647
diff changeset
    77
     GT \<Rightarrow> balL l a (delete x r))"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    78
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    79
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    80
subsection \<open>Functional Correctness Proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    81
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    82
text\<open>Very different from the AFP/AVL proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    83
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    84
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    85
subsubsection "Proofs for insert"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    86
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    87
lemma inorder_balL:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    88
  "inorder (balL l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    89
by (auto simp: node_def balL_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    90
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    91
lemma inorder_balR:
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    92
  "inorder (balR l a r) = inorder l @ a # inorder r"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    93
by (auto simp: node_def balR_def split:tree.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    94
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    95
theorem inorder_insert:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    96
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    97
by (induct t) 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    98
   (auto simp: ins_list_simps inorder_balL inorder_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    99
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   100
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   101
subsubsection "Proofs for delete"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   102
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   103
lemma inorder_split_maxD:
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   104
  "\<lbrakk> split_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   105
   inorder t' @ [a] = inorder t"
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   106
by(induction t arbitrary: t' rule: split_max.induct)
61647
nipkow
parents: 61588
diff changeset
   107
  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   108
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   109
theorem inorder_delete:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   110
  "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   111
by(induction t)
71636
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   112
  (auto simp: del_list_simps inorder_balL inorder_balR inorder_split_maxD split: prod.splits)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   113
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   114
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   115
subsection \<open>AVL invariants\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   116
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   117
text\<open>Essentially the AFP/AVL proofs\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   118
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   119
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   120
subsubsection \<open>Insertion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   121
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   122
declare Let_def [simp]
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   123
70571
e72daea2aab6 tuned names
nipkow
parents: 70350
diff changeset
   124
lemma ht_height[simp]: "avl t \<Longrightarrow> ht t = height t"
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   125
by (cases t rule: tree2_cases) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   126
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   127
text \<open>First, a fast but relatively manual proof with many lemmas:\<close>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   128
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   129
lemma height_balL:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   130
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   131
   height (balL l a r) = height r + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   132
   height (balL l a r) = height r + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   133
by (cases l) (auto simp:node_def balL_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   134
       
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   135
lemma height_balR:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   136
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   137
   height (balR l a r) = height l + 2 \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   138
   height (balR l a r) = height l + 3"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   139
by (cases r) (auto simp add:node_def balR_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   140
70571
e72daea2aab6 tuned names
nipkow
parents: 70350
diff changeset
   141
lemma height_node[simp]: "height(node l a r) = max (height l) (height r) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   142
by (simp add: node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   143
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   144
lemma avl_node:
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   145
  "\<lbrakk> avl l; avl r;
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   146
     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
   147
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   148
by (auto simp add:max_def node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   149
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   150
lemma height_balL2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   151
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   152
   height (balL l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   153
by (cases l, cases r) (simp_all add: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   154
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   155
lemma height_balR2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   156
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   157
   height (balR l a r) = (1 + max (height l) (height r))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   158
by (cases l, cases r) (simp_all add: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   159
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   160
lemma avl_balL: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   161
  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
   162
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   163
  shows "avl(balL l a r)"
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   164
proof(cases l rule: tree2_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   165
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   166
  with assms show ?thesis by (simp add: node_def balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   167
next
68413
b56ed5010e69 tuned order of arguments
nipkow
parents: 68342
diff changeset
   168
  case Node
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   169
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   170
  proof(cases "height l = height r + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   171
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   172
    from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   173
      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   174
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   175
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   176
    with assms show ?thesis by (simp add: avl_node balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   177
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   178
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   179
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   180
lemma avl_balR: 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   181
  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
   182
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   183
  shows "avl(balR l a r)"
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   184
proof(cases r rule: tree2_cases)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   185
  case Leaf
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   186
  with assms show ?thesis by (simp add: node_def balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   187
next
68413
b56ed5010e69 tuned order of arguments
nipkow
parents: 68342
diff changeset
   188
  case Node
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   189
  with assms show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   190
  proof(cases "height r = height l + 2")
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   191
    case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   192
      from True Node assms show ?thesis
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   193
        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   194
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   195
    case False
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   196
    with assms show ?thesis by (simp add: balR_def avl_node)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   197
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   198
qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   199
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   200
text\<open>Insertion maintains the AVL property. Requires simultaneous proof.\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   201
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   202
theorem avl_insert:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   203
  assumes "avl t"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   204
  shows "avl(insert x t)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   205
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   206
using assms
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   207
proof (induction t rule: tree2_induct)
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   208
  case (Node l a _ r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   209
  case 1
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   210
  show ?case
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   211
  proof(cases "x = a")
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   212
    case True with Node 1 show ?thesis by simp
61232
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
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   215
    show ?thesis 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   216
    proof(cases "x<a")
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   217
      case True with Node 1 show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   218
    next
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   219
      case False with Node 1 \<open>x\<noteq>a\<close> show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   220
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   221
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   222
  case 2
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   223
  show ?case
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   224
  proof(cases "x = a")
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   225
    case True with Node 1 show ?thesis by simp
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   226
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   227
    case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   228
    show ?thesis 
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   229
    proof(cases "x<a")
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   230
      case True
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   231
      show ?thesis
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   232
      proof(cases "height (insert x l) = height r + 2")
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   233
        case False with Node 2 \<open>x < a\<close> show ?thesis by (auto simp: height_balL2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   234
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   235
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   236
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   237
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   238
          using Node 2 by (intro height_balL) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   239
        thus ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   240
        proof
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   241
          assume ?A with 2 \<open>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   242
        next
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   243
          assume ?B with True 1 Node(2) \<open>x < a\<close> show ?thesis by (simp) arith
61232
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
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   247
      case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   248
      show ?thesis 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   249
      proof(cases "height (insert x r) = height l + 2")
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   250
        case False with Node 2 \<open>\<not>x < a\<close> show ?thesis by (auto simp: height_balR2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   251
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   252
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   253
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   254
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   255
          using Node 2 by (intro height_balR) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   256
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   257
        proof
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   258
          assume ?A with 2 \<open>\<not>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   259
        next
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   260
          assume ?B with True 1 Node(4) \<open>\<not>x < a\<close> show ?thesis by (simp) arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   261
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   262
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   263
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   264
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   265
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   266
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   267
text \<open>Now an automatic proof without lemmas:\<close>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   268
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   269
theorem avl_insert_auto: "avl t \<Longrightarrow>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   270
  avl(insert x t) \<and> height (insert x t) \<in> {height t, height t + 1}"
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   271
apply (induction t rule: tree2_induct)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   272
 apply (auto split!: if_splits)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   273
 apply (auto simp: balL_def balR_def node_def max_absorb2 split!: tree.splits)
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   274
done
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   275
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   276
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   277
subsubsection \<open>Deletion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   278
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   279
lemma avl_split_max:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   280
  assumes "avl x" and "x \<noteq> Leaf"
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   281
  shows "avl (fst (split_max x))" "height x = height(fst (split_max x)) \<or>
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   282
         height x = height(fst (split_max x)) + 1"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   283
using assms
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   284
proof (induct x rule: split_max_induct)
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   285
  case Node
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   286
  case 1
61647
nipkow
parents: 61588
diff changeset
   287
  thus ?case using Node
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   288
    by (auto simp: height_balL height_balL2 avl_balL split:prod.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   289
next
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   290
  case (Node l a _ r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   291
  case 2
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   292
  let ?r' = "fst (split_max r)"
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   293
  from \<open>avl x\<close> Node 2 have "avl l" and "avl r" by simp_all
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   294
  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   295
    apply (auto split:prod.splits simp del:avl.simps) by arith+
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   296
qed auto
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   297
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   298
text\<open>Deletion maintains the AVL property:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   299
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   300
theorem avl_delete:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   301
  assumes "avl t" 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   302
  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
   303
using assms
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   304
proof (induct t rule: tree2_induct)
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   305
  case (Node l a n r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   306
  case 1
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   307
  show ?case
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   308
  proof(cases "x = a")
71636
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   309
    case True with Node 1 show ?thesis
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   310
      using avl_split_max[of l] by (auto simp: avl_balR split: prod.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   311
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   312
    case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   313
    show ?thesis 
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   314
    proof(cases "x<a")
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   315
      case True with Node 1 show ?thesis by (auto simp add:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   316
    next
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   317
      case False with Node 1 \<open>x\<noteq>a\<close> show ?thesis by (auto simp add:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   318
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   319
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   320
  case 2
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   321
  show ?case
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   322
  proof(cases "x = a")
71636
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   323
    case True then show ?thesis using 1 avl_split_max[of l]
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   324
      by(auto simp: balR_def max_absorb2 split!: if_splits prod.split tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   325
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   326
    case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   327
    show ?thesis 
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   328
    proof(cases "x<a")
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   329
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   330
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   331
      proof(cases "height r = height (delete x l) + 2")
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   332
        case False with Node 1 \<open>x < a\<close> show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   333
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   334
        case True 
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   335
        hence "(height (balR (delete x l) a r) = height (delete x l) + 2) \<or>
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   336
          height (balR (delete x l) a r) = height (delete x l) + 3" (is "?A \<or> ?B")
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   337
          using Node 2 by (intro height_balR) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   338
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   339
        proof
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   340
          assume ?A with \<open>x < a\<close> Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   341
        next
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   342
          assume ?B with \<open>x < a\<close> Node 2 show ?thesis by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   343
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   344
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   345
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   346
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   347
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   348
      proof(cases "height l = height (delete x r) + 2")
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   349
        case False with Node 1 \<open>\<not>x < a\<close> \<open>x \<noteq> a\<close> show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   350
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   351
        case True 
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   352
        hence "(height (balL l a (delete x r)) = height (delete x r) + 2) \<or>
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   353
          height (balL l a (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   354
          using Node 2 by (intro height_balL) auto
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   355
        thus ?thesis 
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   356
        proof
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   357
          assume ?A with \<open>\<not>x < a\<close> \<open>x \<noteq> a\<close> Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   358
        next
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   359
          assume ?B with \<open>\<not>x < a\<close> \<open>x \<noteq> a\<close> Node 2 show ?thesis by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   360
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   361
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   362
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   363
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   364
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   365
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   366
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   367
subsection "Overall correctness"
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   368
68440
6826718f732d qualify interpretations to avoid clashes
nipkow
parents: 68431
diff changeset
   369
interpretation S: Set_by_Ordered
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   370
where empty = empty and isin = isin and insert = insert and delete = delete
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   371
and inorder = inorder and inv = avl
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   372
proof (standard, goal_cases)
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   373
  case 1 show ?case by (simp add: empty_def)
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   374
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   375
  case 2 thus ?case by(simp add: isin_set_inorder)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   376
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   377
  case 3 thus ?case by(simp add: inorder_insert)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   378
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   379
  case 4 thus ?case by(simp add: inorder_delete)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   380
next
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   381
  case 5 thus ?case by (simp add: empty_def)
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   382
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   383
  case 6 thus ?case by (simp add: avl_insert(1))
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   384
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   385
  case 7 thus ?case by (simp add: avl_delete(1))
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   386
qed
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   387
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   388
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   389
subsection \<open>Height-Size Relation\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   390
71487
059c55b61734 removed dead lemma
nipkow
parents: 71486
diff changeset
   391
text \<open>Based on theorems by Daniel St\"uwe, Manuel Eberl and Peter Lammich, much simplified.\<close>
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   392
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   393
text \<open>Any AVL tree of height \<open>n\<close> has at least \<open>fib (n+2)\<close> leaves:\<close>
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   394
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   395
lemma avl_fib_bound: "avl t \<Longrightarrow> height t = n \<Longrightarrow> fib (n+2) \<le> size1 t"
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   396
proof (induction n arbitrary: t rule: fib.induct)
71487
059c55b61734 removed dead lemma
nipkow
parents: 71486
diff changeset
   397
  case 1 thus ?case by (simp)
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   398
next
71487
059c55b61734 removed dead lemma
nipkow
parents: 71486
diff changeset
   399
  case 2 thus ?case by (cases t) (auto)
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   400
next
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   401
  case (3 h)
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   402
  from "3.prems" obtain l a r where
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   403
    [simp]: "t = Node l (a,Suc(Suc h)) r" "avl l" "avl r"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   404
    and C: "
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   405
      height r = Suc h \<and> height l = Suc h
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   406
    \<or> height r = Suc h \<and> height l = h
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   407
    \<or> height r = h \<and> height l = Suc h" (is "?C1 \<or> ?C2 \<or> ?C3")
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   408
    by (cases t) (simp, fastforce)
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   409
  {
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   410
    assume ?C1
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   411
    with "3.IH"(1)
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   412
    have "fib (h + 3) \<le> size1 l" "fib (h + 3) \<le> size1 r"
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   413
      by (simp_all add: eval_nat_numeral)
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   414
    hence ?case by (auto simp: eval_nat_numeral)
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   415
  } moreover {
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   416
    assume ?C2
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   417
    hence ?case using "3.IH"(1)[of r] "3.IH"(2)[of l] by auto
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   418
  } moreover {
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   419
    assume ?C3
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   420
    hence ?case using "3.IH"(1)[of l] "3.IH"(2)[of r] by auto
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   421
  } ultimately show ?case using C by blast
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   422
qed
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   423
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   424
text \<open>An exponential lower bound for \<^const>\<open>fib\<close>:\<close>
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   425
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   426
lemma fib_lowerbound:
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   427
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   428
  shows "real (fib(n+2)) \<ge> \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   429
proof (induction n rule: fib.induct)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   430
  case 1
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   431
  then show ?case by simp
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   432
next
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   433
  case 2
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   434
  then show ?case by (simp add: \<phi>_def real_le_lsqrt)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   435
next
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   436
  case (3 n) term ?case
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   437
  have "\<phi> ^ Suc (Suc n) = \<phi> ^ 2 * \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   438
    by (simp add: field_simps power2_eq_square)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   439
  also have "\<dots> = (\<phi> + 1) * \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   440
    by (simp_all add: \<phi>_def power2_eq_square field_simps)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   441
  also have "\<dots> = \<phi> ^ Suc n + \<phi> ^ n"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   442
      by (simp add: field_simps)
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   443
  also have "\<dots> \<le> real (fib (Suc n + 2)) + real (fib (n + 2))"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   444
      by (intro add_mono "3.IH")
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   445
  finally show ?case by simp
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   446
qed
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   447
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   448
text \<open>The size of an AVL tree is (at least) exponential in its height:\<close>
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   449
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   450
lemma avl_size_lowerbound:
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   451
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   452
  assumes "avl t"
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   453
  shows   "\<phi> ^ (height t) \<le> size1 t"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   454
proof -
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   455
  have "\<phi> ^ height t \<le> fib (height t + 2)"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   456
    unfolding \<phi>_def by(rule fib_lowerbound)
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   457
  also have "\<dots> \<le> size1 t"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   458
    using avl_fib_bound[of t "height t"] assms by simp
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   459
  finally show ?thesis .
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   460
qed
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   461
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   462
text \<open>The height of an AVL tree is most \<^term>\<open>(1/log 2 \<phi>)\<close> \<open>\<approx> 1.44\<close> times worse
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   463
than \<^term>\<open>log 2 (size1 t)\<close>:\<close>
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   464
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   465
lemma  avl_height_upperbound:
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   466
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   467
  assumes "avl t"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   468
  shows   "height t \<le> (1/log 2 \<phi>) * log 2 (size1 t)"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   469
proof -
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   470
  have "\<phi> > 0" "\<phi> > 1" by(auto simp: \<phi>_def pos_add_strict)
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   471
  hence "height t = log \<phi> (\<phi> ^ height t)" by(simp add: log_nat_power)
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   472
  also have "\<dots> \<le> log \<phi> (size1 t)"
70350
571ae57313a4 moved some theorems into HOL main corpus
haftmann
parents: 69597
diff changeset
   473
    using avl_size_lowerbound[OF assms(2), folded \<phi>_def] \<open>1 < \<phi>\<close>
571ae57313a4 moved some theorems into HOL main corpus
haftmann
parents: 69597
diff changeset
   474
    by (simp add: le_log_of_power) 
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   475
  also have "\<dots> = (1/log 2 \<phi>) * log 2 (size1 t)"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   476
    by(simp add: log_base_change[of 2 \<phi>])
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   477
  finally show ?thesis .
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   478
qed
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   479
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   480
end