src/HOL/Data_Structures/Balance.thy
author wenzelm
Thu, 06 Oct 2016 11:13:12 +0200
changeset 64062 a7352cbde7d7
parent 63861 90360390a916
child 64018 c6eb691770d8
permissions -rw-r--r--
misc tuning and clarification;
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
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
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    20
definition balance_list :: "'a list \<Rightarrow> 'a tree" where
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    21
"balance_list xs = fst (bal xs (length xs))"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    22
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    23
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    24
"balance_tree = balance_list o inorder"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    25
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    26
lemma bal_simps:
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    27
  "bal xs 0 = (Leaf, xs)"
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    28
  "n > 0 \<Longrightarrow>
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    29
   bal xs n =
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    30
  (let m = n div 2;
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    31
      (l, ys) = Balance.bal xs m;
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    32
      (r, zs) = Balance.bal (tl ys) (n-1-m)
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    33
  in (Node l (hd ys) r, zs))"
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    34
by(simp_all add: bal.simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    35
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    36
text\<open>The following lemmas take advantage of the fact
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    37
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
    38
  
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    39
lemma size_bal: "bal xs n = (t,ys) \<Longrightarrow> size t = n"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    40
proof(induction xs n arbitrary: t ys rule: bal.induct)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    41
  case (1 xs n)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    42
  thus ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    43
    by(cases "n=0")
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    44
      (auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    45
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
    46
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    47
lemma bal_inorder:
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63663
diff changeset
    48
  "\<lbrakk> bal xs n = (t,ys); n \<le> length xs \<rbrakk>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63663
diff changeset
    49
  \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    50
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    51
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    52
  proof cases
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    53
    assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    54
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    55
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    56
    let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    57
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    58
      b1: "bal xs ?n1 = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    59
      b2: "bal (tl xs') ?n2 = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    60
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    61
      by(auto simp: Let_def bal_simps split: prod.splits)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    62
    have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    63
      using b1 "1.prems" by(intro "1.IH"(1)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    64
    have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    65
      using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    66
    have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    67
    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
    68
      by (metis Suc_eq_plus1 take_Suc)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    69
    hence *: "inorder t = take n xs" using t IH1 IH2
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    70
      using take_add[of ?n1 "?n2+1" xs] by(simp)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    71
    have "n - n div 2 + n div 2 = n" by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    72
    hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    73
    thus ?thesis using * by blast
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    74
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    75
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    76
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    77
corollary inorder_balance_list: "inorder(balance_list xs) = xs"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    78
using bal_inorder[of xs "length xs"]
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
    79
by (metis balance_list_def order_refl prod.collapse take_all)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    80
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    81
lemma bal_height: "bal xs n = (t,ys) \<Longrightarrow> height t = floorlog 2 n"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    82
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    83
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    84
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    85
    assume "n = 0" thus ?thesis
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    86
      using "1.prems" by (simp add: floorlog_def bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    87
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    88
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    89
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    90
      b1: "bal xs (n div 2) = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    91
      b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    92
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
    93
      by(auto simp: bal_simps Let_def split: prod.splits)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    94
    let ?log1 = "floorlog 2 (n div 2)"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    95
    let ?log2 = "floorlog 2 (n - 1 - n div 2)"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    96
    have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    97
    have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    98
    have "n div 2 \<ge> n - 1 - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
    99
    hence le: "?log2 \<le> ?log1" by(simp add:floorlog_mono)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   100
    have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   101
    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
   102
    also have "\<dots> = floorlog 2 n" by (simp add: compute_floorlog)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   103
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   104
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   105
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   106
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   107
lemma bal_min_height:
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   108
  "bal xs n = (t,ys) \<Longrightarrow> min_height t = floorlog 2 (n + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   109
proof(induction xs n arbitrary: t ys rule: bal.induct)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   110
  case (1 xs n) show ?case
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   111
  proof cases
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   112
    assume "n = 0" thus ?thesis
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   113
      using "1.prems" by (simp add: floorlog_def bal_simps)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   114
  next
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   115
    assume [arith]: "n \<noteq> 0"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   116
    from "1.prems" obtain l r xs' where
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   117
      b1: "bal xs (n div 2) = (l,xs')" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   118
      b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   119
      t: "t = \<langle>l, hd xs', r\<rangle>"
63843
ade7c3a20917 more simp rules
nipkow
parents: 63829
diff changeset
   120
      by(auto simp: bal_simps Let_def split: prod.splits)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   121
    let ?log1 = "floorlog 2 (n div 2 + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   122
    let ?log2 = "floorlog 2 (n - 1 - n div 2 + 1) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   123
    let ?log2' = "floorlog 2 (n - n div 2) - 1"
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   124
    have "n - 1 - n div 2 + 1 = n - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   125
    hence IH2: "min_height r = ?log2'" using "1.IH"(2) b1 b2 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   126
    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   127
    have *: "floorlog 2 (n - n div 2) \<ge> 1" by (simp add: floorlog_def)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   128
    have "n div 2 + 1 \<ge> n - n div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   129
    with * have le: "?log2' \<le> ?log1" by(simp add: floorlog_mono diff_le_mono)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   130
    have "min_height t = min ?log1 ?log2' + 1" by (simp add: t IH1 IH2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   131
    also have "\<dots> = ?log2' + 1" using le by (simp add: min_absorb2)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   132
    also have "\<dots> = floorlog 2 (n - n div 2)" by(simp add: floorlog_def)
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   133
    also have "n - n div 2 = (n+1) div 2" by arith
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   134
    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
   135
      by (simp add: compute_floorlog)
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   136
    finally show ?thesis .
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   137
  qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   138
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   139
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   140
lemma balanced_bal:
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63663
diff changeset
   141
  assumes "bal xs n = (t,ys)" shows "balanced t"
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   142
proof -
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   143
  have "floorlog 2 n \<le> floorlog 2 (n+1)" by (rule floorlog_mono) auto
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   144
  thus ?thesis unfolding balanced_def
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   145
    using bal_height[OF assms] bal_min_height[OF assms] by linarith
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   146
qed
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   147
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   148
corollary size_balance_list[simp]: "size(balance_list xs) = length xs"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   149
by (metis inorder_balance_list length_inorder)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   150
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   151
corollary balanced_balance_list[simp]: "balanced (balance_list xs)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   152
by (metis balance_list_def balanced_bal prod.collapse)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   153
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   154
lemma height_balance_list: "height(balance_list xs) = floorlog 2 (length xs)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   155
by (metis bal_height balance_list_def prod.collapse)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   156
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   157
lemma inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   158
by(simp add: balance_tree_def inorder_balance_list)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   159
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   160
lemma size_balance_tree[simp]: "size(balance_tree t) = size t"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   161
by(simp add: balance_tree_def inorder_balance_list)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   162
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   163
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   164
by (simp add: balance_tree_def)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   165
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   166
lemma height_balance_tree: "height(balance_tree t) = floorlog 2 (size t)"
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   167
by(simp add: balance_tree_def height_balance_list)
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   168
63861
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   169
lemma wbalanced_bal: "bal xs n = (t,ys) \<Longrightarrow> wbalanced t"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   170
proof(induction xs n arbitrary: t ys rule: bal.induct)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   171
  case (1 xs n)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   172
  show ?case
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   173
  proof cases
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   174
    assume "n = 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   175
    thus ?thesis
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   176
      using "1.prems" by(simp add: bal_simps)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   177
  next
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   178
    assume "n \<noteq> 0"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   179
    with "1.prems" obtain l ys r zs where
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   180
      rec1: "bal xs (n div 2) = (l, ys)" and
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   181
      rec2: "bal (tl ys) (n - 1 - n div 2) = (r, zs)" and
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   182
      t: "t = \<langle>l, hd ys, r\<rangle>"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   183
      by(auto simp add: bal_simps Let_def split: prod.splits)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   184
    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
   185
    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
   186
    with l t size_bal[OF rec1] size_bal[OF rec2]
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   187
    show ?thesis by auto
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   188
  qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   189
qed
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   190
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   191
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)"
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   192
by(simp add: balance_tree_def balance_list_def)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   193
  (metis prod.collapse wbalanced_bal)
90360390a916 reorganization, more funs and lemmas
nipkow
parents: 63843
diff changeset
   194
63829
6a05c8cbf7de More on balancing; renamed theory to Balance
nipkow
parents: 63755
diff changeset
   195
hide_const (open) bal
63643
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   196
f9ad2e591957 New theory Balance_List
nipkow
parents:
diff changeset
   197
end