Traverse sequence implementation

sequenceA is implemented as follows in Data.Traversable

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

It’s hard for me to understand the type traverse id. traversehas type: traverse :: Applicative f => (a -> f b) -> t a -> f (t b)Its first parameter is of type: (a -> f b)But in case the sequenceAtype of id-function (a -> a). Where is f?

+4
source share
1 answer

Let’s first say what idtype id :: c -> cit is to make things less complicated. ain a function type signature is local in the sense that aof id :: a -> ahas nothing to do with ain traverse :: Applicative f => (a -> f b) -> t a -> f (t b). ais just a type parameter.

So now we have the expression:

traverse id

with:

traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
id       ::                   c -> c

c -> c a -> f b, , Haskell. , :

a   ~ c
f b ~ c

, , a ~ c ~ f b, , , , , a ~ f b. , Haskell traverse id :

traverse id :: Applicative f => t a -> f (t b)
traverse id :: Applicative f => t (f b) -> f (t b) -- a ~ f b

, . id traverse id id :: f b -> f b.

+5

Source: https://habr.com/ru/post/1678944/


All Articles