[Haskell-cafe] What does the Haskell type system do with "show (1+2)"?

Jeff.Harper at handheld.com Jeff.Harper at handheld.com
Thu Jan 12 15:45:40 EST 2006


What does the Haskell type system do with expressions such as these . . . 
?
   show 1
   show (1+2)

The type of the subexpressions "1" and "1+2" are "ambiguous" since they 
have type "(Num a) => a".  I'm under the assumption before "1+2" is 
evaluated, the "1" and "2" must be coerced into a "concrete" type such as 
Int, Integer, Double, etc, and before "show 1" is evaluated, the "1" must 
be coerced into a "concrete" type.  Is my assumption correct?  If so, how 
does Haskell know into which type to coerce the subexpressions?

If I try to write a new function, "my_show", which converts an expression 
into a string representation that includes type information, I run into 
errors with expressions like "show 1" and "show (1+2)" because of the type 
ambiguity.

class (Show a) => My_show a where
   my_show :: a -> String

instance My_show Int where
   my_show a = show a ++ " :: Int"

instance My_show Integer where
   my_show a = show a ++ " :: Integer"

I can avoid the errors if I change it to "my_show (1::Int)" or "my_show 
((1+2)::Int).  I'm wondering what the difference is between, my_show and 
Haskell's built-in show that causes my_show to produce an error message 
when it is used with ambiguous types, but Haskell's show works okay with 
ambiguous types.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060112/6dcd8719/attachment.htm


More information about the Haskell-Cafe mailing list