haskell 书要我实现可遍历的实例
newtype Constant a b = Constant { getConstant :: a }
包括所有必要的超类。下面的代码通过Quickcheck/Checkers
了,但行为很有趣
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
newtype Constant a b = Constant { getConstant :: a }
instance Functor (Constant a) where
fmap f (Constant a) = Constant a
instance Foldable (Constant a) where
foldr f z (Constant x) = z
instance Traversable (Constant a) where
traverse f (Constant a) = pure $ Constant a
type TI = []
main = do
let trigger = undefined :: TI (Int, Int, [Int])
quickBatch (traversable trigger)
当我尝试像这样使用可遍历实例时:
traverse (\x -> [x + 1]) $ Constant 5
我没有得到Constant [5]
我希望的结果,而是
traverse (\x -> [x + 1]) $ Constant 5
:: (Num b, Num a) => [Constant a b]
这是什么意思?我做错了什么吗?