[英]could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ”
Here's the type error I am getting, followed by the code in question. 这是我遇到的类型错误,后面是有问题的代码。 This may be due to an incorrect use of function composition, if so I'd like an explanation as to how I can fix that.
这可能是由于对函数组合的错误使用,如果是的话,我想对如何解决此问题进行解释。 If it's something else, I'd like to talk about that too.
如果还有其他事情,我也想谈一谈。
frameworkDaemon.lhs:125:5:
Couldn't match expected type `IO ()' with actual type `a0 -> c0'
In the expression: popJobState . popProcessConfig . readJobFile
In an equation for `makeJobState':
makeJobState world
= popJobState . popProcessConfig . readJobFile
where
readJobFile
= do { let ...;
.... }
where
getFileContents (x : xs) = readFile (ixiaRoot ++ "/" ++ (show x))
popProcessConfig = undefined
popJobState = undefined
Failed, modules loaded: none. 失败,模块已加载:无。
makeJobState :: JobState -> IO ()
makeJobState world =
popJobState . popProcessConfig . readJobFile -- f .g . h -> f(g(h))
where readJobFile = do
let directoryPath = ixiaRoot ++ "/jobs"
processedPath = map (directoryPath </>) . filter (`notElem` [".",".."])
jobFileNames <- processedPath <$> getDirectoryContents directoryPath
let numStrings = map (last . splitOn "/") jobFileNames
sortJobNumbers = map read numStrings :: [Int]
getFileContents $ sort $ sortJobNumbers
where getFileContents (x:xs) = readFile (ixiaRoot ++ "/" ++ (show x))
popProcessConfig = undefined
popJobState = undefined
-- f .g . h -> f(g(h))
f . g . h
f . g . h
is not equivalent to f(g(h))
, it's equivalent to \\x -> f(g(hx))
, so h
needs to be a function, but readJobFile
(which is the h
in your example) is an IO ()
. f . g . h
不等于f(g(h))
,它等效于\\x -> f(g(hx))
,因此h
需要是一个函数,但readJobFile
(在您的示例中为h
)是一个IO ()
。 That's why the error message complains about expecting a function where you gave it an IO ()
. 这就是为什么错误消息抱怨期望在给它一个
IO ()
的函数的原因。
As sepp2k said, fgh
is a function, \\x -> f $ g $ hx
. 正如sepp2k所说,
fgh
是一个函数, \\x -> f $ g $ hx
。 It seems that readJobFile
is IO String
, though, not IO ()
. 似乎
readJobFile
是IO String
,而不是IO ()
。
But it doesn't complain about expecting a function. 但是它并没有抱怨期望功能。 It explains about having inferred a function, and having expected an
IO ()
. 它说明了有关推断功能以及预期的
IO ()
。 It's inferred a function because your chain of compositions is just that---a function, so the result of applying makeJobState
to a JobState
value is (or would be) a function, not an IO action. 之所以将其推断为函数,是因为您的构成链只是一个函数,因此将
makeJobState
应用于JobState
值的结果是(或将是)一个函数,而不是IO操作。
It seems as if what you want isn't function composition but monadic composition, something like makeJobState world = readJobFile >>= popProcessConfig >>= popJobState
(with the world
parameter actually being used at some point, though it's not really obvious to me where it should go. And that might not be the preferred order; it's hard to tell since the other two variables are undefined.) Since readJobFile
is IO String
, you'd want popProcessConfig
to be String -> IO a
and popJobState
to be a -> IO ()
. 似乎您想要的不是函数组合而是单
makeJobState world = readJobFile >>= popProcessConfig >>= popJobState
组合,例如makeJobState world = readJobFile >>= popProcessConfig >>= popJobState
(虽然有时对world
参数确实makeJobState world = readJobFile >>= popProcessConfig >>= popJobState
,但对我而言并不明显)这可能不是首选顺序;由于其他两个变量未定义,因此很难说。)由于readJobFile
是IO String
,因此您希望popProcessConfig
为popProcessConfig
String -> IO a
和popJobState
为a -> IO ()
。 That would result in an overall type of JobState -> IO ()
. 这将导致
JobState -> IO ()
的整体类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.