[Haskell-cafe] need help with understanding expression
miguelimo38 at yandex.ru
Sat Nov 17 09:17:45 CET 2012
Отправлено с iPhone
17.11.2012, в 11:19, damodar kulkarni <kdamodar2000 at gmail.com> написал(а):
> In the second case, why the GHC doesn't give something like?
> ([Char] (a -> t), Num a) => t
Because "Num" is a class of types, while "String" is a type.
In other words, in the expression 3 "a" ghc doesn't know, what type 3 belongs to, it just knows that it should be of class Num. In particular, it could be of type "String -> t" for some t, ghc wouldn't go over all possible types to see that there is no such instance of "Num".
On the other hand, in "a" 3 it knows exactly what type "a" is, strings are always of one type, "String" (which is an alias for [Char]). You can enable OverloadedStrings extension, which makes string literals polymorphic; then you'll have your type, which would be something like "(IsString (a -> b), Num a) => b".
> Now let's consider the evaluation scenario:
> Prelude> 3 2
> No instance for (Num (t -> t1))
> arising from the literal `3' at <interactive>:1:0-2
> Possible fix: add an instance declaration for (Num (t -> t1))
> In the expression: 3 2
> In the definition of `it': it = 3 2
> Note, how the GHC suggests us this "strange constraint" as a possible fix BUT see this:
> Prelude> "a" 2
> Couldn't match expected type `t1 -> t'
> against inferred type `[Char]'
> In the expression: "a" 2
> In the definition of `it': it = "a" 2
> In this case the GHC doesn't even allow us to add any "possible fix".
Same thing. "String" is NOT a functional type, and it would never be, while it's possible (and sometimes reasonable) to have a functional type of class "Num". Enable OverloadedStrings, and you'll see your desired "possible fix".
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe