<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
I'm following the CIS194 lectures and exercises. In particular, in
one of the Applicative lectures [0] we're asked to implement mapA
and sequenceA, from the name and given type signature alone. I
renamed them mapA' and sequenceA' to avoid name clashes, and
defined:<br>
<br>
<font face="Courier New, Courier, monospace">import
Control.Applicative<br>
<br>
mapA' :: Applicative f => (a -> f b) -> ([a] -> f [b])<br>
mapA' f = sequenceA' . map f<br>
<br>
sequenceA' :: Applicative f => [f a] -> f [a]<br>
sequenceA' = foldr (\fa fla -> (:) <$> fa <*> fla)
(pure [])<br>
</font><br>
These implementations seem to be correct, though undoubtedly not the
nicest.<br>
<br>
Now, I ask for a type in ghci:<br>
<br>
<font face="Courier New, Courier, monospace">> :t mapA' pure
[1..5]<br>
mapA' pure [1..5] :: (Enum b, Num b, Applicative f) => f [b]<br>
</font><br>
And so I try the following:<br>
<font face="Courier New, Courier, monospace">> mapA' pure [1..5]
:: [[Int]]<br>
[[1,2,3,4,5]]<br>
> take 5 . getZipList $ (mapA' pure [1..5] :: ZipList [Int])<br>
[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]<br>
> mapA' pure [1..5] :: Maybe [Int]<br>
Just [1,2,3,4,5]<br>
</font><br>
That makes sense. But I don't understand the following:<br>
<br>
<font face="Courier New, Courier, monospace">> mapA' pure [1..5]<br>
[1,2,3,4,5]<br>
</font><br>
Which Applicative instance is being used here? The type test above
says my expression should have type f [b], where f is an Applicative
and b is a Num, but all I see is a result of type [b] when I compute
mapA' pure [1..5]. What's going on?<br>
<br>
More simply<br>
<font face="Courier New, Courier, monospace">> :t pure<br>
pure :: Applicative f => a -> f a<br>
> pure [1..5]<br>
[1,2,3,4,5]<br>
</font><br>
<font face="Courier New, Courier, monospace">a</font> must match a
list of Nums, but then where did <font face="Courier New, Courier,
monospace">f</font> go? Is ghci using some default instance?<br>
<br>
Actually, after I add the following lines to my source file:<br>
<font face="Courier New, Courier, monospace">ggg = pure [1..5]<br>
hhh = mapA' pure [1..5]<br>
</font><br>
I get the errors:<br>
<font face="Courier New, Courier, monospace">No instance for
(Applicative f0) arising from a use of ‘pure’<br>
No instance for (Applicative f1) arising from a use of ‘mapA'’<br>
</font><br>
respectively. That makes sense to me.<br>
<br>
Graham<br>
<br>
[0]
<a class="moz-txt-link-freetext" href="http://www.seas.upenn.edu/~cis194/fall14/spring13/lectures/11-applicative2.html">http://www.seas.upenn.edu/~cis194/fall14/spring13/lectures/11-applicative2.html</a><br>
<br>
</body>
</html>