simplify instance proofs for rat
authorhuffman
Thu May 10 21:02:36 2012 +0200 (2012-05-10)
changeset 4790754e3847f1669
parent 47906 09a896d295bd
child 47908 25686e1e0024
simplify instance proofs for rat
src/HOL/Rat.thy
     1.1 --- a/src/HOL/Rat.thy	Thu May 10 21:18:41 2012 +0200
     1.2 +++ b/src/HOL/Rat.thy	Thu May 10 21:02:36 2012 +0200
     1.3 @@ -400,200 +400,121 @@
     1.4  
     1.5  subsubsection {* The ordered field of rational numbers *}
     1.6  
     1.7 -instantiation rat :: linorder
     1.8 +lift_definition positive :: "rat \<Rightarrow> bool"
     1.9 +  is "\<lambda>x. 0 < fst x * snd x"
    1.10 +proof (clarsimp)
    1.11 +  fix a b c d :: int
    1.12 +  assume "b \<noteq> 0" and "d \<noteq> 0" and "a * d = c * b"
    1.13 +  hence "a * d * b * d = c * b * b * d"
    1.14 +    by simp
    1.15 +  hence "a * b * d\<twosuperior> = c * d * b\<twosuperior>"
    1.16 +    unfolding power2_eq_square by (simp add: mult_ac)
    1.17 +  hence "0 < a * b * d\<twosuperior> \<longleftrightarrow> 0 < c * d * b\<twosuperior>"
    1.18 +    by simp
    1.19 +  thus "0 < a * b \<longleftrightarrow> 0 < c * d"
    1.20 +    using `b \<noteq> 0` and `d \<noteq> 0`
    1.21 +    by (simp add: zero_less_mult_iff)
    1.22 +qed
    1.23 +
    1.24 +lemma positive_zero: "\<not> positive 0"
    1.25 +  by transfer simp
    1.26 +
    1.27 +lemma positive_add:
    1.28 +  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x + y)"
    1.29 +apply transfer
    1.30 +apply (simp add: zero_less_mult_iff)
    1.31 +apply (elim disjE, simp_all add: add_pos_pos add_neg_neg
    1.32 +  mult_pos_pos mult_pos_neg mult_neg_pos mult_neg_neg)
    1.33 +done
    1.34 +
    1.35 +lemma positive_mult:
    1.36 +  "positive x \<Longrightarrow> positive y \<Longrightarrow> positive (x * y)"
    1.37 +by transfer (drule (1) mult_pos_pos, simp add: mult_ac)
    1.38 +
    1.39 +lemma positive_minus:
    1.40 +  "\<not> positive x \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> positive (- x)"
    1.41 +by transfer (force simp: neq_iff zero_less_mult_iff mult_less_0_iff)
    1.42 +
    1.43 +instantiation rat :: linordered_field_inverse_zero
    1.44  begin
    1.45  
    1.46 -lift_definition less_eq_rat :: "rat \<Rightarrow> rat \<Rightarrow> bool"
    1.47 -  is "\<lambda>x y. (fst x * snd y) * (snd x * snd y) \<le> (fst y * snd x) * (snd x * snd y)"
    1.48 -proof (clarsimp)
    1.49 -  fix a b a' b' c d c' d'::int
    1.50 -  assume neq: "b \<noteq> 0"  "b' \<noteq> 0"  "d \<noteq> 0"  "d' \<noteq> 0"
    1.51 -  assume eq1: "a * b' = a' * b"
    1.52 -  assume eq2: "c * d' = c' * d"
    1.53 +definition
    1.54 +  "x < y \<longleftrightarrow> positive (y - x)"
    1.55 +
    1.56 +definition
    1.57 +  "x \<le> (y::rat) \<longleftrightarrow> x < y \<or> x = y"
    1.58 +
    1.59 +definition
    1.60 +  "abs (a::rat) = (if a < 0 then - a else a)"
    1.61 +
    1.62 +definition
    1.63 +  "sgn (a::rat) = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
    1.64  
    1.65 -  let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))"
    1.66 -  {
    1.67 -    fix a b c d x :: int assume x: "x \<noteq> 0"
    1.68 -    have "?le a b c d = ?le (a * x) (b * x) c d"
    1.69 -    proof -
    1.70 -      from x have "0 < x * x" by (auto simp add: zero_less_mult_iff)
    1.71 -      hence "?le a b c d =
    1.72 -        ((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))"
    1.73 -        by (simp add: mult_le_cancel_right)
    1.74 -      also have "... = ?le (a * x) (b * x) c d"
    1.75 -        by (simp add: mult_ac)
    1.76 -      finally show ?thesis .
    1.77 -    qed
    1.78 -  } note le_factor = this
    1.79 -  
    1.80 -  let ?D = "b * d" and ?D' = "b' * d'"
    1.81 -  from neq have D: "?D \<noteq> 0" by simp
    1.82 -  from neq have "?D' \<noteq> 0" by simp
    1.83 -  hence "?le a b c d = ?le (a * ?D') (b * ?D') c d"
    1.84 -    by (rule le_factor)
    1.85 -  also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')" 
    1.86 -    by (simp add: mult_ac)
    1.87 -  also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')"
    1.88 -    by (simp only: eq1 eq2)
    1.89 -  also have "... = ?le (a' * ?D) (b' * ?D) c' d'"
    1.90 -    by (simp add: mult_ac)
    1.91 -  also from D have "... = ?le a' b' c' d'"
    1.92 -    by (rule le_factor [symmetric])
    1.93 -  finally show "?le a b c d = ?le a' b' c' d'" .
    1.94 +instance proof
    1.95 +  fix a b c :: rat
    1.96 +  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
    1.97 +    by (rule abs_rat_def)
    1.98 +  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
    1.99 +    unfolding less_eq_rat_def less_rat_def
   1.100 +    by (auto, drule (1) positive_add, simp_all add: positive_zero)
   1.101 +  show "a \<le> a"
   1.102 +    unfolding less_eq_rat_def by simp
   1.103 +  show "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
   1.104 +    unfolding less_eq_rat_def less_rat_def
   1.105 +    by (auto, drule (1) positive_add, simp add: algebra_simps)
   1.106 +  show "a \<le> b \<Longrightarrow> b \<le> a \<Longrightarrow> a = b"
   1.107 +    unfolding less_eq_rat_def less_rat_def
   1.108 +    by (auto, drule (1) positive_add, simp add: positive_zero)
   1.109 +  show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   1.110 +    unfolding less_eq_rat_def less_rat_def by (auto simp: diff_minus)
   1.111 +  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   1.112 +    by (rule sgn_rat_def)
   1.113 +  show "a \<le> b \<or> b \<le> a"
   1.114 +    unfolding less_eq_rat_def less_rat_def
   1.115 +    by (auto dest!: positive_minus)
   1.116 +  show "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   1.117 +    unfolding less_rat_def
   1.118 +    by (drule (1) positive_mult, simp add: algebra_simps)
   1.119  qed
   1.120  
   1.121 -lemma le_rat [simp]:
   1.122 -  assumes "b \<noteq> 0" and "d \<noteq> 0"
   1.123 -  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.124 -  using assms by transfer simp
   1.125 +end
   1.126 +
   1.127 +instantiation rat :: distrib_lattice
   1.128 +begin
   1.129 +
   1.130 +definition
   1.131 +  "(inf :: rat \<Rightarrow> rat \<Rightarrow> rat) = min"
   1.132  
   1.133  definition
   1.134 -  less_rat_def: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
   1.135 +  "(sup :: rat \<Rightarrow> rat \<Rightarrow> rat) = max"
   1.136 +
   1.137 +instance proof
   1.138 +qed (auto simp add: inf_rat_def sup_rat_def min_max.sup_inf_distrib1)
   1.139 +
   1.140 +end
   1.141 +
   1.142 +lemma positive_rat: "positive (Fract a b) \<longleftrightarrow> 0 < a * b"
   1.143 +  by transfer simp
   1.144  
   1.145  lemma less_rat [simp]:
   1.146    assumes "b \<noteq> 0" and "d \<noteq> 0"
   1.147    shows "Fract a b < Fract c d \<longleftrightarrow> (a * d) * (b * d) < (c * b) * (b * d)"
   1.148 -  using assms by (simp add: less_rat_def eq_rat order_less_le)
   1.149 +  using assms unfolding less_rat_def
   1.150 +  by (simp add: positive_rat algebra_simps)
   1.151  
   1.152 -instance proof
   1.153 -  fix q r s :: rat
   1.154 -  {
   1.155 -    assume "q \<le> r" and "r \<le> s"
   1.156 -    then show "q \<le> s" 
   1.157 -    proof (induct q, induct r, induct s)
   1.158 -      fix a b c d e f :: int
   1.159 -      assume neq: "b > 0"  "d > 0"  "f > 0"
   1.160 -      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f"
   1.161 -      show "Fract a b \<le> Fract e f"
   1.162 -      proof -
   1.163 -        from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f"
   1.164 -          by (auto simp add: zero_less_mult_iff linorder_neq_iff)
   1.165 -        have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)"
   1.166 -        proof -
   1.167 -          from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.168 -            by simp
   1.169 -          with ff show ?thesis by (simp add: mult_le_cancel_right)
   1.170 -        qed
   1.171 -        also have "... = (c * f) * (d * f) * (b * b)" by algebra
   1.172 -        also have "... \<le> (e * d) * (d * f) * (b * b)"
   1.173 -        proof -
   1.174 -          from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)"
   1.175 -            by simp
   1.176 -          with bb show ?thesis by (simp add: mult_le_cancel_right)
   1.177 -        qed
   1.178 -        finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)"
   1.179 -          by (simp only: mult_ac)
   1.180 -        with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)"
   1.181 -          by (simp add: mult_le_cancel_right)
   1.182 -        with neq show ?thesis by simp
   1.183 -      qed
   1.184 -    qed
   1.185 -  next
   1.186 -    assume "q \<le> r" and "r \<le> q"
   1.187 -    then show "q = r"
   1.188 -    proof (induct q, induct r)
   1.189 -      fix a b c d :: int
   1.190 -      assume neq: "b > 0"  "d > 0"
   1.191 -      assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b"
   1.192 -      show "Fract a b = Fract c d"
   1.193 -      proof -
   1.194 -        from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.195 -          by simp
   1.196 -        also have "... \<le> (a * d) * (b * d)"
   1.197 -        proof -
   1.198 -          from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)"
   1.199 -            by simp
   1.200 -          thus ?thesis by (simp only: mult_ac)
   1.201 -        qed
   1.202 -        finally have "(a * d) * (b * d) = (c * b) * (b * d)" .
   1.203 -        moreover from neq have "b * d \<noteq> 0" by simp
   1.204 -        ultimately have "a * d = c * b" by simp
   1.205 -        with neq show ?thesis by (simp add: eq_rat)
   1.206 -      qed
   1.207 -    qed
   1.208 -  next
   1.209 -    show "q \<le> q"
   1.210 -      by (induct q) simp
   1.211 -    show "(q < r) = (q \<le> r \<and> \<not> r \<le> q)"
   1.212 -      by (induct q, induct r) (auto simp add: le_less mult_commute)
   1.213 -    show "q \<le> r \<or> r \<le> q"
   1.214 -      by (induct q, induct r)
   1.215 -         (simp add: mult_commute, rule linorder_linear)
   1.216 -  }
   1.217 -qed
   1.218 -
   1.219 -end
   1.220 -
   1.221 -instantiation rat :: "{distrib_lattice, abs_if, sgn_if}"
   1.222 -begin
   1.223 -
   1.224 -definition
   1.225 -  abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))"
   1.226 +lemma le_rat [simp]:
   1.227 +  assumes "b \<noteq> 0" and "d \<noteq> 0"
   1.228 +  shows "Fract a b \<le> Fract c d \<longleftrightarrow> (a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.229 +  using assms unfolding le_less by (simp add: eq_rat)
   1.230  
   1.231  lemma abs_rat [simp, code]: "\<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>"
   1.232    by (auto simp add: abs_rat_def zabs_def Zero_rat_def not_less le_less eq_rat zero_less_mult_iff)
   1.233  
   1.234 -definition
   1.235 -  sgn_rat_def: "sgn (q::rat) = (if q = 0 then 0 else if 0 < q then 1 else - 1)"
   1.236 -
   1.237  lemma sgn_rat [simp, code]: "sgn (Fract a b) = of_int (sgn a * sgn b)"
   1.238    unfolding Fract_of_int_eq
   1.239    by (auto simp: zsgn_def sgn_rat_def Zero_rat_def eq_rat)
   1.240      (auto simp: rat_number_collapse not_less le_less zero_less_mult_iff)
   1.241  
   1.242 -definition
   1.243 -  "(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min"
   1.244 -
   1.245 -definition
   1.246 -  "(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max"
   1.247 -
   1.248 -instance by intro_classes
   1.249 -  (auto simp add: abs_rat_def sgn_rat_def min_max.sup_inf_distrib1 inf_rat_def sup_rat_def)
   1.250 -
   1.251 -end
   1.252 -
   1.253 -instance rat :: linordered_field_inverse_zero
   1.254 -proof
   1.255 -  fix q r s :: rat
   1.256 -  show "q \<le> r ==> s + q \<le> s + r"
   1.257 -  proof (induct q, induct r, induct s)
   1.258 -    fix a b c d e f :: int
   1.259 -    assume neq: "b > 0"  "d > 0"  "f > 0"
   1.260 -    assume le: "Fract a b \<le> Fract c d"
   1.261 -    show "Fract e f + Fract a b \<le> Fract e f + Fract c d"
   1.262 -    proof -
   1.263 -      let ?F = "f * f" from neq have F: "0 < ?F"
   1.264 -        by (auto simp add: zero_less_mult_iff)
   1.265 -      from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)"
   1.266 -        by simp
   1.267 -      with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F"
   1.268 -        by (simp add: mult_le_cancel_right)
   1.269 -      with neq show ?thesis by (simp add: mult_ac int_distrib)
   1.270 -    qed
   1.271 -  qed
   1.272 -  show "q < r ==> 0 < s ==> s * q < s * r"
   1.273 -  proof (induct q, induct r, induct s)
   1.274 -    fix a b c d e f :: int
   1.275 -    assume neq: "b > 0"  "d > 0"  "f > 0"
   1.276 -    assume le: "Fract a b < Fract c d"
   1.277 -    assume gt: "0 < Fract e f"
   1.278 -    show "Fract e f * Fract a b < Fract e f * Fract c d"
   1.279 -    proof -
   1.280 -      let ?E = "e * f" and ?F = "f * f"
   1.281 -      from neq gt have "0 < ?E"
   1.282 -        by (auto simp add: Zero_rat_def order_less_le eq_rat)
   1.283 -      moreover from neq have "0 < ?F"
   1.284 -        by (auto simp add: zero_less_mult_iff)
   1.285 -      moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)"
   1.286 -        by simp
   1.287 -      ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F"
   1.288 -        by (simp add: mult_less_cancel_right)
   1.289 -      with neq show ?thesis
   1.290 -        by (simp add: mult_ac)
   1.291 -    qed
   1.292 -  qed
   1.293 -qed auto
   1.294 -
   1.295  lemma Rat_induct_pos [case_names Fract, induct type: rat]:
   1.296    assumes step: "\<And>a b. 0 < b \<Longrightarrow> P (Fract a b)"
   1.297    shows "P q"
   1.298 @@ -1194,13 +1115,13 @@
   1.299  by simp
   1.300  
   1.301  
   1.302 -hide_const (open) normalize
   1.303 +hide_const (open) normalize positive
   1.304  
   1.305  lemmas [transfer_rule del] =
   1.306    rat.All_transfer rat.Ex_transfer rat.rel_eq_transfer forall_rat_transfer
   1.307    Fract.transfer zero_rat.transfer one_rat.transfer plus_rat.transfer
   1.308    uminus_rat.transfer times_rat.transfer inverse_rat.transfer
   1.309 -  less_eq_rat.transfer of_rat.transfer
   1.310 +  positive.transfer of_rat.transfer
   1.311  
   1.312  text {* De-register @{text "rat"} as a quotient type: *}
   1.313