10

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.

4
  • 4
    I agree, these instances seem to make no sense whatsoever. Clear case of “because we can”. Commented Sep 27, 2016 at 15:54
  • 1
    I don't have access to GHCi right now, but is it possible that they could be used for doing something like x :: Sum Int; x = do { 1; 2; 3; 4; 5 }?
    – bheklilr
    Commented Sep 27, 2016 at 16:02
  • 2
    @bheklilr It "works", and returns Sum 5 since Sum is the identity monad.
    – chi
    Commented Sep 27, 2016 at 16:09
  • It seems that it's possible to do something similar: 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/… Commented Sep 28, 2016 at 18:20

2 Answers 2

11

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.

5
  • Yes, but that's arguably an abuse of the Applicative class. It's much better to use Iso 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 of liftA2. Commented Sep 27, 2016 at 17:01
  • 1
    @leftaroundabout, when I look at au and auf, mine eyes glazeth over.
    – dfeuer
    Commented Sep 27, 2016 at 17:02
  • @DanielWagner well, I didn't particularly suggest using lenses to accomplish liftA2 (.&.) on Sum. That is certainly not sensible. What I actually meant is that it doesn't do much good to keep Sum 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 type Sum Int. In other words, I understand Sum 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 a Sum in for a single operation, without needing to manually wrap and unwrap again. If lens is to much of a dependency you can also use them from the original package. Commented Sep 27, 2016 at 18:48
3

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}
1
  • 4
    Sum 5 * Sum 10 also works, because of the Num instance.
    – chi
    Commented Sep 27, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.