Skip to content

Commit 53e45bb

Browse files
Improve reduction behaviour of rationals (#1764)
1 parent ae0702e commit 53e45bb

File tree

5 files changed

+215
-133
lines changed

5 files changed

+215
-133
lines changed

CHANGELOG.md

+36
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,39 @@ Non-backwards compatible changes
306306
∣↥p∣≢0⇒p≄0 : ∀ p → ℤ.∣ (↥ p) ∣ ≢0 → p ≠ 0ℚᵘ
307307
```
308308

309+
### Change in reduction behaviour of rationals
310+
311+
* Currently arithmetic expressions involving rationals (both normalised and
312+
unnormalised) undergo disastorous exponential normalisation. For example,
313+
`p + q` would often be normalised by Agda to
314+
`(↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)`. While the normalised form
315+
of `p + q + r + s + t + u + v` would be ~700 lines long. This behaviour
316+
often chokes both type-checking and the display of the expressions in the IDE.
317+
318+
* To avoid this expansion and make non-trivial reasoning about rationals actually feasible:
319+
1. the records `ℚᵘ` and `` have both had the `no-eta-equality` flag enabled
320+
2. definition of arithmetic operations have trivial pattern matching added to
321+
prevent them reducing, e.g.
322+
```agda
323+
p + q = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
324+
```
325+
has been changed to
326+
```
327+
p@record{} + q@record{} = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
328+
```
329+
330+
* As a consequence of this, some proofs that relied on this reduction behaviour
331+
or on eta-equality may no longer go through. There are several ways to fix this:
332+
1. The principled way is to not rely on this reduction behaviour in the first place.
333+
The `Properties` files for rational numbers have been greatly expanded in `v1.7`
334+
and `v2.0`, and we believe most proofs should be able to be built up from existing
335+
proofs contained within these files.
336+
2. Alternatively, annotating any rational arguments to a proof with either
337+
`@record{}` or `@(mkℚ _ _ _)` should restore the old reduction behaviour for any
338+
terms involving those parameters.
339+
3. Finally, if the above approaches are not viable then you may be forced to explicitly
340+
use `cong` combined with a lemma that proves the old reduction behaviour.
341+
309342
### Change in the definition of `Prime`
310343
311344
* The definition of `Prime` in `Data.Nat.Primality` was:
@@ -1306,6 +1339,9 @@ Other minor changes
13061339

13071340
* Added new definitions and proofs in `Data.Rational.Properties`:
13081341
```agda
1342+
↥ᵘ-toℚᵘ : ↥ᵘ (toℚᵘ p) ≡ ↥ p
1343+
↧ᵘ-toℚᵘ : ↧ᵘ (toℚᵘ p) ≡ ↧ p
1344+
13091345
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ
13101346
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ
13111347
toℚᵘ-isNearSemiringHomomorphism-+-* : IsNearSemiringHomomorphism +-*-rawNearSemiring ℚᵘ.+-*-rawNearSemiring toℚᵘ

src/Data/Rational/Base.agda

+24-15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ open import Relation.Binary.PropositionalEquality.Core
3030
-- way to represent every rational number.
3131

3232
record : Set where
33+
-- We add "no-eta-equality; pattern" to the record to stop Agda
34+
-- automatically unfolding rationals when arithmetic operations are
35+
-- applied to them (see definition of operators below and Issue #1753
36+
-- for details).
37+
no-eta-equality; pattern
38+
3339
constructor mkℚ
3440
field
3541
numerator :
@@ -178,41 +184,44 @@ NonNegative p = ℚᵘ.NonNegative (toℚᵘ p)
178184
≢-nonZero {mkℚ +0 (suc d) c} p≢0 = contradiction (λ {i} C.recompute c {i}) ¬0-coprimeTo-2+
179185

180186
>-nonZero : {p} p > 0ℚ NonZero p
181-
>-nonZero {p} (*<* p<q) = ℚᵘ.>-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
187+
>-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.>-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
182188

183189
<-nonZero : {p} p < 0ℚ NonZero p
184-
<-nonZero {p} (*<* p<q) = ℚᵘ.<-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
190+
<-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.<-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
185191

186192
positive : {p} p > 0ℚ Positive p
187-
positive {p} (*<* p<q) = ℚᵘ.positive {toℚᵘ p} (ℚᵘ.*<* p<q)
193+
positive {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.positive {toℚᵘ p} (ℚᵘ.*<* p<q)
188194

189195
negative : {p} p < 0ℚ Negative p
190-
negative {p} (*<* p<q) = ℚᵘ.negative {toℚᵘ p} (ℚᵘ.*<* p<q)
196+
negative {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.negative {toℚᵘ p} (ℚᵘ.*<* p<q)
191197

192198
nonPositive : {p} p ≤ 0ℚ NonPositive p
193-
nonPositive {p} (*≤* p≤q) = ℚᵘ.nonPositive {toℚᵘ p} (ℚᵘ.*≤* p≤q)
199+
nonPositive {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonPositive {toℚᵘ p} (ℚᵘ.*≤* p≤q)
194200

195201
nonNegative : {p} p ≥ 0ℚ NonNegative p
196-
nonNegative {p} (*≤* p≤q) = ℚᵘ.nonNegative {toℚᵘ p} (ℚᵘ.*≤* p≤q)
202+
nonNegative {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonNegative {toℚᵘ p} (ℚᵘ.*≤* p≤q)
197203

198204
------------------------------------------------------------------------------
199205
-- Operations on rationals
200206

207+
-- For explanation of the `@record{}` annotations see notes in the equivalent
208+
-- place in `Data.Rational.Unnormalised.Base`.
209+
201210
infix 8 -_ 1/_
202211
infixl 7 _*_ _÷_ _⊓_
203212
infixl 6 _-_ _+_ _⊔_
204213

205214
-- addition
206215
_+_ :
207-
p + q = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
216+
p@record{} + q@record{} = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
208217

209218
-- multiplication
210219
_*_ :
211-
p * q = (↥ p ℤ.* ↥ q) / (↧ₙ p ℕ.* ↧ₙ q)
220+
p@record{} * q@record{} = (↥ p ℤ.* ↥ q) / (↧ₙ p ℕ.* ↧ₙ q)
212221

213222
-- subtraction
214223
_-_ :
215-
p - q = p + (- q)
224+
p@record{} - q@record{} = p + (- q)
216225

217226
-- reciprocal: requires a proof that the numerator is not zero
218227
1/_ : (p : ℚ) .{{_ : NonZero p}}
@@ -221,15 +230,15 @@ p - q = p + (- q)
221230

222231
-- division: requires a proof that the denominator is not zero
223232
_÷_ : (p q : ℚ) .{{_ : NonZero q}}
224-
p ÷ q = p * (1/ q)
233+
p@record{} ÷ q@record{} = p * (1/ q)
225234

226235
-- max
227236
_⊔_ : (p q : ℚ)
228-
p ⊔ q = if p ≤ᵇ q then q else p
237+
p@record{} ⊔ q@record{} = if p ≤ᵇ q then q else p
229238

230239
-- min
231240
_⊓_ : (p q : ℚ)
232-
p ⊓ q = if p ≤ᵇ q then p else q
241+
p@record{} ⊓ q@record{} = if p ≤ᵇ q then p else q
233242

234243
-- absolute value
235244
∣_∣ :
@@ -240,11 +249,11 @@ p ⊓ q = if p ≤ᵇ q then p else q
240249

241250
-- Floor (round towards -∞)
242251
floor :
243-
floor p = ↥ p ℤ./ ↧ p
252+
floor p@record{} = ↥ p ℤ./ ↧ p
244253

245254
-- Ceiling (round towards +∞)
246255
ceiling :
247-
ceiling p = ℤ.- floor (- p)
256+
ceiling p@record{} = ℤ.- floor (- p)
248257

249258
-- Truncate (round towards 0)
250259
truncate :
@@ -260,7 +269,7 @@ round p with p ≤ᵇ 0ℚ
260269

261270
-- Fractional part (remainder after floor)
262271
fracPart :
263-
fracPart p = ∣ p - truncate p / 1
272+
fracPart p@record{} = ∣ p - truncate p / 1
264273

265274
-- Extra notations ⌊ ⌋ floor, ⌈ ⌉ ceiling, [ ] truncate
266275
syntax floor p = ⌊ p ⌋

src/Data/Rational/Properties.agda

+67-48
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,18 @@ normalize-injective-≃ m n c d eq = ℕ./-cancelʳ-≡
380380
/-cong { -[1+ n ]} refl = cong -_ ∘′ normalize-cong {suc n} refl
381381

382382
private
383-
/-injective-≃-helper : {m n c-1 d-1}
384-
- normalize (suc m) (suc c-1) ≡ normalize n (suc d-1)
385-
mkℚᵘ -[1+ m ] c-1 ≃ᵘ mkℚᵘ (+ n) d-1
386-
/-injective-≃-helper {m} {n} {c-1} {d-1} eq
387-
with normalize-pos (suc m) (suc c-1) | normalize-nonNeg n (suc d-1)
388-
... | norm[m,c]-pos | norm[n,d]-nonNeg =
389-
contradiction (sym eq) (nonNeg≢neg _ _ {{norm[n,d]-nonNeg}} {{neg-pos norm[m,c]-pos}})
383+
/-injective-≃-helper : {m n c d} .{{_ : ℕ.NonZero c}} .{{_ : ℕ.NonZero d}}
384+
- normalize (suc m) c ≡ normalize n d
385+
mkℚᵘ -[1+ m ] (ℕ.pred c) ≃ᵘ mkℚᵘ (+ n) (ℕ.pred d)
386+
/-injective-≃-helper {m} {n} {c} {d} -norm≡norm = contradiction
387+
(sym -norm≡norm)
388+
(nonNeg≢neg (normalize n d) (- normalize (suc m) c))
389+
where instance
390+
_ : NonNegative (normalize n d)
391+
_ = normalize-nonNeg n d
392+
393+
_ : Negative (- normalize (suc m) c)
394+
_ = neg-pos {normalize (suc m) c} (normalize-pos (suc m) c)
390395

391396
/-injective-≃ : p q ↥ᵘ p / ↧ₙᵘ p ≡ ↥ᵘ q / ↧ₙᵘ q p ≃ᵘ q
392397
/-injective-≃ (mkℚᵘ (+ m) c-1) (mkℚᵘ (+ n) d-1) eq =
@@ -402,11 +407,17 @@ private
402407
-- Properties of toℚ/fromℚ
403408
------------------------------------------------------------------------
404409

410+
↥ᵘ-toℚᵘ : p ↥ᵘ (toℚᵘ p) ≡ ↥ p
411+
↥ᵘ-toℚᵘ p@record{} = refl
412+
413+
↧ᵘ-toℚᵘ : p ↧ᵘ (toℚᵘ p) ≡ ↧ p
414+
↧ᵘ-toℚᵘ p@record{} = refl
415+
405416
toℚᵘ-injective : Injective _≡_ _≃ᵘ_ toℚᵘ
406-
toℚᵘ-injective (*≡* eq) = ≃⇒≡ eq
417+
toℚᵘ-injective {x@record{}} {y@record{}} (*≡* eq) = ≃⇒≡ eq
407418

408419
fromℚᵘ-injective : Injective _≃ᵘ_ _≡_ fromℚᵘ
409-
fromℚᵘ-injective {p} {q} = /-injective-≃ p q
420+
fromℚᵘ-injective {p@record{}} {q@record{}} = /-injective-≃ p q
410421

411422
fromℚᵘ-toℚᵘ : p fromℚᵘ (toℚᵘ p) ≡ p
412423
fromℚᵘ-toℚᵘ (mkℚ (+ n) d-1 c) = normalize-coprime c
@@ -448,10 +459,10 @@ drop-*≤* (*≤* pq≤qp) = pq≤qp
448459
-- toℚᵘ is a isomorphism
449460

450461
toℚᵘ-mono-≤ : p ≤ q toℚᵘ p ≤ᵘ toℚᵘ q
451-
toℚᵘ-mono-≤ (*≤* p≤q) = *≤* p≤q
462+
toℚᵘ-mono-≤ {p@record{}} {q@record{}} (*≤* p≤q) = *≤* p≤q
452463

453464
toℚᵘ-cancel-≤ : toℚᵘ p ≤ᵘ toℚᵘ q p ≤ q
454-
toℚᵘ-cancel-≤ (*≤* p≤q) = *≤* p≤q
465+
toℚᵘ-cancel-≤ {p@record{}} {q@record{}} (*≤* p≤q) = *≤* p≤q
455466

456467
toℚᵘ-isOrderHomomorphism-≤ : IsOrderHomomorphism _≡_ _≃ᵘ_ _≤_ _≤ᵘ_ toℚᵘ
457468
toℚᵘ-isOrderHomomorphism-≤ = record
@@ -556,10 +567,10 @@ drop-*<* (*<* pq<qp) = pq<qp
556567
-- toℚᵘ is a isomorphism
557568

558569
toℚᵘ-mono-< : p < q toℚᵘ p <ᵘ toℚᵘ q
559-
toℚᵘ-mono-< (*<* p<q) = *<* p<q
570+
toℚᵘ-mono-< {p@record{}} {q@record{}} (*<* p<q) = *<* p<q
560571

561572
toℚᵘ-cancel-< : toℚᵘ p <ᵘ toℚᵘ q p < q
562-
toℚᵘ-cancel-< (*<* p<q) = *<* p<q
573+
toℚᵘ-cancel-< {p@record{}} {q@record{}} (*<* p<q) = *<* p<q
563574

564575
toℚᵘ-isOrderHomomorphism-< : IsOrderHomomorphism _≡_ _≃ᵘ_ _<_ _<ᵘ_ toℚᵘ
565576
toℚᵘ-isOrderHomomorphism-< = record
@@ -763,10 +774,10 @@ private
763774
+-nf p q = gcd (↥+ᵘ p q) (↧+ᵘ p q)
764775

765776
↥-+ : p q ↥ (p + q) ℤ.* +-nf p q ≡ ↥+ᵘ p q
766-
↥-+ p q = ↥-/ (↥+ᵘ p q) (↧ₙ p ℕ.* ↧ₙ q)
777+
↥-+ p@record{} q@record{} = ↥-/ (↥+ᵘ p q) (↧ₙ p ℕ.* ↧ₙ q)
767778

768779
↧-+ : p q ↧ (p + q) ℤ.* +-nf p q ≡ ↧+ᵘ p q
769-
↧-+ p q = ↧-/ (↥+ᵘ p q) (↧ₙ p ℕ.* ↧ₙ q)
780+
↧-+ p@record{} q@record{} = ↧-/ (↥+ᵘ p q) (↧ₙ p ℕ.* ↧ₙ q)
770781

771782
------------------------------------------------------------------------
772783
-- Raw bundles
@@ -825,12 +836,14 @@ private
825836
open Definitions ℚ ℚᵘ ℚᵘ._≃_
826837

827838
toℚᵘ-homo-+ : Homomorphic₂ toℚᵘ _+_ ℚᵘ._+_
828-
toℚᵘ-homo-+ p q with +-nf p q ℤ.≟ 0ℤ
829-
... | yes nf[p,q]≡0 = *≡* (begin
830-
↥ (p + q) ℤ.* ↧+ᵘ p q ≡⟨ cong (ℤ._* ↧+ᵘ p q) eq ⟩
831-
0ℤ ℤ.* ↧+ᵘ p q ≡⟨⟩
832-
0ℤ ℤ.* ↧ (p + q) ≡⟨ cong (ℤ._* ↧ (p + q)) (sym eq2) ⟩
833-
↥+ᵘ p q ℤ.* ↧ (p + q) ∎)
839+
toℚᵘ-homo-+ p@record{} q@record{} with +-nf p q ℤ.≟ 0ℤ
840+
... | yes nf[p,q]≡0 = *≡* $ begin
841+
↥ᵘ (toℚᵘ (p + q)) ℤ.* ↧+ᵘ p q ≡⟨ cong (ℤ._* ↧+ᵘ p q) (↥ᵘ-toℚᵘ (p + q)) ⟩
842+
↥ (p + q) ℤ.* ↧+ᵘ p q ≡⟨ cong (ℤ._* ↧+ᵘ p q) eq ⟩
843+
0ℤ ℤ.* ↧+ᵘ p q ≡⟨⟩
844+
0ℤ ℤ.* ↧ (p + q) ≡⟨ cong (ℤ._* ↧ (p + q)) (sym eq2) ⟩
845+
↥+ᵘ p q ℤ.* ↧ (p + q) ≡⟨ cong (↥+ᵘ p q ℤ.*_) (sym (↧ᵘ-toℚᵘ (p + q))) ⟩
846+
↥+ᵘ p q ℤ.* ↧ᵘ (toℚᵘ (p + q)) ∎
834847
where
835848
open ≡-Reasoning
836849
eq2 : ↥+ᵘ p q ≡ 0ℤ
@@ -839,12 +852,14 @@ toℚᵘ-homo-+ p q with +-nf p q ℤ.≟ 0ℤ
839852
eq : ↥ (p + q) ≡ 0ℤ
840853
eq rewrite eq2 = cong ↥_ (0/n≡0 (↧ₙ p ℕ.* ↧ₙ q))
841854

842-
... | no nf[p,q]≢0 = *≡* (ℤ.*-cancelʳ-≡ _ _ (+-nf p q) {{ℤ.≢-nonZero nf[p,q]≢0}} (begin
843-
↥ (p + q) ℤ.* ↧+ᵘ p q ℤ.* +-nf p q ≡⟨ xy∙z≈xz∙y (↥ (p + q)) _ _ ⟩
844-
↥ (p + q) ℤ.* +-nf p q ℤ.* ↧+ᵘ p q ≡⟨ cong (ℤ._* ↧+ᵘ p q) (↥-+ p q) ⟩
845-
↥+ᵘ p q ℤ.* ↧+ᵘ p q ≡⟨ cong (↥+ᵘ p q ℤ.*_) (sym (↧-+ p q)) ⟩
846-
↥+ᵘ p q ℤ.* (↧ (p + q) ℤ.* +-nf p q) ≡⟨ x∙yz≈xy∙z (↥+ᵘ p q) _ _ ⟩
847-
↥+ᵘ p q ℤ.* ↧ (p + q) ℤ.* +-nf p q ∎))
855+
... | no nf[p,q]≢0 = *≡* $ ℤ.*-cancelʳ-≡ _ _ (+-nf p q) {{ℤ.≢-nonZero nf[p,q]≢0}} $ begin
856+
(↥ᵘ (toℚᵘ (p + q))) ℤ.* ↧+ᵘ p q ℤ.* +-nf p q ≡⟨ cong (λ v v ℤ.* ↧+ᵘ p q ℤ.* +-nf p q) (↥ᵘ-toℚᵘ (p + q)) ⟩
857+
↥ (p + q) ℤ.* ↧+ᵘ p q ℤ.* +-nf p q ≡⟨ xy∙z≈xz∙y (↥ (p + q)) _ _ ⟩
858+
↥ (p + q) ℤ.* +-nf p q ℤ.* ↧+ᵘ p q ≡⟨ cong (ℤ._* ↧+ᵘ p q) (↥-+ p q) ⟩
859+
↥+ᵘ p q ℤ.* ↧+ᵘ p q ≡⟨ cong (↥+ᵘ p q ℤ.*_) (sym (↧-+ p q)) ⟩
860+
↥+ᵘ p q ℤ.* (↧ (p + q) ℤ.* +-nf p q) ≡⟨ x∙yz≈xy∙z (↥+ᵘ p q) _ _ ⟩
861+
↥+ᵘ p q ℤ.* ↧ (p + q) ℤ.* +-nf p q ≡˘⟨ cong (λ v ↥+ᵘ p q ℤ.* v ℤ.* +-nf p q) (↧ᵘ-toℚᵘ (p + q)) ⟩
862+
↥+ᵘ p q ℤ.* ↧ᵘ (toℚᵘ (p + q)) ℤ.* +-nf p q ∎
848863
where open ≡-Reasoning; open CommSemigroupProperties ℤ.*-commutativeSemigroup
849864

850865
toℚᵘ-isMagmaHomomorphism-+ : IsMagmaHomomorphism +-rawMagma ℚᵘ.+-rawMagma toℚᵘ
@@ -1024,10 +1039,10 @@ private
10241039
*-nf p q = gcd (↥ p ℤ.* ↥ q) (↧ p ℤ.* ↧ q)
10251040

10261041
↥-* : p q ↥ (p * q) ℤ.* *-nf p q ≡ ↥ p ℤ.* ↥ q
1027-
↥-* p q = ↥-/ (↥ p ℤ.* ↥ q) (↧ₙ p ℕ.* ↧ₙ q)
1042+
↥-* p@record{} q@record{} = ↥-/ (↥ p ℤ.* ↥ q) (↧ₙ p ℕ.* ↧ₙ q)
10281043

10291044
↧-* : p q ↧ (p * q) ℤ.* *-nf p q ≡ ↧ p ℤ.* ↧ q
1030-
↧-* p q = ↧-/ (↥ p ℤ.* ↥ q) (↧ₙ p ℕ.* ↧ₙ q)
1045+
↧-* p@record{} q@record{} = ↧-/ (↥ p ℤ.* ↥ q) (↧ₙ p ℕ.* ↧ₙ q)
10311046

10321047
------------------------------------------------------------------------
10331048
-- Raw bundles
@@ -1049,25 +1064,29 @@ private
10491064
-- Monomorphic to unnormalised _*_
10501065

10511066
toℚᵘ-homo-* : Homomorphic₂ toℚᵘ _*_ ℚᵘ._*_
1052-
toℚᵘ-homo-* p q with *-nf p q ℤ.≟ 0ℤ
1053-
... | yes nf[p,q]≡0 = *≡* (begin
1054-
↥ (p * q) ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong (ℤ._* (↧ p ℤ.* ↧ q)) eq ⟩
1055-
0ℤ ℤ.* (↧ p ℤ.* ↧ q) ≡⟨⟩
1056-
0ℤ ℤ.* ↧ (p * q) ≡⟨ cong (ℤ._* ↧ (p * q)) (sym eq2) ⟩
1057-
(↥ p ℤ.* ↥ q) ℤ.* ↧ (p * q) ∎)
1067+
toℚᵘ-homo-* p@record{} q@record{} with *-nf p q ℤ.≟ 0ℤ
1068+
... | yes nf[p,q]≡0 = *≡* $ begin
1069+
↥ᵘ (toℚᵘ (p * q)) ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong (ℤ._* (↧ p ℤ.* ↧ q)) (↥ᵘ-toℚᵘ (p * q)) ⟩
1070+
↥ (p * q) ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong (ℤ._* (↧ p ℤ.* ↧ q)) eq ⟩
1071+
0ℤ ℤ.* (↧ p ℤ.* ↧ q) ≡⟨⟩
1072+
0ℤ ℤ.* ↧ (p * q) ≡⟨ cong (ℤ._* ↧ (p * q)) (sym eq2) ⟩
1073+
(↥ p ℤ.* ↥ q) ℤ.* ↧ (p * q) ≡⟨ cong ((↥ p ℤ.* ↥ q) ℤ.*_) (sym (↧ᵘ-toℚᵘ (p * q))) ⟩
1074+
(↥ p ℤ.* ↥ q) ℤ.* ↧ᵘ (toℚᵘ (p * q)) ∎
10581075
where
10591076
open ≡-Reasoning
10601077
eq2 : ↥ p ℤ.* ↥ q ≡ 0ℤ
10611078
eq2 = gcd[i,j]≡0⇒i≡0 (↥ p ℤ.* ↥ q) (↧ p ℤ.* ↧ q) nf[p,q]≡0
10621079

10631080
eq : ↥ (p * q) ≡ 0ℤ
10641081
eq rewrite eq2 = cong ↥_ (0/n≡0 (↧ₙ p ℕ.* ↧ₙ q))
1065-
... | no nf[p,q]≢0 = *≡* (ℤ.*-cancelʳ-≡ _ _ (*-nf p q) {{ℤ.≢-nonZero nf[p,q]≢0}} (begin
1066-
↥ (p * q) ℤ.* (↧ p ℤ.* ↧ q) ℤ.* *-nf p q ≡⟨ xy∙z≈xz∙y (↥ (p * q)) _ _ ⟩
1067-
↥ (p * q) ℤ.* *-nf p q ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong (ℤ._* (↧ p ℤ.* ↧ q)) (↥-* p q) ⟩
1068-
(↥ p ℤ.* ↥ q) ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong ((↥ p ℤ.* ↥ q) ℤ.*_) (sym (↧-* p q)) ⟩
1069-
(↥ p ℤ.* ↥ q) ℤ.* (↧ (p * q) ℤ.* *-nf p q) ≡⟨ x∙yz≈xy∙z (↥ p ℤ.* ↥ q) _ _ ⟩
1070-
(↥ p ℤ.* ↥ q) ℤ.* ↧ (p * q) ℤ.* *-nf p q ∎))
1082+
... | no nf[p,q]≢0 = *≡* $ ℤ.*-cancelʳ-≡ _ _ (*-nf p q) {{ℤ.≢-nonZero nf[p,q]≢0}} $ begin
1083+
↥ᵘ (toℚᵘ (p * q)) ℤ.* (↧ p ℤ.* ↧ q) ℤ.* *-nf p q ≡⟨ cong (λ v v ℤ.* (↧ p ℤ.* ↧ q) ℤ.* *-nf p q) (↥ᵘ-toℚᵘ (p * q)) ⟩
1084+
↥ (p * q) ℤ.* (↧ p ℤ.* ↧ q) ℤ.* *-nf p q ≡⟨ xy∙z≈xz∙y (↥ (p * q)) _ _ ⟩
1085+
↥ (p * q) ℤ.* *-nf p q ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong (ℤ._* (↧ p ℤ.* ↧ q)) (↥-* p q) ⟩
1086+
(↥ p ℤ.* ↥ q) ℤ.* (↧ p ℤ.* ↧ q) ≡⟨ cong ((↥ p ℤ.* ↥ q) ℤ.*_) (sym (↧-* p q)) ⟩
1087+
(↥ p ℤ.* ↥ q) ℤ.* (↧ (p * q) ℤ.* *-nf p q) ≡⟨ x∙yz≈xy∙z (↥ p ℤ.* ↥ q) _ _ ⟩
1088+
(↥ p ℤ.* ↥ q) ℤ.* ↧ (p * q) ℤ.* *-nf p q ≡˘⟨ cong (λ v (↥ p ℤ.* ↥ q) ℤ.* v ℤ.* *-nf p q) (↧ᵘ-toℚᵘ (p * q)) ⟩
1089+
(↥ p ℤ.* ↥ q) ℤ.* ↧ᵘ (toℚᵘ (p * q)) ℤ.* *-nf p q ∎
10711090
where open ≡-Reasoning; open CommSemigroupProperties ℤ.*-commutativeSemigroup
10721091

10731092
toℚᵘ-homo-1/ : p .{{_ : NonZero p}} toℚᵘ (1/ p) ℚᵘ.≃ (ℚᵘ.1/ toℚᵘ p)
@@ -1337,22 +1356,22 @@ neg-distribʳ-* = +-*-Monomorphism.neg-distribʳ-* ℚᵘ.+-0-isGroup ℚᵘ.*-i
13371356
------------------------------------------------------------------------
13381357

13391358
p≤q⇒p⊔q≡q : p ≤ q p ⊔ q ≡ q
1340-
p≤q⇒p⊔q≡q {p} {q} p≤q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
1359+
p≤q⇒p⊔q≡q {p@record{}} {q@record{}} p≤q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
13411360
... | true | _ = refl
13421361
... | false | [ p≰q ] = contradiction (≤⇒≤ᵇ p≤q) (subst (¬_ ∘ T) (sym p≰q) λ())
13431362

13441363
p≥q⇒p⊔q≡p : p ≥ q p ⊔ q ≡ p
1345-
p≥q⇒p⊔q≡p {p} {q} p≥q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
1364+
p≥q⇒p⊔q≡p {p@record{}} {q@record{}} p≥q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
13461365
... | true | [ p≤q ] = ≤-antisym p≥q (≤ᵇ⇒≤ (subst T (sym p≤q) _))
13471366
... | false | [ p≤q ] = refl
13481367

13491368
p≤q⇒p⊓q≡p : p ≤ q p ⊓ q ≡ p
1350-
p≤q⇒p⊓q≡p {p} {q} p≤q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
1369+
p≤q⇒p⊓q≡p {p@record{}} {q@record{}} p≤q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
13511370
... | true | _ = refl
13521371
... | false | [ p≰q ] = contradiction (≤⇒≤ᵇ p≤q) (subst (¬_ ∘ T) (sym p≰q) λ())
13531372

13541373
p≥q⇒p⊓q≡q : p ≥ q p ⊓ q ≡ q
1355-
p≥q⇒p⊓q≡q {p} {q} p≥q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
1374+
p≥q⇒p⊓q≡q {p@record{}} {q@record{}} p≥q with p ≤ᵇ q | inspect (p ≤ᵇ_) q
13561375
... | true | [ p≤q ] = ≤-antisym (≤ᵇ⇒≤ (subst T (sym p≤q) _)) p≥q
13571376
... | false | [ p≤q ] = refl
13581377

@@ -1603,15 +1622,15 @@ toℚᵘ-homo-∣-∣ (mkℚ -[1+ _ ] _ _) = *≡* refl
16031622
∣p∣≡0⇒p≡0 (mkℚ +0 zero _) ∣p∣≡0 = refl
16041623

16051624
0≤∣p∣ : p 0ℚ ≤ ∣ p ∣
1606-
0≤∣p∣ p = *≤* (begin
1625+
0≤∣p∣ p@record{} = *≤* (begin
16071626
(↥ 0ℚ) ℤ.* (↧ ∣ p ∣) ≡⟨ ℤ.*-zeroˡ (↧ ∣ p ∣) ⟩
16081627
0ℤ ≤⟨ ℤ.+≤+ ℕ.z≤n ⟩
16091628
↥ ∣ p ∣ ≡˘⟨ ℤ.*-identityʳ (↥ ∣ p ∣) ⟩
16101629
↥ ∣ p ∣ ℤ.* 1ℤ ∎)
16111630
where open ℤ.≤-Reasoning
16121631

16131632
0≤p⇒∣p∣≡p : 0ℚ ≤ p ∣ p ∣ ≡ p
1614-
0≤p⇒∣p∣≡p {p} 0≤p = toℚᵘ-injective (ℚᵘ.0≤p⇒∣p∣≃p (toℚᵘ-mono-≤ 0≤p))
1633+
0≤p⇒∣p∣≡p {p@record{}} 0≤p = toℚᵘ-injective (ℚᵘ.0≤p⇒∣p∣≃p (toℚᵘ-mono-≤ 0≤p))
16151634

16161635
∣-p∣≡∣p∣ : p ∣ - p ∣ ≡ ∣ p ∣
16171636
∣-p∣≡∣p∣ (mkℚ +[1+ n ] d-1 _) = refl
@@ -1640,7 +1659,7 @@ toℚᵘ-homo-∣-∣ (mkℚ -[1+ _ ] _ _) = *≡* refl
16401659
where open ℚᵘ.≤-Reasoning
16411660

16421661
∣p-q∣≤∣p∣+∣q∣ : p q ∣ p - q ∣ ≤ ∣ p ∣ + ∣ q ∣
1643-
∣p-q∣≤∣p∣+∣q∣ p q = begin
1662+
∣p-q∣≤∣p∣+∣q∣ p@record{} q@record{} = begin
16441663
∣ p - q ∣ ≤⟨ ∣p+q∣≤∣p∣+∣q∣ p (- q) ⟩
16451664
∣ p ∣ + ∣ - q ∣ ≡⟨ cong (λ h ∣ p ∣ + h) (∣-p∣≡∣p∣ q) ⟩
16461665
∣ p ∣ + ∣ q ∣ ∎

0 commit comments

Comments
 (0)