module Data.Rational.Properties where
*β-distrib-minusr : β {x y z} β (y -β z) *β x β‘ y *β x -β z *β x
*β-distrib-minusr {x@(inc _)} {y@(inc _)} {z@(inc _)} = *β-distribr x y (-β z) β apβ _+β_ (Ξ» i β y *β x) (βr.*-negatel {z} {x})
*β-distrib-minusl : β {x y z} β x *β (y -β z) β‘ x *β y -β x *β z
*β-distrib-minusl = *β-commutative _ _ β *β-distrib-minusr β apβ _-β_ (*β-commutative _ _) (*β-commutative _ _)
*β-invl : β {x} β¦ p : Nonzero x β¦ β invβ x *β x β‘ 1
*β-invl = *β-commutative _ _ β *β-invr
/β-def : {x y : β} β¦ p : Nonzero y β¦ β (x /β y) β‘ x *β invβ y
/β-def {inc x} {inc y} = refl
*β-nonzero : β {x y} β Nonzero x β Nonzero y β Nonzero (x *β y)
/β-nonzero-num : β {n d} β¦ p : Nonzero d β¦ β Nonzero (n /β d) β Nonzero n
negβ-nonzero : β {n} β Nonzero n β Nonzero (-β n)
invβ-nonzero : β {n} β¦ d : Nonzero n β¦ β Nonzero (invβ n)
unquoteDef *β-nonzero /β-nonzero-num negβ-nonzero invβ-nonzero = do
by-elim-β *β-nonzero Ξ» d a b Ξ± Ξ² β
to-nonzero-frac (β€.*β€-nonzero (from-nonzero-frac Ξ±) (from-nonzero-frac Ξ²))
by-elim-β /β-nonzero-num Ξ» where
d x β€.posz β¦ p β¦ nz β absurd (from-nonzero-frac p refl)
d x (β€.possuc y) nz β to-nonzero-frac (β€.*β€-nonzero-cancel {x} {d} (from-nonzero-frac nz))
(β€.possuc x) β¦ β€.pos .x β¦ y (β€.negsuc z) nz β to-nonzero-frac (β€.*β€-nonzero-cancel {y} {β€.negsuc x} (from-nonzero-frac nz))
by-elim-β negβ-nonzero Ξ» where
d β€.posz (inc Ξ±) β absurd (Ξ± (quotβ (to-same-rational refl)))
d (β€.possuc x) (inc Ξ±) β to-nonzero-frac β€.negsucβ pos
d (β€.negsuc x) (inc Ξ±) β to-nonzero-frac (sucβ zero β β€.pos-injective)
by-elim-β invβ-nonzero Ξ» where
d β€.posz β¦ inc Ξ± β¦ β absurd (Ξ± (quotβ (to-same-rational refl)))
(β€.possuc d) β¦ β€.pos .d β¦ (β€.possuc x) β¦ inc Ξ± β¦ β to-nonzero-frac (sucβ zero β β€.pos-injective)
(β€.possuc d) β¦ β€.pos .d β¦ (β€.negsuc x) β¦ inc Ξ± β¦ β to-nonzero-frac β€.negsucβ pos
instance
Nonzero-*β : β {x y : β} β¦ p : Nonzero x β¦ β¦ q : Nonzero y β¦ β Nonzero (x *β y)
Nonzero-*β β¦ p β¦ β¦ q β¦ = *β-nonzero p q
Nonzero-invβ : β {x} β¦ p : Nonzero x β¦ β Nonzero (invβ x)
Nonzero-invβ β¦ p β¦ = invβ-nonzero β¦ p β¦
Nonzero-/β : β {x y} β¦ p : Nonzero x β¦ β¦ q : Nonzero y β¦ β Nonzero (x /β y)
Nonzero-/β {x} {y} β¦ p β¦ β¦ q β¦ = subst Nonzero (sym /β-def) (*β-nonzero p invβ-nonzero)
{-# OVERLAPPABLE Nonzero-*β Nonzero-invβ Nonzero-/β #-}
/β-oner : β x β x /β 1 β‘ x
/β-oner (inc x) = *β-idr (inc x)
/β-ap : β {x y x' y'} {p : Nonzero y} {q : Nonzero y'} β x β‘ x' β y β‘ y' β (_/β_ x y β¦ p β¦) β‘ (_/β_ x' y' β¦ q β¦)
/β-ap {p = Ξ±} {Ξ²} p q i = _/β_ (p i) (q i) β¦ is-propβpathp (Ξ» i β hlevel {T = Nonzero (q i)} 1) Ξ± Ξ² i β¦
/β-factorr : β {x y} β¦ p : Nonzero y β¦ β (x *β y) /β y β‘ x
/β-factorr = /β-def β sym (*β-associative _ _ _) β apβ _*β_ refl *β-invr β *β-idr _
/β-self : β {x} β¦ p : Nonzero x β¦ β x /β x β‘ 1
/β-self {x} = /β-def β *β-invr
/β-factorl : β {x y} β¦ p : Nonzero y β¦ β (y *β x) /β y β‘ x
/β-factorl = /β-ap (*β-commutative _ _) refl β /β-factorr
/β-scaler : β {x y z} β¦ p : Nonzero y β¦ β (x /β y) *β z β‘ (x *β z) /β y
/β-scaler = ap (_*β _) /β-def β sym (*β-associative _ _ _) β ap (_ *β_) (*β-commutative _ _) β *β-associative _ _ _ β sym /β-def
/β-scalel : β {x y z} β¦ p : Nonzero z β¦ β x *β (y /β z) β‘ (x *β y) /β z
/β-scalel {z = z} = *β-commutative _ _ β /β-scaler β ap (Ξ» e β e /β z) (*β-commutative _ _)
/β-cross : β {q q' d} β¦ Ξ± : Nonzero d β¦ β q *β d β‘ q' β q β‘ (q' /β d)
/β-cross {d = d} p = sym (ap (Ξ» e β (e /β d) β¦ _ β¦) (sym p) β /β-factorr)
/β-same
: β {q q' d d'} β¦ Ξ± : Nonzero d β¦ β¦ Ξ² : Nonzero d' β¦
β q *β d' β‘ q' *β d β q /β d β‘ q' /β d'
/β-same p = /β-cross (/β-scaler β sym (/β-cross (sym p)))
/β-frac
: β {n d} β¦ p : β€.Positive d β¦
β (n / 1) /β (d / 1) β‘ (n / d)
/β-frac {n} {d = β€.possuc x} β¦ p = β€.pos x β¦ = quotβ (to-same-rational (sym (β€.*β€-associative n 1 (β€.possuc x))))
invβ-*β
: β {d d'} β¦ p : Nonzero d β¦ β¦ p' : Nonzero d' β¦
β invβ (d *β d') β‘ invβ d *β invβ d'
invβ-*β {d} {d'} = monoid-inverse-unique *β-monoid (d *β d') _ _ *β-invl
(sym (*β-associative _ _ _) β ap (d *β_)
(ap (d' *β_) (*β-commutative _ _)
β *β-associative d' _ _
β ap (_*β invβ d) *β-invr β *β-idl (invβ d))
β *β-invr {d})
invβ-invβ : β {d} β¦ p : Nonzero d β¦ β invβ (invβ d) β‘ d
invβ-invβ {d} = monoid-inverse-unique *β-monoid (invβ d) _ _ (*β-commutative _ _ β *β-invr) (*β-commutative _ _ β *β-invr)
invβ-/β
: β {q d} β¦ p : Nonzero d β¦ β¦ p' : Nonzero q β¦
β invβ (q /β d) β‘ (d /β q)
invβ-/β = apβ (Ξ» e p β invβ e β¦ p β¦) /β-def prop! β invβ-*β β apβ _*β_ refl invβ-invβ β *β-commutative _ _ β sym /β-def
/β-negatel
: β {q d} β¦ p : Nonzero d β¦ β -β (q /β d) β‘ (-β q) /β d
/β-negatel = ap -β_ /β-def Β·Β· sym *β-negatel Β·Β· sym /β-def
/β-def-+β
: β {q q' d d'} β¦ p : Nonzero d β¦ β¦ p' : Nonzero d' β¦
β (q /β d) +β (q' /β d') β‘ ((q *β d' +β q' *β d) /β (d *β d'))
/β-def-+β {d = d} {d'} = /β-cross
(*β-distribr _ _ _ β apβ _+β_
(/β-scaler β ap (Ξ» e β e /β d) (ap (_ *β_) (*β-commutative d d') β *β-associative _ d' d) β /β-factorr)
(/β-scaler β ap (Ξ» e β e /β d') (*β-associative _ d d') β /β-factorr))
/β-def-subβ
: β {q q' d d'} β¦ p : Nonzero d β¦ β¦ p' : Nonzero d' β¦
β (q /β d) -β (q' /β d') β‘ ((q *β d' -β q' *β d) /β (d *β d'))
/β-def-subβ {d = d} {d'} = /β-cross
(*β-distrib-minusr β apβ _-β_
(/β-scaler β ap (Ξ» e β e /β d) (ap (_ *β_) (*β-commutative d d') β *β-associative _ d' d) β /β-factorr)
(/β-scaler β ap (Ξ» e β e /β d') (*β-associative _ d d') β /β-factorr))
/β-def-*β
: β {q q' d d'} β¦ p : Nonzero d β¦ β¦ p' : Nonzero d' β¦
β (q /β d) *β (q' /β d') β‘ (q *β q') /β (d *β d')
/β-def-*β {d = d} {d'} = /β-cross
(sym (*β-associative _ _ _)
β ap (_ *β_) (/β-scaler β ap (Ξ» e β e /β d') (*β-associative _ d d') β /β-factorr)
β /β-scaler β ap (Ξ» e β e /β d) (*β-associative _ _ d) β /β-factorr)
abstract
*β-positive : β {x y} β Positive x β Positive y β Positive (x *β y)
+β-positive : β {x y} β Positive x β Positive y β Positive (x +β y)
+β-nonneg-positive : β {x y} β 0 β€ x β Positive y β Positive (x +β y)
invβ-positive : β {d} (p : Positive d) β Positive (invβ d β¦ inc (positiveβnonzero p) β¦)
unquoteDef *β-positive +β-positive invβ-positive +β-nonneg-positive = do
by-elim-β *β-positive Ξ» d a b (inc x) (inc y) β inc (β€.*β€-positive x y)
by-elim-β +β-positive Ξ» d a b (inc x) (inc y) β inc (β€.+β€-positive (β€.*β€-positive x auto) (β€.*β€-positive y auto))
by-elim-β invβ-positive Ξ» where
d@(β€.possuc d') β¦ β€.pos .d' β¦ (β€.possuc x) px β inc (β€.pos d')
by-elim-β +β-nonneg-positive Ξ» where
d (β€.posz) b (inc x) (inc y) β inc (subst β€.Positive (sym (β€.+β€-zerol (b β€.*β€ d))) (β€.*β€-positive y auto))
d (β€.possuc a) b (inc x) (inc y) β inc (β€.+β€-positive {β€.possuc a β€.*β€ d} {b β€.*β€ d} (β€.*β€-positive (β€.pos a) auto) (β€.*β€-positive y auto))
/β-positive : β {x y} (p : Positive x) (q : Positive y) β Positive ((x /β y) β¦ inc (positiveβnonzero q) β¦)
/β-positive {y = y} p q = subst Positive
(sym (/β-def β¦ _ β¦)) (*β-positive p (invβ-positive q))
from-positive : β {x} β Positive x β 0 < x
to-positive : β {x} β 0 < x β Positive x
unquoteDef from-positive to-positive = do
by-elim-β from-positive Ξ» where
d (β€.possuc x) (inc (β€.pos .x)) β inc (β€.pos<pos (sβ€s 0β€x))
by-elim-β to-positive Ξ» where
d (β€.pos zero) (inc p) β inc (absurd (β€.<-irrefl refl p))
d (β€.pos (suc x)) (inc p) β inc (β€.pos x)
absβ-nonneg : β {x} β 0 β€ absβ x
unquoteDef absβ-nonneg = by-elim-β absβ-nonneg Ξ» where
d (β€.pos x) β inc (β€.aposβ€apos {0} {x * 1} 0β€x)
d (β€.negsuc x) β inc (β€.posβ€pos 0β€x)
abstract
negβ-anti-< : β {x y} β x < y β -β y < -β x
negβ-anti-full-< : β {x y} β -β x < -β y β y < x
unquoteDef negβ-anti-< negβ-anti-full-< = do
by-elim-β negβ-anti-< Ξ» d x y Ξ± β common-denom-< (β€.negβ€-anti-< (<-common-denom Ξ±))
by-elim-β negβ-anti-full-< Ξ» d x y Ξ± β common-denom-< (β€.negβ€-anti-full-< (<-common-denom Ξ±))