src/HOL/Data_Structures/Balance.thy
author wenzelm
Fri, 18 Aug 2017 20:47:47 +0200
changeset 66453 cc19f7ca2ed6
parent 64541 3d4331b65861
child 66510 ca7a369301f6
permissions -rw-r--r--
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
     1
(* Author: Tobias Nipkow *)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     2
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
     3
section \<open>Creating Balanced Trees\<close>
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     4
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
     5
theory Balance
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     6
imports
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
     7
  Complex_Main
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 64541
diff changeset
     8
  "HOL-Library.Tree"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     9
begin
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    10
64533
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    11
(* The following two lemmas should go into theory \<open>Tree\<close>, except that that
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    12
theory would then depend on \<open>Complex_Main\<close>. *)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    13
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    14
lemma min_height_balanced: assumes "balanced t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    15
shows "min_height t = nat(floor(log 2 (size1 t)))"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    16
proof cases
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    17
  assume *: "complete t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    18
  hence "size1 t = 2 ^ min_height t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    19
    by (simp add: complete_iff_height size1_if_complete)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    20
  hence "size1 t = 2 powr min_height t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    21
    using * by (simp add: powr_realpow)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    22
  hence "min_height t = log 2 (size1 t)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    23
    by simp
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    24
  thus ?thesis
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    25
    by linarith
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    26
next
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    27
  assume *: "\<not> complete t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    28
  hence "height t = min_height t + 1"
64540
f1f4ba6d02c9 spelling
nipkow
parents: 64533
diff changeset
    29
    using assms min_height_le_height[of t]
64533
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    30
    by(auto simp add: balanced_def complete_iff_height)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    31
  hence "2 ^ min_height t \<le> size1 t \<and> size1 t < 2 ^ (min_height t + 1)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    32
    by (metis * min_height_size1 size1_height_if_incomplete)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    33
  hence "2 powr min_height t \<le> size1 t \<and> size1 t < 2 powr (min_height t + 1)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    34
    by(simp only: powr_realpow)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    35
      (metis of_nat_less_iff of_nat_le_iff of_nat_numeral of_nat_power)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    36
  hence "min_height t \<le> log 2 (size1 t) \<and> log 2 (size1 t) < min_height t + 1"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    37
    by(simp add: log_less_iff le_log_iff)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    38
  thus ?thesis by linarith
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    39
qed
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    40
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    41
lemma height_balanced: assumes "balanced t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    42
shows "height t = nat(ceiling(log 2 (size1 t)))"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    43
proof cases
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    44
  assume *: "complete t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    45
  hence "size1 t = 2 ^ height t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    46
    by (simp add: size1_if_complete)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    47
  hence "size1 t = 2 powr height t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    48
    using * by (simp add: powr_realpow)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    49
  hence "height t = log 2 (size1 t)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    50
    by simp
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    51
  thus ?thesis
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    52
    by linarith
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    53
next
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    54
  assume *: "\<not> complete t"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    55
  hence **: "height t = min_height t + 1"
64540
f1f4ba6d02c9 spelling
nipkow
parents: 64533
diff changeset
    56
    using assms min_height_le_height[of t]
64533
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    57
    by(auto simp add: balanced_def complete_iff_height)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    58
  hence 0: "2 ^ min_height t < size1 t \<and> size1 t \<le> 2 ^ (min_height t + 1)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    59
    by (metis "*" min_height_size1_if_incomplete size1_height)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    60
  hence "2 powr min_height t < size1 t \<and> size1 t \<le> 2 powr (min_height t + 1)"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    61
    by(simp only: powr_realpow)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    62
      (metis of_nat_less_iff of_nat_le_iff of_nat_numeral of_nat_power)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    63
  hence "min_height t < log 2 (size1 t) \<and> log 2 (size1 t) \<le> min_height t + 1"
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    64
    by(simp add: log_le_iff less_log_iff)
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    65
  thus ?thesis using ** by linarith
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    66
qed
172f3a047f4a more lemmas, tuned proofs
nipkow
parents: 64444
diff changeset
    67
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    68
(* mv *)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    69
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    70
text \<open>The lemmas about \<open>floor\<close> and \<open>ceiling\<close> of \<open>log 2\<close> should be generalized
64065
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
    71
from 2 to \<open>n\<close> and should be made executable. In the end they should be moved
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
    72
to theory \<open>Log_Nat\<close> and \<open>floorlog\<close> should be replaced.\<close>
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    73
64065
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
    74
lemma floor_log_nat_ivl: fixes b n k :: nat
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    75
assumes "b \<ge> 2" "b^n \<le> k" "k < b^(n+1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    76
shows "floor (log b (real k)) = int(n)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    77
proof -
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    78
  have "k \<ge> 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    79
    using assms(1,2) one_le_power[of b n] by linarith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    80
  show ?thesis
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    81
  proof(rule floor_eq2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    82
    show "int n \<le> log b k"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    83
      using assms(1,2) \<open>k \<ge> 1\<close>
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    84
      by(simp add: powr_realpow le_log_iff of_nat_power[symmetric] del: of_nat_power)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    85
  next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    86
    have "real k < b powr (real(n + 1))" using assms(1,3)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    87
      by (simp only: powr_realpow) (metis of_nat_less_iff of_nat_power)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    88
    thus "log b k < real_of_int (int n) + 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    89
      using assms(1) \<open>k \<ge> 1\<close> by(simp add: log_less_iff add_ac)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    90
  qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    91
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    92
64065
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
    93
lemma ceil_log_nat_ivl: fixes b n k :: nat
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    94
assumes "b \<ge> 2" "b^n < k" "k \<le> b^(n+1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    95
shows "ceiling (log b (real k)) = int(n)+1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    96
proof(rule ceiling_eq)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    97
  show "int n < log b k"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    98
    using assms(1,2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    99
    by(simp add: powr_realpow less_log_iff of_nat_power[symmetric] del: of_nat_power)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   100
next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   101
  have "real k \<le> b powr (real(n + 1))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   102
    using assms(1,3)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   103
    by (simp only: powr_realpow) (metis of_nat_le_iff of_nat_power)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   104
  thus "log b k \<le> real_of_int (int n) + 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   105
    using assms(1,2) by(simp add: log_le_iff add_ac)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   106
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   107
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   108
lemma ceil_log2_div2: assumes "n \<ge> 2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   109
shows "ceiling(log 2 (real n)) = ceiling(log 2 ((n-1) div 2 + 1)) + 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   110
proof cases
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   111
  assume "n=2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   112
  thus ?thesis by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   113
next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   114
  let ?m = "(n-1) div 2 + 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   115
  assume "n\<noteq>2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   116
  hence "2 \<le> ?m"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   117
    using assms by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   118
  then obtain i where i: "2 ^ i < ?m" "?m \<le> 2 ^ (i + 1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   119
    using ex_power_ivl2[of 2 ?m] by auto
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   120
  have "n \<le> 2*?m"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   121
    by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   122
  also have "2*?m \<le> 2 ^ ((i+1)+1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   123
    using i(2) by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   124
  finally have *: "n \<le> \<dots>" .
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   125
  have "2^(i+1) < n"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   126
    using i(1) by (auto simp add: less_Suc_eq_0_disj)
64065
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
   127
  from ceil_log_nat_ivl[OF _ this *] ceil_log_nat_ivl[OF _ i]
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   128
  show ?thesis by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   129
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   130
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   131
lemma floor_log2_div2: fixes n :: nat assumes "n \<ge> 2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   132
shows "floor(log 2 n) = floor(log 2 (n div 2)) + 1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   133
proof cases
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   134
  assume "n=2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   135
  thus ?thesis by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   136
next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   137
  let ?m = "n div 2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   138
  assume "n\<noteq>2"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   139
  hence "1 \<le> ?m"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   140
    using assms by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   141
  then obtain i where i: "2 ^ i \<le> ?m" "?m < 2 ^ (i + 1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   142
    using ex_power_ivl1[of 2 ?m] by auto
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   143
  have "2^(i+1) \<le> 2*?m"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   144
    using i(1) by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   145
  also have "2*?m \<le> n"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   146
    by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   147
  finally have *: "2^(i+1) \<le> \<dots>" .
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   148
  have "n < 2^(i+1+1)"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   149
    using i(2) by simp
64065
40d440b75b00 moved lemmas
nipkow
parents: 64018
diff changeset
   150
  from floor_log_nat_ivl[OF _ * this] floor_log_nat_ivl[OF _ i]
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   151
  show ?thesis by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   152
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   153
64541
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   154
lemma balanced_Node_if_wbal1:
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   155
assumes "balanced l" "balanced r" "size l = size r + 1"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   156
shows "balanced \<langle>l, x, r\<rangle>"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   157
proof -
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   158
  from assms(3) have [simp]: "size1 l = size1 r + 1" by(simp add: size1_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   159
  have "nat \<lceil>log 2 (1 + size1 r)\<rceil> \<ge> nat \<lceil>log 2 (size1 r)\<rceil>"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   160
    by(rule nat_mono[OF ceiling_mono]) simp
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   161
  hence 1: "height(Node l x r) = nat \<lceil>log 2 (1 + size1 r)\<rceil> + 1"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   162
    using height_balanced[OF assms(1)] height_balanced[OF assms(2)]
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   163
    by (simp del: nat_ceiling_le_eq add: max_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   164
  have "nat \<lfloor>log 2 (1 + size1 r)\<rfloor> \<ge> nat \<lfloor>log 2 (size1 r)\<rfloor>"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   165
    by(rule nat_mono[OF floor_mono]) simp
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   166
  hence 2: "min_height(Node l x r) = nat \<lfloor>log 2 (size1 r)\<rfloor> + 1"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   167
    using min_height_balanced[OF assms(1)] min_height_balanced[OF assms(2)]
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   168
    by (simp)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   169
  have "size1 r \<ge> 1" by(simp add: size1_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   170
  then obtain i where i: "2 ^ i \<le> size1 r" "size1 r < 2 ^ (i + 1)"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   171
    using ex_power_ivl1[of 2 "size1 r"] by auto
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   172
  hence i1: "2 ^ i < size1 r + 1" "size1 r + 1 \<le> 2 ^ (i + 1)" by auto
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   173
  from 1 2 floor_log_nat_ivl[OF _ i] ceil_log_nat_ivl[OF _ i1]
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   174
  show ?thesis by(simp add:balanced_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   175
qed
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   176
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   177
lemma balanced_sym: "balanced \<langle>l, x, r\<rangle> \<Longrightarrow> balanced \<langle>r, y, l\<rangle>"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   178
by(auto simp: balanced_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   179
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   180
lemma balanced_Node_if_wbal2:
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   181
assumes "balanced l" "balanced r" "abs(int(size l) - int(size r)) \<le> 1"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   182
shows "balanced \<langle>l, x, r\<rangle>"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   183
proof -
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   184
  have "size l = size r \<or> (size l = size r + 1 \<or> size r = size l + 1)" (is "?A \<or> ?B")
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   185
    using assms(3) by linarith
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   186
  thus ?thesis
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   187
  proof
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   188
    assume "?A"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   189
    thus ?thesis using assms(1,2)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   190
      apply(simp add: balanced_def min_def max_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   191
      by (metis assms(1,2) balanced_optimal le_antisym le_less)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   192
  next
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   193
    assume "?B"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   194
    thus ?thesis
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   195
      by (meson assms(1,2) balanced_sym balanced_Node_if_wbal1)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   196
  qed
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   197
qed
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   198
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   199
lemma balanced_if_wbalanced: "wbalanced t \<Longrightarrow> balanced t"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   200
proof(induction t)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   201
  case Leaf show ?case by (simp add: balanced_def)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   202
next
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   203
  case (Node l x r)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   204
  thus ?case by(simp add: balanced_Node_if_wbal2)
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   205
qed
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   206
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   207
(* end of mv *)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   208
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   209
fun bal :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree * 'a list" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   210
"bal n xs = (if n=0 then (Leaf,xs) else
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   211
 (let m = n div 2;
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   212
      (l, ys) = bal m xs;
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   213
      (r, zs) = bal (n-1-m) (tl ys)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   214
  in (Node l (hd ys) r, zs)))"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   215
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   216
declare bal.simps[simp del]
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   217
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   218
definition bal_list :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   219
"bal_list n xs = fst (bal n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   220
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   221
definition balance_list :: "'a list \<Rightarrow> 'a tree" where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   222
"balance_list xs = bal_list (length xs) xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   223
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   224
definition bal_tree :: "nat \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   225
"bal_tree n t = bal_list n (inorder t)"
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   226
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   227
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   228
"balance_tree t = bal_tree (size t) t"
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   229
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   230
lemma bal_simps:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   231
  "bal 0 xs = (Leaf, xs)"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   232
  "n > 0 \<Longrightarrow>
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   233
   bal n xs =
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   234
  (let m = n div 2;
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   235
      (l, ys) = bal m xs;
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   236
      (r, zs) = bal (n-1-m) (tl ys)
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   237
  in (Node l (hd ys) r, zs))"
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   238
by(simp_all add: bal.simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   239
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   240
text\<open>Some of the following lemmas take advantage of the fact
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   241
that \<open>bal xs n\<close> yields a result even if \<open>n > length xs\<close>.\<close>
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   242
  
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   243
lemma size_bal: "bal n xs = (t,ys) \<Longrightarrow> size t = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   244
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   245
  case (1 n xs)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   246
  thus ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   247
    by(cases "n=0")
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   248
      (auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   249
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   250
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   251
lemma bal_inorder:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   252
  "\<lbrakk> bal n xs = (t,ys); n \<le> length xs \<rbrakk>
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63663
diff changeset
   253
  \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   254
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   255
  case (1 n xs) show ?case
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   256
  proof cases
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   257
    assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   258
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   259
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   260
    let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   261
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   262
      b1: "bal ?n1 xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   263
      b2: "bal ?n2 (tl xs') = (r,ys)" and
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   264
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   265
      by(auto simp: Let_def bal_simps split: prod.splits)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   266
    have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   267
      using b1 "1.prems" by(intro "1.IH"(1)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   268
    have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   269
      using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   270
    have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   271
    hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   272
      by (metis Suc_eq_plus1 take_Suc)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   273
    hence *: "inorder t = take n xs" using t IH1 IH2
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   274
      using take_add[of ?n1 "?n2+1" xs] by(simp)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   275
    have "n - n div 2 + n div 2 = n" by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   276
    hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   277
    thus ?thesis using * by blast
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   278
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   279
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   280
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   281
corollary inorder_bal_list[simp]:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   282
  "n \<le> length xs \<Longrightarrow> inorder(bal_list n xs) = take n xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   283
unfolding bal_list_def by (metis bal_inorder eq_fst_iff)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   284
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   285
corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   286
by(simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   287
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   288
corollary inorder_bal_tree:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   289
  "n \<le> size t \<Longrightarrow> inorder(bal_tree n t) = take n (inorder t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   290
by(simp add: bal_tree_def)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   291
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   292
corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   293
by(simp add: balance_tree_def inorder_bal_tree)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   294
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   295
corollary size_bal_list[simp]: "size(bal_list n xs) = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   296
unfolding bal_list_def by (metis prod.collapse size_bal)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   297
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   298
corollary size_balance_list[simp]: "size(balance_list xs) = length xs"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   299
by (simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   300
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   301
corollary size_bal_tree[simp]: "size(bal_tree n t) = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   302
by(simp add: bal_tree_def)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   303
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   304
corollary size_balance_tree[simp]: "size(balance_tree t) = size t"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   305
by(simp add: balance_tree_def)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   306
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   307
lemma min_height_bal:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   308
  "bal n xs = (t,ys) \<Longrightarrow> min_height t = nat(floor(log 2 (n + 1)))"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   309
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   310
  case (1 n xs) show ?case
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   311
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   312
    assume "n = 0" thus ?thesis
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   313
      using "1.prems" by (simp add: bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   314
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   315
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   316
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   317
      b1: "bal (n div 2) xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   318
      b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   319
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   320
      by(auto simp: bal_simps Let_def split: prod.splits)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   321
    let ?log1 = "nat (floor(log 2 (n div 2 + 1)))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   322
    let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   323
    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   324
    have IH2: "min_height r = ?log2" using "1.IH"(2) b1 b2 by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   325
    have "(n+1) div 2 \<ge> 1" by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   326
    hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   327
    have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   328
    hence le: "?log2 \<le> ?log1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   329
      by(simp add: nat_mono floor_mono)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   330
    have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   331
    also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   332
    also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   333
    also have "nat (floor(log 2 ((n+1) div 2))) + 1
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   334
       = nat (floor(log 2 ((n+1) div 2) + 1))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   335
      using 0 by linarith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   336
    also have "\<dots> = nat (floor(log 2 (n + 1)))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   337
      using floor_log2_div2[of "n+1"] by (simp add: log_mult)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   338
    finally show ?thesis .
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   339
  qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   340
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   341
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   342
lemma height_bal:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   343
  "bal n xs = (t,ys) \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   344
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   345
  case (1 n xs) show ?case
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   346
  proof cases
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   347
    assume "n = 0" thus ?thesis
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   348
      using "1.prems" by (simp add: bal_simps)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   349
  next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   350
    assume [arith]: "n \<noteq> 0"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   351
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   352
      b1: "bal (n div 2) xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   353
      b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   354
      t: "t = \<langle>l, hd xs', r\<rangle>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   355
      by(auto simp: bal_simps Let_def split: prod.splits)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   356
    let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   357
    let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   358
    have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   359
    have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   360
    have 0: "log 2 (n div 2 + 1) \<ge> 0" by auto
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   361
    have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   362
    hence le: "?log2 \<le> ?log1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   363
      by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   364
    have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   365
    also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   366
    also have "\<dots> = nat \<lceil>log 2 (n div 2 + 1) + 1\<rceil>" using 0 by linarith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   367
    also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   368
      using ceil_log2_div2[of "n+1"] by (simp)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   369
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   370
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   371
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   372
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   373
lemma balanced_bal:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   374
  assumes "bal n xs = (t,ys)" shows "balanced t"
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   375
unfolding balanced_def
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   376
using height_bal[OF assms] min_height_bal[OF assms]
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   377
by linarith
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   378
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   379
lemma height_bal_list:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   380
  "n \<le> length xs \<Longrightarrow> height (bal_list n xs) = nat \<lceil>log 2 (n + 1)\<rceil>"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   381
unfolding bal_list_def by (metis height_bal prod.collapse)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   382
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   383
lemma height_balance_list:
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   384
  "height (balance_list xs) = nat \<lceil>log 2 (length xs + 1)\<rceil>"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   385
by (simp add: balance_list_def height_bal_list)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   386
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   387
corollary height_bal_tree:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   388
  "n \<le> length xs \<Longrightarrow> height (bal_tree n t) = nat(ceiling(log 2 (n + 1)))"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   389
unfolding bal_list_def bal_tree_def
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   390
using height_bal prod.exhaust_sel by blast
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   391
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   392
corollary height_balance_tree:
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   393
  "height (balance_tree t) = nat(ceiling(log 2 (size t + 1)))"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   394
by (simp add: bal_tree_def balance_tree_def height_bal_list)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   395
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   396
corollary balanced_bal_list[simp]: "balanced (bal_list n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   397
unfolding bal_list_def by (metis  balanced_bal prod.collapse)
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   398
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   399
corollary balanced_balance_list[simp]: "balanced (balance_list xs)"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   400
by (simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   401
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   402
corollary balanced_bal_tree[simp]: "balanced (bal_tree n t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   403
by (simp add: bal_tree_def)
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   404
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   405
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   406
by (simp add: balance_tree_def)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   407
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   408
lemma wbalanced_bal: "bal n xs = (t,ys) \<Longrightarrow> wbalanced t"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   409
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   410
  case (1 n xs)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   411
  show ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   412
  proof cases
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   413
    assume "n = 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   414
    thus ?thesis
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   415
      using "1.prems" by(simp add: bal_simps)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   416
  next
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   417
    assume "n \<noteq> 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   418
    with "1.prems" obtain l ys r zs where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   419
      rec1: "bal (n div 2) xs = (l, ys)" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   420
      rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   421
      t: "t = \<langle>l, hd ys, r\<rangle>"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   422
      by(auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   423
    have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl rec1] .
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   424
    have "wbalanced r" using "1.IH"(2)[OF \<open>n\<noteq>0\<close> refl rec1[symmetric] refl rec2] .
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   425
    with l t size_bal[OF rec1] size_bal[OF rec2]
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   426
    show ?thesis by auto
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   427
  qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   428
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   429
64541
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   430
text\<open>An alternative proof via @{thm balanced_if_wbalanced}:\<close>
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   431
lemma "bal n xs = (t,ys) \<Longrightarrow> balanced t"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   432
by(rule balanced_if_wbalanced[OF wbalanced_bal])
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   433
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   434
lemma wbalanced_bal_list[simp]: "wbalanced (bal_list n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   435
by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   436
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   437
lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   438
by(simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   439
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   440
lemma wbalanced_bal_tree[simp]: "wbalanced (bal_tree n t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   441
by(simp add: bal_tree_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   442
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   443
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   444
by (simp add: balance_tree_def)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   445
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   446
hide_const (open) bal
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   447
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   448
end