Question regarding error messages

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Question regarding error messages

Choi Waikit
Hi guys,

I have difficulty understanding how GHC determines what kind of error message to return. Could you help explain a bit ? Thanks !

The code can be accessed here

My question is
Why does calling `settleDown 9` return different error message from `settleDown "sting"` ?
 
Thanks,
David

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

Re: Question regarding error messages

Francesco Ariis
On Sun, Jun 18, 2017 at 05:02:13PM +0800, Choi Waikit wrote:

> Hi guys,
>
> I have difficulty understanding how GHC determines what kind of error
> message to return. Could you help explain a bit ? Thanks !
>
> The code can be accessed here <http://lpaste.net/356325>
>
> My question is
> Why does calling `settleDown 9` return different error message from
> `settleDown "sting"` ?

Hello David,
    every time you write a number in a haskell source, it gets automagically
wrapped in `fromInteger` (actually, `fromInteger` if there isn't a dot,
`fromRational` otherwise). This is made to provide some kind of overloading.

`fromInteger` has signature:

    λ> :t fromInteger
    fromInteger :: Num a => Integer -> a

hence the different error with 9 and "string"!

Is it clear now?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding error messages

David CAI
Hi Francesco,

Thanks for your reply! Just to make sure I understood, can I paraphrase your words as the following ?

In the case of `settleDown 9`
`settleDown 9` is first translated to `settleDown $ fromInteger 9`. Since fromInteger lies in Num type class and settleDown's signature is: settleDown :: Mood -> Mood, the compiler tries to look for
an instance for Num typeclass so that it can resolve which `fromInteger` method to call. Therefore it doesn't show a type mismatch error but hint for implementing a typeclass.

In the case of `settleDown "hello"`
There's no preprocessing for "hello" so its type remains [Char]. Since settleDown has type Mood -> Mood, Mood and [Char] are not compatible, therefore the compiler shows a type mismatch error.

Thanks,
David

On 19 June 2017 at 00:49, Francesco Ariis <[hidden email]> wrote:
On Sun, Jun 18, 2017 at 05:02:13PM +0800, Choi Waikit wrote:
> Hi guys,
>
> I have difficulty understanding how GHC determines what kind of error
> message to return. Could you help explain a bit ? Thanks !
>
> The code can be accessed here <http://lpaste.net/356325>
>
> My question is
> Why does calling `settleDown 9` return different error message from
> `settleDown "sting"` ?

Hello David,
    every time you write a number in a haskell source, it gets automagically
wrapped in `fromInteger` (actually, `fromInteger` if there isn't a dot,
`fromRational` otherwise). This is made to provide some kind of overloading.

`fromInteger` has signature:

    λ> :t fromInteger
    fromInteger :: Num a => Integer -> a

hence the different error with 9 and "string"!

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


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

Re: Question regarding error messages

Francesco Ariis
On Mon, Jun 19, 2017 at 10:56:59AM +0800, David CAI wrote:

> Hi Francesco,
>
> Thanks for your reply! Just to make sure I understood, can I paraphrase
> your words as the following ?
>
> In the case of `settleDown 9`
> `settleDown 9` is first translated to `settleDown $ fromInteger 9`. Since
> fromInteger lies in Num type class and settleDown's signature is:
> settleDown :: Mood -> Mood, the compiler tries to look for
> an instance for Num typeclass so that it can resolve which `fromInteger`
> method to call. Therefore it doesn't show a type mismatch error but hint
> for implementing a typeclass.
>
> In the case of `settleDown "hello"`
> There's no preprocessing for "hello" so its type remains [Char]. Since
> settleDown has type Mood -> Mood, Mood and [Char] are not compatible,
> therefore the compiler shows a type mismatch error.

Excellent explanation, that's exactly it!
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...