[Haskell-cafe] Ambiguous type variable

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Fri Oct 15 18:51:22 EDT 2010


On 16 October 2010 09:47, Jacek Generowicz <jacek.generowicz at cern.ch> 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).

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list