【发布时间】:2017-06-17 09:28:48
【问题描述】:
大家早安/晚安! 我有类型:
data FixItem m a = KeepItem|SkipItem|FixItem (m (a -> a))
fixItem f = FixItem $ pure f
我想写函数mapFix :: (a -> b) -> FixItem m a -> FixItem m b。当我尝试时:
mapFix f SkipItem = SkipItem -- good
mapFix f KeepItem = fixItem f -- error "rigid type"!!!
mapFix f (FixItem mf) = FixItem $ pure (.) <*> (pure f) <*> mf -- too!
所以,我得到错误:
• Couldn't match type ‘b’ with ‘a’
‘b’ is a rigid type variable bound by
the type signature for:
mapFix :: forall (m :: * -> *) a b.
Applicative m =>
(a -> b) -> FixItem m a -> FixItem m b
at src/test.hs:235:11
‘a’ is a rigid type variable bound by
the type signature for:
mapFix :: forall (m :: * -> *) a b.
Applicative m =>
(a -> b) -> FixItem m a -> FixItem m b
at src/test.hs:235:11
Expected type: b -> b
Actual type: a -> b
• In the first argument of ‘fixItem’, namely ‘f’
In the expression: fixItem f
In an equation for ‘mapFix’: mapFix f KeepItem = fixItem f
• Relevant bindings include
f :: a -> b (bound at src/test.hs:236:8)
mapFix :: (a -> b) -> FixItem m a -> FixItem m b
(bound at src/test.hs:236:1)
如何为这种类型编写 mapFix 或实现 Functor 实例(FixItem 将a 修复为a,而不是b,即修复为a -> a,而不是a -> b)?
【问题讨论】:
标签: haskell