Automatically Deriving Numeric Type Class Instances

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Automatically Deriving Numeric Type Class Instances

Jake
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Automatically Deriving Numeric Type Class Instances

Adam Vogt

Hi Jake

https://hackage.haskell.org/package/applicative-numbers can generate those instances.

Regards
Adam

On Apr 22, 2016 10:23 AM, "Jake" <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Automatically Deriving Numeric Type Class Instances

Jake

Thanks Adam! My only concern is that this package appears to use the CPP to generate the instances which at least to me feels more hacky than the mechanism by which instances are usually derived, like for Show or Eq or other classes.

I'd also be interested if someone could explain how those instances are derived if I could do something similar myself in this case.


On Fri, Apr 22, 2016, 15:14 adam vogt <[hidden email]> wrote:

Hi Jake

https://hackage.haskell.org/package/applicative-numbers can generate those instances.

Regards
Adam

On Apr 22, 2016 10:23 AM, "Jake" <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Automatically Deriving Numeric Type Class Instances

Conal Elliott
Hi Jake. I wrote applicative-numbers. The key observation is that every applicative functor gives rise to instances of Num & friends in a standard way, as you've noticed. Since your examples are isomorphic to functions from a suitably chosen domain, and function-from-t is an applicative functor for all types t, your examples are also applicative functors, with instances you can derive (as in http://conal.net/papers/type-class-morphisms/).

It might make for a simple and useful project to convert applicative-numbers to use Template Haskell.

- Conal

On Fri, Apr 22, 2016 at 12:39 PM, Jake <[hidden email]> wrote:

Thanks Adam! My only concern is that this package appears to use the CPP to generate the instances which at least to me feels more hacky than the mechanism by which instances are usually derived, like for Show or Eq or other classes.

I'd also be interested if someone could explain how those instances are derived if I could do something similar myself in this case.


On Fri, Apr 22, 2016, 15:14 adam vogt <[hidden email]> wrote:

Hi Jake

https://hackage.haskell.org/package/applicative-numbers can generate those instances.

Regards
Adam

On Apr 22, 2016 10:23 AM, "Jake" <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe