src/HOL/Fields.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (19 months ago)
changeset 67022 49309fe530fd
parent 65057 799bbbb3a395
child 67091 1393c2340eec
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
haftmann@35050
     1
(*  Title:      HOL/Fields.thy
wenzelm@32960
     2
    Author:     Gertrud Bauer
wenzelm@32960
     3
    Author:     Steven Obua
wenzelm@32960
     4
    Author:     Tobias Nipkow
wenzelm@32960
     5
    Author:     Lawrence C Paulson
wenzelm@32960
     6
    Author:     Markus Wenzel
wenzelm@32960
     7
    Author:     Jeremy Avigad
paulson@14265
     8
*)
paulson@14265
     9
wenzelm@60758
    10
section \<open>Fields\<close>
haftmann@25152
    11
haftmann@35050
    12
theory Fields
haftmann@62481
    13
imports Nat
haftmann@25186
    14
begin
paulson@14421
    15
wenzelm@60758
    16
subsection \<open>Division rings\<close>
huffman@44064
    17
wenzelm@60758
    18
text \<open>
huffman@44064
    19
  A division ring is like a field, but without the commutativity requirement.
wenzelm@60758
    20
\<close>
huffman@44064
    21
haftmann@60352
    22
class inverse = divide +
huffman@44064
    23
  fixes inverse :: "'a \<Rightarrow> 'a"
hoelzl@58776
    24
begin
haftmann@60352
    25
  
haftmann@60352
    26
abbreviation inverse_divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "'/" 70)
haftmann@60352
    27
where
haftmann@60352
    28
  "inverse_divide \<equiv> divide"
hoelzl@58776
    29
hoelzl@58776
    30
end
hoelzl@58776
    31
haftmann@62481
    32
text \<open>Setup for linear arithmetic prover\<close>
haftmann@62481
    33
haftmann@62481
    34
ML_file "~~/src/Provers/Arith/fast_lin_arith.ML"
haftmann@62481
    35
ML_file "Tools/lin_arith.ML"
haftmann@62481
    36
setup \<open>Lin_Arith.global_setup\<close>
haftmann@62481
    37
declaration \<open>K Lin_Arith.setup\<close>
haftmann@62481
    38
haftmann@62481
    39
simproc_setup fast_arith_nat ("(m::nat) < n" | "(m::nat) \<le> n" | "(m::nat) = n") =
haftmann@62481
    40
  \<open>K Lin_Arith.simproc\<close>
haftmann@62481
    41
(* Because of this simproc, the arithmetic solver is really only
haftmann@62481
    42
useful to detect inconsistencies among the premises for subgoals which are
haftmann@62481
    43
*not* themselves (in)equalities, because the latter activate
haftmann@62481
    44
fast_nat_arith_simproc anyway. However, it seems cheaper to activate the
haftmann@62481
    45
solver all the time rather than add the additional check. *)
haftmann@62481
    46
haftmann@62481
    47
lemmas [arith_split] = nat_diff_split split_min split_max
haftmann@62481
    48
haftmann@62481
    49
wenzelm@61799
    50
text\<open>Lemmas \<open>divide_simps\<close> move division to the outside and eliminates them on (in)equalities.\<close>
hoelzl@56481
    51
wenzelm@57950
    52
named_theorems divide_simps "rewrite rules to eliminate divisions"
hoelzl@56481
    53
huffman@44064
    54
class division_ring = ring_1 + inverse +
huffman@44064
    55
  assumes left_inverse [simp]:  "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
huffman@44064
    56
  assumes right_inverse [simp]: "a \<noteq> 0 \<Longrightarrow> a * inverse a = 1"
huffman@44064
    57
  assumes divide_inverse: "a / b = a * inverse b"
haftmann@59867
    58
  assumes inverse_zero [simp]: "inverse 0 = 0"
huffman@44064
    59
begin
huffman@44064
    60
huffman@44064
    61
subclass ring_1_no_zero_divisors
huffman@44064
    62
proof
huffman@44064
    63
  fix a b :: 'a
huffman@44064
    64
  assume a: "a \<noteq> 0" and b: "b \<noteq> 0"
huffman@44064
    65
  show "a * b \<noteq> 0"
huffman@44064
    66
  proof
huffman@44064
    67
    assume ab: "a * b = 0"
huffman@44064
    68
    hence "0 = inverse a * (a * b) * inverse b" by simp
huffman@44064
    69
    also have "\<dots> = (inverse a * a) * (b * inverse b)"
haftmann@57512
    70
      by (simp only: mult.assoc)
huffman@44064
    71
    also have "\<dots> = 1" using a b by simp
huffman@44064
    72
    finally show False by simp
huffman@44064
    73
  qed
huffman@44064
    74
qed
huffman@44064
    75
huffman@44064
    76
lemma nonzero_imp_inverse_nonzero:
huffman@44064
    77
  "a \<noteq> 0 \<Longrightarrow> inverse a \<noteq> 0"
huffman@44064
    78
proof
huffman@44064
    79
  assume ianz: "inverse a = 0"
huffman@44064
    80
  assume "a \<noteq> 0"
huffman@44064
    81
  hence "1 = a * inverse a" by simp
huffman@44064
    82
  also have "... = 0" by (simp add: ianz)
huffman@44064
    83
  finally have "1 = 0" .
huffman@44064
    84
  thus False by (simp add: eq_commute)
huffman@44064
    85
qed
huffman@44064
    86
huffman@44064
    87
lemma inverse_zero_imp_zero:
huffman@44064
    88
  "inverse a = 0 \<Longrightarrow> a = 0"
huffman@44064
    89
apply (rule classical)
huffman@44064
    90
apply (drule nonzero_imp_inverse_nonzero)
huffman@44064
    91
apply auto
huffman@44064
    92
done
huffman@44064
    93
lp15@59667
    94
lemma inverse_unique:
huffman@44064
    95
  assumes ab: "a * b = 1"
huffman@44064
    96
  shows "inverse a = b"
huffman@44064
    97
proof -
huffman@44064
    98
  have "a \<noteq> 0" using ab by (cases "a = 0") simp_all
huffman@44064
    99
  moreover have "inverse a * (a * b) = inverse a" by (simp add: ab)
haftmann@57512
   100
  ultimately show ?thesis by (simp add: mult.assoc [symmetric])
huffman@44064
   101
qed
huffman@44064
   102
huffman@44064
   103
lemma nonzero_inverse_minus_eq:
huffman@44064
   104
  "a \<noteq> 0 \<Longrightarrow> inverse (- a) = - inverse a"
huffman@44064
   105
by (rule inverse_unique) simp
huffman@44064
   106
huffman@44064
   107
lemma nonzero_inverse_inverse_eq:
huffman@44064
   108
  "a \<noteq> 0 \<Longrightarrow> inverse (inverse a) = a"
huffman@44064
   109
by (rule inverse_unique) simp
huffman@44064
   110
huffman@44064
   111
lemma nonzero_inverse_eq_imp_eq:
huffman@44064
   112
  assumes "inverse a = inverse b" and "a \<noteq> 0" and "b \<noteq> 0"
huffman@44064
   113
  shows "a = b"
huffman@44064
   114
proof -
wenzelm@60758
   115
  from \<open>inverse a = inverse b\<close>
huffman@44064
   116
  have "inverse (inverse a) = inverse (inverse b)" by (rule arg_cong)
wenzelm@60758
   117
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> show "a = b"
huffman@44064
   118
    by (simp add: nonzero_inverse_inverse_eq)
huffman@44064
   119
qed
huffman@44064
   120
huffman@44064
   121
lemma inverse_1 [simp]: "inverse 1 = 1"
huffman@44064
   122
by (rule inverse_unique) simp
huffman@44064
   123
lp15@59667
   124
lemma nonzero_inverse_mult_distrib:
huffman@44064
   125
  assumes "a \<noteq> 0" and "b \<noteq> 0"
huffman@44064
   126
  shows "inverse (a * b) = inverse b * inverse a"
huffman@44064
   127
proof -
huffman@44064
   128
  have "a * (b * inverse b) * inverse a = 1" using assms by simp
haftmann@57512
   129
  hence "a * b * (inverse b * inverse a) = 1" by (simp only: mult.assoc)
huffman@44064
   130
  thus ?thesis by (rule inverse_unique)
huffman@44064
   131
qed
huffman@44064
   132
huffman@44064
   133
lemma division_ring_inverse_add:
huffman@44064
   134
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a + inverse b = inverse a * (a + b) * inverse b"
huffman@44064
   135
by (simp add: algebra_simps)
huffman@44064
   136
huffman@44064
   137
lemma division_ring_inverse_diff:
huffman@44064
   138
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a - inverse b = inverse a * (b - a) * inverse b"
huffman@44064
   139
by (simp add: algebra_simps)
huffman@44064
   140
huffman@44064
   141
lemma right_inverse_eq: "b \<noteq> 0 \<Longrightarrow> a / b = 1 \<longleftrightarrow> a = b"
huffman@44064
   142
proof
huffman@44064
   143
  assume neq: "b \<noteq> 0"
huffman@44064
   144
  {
haftmann@57512
   145
    hence "a = (a / b) * b" by (simp add: divide_inverse mult.assoc)
huffman@44064
   146
    also assume "a / b = 1"
huffman@44064
   147
    finally show "a = b" by simp
huffman@44064
   148
  next
huffman@44064
   149
    assume "a = b"
huffman@44064
   150
    with neq show "a / b = 1" by (simp add: divide_inverse)
huffman@44064
   151
  }
huffman@44064
   152
qed
huffman@44064
   153
huffman@44064
   154
lemma nonzero_inverse_eq_divide: "a \<noteq> 0 \<Longrightarrow> inverse a = 1 / a"
huffman@44064
   155
by (simp add: divide_inverse)
huffman@44064
   156
huffman@44064
   157
lemma divide_self [simp]: "a \<noteq> 0 \<Longrightarrow> a / a = 1"
huffman@44064
   158
by (simp add: divide_inverse)
huffman@44064
   159
hoelzl@56481
   160
lemma inverse_eq_divide [field_simps, divide_simps]: "inverse a = 1 / a"
huffman@44064
   161
by (simp add: divide_inverse)
huffman@44064
   162
huffman@44064
   163
lemma add_divide_distrib: "(a+b) / c = a/c + b/c"
huffman@44064
   164
by (simp add: divide_inverse algebra_simps)
huffman@44064
   165
huffman@44064
   166
lemma times_divide_eq_right [simp]: "a * (b / c) = (a * b) / c"
haftmann@57512
   167
  by (simp add: divide_inverse mult.assoc)
huffman@44064
   168
huffman@44064
   169
lemma minus_divide_left: "- (a / b) = (-a) / b"
huffman@44064
   170
  by (simp add: divide_inverse)
huffman@44064
   171
huffman@44064
   172
lemma nonzero_minus_divide_right: "b \<noteq> 0 ==> - (a / b) = a / (- b)"
huffman@44064
   173
  by (simp add: divide_inverse nonzero_inverse_minus_eq)
huffman@44064
   174
huffman@44064
   175
lemma nonzero_minus_divide_divide: "b \<noteq> 0 ==> (-a) / (-b) = a / b"
huffman@44064
   176
  by (simp add: divide_inverse nonzero_inverse_minus_eq)
huffman@44064
   177
hoelzl@56479
   178
lemma divide_minus_left [simp]: "(-a) / b = - (a / b)"
huffman@44064
   179
  by (simp add: divide_inverse)
huffman@44064
   180
huffman@44064
   181
lemma diff_divide_distrib: "(a - b) / c = a / c - b / c"
hoelzl@56479
   182
  using add_divide_distrib [of a "- b" c] by simp
huffman@44064
   183
huffman@44064
   184
lemma nonzero_eq_divide_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> a = b / c \<longleftrightarrow> a * c = b"
huffman@44064
   185
proof -
huffman@44064
   186
  assume [simp]: "c \<noteq> 0"
huffman@44064
   187
  have "a = b / c \<longleftrightarrow> a * c = (b / c) * c" by simp
haftmann@57512
   188
  also have "... \<longleftrightarrow> a * c = b" by (simp add: divide_inverse mult.assoc)
huffman@44064
   189
  finally show ?thesis .
huffman@44064
   190
qed
huffman@44064
   191
huffman@44064
   192
lemma nonzero_divide_eq_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> b / c = a \<longleftrightarrow> b = a * c"
huffman@44064
   193
proof -
huffman@44064
   194
  assume [simp]: "c \<noteq> 0"
huffman@44064
   195
  have "b / c = a \<longleftrightarrow> (b / c) * c = a * c" by simp
lp15@59667
   196
  also have "... \<longleftrightarrow> b = a * c" by (simp add: divide_inverse mult.assoc)
huffman@44064
   197
  finally show ?thesis .
huffman@44064
   198
qed
huffman@44064
   199
hoelzl@56480
   200
lemma nonzero_neg_divide_eq_eq [field_simps]: "b \<noteq> 0 \<Longrightarrow> - (a / b) = c \<longleftrightarrow> - a = c * b"
haftmann@59535
   201
  using nonzero_divide_eq_eq[of b "-a" c] by simp
nipkow@56441
   202
hoelzl@56480
   203
lemma nonzero_neg_divide_eq_eq2 [field_simps]: "b \<noteq> 0 \<Longrightarrow> c = - (a / b) \<longleftrightarrow> c * b = - a"
hoelzl@56480
   204
  using nonzero_neg_divide_eq_eq[of b a c] by auto
nipkow@56441
   205
huffman@44064
   206
lemma divide_eq_imp: "c \<noteq> 0 \<Longrightarrow> b = a * c \<Longrightarrow> b / c = a"
haftmann@57512
   207
  by (simp add: divide_inverse mult.assoc)
huffman@44064
   208
huffman@44064
   209
lemma eq_divide_imp: "c \<noteq> 0 \<Longrightarrow> a * c = b \<Longrightarrow> a = b / c"
haftmann@57512
   210
  by (drule sym) (simp add: divide_inverse mult.assoc)
huffman@44064
   211
nipkow@56445
   212
lemma add_divide_eq_iff [field_simps]:
nipkow@56445
   213
  "z \<noteq> 0 \<Longrightarrow> x + y / z = (x * z + y) / z"
nipkow@56445
   214
  by (simp add: add_divide_distrib nonzero_eq_divide_eq)
nipkow@56445
   215
nipkow@56445
   216
lemma divide_add_eq_iff [field_simps]:
nipkow@56445
   217
  "z \<noteq> 0 \<Longrightarrow> x / z + y = (x + y * z) / z"
nipkow@56445
   218
  by (simp add: add_divide_distrib nonzero_eq_divide_eq)
nipkow@56445
   219
nipkow@56445
   220
lemma diff_divide_eq_iff [field_simps]:
nipkow@56445
   221
  "z \<noteq> 0 \<Longrightarrow> x - y / z = (x * z - y) / z"
nipkow@56445
   222
  by (simp add: diff_divide_distrib nonzero_eq_divide_eq eq_diff_eq)
nipkow@56445
   223
hoelzl@56480
   224
lemma minus_divide_add_eq_iff [field_simps]:
hoelzl@56480
   225
  "z \<noteq> 0 \<Longrightarrow> - (x / z) + y = (- x + y * z) / z"
haftmann@59535
   226
  by (simp add: add_divide_distrib diff_divide_eq_iff)
hoelzl@56480
   227
nipkow@56445
   228
lemma divide_diff_eq_iff [field_simps]:
nipkow@56445
   229
  "z \<noteq> 0 \<Longrightarrow> x / z - y = (x - y * z) / z"
nipkow@56445
   230
  by (simp add: field_simps)
nipkow@56445
   231
hoelzl@56480
   232
lemma minus_divide_diff_eq_iff [field_simps]:
hoelzl@56480
   233
  "z \<noteq> 0 \<Longrightarrow> - (x / z) - y = (- x - y * z) / z"
haftmann@59535
   234
  by (simp add: divide_diff_eq_iff[symmetric])
hoelzl@56480
   235
haftmann@60353
   236
lemma division_ring_divide_zero [simp]:
huffman@44064
   237
  "a / 0 = 0"
huffman@44064
   238
  by (simp add: divide_inverse)
huffman@44064
   239
huffman@44064
   240
lemma divide_self_if [simp]:
huffman@44064
   241
  "a / a = (if a = 0 then 0 else 1)"
huffman@44064
   242
  by simp
huffman@44064
   243
huffman@44064
   244
lemma inverse_nonzero_iff_nonzero [simp]:
huffman@44064
   245
  "inverse a = 0 \<longleftrightarrow> a = 0"
huffman@44064
   246
  by rule (fact inverse_zero_imp_zero, simp)
huffman@44064
   247
huffman@44064
   248
lemma inverse_minus_eq [simp]:
huffman@44064
   249
  "inverse (- a) = - inverse a"
huffman@44064
   250
proof cases
huffman@44064
   251
  assume "a=0" thus ?thesis by simp
huffman@44064
   252
next
lp15@59667
   253
  assume "a\<noteq>0"
huffman@44064
   254
  thus ?thesis by (simp add: nonzero_inverse_minus_eq)
huffman@44064
   255
qed
huffman@44064
   256
huffman@44064
   257
lemma inverse_inverse_eq [simp]:
huffman@44064
   258
  "inverse (inverse a) = a"
huffman@44064
   259
proof cases
huffman@44064
   260
  assume "a=0" thus ?thesis by simp
huffman@44064
   261
next
lp15@59667
   262
  assume "a\<noteq>0"
huffman@44064
   263
  thus ?thesis by (simp add: nonzero_inverse_inverse_eq)
huffman@44064
   264
qed
huffman@44064
   265
huffman@44680
   266
lemma inverse_eq_imp_eq:
huffman@44680
   267
  "inverse a = inverse b \<Longrightarrow> a = b"
huffman@44680
   268
  by (drule arg_cong [where f="inverse"], simp)
huffman@44680
   269
huffman@44680
   270
lemma inverse_eq_iff_eq [simp]:
huffman@44680
   271
  "inverse a = inverse b \<longleftrightarrow> a = b"
huffman@44680
   272
  by (force dest!: inverse_eq_imp_eq)
huffman@44680
   273
hoelzl@56481
   274
lemma add_divide_eq_if_simps [divide_simps]:
hoelzl@56481
   275
    "a + b / z = (if z = 0 then a else (a * z + b) / z)"
hoelzl@56481
   276
    "a / z + b = (if z = 0 then b else (a + b * z) / z)"
hoelzl@56481
   277
    "- (a / z) + b = (if z = 0 then b else (-a + b * z) / z)"
hoelzl@56481
   278
    "a - b / z = (if z = 0 then a else (a * z - b) / z)"
hoelzl@56481
   279
    "a / z - b = (if z = 0 then -b else (a - b * z) / z)"
hoelzl@56481
   280
    "- (a / z) - b = (if z = 0 then -b else (- a - b * z) / z)"
hoelzl@56481
   281
  by (simp_all add: add_divide_eq_iff divide_add_eq_iff diff_divide_eq_iff divide_diff_eq_iff
hoelzl@56481
   282
      minus_divide_diff_eq_iff)
hoelzl@56481
   283
hoelzl@56481
   284
lemma [divide_simps]:
hoelzl@56481
   285
  shows divide_eq_eq: "b / c = a \<longleftrightarrow> (if c \<noteq> 0 then b = a * c else a = 0)"
hoelzl@56481
   286
    and eq_divide_eq: "a = b / c \<longleftrightarrow> (if c \<noteq> 0 then a * c = b else a = 0)"
hoelzl@56481
   287
    and minus_divide_eq_eq: "- (b / c) = a \<longleftrightarrow> (if c \<noteq> 0 then - b = a * c else a = 0)"
hoelzl@56481
   288
    and eq_minus_divide_eq: "a = - (b / c) \<longleftrightarrow> (if c \<noteq> 0 then a * c = - b else a = 0)"
hoelzl@56481
   289
  by (auto simp add:  field_simps)
hoelzl@56481
   290
huffman@44064
   291
end
huffman@44064
   292
wenzelm@60758
   293
subsection \<open>Fields\<close>
huffman@44064
   294
huffman@22987
   295
class field = comm_ring_1 + inverse +
haftmann@35084
   296
  assumes field_inverse: "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
haftmann@35084
   297
  assumes field_divide_inverse: "a / b = a * inverse b"
haftmann@59867
   298
  assumes field_inverse_zero: "inverse 0 = 0"
haftmann@25267
   299
begin
huffman@20496
   300
haftmann@25267
   301
subclass division_ring
haftmann@28823
   302
proof
huffman@22987
   303
  fix a :: 'a
huffman@22987
   304
  assume "a \<noteq> 0"
huffman@22987
   305
  thus "inverse a * a = 1" by (rule field_inverse)
haftmann@57512
   306
  thus "a * inverse a = 1" by (simp only: mult.commute)
haftmann@35084
   307
next
haftmann@35084
   308
  fix a b :: 'a
haftmann@35084
   309
  show "a / b = a * inverse b" by (rule field_divide_inverse)
haftmann@59867
   310
next
haftmann@59867
   311
  show "inverse 0 = 0"
haftmann@59867
   312
    by (fact field_inverse_zero) 
obua@14738
   313
qed
haftmann@25230
   314
haftmann@60353
   315
subclass idom_divide
haftmann@60353
   316
proof
haftmann@60353
   317
  fix b a
haftmann@60353
   318
  assume "b \<noteq> 0"
haftmann@60353
   319
  then show "a * b / b = a"
haftmann@60353
   320
    by (simp add: divide_inverse ac_simps)
haftmann@60353
   321
next
haftmann@60353
   322
  fix a
haftmann@60353
   323
  show "a / 0 = 0"
haftmann@60353
   324
    by (simp add: divide_inverse)
haftmann@60353
   325
qed
haftmann@25230
   326
wenzelm@60758
   327
text\<open>There is no slick version using division by zero.\<close>
huffman@30630
   328
lemma inverse_add:
haftmann@60353
   329
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a + inverse b = (a + b) * inverse a * inverse b"
haftmann@60353
   330
  by (simp add: division_ring_inverse_add ac_simps)
huffman@30630
   331
blanchet@54147
   332
lemma nonzero_mult_divide_mult_cancel_left [simp]:
haftmann@60353
   333
  assumes [simp]: "c \<noteq> 0"
haftmann@60353
   334
  shows "(c * a) / (c * b) = a / b"
haftmann@60353
   335
proof (cases "b = 0")
haftmann@60353
   336
  case True then show ?thesis by simp
haftmann@60353
   337
next
haftmann@60353
   338
  case False
haftmann@60353
   339
  then have "(c*a)/(c*b) = c * a * (inverse b * inverse c)"
huffman@30630
   340
    by (simp add: divide_inverse nonzero_inverse_mult_distrib)
huffman@30630
   341
  also have "... =  a * inverse b * (inverse c * c)"
haftmann@57514
   342
    by (simp only: ac_simps)
huffman@30630
   343
  also have "... =  a * inverse b" by simp
huffman@30630
   344
    finally show ?thesis by (simp add: divide_inverse)
huffman@30630
   345
qed
huffman@30630
   346
blanchet@54147
   347
lemma nonzero_mult_divide_mult_cancel_right [simp]:
haftmann@60353
   348
  "c \<noteq> 0 \<Longrightarrow> (a * c) / (b * c) = a / b"
haftmann@60353
   349
  using nonzero_mult_divide_mult_cancel_left [of c a b] by (simp add: ac_simps)
huffman@30630
   350
haftmann@36304
   351
lemma times_divide_eq_left [simp]: "(b / c) * a = (b * a) / c"
haftmann@57514
   352
  by (simp add: divide_inverse ac_simps)
huffman@30630
   353
lp15@61238
   354
lemma divide_inverse_commute: "a / b = inverse b * a"
lp15@61238
   355
  by (simp add: divide_inverse mult.commute)
lp15@61238
   356
huffman@30630
   357
lemma add_frac_eq:
huffman@30630
   358
  assumes "y \<noteq> 0" and "z \<noteq> 0"
huffman@30630
   359
  shows "x / y + w / z = (x * z + w * y) / (y * z)"
huffman@30630
   360
proof -
huffman@30630
   361
  have "x / y + w / z = (x * z) / (y * z) + (y * w) / (y * z)"
huffman@30630
   362
    using assms by simp
huffman@30630
   363
  also have "\<dots> = (x * z + y * w) / (y * z)"
huffman@30630
   364
    by (simp only: add_divide_distrib)
huffman@30630
   365
  finally show ?thesis
haftmann@57512
   366
    by (simp only: mult.commute)
huffman@30630
   367
qed
huffman@30630
   368
wenzelm@60758
   369
text\<open>Special Cancellation Simprules for Division\<close>
huffman@30630
   370
blanchet@54147
   371
lemma nonzero_divide_mult_cancel_right [simp]:
haftmann@60353
   372
  "b \<noteq> 0 \<Longrightarrow> b / (a * b) = 1 / a"
haftmann@60353
   373
  using nonzero_mult_divide_mult_cancel_right [of b 1 a] by simp
huffman@30630
   374
blanchet@54147
   375
lemma nonzero_divide_mult_cancel_left [simp]:
haftmann@60353
   376
  "a \<noteq> 0 \<Longrightarrow> a / (a * b) = 1 / b"
haftmann@60353
   377
  using nonzero_mult_divide_mult_cancel_left [of a 1 b] by simp
huffman@30630
   378
blanchet@54147
   379
lemma nonzero_mult_divide_mult_cancel_left2 [simp]:
haftmann@60353
   380
  "c \<noteq> 0 \<Longrightarrow> (c * a) / (b * c) = a / b"
haftmann@60353
   381
  using nonzero_mult_divide_mult_cancel_left [of c a b] by (simp add: ac_simps)
huffman@30630
   382
blanchet@54147
   383
lemma nonzero_mult_divide_mult_cancel_right2 [simp]:
haftmann@60353
   384
  "c \<noteq> 0 \<Longrightarrow> (a * c) / (c * b) = a / b"
haftmann@60353
   385
  using nonzero_mult_divide_mult_cancel_right [of b c a] by (simp add: ac_simps)
huffman@30630
   386
huffman@30630
   387
lemma diff_frac_eq:
huffman@30630
   388
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y - w / z = (x * z - w * y) / (y * z)"
haftmann@36348
   389
  by (simp add: field_simps)
huffman@30630
   390
huffman@30630
   391
lemma frac_eq_eq:
huffman@30630
   392
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (x / y = w / z) = (x * z = w * y)"
haftmann@36348
   393
  by (simp add: field_simps)
haftmann@36348
   394
haftmann@58512
   395
lemma divide_minus1 [simp]: "x / - 1 = - x"
haftmann@58512
   396
  using nonzero_minus_divide_right [of "1" x] by simp
lp15@59667
   397
wenzelm@60758
   398
text\<open>This version builds in division by zero while also re-orienting
wenzelm@60758
   399
      the right-hand side.\<close>
paulson@14270
   400
lemma inverse_mult_distrib [simp]:
haftmann@36409
   401
  "inverse (a * b) = inverse a * inverse b"
haftmann@36409
   402
proof cases
lp15@59667
   403
  assume "a \<noteq> 0 & b \<noteq> 0"
haftmann@57514
   404
  thus ?thesis by (simp add: nonzero_inverse_mult_distrib ac_simps)
haftmann@36409
   405
next
lp15@59667
   406
  assume "~ (a \<noteq> 0 & b \<noteq> 0)"
haftmann@36409
   407
  thus ?thesis by force
haftmann@36409
   408
qed
paulson@14270
   409
paulson@14365
   410
lemma inverse_divide [simp]:
haftmann@36409
   411
  "inverse (a / b) = b / a"
haftmann@57512
   412
  by (simp add: divide_inverse mult.commute)
paulson@14365
   413
wenzelm@23389
   414
wenzelm@60758
   415
text \<open>Calculations with fractions\<close>
avigad@16775
   416
wenzelm@61799
   417
text\<open>There is a whole bunch of simp-rules just for class \<open>field\<close> but none for class \<open>field\<close> and \<open>nonzero_divides\<close>
wenzelm@60758
   418
because the latter are covered by a simproc.\<close>
nipkow@23413
   419
nipkow@23413
   420
lemma mult_divide_mult_cancel_left:
haftmann@36409
   421
  "c \<noteq> 0 \<Longrightarrow> (c * a) / (c * b) = a / b"
haftmann@21328
   422
apply (cases "b = 0")
huffman@35216
   423
apply simp_all
paulson@14277
   424
done
paulson@14277
   425
nipkow@23413
   426
lemma mult_divide_mult_cancel_right:
haftmann@36409
   427
  "c \<noteq> 0 \<Longrightarrow> (a * c) / (b * c) = a / b"
haftmann@21328
   428
apply (cases "b = 0")
huffman@35216
   429
apply simp_all
paulson@14321
   430
done
nipkow@23413
   431
blanchet@54147
   432
lemma divide_divide_eq_right [simp]:
haftmann@36409
   433
  "a / (b / c) = (a * c) / b"
haftmann@57514
   434
  by (simp add: divide_inverse ac_simps)
paulson@14288
   435
blanchet@54147
   436
lemma divide_divide_eq_left [simp]:
haftmann@36409
   437
  "(a / b) / c = a / (b * c)"
haftmann@57512
   438
  by (simp add: divide_inverse mult.assoc)
paulson@14288
   439
lp15@56365
   440
lemma divide_divide_times_eq:
lp15@56365
   441
  "(x / y) / (z / w) = (x * w) / (y * z)"
lp15@56365
   442
  by simp
wenzelm@23389
   443
wenzelm@60758
   444
text \<open>Special Cancellation Simprules for Division\<close>
paulson@15234
   445
blanchet@54147
   446
lemma mult_divide_mult_cancel_left_if [simp]:
haftmann@36409
   447
  shows "(c * a) / (c * b) = (if c = 0 then 0 else a / b)"
haftmann@60353
   448
  by simp
nipkow@23413
   449
paulson@15234
   450
wenzelm@60758
   451
text \<open>Division and Unary Minus\<close>
paulson@14293
   452
haftmann@36409
   453
lemma minus_divide_right:
haftmann@36409
   454
  "- (a / b) = a / - b"
haftmann@36409
   455
  by (simp add: divide_inverse)
paulson@14430
   456
hoelzl@56479
   457
lemma divide_minus_right [simp]:
haftmann@36409
   458
  "a / - b = - (a / b)"
haftmann@36409
   459
  by (simp add: divide_inverse)
huffman@30630
   460
hoelzl@56479
   461
lemma minus_divide_divide:
haftmann@36409
   462
  "(- a) / (- b) = a / b"
lp15@59667
   463
apply (cases "b=0", simp)
lp15@59667
   464
apply (simp add: nonzero_minus_divide_divide)
paulson@14293
   465
done
paulson@14293
   466
haftmann@36301
   467
lemma inverse_eq_1_iff [simp]:
haftmann@36409
   468
  "inverse x = 1 \<longleftrightarrow> x = 1"
lp15@59667
   469
  by (insert inverse_eq_iff_eq [of x 1], simp)
wenzelm@23389
   470
blanchet@54147
   471
lemma divide_eq_0_iff [simp]:
haftmann@36409
   472
  "a / b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
haftmann@36409
   473
  by (simp add: divide_inverse)
haftmann@36301
   474
blanchet@54147
   475
lemma divide_cancel_right [simp]:
haftmann@36409
   476
  "a / c = b / c \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@36409
   477
  apply (cases "c=0", simp)
haftmann@36409
   478
  apply (simp add: divide_inverse)
haftmann@36409
   479
  done
haftmann@36301
   480
blanchet@54147
   481
lemma divide_cancel_left [simp]:
lp15@59667
   482
  "c / a = c / b \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@36409
   483
  apply (cases "c=0", simp)
haftmann@36409
   484
  apply (simp add: divide_inverse)
haftmann@36409
   485
  done
haftmann@36301
   486
blanchet@54147
   487
lemma divide_eq_1_iff [simp]:
haftmann@36409
   488
  "a / b = 1 \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
haftmann@36409
   489
  apply (cases "b=0", simp)
haftmann@36409
   490
  apply (simp add: right_inverse_eq)
haftmann@36409
   491
  done
haftmann@36301
   492
blanchet@54147
   493
lemma one_eq_divide_iff [simp]:
haftmann@36409
   494
  "1 = a / b \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
haftmann@36409
   495
  by (simp add: eq_commute [of 1])
haftmann@36409
   496
lp15@65057
   497
lemma divide_eq_minus_1_iff:
lp15@65057
   498
   "(a / b = - 1) \<longleftrightarrow> b \<noteq> 0 \<and> a = - b"
lp15@65057
   499
using divide_eq_1_iff by fastforce
lp15@65057
   500
haftmann@36719
   501
lemma times_divide_times_eq:
haftmann@36719
   502
  "(x / y) * (z / w) = (x * z) / (y * w)"
haftmann@36719
   503
  by simp
haftmann@36719
   504
haftmann@36719
   505
lemma add_frac_num:
haftmann@36719
   506
  "y \<noteq> 0 \<Longrightarrow> x / y + z = (x + z * y) / y"
haftmann@36719
   507
  by (simp add: add_divide_distrib)
haftmann@36719
   508
haftmann@36719
   509
lemma add_num_frac:
haftmann@36719
   510
  "y \<noteq> 0 \<Longrightarrow> z + x / y = (x + z * y) / y"
haftmann@36719
   511
  by (simp add: add_divide_distrib add.commute)
haftmann@36719
   512
haftmann@64591
   513
lemma dvd_field_iff:
haftmann@64591
   514
  "a dvd b \<longleftrightarrow> (a = 0 \<longrightarrow> b = 0)"
haftmann@64591
   515
proof (cases "a = 0")
haftmann@64591
   516
  case True
haftmann@64591
   517
  then show ?thesis
haftmann@64591
   518
    by simp
haftmann@64591
   519
next
haftmann@64591
   520
  case False
haftmann@64591
   521
  then have "b = a * (b / a)"
haftmann@64591
   522
    by (simp add: field_simps)
haftmann@64591
   523
  then have "a dvd b" ..
haftmann@64591
   524
  with False show ?thesis
haftmann@64591
   525
    by simp
haftmann@64591
   526
qed
haftmann@64591
   527
haftmann@36409
   528
end
haftmann@36301
   529
haftmann@62481
   530
class field_char_0 = field + ring_char_0
haftmann@62481
   531
haftmann@36301
   532
wenzelm@60758
   533
subsection \<open>Ordered fields\<close>
haftmann@36301
   534
haftmann@64290
   535
class field_abs_sgn = field + idom_abs_sgn
haftmann@64290
   536
begin
haftmann@64290
   537
haftmann@64290
   538
lemma sgn_inverse [simp]:
haftmann@64290
   539
  "sgn (inverse a) = inverse (sgn a)"
haftmann@64290
   540
proof (cases "a = 0")
haftmann@64290
   541
  case True then show ?thesis by simp
haftmann@64290
   542
next
haftmann@64290
   543
  case False
haftmann@64290
   544
  then have "a * inverse a = 1"
haftmann@64290
   545
    by simp
haftmann@64290
   546
  then have "sgn (a * inverse a) = sgn 1"
haftmann@64290
   547
    by simp
haftmann@64290
   548
  then have "sgn a * sgn (inverse a) = 1"
haftmann@64290
   549
    by (simp add: sgn_mult)
haftmann@64290
   550
  then have "inverse (sgn a) * (sgn a * sgn (inverse a)) = inverse (sgn a) * 1"
haftmann@64290
   551
    by simp
haftmann@64290
   552
  then have "(inverse (sgn a) * sgn a) * sgn (inverse a) = inverse (sgn a)"
haftmann@64290
   553
    by (simp add: ac_simps)
haftmann@64290
   554
  with False show ?thesis
haftmann@64290
   555
    by (simp add: sgn_eq_0_iff)
haftmann@64290
   556
qed
haftmann@64290
   557
haftmann@64290
   558
lemma abs_inverse [simp]:
haftmann@64290
   559
  "\<bar>inverse a\<bar> = inverse \<bar>a\<bar>"
haftmann@64290
   560
proof -
haftmann@64290
   561
  from sgn_mult_abs [of "inverse a"] sgn_mult_abs [of a]
haftmann@64290
   562
  have "inverse (sgn a) * \<bar>inverse a\<bar> = inverse (sgn a * \<bar>a\<bar>)"
haftmann@64290
   563
    by simp
haftmann@64290
   564
  then show ?thesis by (auto simp add: sgn_eq_0_iff)
haftmann@64290
   565
qed
haftmann@64290
   566
    
haftmann@64290
   567
lemma sgn_divide [simp]:
haftmann@64290
   568
  "sgn (a / b) = sgn a / sgn b"
haftmann@64290
   569
  unfolding divide_inverse sgn_mult by simp
haftmann@64290
   570
haftmann@64290
   571
lemma abs_divide [simp]:
haftmann@64290
   572
  "\<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
haftmann@64290
   573
  unfolding divide_inverse abs_mult by simp
haftmann@64290
   574
  
haftmann@64290
   575
end
haftmann@64290
   576
haftmann@36301
   577
class linordered_field = field + linordered_idom
haftmann@36301
   578
begin
paulson@14268
   579
lp15@59667
   580
lemma positive_imp_inverse_positive:
lp15@59667
   581
  assumes a_gt_0: "0 < a"
haftmann@36301
   582
  shows "0 < inverse a"
nipkow@23482
   583
proof -
lp15@59667
   584
  have "0 < a * inverse a"
haftmann@36301
   585
    by (simp add: a_gt_0 [THEN less_imp_not_eq2])
lp15@59667
   586
  thus "0 < inverse a"
haftmann@36301
   587
    by (simp add: a_gt_0 [THEN less_not_sym] zero_less_mult_iff)
nipkow@23482
   588
qed
paulson@14268
   589
paulson@14277
   590
lemma negative_imp_inverse_negative:
haftmann@36301
   591
  "a < 0 \<Longrightarrow> inverse a < 0"
lp15@59667
   592
  by (insert positive_imp_inverse_positive [of "-a"],
haftmann@36301
   593
    simp add: nonzero_inverse_minus_eq less_imp_not_eq)
paulson@14268
   594
paulson@14268
   595
lemma inverse_le_imp_le:
haftmann@36301
   596
  assumes invle: "inverse a \<le> inverse b" and apos: "0 < a"
haftmann@36301
   597
  shows "b \<le> a"
nipkow@23482
   598
proof (rule classical)
paulson@14268
   599
  assume "~ b \<le> a"
nipkow@23482
   600
  hence "a < b"  by (simp add: linorder_not_le)
haftmann@36301
   601
  hence bpos: "0 < b"  by (blast intro: apos less_trans)
paulson@14268
   602
  hence "a * inverse a \<le> a * inverse b"
haftmann@36301
   603
    by (simp add: apos invle less_imp_le mult_left_mono)
paulson@14268
   604
  hence "(a * inverse a) * b \<le> (a * inverse b) * b"
haftmann@36301
   605
    by (simp add: bpos less_imp_le mult_right_mono)
haftmann@57512
   606
  thus "b \<le> a"  by (simp add: mult.assoc apos bpos less_imp_not_eq2)
nipkow@23482
   607
qed
paulson@14268
   608
paulson@14277
   609
lemma inverse_positive_imp_positive:
haftmann@36301
   610
  assumes inv_gt_0: "0 < inverse a" and nz: "a \<noteq> 0"
haftmann@36301
   611
  shows "0 < a"
wenzelm@23389
   612
proof -
paulson@14277
   613
  have "0 < inverse (inverse a)"
wenzelm@23389
   614
    using inv_gt_0 by (rule positive_imp_inverse_positive)
paulson@14277
   615
  thus "0 < a"
wenzelm@23389
   616
    using nz by (simp add: nonzero_inverse_inverse_eq)
wenzelm@23389
   617
qed
paulson@14277
   618
haftmann@36301
   619
lemma inverse_negative_imp_negative:
haftmann@36301
   620
  assumes inv_less_0: "inverse a < 0" and nz: "a \<noteq> 0"
haftmann@36301
   621
  shows "a < 0"
haftmann@36301
   622
proof -
haftmann@36301
   623
  have "inverse (inverse a) < 0"
haftmann@36301
   624
    using inv_less_0 by (rule negative_imp_inverse_negative)
haftmann@36301
   625
  thus "a < 0" using nz by (simp add: nonzero_inverse_inverse_eq)
haftmann@36301
   626
qed
haftmann@36301
   627
haftmann@36301
   628
lemma linordered_field_no_lb:
haftmann@36301
   629
  "\<forall>x. \<exists>y. y < x"
haftmann@36301
   630
proof
haftmann@36301
   631
  fix x::'a
haftmann@36301
   632
  have m1: "- (1::'a) < 0" by simp
lp15@59667
   633
  from add_strict_right_mono[OF m1, where c=x]
haftmann@36301
   634
  have "(- 1) + x < x" by simp
haftmann@36301
   635
  thus "\<exists>y. y < x" by blast
haftmann@36301
   636
qed
haftmann@36301
   637
haftmann@36301
   638
lemma linordered_field_no_ub:
haftmann@36301
   639
  "\<forall> x. \<exists>y. y > x"
haftmann@36301
   640
proof
haftmann@36301
   641
  fix x::'a
haftmann@36301
   642
  have m1: " (1::'a) > 0" by simp
lp15@59667
   643
  from add_strict_right_mono[OF m1, where c=x]
haftmann@36301
   644
  have "1 + x > x" by simp
haftmann@36301
   645
  thus "\<exists>y. y > x" by blast
haftmann@36301
   646
qed
haftmann@36301
   647
haftmann@36301
   648
lemma less_imp_inverse_less:
haftmann@36301
   649
  assumes less: "a < b" and apos:  "0 < a"
haftmann@36301
   650
  shows "inverse b < inverse a"
haftmann@36301
   651
proof (rule ccontr)
haftmann@36301
   652
  assume "~ inverse b < inverse a"
haftmann@36301
   653
  hence "inverse a \<le> inverse b" by simp
haftmann@36301
   654
  hence "~ (a < b)"
haftmann@36301
   655
    by (simp add: not_less inverse_le_imp_le [OF _ apos])
haftmann@36301
   656
  thus False by (rule notE [OF _ less])
haftmann@36301
   657
qed
haftmann@36301
   658
haftmann@36301
   659
lemma inverse_less_imp_less:
haftmann@36301
   660
  "inverse a < inverse b \<Longrightarrow> 0 < a \<Longrightarrow> b < a"
haftmann@36301
   661
apply (simp add: less_le [of "inverse a"] less_le [of "b"])
lp15@59667
   662
apply (force dest!: inverse_le_imp_le nonzero_inverse_eq_imp_eq)
haftmann@36301
   663
done
haftmann@36301
   664
wenzelm@60758
   665
text\<open>Both premises are essential. Consider -1 and 1.\<close>
blanchet@54147
   666
lemma inverse_less_iff_less [simp]:
haftmann@36301
   667
  "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
lp15@59667
   668
  by (blast intro: less_imp_inverse_less dest: inverse_less_imp_less)
haftmann@36301
   669
haftmann@36301
   670
lemma le_imp_inverse_le:
haftmann@36301
   671
  "a \<le> b \<Longrightarrow> 0 < a \<Longrightarrow> inverse b \<le> inverse a"
haftmann@36301
   672
  by (force simp add: le_less less_imp_inverse_less)
haftmann@36301
   673
blanchet@54147
   674
lemma inverse_le_iff_le [simp]:
haftmann@36301
   675
  "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
lp15@59667
   676
  by (blast intro: le_imp_inverse_le dest: inverse_le_imp_le)
haftmann@36301
   677
haftmann@36301
   678
wenzelm@60758
   679
text\<open>These results refer to both operands being negative.  The opposite-sign
wenzelm@60758
   680
case is trivial, since inverse preserves signs.\<close>
haftmann@36301
   681
lemma inverse_le_imp_le_neg:
haftmann@36301
   682
  "inverse a \<le> inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b \<le> a"
lp15@59667
   683
apply (rule classical)
lp15@59667
   684
apply (subgoal_tac "a < 0")
haftmann@36301
   685
 prefer 2 apply force
haftmann@36301
   686
apply (insert inverse_le_imp_le [of "-b" "-a"])
lp15@59667
   687
apply (simp add: nonzero_inverse_minus_eq)
haftmann@36301
   688
done
haftmann@36301
   689
haftmann@36301
   690
lemma less_imp_inverse_less_neg:
haftmann@36301
   691
   "a < b \<Longrightarrow> b < 0 \<Longrightarrow> inverse b < inverse a"
lp15@59667
   692
apply (subgoal_tac "a < 0")
lp15@59667
   693
 prefer 2 apply (blast intro: less_trans)
haftmann@36301
   694
apply (insert less_imp_inverse_less [of "-b" "-a"])
lp15@59667
   695
apply (simp add: nonzero_inverse_minus_eq)
haftmann@36301
   696
done
haftmann@36301
   697
haftmann@36301
   698
lemma inverse_less_imp_less_neg:
haftmann@36301
   699
   "inverse a < inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b < a"
lp15@59667
   700
apply (rule classical)
lp15@59667
   701
apply (subgoal_tac "a < 0")
haftmann@36301
   702
 prefer 2
haftmann@36301
   703
 apply force
haftmann@36301
   704
apply (insert inverse_less_imp_less [of "-b" "-a"])
lp15@59667
   705
apply (simp add: nonzero_inverse_minus_eq)
haftmann@36301
   706
done
haftmann@36301
   707
blanchet@54147
   708
lemma inverse_less_iff_less_neg [simp]:
haftmann@36301
   709
  "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
haftmann@36301
   710
apply (insert inverse_less_iff_less [of "-b" "-a"])
lp15@59667
   711
apply (simp del: inverse_less_iff_less
haftmann@36301
   712
            add: nonzero_inverse_minus_eq)
haftmann@36301
   713
done
haftmann@36301
   714
haftmann@36301
   715
lemma le_imp_inverse_le_neg:
haftmann@36301
   716
  "a \<le> b \<Longrightarrow> b < 0 ==> inverse b \<le> inverse a"
haftmann@36301
   717
  by (force simp add: le_less less_imp_inverse_less_neg)
haftmann@36301
   718
blanchet@54147
   719
lemma inverse_le_iff_le_neg [simp]:
haftmann@36301
   720
  "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
lp15@59667
   721
  by (blast intro: le_imp_inverse_le_neg dest: inverse_le_imp_le_neg)
haftmann@36301
   722
huffman@36774
   723
lemma one_less_inverse:
huffman@36774
   724
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> 1 < inverse a"
huffman@36774
   725
  using less_imp_inverse_less [of a 1, unfolded inverse_1] .
huffman@36774
   726
huffman@36774
   727
lemma one_le_inverse:
huffman@36774
   728
  "0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> 1 \<le> inverse a"
huffman@36774
   729
  using le_imp_inverse_le [of a 1, unfolded inverse_1] .
huffman@36774
   730
haftmann@59546
   731
lemma pos_le_divide_eq [field_simps]:
haftmann@59546
   732
  assumes "0 < c"
haftmann@59546
   733
  shows "a \<le> b / c \<longleftrightarrow> a * c \<le> b"
haftmann@36301
   734
proof -
haftmann@59546
   735
  from assms have "a \<le> b / c \<longleftrightarrow> a * c \<le> (b / c) * c"
haftmann@59546
   736
    using mult_le_cancel_right [of a c "b * inverse c"] by (auto simp add: field_simps)
haftmann@59546
   737
  also have "... \<longleftrightarrow> a * c \<le> b"
lp15@59667
   738
    by (simp add: less_imp_not_eq2 [OF assms] divide_inverse mult.assoc)
haftmann@36301
   739
  finally show ?thesis .
haftmann@36301
   740
qed
haftmann@36301
   741
haftmann@59546
   742
lemma pos_less_divide_eq [field_simps]:
haftmann@59546
   743
  assumes "0 < c"
haftmann@59546
   744
  shows "a < b / c \<longleftrightarrow> a * c < b"
haftmann@36301
   745
proof -
haftmann@59546
   746
  from assms have "a < b / c \<longleftrightarrow> a * c < (b / c) * c"
haftmann@59546
   747
    using mult_less_cancel_right [of a c "b / c"] by auto
haftmann@59546
   748
  also have "... = (a*c < b)"
lp15@59667
   749
    by (simp add: less_imp_not_eq2 [OF assms] divide_inverse mult.assoc)
haftmann@36301
   750
  finally show ?thesis .
haftmann@36301
   751
qed
haftmann@36301
   752
haftmann@59546
   753
lemma neg_less_divide_eq [field_simps]:
haftmann@59546
   754
  assumes "c < 0"
haftmann@59546
   755
  shows "a < b / c \<longleftrightarrow> b < a * c"
haftmann@36301
   756
proof -
haftmann@59546
   757
  from assms have "a < b / c \<longleftrightarrow> (b / c) * c < a * c"
haftmann@59546
   758
    using mult_less_cancel_right [of "b / c" c a] by auto
haftmann@59546
   759
  also have "... \<longleftrightarrow> b < a * c"
lp15@59667
   760
    by (simp add: less_imp_not_eq [OF assms] divide_inverse mult.assoc)
haftmann@36301
   761
  finally show ?thesis .
haftmann@36301
   762
qed
haftmann@36301
   763
haftmann@59546
   764
lemma neg_le_divide_eq [field_simps]:
haftmann@59546
   765
  assumes "c < 0"
haftmann@59546
   766
  shows "a \<le> b / c \<longleftrightarrow> b \<le> a * c"
haftmann@36301
   767
proof -
haftmann@59546
   768
  from assms have "a \<le> b / c \<longleftrightarrow> (b / c) * c \<le> a * c"
haftmann@59546
   769
    using mult_le_cancel_right [of "b * inverse c" c a] by (auto simp add: field_simps)
haftmann@59546
   770
  also have "... \<longleftrightarrow> b \<le> a * c"
lp15@59667
   771
    by (simp add: less_imp_not_eq [OF assms] divide_inverse mult.assoc)
haftmann@36301
   772
  finally show ?thesis .
haftmann@36301
   773
qed
haftmann@36301
   774
haftmann@59546
   775
lemma pos_divide_le_eq [field_simps]:
haftmann@59546
   776
  assumes "0 < c"
haftmann@59546
   777
  shows "b / c \<le> a \<longleftrightarrow> b \<le> a * c"
haftmann@36301
   778
proof -
haftmann@59546
   779
  from assms have "b / c \<le> a \<longleftrightarrow> (b / c) * c \<le> a * c"
haftmann@59546
   780
    using mult_le_cancel_right [of "b / c" c a] by auto
haftmann@59546
   781
  also have "... \<longleftrightarrow> b \<le> a * c"
lp15@59667
   782
    by (simp add: less_imp_not_eq2 [OF assms] divide_inverse mult.assoc)
haftmann@36301
   783
  finally show ?thesis .
haftmann@36301
   784
qed
haftmann@36301
   785
haftmann@59546
   786
lemma pos_divide_less_eq [field_simps]:
haftmann@59546
   787
  assumes "0 < c"
haftmann@59546
   788
  shows "b / c < a \<longleftrightarrow> b < a * c"
haftmann@36301
   789
proof -
haftmann@59546
   790
  from assms have "b / c < a \<longleftrightarrow> (b / c) * c < a * c"
haftmann@59546
   791
    using mult_less_cancel_right [of "b / c" c a] by auto
haftmann@59546
   792
  also have "... \<longleftrightarrow> b < a * c"
lp15@59667
   793
    by (simp add: less_imp_not_eq2 [OF assms] divide_inverse mult.assoc)
haftmann@36301
   794
  finally show ?thesis .
haftmann@36301
   795
qed
haftmann@36301
   796
haftmann@59546
   797
lemma neg_divide_le_eq [field_simps]:
haftmann@59546
   798
  assumes "c < 0"
haftmann@59546
   799
  shows "b / c \<le> a \<longleftrightarrow> a * c \<le> b"
haftmann@36301
   800
proof -
haftmann@59546
   801
  from assms have "b / c \<le> a \<longleftrightarrow> a * c \<le> (b / c) * c"
lp15@59667
   802
    using mult_le_cancel_right [of a c "b / c"] by auto
haftmann@59546
   803
  also have "... \<longleftrightarrow> a * c \<le> b"
lp15@59667
   804
    by (simp add: less_imp_not_eq [OF assms] divide_inverse mult.assoc)
haftmann@36301
   805
  finally show ?thesis .
haftmann@36301
   806
qed
haftmann@36301
   807
haftmann@59546
   808
lemma neg_divide_less_eq [field_simps]:
haftmann@59546
   809
  assumes "c < 0"
haftmann@59546
   810
  shows "b / c < a \<longleftrightarrow> a * c < b"
haftmann@36301
   811
proof -
haftmann@59546
   812
  from assms have "b / c < a \<longleftrightarrow> a * c < b / c * c"
haftmann@59546
   813
    using mult_less_cancel_right [of a c "b / c"] by auto
haftmann@59546
   814
  also have "... \<longleftrightarrow> a * c < b"
lp15@59667
   815
    by (simp add: less_imp_not_eq [OF assms] divide_inverse mult.assoc)
haftmann@36301
   816
  finally show ?thesis .
haftmann@36301
   817
qed
haftmann@36301
   818
wenzelm@61799
   819
text\<open>The following \<open>field_simps\<close> rules are necessary, as minus is always moved atop of
wenzelm@60758
   820
division but we want to get rid of division.\<close>
hoelzl@56480
   821
hoelzl@56480
   822
lemma pos_le_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> a * c \<le> - b"
hoelzl@56480
   823
  unfolding minus_divide_left by (rule pos_le_divide_eq)
hoelzl@56480
   824
hoelzl@56480
   825
lemma neg_le_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> - b \<le> a * c"
hoelzl@56480
   826
  unfolding minus_divide_left by (rule neg_le_divide_eq)
hoelzl@56480
   827
hoelzl@56480
   828
lemma pos_less_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a < - (b / c) \<longleftrightarrow> a * c < - b"
hoelzl@56480
   829
  unfolding minus_divide_left by (rule pos_less_divide_eq)
hoelzl@56480
   830
hoelzl@56480
   831
lemma neg_less_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a < - (b / c) \<longleftrightarrow> - b < a * c"
hoelzl@56480
   832
  unfolding minus_divide_left by (rule neg_less_divide_eq)
hoelzl@56480
   833
hoelzl@56480
   834
lemma pos_minus_divide_less_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) < a \<longleftrightarrow> - b < a * c"
hoelzl@56480
   835
  unfolding minus_divide_left by (rule pos_divide_less_eq)
hoelzl@56480
   836
hoelzl@56480
   837
lemma neg_minus_divide_less_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) < a \<longleftrightarrow> a * c < - b"
hoelzl@56480
   838
  unfolding minus_divide_left by (rule neg_divide_less_eq)
hoelzl@56480
   839
hoelzl@56480
   840
lemma pos_minus_divide_le_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> - b \<le> a * c"
hoelzl@56480
   841
  unfolding minus_divide_left by (rule pos_divide_le_eq)
hoelzl@56480
   842
hoelzl@56480
   843
lemma neg_minus_divide_le_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> a * c \<le> - b"
hoelzl@56480
   844
  unfolding minus_divide_left by (rule neg_divide_le_eq)
hoelzl@56480
   845
lp15@56365
   846
lemma frac_less_eq:
lp15@56365
   847
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y < w / z \<longleftrightarrow> (x * z - w * y) / (y * z) < 0"
lp15@56365
   848
  by (subst less_iff_diff_less_0) (simp add: diff_frac_eq )
lp15@56365
   849
lp15@56365
   850
lemma frac_le_eq:
lp15@56365
   851
  "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y \<le> w / z \<longleftrightarrow> (x * z - w * y) / (y * z) \<le> 0"
lp15@56365
   852
  by (subst le_iff_diff_le_0) (simp add: diff_frac_eq )
lp15@56365
   853
wenzelm@61799
   854
text\<open>Lemmas \<open>sign_simps\<close> is a first attempt to automate proofs
wenzelm@61799
   855
of positivity/negativity needed for \<open>field_simps\<close>. Have not added \<open>sign_simps\<close> to \<open>field_simps\<close> because the former can lead to case
wenzelm@60758
   856
explosions.\<close>
haftmann@36301
   857
blanchet@54147
   858
lemmas sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff
haftmann@36348
   859
blanchet@54147
   860
lemmas (in -) sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff
haftmann@36301
   861
haftmann@36301
   862
(* Only works once linear arithmetic is installed:
haftmann@36301
   863
text{*An example:*}
haftmann@36301
   864
lemma fixes a b c d e f :: "'a::linordered_field"
haftmann@36301
   865
shows "\<lbrakk>a>b; c<d; e<f; 0 < u \<rbrakk> \<Longrightarrow>
haftmann@36301
   866
 ((a-b)*(c-d)*(e-f))/((c-d)*(e-f)*(a-b)) <
haftmann@36301
   867
 ((e-f)*(a-b)*(c-d))/((e-f)*(a-b)*(c-d)) + u"
haftmann@36301
   868
apply(subgoal_tac "(c-d)*(e-f)*(a-b) > 0")
haftmann@36301
   869
 prefer 2 apply(simp add:sign_simps)
haftmann@36301
   870
apply(subgoal_tac "(c-d)*(e-f)*(a-b)*u > 0")
haftmann@36301
   871
 prefer 2 apply(simp add:sign_simps)
haftmann@36301
   872
apply(simp add:field_simps)
haftmann@36301
   873
done
haftmann@36301
   874
*)
haftmann@36301
   875
nipkow@56541
   876
lemma divide_pos_pos[simp]:
haftmann@36301
   877
  "0 < x ==> 0 < y ==> 0 < x / y"
haftmann@36301
   878
by(simp add:field_simps)
haftmann@36301
   879
haftmann@36301
   880
lemma divide_nonneg_pos:
haftmann@36301
   881
  "0 <= x ==> 0 < y ==> 0 <= x / y"
haftmann@36301
   882
by(simp add:field_simps)
haftmann@36301
   883
haftmann@36301
   884
lemma divide_neg_pos:
haftmann@36301
   885
  "x < 0 ==> 0 < y ==> x / y < 0"
haftmann@36301
   886
by(simp add:field_simps)
haftmann@36301
   887
haftmann@36301
   888
lemma divide_nonpos_pos:
haftmann@36301
   889
  "x <= 0 ==> 0 < y ==> x / y <= 0"
haftmann@36301
   890
by(simp add:field_simps)
haftmann@36301
   891
haftmann@36301
   892
lemma divide_pos_neg:
haftmann@36301
   893
  "0 < x ==> y < 0 ==> x / y < 0"
haftmann@36301
   894
by(simp add:field_simps)
haftmann@36301
   895
haftmann@36301
   896
lemma divide_nonneg_neg:
lp15@59667
   897
  "0 <= x ==> y < 0 ==> x / y <= 0"
haftmann@36301
   898
by(simp add:field_simps)
haftmann@36301
   899
haftmann@36301
   900
lemma divide_neg_neg:
haftmann@36301
   901
  "x < 0 ==> y < 0 ==> 0 < x / y"
haftmann@36301
   902
by(simp add:field_simps)
haftmann@36301
   903
haftmann@36301
   904
lemma divide_nonpos_neg:
haftmann@36301
   905
  "x <= 0 ==> y < 0 ==> 0 <= x / y"
haftmann@36301
   906
by(simp add:field_simps)
haftmann@36301
   907
haftmann@36301
   908
lemma divide_strict_right_mono:
haftmann@36301
   909
     "[|a < b; 0 < c|] ==> a / c < b / c"
lp15@59667
   910
by (simp add: less_imp_not_eq2 divide_inverse mult_strict_right_mono
haftmann@36301
   911
              positive_imp_inverse_positive)
haftmann@36301
   912
haftmann@36301
   913
haftmann@36301
   914
lemma divide_strict_right_mono_neg:
haftmann@36301
   915
     "[|b < a; c < 0|] ==> a / c < b / c"
haftmann@36301
   916
apply (drule divide_strict_right_mono [of _ _ "-c"], simp)
haftmann@36301
   917
apply (simp add: less_imp_not_eq nonzero_minus_divide_right [symmetric])
haftmann@36301
   918
done
haftmann@36301
   919
wenzelm@60758
   920
text\<open>The last premise ensures that @{term a} and @{term b}
wenzelm@60758
   921
      have the same sign\<close>
haftmann@36301
   922
lemma divide_strict_left_mono:
haftmann@36301
   923
  "[|b < a; 0 < c; 0 < a*b|] ==> c / a < c / b"
huffman@44921
   924
  by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono)
haftmann@36301
   925
haftmann@36301
   926
lemma divide_left_mono:
haftmann@36301
   927
  "[|b \<le> a; 0 \<le> c; 0 < a*b|] ==> c / a \<le> c / b"
huffman@44921
   928
  by (auto simp: field_simps zero_less_mult_iff mult_right_mono)
haftmann@36301
   929
haftmann@36301
   930
lemma divide_strict_left_mono_neg:
haftmann@36301
   931
  "[|a < b; c < 0; 0 < a*b|] ==> c / a < c / b"
huffman@44921
   932
  by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono_neg)
haftmann@36301
   933
haftmann@36301
   934
lemma mult_imp_div_pos_le: "0 < y ==> x <= z * y ==>
haftmann@36301
   935
    x / y <= z"
haftmann@36301
   936
by (subst pos_divide_le_eq, assumption+)
haftmann@36301
   937
haftmann@36301
   938
lemma mult_imp_le_div_pos: "0 < y ==> z * y <= x ==>
haftmann@36301
   939
    z <= x / y"
haftmann@36301
   940
by(simp add:field_simps)
haftmann@36301
   941
haftmann@36301
   942
lemma mult_imp_div_pos_less: "0 < y ==> x < z * y ==>
haftmann@36301
   943
    x / y < z"
haftmann@36301
   944
by(simp add:field_simps)
haftmann@36301
   945
haftmann@36301
   946
lemma mult_imp_less_div_pos: "0 < y ==> z * y < x ==>
haftmann@36301
   947
    z < x / y"
haftmann@36301
   948
by(simp add:field_simps)
haftmann@36301
   949
lp15@59667
   950
lemma frac_le: "0 <= x ==>
haftmann@36301
   951
    x <= y ==> 0 < w ==> w <= z  ==> x / z <= y / w"
haftmann@36301
   952
  apply (rule mult_imp_div_pos_le)
haftmann@36301
   953
  apply simp
haftmann@36301
   954
  apply (subst times_divide_eq_left)
haftmann@36301
   955
  apply (rule mult_imp_le_div_pos, assumption)
haftmann@36301
   956
  apply (rule mult_mono)
haftmann@36301
   957
  apply simp_all
haftmann@36301
   958
done
haftmann@36301
   959
lp15@59667
   960
lemma frac_less: "0 <= x ==>
haftmann@36301
   961
    x < y ==> 0 < w ==> w <= z  ==> x / z < y / w"
haftmann@36301
   962
  apply (rule mult_imp_div_pos_less)
haftmann@36301
   963
  apply simp
haftmann@36301
   964
  apply (subst times_divide_eq_left)
haftmann@36301
   965
  apply (rule mult_imp_less_div_pos, assumption)
haftmann@36301
   966
  apply (erule mult_less_le_imp_less)
haftmann@36301
   967
  apply simp_all
haftmann@36301
   968
done
haftmann@36301
   969
lp15@59667
   970
lemma frac_less2: "0 < x ==>
haftmann@36301
   971
    x <= y ==> 0 < w ==> w < z  ==> x / z < y / w"
haftmann@36301
   972
  apply (rule mult_imp_div_pos_less)
haftmann@36301
   973
  apply simp_all
haftmann@36301
   974
  apply (rule mult_imp_less_div_pos, assumption)
haftmann@36301
   975
  apply (erule mult_le_less_imp_less)
haftmann@36301
   976
  apply simp_all
haftmann@36301
   977
done
haftmann@36301
   978
haftmann@36301
   979
lemma less_half_sum: "a < b ==> a < (a+b) / (1+1)"
haftmann@36301
   980
by (simp add: field_simps zero_less_two)
haftmann@36301
   981
haftmann@36301
   982
lemma gt_half_sum: "a < b ==> (a+b)/(1+1) < b"
haftmann@36301
   983
by (simp add: field_simps zero_less_two)
haftmann@36301
   984
hoelzl@53215
   985
subclass unbounded_dense_linorder
haftmann@36301
   986
proof
haftmann@36301
   987
  fix x y :: 'a
lp15@59667
   988
  from less_add_one show "\<exists>y. x < y" ..
haftmann@36301
   989
  from less_add_one have "x + (- 1) < (x + 1) + (- 1)" by (rule add_strict_right_mono)
haftmann@54230
   990
  then have "x - 1 < x + 1 - 1" by simp
haftmann@36301
   991
  then have "x - 1 < x" by (simp add: algebra_simps)
haftmann@36301
   992
  then show "\<exists>y. y < x" ..
haftmann@36301
   993
  show "x < y \<Longrightarrow> \<exists>z>x. z < y" by (blast intro!: less_half_sum gt_half_sum)
haftmann@36301
   994
qed
haftmann@36301
   995
haftmann@64290
   996
subclass field_abs_sgn ..
haftmann@64290
   997
haftmann@64329
   998
lemma inverse_sgn [simp]:
haftmann@64329
   999
  "inverse (sgn a) = sgn a"
haftmann@64329
  1000
  by (cases a 0 rule: linorder_cases) simp_all
haftmann@64329
  1001
haftmann@64329
  1002
lemma divide_sgn [simp]:
haftmann@64329
  1003
  "a / sgn b = a * sgn b"
haftmann@64329
  1004
  by (cases b 0 rule: linorder_cases) simp_all
haftmann@64329
  1005
haftmann@36301
  1006
lemma nonzero_abs_inverse:
haftmann@64290
  1007
  "a \<noteq> 0 ==> \<bar>inverse a\<bar> = inverse \<bar>a\<bar>"
haftmann@64290
  1008
  by (rule abs_inverse)
haftmann@36301
  1009
haftmann@36301
  1010
lemma nonzero_abs_divide:
haftmann@64290
  1011
  "b \<noteq> 0 ==> \<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
haftmann@64290
  1012
  by (rule abs_divide)
haftmann@36301
  1013
haftmann@36301
  1014
lemma field_le_epsilon:
haftmann@36301
  1015
  assumes e: "\<And>e. 0 < e \<Longrightarrow> x \<le> y + e"
haftmann@36301
  1016
  shows "x \<le> y"
haftmann@36301
  1017
proof (rule dense_le)
haftmann@36301
  1018
  fix t assume "t < x"
haftmann@36301
  1019
  hence "0 < x - t" by (simp add: less_diff_eq)
haftmann@36301
  1020
  from e [OF this] have "x + 0 \<le> x + (y - t)" by (simp add: algebra_simps)
haftmann@36301
  1021
  then have "0 \<le> y - t" by (simp only: add_le_cancel_left)
haftmann@36301
  1022
  then show "t \<le> y" by (simp add: algebra_simps)
haftmann@36301
  1023
qed
haftmann@36301
  1024
paulson@14277
  1025
lemma inverse_positive_iff_positive [simp]:
haftmann@36409
  1026
  "(0 < inverse a) = (0 < a)"
haftmann@21328
  1027
apply (cases "a = 0", simp)
paulson@14277
  1028
apply (blast intro: inverse_positive_imp_positive positive_imp_inverse_positive)
paulson@14277
  1029
done
paulson@14277
  1030
paulson@14277
  1031
lemma inverse_negative_iff_negative [simp]:
haftmann@36409
  1032
  "(inverse a < 0) = (a < 0)"
haftmann@21328
  1033
apply (cases "a = 0", simp)
paulson@14277
  1034
apply (blast intro: inverse_negative_imp_negative negative_imp_inverse_negative)
paulson@14277
  1035
done
paulson@14277
  1036
paulson@14277
  1037
lemma inverse_nonnegative_iff_nonnegative [simp]:
haftmann@36409
  1038
  "0 \<le> inverse a \<longleftrightarrow> 0 \<le> a"
haftmann@36409
  1039
  by (simp add: not_less [symmetric])
paulson@14277
  1040
paulson@14277
  1041
lemma inverse_nonpositive_iff_nonpositive [simp]:
haftmann@36409
  1042
  "inverse a \<le> 0 \<longleftrightarrow> a \<le> 0"
haftmann@36409
  1043
  by (simp add: not_less [symmetric])
paulson@14277
  1044
hoelzl@56480
  1045
lemma one_less_inverse_iff: "1 < inverse x \<longleftrightarrow> 0 < x \<and> x < 1"
hoelzl@56480
  1046
  using less_trans[of 1 x 0 for x]
hoelzl@56480
  1047
  by (cases x 0 rule: linorder_cases) (auto simp add: field_simps)
paulson@14365
  1048
hoelzl@56480
  1049
lemma one_le_inverse_iff: "1 \<le> inverse x \<longleftrightarrow> 0 < x \<and> x \<le> 1"
haftmann@36409
  1050
proof (cases "x = 1")
haftmann@36409
  1051
  case True then show ?thesis by simp
haftmann@36409
  1052
next
haftmann@36409
  1053
  case False then have "inverse x \<noteq> 1" by simp
haftmann@36409
  1054
  then have "1 \<noteq> inverse x" by blast
haftmann@36409
  1055
  then have "1 \<le> inverse x \<longleftrightarrow> 1 < inverse x" by (simp add: le_less)
haftmann@36409
  1056
  with False show ?thesis by (auto simp add: one_less_inverse_iff)
haftmann@36409
  1057
qed
paulson@14365
  1058
hoelzl@56480
  1059
lemma inverse_less_1_iff: "inverse x < 1 \<longleftrightarrow> x \<le> 0 \<or> 1 < x"
lp15@59667
  1060
  by (simp add: not_le [symmetric] one_le_inverse_iff)
paulson@14365
  1061
hoelzl@56480
  1062
lemma inverse_le_1_iff: "inverse x \<le> 1 \<longleftrightarrow> x \<le> 0 \<or> 1 \<le> x"
lp15@59667
  1063
  by (simp add: not_less [symmetric] one_less_inverse_iff)
paulson@14365
  1064
hoelzl@56481
  1065
lemma [divide_simps]:
hoelzl@56480
  1066
  shows le_divide_eq: "a \<le> b / c \<longleftrightarrow> (if 0 < c then a * c \<le> b else if c < 0 then b \<le> a * c else a \<le> 0)"
hoelzl@56480
  1067
    and divide_le_eq: "b / c \<le> a \<longleftrightarrow> (if 0 < c then b \<le> a * c else if c < 0 then a * c \<le> b else 0 \<le> a)"
hoelzl@56480
  1068
    and less_divide_eq: "a < b / c \<longleftrightarrow> (if 0 < c then a * c < b else if c < 0 then b < a * c else a < 0)"
hoelzl@56480
  1069
    and divide_less_eq: "b / c < a \<longleftrightarrow> (if 0 < c then b < a * c else if c < 0 then a * c < b else 0 < a)"
hoelzl@56481
  1070
    and le_minus_divide_eq: "a \<le> - (b / c) \<longleftrightarrow> (if 0 < c then a * c \<le> - b else if c < 0 then - b \<le> a * c else a \<le> 0)"
hoelzl@56481
  1071
    and minus_divide_le_eq: "- (b / c) \<le> a \<longleftrightarrow> (if 0 < c then - b \<le> a * c else if c < 0 then a * c \<le> - b else 0 \<le> a)"
hoelzl@56481
  1072
    and less_minus_divide_eq: "a < - (b / c) \<longleftrightarrow> (if 0 < c then a * c < - b else if c < 0 then - b < a * c else  a < 0)"
hoelzl@56481
  1073
    and minus_divide_less_eq: "- (b / c) < a \<longleftrightarrow> (if 0 < c then - b < a * c else if c < 0 then a * c < - b else 0 < a)"
hoelzl@56480
  1074
  by (auto simp: field_simps not_less dest: antisym)
paulson@14288
  1075
wenzelm@60758
  1076
text \<open>Division and Signs\<close>
avigad@16775
  1077
hoelzl@56480
  1078
lemma
hoelzl@56480
  1079
  shows zero_less_divide_iff: "0 < a / b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
hoelzl@56480
  1080
    and divide_less_0_iff: "a / b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
hoelzl@56480
  1081
    and zero_le_divide_iff: "0 \<le> a / b \<longleftrightarrow> 0 \<le> a \<and> 0 \<le> b \<or> a \<le> 0 \<and> b \<le> 0"
hoelzl@56480
  1082
    and divide_le_0_iff: "a / b \<le> 0 \<longleftrightarrow> 0 \<le> a \<and> b \<le> 0 \<or> a \<le> 0 \<and> 0 \<le> b"
hoelzl@56481
  1083
  by (auto simp add: divide_simps)
avigad@16775
  1084
wenzelm@60758
  1085
text \<open>Division and the Number One\<close>
paulson@14353
  1086
wenzelm@60758
  1087
text\<open>Simplify expressions equated with 1\<close>
paulson@14353
  1088
hoelzl@56480
  1089
lemma zero_eq_1_divide_iff [simp]: "0 = 1 / a \<longleftrightarrow> a = 0"
hoelzl@56480
  1090
  by (cases "a = 0") (auto simp: field_simps)
paulson@14353
  1091
hoelzl@56480
  1092
lemma one_divide_eq_0_iff [simp]: "1 / a = 0 \<longleftrightarrow> a = 0"
hoelzl@56480
  1093
  using zero_eq_1_divide_iff[of a] by simp
paulson@14353
  1094
wenzelm@61799
  1095
text\<open>Simplify expressions such as \<open>0 < 1/x\<close> to \<open>0 < x\<close>\<close>
haftmann@36423
  1096
blanchet@54147
  1097
lemma zero_le_divide_1_iff [simp]:
haftmann@36423
  1098
  "0 \<le> 1 / a \<longleftrightarrow> 0 \<le> a"
haftmann@36423
  1099
  by (simp add: zero_le_divide_iff)
paulson@17085
  1100
blanchet@54147
  1101
lemma zero_less_divide_1_iff [simp]:
haftmann@36423
  1102
  "0 < 1 / a \<longleftrightarrow> 0 < a"
haftmann@36423
  1103
  by (simp add: zero_less_divide_iff)
haftmann@36423
  1104
blanchet@54147
  1105
lemma divide_le_0_1_iff [simp]:
haftmann@36423
  1106
  "1 / a \<le> 0 \<longleftrightarrow> a \<le> 0"
haftmann@36423
  1107
  by (simp add: divide_le_0_iff)
haftmann@36423
  1108
blanchet@54147
  1109
lemma divide_less_0_1_iff [simp]:
haftmann@36423
  1110
  "1 / a < 0 \<longleftrightarrow> a < 0"
haftmann@36423
  1111
  by (simp add: divide_less_0_iff)
paulson@14353
  1112
paulson@14293
  1113
lemma divide_right_mono:
haftmann@36409
  1114
     "[|a \<le> b; 0 \<le> c|] ==> a/c \<le> b/c"
haftmann@36409
  1115
by (force simp add: divide_strict_right_mono le_less)
paulson@14293
  1116
lp15@59667
  1117
lemma divide_right_mono_neg: "a <= b
avigad@16775
  1118
    ==> c <= 0 ==> b / c <= a / c"
nipkow@23482
  1119
apply (drule divide_right_mono [of _ _ "- c"])
hoelzl@56479
  1120
apply auto
avigad@16775
  1121
done
avigad@16775
  1122
lp15@59667
  1123
lemma divide_left_mono_neg: "a <= b
avigad@16775
  1124
    ==> c <= 0 ==> 0 < a * b ==> c / a <= c / b"
avigad@16775
  1125
  apply (drule divide_left_mono [of _ _ "- c"])
haftmann@57512
  1126
  apply (auto simp add: mult.commute)
avigad@16775
  1127
done
avigad@16775
  1128
hoelzl@56480
  1129
lemma inverse_le_iff: "inverse a \<le> inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b \<le> a) \<and> (a * b \<le> 0 \<longrightarrow> a \<le> b)"
hoelzl@56480
  1130
  by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
hoelzl@56480
  1131
     (auto simp add: field_simps zero_less_mult_iff mult_le_0_iff)
hoelzl@42904
  1132
hoelzl@56480
  1133
lemma inverse_less_iff: "inverse a < inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b < a) \<and> (a * b \<le> 0 \<longrightarrow> a < b)"
hoelzl@42904
  1134
  by (subst less_le) (auto simp: inverse_le_iff)
hoelzl@42904
  1135
hoelzl@56480
  1136
lemma divide_le_cancel: "a / c \<le> b / c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
hoelzl@42904
  1137
  by (simp add: divide_inverse mult_le_cancel_right)
hoelzl@42904
  1138
hoelzl@56480
  1139
lemma divide_less_cancel: "a / c < b / c \<longleftrightarrow> (0 < c \<longrightarrow> a < b) \<and> (c < 0 \<longrightarrow> b < a) \<and> c \<noteq> 0"
hoelzl@42904
  1140
  by (auto simp add: divide_inverse mult_less_cancel_right)
hoelzl@42904
  1141
wenzelm@60758
  1142
text\<open>Simplify quotients that are compared with the value 1.\<close>
avigad@16775
  1143
blanchet@54147
  1144
lemma le_divide_eq_1:
haftmann@36409
  1145
  "(1 \<le> b / a) = ((0 < a & a \<le> b) | (a < 0 & b \<le> a))"
avigad@16775
  1146
by (auto simp add: le_divide_eq)
avigad@16775
  1147
blanchet@54147
  1148
lemma divide_le_eq_1:
haftmann@36409
  1149
  "(b / a \<le> 1) = ((0 < a & b \<le> a) | (a < 0 & a \<le> b) | a=0)"
avigad@16775
  1150
by (auto simp add: divide_le_eq)
avigad@16775
  1151
blanchet@54147
  1152
lemma less_divide_eq_1:
haftmann@36409
  1153
  "(1 < b / a) = ((0 < a & a < b) | (a < 0 & b < a))"
avigad@16775
  1154
by (auto simp add: less_divide_eq)
avigad@16775
  1155
blanchet@54147
  1156
lemma divide_less_eq_1:
haftmann@36409
  1157
  "(b / a < 1) = ((0 < a & b < a) | (a < 0 & a < b) | a=0)"
avigad@16775
  1158
by (auto simp add: divide_less_eq)
avigad@16775
  1159
hoelzl@56571
  1160
lemma divide_nonneg_nonneg [simp]:
hoelzl@56571
  1161
  "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x / y"
hoelzl@56571
  1162
  by (auto simp add: divide_simps)
hoelzl@56571
  1163
hoelzl@56571
  1164
lemma divide_nonpos_nonpos:
hoelzl@56571
  1165
  "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> 0 \<le> x / y"
hoelzl@56571
  1166
  by (auto simp add: divide_simps)
hoelzl@56571
  1167
hoelzl@56571
  1168
lemma divide_nonneg_nonpos:
hoelzl@56571
  1169
  "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> x / y \<le> 0"
hoelzl@56571
  1170
  by (auto simp add: divide_simps)
hoelzl@56571
  1171
hoelzl@56571
  1172
lemma divide_nonpos_nonneg:
hoelzl@56571
  1173
  "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x / y \<le> 0"
hoelzl@56571
  1174
  by (auto simp add: divide_simps)
wenzelm@23389
  1175
wenzelm@60758
  1176
text \<open>Conditional Simplification Rules: No Case Splits\<close>
avigad@16775
  1177
blanchet@54147
  1178
lemma le_divide_eq_1_pos [simp]:
haftmann@36409
  1179
  "0 < a \<Longrightarrow> (1 \<le> b/a) = (a \<le> b)"
avigad@16775
  1180
by (auto simp add: le_divide_eq)
avigad@16775
  1181
blanchet@54147
  1182
lemma le_divide_eq_1_neg [simp]:
haftmann@36409
  1183
  "a < 0 \<Longrightarrow> (1 \<le> b/a) = (b \<le> a)"
avigad@16775
  1184
by (auto simp add: le_divide_eq)
avigad@16775
  1185
blanchet@54147
  1186
lemma divide_le_eq_1_pos [simp]:
haftmann@36409
  1187
  "0 < a \<Longrightarrow> (b/a \<le> 1) = (b \<le> a)"
avigad@16775
  1188
by (auto simp add: divide_le_eq)
avigad@16775
  1189
blanchet@54147
  1190
lemma divide_le_eq_1_neg [simp]:
haftmann@36409
  1191
  "a < 0 \<Longrightarrow> (b/a \<le> 1) = (a \<le> b)"
avigad@16775
  1192
by (auto simp add: divide_le_eq)
avigad@16775
  1193
blanchet@54147
  1194
lemma less_divide_eq_1_pos [simp]:
haftmann@36409
  1195
  "0 < a \<Longrightarrow> (1 < b/a) = (a < b)"
avigad@16775
  1196
by (auto simp add: less_divide_eq)
avigad@16775
  1197
blanchet@54147
  1198
lemma less_divide_eq_1_neg [simp]:
haftmann@36409
  1199
  "a < 0 \<Longrightarrow> (1 < b/a) = (b < a)"
avigad@16775
  1200
by (auto simp add: less_divide_eq)
avigad@16775
  1201
blanchet@54147
  1202
lemma divide_less_eq_1_pos [simp]:
haftmann@36409
  1203
  "0 < a \<Longrightarrow> (b/a < 1) = (b < a)"
paulson@18649
  1204
by (auto simp add: divide_less_eq)
paulson@18649
  1205
blanchet@54147
  1206
lemma divide_less_eq_1_neg [simp]:
wenzelm@61941
  1207
  "a < 0 \<Longrightarrow> b/a < 1 \<longleftrightarrow> a < b"
avigad@16775
  1208
by (auto simp add: divide_less_eq)
avigad@16775
  1209
blanchet@54147
  1210
lemma eq_divide_eq_1 [simp]:
haftmann@36409
  1211
  "(1 = b/a) = ((a \<noteq> 0 & a = b))"
avigad@16775
  1212
by (auto simp add: eq_divide_eq)
avigad@16775
  1213
blanchet@54147
  1214
lemma divide_eq_eq_1 [simp]:
haftmann@36409
  1215
  "(b/a = 1) = ((a \<noteq> 0 & a = b))"
avigad@16775
  1216
by (auto simp add: divide_eq_eq)
avigad@16775
  1217
lp15@59667
  1218
lemma abs_div_pos: "0 < y ==>
haftmann@36301
  1219
    \<bar>x\<bar> / y = \<bar>x / y\<bar>"
haftmann@25304
  1220
  apply (subst abs_divide)
haftmann@25304
  1221
  apply (simp add: order_less_imp_le)
haftmann@25304
  1222
done
avigad@16775
  1223
wenzelm@61944
  1224
lemma zero_le_divide_abs_iff [simp]: "(0 \<le> a / \<bar>b\<bar>) = (0 \<le> a | b = 0)"
lp15@55718
  1225
by (auto simp: zero_le_divide_iff)
lp15@55718
  1226
wenzelm@61944
  1227
lemma divide_le_0_abs_iff [simp]: "(a / \<bar>b\<bar> \<le> 0) = (a \<le> 0 | b = 0)"
lp15@55718
  1228
by (auto simp: divide_le_0_iff)
lp15@55718
  1229
hoelzl@35579
  1230
lemma field_le_mult_one_interval:
hoelzl@35579
  1231
  assumes *: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
hoelzl@35579
  1232
  shows "x \<le> y"
hoelzl@35579
  1233
proof (cases "0 < x")
hoelzl@35579
  1234
  assume "0 < x"
hoelzl@35579
  1235
  thus ?thesis
hoelzl@35579
  1236
    using dense_le_bounded[of 0 1 "y/x"] *
wenzelm@60758
  1237
    unfolding le_divide_eq if_P[OF \<open>0 < x\<close>] by simp
hoelzl@35579
  1238
next
hoelzl@35579
  1239
  assume "\<not>0 < x" hence "x \<le> 0" by simp
wenzelm@61076
  1240
  obtain s::'a where s: "0 < s" "s < 1" using dense[of 0 "1::'a"] by auto
wenzelm@60758
  1241
  hence "x \<le> s * x" using mult_le_cancel_right[of 1 x s] \<open>x \<le> 0\<close> by auto
hoelzl@35579
  1242
  also note *[OF s]
hoelzl@35579
  1243
  finally show ?thesis .
hoelzl@35579
  1244
qed
haftmann@35090
  1245
lp15@63952
  1246
text\<open>For creating values between @{term u} and @{term v}.\<close>
lp15@63952
  1247
lemma scaling_mono:
lp15@63952
  1248
  assumes "u \<le> v" "0 \<le> r" "r \<le> s"
lp15@63952
  1249
    shows "u + r * (v - u) / s \<le> v"
lp15@63952
  1250
proof -
lp15@63952
  1251
  have "r/s \<le> 1" using assms
lp15@63952
  1252
    using divide_le_eq_1 by fastforce
lp15@63952
  1253
  then have "(r/s) * (v - u) \<le> 1 * (v - u)"
lp15@63952
  1254
    apply (rule mult_right_mono)
lp15@63952
  1255
    using assms by simp
lp15@63952
  1256
  then show ?thesis
lp15@63952
  1257
    by (simp add: field_simps)
lp15@63952
  1258
qed
lp15@63952
  1259
haftmann@36409
  1260
end
haftmann@36409
  1261
lp15@61238
  1262
text \<open>Min/max Simplification Rules\<close>
lp15@61238
  1263
lp15@61238
  1264
lemma min_mult_distrib_left:
lp15@61238
  1265
  fixes x::"'a::linordered_idom" 
lp15@61238
  1266
  shows "p * min x y = (if 0 \<le> p then min (p*x) (p*y) else max (p*x) (p*y))"
lp15@61238
  1267
by (auto simp add: min_def max_def mult_le_cancel_left)
lp15@61238
  1268
lp15@61238
  1269
lemma min_mult_distrib_right:
lp15@61238
  1270
  fixes x::"'a::linordered_idom" 
lp15@61238
  1271
  shows "min x y * p = (if 0 \<le> p then min (x*p) (y*p) else max (x*p) (y*p))"
lp15@61238
  1272
by (auto simp add: min_def max_def mult_le_cancel_right)
lp15@61238
  1273
lp15@61238
  1274
lemma min_divide_distrib_right:
lp15@61238
  1275
  fixes x::"'a::linordered_field" 
lp15@61238
  1276
  shows "min x y / p = (if 0 \<le> p then min (x/p) (y/p) else max (x/p) (y/p))"
lp15@61238
  1277
by (simp add: min_mult_distrib_right divide_inverse)
lp15@61238
  1278
lp15@61238
  1279
lemma max_mult_distrib_left:
lp15@61238
  1280
  fixes x::"'a::linordered_idom" 
lp15@61238
  1281
  shows "p * max x y = (if 0 \<le> p then max (p*x) (p*y) else min (p*x) (p*y))"
lp15@61238
  1282
by (auto simp add: min_def max_def mult_le_cancel_left)
lp15@61238
  1283
lp15@61238
  1284
lemma max_mult_distrib_right:
lp15@61238
  1285
  fixes x::"'a::linordered_idom" 
lp15@61238
  1286
  shows "max x y * p = (if 0 \<le> p then max (x*p) (y*p) else min (x*p) (y*p))"
lp15@61238
  1287
by (auto simp add: min_def max_def mult_le_cancel_right)
lp15@61238
  1288
lp15@61238
  1289
lemma max_divide_distrib_right:
lp15@61238
  1290
  fixes x::"'a::linordered_field" 
lp15@61238
  1291
  shows "max x y / p = (if 0 \<le> p then max (x/p) (y/p) else min (x/p) (y/p))"
lp15@61238
  1292
by (simp add: max_mult_distrib_right divide_inverse)
lp15@61238
  1293
haftmann@59557
  1294
hide_fact (open) field_inverse field_divide_inverse field_inverse_zero
haftmann@59557
  1295
haftmann@52435
  1296
code_identifier
haftmann@52435
  1297
  code_module Fields \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
lp15@59667
  1298
paulson@14265
  1299
end