I am attempting to tackle this exercise from chapter 7 in a more generic way.
lift2 to write lifted versions of the numeric operators
/ which work with optional arguments.
Here’s one solution:
addApply :: forall f a. Apply f => Semiring a => f a -> f a -> f a addApply = lift2 add subApply :: forall f a. Apply f => Ring a => f a -> f a -> f a subApply = lift2 sub -- etc.
I’d like to find a solution using type class instances, so I can use the default
add instead of my custom
Here’s an attempt which produces an orphan instance error. I believe I need to create a type for
(f a), but I’m failing to find the correct syntax.
instance semiringApply :: (Apply f, Semiring a) => Semiring (f a) where add = lift2 add mul = lift2 mul zero = apply zero one = apply one