Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何包装?_Haskell - Fatal编程技术网

Haskell 如何包装?

Haskell 如何包装?,haskell,Haskell,我有Haskell手册中的以下代码片段,逐步展示monad transformer将如何展开: module OuterInner where import Control.Monad.Trans.Except import Control.Monad.Trans.Maybe import Control.Monad.Trans.Reader -- We only need to use return once -- because it's one big Monad

我有Haskell手册中的以下代码片段,逐步展示monad transformer将如何展开:

module OuterInner where

  import Control.Monad.Trans.Except
  import Control.Monad.Trans.Maybe
  import Control.Monad.Trans.Reader

  -- We only need to use return once
  -- because it's one big Monad
  embedded :: MaybeT (ExceptT String (ReaderT () IO)) Int
  embedded = return 1

  maybeUnwrap :: ExceptT String (ReaderT () IO) (Maybe Int)
  maybeUnwrap = runMaybeT embedded

  -- Next
  eitherUnwrap :: ReaderT () IO (Either String (Maybe Int))
  eitherUnwrap = runExceptT maybeUnwrap

  -- Lastly
  readerUnwrap :: () -> IO (Either String (Maybe Int))
  readerUnwrap = runReaderT eitherUnwrap
有一个练习,我必须再次包装所有东西:

embedded :: MaybeT (ExceptT String (ReaderT () IO)) Int
embedded = ??? (const (Right (Just 1))) 
我做了如下尝试:

embedded' :: MaybeT (ExceptT String (ReaderT () IO)) Int
embedded' = MaybeT (ExceptT (ReaderT (const (Right (Just 1)))))
但编译器抱怨:

D:\haskell\chapter26\src\OuterInner.hs:24:15: error:
    * Couldn't match type `Either a0' with `IO'
      Expected type: MaybeT (ExceptT String (ReaderT () IO)) Int
        Actual type: MaybeT (ExceptT String (ReaderT () (Either a0))) Int
    * In the expression:
        MaybeT (ExceptT (ReaderT (const (Right (Just 1)))))
      In an equation for embedded':
          embedded' = MaybeT (ExceptT (ReaderT (const (Right (Just 1)))))

D:\haskell\chapter26\src\OuterInner.hs:24:32: error:
    * Couldn't match type `Maybe Integer'
                     with `Either String (Maybe Int)'
      Expected type: ReaderT () (Either a0) (Either String (Maybe Int))
        Actual type: ReaderT () (Either a0) (Maybe Integer)
    * In the first argument of `ExceptT', namely
        `(ReaderT (const (Right (Just 1))))'
      In the first argument of `MaybeT', namely
        `(ExceptT (ReaderT (const (Right (Just 1)))))'
      In the expression:
        MaybeT (ExceptT (ReaderT (const (Right (Just 1)))))
Failed, modules loaded: none.
如何解决此练习?

您已定义了除字符串读取器Int之外的MaybeT。您错过了最内层IO。与其他类型不同,IO没有具体的表示形式,但您当然可以使用return或pure duh将纯值注入其中

您已经定义了除字符串读取器Int之外的MaybeT。您错过了最内层IO。与其他类型不同,IO没有具体的表示形式,但您当然可以使用return或pure duh将纯值注入其中


GHC型孔扩展到救援!它可以让你在表达式中的某个地方找到a u,并让GHC告诉你需要的类型。那么,让我们从

:t MaybeT (ExceptT (ReaderT (const _)))

<interactive>:1:33: error:
    * Found hole: _ :: m (Either e (Maybe a))

GHC型孔扩展到救援!它可以让你在表达式中的某个地方找到a u,并让GHC告诉你需要的类型。那么,让我们从

:t MaybeT (ExceptT (ReaderT (const _)))

<interactive>:1:33: error:
    * Found hole: _ :: m (Either e (Maybe a))

这对救援意味着什么?嗯。就像奇普戴尔。来帮你的。在表达式中的任意位置放置一个u。根据规范,它是一个无效的标识符,因此无论如何它都是一个错误。GHC可以自由地推断它的类型,并在导致该错误之前打印该类型。你建立了一个表达式,把uu放在你不确定的地方,然后你在某种程度上,让类型写代码,在一个模糊的意义上。这意味着什么GHC类型的孔扩展到救援?嗯。就像奇普戴尔。来帮你的。在表达式中的任意位置放置一个u。根据规范,它是一个无效的标识符,因此无论如何它都是一个错误。GHC可以自由地推断它的类型,并在导致该错误之前打印该类型。你建立了一个表达式,把uu放在你不确定的地方,然后你在某种程度上,让类型在模糊的意义上写代码。
embedded = MaybeT (ExceptT (ReaderT (const (return (Right (Just 1))))))