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