Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  type variables - error: 'No instance for' (Corrado Corrado)
   2. Re:  type variables - error: 'No instance for' (David James)


----------------------------------------------------------------------

Message: 1
Date: Fri, 5 Feb 2021 07:27:06 +0100
From: Corrado Corrado <conrad....@mail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] type variables - error: 'No instance for'
Message-ID:
        
<trinity-4ad06460-9d75-4c44-93ce-3365364ecbcb-1612506426005@3c-app-mailcom-lxa04>
        
Content-Type: text/plain; charset="utf-8"

An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210205/6bb443c4/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 5 Feb 2021 09:41:54 +0000
From: David James <dj112...@outlook.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] type variables - error: 'No instance
        for'
Message-ID:
        
<am6pr04mb421497ccee992ec27b698c7db6...@am6pr04mb4214.eurprd04.prod.outlook.com>
        
Content-Type: text/plain; charset="cp1253"

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....@mail.com>
Sent: 05 February 2021 06:27
To: beginners@haskell.org<mailto:beginners@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-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 151, Issue 3
*****************************************

Reply via email to