简体   繁体   English

Haskell错误:无法将预期类型'ServerPartT IO a0'与实际类型'[Response]'相匹配

[英]Haskell error: couldn't match expected type ‘ServerPartT IO a0’ with actual type '[Response]'

When I tried to compile the code, two errors occur. 当我尝试编译代码时,会发生两个错误。

the first one is: 第一个是:

Couldn't match expected type ‘ServerPartT IO a0’
            with actual type ‘[Response]’
In a stmt of a 'do' block:
  msum
    (map (\ (a, b) -> dir a b)
     $ routes
         staticDir
         redirectUrlGraphEmail
         redirectUrlGraphPost
         aboutContents
         privacyContents)
  ++
    [do { nullDir;
          seeOther "graph" (toResponse "Redirecting to /graph") },
     notFoundResponse]
In the second argument of ‘($)’, namely
  ‘do { decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096);
        msum
          (map (\ (a, b) -> dir a b)
           $ routes
               staticDir
               redirectUrlGraphEmail
               redirectUrlGraphPost
               aboutContents
               privacyContents)
        ++
          [do { nullDir;
                .... },
           notFoundResponse] }’
In a stmt of a 'do' block:
  simpleHTTP serverConf
  $ do { decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096);
         msum
           (map (\ (a, b) -> dir a b)
            $ routes
                staticDir
                redirectUrlGraphEmail
                redirectUrlGraphPost
                aboutContents
                privacyContents)
         ++
           [do { nullDir;
                 .... },
            notFoundResponse] }

It mentioned three chunks of code, which one 它提到了三个代码块,其中一个

the second one is: 第二个是:

    Couldn't match type ‘ServerPartT IO’ with ‘[]’
Expected type: [[Response]]
  Actual type: [ServerPartT IO Response]
In the first argument of ‘msum’, namely
  ‘(map (\ (a, b) -> dir a b)
    $ routes
        staticDir
        redirectUrlGraphEmail
        redirectUrlGraphPost
        aboutContents
        privacyContents)’
In the first argument of ‘(++)’, namely
  ‘msum
     (map (\ (a, b) -> dir a b)
      $ routes
          staticDir
          redirectUrlGraphEmail
          redirectUrlGraphPost
          aboutContents
          privacyContents)’
In a stmt of a 'do' block:
  msum
    (map (\ (a, b) -> dir a b)
     $ routes
         staticDir
         redirectUrlGraphEmail
         redirectUrlGraphPost
         aboutContents
         privacyContents)
  ++
    [do { nullDir;
          seeOther "graph" (toResponse "Redirecting to /graph") },
     notFoundResponse]

I am also not quite sure about the location of the error. 我也不太确定错误的位置。

It seems like these two errors have totally opposite meanings. 似乎这两个错误的含义完全相反。 I am confused now. 我现在很困惑。 Can anyone help to explain this? 有人可以帮忙解释一下吗? Thanks! 谢谢!

The original code is here: 原始代码在这里:

runServer :: IO ()
runServer = do
configureLogger
staticDir <- getStaticDir
redirectUrlGraphEmail <- retrieveAuthURL testUrl
redirectUrlGraphPost <- retrieveAuthURL testPostUrl
aboutContents <- LazyIO.readFile $ markdownPath ++ "README.md" 
privacyContents <- LazyIO.readFile $ markdownPath ++ "PRIVACY.md"

-- Start the HTTP server
simpleHTTP serverConf $ do
  decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
  msum  
       (map (\ (a, b) -> dir a b) $ routes staticDir redirectUrlGraphEmail redirectUrlGraphPost aboutContents privacyContents ) ++  
       [ do
          nullDir
          seeOther "graph" (toResponse "Redirecting to /graph"),    
          notFoundResponse
    ]

where routes is in another module: routes在另一个模块中的位置:

routes :: [Char] -> T.Text -> T.Text -> Text -> Text -> [ (String, ServerPart Response)]
routes staticDir redirectUrlGraphEmail redirectUrlGraphPost aboutContents privacyContents = [
("grid", gridResponse),
("graph", graphResponse),
("image", graphImageResponse),
...
]

I guess the problem is that the second statement in your do block is msum (...) ++ [...] which is parsed as (msum [...]) ++ [...] . 我想问题是您的do块中的第二条语句是msum (...) ++ [...] ,被解析为(msum [...]) ++ [...] So the the compiler sees the ++ and infers that this is a statement in the list monad. 因此,编译器会看到++并推断这是列表monad中的一条语句。 But based on the rest of the code, the do block should use the ServerPartT IO monad. 但是根据其余代码,do块应使用ServerPartT IO monad。 That's why you get the first error message that ServerPartT IO a0 and '[Response] don't match. 这就是为什么您收到第一条错误消息,指出ServerPartT IO a0'[Response]不匹配。

To fix this , try putting more parentheses: 要解决此问题 ,请尝试添加更多的括号:

msum ((...) ++ [...])

Or another dollar operator: 或其他美元运营商:

msum $ (...) ++ [...]

The second error message is another result of the same problem. 第二条错误消息是相同问题的另一个结果。 Since the compiler infers that the statement is in the list monad, it assumes that the msum belongs to the list monad, too. 由于编译器推断该语句在列表monad中,因此它假定msum属于列表monad。 So the argument of msum should be a list of actions in the list monad (= a list of lists), but it is a list of actions in the ServerPartT IO monad. 因此,msum的参数应该是list monad中的一个动作列表(=列表列表),但是它是ServerPartT IO monad中的一个动作列表。 I expect that this error goes away when you add the missing parentheses and the compiler sees that the msum should be from the ServerPartT IO monad. 我希望当您添加缺少的括号时该错误会消失,并且编译器会看到msum应该来自ServerPartT IO monad。

The error location should be provided in the compiler output above the part you copied. 错误位置应在复制部件上方的编译器输出中提供。 There should be a filename, then a colon, then a line number. 应该有一个文件名,然后是冒号,然后是行号。 The compiler also reports the pieces of source code that are relevant to the error. 编译器还会报告与错误相关的源代码。 Note that, for example, the first error message doesn't have three unrelated pieces of source code, but that the first piece is a part of the second piece, and the second piece is a part of the third piece. 请注意,例如,第一个错误消息没有三个不相关的源代码段,但是第一段是第二段的一部分,第二段是第三段的一部分。 So the compiler starts by showing you the piece with the error, and then larger and larger pieces to provide more context. 因此,编译器首先向您显示有错误的片段,然后再为越来越大的片段提供更多的上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Haskell IO:无法将预期类型“IO a0”与实际类型匹配 - Haskell IO: Couldn't match expected type `IO a0' with actual type Haskell“无法将预期类型“ a”与实际类型“ [a0]”匹配” - Haskell “Couldn't match expected type ‘a’ with actual type ‘[a0]’” 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () Haskell中的软件事务存储器:无法将预期的STM a0类型与实际的IO类型进行匹配() - Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO () 有人可以解释此类型错误“无法将期望的类型&#39;IO()&#39;与实际类型&#39;a0-&gt; c0&#39;匹配” - could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ” 无法将预期的类型“ IO()”与实际类型“ a0-&gt; m0 a0”匹配 - Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0' 无法将预期的类型“ IO a0”与实际类型“ a1-&gt; IO()”匹配 - Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’ [HASKELL]无法将预期类型`[a0]&#39;与实际类型`[a1] - &gt; Bool&#39;匹配 - [HASKELL]Couldn't match expected type `[a0]' with actual type `[a1] -> Bool' 无法匹配预期类型`[([Char],a0)]&#39;与实际类型`([Char],t0)&#39;Haskell - Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell Haskell编程分配,“无法将期望的类型&#39;Int&#39;与实际类型&#39;[a0]-&gt; Int&#39;匹配”以及其他一些错误 - Haskell Programming Assignment, “Couldn't match expected type ‘Int’ with actual type ‘[a0] -> Int’ ”and a few more Errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM