I have an infinite structure similar to the following (using Stream type streams ):
data U x = U (Stream x) x (Stream x) deriving (Functor,Foldable)
I want to provide a Traversable instance for it, for example:
instance Traversable U where traverse f (U lstream focus rstream) = let pairs = liftA unzip . sequenceA . fmap (traversepair f) $ zip lstream rstream traversepair f (a,b) = (,) <$> fa <*> fb rebuild c (u,v) = U ucv in rebuild <$> f focus <*> pairs
The documentation for Data.Traversable says they are "a class of data structures that can be traversed from left to right." But my definition does not pass from left to right, it passes out. I had to define it in such a way as to lazily extract values from both sides after the sequence operation using Rand monad.
Is this the correct definition? I noticed that the Typeclassopedia entry for Traversable does not say anything about “from left to right,” it only talks about “switching two functors”.
source share