【发布时间】:2020-09-10 04:29:02
【问题描述】:
在 Haskell 中描述 adjunction 很容易:
class Functor f where
map :: (a -> b) -> f a -> f b
class Functor m => Monad m where
return :: a -> m a
join :: m (m a) -> m a
class Functor w => Comonad w where
extract :: w a -> a
duplicate :: w a -> w (w a)
class (Comonad w, Monad m) => Adjoint w m | w -> m, m -> w where
unit :: a -> m (w a)
counit :: w (m a) -> a
但是,我很难证明 StoreT s w 和 StateT s m 是伴随的。
instance Adjoint w m => Adjoint (StoreT s w) (StateT s m) where
-- ...
GHC 抱怨m 不能被StoreT s w 确定,因为m 没有出现在StoreT s w 中:
• Illegal instance declaration for
‘Adjoint (StoreT s w) (StateT s m)’
The coverage condition fails in class ‘Adjoint’
for functional dependency: ‘w -> m’
Reason: lhs type ‘StoreT s w’
does not determine rhs type ‘StateT s m’
Un-determined variable: m
Using UndecidableInstances might help
• In the instance declaration for
‘Adjoint (StoreT s w) (StateT s m)’
我真的不明白为什么这是一个问题,因为 w -> m by Adjoint w m。 GHC 进一步建议开启-XUndecidableInstances。在这种情况下这样做是否安全?我在尝试做一些不可能的事情吗?
【问题讨论】:
-
我已经添加了完整的错误消息,因为提及覆盖条件说明了发生了什么。如果该消息与您收到的消息有所不同,请告诉我和/或编辑它。
-
谢谢,它与我收到的错误消息相符。
标签: haskell functional-programming monads functor functional-dependencies