Document Data.Ratio

This commit is contained in:
Kiana Sheibani 2022-09-04 16:31:40 -04:00
parent 776762298b
commit a8f9b98c70
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -2,28 +2,45 @@ module Data.Ratio
infix 10 // infix 10 //
||| Ratio types, represented by a numerator and denominator of type `a`.
|||
||| Most numeric operations require an instance `Integral a` in order to
||| simplify the returned ratio and reduce storage space.
export export
record Ratio a where record Ratio a where
constructor MkRatio constructor MkRatio
nm, dn : a nm, dn : a
||| Rational numbers, represented as a ratio between
||| two arbitrary-precision integers.
|||
||| Rationals can be constructed using `//`.
public export public export
Rational : Type Rational : Type
Rational = Ratio Integer Rational = Ratio Integer
-- This function is almost always total; after all, it's a fairly standard
-- implementation of Euclid's algorithm. Unfortunately, we can't _guarantee_
-- it's total without knowing exactly what the implementation of `Integral a` is,
-- so using an `assert_total` here would be potentially unsafe. The only option
-- is to mark this function, and by extention `reduce`, as non-total.
gcd : (Eq a, Integral a) => a -> a -> a gcd : (Eq a, Integral a) => a -> a -> a
gcd x y = gcd x y =
if x == 0 then y if x == 0 then y
else if y == 0 then x else if y == 0 then x
else gcd y (x `mod` y) else gcd y (x `mod` y)
||| Reduce a ratio by dividing both components by their common factor. Most
||| operations automatically reduce the returned ratio, so explicitly calling
||| this function is usually unnecessary.
export export
reduce : (Eq a, Integral a) => Ratio a -> Ratio a reduce : (Eq a, Integral a) => Ratio a -> Ratio a
reduce (MkRatio n d) = reduce (MkRatio n d) =
let g = gcd n d in MkRatio (n `div` g) (d `div` g) let g = gcd n d in MkRatio (n `div` g) (d `div` g)
||| Construct a ratio of two values.
export partial export partial
(//) : (Eq a, Integral a) => a -> a -> Ratio a (//) : (Eq a, Integral a) => a -> a -> Ratio a
n // d = case d /= 0 of n // d = case d /= 0 of
@ -31,18 +48,24 @@ n // d = case d /= 0 of
namespace Ratio namespace Ratio
||| Return the numerator of the fraction in reduced form.
export %inline export %inline
numer : Ratio a -> a numer : Ratio a -> a
numer = nm numer = nm
||| Return the numerator of the fraction in reduced form.
export %inline export %inline
(.numer) : Ratio a -> a (.numer) : Ratio a -> a
(.numer) = nm (.numer) = nm
||| Return the denominator of the fraction in reduced form.
||| This value is guaranteed to always be positive.
export %inline export %inline
denom : Ratio a -> a denom : Ratio a -> a
denom = dn denom = dn
||| Return the denominator of the fraction in reduced form.
||| This value is guaranteed to always be positive.
export %inline export %inline
(.denom) : Ratio a -> a (.denom) : Ratio a -> a
(.denom) = dn (.denom) = dn