{-# LANGUAGE FlexibleContexts, UndecidableInstances, FlexibleInstances #-} -- Lifting Num over Applicative module ApplicativeNum where import Control.Applicative -- The property that allows the lifting. We can't lift all -- Applicatives, so we need to bless the ones we can. class Applicative a => NumericApp a class NumericApp a => NumericAppOpaque a -- These don't work in general. instance NumericAppOpaque a => Eq (a n) where (==) _ _ = False instance NumericAppOpaque a => Show (a n) where show _ = "#<...>" instance (NumericApp a, Num n, Show (a n), Eq (a n)) => Num (a n) where (+) = liftA2 (+) (*) = liftA2 (*) abs = fmap abs signum = fmap signum fromInteger = pure . fromInteger