[Haskell-cafe] Automatically Deriving Numeric Type Class Instances
adam vogt
vogt.adam at gmail.com
Fri Apr 22 19:14:00 UTC 2016
Hi Jake
https://hackage.haskell.org/package/applicative-numbers can generate those
instances.
Regards
Adam
On Apr 22, 2016 10:23 AM, "Jake" <jake.waksbaum at gmail.com> wrote:
> Is it possible to automatically derive instances of Numeric type classes
> like Num, Fractional, Real, Floating, etc?
>
> I currently have two datatypes, Pair and Triple, that are defined like
> this:
>
> data Pair a = Pair a a
> data Triple a = Triple a a a
>
> I wrote these pretty trivial instances for Num and Floating:
>
> instance Num a => Num (Pair a) where
> (+) = liftA2 (+)
> (*) = liftA2 (*)
> abs = liftA abs
> negate = liftA negate
> signum = liftA signum
> fromInteger = pure . fromInteger
>
> instance Fractional a => Fractional (Pair a) where
> (/) = liftA2 (/)
> recip = liftA recip
> fromRational = pure . fromRational
>
> and practically identical instances for Triple as well.
>
> Is there anyway to have GHC derive these instances and the other numeric
> type classes?
>
> Thanks,
> Jake
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160422/797bc424/attachment.html>
More information about the Haskell-Cafe
mailing list