Haskell错误:无法';t匹配预期类型‘;服务器部件IO a0和x2019;实际类型为';[答复]';
当我试图编译代码时,出现了两个错误 第一个是:Haskell错误:无法';t匹配预期类型‘;服务器部件IO a0和x2019;实际类型为';[答复]';,haskell,happstack,Haskell,Happstack,当我试图编译代码时,出现了两个错误 第一个是: 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
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] }
它提到了三段代码,哪一段
第二个是:
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]
我也不太确定错误的位置
这两个错误似乎有着完全相反的含义。我现在很困惑。有人能解释一下吗?谢谢
原始代码如下:
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
]
我猜问题在于do块中的第二条语句是
msum(…)++[…]
,它被解析为(msum[…])++[…]
。因此,编译器看到++
并推断这是列表monad中的一条语句。但是基于代码的其余部分,do块应该使用ServerPartT IO
monad。这就是为什么您会收到第一条错误消息,ServerPartT IO a0
和“[Response]
不匹配
要解决此问题,请尝试添加更多括号:
msum ((...) ++ [...])
或其他美元运营商:
msum $ (...) ++ [...]
第二条错误消息是相同问题的另一个结果。由于编译器推断语句在列表单子中,因此它假定msum
也属于列表单子。因此msum的参数应该是list monad(=列表列表)中的操作列表,但它是ServerPartT IO
monad中的操作列表。当您添加缺少的括号,并且编译器看到msum
应该来自ServerPartT IO
monad时,我希望这个错误会消失
应在您复制的部件上方的编译器输出中提供错误位置。应该有一个文件名,然后是冒号,然后是行号。编译器还报告与错误相关的源代码片段。请注意,例如,第一条错误消息没有三段不相关的源代码,但第一段是第二段的一部分,第二段是第三段的一部分。因此,编译器首先向您显示带有错误的片段,然后再显示越来越大的片段以提供更多的上下文。我只看了您的代码一眼,但是-我怀疑
映射中有一些不正确的父项设置
尝试使用显式paren而不是$
,看看您是否有进一步的改进。我还要说,这不是一个最小的“不工作”的例子-尝试删除所有不改变错误消息的内容。
msum $ (...) ++ [...]