为什么monad不能被分解? [英] Why can't a monad be decomposed?
问题描述
我不明白为什么我不能分解一个(比方说IO)monad。像 IO a - >> a
?
我的问题源于使用happstack并希望获取 Text
out ServerPart(也许文本)
,它由(可选的$ lookTextdomain)
返回。
然后我记得读了 IO
monad无法转义。
我读过关于 unsafePerformIO
的原因以及它不好的原因,但这些原因似乎都没有答案我的问题。
可以从<?c $ c> Monad h1>
是的。这对于许多 Monad
s非常简单,例如可能
,或者
, State
, Identity
等等。其中一个最常见的 Monad
是从 Monad
:( - >)r
。如果无法将函数转换为值,那么Haskell就没有多少用处了。
可以 IO code>可以逃脱?
不幸的是,是的。对于初学者来说,如果他们没有谷歌,并且看到他们可以从 IO
使用 unsafePerformIO
,正如你可能猜到的那样不安全。这是不是意味着在普通代码中使用,而是当你真正需要它的时候进入运行时系统的后门。主要用于实现一些较低级别的库,如 Vector
,但也用于与外部共享库(DLL)进行接口。 如果你不写这种代码,不要使用 unsafePerformIO
。否则,最终的代码将难以推理和维护,因为它会绕过类型系统。
我们如何从其他 Monad
s?
它从 Monad
到 Monad
,但大多数monad转换器都有 run -
, eval -
或 exec -
方法:
> :m Control.Monad.State
> runState(modify(* 10)>> get>> = return。show)1
(10,10)
> :runState(modify(* 10)>> get>> = return。show)1
runState(modify(* 10)>> get>> = return。show)1 ::(String,Int)
> evalState(modify(* 10)>> get>> = return。show)1
10
> execState(modify(* 10)>> get>> = return。show)1
10
也许
Monad
有多种逃避方法:
> :m Data.Maybe
>也许nada节目(只是2)
2
>也许nada显示Nothing
nada
> fromMaybe 1(Just 10)
10
> fromMaybe 1 Nothing
1
> fromJust(Just 1)
1
> fromJust Nothing
***例外:Maybe.fromJust:Nothing
如你所见,不是所有的人都可以安全使用。
这与Happstack有什么关系?
我不知道,我没有使用Happstack足以知道它。但是,通过快速搜索,我在他们的网站上这个示例,它的外观很适合你的情况。
I don't understand why I can't decompose a (let's say IO) monad. Like IO a -> a
?
My question originated when using happstack and wanting to get the Text
out of ServerPart (Maybe Text)
which is returned by (optional $ lookText "domain")
.
Then I remembered reading that IO
monad can't be escaped.
I've read about unsafePerformIO
and the reasons why it is bad, but none of those reasons seem to answer my question.
Can Monad
s be escaped from?
Yes. This is very easy with many Monad
s, such as Maybe
, Either a
, State
, Identity
, and more. One of the most common Monad
s that is escaped from is the function Monad
: (->) r
. If it weren't possible to turn a function into a value, then Haskell wouldn't have much going for it.
Can IO
be escaped from?
Unfortunately, yes. It would be a lot better for beginners if they didn't google around and see that they can technically escape from IO
using unsafePerformIO
, which as you might have guessed is not safe. It is not meant to be used in normal code, but is rather a backdoor into the runtime system for when you really need it. Primarily, it's used for implementing some lower level libraries like Vector
, but also for interfacing with external shared libraries (DLLs). If you're not writing that kind of code, don't use unsafePerformIO
. Otherwise, you will end up with code that becomes difficult to reason about and maintain because it bypasses the type system.
How do we escape from other Monad
s?
It varies from Monad
to Monad
, but most monad transformers have run-
, eval-
or exec-
methods:
> :m Control.Monad.State
> runState (modify (*10) >> get >>= return . show) 1
("10", 10)
> :type runState (modify (*10) >> get >>= return . show) 1
runState (modify (*10) >> get >>= return . show) 1 :: (String, Int)
> evalState (modify (*10) >> get >>= return . show) 1
"10"
> execState (modify (*10) >> get >>= return . show) 1
10
The Maybe
Monad
has several ways to escape from it:
> :m Data.Maybe
> maybe "nada" show (Just 2)
"2"
> maybe "nada" show Nothing
"nada"
> fromMaybe 1 (Just 10)
10
> fromMaybe 1 Nothing
1
> fromJust (Just 1)
1
> fromJust Nothing
*** Exception: Maybe.fromJust: Nothing
So as you can see, not all of them are safe to use either.
What does this have to do with Happstack?
I don't know, I haven't used Happstack enough to know about it. However, a quick search led me to this example on their website, which looks pretty applicable to your situation.
这篇关于为什么monad不能被分解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!