The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

509898 (4) [Avatar] Offline
#1
2-tuple (,) is instance of Applicative when the first parameter is instance of Monoid.

instance Monoid a => Applicative ((,) a) where
  pure = (,) empty
  (x, f) <*> (x', y) = (x `mappend` x', f y)


Later, it also says:

Quick check 29.3 Suppose you want to make it so that (pure +) <*> (1,2) <*> (3,4) =
(1+2,1+4,2+3,2+4) = (3,5,5,6). Why doesn’t this work?

QC 29.3 answer This doesn’t work because (3,5,5,6) is an entirely different type than (1,2) or
(3,4). The first is type (a,b,c,d), and the other two are (a,b).

I think the reasoning is not right. The reasons why it does not work are other:

a) kind of (,) is * -> * -> *, and Applicative is * -> *. The first parameter must be set.
b) The first parameter is a number and it is instance of Moniod. But it has two ways of being Moniod: with the sum and with the product and you have to choose which one you want to use.
c) Minor typo: The + operator must appear in brackets.

With these considerations, it does work:

GHCi> pure (+) <*> (Sum 1,2) <*> (Sum 3,4)
(Sum {getSum = 4},6)

GHCi> pure (+) <*> (Product 1,2) <*> (Product 3,4)
(Product {getProduct = 3},6)