Applicative Distributor for List `dist` Function

泪湿孤枕 提交于 2019-12-25 01:48:48

问题


I think that I copied the following dist function from Applicative Programming with Effects:

The paper prefaces this function with:

Have you noticed that sequence and transpose now look rather alike? The details that distinguish the two programs are inferred by the compiler from their types. Both are instances of the applicative distributor for lists

dist :: Applicative f => [f a] -> f [a]
dist []       = [[]]
dist (v : vs) = [(:) v (dist vs)]

However, I get the following compile-time error:

ghci> :l ApplicativePaper.hs
[1 of 1] Compiling Main             ( ApplicativePaper.hs, interpreted )

ApplicativePaper.hs:12:20:
    Could not deduce (a ~ f a)
    from the context (Applicative f)
      bound by the type signature for
                 dist :: Applicative f => [f a] -> f [a]
      at ApplicativePaper.hs:10:9-39
      `a' is a rigid type variable bound by
          the type signature for dist :: Applicative f => [f a] -> f [a]
          at ApplicativePaper.hs:10:9
    Relevant bindings include
      vs :: [f a] (bound at ApplicativePaper.hs:12:9)
      v :: f a (bound at ApplicativePaper.hs:12:7)
      dist :: [f a] -> f [a] (bound at ApplicativePaper.hs:11:1)
    In the first argument of `(:)', namely `v'
    In the expression: (:) v (dist vs)
Failed, modules loaded: none.

Please let me know what I'm doing wrong. Also, please provide an intuition for this function.


回答1:


In the paper, the double bracket 〚 f u1 … un 〛 is defined on page 4 to be the same as pure f <*> u1 <*> … <*> un.

The definition of dist in the text is

dist :: Applicative f ⇒ [f a] → f [a ]
dist []       = 〚 [] 〛
dist (v : vs) = 〚 (:) v (dist vs) 〛

Translating the other symbols into Haskell and making the substitution for the definition of 〚 … 〛 results in

dist :: Applicative f => [f a] -> f [a ]
dist []       = pure []
dist (v : vs) = pure (:) <*> v <*> (dist vs)



回答2:


Wrong brackets. Conor McBride and Ross Paterson use so called idiom brackets. Here is your definition in imaginary syntax (but it probably compiles with the Strathclyde Haskell Enhancement, I'm not sure):

dist :: Applicative f => [f a] -> f [a]
dist []       = (| [] |)
dist (v : vs) = (| (:) v (dist vs) |)

(| f x y z |) elaborates to pure f <*> x <*> y <*> z. So dist defined right is

dist :: Applicative f => [f a] -> f [a]
dist  []      = pure []
dist (v : vs) = (:) <$> v <*> dist vs -- f <$> x == pure f <*> x

There is a very similar function, defined in the Control.Monad module:

-- | Evaluate each action in the sequence from left to right,
-- and collect the results.
sequence       :: Monad m => [m a] -> m [a] 
{-# INLINE sequence #-}
sequence ms = foldr k (return []) ms
            where
              k m m' = do { x <- m; xs <- m'; return (x:xs) }

Only it requires a monad instead of an applicative.

There is also a generalization of dist, provided by the Traversable typeclass:

sequenceA :: Applicative f => t (f a) -> f (t a)

So you have some t (which is both a Functor and a Foldable) instead of [] here.

This description of sequence

-- | Evaluate each action in the sequence from left to right,
-- and collect the results.

is pretty exhaustive. For example

main = dist [print 3, print 4, print 5]

prints

3
4
5

Since every monad is an applicative, dist works properly here with the IO monad. However there is no need in collecting the results here. That's why there is the sequence_ function, that returns () as the result.

It's easy to define dist_:

dist_ :: Applicative f => [f a] -> f ()
dist_  []      = pure ()
dist_ (v : vs) = v *> dist_ vs

So you simply perform all actions in a list.

For example

main = do
    print $ dist_ [Just 3, Just 4, Just 5]
    print $ dist_ [Nothing, Just 3]

prints

Just ()
Nothing

because

Just smth1 *> Just smth2 == Just smth2
Nothing    *> Just smth2 == Nothing

The only thing, that dist adds to this machinery, is collecting results.

So

main = do
    print $ dist [Just 3, Just 4, Just 5]
    print $ dist [Just 3, Nothing]

prints

Just [3,4,5]
Nothing

Also, have a look at the Typeclassopedia.



来源:https://stackoverflow.com/questions/27350924/applicative-distributor-for-list-dist-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!