merged
authornipkow
Tue Sep 24 17:36:14 2019 +0200 (3 weeks ago ago)
changeset 70943c5232e6fb10b
parent 70942 07673e7cb5e6
parent 70941 401cd34711b5
child 70944 3fb16bed5d6c
merged
     1.1 --- a/src/HOL/Data_Structures/Balance.thy	Tue Sep 24 16:17:37 2019 +0200
     1.2 +++ b/src/HOL/Data_Structures/Balance.thy	Tue Sep 24 17:36:14 2019 +0200
     1.3 @@ -40,7 +40,7 @@
     1.4  
     1.5  lemma bal_inorder:
     1.6    "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk>
     1.7 -  \<Longrightarrow> inorder t = take n xs \<and> zs = drop n xs"
     1.8 +  \<Longrightarrow> xs = inorder t @ zs \<and> size t = n"
     1.9  proof(induction n xs arbitrary: t zs rule: bal.induct)
    1.10    case (1 n xs) show ?case
    1.11    proof cases
    1.12 @@ -53,24 +53,18 @@
    1.13        b2: "bal ?m' (tl ys) = (r,zs)" and
    1.14        t: "t = \<langle>l, hd ys, r\<rangle>"
    1.15        by(auto simp: Let_def bal_simps split: prod.splits)
    1.16 -    have IH1: "inorder l = take ?m xs \<and> ys = drop ?m xs"
    1.17 +    have IH1: "xs = inorder l @ ys \<and> size l = ?m"
    1.18        using b1 "1.prems"(1) by(intro "1.IH"(1)) auto
    1.19 -    have IH2: "inorder r = take ?m' (tl ys) \<and> zs = drop ?m' (tl ys)"
    1.20 +    have IH2: "tl ys = inorder r @ zs \<and> size r = ?m'"
    1.21        using b1 b2 IH1 "1.prems"(1) by(intro "1.IH"(2)) auto
    1.22 -    have "drop (n div 2) xs \<noteq> []" using "1.prems"(1) by simp
    1.23 -    hence "hd (drop ?m xs) # take ?m' (tl (drop ?m xs)) = take (?m' + 1) (drop ?m xs)"
    1.24 -      by (metis Suc_eq_plus1 take_Suc)
    1.25 -    hence *: "inorder t = take n xs" using t IH1 IH2
    1.26 -      using take_add[of ?m "?m'+1" xs] by(simp)
    1.27 -    have "n - n div 2 + n div 2 = n" by simp
    1.28 -    hence "zs = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric])
    1.29 -    thus ?thesis using * by blast
    1.30 +    show ?thesis using t IH1 IH2  "1.prems"(1) hd_Cons_tl[of ys] by fastforce
    1.31    qed
    1.32  qed
    1.33  
    1.34  corollary inorder_bal_list[simp]:
    1.35    "n \<le> length xs \<Longrightarrow> inorder(bal_list n xs) = take n xs"
    1.36 -unfolding bal_list_def by (metis bal_inorder eq_fst_iff)
    1.37 +unfolding bal_list_def
    1.38 +by (metis (mono_tags) prod.collapse[of "bal n xs"] append_eq_conv_conj bal_inorder length_inorder)
    1.39  
    1.40  corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs"
    1.41  by(simp add: balance_list_def)
    1.42 @@ -83,18 +77,18 @@
    1.43  by(simp add: balance_tree_def inorder_bal_tree)
    1.44  
    1.45  
    1.46 -text\<open>The size lemmas below do not require the precondition @{prop"n \<le> length xs"}
    1.47 +text\<open>The length/size lemmas below do not require the precondition @{prop"n \<le> length xs"}
    1.48  (or  @{prop"n \<le> size t"}) that they come with. They could take advantage of the fact
    1.49  that @{term "bal xs n"} yields a result even if @{prop "n > length xs"}.
    1.50  In that case the result will contain one or more occurrences of @{term "hd []"}.
    1.51  However, this is counter-intuitive and does not reflect the execution
    1.52  in an eager functional language.\<close>
    1.53  
    1.54 -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.55 -by (metis bal_inorder length_drop length_inorder length_take min.absorb2)
    1.56 +lemma bal_length: "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> length zs = length xs - n"
    1.57 +using bal_inorder by fastforce
    1.58  
    1.59  corollary size_bal_list[simp]: "n \<le> length xs \<Longrightarrow> size(bal_list n xs) = n"
    1.60 -unfolding bal_list_def by (metis prod.collapse size_bal)
    1.61 +unfolding bal_list_def using bal_inorder prod.exhaust_sel by blast
    1.62  
    1.63  corollary size_balance_list[simp]: "size(balance_list xs) = length xs"
    1.64  by (simp add: balance_list_def)
    1.65 @@ -105,10 +99,6 @@
    1.66  corollary size_balance_tree[simp]: "size(balance_tree t) = size t"
    1.67  by(simp add: balance_tree_def)
    1.68  
    1.69 -lemma pre_rec2: "\<lbrakk> n \<le> length xs; bal (n div 2) xs = (l, ys) \<rbrakk>
    1.70 - \<Longrightarrow> (n - 1 - n div 2) \<le> length(tl ys)"
    1.71 -using size_bal[of "n div 2" xs l ys] by simp
    1.72 -
    1.73  lemma min_height_bal:
    1.74    "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)"
    1.75  proof(induction n xs arbitrary: t zs rule: bal.induct)
    1.76 @@ -118,23 +108,24 @@
    1.77      assume "n = 0" thus ?thesis using "1.prems"(2) by (simp add: bal_simps)
    1.78    next
    1.79      assume [arith]: "n \<noteq> 0"
    1.80 +    let ?m = "n div 2" let ?m' = "n - 1 - ?m"
    1.81      from "1.prems" obtain l r ys where
    1.82 -      b1: "bal (n div 2) xs = (l,ys)" and
    1.83 -      b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and
    1.84 +      b1: "bal ?m xs = (l,ys)" and
    1.85 +      b2: "bal ?m' (tl ys) = (r,zs)" and
    1.86        t: "t = \<langle>l, hd ys, r\<rangle>"
    1.87        by(auto simp: bal_simps Let_def split: prod.splits)
    1.88 -    let ?log1 = "nat (floor(log 2 (n div 2 + 1)))"
    1.89 -    let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))"
    1.90 -    have IH1: "min_height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp
    1.91 -    have IH2: "min_height r = ?log2"
    1.92 -      using "1.prems"(1) size_bal[OF _ b1] size_bal[OF _ b2] b1 b2 by(intro "1.IH"(2)) auto
    1.93 +    let ?hl = "nat (floor(log 2 (?m + 1)))"
    1.94 +    let ?hr = "nat (floor(log 2 (?m' + 1)))"
    1.95 +    have IH1: "min_height l = ?hl" using "1.IH"(1) b1 "1.prems"(1) by simp
    1.96 +    have IH2: "min_height r = ?hr"
    1.97 +      using "1.prems"(1) bal_length[OF _ b1] b1 b2 by(intro "1.IH"(2)) auto
    1.98      have "(n+1) div 2 \<ge> 1" by arith
    1.99      hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp
   1.100 -    have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
   1.101 -    hence le: "?log2 \<le> ?log1" by(simp add: nat_mono floor_mono)
   1.102 -    have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
   1.103 -    also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2)
   1.104 -    also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith
   1.105 +    have "?m' \<le> ?m" by arith
   1.106 +    hence le: "?hr \<le> ?hl" by(simp add: nat_mono floor_mono)
   1.107 +    have "min_height t = min ?hl ?hr + 1" by (simp add: t IH1 IH2)
   1.108 +    also have "\<dots> = ?hr + 1" using le by (simp add: min_absorb2)
   1.109 +    also have "?m' + 1 = (n+1) div 2" by linarith
   1.110      also have "nat (floor(log 2 ((n+1) div 2))) + 1
   1.111         = nat (floor(log 2 ((n+1) div 2) + 1))"
   1.112        using 0 by linarith
   1.113 @@ -153,25 +144,24 @@
   1.114        using "1.prems" by (simp add: bal_simps)
   1.115    next
   1.116      assume [arith]: "n \<noteq> 0"
   1.117 +    let ?m = "n div 2" let ?m' = "n - 1 - ?m"
   1.118      from "1.prems" obtain l r ys where
   1.119 -      b1: "bal (n div 2) xs = (l,ys)" and
   1.120 -      b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and
   1.121 +      b1: "bal ?m xs = (l,ys)" and
   1.122 +      b2: "bal ?m' (tl ys) = (r,zs)" and
   1.123        t: "t = \<langle>l, hd ys, r\<rangle>"
   1.124        by(auto simp: bal_simps Let_def split: prod.splits)
   1.125 -    let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>"
   1.126 -    let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>"
   1.127 -    have 1: "n div 2 \<le> length xs" using "1.prems"(1) by linarith
   1.128 -    have 2: "n - 1 - n div 2 \<le> length (tl ys)" using "1.prems"(1) size_bal[OF 1 b1] by simp
   1.129 -    have IH1: "height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp
   1.130 -    have IH2: "height r = ?log2"
   1.131 -      using b1 b2 size_bal[OF _ b1] size_bal[OF _ b2] "1.prems"(1) by(intro "1.IH"(2)) auto
   1.132 -    have 0: "log 2 (n div 2 + 1) \<ge> 0" by simp
   1.133 -    have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith
   1.134 -    hence le: "?log2 \<le> ?log1"
   1.135 +    let ?hl = "nat \<lceil>log 2 (?m + 1)\<rceil>"
   1.136 +    let ?hr = "nat \<lceil>log 2 (?m' + 1)\<rceil>"
   1.137 +    have IH1: "height l = ?hl" using "1.IH"(1) b1 "1.prems"(1) by simp
   1.138 +    have IH2: "height r = ?hr"
   1.139 +      using b1 b2 bal_length[OF _ b1] "1.prems"(1) by(intro "1.IH"(2)) auto
   1.140 +    have 0: "log 2 (?m + 1) \<ge> 0" by simp
   1.141 +    have "?m' \<le> ?m" by arith
   1.142 +    hence le: "?hr \<le> ?hl"
   1.143        by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq)
   1.144 -    have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2)
   1.145 -    also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1)
   1.146 -    also have "\<dots> = nat \<lceil>log 2 (n div 2 + 1) + 1\<rceil>" using 0 by linarith
   1.147 +    have "height t = max ?hl ?hr + 1" by (simp add: t IH1 IH2)
   1.148 +    also have "\<dots> = ?hl + 1" using le by (simp add: max_absorb1)
   1.149 +    also have "\<dots> = nat \<lceil>log 2 (?m + 1) + 1\<rceil>" using 0 by linarith
   1.150      also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>"
   1.151        using ceiling_log2_div2[of "n+1"] by (simp)
   1.152      finally show ?thesis .
   1.153 @@ -229,8 +219,8 @@
   1.154        by(auto simp add: bal_simps Let_def split: prod.splits)
   1.155      have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl _ rec1] "1.prems"(1) by linarith
   1.156      have "wbalanced r"
   1.157 -      using rec1 rec2 pre_rec2[OF "1.prems"(1) rec1] by(intro "1.IH"(2)) auto
   1.158 -    with l t size_bal[OF _ rec1] size_bal[OF _ rec2] "1.prems"(1)
   1.159 +      using rec1 rec2 bal_length[OF _ rec1] "1.prems"(1) by(intro "1.IH"(2)) auto
   1.160 +    with l t bal_length[OF _ rec1] "1.prems"(1) bal_inorder[OF _ rec1] bal_inorder[OF _ rec2]
   1.161      show ?thesis by auto
   1.162    qed
   1.163  qed