
-- Given a definition of view which is essentially a synonym for show: class View a where view :: a -> String instance View Int where view = show -- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2

On 16 October 2010 00:47, Jacek Generowicz
-- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error
fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2
Don't integral literals default to Integer, of which there is a Show instance but no View instance?

On 2010 Oct 16, at 00:51, Ivan Lazar Miljenovic wrote:
On 16 October 2010 09:47, Jacek Generowicz
wrote: -- Given a definition of view which is essentially a synonym for show:
class View a where view :: a -> String
instance View Int where view = show
-- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error
fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for).
Which implies that defining all instances of Num to be instances of View should do the trick, and that doesn't seem to work. See below. On 2010 Oct 16, at 00:51, Christopher Done wrote:
Don't integral literals default to Integer, of which there is a Show instance but no View instance?
Hmm, it doesn't seem to be that simple. The phenomenology seems to be: As far as entering "view 2" into ghci is concerned, you need 'instance View Integer' or 'instance View Double'. To get "x = view 2" to compile in ghc, having all of Int, Integer, Float and Double as instances of View is still not enough. I did all this in an environment where I had not imported any other Num instances, and ":i Num" in ghci showed only the 4 aforementioned types as instances.

On 2010 Oct 16, at 01:14, Jacek Generowicz wrote:
On 2010 Oct 16, at 00:51, Ivan Lazar Miljenovic wrote:
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for).
On 2010 Oct 16, at 00:51, Christopher Done wrote:
Don't integral literals default to Integer, of which there is a Show instance but no View instance?
An in both of the explanations above, it should then complain about the lack of an instance of View, rather than about ambiguous type variables, n'est-ce pas?

On Saturday 16 October 2010 01:18:55, Jacek Generowicz wrote:
On 2010 Oct 16, at 01:14, Jacek Generowicz wrote:
On 2010 Oct 16, at 00:51, Ivan Lazar Miljenovic wrote:
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for).
On 2010 Oct 16, at 00:51, Christopher Done wrote:
Don't integral literals default to Integer, of which there is a Show instance but no View instance?
An in both of the explanations above, it should then complain about the lack of an instance of View, rather than about ambiguous type variables, n'est-ce pas?
Non. If you write x = view 2 the type checker looks at the expression 2 and it sees it has the type Num a => a (because integer literals are actually shorthand for (fromInteger literal) and fromInteger :: Num a => Integer -> a). Then (or before that) it looks at the context in which the 2 appears. That context is that the function view is applied to it. view :: View v => v -> String So, in order for the expression 2 to be well typed, the type checker finds the two constraints 2 :: Num a => a 2 :: View v => v These constraints have to be unified (which is very easy here), resulting in 2 :: (Num a, View a) => a But there's no way to find out what type a is/has to be. Hence a is an ambiguous type variable. Now, under certain circumstances such ambiguous type variables are resolved by defaulting. If you replace view with show, you get a Show constraint instead of the View constraint and then defaulting may (and must) happen. But since View is defined outside the standard libraries, the language report says defaulting mustn't take place, so it doesn't (it may work if you specify {-# LANGUAGE ExtendedDefaultRules #-} at the top of your module).

On 2010 Oct 16, at 01:39, Daniel Fischer wrote:
On Saturday 16 October 2010 01:18:55, Jacek Generowicz wrote:
On 2010 Oct 16, at 01:14, Jacek Generowicz wrote:
On 2010 Oct 16, at 00:51, Ivan Lazar Miljenovic wrote:
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for).
On 2010 Oct 16, at 00:51, Christopher Done wrote:
Don't integral literals default to Integer, of which there is a Show instance but no View instance?
An in both of the explanations above, it should then complain about the lack of an instance of View, rather than about ambiguous type variables, n'est-ce pas?
Non.
If you write
x = view 2
the type checker looks at the expression 2 and it sees it has the type
Num a => a
(because integer literals are actually shorthand for (fromInteger literal) and fromInteger :: Num a => Integer -> a).
Then (or before that) it looks at the context in which the 2 appears. That context is that the function view is applied to it.
view :: View v => v -> String
So, in order for the expression 2 to be well typed, the type checker finds the two constraints
2 :: Num a => a 2 :: View v => v
These constraints have to be unified (which is very easy here), resulting in
2 :: (Num a, View a) => a
But there's no way to find out what type a is/has to be. Hence a is an ambiguous type variable.
Exactly. Which is why I made the point that the two explanations offered by Christopher and Ivan (both of which suggested that the problem was related to a missing View instance) imply that the error report should mention missing View instances: As the error message does not mention them, I took this as further evidence that those suggestions were not correct.
Now, under certain circumstances such ambiguous type variables are resolved by defaulting. If you replace view with show, you get a Show constraint instead of the View constraint and then defaulting may (and must) happen. But since View is defined outside the standard libraries, the language report says defaulting mustn't take place, so it doesn't
Yup. That much was clear from you last message.
(it may work if you specify {-# LANGUAGE ExtendedDefaultRules #-} at the top of your module).
Interesting. Thanks.

On Saturday 16 October 2010 01:14:51, Jacek Generowicz wrote:
On 2010 Oct 16, at 00:51, Ivan Lazar Miljenovic wrote:
On 16 October 2010 09:47, Jacek Generowicz
wrote: -- Given a definition of view which is essentially a synonym for show:
class View a where view :: a -> String
instance View Int where view = show
-- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error
fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for).
Which implies that defining all instances of Num to be instances of View should do the trick, and that doesn't seem to work. See below.
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3... Defaulting only takes place when all involved classes are defined in the Prelude or the standard libraries. Your View class isn't, hence there's no defaulting. It works in ghci because ghci uses extended default rules (otherwise it would have to give too many `ambiguous type variable' messages).
On 2010 Oct 16, at 00:51, Christopher Done wrote:
Don't integral literals default to Integer, of which there is a Show instance but no View instance?
Hmm, it doesn't seem to be that simple.
The phenomenology seems to be:
As far as entering "view 2" into ghci is concerned, you need 'instance View Integer' or 'instance View Double'.
To get "x = view 2" to compile in ghc, having all of Int, Integer, Float and Double as instances of View is still not enough.
I did all this in an environment where I had not imported any other Num instances, and ":i Num" in ghci showed only the 4 aforementioned types as instances.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

-- Given a definition of view which is essentially a synonym for show:
class View a where view :: a -> String
instance View Int where view = show
-- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error
fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2
On 2010 Oct 16, at 01:25, Daniel Fischer wrote:
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3...
Defaulting only takes place when all involved classes are defined in the Prelude or the standard libraries.
Your View class isn't, hence there's no defaulting.
Bingo. Thank you.

On 16 October 2010 09:47, Jacek Generowicz
-- Given a definition of view which is essentially a synonym for show:
class View a where view :: a -> String
instance View Int where view = show
-- why does "show 2" compile, while "view 2" gives an -- 'Ambiguous type variable' error
fine = view (2::Int) noProblem = show 2 ambiguousTypeVariable = view 2
"2" is a generic number. If you don't specify a type, it usually defaults to Integer. All Num instances that come in the Prelude have Show instances, so no matter which gets picked "show 2" works. However, when you say "view 2" ghc/ghci doesn't know that you want 2 to be an Int (as that's the only type you have an instance for View for). -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com
participants (4)
-
Christopher Done
-
Daniel Fischer
-
Ivan Lazar Miljenovic
-
Jacek Generowicz