【发布时间】:2012-08-21 13:45:18
【问题描述】:
Here'sEither a 的标准 Functor 实例:
instance Functor (Either a) where
fmap _ (Left x) = Left x
fmap f (Right y) = Right (f y)
在加载到 GHCi 时添加 as-pattern 会导致编译错误:
instance Functor (Either a) where
fmap _ z@(Left x) = z -- <-- here's the as-pattern
fmap f (Right y) = Right (f y)
Couldn't match expected type `b' against inferred type `a1'
`b' is a rigid type variable bound by
the type signature for `fmap' at <no location info>
`a1' is a rigid type variable bound by
the type signature for `fmap' at <no location info>
Expected type: Either a b
Inferred type: Either a a1
In the expression: z
In the definition of `fmap': fmap _ (z@(Left x)) = z
为什么这不起作用?
【问题讨论】:
-
Either x a和Either x b都有名为L x的元素,所以fmap f (L x) = L x有效。但是左边的a@(L x)表示a被标识为Either x a类型的东西。但是右边需要的是Either x b类型的东西
标签: haskell types as-pattern