src/HOL/Data_Structures/Balance.thy
author nipkow
Sat, 26 Aug 2017 16:47:25 +0200
changeset 66515 85c505c98332
parent 66510 ca7a369301f6
child 66516 97c2d3846e10
permissions -rw-r--r--
reorganized and added log-related lemmas
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
66510
ca7a369301f6 reorganization of tree lemmas; new lemmas
nipkow
parents: 66453
diff changeset
     7
  "HOL-Library.Tree_Real"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     8
begin
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     9
66515
85c505c98332 reorganized and added log-related lemmas
nipkow
parents: 66510
diff changeset
    10
(* FIXME rm floor_eq_iff / rename unique \<rightarrow> eq *)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    11
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    12
fun bal :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree * 'a list" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    13
"bal n xs = (if n=0 then (Leaf,xs) else
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    14
 (let m = n div 2;
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    15
      (l, ys) = bal m xs;
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    16
      (r, zs) = bal (n-1-m) (tl ys)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    17
  in (Node l (hd ys) r, zs)))"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    18
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    19
declare bal.simps[simp del]
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    20
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    21
definition bal_list :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    22
"bal_list n xs = fst (bal n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    23
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    24
definition balance_list :: "'a list \<Rightarrow> 'a tree" where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    25
"balance_list xs = bal_list (length xs) xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    26
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    27
definition bal_tree :: "nat \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    28
"bal_tree n t = bal_list n (inorder t)"
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    29
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    30
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    31
"balance_tree t = bal_tree (size t) t"
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    32
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    33
lemma bal_simps:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    34
  "bal 0 xs = (Leaf, xs)"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    35
  "n > 0 \<Longrightarrow>
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    36
   bal n xs =
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    37
  (let m = n div 2;
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    38
      (l, ys) = bal m xs;
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    39
      (r, zs) = bal (n-1-m) (tl ys)
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    40
  in (Node l (hd ys) r, zs))"
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    41
by(simp_all add: bal.simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    42
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    43
text\<open>Some of the following lemmas take advantage of the fact
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    44
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
    45
  
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    46
lemma size_bal: "bal n xs = (t,ys) \<Longrightarrow> size t = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    47
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    48
  case (1 n xs)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    49
  thus ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    50
    by(cases "n=0")
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    51
      (auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    52
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    53
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    54
lemma bal_inorder:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    55
  "\<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
    56
  \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    57
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    58
  case (1 n xs) show ?case
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    59
  proof cases
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    60
    assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    61
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    62
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    63
    let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    64
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    65
      b1: "bal ?n1 xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    66
      b2: "bal ?n2 (tl xs') = (r,ys)" and
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    67
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    68
      by(auto simp: Let_def bal_simps split: prod.splits)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    69
    have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    70
      using b1 "1.prems" by(intro "1.IH"(1)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    71
    have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    72
      using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    73
    have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    74
    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
    75
      by (metis Suc_eq_plus1 take_Suc)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    76
    hence *: "inorder t = take n xs" using t IH1 IH2
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    77
      using take_add[of ?n1 "?n2+1" xs] by(simp)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    78
    have "n - n div 2 + n div 2 = n" by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    79
    hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    80
    thus ?thesis using * by blast
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    81
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    82
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    83
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    84
corollary inorder_bal_list[simp]:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    85
  "n \<le> length xs \<Longrightarrow> inorder(bal_list n xs) = take n xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    86
unfolding bal_list_def by (metis bal_inorder eq_fst_iff)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    87
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    88
corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    89
by(simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    90
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    91
corollary inorder_bal_tree:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    92
  "n \<le> size t \<Longrightarrow> inorder(bal_tree n t) = take n (inorder t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    93
by(simp add: bal_tree_def)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    94
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
    95
corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    96
by(simp add: balance_tree_def inorder_bal_tree)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    97
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    98
corollary size_bal_list[simp]: "size(bal_list n xs) = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
    99
unfolding bal_list_def by (metis prod.collapse size_bal)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   100
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   101
corollary size_balance_list[simp]: "size(balance_list xs) = length xs"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   102
by (simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   103
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   104
corollary size_bal_tree[simp]: "size(bal_tree n t) = n"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   105
by(simp add: bal_tree_def)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   106
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   107
corollary size_balance_tree[simp]: "size(balance_tree t) = size t"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   108
by(simp add: balance_tree_def)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   109
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   110
lemma min_height_bal:
66515
85c505c98332 reorganized and added log-related lemmas
nipkow
parents: 66510
diff changeset
   111
  "bal n xs = (t,ys) \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   112
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   113
  case (1 n xs) show ?case
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   114
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   115
    assume "n = 0" thus ?thesis
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   116
      using "1.prems" by (simp add: bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   117
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   118
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   119
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   120
      b1: "bal (n div 2) xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   121
      b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   122
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   123
      by(auto simp: bal_simps Let_def split: prod.splits)
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   124
    let ?log1 = "nat (floor(log 2 (n div 2 + 1)))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   125
    let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   126
    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
   127
    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
   128
    have "(n+1) div 2 \<ge> 1" by arith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   129
    hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   130
    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
   131
    hence le: "?log2 \<le> ?log1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   132
      by(simp add: nat_mono floor_mono)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   133
    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
   134
    also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   135
    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
   136
    also have "nat (floor(log 2 ((n+1) div 2))) + 1
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   137
       = nat (floor(log 2 ((n+1) div 2) + 1))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   138
      using 0 by linarith
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   139
    also have "\<dots> = nat (floor(log 2 (n + 1)))"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   140
      using floor_log2_div2[of "n+1"] by (simp add: log_mult)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   141
    finally show ?thesis .
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   142
  qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   143
qed
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   144
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   145
lemma height_bal:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   146
  "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
   147
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   148
  case (1 n xs) show ?case
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   149
  proof cases
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   150
    assume "n = 0" thus ?thesis
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   151
      using "1.prems" by (simp add: bal_simps)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   152
  next
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   153
    assume [arith]: "n \<noteq> 0"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   154
    from "1.prems" obtain l r xs' where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   155
      b1: "bal (n div 2) xs = (l,xs')" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   156
      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
   157
      t: "t = \<langle>l, hd xs', r\<rangle>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   158
      by(auto simp: bal_simps Let_def split: prod.splits)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   159
    let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   160
    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
   161
    have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   162
    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
   163
    have 0: "log 2 (n div 2 + 1) \<ge> 0" by auto
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   164
    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
   165
    hence le: "?log2 \<le> ?log1"
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   166
      by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   167
    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
   168
    also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1)
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   169
    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
   170
    also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>"
66515
85c505c98332 reorganized and added log-related lemmas
nipkow
parents: 66510
diff changeset
   171
      using ceiling_log2_div2[of "n+1"] by (simp)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   172
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   173
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   174
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   175
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   176
lemma balanced_bal:
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   177
  assumes "bal n xs = (t,ys)" shows "balanced t"
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   178
unfolding balanced_def
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   179
using height_bal[OF assms] min_height_bal[OF assms]
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   180
by linarith
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   181
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   182
lemma height_bal_list:
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   183
  "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
   184
unfolding bal_list_def by (metis height_bal prod.collapse)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   185
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   186
lemma height_balance_list:
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   187
  "height (balance_list xs) = nat \<lceil>log 2 (length xs + 1)\<rceil>"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   188
by (simp add: balance_list_def height_bal_list)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   189
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   190
corollary height_bal_tree:
66515
85c505c98332 reorganized and added log-related lemmas
nipkow
parents: 66510
diff changeset
   191
  "n \<le> length xs \<Longrightarrow> height (bal_tree n t) = nat\<lceil>log 2 (n + 1)\<rceil>"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   192
unfolding bal_list_def bal_tree_def
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   193
using height_bal prod.exhaust_sel by blast
64018
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   194
c6eb691770d8 replaced floorlog by floor/ceiling(log .)
nipkow
parents: 63861
diff changeset
   195
corollary height_balance_tree:
66515
85c505c98332 reorganized and added log-related lemmas
nipkow
parents: 66510
diff changeset
   196
  "height (balance_tree t) = nat\<lceil>log 2 (size t + 1)\<rceil>"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   197
by (simp add: bal_tree_def balance_tree_def height_bal_list)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   198
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   199
corollary balanced_bal_list[simp]: "balanced (bal_list n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   200
unfolding bal_list_def by (metis  balanced_bal prod.collapse)
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   201
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   202
corollary balanced_balance_list[simp]: "balanced (balance_list xs)"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   203
by (simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   204
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   205
corollary balanced_bal_tree[simp]: "balanced (bal_tree n t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   206
by (simp add: bal_tree_def)
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   207
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   208
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   209
by (simp add: balance_tree_def)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   210
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   211
lemma wbalanced_bal: "bal n xs = (t,ys) \<Longrightarrow> wbalanced t"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   212
proof(induction n xs arbitrary: t ys rule: bal.induct)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   213
  case (1 n xs)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   214
  show ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   215
  proof cases
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   216
    assume "n = 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   217
    thus ?thesis
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   218
      using "1.prems" by(simp add: bal_simps)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   219
  next
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   220
    assume "n \<noteq> 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   221
    with "1.prems" obtain l ys r zs where
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   222
      rec1: "bal (n div 2) xs = (l, ys)" and
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   223
      rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   224
      t: "t = \<langle>l, hd ys, r\<rangle>"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   225
      by(auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   226
    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
   227
    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
   228
    with l t size_bal[OF rec1] size_bal[OF rec2]
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   229
    show ?thesis by auto
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   230
  qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   231
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   232
64541
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   233
text\<open>An alternative proof via @{thm balanced_if_wbalanced}:\<close>
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   234
lemma "bal n xs = (t,ys) \<Longrightarrow> balanced t"
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   235
by(rule balanced_if_wbalanced[OF wbalanced_bal])
3d4331b65861 more lemmas
nipkow
parents: 64540
diff changeset
   236
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   237
lemma wbalanced_bal_list[simp]: "wbalanced (bal_list n xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   238
by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   239
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   240
lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   241
by(simp add: balance_list_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   242
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   243
lemma wbalanced_bal_tree[simp]: "wbalanced (bal_tree n t)"
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   244
by(simp add: bal_tree_def)
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   245
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   246
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)"
64444
daae191c9344 provided more efficient interface
nipkow
parents: 64065
diff changeset
   247
by (simp add: balance_tree_def)
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   248
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   249
hide_const (open) bal
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   250
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   251
end