src/HOL/Data_Structures/Balance_List.thy
author nipkow
Fri, 12 Aug 2016 08:20:17 +0200
changeset 63663 28d1deca302e
parent 63643 f9ad2e591957
child 63755 182c111190e5
permissions -rw-r--r--
Extracted floorlog and bitlen to separate theory Log_Nat
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     1
(* Tobias Nipkow *)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     2
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     3
section \<open>Creating a Balanced Tree from a List\<close>
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     4
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     5
theory Balance_List
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     6
imports
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     7
  "~~/src/HOL/Library/Tree"
63663
28d1deca302e Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents: 63643
diff changeset
     8
  "~~/src/HOL/Library/Log_Nat"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
     9
begin
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    10
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    11
fun bal :: "'a list \<Rightarrow> nat \<Rightarrow> 'a tree * 'a list" where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    12
"bal xs n = (if n=0 then (Leaf,xs) else
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    13
 (let m = n div 2;
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    14
      (l, ys) = bal xs m;
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    15
      (r, zs) = bal (tl ys) (n-1-m)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    16
  in (Node l (hd ys) r, zs)))"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    17
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    18
declare bal.simps[simp del]
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    19
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    20
definition "balance xs = fst (bal xs (length xs))"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    21
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    22
lemma bal_inorder:
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    23
  "bal xs n = (t,ys) \<Longrightarrow> n \<le> length xs \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    24
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    25
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    26
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    27
    assume "n = 0" thus ?thesis using 1 by (simp add: bal.simps)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    28
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    29
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    30
    let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    31
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    32
      b1: "bal xs ?n1 = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    33
      b2: "bal (tl xs') ?n2 = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    34
      t: "t = \<langle>l, hd xs', r\<rangle>"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    35
      using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    36
    have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    37
      using b1 "1.prems" by(intro "1.IH"(1)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    38
    have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    39
      using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    40
    have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    41
    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
    42
      by (metis Suc_eq_plus1 take_Suc)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    43
    hence *: "inorder t = take n xs" using t IH1 IH2
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    44
      using take_add[of ?n1 "?n2+1" xs] by(simp)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    45
    have "n - n div 2 + n div 2 = n" by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    46
    hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    47
    thus ?thesis using * by blast
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    48
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    49
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    50
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    51
corollary balance_inorder: "inorder(balance xs) = xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    52
using bal_inorder[of xs "length xs"]
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    53
by (metis balance_def order_refl prod.collapse take_all)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    54
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    55
lemma bal_height: "bal xs n = (t,ys) \<Longrightarrow> height t = floorlog 2 n"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    56
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    57
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    58
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    59
    assume "n = 0" thus ?thesis
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    60
      using "1.prems" by (simp add: floorlog_def bal.simps)
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
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    64
      b1: "bal xs (n div 2) = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    65
      b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    66
      t: "t = \<langle>l, hd xs', r\<rangle>"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    67
      using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    68
    let ?log1 = "floorlog 2 (n div 2)"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    69
    let ?log2 = "floorlog 2 (n - 1 - n div 2)"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    70
    have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    71
    have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    72
    have "n div 2 \<ge> n - 1 - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    73
    hence le: "?log2 \<le> ?log1" by(simp add:floorlog_mono)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    74
    have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    75
    also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1)
63663
28d1deca302e Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents: 63643
diff changeset
    76
    also have "\<dots> = floorlog 2 n" by (simp add: compute_floorlog)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    77
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    78
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    79
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    80
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    81
lemma bal_min_height:
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    82
  "bal xs n = (t,ys) \<Longrightarrow> min_height t = floorlog 2 (n + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    83
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    84
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    85
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    86
    assume "n = 0" thus ?thesis
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    87
      using "1.prems" by (simp add: floorlog_def bal.simps)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    88
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    89
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    90
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    91
      b1: "bal xs (n div 2) = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    92
      b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    93
      t: "t = \<langle>l, hd xs', r\<rangle>"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    94
      using bal.simps[of xs n] by(auto simp: Let_def split: prod.splits)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    95
    let ?log1 = "floorlog 2 (n div 2 + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    96
    let ?log2 = "floorlog 2 (n - 1 - n div 2 + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    97
    let ?log2' = "floorlog 2 (n - n div 2) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    98
    have "n - 1 - n div 2 + 1 = n - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    99
    hence IH2: "min_height r = ?log2'" using "1.IH"(2) b1 b2 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   100
    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   101
    have *: "floorlog 2 (n - n div 2) \<ge> 1" by (simp add: floorlog_def)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   102
    have "n div 2 + 1 \<ge> n - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   103
    with * have le: "?log2' \<le> ?log1" by(simp add: floorlog_mono diff_le_mono)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   104
    have "min_height t = min ?log1 ?log2' + 1" by (simp add: t IH1 IH2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   105
    also have "\<dots> = ?log2' + 1" using le by (simp add: min_absorb2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   106
    also have "\<dots> = floorlog 2 (n - n div 2)" by(simp add: floorlog_def)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   107
    also have "n - n div 2 = (n+1) div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   108
    also have "floorlog 2 \<dots> = floorlog 2 (n+1) - 1"
63663
28d1deca302e Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents: 63643
diff changeset
   109
      by (simp add: compute_floorlog)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   110
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   111
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   112
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   113
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   114
lemma balanced_bal:
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   115
  assumes "bal xs n = (t,ys)" shows "height t - min_height t \<le> 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   116
proof -
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   117
  have "floorlog 2 n \<le> floorlog 2 (n+1)" by (rule floorlog_mono) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   118
  thus ?thesis
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   119
    using bal_height[OF assms] bal_min_height[OF assms] by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   120
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   121
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   122
corollary balanced_balance: "height(balance xs) - min_height(balance xs) \<le> 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   123
by (metis balance_def balanced_bal prod.collapse)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   124
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   125
end