Question

I am trying to traverse all members of a data structure in haskell using Data.Traversable, which is documented at the following urls:

http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-Traversable.html http://www.haskell.org/haskellwiki/Foldable_and_Traversable

So far I have come up with the following code which is as far as I know missing a proper implementation of the Tr.Traversable instancing.

import qualified Data.Traversable as Tr
import qualified Data.Foldable as Fl
import Control.Monad
import Control.Applicative

data Test = Test { desc  :: String
                 , value :: Int
} 

data Data t = Data { foo :: t 
                   , bar :: t       
} 

exampleData = Data { foo = Test "Foo" 1 
                   , bar = Test "Bar" 2
}

instance Show Test where
  show f = (desc f) ++ ": " ++ (show $ value f)

instance (Show a) => Show (Data a) where
  show f = show (foo f)

instance Functor Data where
  fmap = Tr.fmapDefault

instance Fl.Foldable Data where
  foldMap = Tr.foldMapDefault

instance Tr.Traversable Data where
    traverse f = Data f  -- Try to show a Test entry inside the Data structure

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

main = do
  putStrLn $ show exampleData
  Tr.traverse (putStrLn show) exampleData

I am trying to print all the items in exampleData, member by member and using show. Am I on the right track and how should the traversable instancing be implemented?

Était-ce utile?

La solution

I would just use the language extensions to derive it for me:

{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
import qualified Data.Traversable as Tr
import qualified Data.Foldable as Fl
import Control.Monad
import Control.Applicative

data Test = Test { desc  :: String
                 , value :: Int }

data Data t = Data { foo :: t
                   , bar :: t } deriving (Functor, Tr.Traversable, Fl.Foldable)

exampleData = Data { foo = Test "Foo" 1
                   , bar = Test "Bar" 2
}

instance Show Test where
  show f = (desc f) ++ ": " ++ (show $ value f)

instance (Show a) => Show (Data a) where
  show f = show (foo f)

main = do
  putStrLn $ show exampleData
  Tr.traverse (putStrLn . show) exampleData

> runhaskell test_traversable.hs
Foo: 1
Foo: 1
Bar: 2
()

If you want to know how the compiler automatically implements it, you could compile it with the -ddump-deriv flag (cleaned up a bit):

instance Functor Data where
  fmap f (Data foo' bar') = Data (f foo') (f bar')

instance Tr.Traversable Data where
  tr.traverse f (Data foo' bar') = Data <$> (f foo') <*> (f bar')

instance Fl.Foldable Data where
  Fl.foldr f z (Data foo' bar') = f foo' (f bar' z)

Autres conseils

The typical Traversable implementation here would be

traverse f (Data foo bar) = Data <$> f foo <*> f bar

So your definition of traverse doesn't unify with the given signature.

traverse f = Data f -- f :: x implies Data f :: x -> Data x, so this implies
traverse :: x -> x -> Data x

whereas if Data were an instance of Traversable, then we'd specialize Tr.traverse to

Tr.traverse :: Applicative f => (a -> f b) -> Data a -> f (Data b)

Attempting to unify them gives problems:

traverse ~ Tr.traverse if and only if
  x ~ (a -> f b)
  x ~ Data a
  Data x ~ f (Data b)

Which is why the compiler complains:

Couldn't match expected type `Data a' with actual type `a -> f b'
In the first argument of `Data', namely `f'
In the expression: Data f
In an equation for `traverse': traverse f = Data f

So let's give a valid definition for traverse:

traverse f (Data a a') = Data <$> f a <*> f a'

Next you have an error in your main function. You wrote Tr.traverse (putStrLn show) exampleData when you meant Tr.traverse (putStrLn . show) exampleData.

putStrLn is of type String -> IO (), so putStrLn show needed show to be a string in order to typecheck, but show :: Show a -> a -> String. This is why the compiler complains:

Couldn't match expected type `Test -> IO b0'
            with actual type `IO ()'
In the return type of a call of `putStrLn'
Probable cause: `putStrLn' is applied to too many arguments
In the first argument of `Tr.traverse', namely `(putStrLn show)'
In a stmt of a 'do' block: Tr.traverse (putStrLn show) exampleData

Couldn't match type `a0 -> String' with `[Char]'
Expected type: String
  Actual type: a0 -> String
In the first argument of `putStrLn', namely `show'
In the first argument of `Tr.traverse', namely `(putStrLn show)'
In a stmt of a 'do' block: Tr.traverse (putStrLn show) exampleData

You wanted to compose those functions using the . operator:

putStrLn . show == \a -> putStrLn (show a)

You could also just use print which is defined as putStrLn . show.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top