Sun Aug 21 18:55:42 CEST 2011

From Applicative to Num

It's been a couple of times that I've written something similar to the
following instance declarations for Num t, Applicative (X t), where X
is some kind of "bigger t".

Is there a way to abstract this lifting to Num (X t)?

instance (Eq (SigOp i o)) where (==) _ _ = False

instance Show (SigOp i v) where show _ = "#<SigOp>"

instance (Num o, Show (SigOp i o), Eq (SigOp i o)) =>
         Num (SigOp i o) where
  (+) = liftA2 (+)
  (*) = liftA2 (*)
  abs = fmap abs
  signum = fmap signum
  fromInteger = pure . fromInteger

To do it generally it might be best to restrict this so it doesn't
include all Applicative instances by defining a blessing class:

  class Applicative a => NumericApp a

The rest is straightforward.  The following is for NumericPrelude:

  instance (Algebra.Additive.C n, NumericApp a)
           => Algebra.Additive.C (a n) where
    (+) = liftA2 (+)
    zero = pure zero

  instance (Algebra.Ring.C n, NumericApp a)
           => Algebra.Ring.C (a n) where
    (*) = liftA2 (*)

  instance (Algebra.Field.C n, NumericApp a)
           => Algebra.Field.C (a n) where
    (/) = liftA2 (/)