为什么“无任何回报”没有回报? [英] Why does "return Nothing" return Nothing?

查看:151
本文介绍了为什么“无任何回报”没有回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

return a应该包含在某些Monad的上下文中:

  * Main> :我返回
class Applicative m => Monad(m :: * - > *)其中
...
return :: a - > ma
...
- 定义于'GHC.Base'

如果我问GHCI什么类型的无返回,它符合:

  * Main> :t返回Nothing 
return Nothing :: Monad m => m(也许a)

但是如果我评估它,我看不到外层Monad, :

  * Main>返回Nothing 
Nothing


解决方案

当GHCi进入打印一个值,它会尝试两种不同的东西。首先,它尝试将 IO a 的类型与 a 一致。如果可以,那么它会执行IO操作并尝试打印结果。如果它不能这样做,它会尝试打印给定的值。在你的情况下, Monad m => m(也许a)可以与 IO(也许a)



回顾GHCi会议可能会有所帮助:

  Prelude> return Nothing 
Nothing
Prelude> return Nothing :: IO(也许a)
Nothing
Prelude> return Nothing :: Maybe(也许a)
只是没有
Prelude> Nothing
Nothing


"return a" is supposed to wrap a in the context of some Monad:

*Main> :i return
class Applicative m => Monad (m :: * -> *) where
  ...
  return :: a -> m a
  ...
        -- Defined in ‘GHC.Base’

If I ask GHCI what the type of "return Nothing" is, it conforms to that:

*Main> :t return Nothing
return Nothing :: Monad m => m (Maybe a)

But if I evaluate it, I see no outer Monad, just the inner Maybe:

*Main>  return Nothing
Nothing

解决方案

When GHCi goes to print a value, it tries two different things. First, it tries to unify the type with IO a for some a. If it can do that then it executes the IO action and tries to print the result. If it can't do that, it tries to print the given value. In your case, Monad m => m (Maybe a) can be unified with IO (Maybe a).

Reviewing this GHCi session might help:

Prelude> return Nothing
Nothing
Prelude> return Nothing :: IO (Maybe a)
Nothing
Prelude> return Nothing :: Maybe (Maybe a)
Just Nothing
Prelude> Nothing
Nothing

这篇关于为什么“无任何回报”没有回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆