src/HOL/Library/Tree_Real.thy
author nipkow
Fri, 25 Aug 2017 23:09:56 +0200
changeset 66510 ca7a369301f6
child 66515 85c505c98332
permissions -rw-r--r--
reorganization of tree lemmas; new lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66510
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     2
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     3
theory Tree_Real
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     4
imports
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     5
  Complex_Main
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     6
  Tree
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     7
begin
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     8
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
     9
text \<open>This theory is separate from @{theory Tree} because the former is discrete and builds on
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    10
@{theory Main} whereas this theory builds on @{theory Complex_Main}.\<close>
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    11
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    12
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    13
lemma size1_height_log: "log 2 (size1 t) \<le> height t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    14
by (simp add: log2_of_power_le size1_height)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    15
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    16
lemma min_height_size1_log: "min_height t \<le> log 2 (size1 t)"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    17
by (simp add: le_log2_of_power min_height_size1)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    18
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    19
lemma size1_log_if_complete: "complete t \<Longrightarrow> height t = log 2 (size1 t)"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    20
by (simp add: log2_of_power_eq size1_if_complete)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    21
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    22
lemma min_height_size1_log_if_incomplete:
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    23
  "\<not> complete t \<Longrightarrow> min_height t < log 2 (size1 t)"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    24
by (simp add: less_log2_of_power min_height_size1_if_incomplete)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    25
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    26
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    27
lemma min_height_balanced: assumes "balanced t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    28
shows "min_height t = nat(floor(log 2 (size1 t)))"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    29
proof cases
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    30
  assume *: "complete t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    31
  hence "size1 t = 2 ^ min_height t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    32
    by (simp add: complete_iff_height size1_if_complete)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    33
  from log2_of_power_eq[OF this] show ?thesis by linarith
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    34
next
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    35
  assume *: "\<not> complete t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    36
  hence "height t = min_height t + 1"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    37
    using assms min_height_le_height[of t]
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    38
    by(auto simp add: balanced_def complete_iff_height)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    39
  hence "size1 t < 2 ^ (min_height t + 1)"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    40
    by (metis * size1_height_if_incomplete)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    41
  hence "log 2 (size1 t) < min_height t + 1"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    42
    using log2_of_power_less size1_ge0 by blast
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    43
  thus ?thesis using min_height_size1_log[of t] by linarith
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    44
qed
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    45
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    46
lemma height_balanced: assumes "balanced t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    47
shows "height t = nat(ceiling(log 2 (size1 t)))"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    48
proof cases
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    49
  assume *: "complete t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    50
  hence "size1 t = 2 ^ height t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    51
    by (simp add: size1_if_complete)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    52
  from log2_of_power_eq[OF this] show ?thesis
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    53
    by linarith
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    54
next
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    55
  assume *: "\<not> complete t"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    56
  hence **: "height t = min_height t + 1"
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    57
    using assms min_height_le_height[of t]
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    58
    by(auto simp add: balanced_def complete_iff_height)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    59
  hence "size1 t \<le> 2 ^ (min_height t + 1)" by (metis size1_height)
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    60
  from  log2_of_power_le[OF this size1_ge0] min_height_size1_log_if_incomplete[OF *] **
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    61
  show ?thesis by linarith
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    62
qed
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    63
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents:
diff changeset
    64
end