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