[Haskell-beginners] Question regarding error messages
David CAI
david.weijiecai at gmail.com
Mon Jun 19 02:56:59 UTC 2017
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 <fa-ml at ariis.it> 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
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170619/11c2c00f/attachment.html>
More information about the Beginners
mailing list