L292092
L292092

Reputation: 128

What is the cause of "No instance for (Show (Int -> Int)) arising from a use of `print'" error when trying to give function a type in Haskell?

Error came up when writing this:

definition x = x
definition :: Int -> Int

Upvotes: 2

Views: 81

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477190

You here first defined a function definition x = x. In the second line, you then have an expression definition :: Int -> Int. But a function is not an instance of Show, and thus the interpreter can not print the function.

It looks however like your second line is the signature you want to add to the function. You can use :{ and :} to write multi-line statements, like:

Prelude> :{
Prelude| definition :: Int -> Int
Prelude| definition x = x
Prelude| :}

Upvotes: 6

chi
chi

Reputation: 116174

You are entering your code in GHCi, line by line.

The first line defines your function.

The second line asks it to be printed, which can not be done.

Don't write your code line by line in GHCi. Write it in a file, and then load it in GHCi.

Upvotes: 6

Related Questions