gpt4 book ai didi

haskell - Haskell 如何在 MaybeT 实现中推断出正确的类型类?

转载 作者:行者123 更新时间:2023-12-02 13:57:48 27 4
gpt4 key购买 nike

Haskell 如何知道每个 return 表达式哪个是正确的 monad 实例?

newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) }

instance Monad m => Monad (MaybeT m) where
return = MaybeT . return . return

最佳答案

从上下文来看,它实际上是明确的。

让我们玩一下类型检查器,

-- From the signature
MaybeT . return . return :: a -> MaybeT a
-- From the type of MaybeT
return . return :: a -> m (Maybe a)
-- From the type of `.`
(return :: Maybe a -> m a) . (return :: a -> Maybe a)

一旦我们知道了每个return的类型,“实例选择算法”就会正确选择第一个使用ms return第二个是也许

关于haskell - Haskell 如何在 MaybeT 实现中推断出正确的类型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com