type variables - error: 'No instance for'

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

type variables - error: 'No instance for'

Corrado Corrado
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
 
 
 

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: type variables - error: 'No instance for'

David James

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.

 

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: [hidden email]
Sent: 05 February 2021 06:27
To: [hidden email]
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

 

 

 

 


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners