如何从monadic动作中提取价值 [英] How to extract value from monadic action
问题描述
是否有内置函数,其签名 ::(Monad m)=> m a - > a
?
Is there a built-in function with signature :: (Monad m) => m a -> a
?
Hoogle告诉我们没有这样的功能。
Hoogle tells that there is no such function.
你能解释一下为什么吗?
Can you explain why?
推荐答案
函数:
A monad only supplies two functions:
return :: Monad m => a -> m a
(>>=) :: Monad m => m a -> (a -> m b) -> m b
这两个都返回类型为 ma
,因此无法以任何方式组合这些函数来获取类型为 Monad m =>的函数。 m a - >一个
。要做到这一点,你需要的不仅仅是这两个函数,所以你需要知道更多关于 m
的知识,而不是它的单子。
Both of these return something of type m a
, so there is no way to combine these in any way to get a function of type Monad m => m a -> a
. To do that, you'll need more than these two functions, so you need to know more about m
than that it's a monad.
例如, Identity
monad具有 runIdentity :: Identity a - >一个
和几个monad具有相似的功能,但是没有办法一般地提供它。事实上,无法从monad中逃离对像 IO
这样的单子来说至关重要。
For example, the Identity
monad has runIdentity :: Identity a -> a
, and several monads have similar functions, but there is no way to provide it generically. In fact, the inability to "escape" from the monad is essential for monads like IO
.
这篇关于如何从monadic动作中提取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!