我通过阅读《第一原理》(Allen&Moronuki)的《Haskell编程》一书来学习Haskell 。
在有关Monad Transformers,Functor和Applicative组成部分的练习中,它要求读者为以下类型编写Bifunctor实例
data SemiDrei a b c = SemiDrei a
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试(编译)是
instance Bifunctor (SemiDrei a) where
bimap f g (SemiDrei a) = SemiDrei a
Run Code Online (Sandbox Code Playgroud)
但是,看着它,在我看来我应该应该能够写,bimap f g = id因为最后一个参数不变或写了bimap f g x = x。两者都给我带来了编译错误,并且我希望有人可以向我解释为什么我不能bimap用这些较短的替代方法来表达,即为什么我必须指定(SemiDrei a)。
我在Haskell 8.6.5上运行了此命令(如果相关)
尝试:ID
instance Bifunctor (SemiDrei a) where
bimap f g = id
-- compile error message:
• Couldn't match type ‘a1’ with ‘b’
‘a1’ is a rigid type variable bound …Run Code Online (Sandbox Code Playgroud)