Add casting implementations

This commit is contained in:
Kiana Sheibani 2024-06-07 02:07:25 -04:00
parent 0dbd3e6bc8
commit 80e43be8fc
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -135,3 +135,18 @@ IntegralGCD a => Fractional (Ratio a) where
recip (MkRatio n d) = case n /= 0 of True => MkRatio d n recip (MkRatio n d) = case n /= 0 of True => MkRatio d n
MkRatio n d / MkRatio m b = case m /= 0 of MkRatio n d / MkRatio m b = case m /= 0 of
True => reduce $ MkRatio (n*b) (m*d) True => reduce $ MkRatio (n*b) (m*d)
-- ## Casting
export
Num a => Cast a (Ratio a) where
cast x = MkRatio x 1
export
Cast a b => Cast (Ratio a) (Ratio b) where
cast (MkRatio n d) = MkRatio (cast n) (cast d)
-- Special case: `Cast Rational Double`
export
(Cast a b, Fractional b) => Cast (Ratio a) b where
cast (MkRatio n d) = cast n / cast d