How to solve these errors Haskell Kind

So I messed around with the Haskell, and I ran into this strange error in my code. "IO" does not apply to a sufficient argument type
'?' Kind of expected, but 'the IO' looks '->'
The type signature for the 'loop': loop :: State β†’ IO

Here is the code

import System.IO data State = State [Int] Int Int deriving (Show) main = do loop (State [] 0 0) loop::State -> IO loop state = do putStr "file: " f <- getLine handle <- openFile f ReadMode cde <- hGetContents handle hClose handle putStrLn cde loop state 

How to fix this error? In addition, it would be useful to evaluate any information about the kinds.

+4
source share
3 answers

IO is a type constructor, not a complete type. You must declare

 loop :: State -> IO () 

where () - unit type; type with a single value, also spelled () . This type corresponding to an eternal loop or any other function which does not return a (significant) value.

+11
source

IO - is a type constructor, which means that in order to become a type, he needs argument. So:

 IO Int IO String IO () 

They are types, but the IO itself is not.

Type IO is equal to * -> * , which indicates that this is a function that takes a type and returns the type.

I would suggest change

 loop :: State -> IO 

to

 loop :: State -> IO () 

( () - this "one type", he has only one value (also called () ), and is typically used where the void will be used on the C-type languages)

+11
source

As mentioned by others, IO - is a type constructor, not the type. Therefore, you should apply it to another type. Type value IO Foo means that this calculation, which potentially makes some input-output operations, and then returns a value of type Foo .

luqui larsman and suggested the use of () as the return value. I think the next type - the best alternative for a function that loops forever:

 loop :: String -> IO a 

Note that now the function is polymorphic in the return value. This type is much more informative than the return () . What for? Because the function of this type should be a function of cycling. It is impossible to implement complete function with this type. The user of this function will immediately see that this cycle function. Thus, you get the documentation for free with this type.

+4
source

All Articles