| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Rational numbers in non-reduced form. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Rational.Unnormalised.Base where |
| 10 |
|
| 11 |
open import Algebra.Bundles.Raw |
| 12 |
using (RawMagma; RawMonoid; RawGroup; RawNearSemiring; RawSemiring; RawRing) |
| 13 |
open import Data.Bool.Base using (Bool; true; false; if_then_else_) |
| 14 |
open import Data.Integer.Base as ℤ |
| 15 |
using (ℤ; +_; +0; +[1+_]; -[1+_]; +<+; +≤+) |
| 16 |
hiding (module ℤ) |
| 17 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc) |
| 18 |
open import Level using (0ℓ) |
| 19 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 20 |
open import Relation.Unary using (Pred) |
| 21 |
open import Relation.Binary.Core using (Rel) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core |
| 23 |
using (_≡_; _≢_; refl) |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Definition |
| 27 |
|
| 28 |
-- Here we define rationals that are not necessarily in reduced form. |
| 29 |
-- Consequently there are multiple ways of representing a given rational |
| 30 |
-- number, and the performance of the arithmetic operations may suffer |
| 31 |
-- due to blowup of the numerator and denominator. |
| 32 |
|
| 33 |
-- Nonetheless they are much easier to reason about. In general proofs |
| 34 |
-- are first proved for these unnormalised rationals and then translated |
| 35 |
-- into the normalised rationals. |
| 36 |
|
| 37 |
record ℚᵘ : Set where |
| 38 |
-- We add "no-eta-equality; pattern" to the record to stop Agda |
| 39 |
-- automatically unfolding rationals when arithmetic operations are |
| 40 |
-- applied to them (see definition of operators below and Issue #1753 |
| 41 |
-- for details). |
| 42 |
no-eta-equality; pattern |
| 43 |
|
| 44 |
constructor mkℚᵘ |
| 45 |
field |
| 46 |
numerator : ℤ |
| 47 |
denominator-1 : ℕ |
| 48 |
|
| 49 |
denominatorℕ : ℕ |
| 50 |
denominatorℕ = suc denominator-1 |
| 51 |
|
| 52 |
denominator : ℤ |
| 53 |
denominator = + denominatorℕ |
| 54 |
|
| 55 |
open ℚᵘ public using () |
| 56 |
renaming |
| 57 |
( numerator to ↥_ |
| 58 |
; denominator to ↧_ |
| 59 |
; denominatorℕ to ↧ₙ_ |
| 60 |
) |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Equality of rational numbers (does not coincide with _≡_) |
| 64 |
|
| 65 |
infix 4 _≃_ _≠_ |
| 66 |
|
| 67 |
data _≃_ : Rel ℚᵘ 0ℓ where |
| 68 |
*≡* : ∀ {p q} → (↥ p ℤ.* ↧ q) ≡ (↥ q ℤ.* ↧ p) → p ≃ q |
| 69 |
|
| 70 |
_≄_ : Rel ℚᵘ 0ℓ |
| 71 |
p ≄ q = ¬ (p ≃ q) |
| 72 |
|
| 73 |
------------------------------------------------------------------------ |
| 74 |
-- Ordering of rationals |
| 75 |
|
| 76 |
infix 4 _≤_ _<_ _≥_ _>_ _≰_ _≱_ _≮_ _≯_ |
| 77 |
|
| 78 |
data _≤_ : Rel ℚᵘ 0ℓ where |
| 79 |
*≤* : ∀ {p q} → (↥ p ℤ.* ↧ q) ℤ.≤ (↥ q ℤ.* ↧ p) → p ≤ q |
| 80 |
|
| 81 |
data _<_ : Rel ℚᵘ 0ℓ where |
| 82 |
*<* : ∀ {p q} → (↥ p ℤ.* ↧ q) ℤ.< (↥ q ℤ.* ↧ p) → p < q |
| 83 |
|
| 84 |
_≥_ : Rel ℚᵘ 0ℓ |
| 85 |
x ≥ y = y ≤ x |
| 86 |
|
| 87 |
_>_ : Rel ℚᵘ 0ℓ |
| 88 |
x > y = y < x |
| 89 |
|
| 90 |
_≰_ : Rel ℚᵘ 0ℓ |
| 91 |
x ≰ y = ¬ (x ≤ y) |
| 92 |
|
| 93 |
_≱_ : Rel ℚᵘ 0ℓ |
| 94 |
x ≱ y = ¬ (x ≥ y) |
| 95 |
|
| 96 |
_≮_ : Rel ℚᵘ 0ℓ |
| 97 |
x ≮ y = ¬ (x < y) |
| 98 |
|
| 99 |
_≯_ : Rel ℚᵘ 0ℓ |
| 100 |
x ≯ y = ¬ (x > y) |
| 101 |
|
| 102 |
------------------------------------------------------------------------ |
| 103 |
-- Boolean ordering |
| 104 |
|
| 105 |
infix 4 _≤ᵇ_ |
| 106 |
|
| 107 |
_≤ᵇ_ : ℚᵘ → ℚᵘ → Bool |
| 108 |
p ≤ᵇ q = (↥ p ℤ.* ↧ q) ℤ.≤ᵇ (↥ q ℤ.* ↧ p) |
| 109 |
|
| 110 |
------------------------------------------------------------------------ |
| 111 |
-- Constructing rationals |
| 112 |
|
| 113 |
-- An alternative constructor for ℚᵘ. See the constants section below |
| 114 |
-- for examples of how to use this operator. |
| 115 |
|
| 116 |
infixl 7 _/_ |
| 117 |
|
| 118 |
_/_ : (n : ℤ) (d : ℕ) .{{_ : ℕ.NonZero d}} → ℚᵘ |
| 119 |
n / suc d = mkℚᵘ n d |
| 120 |
|
| 121 |
------------------------------------------------------------------------ |
| 122 |
-- Some constants |
| 123 |
|
| 124 |
0ℚᵘ : ℚᵘ |
| 125 |
0ℚᵘ = + 0 / 1 |
| 126 |
|
| 127 |
1ℚᵘ : ℚᵘ |
| 128 |
1ℚᵘ = + 1 / 1 |
| 129 |
|
| 130 |
½ : ℚᵘ |
| 131 |
½ = + 1 / 2 |
| 132 |
|
| 133 |
-½ : ℚᵘ |
| 134 |
-½ = ℤ.- (+ 1) / 2 |
| 135 |
|
| 136 |
------------------------------------------------------------------------ |
| 137 |
-- Simple predicates |
| 138 |
|
| 139 |
NonZero : Pred ℚᵘ 0ℓ |
| 140 |
NonZero p = ℤ.NonZero (↥ p) |
| 141 |
|
| 142 |
Positive : Pred ℚᵘ 0ℓ |
| 143 |
Positive p = ℤ.Positive (↥ p) |
| 144 |
|
| 145 |
Negative : Pred ℚᵘ 0ℓ |
| 146 |
Negative p = ℤ.Negative (↥ p) |
| 147 |
|
| 148 |
NonPositive : Pred ℚᵘ 0ℓ |
| 149 |
NonPositive p = ℤ.NonPositive (↥ p) |
| 150 |
|
| 151 |
NonNegative : Pred ℚᵘ 0ℓ |
| 152 |
NonNegative p = ℤ.NonNegative (↥ p) |
| 153 |
|
| 154 |
-- Instances |
| 155 |
|
| 156 |
open ℤ public |
| 157 |
using (nonZero; pos; nonNeg; nonPos0; nonPos; neg) |
| 158 |
|
| 159 |
-- Constructors and destructors |
| 160 |
|
| 161 |
-- Note: these could be proved more elegantly using the constructors |
| 162 |
-- from ℤ but it requires importing `Data.Integer.Properties` which |
| 163 |
-- we would like to avoid doing. |
| 164 |
|
| 165 |
≢-nonZero : ∀ {p} → p ≄ 0ℚᵘ → NonZero p |
| 166 |
≢-nonZero {mkℚᵘ -[1+ _ ] _ } _ = _ |
| 167 |
≢-nonZero {mkℚᵘ +[1+ _ ] _ } _ = _ |
| 168 |
≢-nonZero {mkℚᵘ +0 zero } p≢0 = contradiction (*≡* refl) p≢0 |
| 169 |
≢-nonZero {mkℚᵘ +0 (suc d)} p≢0 = contradiction (*≡* refl) p≢0 |
| 170 |
|
| 171 |
>-nonZero : ∀ {p} → p > 0ℚᵘ → NonZero p |
| 172 |
>-nonZero {mkℚᵘ +0 _} (*<* (+<+ ())) |
| 173 |
>-nonZero {mkℚᵘ +[1+ n ] _} (*<* _) = _ |
| 174 |
|
| 175 |
<-nonZero : ∀ {p} → p < 0ℚᵘ → NonZero p |
| 176 |
<-nonZero {mkℚᵘ +[1+ n ] _} (*<* _) = _ |
| 177 |
<-nonZero {mkℚᵘ +0 _} (*<* (+<+ ())) |
| 178 |
<-nonZero {mkℚᵘ -[1+ n ] _} (*<* _) = _ |
| 179 |
|
| 180 |
positive : ∀ {p} → p > 0ℚᵘ → Positive p |
| 181 |
positive {mkℚᵘ +[1+ n ] _} (*<* _) = _ |
| 182 |
positive {mkℚᵘ +0 _} (*<* (+<+ ())) |
| 183 |
positive {mkℚᵘ (-[1+_] n) _} (*<* ()) |
| 184 |
|
| 185 |
negative : ∀ {p} → p < 0ℚᵘ → Negative p |
| 186 |
negative {mkℚᵘ +[1+ n ] _} (*<* (+<+ ())) |
| 187 |
negative {mkℚᵘ +0 _} (*<* (+<+ ())) |
| 188 |
negative {mkℚᵘ (-[1+_] n) _} (*<* _ ) = _ |
| 189 |
|
| 190 |
nonPositive : ∀ {p} → p ≤ 0ℚᵘ → NonPositive p |
| 191 |
nonPositive {mkℚᵘ +[1+ n ] _} (*≤* (+≤+ ())) |
| 192 |
nonPositive {mkℚᵘ +0 _} (*≤* _) = _ |
| 193 |
nonPositive {mkℚᵘ -[1+ n ] _} (*≤* _) = _ |
| 194 |
|
| 195 |
nonNegative : ∀ {p} → p ≥ 0ℚᵘ → NonNegative p |
| 196 |
nonNegative {mkℚᵘ +0 _} (*≤* _) = _ |
| 197 |
nonNegative {mkℚᵘ +[1+ n ] _} (*≤* _) = _ |
| 198 |
|
| 199 |
------------------------------------------------------------------------ |
| 200 |
-- Operations on rationals |
| 201 |
|
| 202 |
-- Explanation for `@record{}` everywhere: combined with no-eta-equality |
| 203 |
-- on the record definition of ℚᵘ above, these annotations prevent the |
| 204 |
-- operations from automatically expanding unless their arguments are |
| 205 |
-- explicitly pattern matched on. |
| 206 |
-- |
| 207 |
-- For example prior to their addition, `p + q` would often be |
| 208 |
-- normalised by Agda to `(↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)`. |
| 209 |
-- While in this small example this isn't a big problem, it leads to an |
| 210 |
-- exponential blowup when you have large arithmetic expressions which |
| 211 |
-- would often choke both type-checking and the display code. For |
| 212 |
-- example, the normalised form of `p + q + r + s + t + u` would be |
| 213 |
-- ~300 lines long. |
| 214 |
-- |
| 215 |
-- This is fundementally a problem with Agda, so if over-eager |
| 216 |
-- normalisation is ever fixed in Agda (e.g. with glued representation |
| 217 |
-- of terms) these annotations can be removed. |
| 218 |
|
| 219 |
infix 8 -_ 1/_ |
| 220 |
infixl 7 _*_ _÷_ _⊓_ |
| 221 |
infixl 6 _-_ _+_ _⊔_ |
| 222 |
|
| 223 |
-- negation |
| 224 |
|
| 225 |
-_ : ℚᵘ → ℚᵘ |
| 226 |
- mkℚᵘ n d = mkℚᵘ (ℤ.- n) d |
| 227 |
|
| 228 |
-- addition |
| 229 |
|
| 230 |
_+_ : ℚᵘ → ℚᵘ → ℚᵘ |
| 231 |
p@record{} + q@record{} = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q) |
| 232 |
|
| 233 |
-- multiplication |
| 234 |
|
| 235 |
_*_ : ℚᵘ → ℚᵘ → ℚᵘ |
| 236 |
p@record{} * q@record{} = (↥ p ℤ.* ↥ q) / (↧ₙ p ℕ.* ↧ₙ q) |
| 237 |
|
| 238 |
-- subtraction |
| 239 |
|
| 240 |
_-_ : ℚᵘ → ℚᵘ → ℚᵘ |
| 241 |
p - q = p + (- q) |
| 242 |
|
| 243 |
-- reciprocal: requires a proof that the numerator is not zero |
| 244 |
|
| 245 |
1/_ : (p : ℚᵘ) → .{{_ : NonZero p}} → ℚᵘ |
| 246 |
1/ mkℚᵘ +[1+ n ] d = mkℚᵘ +[1+ d ] n |
| 247 |
1/ mkℚᵘ -[1+ n ] d = mkℚᵘ -[1+ d ] n |
| 248 |
|
| 249 |
-- division: requires a proof that the denominator is not zero |
| 250 |
|
| 251 |
_÷_ : (p q : ℚᵘ) → .{{_ : NonZero q}} → ℚᵘ |
| 252 |
p@record{} ÷ q@record{} = p * (1/ q) |
| 253 |
|
| 254 |
-- max |
| 255 |
_⊔_ : (p q : ℚᵘ) → ℚᵘ |
| 256 |
p@record{} ⊔ q@record{} = if p ≤ᵇ q then q else p |
| 257 |
|
| 258 |
-- min |
| 259 |
_⊓_ : (p q : ℚᵘ) → ℚᵘ |
| 260 |
p@record{} ⊓ q@record{} = if p ≤ᵇ q then p else q |
| 261 |
|
| 262 |
-- absolute value |
| 263 |
∣_∣ : ℚᵘ → ℚᵘ |
| 264 |
∣ mkℚᵘ p q ∣ = mkℚᵘ (+ ℤ.∣ p ∣) q |
| 265 |
|
| 266 |
------------------------------------------------------------------------ |
| 267 |
-- Rounding functions |
| 268 |
|
| 269 |
-- Floor (round towards -∞) |
| 270 |
floor : ℚᵘ → ℤ |
| 271 |
floor p@record{} = ↥ p ℤ./ ↧ p |
| 272 |
|
| 273 |
-- Ceiling (round towards +∞) |
| 274 |
ceiling : ℚᵘ → ℤ |
| 275 |
ceiling p@record{} = ℤ.- floor (- p) |
| 276 |
|
| 277 |
-- Truncate (round towards 0) |
| 278 |
truncate : ℚᵘ → ℤ |
| 279 |
truncate p = if p ≤ᵇ 0ℚᵘ then ceiling p else floor p |
| 280 |
|
| 281 |
-- Round (to nearest integer) |
| 282 |
round : ℚᵘ → ℤ |
| 283 |
round p = if p ≤ᵇ 0ℚᵘ then ceiling (p - ½) else floor (p + ½) |
| 284 |
|
| 285 |
-- Fractional part (remainder after floor) |
| 286 |
fracPart : ℚᵘ → ℚᵘ |
| 287 |
fracPart p@record{} = ∣ p - truncate p / 1 ∣ |
| 288 |
|
| 289 |
-- Extra notations ⌊ ⌋ floor, ⌈ ⌉ ceiling, [ ] truncate |
| 290 |
syntax floor p = ⌊ p ⌋ |
| 291 |
syntax ceiling p = ⌈ p ⌉ |
| 292 |
syntax truncate p = [ p ] |
| 293 |
|
| 294 |
------------------------------------------------------------------------ |
| 295 |
-- Raw bundles for _+_ |
| 296 |
|
| 297 |
+-rawMagma : RawMagma 0ℓ 0ℓ |
| 298 |
+-rawMagma = record |
| 299 |
{ _≈_ = _≃_ |
| 300 |
; _∙_ = _+_ |
| 301 |
} |
| 302 |
|
| 303 |
+-0-rawMonoid : RawMonoid 0ℓ 0ℓ |
| 304 |
+-0-rawMonoid = record |
| 305 |
{ _≈_ = _≃_ |
| 306 |
; _∙_ = _+_ |
| 307 |
; ε = 0ℚᵘ |
| 308 |
} |
| 309 |
|
| 310 |
+-0-rawGroup : RawGroup 0ℓ 0ℓ |
| 311 |
+-0-rawGroup = record |
| 312 |
{ Carrier = ℚᵘ |
| 313 |
; _≈_ = _≃_ |
| 314 |
; _∙_ = _+_ |
| 315 |
; ε = 0ℚᵘ |
| 316 |
; _⁻¹ = -_ |
| 317 |
} |
| 318 |
|
| 319 |
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ |
| 320 |
+-*-rawNearSemiring = record |
| 321 |
{ Carrier = ℚᵘ |
| 322 |
; _≈_ = _≃_ |
| 323 |
; _+_ = _+_ |
| 324 |
; _*_ = _*_ |
| 325 |
; 0# = 0ℚᵘ |
| 326 |
} |
| 327 |
|
| 328 |
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ |
| 329 |
+-*-rawSemiring = record |
| 330 |
{ Carrier = ℚᵘ |
| 331 |
; _≈_ = _≃_ |
| 332 |
; _+_ = _+_ |
| 333 |
; _*_ = _*_ |
| 334 |
; 0# = 0ℚᵘ |
| 335 |
; 1# = 1ℚᵘ |
| 336 |
} |
| 337 |
|
| 338 |
+-*-rawRing : RawRing 0ℓ 0ℓ |
| 339 |
+-*-rawRing = record |
| 340 |
{ Carrier = ℚᵘ |
| 341 |
; _≈_ = _≃_ |
| 342 |
; _+_ = _+_ |
| 343 |
; _*_ = _*_ |
| 344 |
; -_ = -_ |
| 345 |
; 0# = 0ℚᵘ |
| 346 |
; 1# = 1ℚᵘ |
| 347 |
} |
| 348 |
|
| 349 |
------------------------------------------------------------------------ |
| 350 |
-- Raw bundles for _*_ |
| 351 |
|
| 352 |
*-rawMagma : RawMagma 0ℓ 0ℓ |
| 353 |
*-rawMagma = record |
| 354 |
{ _≈_ = _≃_ |
| 355 |
; _∙_ = _*_ |
| 356 |
} |
| 357 |
|
| 358 |
*-1-rawMonoid : RawMonoid 0ℓ 0ℓ |
| 359 |
*-1-rawMonoid = record |
| 360 |
{ _≈_ = _≃_ |
| 361 |
; _∙_ = _*_ |
| 362 |
; ε = 1ℚᵘ |
| 363 |
} |
| 364 |
|
| 365 |
------------------------------------------------------------------------ |
| 366 |
-- DEPRECATED NAMES |
| 367 |
------------------------------------------------------------------------ |
| 368 |
-- Please use the new names as continuing support for the old names is |
| 369 |
-- not guaranteed. |
| 370 |
|
| 371 |
-- Version 2.0 |
| 372 |
|
| 373 |
+-rawMonoid = +-0-rawMonoid |
| 374 |
{-# WARNING_ON_USAGE +-rawMonoid |
| 375 |
"Warning: +-rawMonoid was deprecated in v2.0 |
| 376 |
Please use +-0-rawMonoid instead." |
| 377 |
#-} |
| 378 |
*-rawMonoid = *-1-rawMonoid |
| 379 |
{-# WARNING_ON_USAGE *-rawMonoid |
| 380 |
"Warning: *-rawMonoid was deprecated in v2.0 |
| 381 |
Please use *-1-rawMonoid instead." |
| 382 |
#-} |
| 383 |
_≠_ = _≄_ |
| 384 |
{-# WARNING_ON_USAGE _≠_ |
| 385 |
"Warning: _≠_ was deprecated in v2.0 |
| 386 |
Please use _≄_ instead." |
| 387 |
#-} |