My understanding of Sum
and Product
newtypes is that they serve as monoidial wrappers for numeric types. I would understand Functor
instance on them, but why there are also Applicative
, Monad
any many other seemingly useless instances? I understand that they are mathemathically OK (isomorphic to Identity
modad, right?) But what is the use case? If there is an Applicative Sum
instance, for example, I would expect to encounter a value of type Sum (a -> b)
somewhere. I can't imagine where this could possibly be useful.
2 Answers
Such instances are convenient for lifting arbitrary functions to work on things that happen to currently be living inside a Sum
or Product
. For example, one might imagine wanting to do some bitwise operations on something that is nevertheless more convenient in a Sum
than bare; then liftA2 (.&.) :: Sum Int -> Sum Int -> Sum Int
(for example).
One could also provide this operation by giving a Bits
instance for Sum
, but generalizing that technique would require the implementors of Sum
to predict every operation one might ever want to do, which seems like a tall order. Providing Applicative
and Monad
instances give a once-and-for-all translation for users to lift any function they like -- including ones the implementors of Sum
did not predict being useful.
-
Yes, but that's arguably an abuse of the
Applicative
class. It's much better to useIso
combinators for this purpose. Commented Sep 27, 2016 at 16:57 -
1@leftaroundabout Can you say carefully what is better about the
Iso
combinators? Naively, they look like a strange tradeoff: it's a very heavy dependency (certainly not one that should be rolled into base), and the syntax for lifting two- or more-argument functions looks verbose compared to a single application ofliftA2
. Commented Sep 27, 2016 at 17:01 -
1@leftaroundabout, when I look at
au
andauf
, mine eyes glazeth over.– dfeuerCommented Sep 27, 2016 at 17:02 -
@DanielWagner well, I didn't particularly suggest using
lenses
to accomplishliftA2 (.&.)
onSum
. That is certainly not sensible. What I actually meant is that it doesn't do much good to keepSum
wrapped values around at all and combine them like that – these newtypes are intended so you can inject them into a single algorithm that works on monoids but that you want to use with numbers, but not to actually get an result of typeSum Int
. In other words, I understandSum
more as a tag you can pass to functions to explain what to do with numbers, not as an actual type constructor. Commented Sep 27, 2016 at 18:47 -
...hence I also don't find it reasonable to give them any instances at all apart from the
Monoid
one which these newtypes are all about. — The iso combinators make it easier to “tag-wrap” a number into aSum
in for a single operation, without needing to manually wrap and unwrap again. Iflens
is to much of a dependency you can also use them from the original package. Commented Sep 27, 2016 at 18:48
Values like this typically result from partial application of binary operators. Assuming Functor
and Applicative
instances like
import Control.Applicative
import Data.Monoid
instance Functor Sum where
fmap f (Sum x) = Sum (f x)
instance Applicative Sum where
pure = Sum
(Sum f) <*> (Sum x) = Sum (f x)
then you can see how a value of Sum (a -> b)
would arise.
> :t (*) <$> (Sum 5)
(*) <$> (Sum 5) :: Num a => Sum (a -> a)
> (*) <$> (Sum 5) <*> (Sum 10)
Sum {getSum = 50}
-
4
x :: Sum Int; x = do { 1; 2; 3; 4; 5 }
?Sum 5
sinceSum
is the identity monad.x :: Sum Int; x = do {Sum 1; Sum 2}
) and get the actual sum with -XRebindableSyntax by saying(>>) = mappend
ocharles.org.uk/blog/guest-posts/…