Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve these Haskell Kind errors

Tags:

types

haskell

So I've been messing around with Haskell, and I've come across this strange error in my code.

" 'IO' is not applied to enough type arguments
Expected kind '?', but 'IO' has kind '->'
In the type signature for '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 do I fix this error? Also, any insight on kinds would be greatly appreciated.

like image 469
Mike Avatar asked Nov 28 '22 22:11

Mike


2 Answers

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

IO Int
IO String
IO ()

are types, but IO by itself is not.

The kind of IO is * -> *, which is like saying it is a function that takes a type and returns a type.

I would suggest changing

loop :: State -> IO

to

loop :: State -> IO ()

(() is the "unit type", it has only one value (also called ()), and is typically used where void would be used in C-like languages)

like image 107
luqui Avatar answered Nov 30 '22 12:11

luqui


IO is a type constructor, not a full type. You should declare

loop :: State -> IO ()

where () is the unit type; the type with only one value, also spelled (). That's the appropriate type for an eternal loop or any other function that does not return a (meaningful) value.

like image 34
Fred Foo Avatar answered Nov 30 '22 10:11

Fred Foo