{-# LANGUAGE FlexibleContexts, UndecidableInstances, FlexibleInstances #-} -- Lifting Numeric Prelude to Applicative module ApplicativeAlgebra where import Prelude hiding ((+), (*), (/), signum, fromInteger, abs) import Control.Applicative import Algebra.Additive import Algebra.Field import Algebra.Ring -- 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 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 (/) {- test data Pair n = Pair n n deriving (Eq, Show) instance Functor Pair where fmap f (Pair a b) = Pair (f a) (f b) instance Applicative Pair where pure f = Pair f f (<*>) (Pair fa fb) (Pair a b) = Pair (fa a) (fb b) -- Allow lifting instance NumericApp Pair -}