如何从 monadic action 中提取价值 [英] How to extract value from monadic action
问题描述
是否有带有签名的内置函数:: (Monad m) =>m a ->一个
?
Is there a built-in function with signature :: (Monad m) => m a -> a
?
Hoogle 说没有这样的功能.
Hoogle tells that there is no such function.
你能解释一下原因吗?
推荐答案
monad 只提供两个功能:
A monad only supplies two functions:
return :: Monad m => a -> m a
(>>=) :: Monad m => m a -> (a -> m b) -> m b
这两个都返回 m a
类型的东西,所以没有办法以任何方式组合它们来获得 Monad m => 类型的函数.m a ->一个
.要做到这一点,你需要的不仅仅是这两个函数,所以你需要更多地了解 m
而不是它是一个 monad.
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都有类似的功能,但是没有办法通用.事实上,对于像 IO
这样的 monad 来说,无法从 monad 中逃脱"是必不可少的.
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 action 中提取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!