简体   繁体   English

有人可以解释此类型错误“无法将期望的类型'IO()'与实际类型'a0-> c0'匹配”

[英]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 () . 似乎readJobFileIO 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 ,但对我而言并不明显)这可能不是首选顺序;由于其他两个变量未定义,因此很难说。)由于readJobFileIO String ,因此您希望popProcessConfigpopProcessConfig String -> IO apopJobStatea -> IO () That would result in an overall type of JobState -> IO () . 这将导致JobState -> IO ()的整体类型。

暂无
暂无

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

相关问题 无法将预期的类型“ Double”与实际类型“ [c0]”匹配 - Couldn't match expected type `Double' with actual type `[c0]' 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () 无法将预期类型 &#39;(a0, b0, c0, Geometry -&gt; b)&#39; 与实际类型 &#39;Geometry -&gt; (Int, Int, Int, Int) 匹配 - Couldn't match expected type ‘(a0, b0, c0, Geometry -> b)’ with actual type ‘Geometry -> (Int, Int, Int, Int) Haskell错误:无法将预期类型&#39;ServerPartT IO a0&#39;与实际类型&#39;[Response]&#39;相匹配 - Haskell error: couldn't match expected type ‘ServerPartT IO a0’ with actual type '[Response]' 无法将预期的类型“ 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 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]’” 无法将预期的类型“ IO b0”与实际类型“ [a0]”匹配 - Couldn't match expected type `IO b0' with actual type `[a0]' Haskell中的软件事务存储器:无法将预期的STM a0类型与实际的IO类型进行匹配() - Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO ()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM