{-# OPTIONS --without-K --safe #-}
module Data.Rational.Base where
open import Data.Bool.Base using (Bool; true; false; if_then_else_)
open import Data.Integer.Base as ℤ using (ℤ; +_; +0; +[1+_]; -[1+_])
open import Data.Nat.GCD
open import Data.Nat.Coprimality as C
using (Coprime; Bézout-coprime; coprime-/gcd; coprime?; ¬0-coprimeTo-2+)
open import Data.Nat.Base as ℕ using (ℕ; zero; suc) hiding (module ℕ)
open import Data.Rational.Unnormalised.Base as ℚᵘ using (ℚᵘ; mkℚᵘ)
open import Data.Sum.Base using (inj₂)
open import Function.Base using (id)
open import Level using (0ℓ)
open import Relation.Nullary using (¬_; recompute)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Unary using (Pred)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; _≢_; refl)
record ℚ : Set where
no-eta-equality; pattern
constructor mkℚ
field
numerator : ℤ
denominator-1 : ℕ
.isCoprime : Coprime ℤ.∣ numerator ∣ (suc denominator-1)
denominatorℕ : ℕ
denominatorℕ = suc denominator-1
denominator : ℤ
denominator = + denominatorℕ
open ℚ public using ()
renaming
( numerator to ↥_
; denominator to ↧_
; denominatorℕ to ↧ₙ_
)
mkℚ+ : ∀ n d → .{{_ : ℕ.NonZero d}} → .(Coprime n d) → ℚ
mkℚ+ n (suc d) coprime = mkℚ (+ n) d coprime
infix 4 _≃_
_≃_ : Rel ℚ 0ℓ
p ≃ q = (↥ p ℤ.* ↧ q) ≡ (↥ q ℤ.* ↧ p)
infix 4 _≤_ _<_ _≥_ _>_ _≰_ _≱_ _≮_ _≯_
data _≤_ : Rel ℚ 0ℓ where
*≤* : ∀ {p q} → (↥ p ℤ.* ↧ q) ℤ.≤ (↥ q ℤ.* ↧ p) → p ≤ q
data _<_ : Rel ℚ 0ℓ where
*<* : ∀ {p q} → (↥ p ℤ.* ↧ q) ℤ.< (↥ q ℤ.* ↧ p) → p < q
_≥_ : Rel ℚ 0ℓ
x ≥ y = y ≤ x
_>_ : Rel ℚ 0ℓ
x > y = y < x
_≰_ : Rel ℚ 0ℓ
x ≰ y = ¬ (x ≤ y)
_≱_ : Rel ℚ 0ℓ
x ≱ y = ¬ (x ≥ y)
_≮_ : Rel ℚ 0ℓ
x ≮ y = ¬ (x < y)
_≯_ : Rel ℚ 0ℓ
x ≯ y = ¬ (x > y)
infix 4 _≤ᵇ_
_≤ᵇ_ : ℚ → ℚ → Bool
p ≤ᵇ q = (↥ p ℤ.* ↧ q) ℤ.≤ᵇ (↥ q ℤ.* ↧ p)
-_ : ℚ → ℚ
- mkℚ -[1+ n ] d prf = mkℚ +[1+ n ] d prf
- mkℚ +0 d prf = mkℚ +0 d prf
- mkℚ +[1+ n ] d prf = mkℚ -[1+ n ] d prf
normalize : ∀ (m n : ℕ) .{{_ : ℕ.NonZero n}} → ℚ
normalize m n = mkℚ+ (m ℕ./ gcd m n) (n ℕ./ gcd m n) (coprime-/gcd m n)
where
instance
g≢0 = ℕ.≢-nonZero (gcd[m,n]≢0 m n (inj₂ (ℕ.≢-nonZero⁻¹ n)))
n/g≢0 = ℕ.≢-nonZero (n/gcd[m,n]≢0 m n {{gcd≢0 = g≢0}})
infixl 7 _/_
_/_ : (n : ℤ) (d : ℕ) → .{{_ : ℕ.NonZero d}} → ℚ
(+ n / d) = normalize n d
(-[1+ n ] / d) = - normalize (suc n) d
toℚᵘ : ℚ → ℚᵘ
toℚᵘ (mkℚ n d-1 _) = mkℚᵘ n d-1
fromℚᵘ : ℚᵘ → ℚ
fromℚᵘ (mkℚᵘ n d-1) = n / suc d-1
0ℚ : ℚ
0ℚ = + 0 / 1
1ℚ : ℚ
1ℚ = + 1 / 1
½ : ℚ
½ = + 1 / 2
-½ : ℚ
-½ = - ½
NonZero : Pred ℚ 0ℓ
NonZero p = ℚᵘ.NonZero (toℚᵘ p)
Positive : Pred ℚ 0ℓ
Positive p = ℚᵘ.Positive (toℚᵘ p)
Negative : Pred ℚ 0ℓ
Negative p = ℚᵘ.Negative (toℚᵘ p)
NonPositive : Pred ℚ 0ℓ
NonPositive p = ℚᵘ.NonPositive (toℚᵘ p)
NonNegative : Pred ℚ 0ℓ
NonNegative p = ℚᵘ.NonNegative (toℚᵘ p)
≢-nonZero : ∀ {p} → p ≢ 0ℚ → NonZero p
≢-nonZero {mkℚ -[1+ _ ] _ _} _ = _
≢-nonZero {mkℚ +[1+ _ ] _ _} _ = _
≢-nonZero {mkℚ +0 zero _} p≢0 = contradiction refl p≢0
≢-nonZero {mkℚ +0 (suc d) c} p≢0 = contradiction (λ {i} → C.recompute c {i}) ¬0-coprimeTo-2+
>-nonZero : ∀ {p} → p > 0ℚ → NonZero p
>-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.>-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
<-nonZero : ∀ {p} → p < 0ℚ → NonZero p
<-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.<-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
positive : ∀ {p} → p > 0ℚ → Positive p
positive {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.positive {toℚᵘ p} (ℚᵘ.*<* p<q)
negative : ∀ {p} → p < 0ℚ → Negative p
negative {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.negative {toℚᵘ p} (ℚᵘ.*<* p<q)
nonPositive : ∀ {p} → p ≤ 0ℚ → NonPositive p
nonPositive {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonPositive {toℚᵘ p} (ℚᵘ.*≤* p≤q)
nonNegative : ∀ {p} → p ≥ 0ℚ → NonNegative p
nonNegative {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonNegative {toℚᵘ p} (ℚᵘ.*≤* p≤q)
infix 8 -_ 1/_
infixl 7 _*_ _÷_ _⊓_
infixl 6 _-_ _+_ _⊔_
_+_ : ℚ → ℚ → ℚ
p@record{} + q@record{} = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
_*_ : ℚ → ℚ → ℚ
p@record{} * q@record{} = (↥ p ℤ.* ↥ q) / (↧ₙ p ℕ.* ↧ₙ q)
_-_ : ℚ → ℚ → ℚ
p@record{} - q@record{} = p + (- q)
1/_ : (p : ℚ) → .{{_ : NonZero p}} → ℚ
1/ mkℚ +[1+ n ] d prf = mkℚ +[1+ d ] n (C.sym prf)
1/ mkℚ -[1+ n ] d prf = mkℚ -[1+ d ] n (C.sym prf)
_÷_ : (p q : ℚ) → .{{_ : NonZero q}} → ℚ
p@record{} ÷ q@record{} = p * (1/ q)
_⊔_ : (p q : ℚ) → ℚ
p@record{} ⊔ q@record{} = if p ≤ᵇ q then q else p
_⊓_ : (p q : ℚ) → ℚ
p@record{} ⊓ q@record{} = if p ≤ᵇ q then p else q
∣_∣ : ℚ → ℚ
∣ mkℚ n d c ∣ = mkℚ (+ ℤ.∣ n ∣) d c
floor : ℚ → ℤ
floor p@record{} = ↥ p ℤ./ ↧ p
ceiling : ℚ → ℤ
ceiling p@record{} = ℤ.- floor (- p)
truncate : ℚ → ℤ
truncate p with p ≤ᵇ 0ℚ
... | true = ceiling p
... | false = floor p
round : ℚ → ℤ
round p with p ≤ᵇ 0ℚ
... | true = ceiling (p - ½)
... | false = floor (p + ½)
fracPart : ℚ → ℚ
fracPart p@record{} = ∣ p - truncate p / 1 ∣
syntax floor p = ⌊ p ⌋
syntax ceiling p = ⌈ p ⌉
syntax truncate p = [ p ]