src/HOL/Data_Structures/AVL_Set.thy
author nipkow
Tue Jun 12 07:18:09 2018 +0200 (11 months ago)
changeset 68422 0a3a36fa1d63
parent 68413 b56ed5010e69
child 68431 b294e095f64c
permissions -rw-r--r--
proved avl for map (finally); tuned
nipkow@61232
     1
(*
nipkow@63411
     2
Author:     Tobias Nipkow, Daniel Stüwe
nipkow@63411
     3
Largely derived from AFP entry AVL.
nipkow@61232
     4
*)
nipkow@61232
     5
nipkow@61232
     6
section "AVL Tree Implementation of Sets"
nipkow@61232
     7
nipkow@61232
     8
theory AVL_Set
nipkow@63411
     9
imports
nipkow@67964
    10
  Cmp
nipkow@67964
    11
  Isin2
wenzelm@66453
    12
  "HOL-Number_Theory.Fib"
nipkow@61232
    13
begin
nipkow@61232
    14
nipkow@61232
    15
type_synonym 'a avl_tree = "('a,nat) tree"
nipkow@61232
    16
wenzelm@67406
    17
text \<open>Invariant:\<close>
nipkow@61232
    18
nipkow@61232
    19
fun avl :: "'a avl_tree \<Rightarrow> bool" where
nipkow@61232
    20
"avl Leaf = True" |
nipkow@68413
    21
"avl (Node l a h r) =
nipkow@61232
    22
 ((height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1) \<and> 
nipkow@61232
    23
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
nipkow@61232
    24
nipkow@61232
    25
fun ht :: "'a avl_tree \<Rightarrow> nat" where
nipkow@61232
    26
"ht Leaf = 0" |
nipkow@68413
    27
"ht (Node l a h r) = h"
nipkow@61232
    28
nipkow@61232
    29
definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@68413
    30
"node l a r = Node l a (max (ht l) (ht r) + 1) r"
nipkow@61232
    31
nipkow@61581
    32
definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61678
    33
"balL l a r =
nipkow@61678
    34
  (if ht l = ht r + 2 then
nipkow@61678
    35
     case l of 
nipkow@68413
    36
       Node bl b _ br \<Rightarrow>
nipkow@61678
    37
         if ht bl < ht br then
nipkow@61678
    38
           case br of
nipkow@68413
    39
             Node cl c _ cr \<Rightarrow> node (node bl b cl) c (node cr a r)
nipkow@61678
    40
         else node bl b (node br a r)
nipkow@61678
    41
   else node l a r)"
nipkow@61232
    42
nipkow@61581
    43
definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61678
    44
"balR l a r =
nipkow@61678
    45
   (if ht r = ht l + 2 then
nipkow@61678
    46
      case r of
nipkow@68413
    47
        Node bl b _ br \<Rightarrow>
nipkow@61678
    48
          if ht bl > ht br then
nipkow@61678
    49
            case bl of
nipkow@68413
    50
              Node cl c _ cr \<Rightarrow> node (node l a cl) c (node cr b br)
nipkow@61678
    51
          else node (node l a bl) b br
nipkow@61232
    52
  else node l a r)"
nipkow@61232
    53
nipkow@63411
    54
fun insert :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@68413
    55
"insert x Leaf = Node Leaf x 1 Leaf" |
nipkow@68413
    56
"insert x (Node l a h r) = (case cmp x a of
nipkow@68413
    57
   EQ \<Rightarrow> Node l a h r |
nipkow@61581
    58
   LT \<Rightarrow> balL (insert x l) a r |
nipkow@61581
    59
   GT \<Rightarrow> balR l a (insert x r))"
nipkow@61232
    60
nipkow@68023
    61
fun split_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
nipkow@68413
    62
"split_max (Node l a _ r) =
nipkow@68023
    63
  (if r = Leaf then (l,a) else let (r',a') = split_max r in (balL l a r', a'))"
nipkow@61232
    64
nipkow@68023
    65
lemmas split_max_induct = split_max.induct[case_names Node Leaf]
nipkow@61232
    66
nipkow@61647
    67
fun del_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@68413
    68
"del_root (Node Leaf a h r) = r" |
nipkow@68413
    69
"del_root (Node l a h Leaf) = l" |
nipkow@68413
    70
"del_root (Node l a h r) = (let (l', a') = split_max l in balR l' a' r)"
nipkow@61232
    71
nipkow@61647
    72
lemmas del_root_cases = del_root.cases[case_names Leaf_t Node_Leaf Node_Node]
nipkow@61232
    73
nipkow@63411
    74
fun delete :: "'a::linorder \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61232
    75
"delete _ Leaf = Leaf" |
nipkow@68413
    76
"delete x (Node l a h r) =
nipkow@61678
    77
  (case cmp x a of
nipkow@68413
    78
     EQ \<Rightarrow> del_root (Node l a h r) |
nipkow@61678
    79
     LT \<Rightarrow> balR (delete x l) a r |
nipkow@61678
    80
     GT \<Rightarrow> balL l a (delete x r))"
nipkow@61232
    81
nipkow@61232
    82
wenzelm@67406
    83
subsection \<open>Functional Correctness Proofs\<close>
nipkow@61232
    84
wenzelm@67406
    85
text\<open>Very different from the AFP/AVL proofs\<close>
nipkow@61232
    86
nipkow@61232
    87
nipkow@61232
    88
subsubsection "Proofs for insert"
nipkow@61232
    89
nipkow@61581
    90
lemma inorder_balL:
nipkow@61581
    91
  "inorder (balL l a r) = inorder l @ a # inorder r"
nipkow@61581
    92
by (auto simp: node_def balL_def split:tree.splits)
nipkow@61232
    93
nipkow@61581
    94
lemma inorder_balR:
nipkow@61581
    95
  "inorder (balR l a r) = inorder l @ a # inorder r"
nipkow@61581
    96
by (auto simp: node_def balR_def split:tree.splits)
nipkow@61232
    97
nipkow@61232
    98
theorem inorder_insert:
nipkow@61232
    99
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61232
   100
by (induct t) 
nipkow@61581
   101
   (auto simp: ins_list_simps inorder_balL inorder_balR)
nipkow@61232
   102
nipkow@61232
   103
nipkow@61232
   104
subsubsection "Proofs for delete"
nipkow@61232
   105
nipkow@68023
   106
lemma inorder_split_maxD:
nipkow@68023
   107
  "\<lbrakk> split_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
nipkow@61232
   108
   inorder t' @ [a] = inorder t"
nipkow@68023
   109
by(induction t arbitrary: t' rule: split_max.induct)
nipkow@61647
   110
  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
nipkow@61232
   111
nipkow@61647
   112
lemma inorder_del_root:
nipkow@68413
   113
  "inorder (del_root (Node l a h r)) = inorder l @ inorder r"
nipkow@68413
   114
by(cases "Node l a h r" rule: del_root.cases)
nipkow@68023
   115
  (auto simp: inorder_balL inorder_balR inorder_split_maxD split: if_splits prod.splits)
nipkow@61232
   116
nipkow@61232
   117
theorem inorder_delete:
nipkow@61232
   118
  "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
nipkow@61232
   119
by(induction t)
nipkow@61581
   120
  (auto simp: del_list_simps inorder_balL inorder_balR
nipkow@68023
   121
    inorder_del_root inorder_split_maxD split: prod.splits)
nipkow@61232
   122
nipkow@61232
   123
wenzelm@67406
   124
subsection \<open>AVL invariants\<close>
nipkow@61232
   125
wenzelm@67406
   126
text\<open>Essentially the AFP/AVL proofs\<close>
nipkow@61232
   127
nipkow@61232
   128
wenzelm@67406
   129
subsubsection \<open>Insertion maintains AVL balance\<close>
nipkow@61232
   130
nipkow@61232
   131
declare Let_def [simp]
nipkow@61232
   132
nipkow@61232
   133
lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
nipkow@61232
   134
by (induct t) simp_all
nipkow@61232
   135
nipkow@61581
   136
lemma height_balL:
nipkow@61232
   137
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
nipkow@61581
   138
   height (balL l a r) = height r + 2 \<or>
nipkow@61581
   139
   height (balL l a r) = height r + 3"
nipkow@61581
   140
by (cases l) (auto simp:node_def balL_def split:tree.split)
nipkow@61232
   141
       
nipkow@61581
   142
lemma height_balR:
nipkow@61232
   143
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
nipkow@61581
   144
   height (balR l a r) = height l + 2 \<or>
nipkow@61581
   145
   height (balR l a r) = height l + 3"
nipkow@61581
   146
by (cases r) (auto simp add:node_def balR_def split:tree.split)
nipkow@61232
   147
nipkow@61232
   148
lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
nipkow@61232
   149
by (simp add: node_def)
nipkow@61232
   150
nipkow@61232
   151
lemma avl_node:
nipkow@61232
   152
  "\<lbrakk> avl l; avl r;
nipkow@61232
   153
     height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1
nipkow@61232
   154
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
nipkow@61232
   155
by (auto simp add:max_def node_def)
nipkow@61232
   156
nipkow@61581
   157
lemma height_balL2:
nipkow@61232
   158
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
nipkow@61581
   159
   height (balL l a r) = (1 + max (height l) (height r))"
nipkow@61581
   160
by (cases l, cases r) (simp_all add: balL_def)
nipkow@61232
   161
nipkow@61581
   162
lemma height_balR2:
nipkow@61232
   163
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
nipkow@61581
   164
   height (balR l a r) = (1 + max (height l) (height r))"
nipkow@61581
   165
by (cases l, cases r) (simp_all add: balR_def)
nipkow@61232
   166
nipkow@61581
   167
lemma avl_balL: 
nipkow@61232
   168
  assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
nipkow@61232
   169
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
nipkow@61581
   170
  shows "avl(balL l a r)"
nipkow@61232
   171
proof(cases l)
nipkow@61232
   172
  case Leaf
nipkow@61581
   173
  with assms show ?thesis by (simp add: node_def balL_def)
nipkow@61232
   174
next
nipkow@68413
   175
  case Node
nipkow@61232
   176
  with assms show ?thesis
nipkow@61232
   177
  proof(cases "height l = height r + 2")
nipkow@61232
   178
    case True
nipkow@61232
   179
    from True Node assms show ?thesis
nipkow@61581
   180
      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
nipkow@61232
   181
  next
nipkow@61232
   182
    case False
nipkow@61581
   183
    with assms show ?thesis by (simp add: avl_node balL_def)
nipkow@61232
   184
  qed
nipkow@61232
   185
qed
nipkow@61232
   186
nipkow@61581
   187
lemma avl_balR: 
nipkow@61232
   188
  assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
nipkow@61232
   189
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
nipkow@61581
   190
  shows "avl(balR l a r)"
nipkow@61232
   191
proof(cases r)
nipkow@61232
   192
  case Leaf
nipkow@61581
   193
  with assms show ?thesis by (simp add: node_def balR_def)
nipkow@61232
   194
next
nipkow@68413
   195
  case Node
nipkow@61232
   196
  with assms show ?thesis
nipkow@61232
   197
  proof(cases "height r = height l + 2")
nipkow@61232
   198
    case True
nipkow@61232
   199
      from True Node assms show ?thesis
nipkow@61581
   200
        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
nipkow@61232
   201
  next
nipkow@61232
   202
    case False
nipkow@61581
   203
    with assms show ?thesis by (simp add: balR_def avl_node)
nipkow@61232
   204
  qed
nipkow@61232
   205
qed
nipkow@61232
   206
nipkow@61232
   207
(* It appears that these two properties need to be proved simultaneously: *)
nipkow@61232
   208
wenzelm@67406
   209
text\<open>Insertion maintains the AVL property:\<close>
nipkow@61232
   210
nipkow@68422
   211
theorem avl_insert:
nipkow@61232
   212
  assumes "avl t"
nipkow@61232
   213
  shows "avl(insert x t)"
nipkow@61232
   214
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
nipkow@61232
   215
using assms
nipkow@61232
   216
proof (induction t)
nipkow@68413
   217
  case (Node l a h r)
nipkow@61232
   218
  case 1
nipkow@68422
   219
  show ?case
nipkow@61232
   220
  proof(cases "x = a")
nipkow@68422
   221
    case True with Node 1 show ?thesis by simp
nipkow@61232
   222
  next
nipkow@61232
   223
    case False
nipkow@68422
   224
    show ?thesis 
nipkow@61232
   225
    proof(cases "x<a")
nipkow@68422
   226
      case True with Node 1 show ?thesis by (auto simp add:avl_balL)
nipkow@61232
   227
    next
nipkow@68422
   228
      case False with Node 1 \<open>x\<noteq>a\<close> show ?thesis by (auto simp add:avl_balR)
nipkow@61232
   229
    qed
nipkow@61232
   230
  qed
nipkow@61232
   231
  case 2
nipkow@68422
   232
  show ?case
nipkow@61232
   233
  proof(cases "x = a")
nipkow@68422
   234
    case True with Node 1 show ?thesis by simp
nipkow@61232
   235
  next
nipkow@61232
   236
    case False
nipkow@68422
   237
    show ?thesis 
nipkow@68422
   238
    proof(cases "x<a")
nipkow@61232
   239
      case True
nipkow@68422
   240
      show ?thesis
nipkow@61232
   241
      proof(cases "height (insert x l) = height r + 2")
wenzelm@67406
   242
        case False with Node 2 \<open>x < a\<close> show ?thesis by (auto simp: height_balL2)
nipkow@61232
   243
      next
nipkow@61232
   244
        case True 
nipkow@61581
   245
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
nipkow@61581
   246
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
nipkow@61581
   247
          using Node 2 by (intro height_balL) simp_all
nipkow@61232
   248
        thus ?thesis
nipkow@61232
   249
        proof
nipkow@68422
   250
          assume ?A with 2 \<open>x < a\<close> show ?thesis by (auto)
nipkow@61232
   251
        next
nipkow@68422
   252
          assume ?B with True 1 Node(2) \<open>x < a\<close> show ?thesis by (simp) arith
nipkow@61232
   253
        qed
nipkow@61232
   254
      qed
nipkow@61232
   255
    next
nipkow@61232
   256
      case False
nipkow@68422
   257
      show ?thesis 
nipkow@61232
   258
      proof(cases "height (insert x r) = height l + 2")
nipkow@68422
   259
        case False with Node 2 \<open>\<not>x < a\<close> show ?thesis by (auto simp: height_balR2)
nipkow@61232
   260
      next
nipkow@61232
   261
        case True 
nipkow@61581
   262
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
nipkow@61581
   263
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
nipkow@61581
   264
          using Node 2 by (intro height_balR) simp_all
nipkow@61232
   265
        thus ?thesis 
nipkow@61232
   266
        proof
nipkow@68422
   267
          assume ?A with 2 \<open>\<not>x < a\<close> show ?thesis by (auto)
nipkow@61232
   268
        next
nipkow@68422
   269
          assume ?B with True 1 Node(4) \<open>\<not>x < a\<close> show ?thesis by (simp) arith
nipkow@61232
   270
        qed
nipkow@61232
   271
      qed
nipkow@61232
   272
    qed
nipkow@61232
   273
  qed
nipkow@61232
   274
qed simp_all
nipkow@61232
   275
nipkow@61232
   276
wenzelm@67406
   277
subsubsection \<open>Deletion maintains AVL balance\<close>
nipkow@61232
   278
nipkow@68023
   279
lemma avl_split_max:
nipkow@61232
   280
  assumes "avl x" and "x \<noteq> Leaf"
nipkow@68023
   281
  shows "avl (fst (split_max x))" "height x = height(fst (split_max x)) \<or>
nipkow@68023
   282
         height x = height(fst (split_max x)) + 1"
nipkow@61232
   283
using assms
nipkow@68023
   284
proof (induct x rule: split_max_induct)
nipkow@68413
   285
  case (Node l a h r)
nipkow@61232
   286
  case 1
nipkow@61647
   287
  thus ?case using Node
nipkow@68422
   288
    by (auto simp: height_balL height_balL2 avl_balL split:prod.split)
nipkow@61232
   289
next
nipkow@68413
   290
  case (Node l a h r)
nipkow@61232
   291
  case 2
nipkow@68023
   292
  let ?r' = "fst (split_max r)"
wenzelm@67406
   293
  from \<open>avl x\<close> Node 2 have "avl l" and "avl r" by simp_all
nipkow@61581
   294
  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
nipkow@61232
   295
    apply (auto split:prod.splits simp del:avl.simps) by arith+
nipkow@61232
   296
qed auto
nipkow@61232
   297
nipkow@61647
   298
lemma avl_del_root:
nipkow@61232
   299
  assumes "avl t" and "t \<noteq> Leaf"
nipkow@61647
   300
  shows "avl(del_root t)" 
nipkow@61232
   301
using assms
nipkow@61647
   302
proof (cases t rule:del_root_cases)
nipkow@68413
   303
  case (Node_Node ll ln lh lr n h rl rn rh rr)
nipkow@68413
   304
  let ?l = "Node ll ln lh lr"
nipkow@68413
   305
  let ?r = "Node rl rn rh rr"
nipkow@68023
   306
  let ?l' = "fst (split_max ?l)"
wenzelm@67406
   307
  from \<open>avl t\<close> and Node_Node have "avl ?r" by simp
wenzelm@67406
   308
  from \<open>avl t\<close> and Node_Node have "avl ?l" by simp
nipkow@61232
   309
  hence "avl(?l')" "height ?l = height(?l') \<or>
nipkow@68023
   310
         height ?l = height(?l') + 1" by (rule avl_split_max,simp)+
wenzelm@67406
   311
  with \<open>avl t\<close> Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
nipkow@61232
   312
            \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
nipkow@68023
   313
  with \<open>avl ?l'\<close> \<open>avl ?r\<close> have "avl(balR ?l' (snd(split_max ?l)) ?r)"
nipkow@61581
   314
    by (rule avl_balR)
nipkow@61232
   315
  with Node_Node show ?thesis by (auto split:prod.splits)
nipkow@61232
   316
qed simp_all
nipkow@61232
   317
nipkow@61647
   318
lemma height_del_root:
nipkow@61232
   319
  assumes "avl t" and "t \<noteq> Leaf" 
nipkow@61647
   320
  shows "height t = height(del_root t) \<or> height t = height(del_root t) + 1"
nipkow@61232
   321
using assms
nipkow@61647
   322
proof (cases t rule: del_root_cases)
nipkow@68413
   323
  case (Node_Node ll ln lh lr n h rl rn rh rr)
nipkow@68413
   324
  let ?l = "Node ll ln lh lr"
nipkow@68413
   325
  let ?r = "Node rl rn rh rr"
nipkow@68023
   326
  let ?l' = "fst (split_max ?l)"
nipkow@68023
   327
  let ?t' = "balR ?l' (snd(split_max ?l)) ?r"
wenzelm@67406
   328
  from \<open>avl t\<close> and Node_Node have "avl ?r" by simp
wenzelm@67406
   329
  from \<open>avl t\<close> and Node_Node have "avl ?l" by simp
nipkow@68023
   330
  hence "avl(?l')"  by (rule avl_split_max,simp)
nipkow@68023
   331
  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using \<open>avl ?l\<close> by (intro avl_split_max) auto
wenzelm@67406
   332
  have t_height: "height t = 1 + max (height ?l) (height ?r)" using \<open>avl t\<close> Node_Node by simp
wenzelm@67406
   333
  have "height t = height ?t' \<or> height t = height ?t' + 1" using  \<open>avl t\<close> Node_Node
nipkow@61232
   334
  proof(cases "height ?r = height ?l' + 2")
nipkow@61232
   335
    case False
nipkow@68422
   336
    show ?thesis using l'_height t_height False
nipkow@68422
   337
      by (subst height_balR2[OF \<open>avl ?l'\<close> \<open>avl ?r\<close> False])+ arith
nipkow@61232
   338
  next
nipkow@61232
   339
    case True
nipkow@61232
   340
    show ?thesis
nipkow@68023
   341
    proof(cases rule: disjE[OF height_balR[OF True \<open>avl ?l'\<close> \<open>avl ?r\<close>, of "snd (split_max ?l)"]])
nipkow@68422
   342
      case 1 thus ?thesis using l'_height t_height True by arith
nipkow@61232
   343
    next
nipkow@68422
   344
      case 2 thus ?thesis using l'_height t_height True by arith
nipkow@61232
   345
    qed
nipkow@61232
   346
  qed
nipkow@61232
   347
  thus ?thesis using Node_Node by (auto split:prod.splits)
nipkow@61232
   348
qed simp_all
nipkow@61232
   349
wenzelm@67406
   350
text\<open>Deletion maintains the AVL property:\<close>
nipkow@61232
   351
nipkow@68422
   352
theorem avl_delete:
nipkow@61232
   353
  assumes "avl t" 
nipkow@61232
   354
  shows "avl(delete x t)" and "height t = (height (delete x t)) \<or> height t = height (delete x t) + 1"
nipkow@61232
   355
using assms
nipkow@61232
   356
proof (induct t)
nipkow@68413
   357
  case (Node l n h r)
nipkow@61232
   358
  case 1
nipkow@68422
   359
  show ?case
nipkow@61232
   360
  proof(cases "x = n")
nipkow@68422
   361
    case True with Node 1 show ?thesis by (auto simp:avl_del_root)
nipkow@61232
   362
  next
nipkow@61232
   363
    case False
nipkow@68422
   364
    show ?thesis 
nipkow@61232
   365
    proof(cases "x<n")
nipkow@68422
   366
      case True with Node 1 show ?thesis by (auto simp add:avl_balR)
nipkow@61232
   367
    next
nipkow@68422
   368
      case False with Node 1 \<open>x\<noteq>n\<close> show ?thesis by (auto simp add:avl_balL)
nipkow@61232
   369
    qed
nipkow@61232
   370
  qed
nipkow@61232
   371
  case 2
nipkow@68422
   372
  show ?case
nipkow@61232
   373
  proof(cases "x = n")
nipkow@61232
   374
    case True
nipkow@68413
   375
    with 1 have "height (Node l n h r) = height(del_root (Node l n h r))
nipkow@68413
   376
      \<or> height (Node l n h r) = height(del_root (Node l n h r)) + 1"
nipkow@61647
   377
      by (subst height_del_root,simp_all)
nipkow@61232
   378
    with True show ?thesis by simp
nipkow@61232
   379
  next
nipkow@61232
   380
    case False
nipkow@68422
   381
    show ?thesis 
nipkow@68422
   382
    proof(cases "x<n")
nipkow@61232
   383
      case True
nipkow@61232
   384
      show ?thesis
nipkow@61232
   385
      proof(cases "height r = height (delete x l) + 2")
wenzelm@67406
   386
        case False with Node 1 \<open>x < n\<close> show ?thesis by(auto simp: balR_def)
nipkow@61232
   387
      next
nipkow@61232
   388
        case True 
nipkow@61581
   389
        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
nipkow@61581
   390
          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
nipkow@61581
   391
          using Node 2 by (intro height_balR) auto
nipkow@61232
   392
        thus ?thesis 
nipkow@61232
   393
        proof
nipkow@68422
   394
          assume ?A with \<open>x < n\<close> Node 2 show ?thesis by(auto simp: balR_def)
nipkow@61232
   395
        next
nipkow@68422
   396
          assume ?B with \<open>x < n\<close> Node 2 show ?thesis by(auto simp: balR_def)
nipkow@61232
   397
        qed
nipkow@61232
   398
      qed
nipkow@61232
   399
    next
nipkow@61232
   400
      case False
nipkow@61232
   401
      show ?thesis
nipkow@61232
   402
      proof(cases "height l = height (delete x r) + 2")
wenzelm@67406
   403
        case False with Node 1 \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> show ?thesis by(auto simp: balL_def)
nipkow@61232
   404
      next
nipkow@61232
   405
        case True 
nipkow@61581
   406
        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
nipkow@61581
   407
          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
nipkow@61581
   408
          using Node 2 by (intro height_balL) auto
nipkow@61232
   409
        thus ?thesis 
nipkow@61232
   410
        proof
nipkow@68422
   411
          assume ?A with \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> Node 2 show ?thesis by(auto simp: balL_def)
nipkow@61232
   412
        next
nipkow@68422
   413
          assume ?B with \<open>\<not>x < n\<close> \<open>x \<noteq> n\<close> Node 2 show ?thesis by(auto simp: balL_def)
nipkow@61232
   414
        qed
nipkow@61232
   415
      qed
nipkow@61232
   416
    qed
nipkow@61232
   417
  qed
nipkow@61232
   418
qed simp_all
nipkow@61232
   419
nipkow@63411
   420
nipkow@68422
   421
subsection "Overall correctness"
nipkow@68422
   422
nipkow@68422
   423
interpretation Set_by_Ordered
nipkow@68422
   424
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@68422
   425
and inorder = inorder and inv = avl
nipkow@68422
   426
proof (standard, goal_cases)
nipkow@68422
   427
  case 1 show ?case by simp
nipkow@68422
   428
next
nipkow@68422
   429
  case 2 thus ?case by(simp add: isin_set_inorder)
nipkow@68422
   430
next
nipkow@68422
   431
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@68422
   432
next
nipkow@68422
   433
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@68422
   434
next
nipkow@68422
   435
  case 5 thus ?case by simp
nipkow@68422
   436
next
nipkow@68422
   437
  case 6 thus ?case by (simp add: avl_insert(1))
nipkow@68422
   438
next
nipkow@68422
   439
  case 7 thus ?case by (simp add: avl_delete(1))
nipkow@68422
   440
qed
nipkow@68422
   441
nipkow@68422
   442
nipkow@63411
   443
subsection \<open>Height-Size Relation\<close>
nipkow@63411
   444
nipkow@68342
   445
text \<open>Based on theorems by Daniel St\"uwe, Manuel Eberl and Peter Lammich.\<close>
nipkow@63411
   446
nipkow@68313
   447
lemma height_invers: 
nipkow@63411
   448
  "(height t = 0) = (t = Leaf)"
nipkow@68413
   449
  "avl t \<Longrightarrow> (height t = Suc h) = (\<exists> l a r . t = Node l a (Suc h) r)"
nipkow@63411
   450
by (induction t) auto
nipkow@63411
   451
nipkow@68313
   452
text \<open>Any AVL tree of height \<open>h\<close> has at least \<open>fib (h+2)\<close> leaves:\<close>
nipkow@63411
   453
nipkow@68313
   454
lemma avl_fib_bound: "avl t \<Longrightarrow> height t = h \<Longrightarrow> fib (h+2) \<le> size1 t"
nipkow@68313
   455
proof (induction h arbitrary: t rule: fib.induct)
nipkow@68313
   456
  case 1 thus ?case by (simp add: height_invers)
nipkow@63411
   457
next
nipkow@68313
   458
  case 2 thus ?case by (cases t) (auto simp: height_invers)
nipkow@63411
   459
next
nipkow@68313
   460
  case (3 h)
nipkow@68313
   461
  from "3.prems" obtain l a r where
nipkow@68413
   462
    [simp]: "t = Node l a (Suc(Suc h)) r" "avl l" "avl r"
nipkow@68313
   463
    and C: "
nipkow@68313
   464
      height r = Suc h \<and> height l = Suc h
nipkow@68313
   465
    \<or> height r = Suc h \<and> height l = h
nipkow@68313
   466
    \<or> height r = h \<and> height l = Suc h" (is "?C1 \<or> ?C2 \<or> ?C3")
nipkow@68313
   467
    by (cases t) (simp, fastforce)
nipkow@68313
   468
  {
nipkow@68313
   469
    assume ?C1
nipkow@68313
   470
    with "3.IH"(1)
nipkow@68313
   471
    have "fib (h + 3) \<le> size1 l" "fib (h + 3) \<le> size1 r"
nipkow@68313
   472
      by (simp_all add: eval_nat_numeral)
nipkow@68313
   473
    hence ?case by (auto simp: eval_nat_numeral)
nipkow@68313
   474
  } moreover {
nipkow@68313
   475
    assume ?C2
nipkow@68313
   476
    hence ?case using "3.IH"(1)[of r] "3.IH"(2)[of l] by auto
nipkow@68313
   477
  } moreover {
nipkow@68313
   478
    assume ?C3
nipkow@68313
   479
    hence ?case using "3.IH"(1)[of l] "3.IH"(2)[of r] by auto
nipkow@68313
   480
  } ultimately show ?case using C by blast
nipkow@68313
   481
qed
nipkow@68313
   482
nipkow@68313
   483
lemma fib_alt_induct [consumes 1, case_names 1 2 rec]:
nipkow@68313
   484
  assumes "n > 0" "P 1" "P 2" "\<And>n. n > 0 \<Longrightarrow> P n \<Longrightarrow> P (Suc n) \<Longrightarrow> P (Suc (Suc n))"
nipkow@68313
   485
  shows   "P n"
nipkow@68313
   486
  using assms(1)
nipkow@68313
   487
proof (induction n rule: fib.induct)
nipkow@68313
   488
  case (3 n)
nipkow@68313
   489
  thus ?case using assms by (cases n) (auto simp: eval_nat_numeral)
nipkow@68313
   490
qed (insert assms, auto)
nipkow@68313
   491
nipkow@68313
   492
text \<open>An exponential lower bound for @{const fib}:\<close>
nipkow@63411
   493
nipkow@68313
   494
lemma fib_lowerbound:
nipkow@68313
   495
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
nipkow@68313
   496
  defines "c \<equiv> 1 / \<phi> ^ 2"
nipkow@68313
   497
  assumes "n > 0"
nipkow@68313
   498
  shows   "real (fib n) \<ge> c * \<phi> ^ n"
nipkow@68313
   499
proof -
nipkow@68313
   500
  have "\<phi> > 1" by (simp add: \<phi>_def)
nipkow@68313
   501
  hence "c > 0" by (simp add: c_def)
nipkow@68313
   502
  from \<open>n > 0\<close> show ?thesis
nipkow@68313
   503
  proof (induction n rule: fib_alt_induct)
nipkow@68313
   504
    case (rec n)
nipkow@68313
   505
    have "c * \<phi> ^ Suc (Suc n) = \<phi> ^ 2 * (c * \<phi> ^ n)"
nipkow@68313
   506
      by (simp add: field_simps power2_eq_square)
nipkow@68313
   507
    also have "\<dots> \<le> (\<phi> + 1) * (c * \<phi> ^ n)"
nipkow@68313
   508
      by (rule mult_right_mono) (insert \<open>c > 0\<close>, simp_all add: \<phi>_def power2_eq_square field_simps)
nipkow@68313
   509
    also have "\<dots> = c * \<phi> ^ Suc n + c * \<phi> ^ n"
nipkow@68313
   510
      by (simp add: field_simps)
nipkow@68313
   511
    also have "\<dots> \<le> real (fib (Suc n)) + real (fib n)"
nipkow@68313
   512
      by (intro add_mono rec.IH)
nipkow@68313
   513
    finally show ?case by simp
nipkow@68313
   514
  qed (insert \<open>\<phi> > 1\<close>, simp_all add: c_def power2_eq_square eval_nat_numeral)
nipkow@68313
   515
qed
nipkow@68313
   516
nipkow@68313
   517
text \<open>The size of an AVL tree is (at least) exponential in its height:\<close>
nipkow@68313
   518
nipkow@68342
   519
lemma avl_size_lowerbound:
nipkow@68313
   520
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
nipkow@68313
   521
  assumes "avl t"
nipkow@68342
   522
  shows   "\<phi> ^ (height t) \<le> size1 t"
nipkow@68313
   523
proof -
nipkow@68313
   524
  have "\<phi> > 0" by(simp add: \<phi>_def add_pos_nonneg)
nipkow@68313
   525
  hence "\<phi> ^ height t = (1 / \<phi> ^ 2) * \<phi> ^ (height t + 2)"
nipkow@68313
   526
    by(simp add: field_simps power2_eq_square)
nipkow@68342
   527
  also have "\<dots> \<le> fib (height t + 2)"
nipkow@68313
   528
    using fib_lowerbound[of "height t + 2"] by(simp add: \<phi>_def)
nipkow@68342
   529
  also have "\<dots> \<le> size1 t"
nipkow@68313
   530
    using avl_fib_bound[of t "height t"] assms by simp
nipkow@68313
   531
  finally show ?thesis .
nipkow@68313
   532
qed
nipkow@63411
   533
nipkow@68342
   534
text \<open>The height of an AVL tree is most @{term "(1/log 2 \<phi>)"} \<open>\<approx> 1.44\<close> times worse
nipkow@68342
   535
than @{term "log 2 (size1 t)"}:\<close>
nipkow@68342
   536
nipkow@68342
   537
lemma  avl_height_upperbound:
nipkow@68342
   538
  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
nipkow@68342
   539
  assumes "avl t"
nipkow@68342
   540
  shows   "height t \<le> (1/log 2 \<phi>) * log 2 (size1 t)"
nipkow@68342
   541
proof -
nipkow@68342
   542
  have "\<phi> > 0" "\<phi> > 1" by(auto simp: \<phi>_def pos_add_strict)
nipkow@68342
   543
  hence "height t = log \<phi> (\<phi> ^ height t)" by(simp add: log_nat_power)
nipkow@68342
   544
  also have "\<dots> \<le> log \<phi> (size1 t)"
nipkow@68342
   545
    using avl_size_lowerbound[OF assms(2), folded \<phi>_def] \<open>1 < \<phi>\<close>  by simp
nipkow@68342
   546
  also have "\<dots> = (1/log 2 \<phi>) * log 2 (size1 t)"
nipkow@68342
   547
    by(simp add: log_base_change[of 2 \<phi>])
nipkow@68342
   548
  finally show ?thesis .
nipkow@68342
   549
qed
nipkow@68342
   550
nipkow@61232
   551
end