- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
查看 MaybeT
:
λ: import Monad.Trans
λ: import Monad.Trans.Maybe
λ: :t MaybeT
MaybeT :: m (Maybe a) -> MaybeT m a
在 MaybeT
的签名中,m
可以是任何更高级的类型,即 * -> *
吗?
我正在尝试学习 Monad Transformers,所以我很好奇为什么 m
没有 Monad
的约束。
最佳答案
Another example of a parameterized type that we've already met is
Map k v
fromData.Map
. Thek
is the type of the keys in a map and thev
is the type of the values. This is a good example of where type parameters are very useful. Having maps parameterized enables us to have mappings from any type to any other type, as long as the type of the key is part of theOrd
type class. If we were defining a mapping type, we could add a type class constraint in the data declaration:data (Ord k) => Map k v = ...
However, it is a very strong convention in Haskell to never add type class constraints in data declarations. Why? Well, because we don't benefit a lot, but we end up writing more class constraints, even when we don't need them. If we put or don't put the
Ord k
constraint in the data declaration forMap k v
, we're going to have to put the constraint into functions that assume the keys in a map can be ordered. But if we don't put the constraint in the data declaration, we don't have to put(Ord k) =>
in the type declarations of functions that don't care whether the keys can be ordered or not. An example of such a function istoList
, that just takes a mapping and converts it to an associative list. It's type signature istoList :: Map k a -> [(k, a)]
. IfMap k v
had a type constraint in its data declaration, the type fortoList
would have to betoList :: Ord k => Map k a -> [(k, a)]
, even though the function doesn't do any comparing of keys by order.So don't put type constraints into data declarations even if it seems to make sense, because you'll have to put them into the function type declarations either way.
我希望这能回答您的问题。虽然 MaybeT m a
中的 m
是不受约束的,但有一个隐含的假设,即 m
是 Monad
的一个实例。事实上,如果 m
不是 Monad
的实例,那么您将无法使用 MaybeT m a
值做很多事情,因为大多数函数将要求 m
是 Monad
的一个实例。
关于haskell - MaybeT 的 m 在类型签名中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814151/
我正在尝试了解 monad 转换器,并且正在阅读 this page在 haskell wiki 上。 我感到困惑的代码如下: isValid :: String -> Bool isValid s
我试图通过在 ghci 中运行一些示例来理解 MaybeT: λ: import Control.Monad.Trans.Maybe λ: let x = return $ 42 :: MaybeT
在 IO 计算中工作,我最终得到了 case mbValue of … 的阶梯。 s 并发现我应该使用 Maybe monad 来简化代码。因为它在 IO 内计算,我需要得到 IO值,我使用了 May
我有一个程序。 import Control.Monad import Control.Monad.Identity import Control.Monad.Trans.Maybe import S
我有一个使用 Control.Monad.Random 的简单函数来创建一个可以对随机数进行采样的函数。 import Control.Monad.Random import Data.R
idris 有没有MaybeT来自 Haskell 还是我应该使用其他东西?我正在尝试使用 IO (Maybe a) 类型的许多值进行计算.怎么能结合Maybe和 IO到 Idris 中的单个 mon
在https://en.wikibooks.org/wiki/Haskell/Monad_transformers , 我明白了 newtype MaybeT m a = MaybeT { runMa
Haskell 如何知道每个 return 表达式哪个是正确的 monad 实例? newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } i
查看 MaybeT: λ: import Monad.Trans λ: import Monad.Trans.Maybe λ: :t MaybeT MaybeT :: m (Maybe a) -> M
在https://en.wikibooks.org/wiki/Haskell/Monad_transformers , 我明白了 newtype MaybeT m a = MaybeT { runMa
这是一个与 Why doesn't Option have a fold method? 类似的问题,但对于函数式java。 如果选项为 None,我想执行一些副作用。除了if maybeT.isNo
重新访问我的 MaybeT 练习后,我收到警告说我也应该有 Applicative 实例。我试图实现它,但因为我找不到使用 Applicative m 将 m x 应用到 m f 的方法而陷入困境。我
对于我之前关于链接失败的问题,Michael Snoyman 建议我使用 MaybeT运行它们,所以如果它们中的任何一个失败,它就会短路到 Nothing . 我的印象是runDb在事务中运行所有内容
查看 Monad 的 MaybeT 实例中 fail 的源代码: instance (Monad m) => Monad (MaybeT m) where fail _ = MaybeT (r
具体来说,假设我有这个 monadT 堆栈: type MHeap e ret = MaybeT ( StateT [e] Identity ) ret 和一个方便的 runMheap 函数: run
我一直在使用Haxl monad(在此处描述:http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_
如果我为 Identity 定义单子(monad)变压器类型,可以推导出Show实例。 newtype IdentityT f a = IdentityT { runIdentityT :: f
说我有一些foo :: Maybe Int例如,我想将它与 bar :: Int -> MaybeT (Writer String) Int 绑定(bind),这样做的惯用方法是什么? 我可以定义自己
简短版本:当我使用 runMaybeT 时然后runState在 MaybeT (State ) () 类型的单子(monad)上,即使 Maybe 看起来也没有发生状态变化。结果等于Just ()
我正在尝试 MaybeT monad,特别是 MaybeT Identity String import Control.Monad.Trans.Maybe import Control.Monad.
我是一名优秀的程序员,十分优秀!