merged
authornipkow
Mon Sep 23 17:15:44 2019 +0200 (4 weeks ago ago)
changeset 70938b3b84b71e398
parent 70937 cf7b5020c207
parent 70936 548420d389ea
child 70939 fd9614c98dd6
child 70940 5d06b7bb9d22
merged
     1.1 --- a/src/HOL/Data_Structures/Balance.thy	Mon Sep 23 14:08:49 2019 +0100
     1.2 +++ b/src/HOL/Data_Structures/Balance.thy	Mon Sep 23 17:15:44 2019 +0200
     1.3 @@ -38,43 +38,32 @@
     1.4    in (Node l (hd ys) r, zs))"
     1.5  by(simp_all add: bal.simps)
     1.6  
     1.7 -text\<open>Some of the following lemmas take advantage of the fact
     1.8 -that \<open>bal xs n\<close> yields a result even if \<open>n > length xs\<close>.\<close>
     1.9 -  
    1.10 -lemma size_bal: "bal n xs = (t,ys) \<Longrightarrow> size t = n"
    1.11 -proof(induction n xs arbitrary: t ys rule: bal.induct)
    1.12 -  case (1 n xs)
    1.13 -  thus ?case
    1.14 -    by(cases "n=0")
    1.15 -      (auto simp add: bal_simps Let_def split: prod.splits)
    1.16 -qed
    1.17 -
    1.18  lemma bal_inorder:
    1.19 -  "\<lbrakk> bal n xs = (t,ys); n \<le> length xs \<rbrakk>
    1.20 -  \<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs"
    1.21 -proof(induction n xs arbitrary: t ys rule: bal.induct)
    1.22 +  "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk>
    1.23 +  \<Longrightarrow> inorder t = take n xs \<and> zs = drop n xs"
    1.24 +proof(induction n xs arbitrary: t zs rule: bal.induct)
    1.25    case (1 n xs) show ?case
    1.26    proof cases
    1.27      assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps)
    1.28    next
    1.29      assume [arith]: "n \<noteq> 0"
    1.30 -    let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1"
    1.31 -    from "1.prems" obtain l r xs' where
    1.32 -      b1: "bal ?n1 xs = (l,xs')" and
    1.33 -      b2: "bal ?n2 (tl xs') = (r,ys)" and
    1.34 -      t: "t = \<langle>l, hd xs', r\<rangle>"
    1.35 +    let ?m = "n div 2" let ?m' = "n - 1 - ?m"
    1.36 +    from "1.prems"(2) obtain l r ys where
    1.37 +      b1: "bal ?m xs = (l,ys)" and
    1.38 +      b2: "bal ?m' (tl ys) = (r,zs)" and
    1.39 +      t: "t = \<langle>l, hd ys, r\<rangle>"
    1.40        by(auto simp: Let_def bal_simps split: prod.splits)
    1.41 -    have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs"
    1.42 -      using b1 "1.prems" by(intro "1.IH"(1)) auto
    1.43 -    have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')"
    1.44 -      using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto
    1.45 -    have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp
    1.46 -    hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)"
    1.47 +    have IH1: "inorder l = take ?m xs \<and> ys = drop ?m xs"
    1.48 +      using b1 "1.prems"(1) by(intro "1.IH"(1)) auto
    1.49 +    have IH2: "inorder r = take ?m' (tl ys) \<and> zs = drop ?m' (tl ys)"
    1.50 +      using b1 b2 IH1 "1.prems"(1) by(intro "1.IH"(2)) auto
    1.51 +    have "drop (n div 2) xs \<noteq> []" using "1.prems"(1) by simp
    1.52 +    hence "hd (drop ?m xs) # take ?m' (tl (drop ?m xs)) = take (?m' + 1) (drop ?m xs)"
    1.53        by (metis Suc_eq_plus1 take_Suc)
    1.54      hence *: "inorder t = take n xs" using t IH1 IH2
    1.55 -      using take_add[of ?n1 "?n2+1" xs] by(simp)
    1.56 +      using take_add[of ?m "?m'+1" xs] by(simp)
    1.57      have "n - n div 2 + n div 2 = n" by simp
    1.58 -    hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
    1.59 +    hence "zs = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
    1.60      thus ?thesis using * by blast
    1.61    qed
    1.62  qed
    1.63 @@ -93,41 +82,56 @@
    1.64  corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t"
    1.65  by(simp add: balance_tree_def inorder_bal_tree)
    1.66  
    1.67 -corollary size_bal_list[simp]: "size(bal_list n xs) = n"
    1.68 +
    1.69 +text\<open>The size lemmas below do not require the precondition @{prop"n \<le> length xs"}
    1.70 +(or  @{prop"n \<le> size t"}) that they come with. They could take advantage of the fact
    1.71 +that @{term "bal xs n"} yields a result even if @{prop "n > length xs"}.
    1.72 +In that case the result will contain one or more occurrences of @{term "hd []"}.
    1.73 +However, this is counter-intuitive and does not reflect the execution
    1.74 +in an eager functional language.\<close>
    1.75 +
    1.76 +lemma size_bal: "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> size t = n \<and> length zs = length xs - n"
    1.77 +by (metis bal_inorder length_drop length_inorder length_take min.absorb2)
    1.78 +
    1.79 +corollary size_bal_list[simp]: "n \<le> length xs \<Longrightarrow> size(bal_list n xs) = n"
    1.80  unfolding bal_list_def by (metis prod.collapse size_bal)
    1.81  
    1.82  corollary size_balance_list[simp]: "size(balance_list xs) = length xs"
    1.83  by (simp add: balance_list_def)
    1.84  
    1.85 -corollary size_bal_tree[simp]: "size(bal_tree n t) = n"
    1.86 +corollary size_bal_tree[simp]: "n \<le> size t \<Longrightarrow> size(bal_tree n t) = n"
    1.87  by(simp add: bal_tree_def)
    1.88  
    1.89  corollary size_balance_tree[simp]: "size(balance_tree t) = size t"
    1.90  by(simp add: balance_tree_def)
    1.91  
    1.92 +lemma pre_rec2: "\<lbrakk> n \<le> length xs; bal (n div 2) xs = (l, ys) \<rbrakk>
    1.93 + \<Longrightarrow> (n - 1 - n div 2) \<le> length(tl ys)"
    1.94 +using size_bal[of "n div 2" xs l ys] by simp
    1.95 +
    1.96  lemma min_height_bal:
    1.97 -  "bal n xs = (t,ys) \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)"
    1.98 -proof(induction n xs arbitrary: t ys rule: bal.induct)
    1.99 -  case (1 n xs) show ?case
   1.100 +  "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)"
   1.101 +proof(induction n xs arbitrary: t zs rule: bal.induct)
   1.102 +  case (1 n xs)
   1.103 +  show ?case
   1.104    proof cases
   1.105 -    assume "n = 0" thus ?thesis
   1.106 -      using "1.prems" by (simp add: bal_simps)
   1.107 +    assume "n = 0" thus ?thesis using "1.prems"(2) by (simp add: bal_simps)
   1.108    next
   1.109      assume [arith]: "n \<noteq> 0"
   1.110 -    from "1.prems" obtain l r xs' where
   1.111 -      b1: "bal (n div 2) xs = (l,xs')" and
   1.112 -      b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
   1.113 -      t: "t = \<langle>l, hd xs', r\<rangle>"
   1.114 +    from "1.prems" obtain l r ys where
   1.115 +      b1: "bal (n div 2) xs = (l,ys)" and
   1.116 +      b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and
   1.117 +      t: "t = \<langle>l, hd ys, r\<rangle>"
   1.118        by(auto simp: bal_simps Let_def split: prod.splits)
   1.119      let ?log1 = "nat (floor(log 2 (n div 2 + 1)))"
   1.120      let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))"
   1.121 -    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp
   1.122 -    have IH2: "min_height r = ?log2" using "1.IH"(2) b1 b2 by simp
   1.123 +    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp
   1.124 +    have IH2: "min_height r = ?log2"
   1.125 +      using "1.prems"(1) size_bal[OF _ b1] size_bal[OF _ b2] b1 b2 by(intro "1.IH"(2)) auto
   1.126      have "(n+1) div 2 \<ge> 1" by arith
   1.127      hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp
   1.128      have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
   1.129 -    hence le: "?log2 \<le> ?log1"
   1.130 -      by(simp add: nat_mono floor_mono)
   1.131 +    hence le: "?log2 \<le> ?log1" by(simp add: nat_mono floor_mono)
   1.132      have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
   1.133      also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2)
   1.134      also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith
   1.135 @@ -141,24 +145,27 @@
   1.136  qed
   1.137  
   1.138  lemma height_bal:
   1.139 -  "bal n xs = (t,ys) \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>"
   1.140 -proof(induction n xs arbitrary: t ys rule: bal.induct)
   1.141 +  "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>"
   1.142 +proof(induction n xs arbitrary: t zs rule: bal.induct)
   1.143    case (1 n xs) show ?case
   1.144    proof cases
   1.145      assume "n = 0" thus ?thesis
   1.146        using "1.prems" by (simp add: bal_simps)
   1.147    next
   1.148      assume [arith]: "n \<noteq> 0"
   1.149 -    from "1.prems" obtain l r xs' where
   1.150 -      b1: "bal (n div 2) xs = (l,xs')" and
   1.151 -      b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and
   1.152 -      t: "t = \<langle>l, hd xs', r\<rangle>"
   1.153 +    from "1.prems" obtain l r ys where
   1.154 +      b1: "bal (n div 2) xs = (l,ys)" and
   1.155 +      b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and
   1.156 +      t: "t = \<langle>l, hd ys, r\<rangle>"
   1.157        by(auto simp: bal_simps Let_def split: prod.splits)
   1.158      let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>"
   1.159      let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>"
   1.160 -    have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp
   1.161 -    have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp
   1.162 -    have 0: "log 2 (n div 2 + 1) \<ge> 0" by auto
   1.163 +    have 1: "n div 2 \<le> length xs" using "1.prems"(1) by linarith
   1.164 +    have 2: "n - 1 - n div 2 \<le> length (tl ys)" using "1.prems"(1) size_bal[OF 1 b1] by simp
   1.165 +    have IH1: "height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp
   1.166 +    have IH2: "height r = ?log2"
   1.167 +      using b1 b2 size_bal[OF _ b1] size_bal[OF _ b2] "1.prems"(1) by(intro "1.IH"(2)) auto
   1.168 +    have 0: "log 2 (n div 2 + 1) \<ge> 0" by simp
   1.169      have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
   1.170      hence le: "?log2 \<le> ?log1"
   1.171        by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq)
   1.172 @@ -172,7 +179,7 @@
   1.173  qed
   1.174  
   1.175  lemma balanced_bal:
   1.176 -  assumes "bal n xs = (t,ys)" shows "balanced t"
   1.177 +  assumes "n \<le> length xs" "bal n xs = (t,ys)" shows "balanced t"
   1.178  unfolding balanced_def
   1.179  using height_bal[OF assms] min_height_bal[OF assms]
   1.180  by linarith
   1.181 @@ -186,59 +193,59 @@
   1.182  by (simp add: balance_list_def height_bal_list)
   1.183  
   1.184  corollary height_bal_tree:
   1.185 -  "n \<le> length xs \<Longrightarrow> height (bal_tree n t) = nat\<lceil>log 2 (n + 1)\<rceil>"
   1.186 +  "n \<le> size t \<Longrightarrow> height (bal_tree n t) = nat\<lceil>log 2 (n + 1)\<rceil>"
   1.187  unfolding bal_list_def bal_tree_def
   1.188 -using height_bal prod.exhaust_sel by blast
   1.189 +by (metis bal_list_def height_bal_list length_inorder)
   1.190  
   1.191  corollary height_balance_tree:
   1.192    "height (balance_tree t) = nat\<lceil>log 2 (size t + 1)\<rceil>"
   1.193  by (simp add: bal_tree_def balance_tree_def height_bal_list)
   1.194  
   1.195 -corollary balanced_bal_list[simp]: "balanced (bal_list n xs)"
   1.196 +corollary balanced_bal_list[simp]: "n \<le> length xs \<Longrightarrow> balanced (bal_list n xs)"
   1.197  unfolding bal_list_def by (metis  balanced_bal prod.collapse)
   1.198  
   1.199  corollary balanced_balance_list[simp]: "balanced (balance_list xs)"
   1.200  by (simp add: balance_list_def)
   1.201  
   1.202 -corollary balanced_bal_tree[simp]: "balanced (bal_tree n t)"
   1.203 +corollary balanced_bal_tree[simp]: "n \<le> size t \<Longrightarrow> balanced (bal_tree n t)"
   1.204  by (simp add: bal_tree_def)
   1.205  
   1.206  corollary balanced_balance_tree[simp]: "balanced (balance_tree t)"
   1.207  by (simp add: balance_tree_def)
   1.208  
   1.209 -lemma wbalanced_bal: "bal n xs = (t,ys) \<Longrightarrow> wbalanced t"
   1.210 +lemma wbalanced_bal: "\<lbrakk> n \<le> length xs; bal n xs = (t,ys) \<rbrakk> \<Longrightarrow> wbalanced t"
   1.211  proof(induction n xs arbitrary: t ys rule: bal.induct)
   1.212    case (1 n xs)
   1.213    show ?case
   1.214    proof cases
   1.215      assume "n = 0"
   1.216 -    thus ?thesis
   1.217 -      using "1.prems" by(simp add: bal_simps)
   1.218 +    thus ?thesis using "1.prems"(2) by(simp add: bal_simps)
   1.219    next
   1.220 -    assume "n \<noteq> 0"
   1.221 +    assume [arith]: "n \<noteq> 0"
   1.222      with "1.prems" obtain l ys r zs where
   1.223        rec1: "bal (n div 2) xs = (l, ys)" and
   1.224        rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and
   1.225        t: "t = \<langle>l, hd ys, r\<rangle>"
   1.226        by(auto simp add: bal_simps Let_def split: prod.splits)
   1.227 -    have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl rec1] .
   1.228 -    have "wbalanced r" using "1.IH"(2)[OF \<open>n\<noteq>0\<close> refl rec1[symmetric] refl rec2] .
   1.229 -    with l t size_bal[OF rec1] size_bal[OF rec2]
   1.230 +    have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl _ rec1] "1.prems"(1) by linarith
   1.231 +    have "wbalanced r"
   1.232 +      using rec1 rec2 pre_rec2[OF "1.prems"(1) rec1] by(intro "1.IH"(2)) auto
   1.233 +    with l t size_bal[OF _ rec1] size_bal[OF _ rec2] "1.prems"(1)
   1.234      show ?thesis by auto
   1.235    qed
   1.236  qed
   1.237  
   1.238  text\<open>An alternative proof via @{thm balanced_if_wbalanced}:\<close>
   1.239 -lemma "bal n xs = (t,ys) \<Longrightarrow> balanced t"
   1.240 +lemma "\<lbrakk> n \<le> length xs; bal n xs = (t,ys) \<rbrakk> \<Longrightarrow> balanced t"
   1.241  by(rule balanced_if_wbalanced[OF wbalanced_bal])
   1.242  
   1.243 -lemma wbalanced_bal_list[simp]: "wbalanced (bal_list n xs)"
   1.244 +lemma wbalanced_bal_list[simp]: "n \<le> length xs \<Longrightarrow> wbalanced (bal_list n xs)"
   1.245  by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal)
   1.246  
   1.247  lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)"
   1.248  by(simp add: balance_list_def)
   1.249  
   1.250 -lemma wbalanced_bal_tree[simp]: "wbalanced (bal_tree n t)"
   1.251 +lemma wbalanced_bal_tree[simp]: "n \<le> size t \<Longrightarrow> wbalanced (bal_tree n t)"
   1.252  by(simp add: bal_tree_def)
   1.253  
   1.254  lemma wbalanced_balance_tree: "wbalanced (balance_tree t)"