6

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?

4

3 回答 3

5

我只会使用语言扩展来为我派生它:

{-# 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
()

如果您想知道编译器如何自动实现它,您可以使用-ddump-deriv标志编译它(稍微清理一下):

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)
于 2014-01-24T21:49:31.920 回答
3

这里的典型Traversable实现是

traverse f (Data foo bar) = Data <$> f foo <*> f bar
于 2014-01-24T21:19:57.270 回答
3

所以你的定义traverse与给定的签名不一致。

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

而如果Data是 的一个实例Traversable,那么我们将专注Tr.traverse

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

试图统一它们会带来问题:

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

这就是编译器抱怨的原因:

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

所以让我们给 traverse 一个有效的定义:

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

接下来,您的 main 函数中有一个错误。Tr.traverse (putStrLn show) exampleData你是按你的意思写Tr.traverse (putStrLn . show) exampleData的。

putStrLn是 type String -> IO (),所以putStrLn show需要show是一个字符串才能进行类型检查,但是show :: Show a -> a -> String. 这就是编译器抱怨的原因:

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

您想使用.运算符组合这些函数:

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

您也可以只使用print定义为putStrLn . show.

于 2014-01-24T21:20:10.947 回答