src/HOL/Data_Structures/AVL_Set.thy
author wenzelm
Fri, 07 Aug 2020 11:46:14 +0200
changeset 72110 16fab31feadc
parent 71818 986d5abbe77c
permissions -rw-r--r--
avoid failure of "isabelle build -o skip_proofs";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
     1
(* Author: Tobias Nipkow *)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     2
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
     3
subsection \<open>Invariant\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     4
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     5
theory AVL_Set
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
     6
imports
71795
8ad9b2d3dd81 split AVL_Set.thy
nipkow
parents: 71722
diff changeset
     7
  AVL_Set_Code
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 63411
diff changeset
     8
  "HOL-Number_Theory.Fib"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
     9
begin
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    10
71818
nipkow
parents: 71817
diff changeset
    11
fun avl :: "'a tree_ht \<Rightarrow> bool" where
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    12
"avl Leaf = True" |
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    13
"avl (Node l (a,n) r) =
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    14
 (abs(int(height l) - int(height r)) \<le> 1 \<and>
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    15
  n = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    16
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
    17
subsubsection \<open>Insertion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    18
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    19
declare Let_def [simp]
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    20
70571
e72daea2aab6 tuned names
nipkow
parents: 70350
diff changeset
    21
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
    22
by (cases t rule: tree2_cases) simp_all
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    23
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    24
text \<open>First, a fast but relatively manual proof with many lemmas:\<close>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    25
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    26
lemma height_balL:
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    27
  "\<lbrakk> avl l; avl r; height l = height r + 2 \<rbrakk> \<Longrightarrow>
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    28
   height (balL l a r) \<in> {height r + 2, height r + 3}"
71818
nipkow
parents: 71817
diff changeset
    29
by (auto simp:node_def balL_def split:tree.split)
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    30
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    31
lemma height_balR:
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    32
  "\<lbrakk> avl l; avl r; height r = height l + 2 \<rbrakk> \<Longrightarrow>
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    33
   height (balR l a r) : {height l + 2, height l + 3}"
71818
nipkow
parents: 71817
diff changeset
    34
by(auto simp add:node_def balR_def split:tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    35
70571
e72daea2aab6 tuned names
nipkow
parents: 70350
diff changeset
    36
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
    37
by (simp add: node_def)
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    38
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    39
lemma height_balL2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    40
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    41
   height (balL l a r) = 1 + max (height l) (height r)"
71817
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
    42
by (simp_all add: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    43
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    44
lemma height_balR2:
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    45
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    46
   height (balR l a r) = 1 + max (height l) (height r)"
71817
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
    47
by (simp_all add: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    48
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    49
lemma avl_balL: 
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    50
  "\<lbrakk> avl l; avl r; height r - 1 \<le> height l \<and> height l \<le> height r + 2 \<rbrakk> \<Longrightarrow> avl(balL l a r)"
71817
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
    51
by(auto simp: balL_def node_def split!: if_split tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    52
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    53
lemma avl_balR: 
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
    54
  "\<lbrakk> avl l; avl r; height l - 1 \<le> height r \<and> height r \<le> height l + 2 \<rbrakk> \<Longrightarrow> avl(balR l a r)"
71817
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
    55
by(auto simp: balR_def node_def split!: if_split tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    56
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
    57
text\<open>Insertion maintains the AVL property. Requires simultaneous proof.\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    58
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    59
theorem avl_insert:
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    60
  "avl t \<Longrightarrow> avl(insert x t)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    61
  "avl t \<Longrightarrow> height (insert x t) \<in> {height t, height t + 1}"
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
    62
proof (induction t rule: tree2_induct)
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
    63
  case (Node l a _ r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    64
  case 1
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    65
  show ?case
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    66
  proof(cases "x = a")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    67
    case True with 1 show ?thesis by simp
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    68
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    69
    case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    70
    show ?thesis 
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    71
    proof(cases "x<a")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    72
      case True with 1 Node(1,2) show ?thesis by (auto intro!:avl_balL)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    73
    next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    74
      case False with 1 Node(3,4) \<open>x\<noteq>a\<close> show ?thesis by (auto intro!:avl_balR)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    75
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    76
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    77
  case 2
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    78
  show ?case
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    79
  proof(cases "x = a")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    80
    case True with 2 show ?thesis by simp
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    81
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    82
    case False
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    83
    show ?thesis 
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    84
    proof(cases "x<a")
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    85
      case True
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    86
      show ?thesis
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    87
      proof(cases "height (insert x l) = height r + 2")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    88
        case False with 2 Node(1,2) \<open>x < a\<close> show ?thesis by (auto simp: height_balL2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    89
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    90
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    91
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    92
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    93
          using 2 Node(1,2) height_balL[OF _ _ True] by simp
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    94
        thus ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    95
        proof
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
    96
          assume ?A with 2 \<open>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    97
        next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
    98
          assume ?B with 2 Node(2) True \<open>x < a\<close> show ?thesis by (simp) arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
    99
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   100
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   101
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   102
      case False
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   103
      show ?thesis
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   104
      proof(cases "height (insert x r) = height l + 2")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   105
        case False with 2 Node(3,4) \<open>\<not>x < a\<close> show ?thesis by (auto simp: height_balR2)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   106
      next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   107
        case True 
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   108
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
   109
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   110
          using 2 Node(3) height_balR[OF _ _ True] by simp
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   111
        thus ?thesis
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   112
        proof
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   113
          assume ?A with 2 \<open>\<not>x < a\<close> show ?thesis by (auto)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   114
        next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   115
          assume ?B with 2 Node(4) True \<open>\<not>x < a\<close> show ?thesis by (simp) arith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   116
        qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   117
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   118
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   119
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   120
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   121
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   122
text \<open>Now an automatic proof without lemmas:\<close>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   123
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   124
theorem avl_insert_auto: "avl t \<Longrightarrow>
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   125
  avl(insert x t) \<and> height (insert x t) \<in> {height t, height t + 1}"
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   126
apply (induction t rule: tree2_induct)
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   127
 (* if you want to save a few secs: apply (auto split!: if_split) *)
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   128
 apply (auto simp: balL_def balR_def node_def max_absorb2 split!: if_split tree.split)
71635
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   129
done
b36f07d28867 automated proof
nipkow
parents: 71487
diff changeset
   130
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   131
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   132
subsubsection \<open>Deletion maintains AVL balance\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   133
68023
75130777ece4 del_max -> split_max
nipkow
parents: 67967
diff changeset
   134
lemma avl_split_max:
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   135
  "\<lbrakk> avl t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   136
  avl (fst (split_max t)) \<and>
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   137
  height t \<in> {height(fst (split_max t)), height(fst (split_max t)) + 1}"
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   138
by(induct t rule: split_max_induct)
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   139
  (auto simp: balL_def node_def max_absorb2 split!: prod.split if_split tree.split)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   140
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66453
diff changeset
   141
text\<open>Deletion maintains the AVL property:\<close>
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   142
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   143
theorem avl_delete:
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   144
  "avl t \<Longrightarrow> avl(delete x t)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   145
  "avl t \<Longrightarrow> height t \<in> {height (delete x t), height (delete x t) + 1}"
70755
3fb16bed5d6c replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents: 70585
diff changeset
   146
proof (induct t rule: tree2_induct)
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   147
  case (Node l a n r)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   148
  case 1
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   149
  show ?case
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   150
  proof(cases "x = a")
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   151
    case True thus ?thesis
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   152
      using 1 avl_split_max[of l] by (auto intro!: avl_balR split: prod.split)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   153
  next
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   154
    case False thus ?thesis
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   155
      using Node 1 by (auto intro!: avl_balL avl_balR)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   156
  qed
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   157
  case 2
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   158
  show ?case
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   159
  proof(cases "x = a")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   160
    case True thus ?thesis using 2 avl_split_max[of l]
71636
9d8fb1dbc368 simplified code and proofs
nipkow
parents: 71635
diff changeset
   161
      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
   162
  next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   163
    case False
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   164
    show ?thesis
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   165
    proof(cases "x<a")
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   166
      case True
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   167
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   168
      proof(cases "height r = height (delete x l) + 2")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   169
        case False
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   170
        thus ?thesis using 2 Node(1,2) \<open>x < a\<close> by(auto simp: balR_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   171
      next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   172
        case True
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   173
        thus ?thesis using height_balR[OF _ _ True, of a] 2 Node(1,2) \<open>x < a\<close> by simp linarith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   174
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   175
    next
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   176
      case False
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   177
      show ?thesis
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   178
      proof(cases "height l = height (delete x r) + 2")
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   179
        case False
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   180
        thus ?thesis using 2 Node(3,4) \<open>\<not>x < a\<close> \<open>x \<noteq> a\<close> by(auto simp: balL_def)
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   181
      next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   182
        case True
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   183
        thus ?thesis
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   184
          using height_balL[OF _ _ True, of a] 2 Node(3,4) \<open>\<not>x < a\<close> \<open>x \<noteq> a\<close> by simp linarith
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   185
      qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   186
    qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   187
  qed
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   188
qed simp_all
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   189
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   190
text \<open>A more automatic proof.
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   191
Complete automation as for insertion seems hard due to resource requirements.\<close>
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   192
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   193
theorem avl_delete_auto:
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   194
  "avl t \<Longrightarrow> avl(delete x t)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   195
  "avl t \<Longrightarrow> height t \<in> {height (delete x t), height (delete x t) + 1}"
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   196
proof (induct t rule: tree2_induct)
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   197
  case (Node l a n r)
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   198
  case 1
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   199
  thus ?case
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   200
    using Node avl_split_max[of l] by (auto intro!: avl_balL avl_balR split: prod.split)
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   201
  case 2
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   202
  show ?case
71817
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
   203
    using 2 Node avl_split_max[of l]
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
   204
      by auto
e8e0313881b9 tuned proofs
nipkow
parents: 71806
diff changeset
   205
         (auto simp: balL_def balR_def max_absorb1 max_absorb2  split!: tree.splits prod.splits if_splits)
71722
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   206
qed simp_all
1cffe8f4d7b3 more automation and clarification
nipkow
parents: 71636
diff changeset
   207
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   208
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   209
subsection "Overall correctness"
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   210
68440
6826718f732d qualify interpretations to avoid clashes
nipkow
parents: 68431
diff changeset
   211
interpretation S: Set_by_Ordered
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   212
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
   213
and inorder = inorder and inv = avl
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   214
proof (standard, goal_cases)
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   215
  case 1 show ?case by (simp add: empty_def)
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   216
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   217
  case 2 thus ?case by(simp add: isin_set_inorder)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   218
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   219
  case 3 thus ?case by(simp add: inorder_insert)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   220
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   221
  case 4 thus ?case by(simp add: inorder_delete)
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   222
next
68431
b294e095f64c more abstract naming
nipkow
parents: 68422
diff changeset
   223
  case 5 thus ?case by (simp add: empty_def)
68422
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   224
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   225
  case 6 thus ?case by (simp add: avl_insert(1))
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   226
next
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   227
  case 7 thus ?case by (simp add: avl_delete(1))
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   228
qed
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   229
0a3a36fa1d63 proved avl for map (finally); tuned
nipkow
parents: 68413
diff changeset
   230
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   231
subsection \<open>Height-Size Relation\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   232
71466
ac70b63785bb tuned var names (avoid h)
nipkow
parents: 70755
diff changeset
   233
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
   234
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   235
theorem avl_fib_bound:
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   236
  "avl t \<Longrightarrow> fib(height t + 2) \<le> size1 t"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   237
proof (induction rule: tree2_induct)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   238
  case (Node l a h r)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   239
  have 1: "height l + 1 \<le> height r + 2" and 2: "height r + 1 \<le> height l + 2"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   240
    using Node.prems by auto
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   241
  have "fib (max (height l) (height r) + 3) \<le> size1 l + size1 r"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   242
  proof cases
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   243
    assume "height l \<ge> height r"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   244
    hence "fib (max (height l) (height r) + 3) = fib (height l + 3)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   245
      by(simp add: max_absorb1)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   246
    also have "\<dots> = fib (height l + 2) + fib (height l + 1)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   247
      by (simp add: numeral_eq_Suc)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   248
    also have "\<dots> \<le> size1 l + fib (height l + 1)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   249
      using Node by (simp)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   250
    also have "\<dots> \<le> size1 r + size1 l"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   251
      using Node fib_mono[OF 1] by auto
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   252
    also have "\<dots> = size1 (Node l (a,h) r)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   253
      by simp
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   254
    finally show ?thesis 
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   255
      by (simp)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   256
  next
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   257
    assume "\<not> height l \<ge> height r"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   258
    hence "fib (max (height l) (height r) + 3) = fib (height r + 3)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   259
      by(simp add: max_absorb1)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   260
    also have "\<dots> = fib (height r + 2) + fib (height r + 1)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   261
      by (simp add: numeral_eq_Suc)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   262
    also have "\<dots> \<le> size1 r + fib (height r + 1)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   263
      using Node by (simp)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   264
    also have "\<dots> \<le> size1 r + size1 l"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   265
      using Node fib_mono[OF 2] by auto
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   266
    also have "\<dots> = size1 (Node l (a,h) r)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   267
      by simp
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   268
    finally show ?thesis 
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   269
      by (simp)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   270
  qed
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   271
  also have "\<dots> = size1 (Node l (a,h) r)"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   272
    by simp
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   273
  finally show ?case by (simp del: fib.simps add: numeral_eq_Suc)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   274
qed auto
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   275
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   276
lemma avl_fib_bound_auto: "avl t \<Longrightarrow> fib (height t + 2) \<le> size1 t"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   277
proof (induction t rule: tree2_induct)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   278
  case Leaf thus ?case by (simp)
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   279
next
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   280
  case (Node l a h r)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   281
  have 1: "height l + 1 \<le> height r + 2" and 2: "height r + 1 \<le> height l + 2"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   282
    using Node.prems by auto
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   283
  have left: "height l \<ge> height r \<Longrightarrow> ?case" (is "?asm \<Longrightarrow> _")
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   284
    using Node fib_mono[OF 1] by (simp add: max.absorb1)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   285
  have right: "height l \<le> height r \<Longrightarrow> ?case"
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   286
    using Node fib_mono[OF 2] by (simp add: max.absorb2)
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   287
  show ?case using left right using Node.prems by simp linarith
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   288
qed
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   289
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   290
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
   291
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   292
lemma fib_lowerbound:
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   293
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   294
  shows "real (fib(n+2)) \<ge> \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   295
proof (induction n rule: fib.induct)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   296
  case 1
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   297
  then show ?case by simp
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   298
next
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   299
  case 2
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   300
  then show ?case by (simp add: \<phi>_def real_le_lsqrt)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   301
next
72110
16fab31feadc avoid failure of "isabelle build -o skip_proofs";
wenzelm
parents: 71818
diff changeset
   302
  case (3 n)
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   303
  have "\<phi> ^ Suc (Suc n) = \<phi> ^ 2 * \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   304
    by (simp add: field_simps power2_eq_square)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   305
  also have "\<dots> = (\<phi> + 1) * \<phi> ^ n"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   306
    by (simp_all add: \<phi>_def power2_eq_square field_simps)
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   307
  also have "\<dots> = \<phi> ^ Suc n + \<phi> ^ n"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   308
      by (simp add: field_simps)
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   309
  also have "\<dots> \<le> real (fib (Suc n + 2)) + real (fib (n + 2))"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   310
      by (intro add_mono "3.IH")
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   311
  finally show ?case by simp
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   312
qed
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   314
text \<open>The size of an AVL tree is (at least) exponential in its height:\<close>
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   315
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   316
lemma avl_size_lowerbound:
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   317
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   318
  assumes "avl t"
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   319
  shows   "\<phi> ^ (height t) \<le> size1 t"
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   320
proof -
71486
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   321
  have "\<phi> ^ height t \<le> fib (height t + 2)"
0e1b9b308d8f simplified proofs
nipkow
parents: 71466
diff changeset
   322
    unfolding \<phi>_def by(rule fib_lowerbound)
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   323
  also have "\<dots> \<le> size1 t"
71806
884c6c0bc99a use abs(h l - h r) instead of 3 cases, tuned proofs
nipkow
parents: 71801
diff changeset
   324
    using avl_fib_bound[of t] assms by simp
68313
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   325
  finally show ?thesis .
56c57e91edf9 slicker proof
nipkow
parents: 68023
diff changeset
   326
qed
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   327
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68440
diff changeset
   328
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
   329
than \<^term>\<open>log 2 (size1 t)\<close>:\<close>
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   330
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   331
lemma  avl_height_upperbound:
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   332
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   333
  assumes "avl t"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   334
  shows   "height t \<le> (1/log 2 \<phi>) * log 2 (size1 t)"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   335
proof -
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   336
  have "\<phi> > 0" "\<phi> > 1" by(auto simp: \<phi>_def pos_add_strict)
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   337
  hence "height t = log \<phi> (\<phi> ^ height t)" by(simp add: log_nat_power)
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   338
  also have "\<dots> \<le> log \<phi> (size1 t)"
70350
571ae57313a4 moved some theorems into HOL main corpus
haftmann
parents: 69597
diff changeset
   339
    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
   340
    by (simp add: le_log_of_power) 
68342
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   341
  also have "\<dots> = (1/log 2 \<phi>) * log 2 (size1 t)"
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   342
    by(simp add: log_base_change[of 2 \<phi>])
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   343
  finally show ?thesis .
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   344
qed
b80734daf7ed added lemma
nipkow
parents: 68313
diff changeset
   345
61232
c46faf9762f7 added AVL and lookup function
nipkow
parents:
diff changeset
   346
end