src/HOL/Library/Tree_Real.thy
author Manuel Eberl <eberlm@in.tum.de>
Mon Mar 26 16:14:16 2018 +0200 (19 months ago)
changeset 67951 655aa11359dc
parent 67399 eab6ce8368fa
child 68484 59793df7f853
permissions -rw-r--r--
Removed some uses of deprecated _tac methods. (Patch from Viorel Preoteasa)
nipkow@66510
     1
(* Author: Tobias Nipkow *)
nipkow@66510
     2
nipkow@66510
     3
theory Tree_Real
nipkow@66510
     4
imports
nipkow@66510
     5
  Complex_Main
nipkow@66510
     6
  Tree
nipkow@66510
     7
begin
nipkow@66510
     8
nipkow@66510
     9
text \<open>This theory is separate from @{theory Tree} because the former is discrete and builds on
nipkow@66510
    10
@{theory Main} whereas this theory builds on @{theory Complex_Main}.\<close>
nipkow@66510
    11
nipkow@66510
    12
nipkow@66510
    13
lemma size1_height_log: "log 2 (size1 t) \<le> height t"
nipkow@66510
    14
by (simp add: log2_of_power_le size1_height)
nipkow@66510
    15
nipkow@66510
    16
lemma min_height_size1_log: "min_height t \<le> log 2 (size1 t)"
nipkow@66510
    17
by (simp add: le_log2_of_power min_height_size1)
nipkow@66510
    18
nipkow@66510
    19
lemma size1_log_if_complete: "complete t \<Longrightarrow> height t = log 2 (size1 t)"
nipkow@66510
    20
by (simp add: log2_of_power_eq size1_if_complete)
nipkow@66510
    21
nipkow@66510
    22
lemma min_height_size1_log_if_incomplete:
nipkow@66510
    23
  "\<not> complete t \<Longrightarrow> min_height t < log 2 (size1 t)"
nipkow@66510
    24
by (simp add: less_log2_of_power min_height_size1_if_incomplete)
nipkow@66510
    25
nipkow@66510
    26
nipkow@66510
    27
lemma min_height_balanced: assumes "balanced t"
nipkow@66510
    28
shows "min_height t = nat(floor(log 2 (size1 t)))"
nipkow@66510
    29
proof cases
nipkow@66510
    30
  assume *: "complete t"
nipkow@66510
    31
  hence "size1 t = 2 ^ min_height t"
nipkow@66510
    32
    by (simp add: complete_iff_height size1_if_complete)
nipkow@66510
    33
  from log2_of_power_eq[OF this] show ?thesis by linarith
nipkow@66510
    34
next
nipkow@66510
    35
  assume *: "\<not> complete t"
nipkow@66510
    36
  hence "height t = min_height t + 1"
nipkow@66510
    37
    using assms min_height_le_height[of t]
nipkow@66510
    38
    by(auto simp add: balanced_def complete_iff_height)
nipkow@66510
    39
  hence "size1 t < 2 ^ (min_height t + 1)"
nipkow@66510
    40
    by (metis * size1_height_if_incomplete)
nipkow@66510
    41
  hence "log 2 (size1 t) < min_height t + 1"
nipkow@66510
    42
    using log2_of_power_less size1_ge0 by blast
nipkow@66510
    43
  thus ?thesis using min_height_size1_log[of t] by linarith
nipkow@66510
    44
qed
nipkow@66510
    45
nipkow@66510
    46
lemma height_balanced: assumes "balanced t"
nipkow@66510
    47
shows "height t = nat(ceiling(log 2 (size1 t)))"
nipkow@66510
    48
proof cases
nipkow@66510
    49
  assume *: "complete t"
nipkow@66510
    50
  hence "size1 t = 2 ^ height t"
nipkow@66510
    51
    by (simp add: size1_if_complete)
nipkow@66510
    52
  from log2_of_power_eq[OF this] show ?thesis
nipkow@66510
    53
    by linarith
nipkow@66510
    54
next
nipkow@66510
    55
  assume *: "\<not> complete t"
nipkow@66510
    56
  hence **: "height t = min_height t + 1"
nipkow@66510
    57
    using assms min_height_le_height[of t]
nipkow@66510
    58
    by(auto simp add: balanced_def complete_iff_height)
nipkow@66510
    59
  hence "size1 t \<le> 2 ^ (min_height t + 1)" by (metis size1_height)
nipkow@66510
    60
  from  log2_of_power_le[OF this size1_ge0] min_height_size1_log_if_incomplete[OF *] **
nipkow@66510
    61
  show ?thesis by linarith
nipkow@66510
    62
qed
nipkow@66510
    63
nipkow@66515
    64
lemma balanced_Node_if_wbal1:
nipkow@66515
    65
assumes "balanced l" "balanced r" "size l = size r + 1"
nipkow@66515
    66
shows "balanced \<langle>l, x, r\<rangle>"
nipkow@66515
    67
proof -
nipkow@66515
    68
  from assms(3) have [simp]: "size1 l = size1 r + 1" by(simp add: size1_def)
nipkow@66515
    69
  have "nat \<lceil>log 2 (1 + size1 r)\<rceil> \<ge> nat \<lceil>log 2 (size1 r)\<rceil>"
nipkow@66515
    70
    by(rule nat_mono[OF ceiling_mono]) simp
nipkow@66515
    71
  hence 1: "height(Node l x r) = nat \<lceil>log 2 (1 + size1 r)\<rceil> + 1"
nipkow@66515
    72
    using height_balanced[OF assms(1)] height_balanced[OF assms(2)]
nipkow@66515
    73
    by (simp del: nat_ceiling_le_eq add: max_def)
nipkow@66515
    74
  have "nat \<lfloor>log 2 (1 + size1 r)\<rfloor> \<ge> nat \<lfloor>log 2 (size1 r)\<rfloor>"
nipkow@66515
    75
    by(rule nat_mono[OF floor_mono]) simp
nipkow@66515
    76
  hence 2: "min_height(Node l x r) = nat \<lfloor>log 2 (size1 r)\<rfloor> + 1"
nipkow@66515
    77
    using min_height_balanced[OF assms(1)] min_height_balanced[OF assms(2)]
nipkow@66515
    78
    by (simp)
nipkow@66515
    79
  have "size1 r \<ge> 1" by(simp add: size1_def)
nipkow@66515
    80
  then obtain i where i: "2 ^ i \<le> size1 r" "size1 r < 2 ^ (i + 1)"
nipkow@66515
    81
    using ex_power_ivl1[of 2 "size1 r"] by auto
nipkow@66515
    82
  hence i1: "2 ^ i < size1 r + 1" "size1 r + 1 \<le> 2 ^ (i + 1)" by auto
nipkow@66515
    83
  from 1 2 floor_log_nat_eq_if[OF i] ceiling_log_nat_eq_if[OF i1]
nipkow@66515
    84
  show ?thesis by(simp add:balanced_def)
nipkow@66515
    85
qed
nipkow@66515
    86
nipkow@66515
    87
lemma balanced_sym: "balanced \<langle>l, x, r\<rangle> \<Longrightarrow> balanced \<langle>r, y, l\<rangle>"
nipkow@66515
    88
by(auto simp: balanced_def)
nipkow@66515
    89
nipkow@66515
    90
lemma balanced_Node_if_wbal2:
nipkow@66515
    91
assumes "balanced l" "balanced r" "abs(int(size l) - int(size r)) \<le> 1"
nipkow@66515
    92
shows "balanced \<langle>l, x, r\<rangle>"
nipkow@66515
    93
proof -
nipkow@66515
    94
  have "size l = size r \<or> (size l = size r + 1 \<or> size r = size l + 1)" (is "?A \<or> ?B")
nipkow@66515
    95
    using assms(3) by linarith
nipkow@66515
    96
  thus ?thesis
nipkow@66515
    97
  proof
nipkow@66515
    98
    assume "?A"
nipkow@66515
    99
    thus ?thesis using assms(1,2)
nipkow@66515
   100
      apply(simp add: balanced_def min_def max_def)
nipkow@66515
   101
      by (metis assms(1,2) balanced_optimal le_antisym le_less)
nipkow@66515
   102
  next
nipkow@66515
   103
    assume "?B"
nipkow@66515
   104
    thus ?thesis
nipkow@66515
   105
      by (meson assms(1,2) balanced_sym balanced_Node_if_wbal1)
nipkow@66515
   106
  qed
nipkow@66515
   107
qed
nipkow@66515
   108
nipkow@66515
   109
lemma balanced_if_wbalanced: "wbalanced t \<Longrightarrow> balanced t"
nipkow@66515
   110
proof(induction t)
nipkow@66515
   111
  case Leaf show ?case by (simp add: balanced_def)
nipkow@66515
   112
next
nipkow@66515
   113
  case (Node l x r)
nipkow@66515
   114
  thus ?case by(simp add: balanced_Node_if_wbal2)
nipkow@66515
   115
qed
nipkow@66515
   116
nipkow@67399
   117
end