[Haskell-beginners] type variables - error: 'No instance for'

David James dj112358 at outlook.com
Fri Feb 5 09:41:54 UTC 2021


Hello,

Have a look at this:

> mytriple (1 :: Integer) 'a' 'b'
<interactive>:418:28: error:
    • Couldn't match expected type ‘Integer’ with actual type ‘Char’
    • In the third argument of ‘mytriple’, namely ‘'b'’
      In the expression: mytriple (1 :: Integer) 'a' 'b'
      In an equation for ‘it’: it = mytriple (1 :: Integer) 'a' 'b'

This is probably what you expected. In this case, you’ve explicitly stated that 1 is a value of type Integer.

But look at the type here:

> :t 3
3 :: Num p => p

This might be a bit confusing. Haskell supports many different types of number (Int, Integer, Double, Complex, etc). Although they are different types, they are all instances of the same class (Num). When you type 1 (or some other numeric literal) into GHCi, it doesn’t (yet) know which of these types you want, but does know it has to be a type that’s an instance of Num, which is what the type signature gives. Hence you can do:

> 3 `mod` 2
1
(which treats the 3 as some integral type), or

> 3 / 2
1.5
(which treats the 3 as a fractional – i.e. non-integral- type)

Now it knows more about the type of 3 needed, it will ensure it is of a more specific type.

If typeclasses are new to you, you might want to read something like this<http://learnyouahaskell.com/types-and-typeclasses#believe-the-type>.

Now look at:

> :t (+)
(+) :: Num a => a -> a -> a

This says (+) can add two values of the same type, as long as that type is an instance of class Num. Hence:

> 3 + 'a'
<interactive>:407:1: error:
    • No instance for (Num Char) arising from a use of ‘+’
    • In the expression: 3 + 'a'
      In an equation for ‘it’: it = 3 + 'a'

This now makes sense: ‘a’ is of type Char, and there is no instance declaration instance Num Char (since Char’s are not numbers).

Your example is much the same. In order for mytriple 1 'a' 'b' to typecheck: ‘b’ must be a Char, and 1 must also be a Char, but 1 has to be a type that’s an instance of Num, so would require an instance Num Char to exist.

Hope that all makes sense,
David.

From: Corrado Corrado<mailto:conrad.dev at mail.com>
Sent: 05 February 2021 06:27
To: beginners at haskell.org<mailto:beginners at haskell.org>
Subject: [Haskell-beginners] type variables - error: 'No instance for'

Hello,
I defined a simple function as below:

mytriple :: a -> b -> a -> (a,b,a);
mytriple x y z = (x,y,z);

Then I checked 'mytriple' type variables signatures, so I declared a mytriple values in a 'wrong way':

                         ____ should be to the same type of the 1st argument
                        /
λ:t2 = mytriple 1 'a' 'b'
 error:
    'No instance for (Num Char) arising from the literal '1'
    'In the first argument of mytriple', namely '1'
      In the expression: mytriple 1 'a' 'b'
      In an equation for 't2': t2 = mytriple 1 'a' 'b'

Ok, this is exactly what I aspected, but I do not understand the error.

What means the sentence : 'No instance for (Num Char) arising from the literal '1'.'
How is evaluated a function, in order to check that the params are type variables signatures?

Why the error message refers to the 1st arguments?

Thanks
Conrad




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210205/88908482/attachment.html>


More information about the Beginners mailing list