
Hi cafè, given the following toy code: --------------- module Main where class Dumb p where dumb :: p -> String newtype Branded a b = Branded b unbrand :: Branded a b -> b unbrand (Branded x) = x wrong :: Dumb a => b -> Branded a b wrong = Branded right :: Show a => b -> Branded a b right = Branded --------------- Why: --------------- quarry:Haskell paris$ ghci -O1 GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help *Main> unbrand $ right True True *Main> unbrand $ right "Foo" "Foo" --------------- but: --------------- *Main> unbrand $ wrong True <interactive>:1:10: Ambiguous type variable `a' in the constraint: `Dumb a' arising from a use of `wrong' at <interactive>:1:10-19 Probable fix: add a type signature that fixes these type variable(s) --------------- ? Maybe it's a dumb question but... thank you for any explanation... -- Cristiano GPG Key: 4096R/C17E53C6 2010-02-22 Fingerprint = 4575 4FB5 DC8E 7641 D3D8 8EBE DF59 B4E9 C17E 53C6

On Friday 11 February 2011 19:33:27, Cristiano Paris wrote:
Hi cafè,
given the following toy code:
--------------- module Main where
class Dumb p where dumb :: p -> String
newtype Branded a b = Branded b
unbrand :: Branded a b -> b unbrand (Branded x) = x
wrong :: Dumb a => b -> Branded a b wrong = Branded
right :: Show a => b -> Branded a b right = Branded ---------------
Why:
--------------- quarry:Haskell paris$ ghci -O1 GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help *Main> unbrand $ right True True *Main> unbrand $ right "Foo" "Foo" ---------------
but:
--------------- *Main> unbrand $ wrong True
<interactive>:1:10: Ambiguous type variable `a' in the constraint: `Dumb a' arising from a use of `wrong' at <interactive>:1:10-19 Probable fix: add a type signature that fixes these type variable(s) ---------------
?
Maybe it's a dumb question but... thank you for any explanation...
It's because there's no way to determine the type variable a (in either wrong or right). In such cases, under some circumstances, the type variable gets defaulted (the ambiguous contexts all must have the form (C a), at least one numeric class [Num, Integral, Fractional, ...] is involved, all involved classes come from the Prelude or the standard libraries) as specified in the report (section 4.3, iirc). These conditions are not met by your 'right' function, but ghci uses extended default rules, a type variable a with a Show constraint [and no other] gets defaulted to (). But ghci has no rule how to default your Dumb class, so it reports the ambiguous type variable there. [Without extended defaulting rules, ambiguous type variable errors would be too frequent at the ghci prompt.] If you try to compile the module, both should raise an ambiguous type variable error, since the compiler follows the standard defaulting rules (unless you enable -XExtendedDefaultRules).

On Fri, Feb 11, 2011 at 20:00, Daniel Fischer
... It's because there's no way to determine the type variable a (in either wrong or right).
That's what I thought when I wrote the code at first but then I was surprised to see it working with the Show type-class.
In such cases, under some circumstances, the type variable gets defaulted (the ambiguous contexts all must have the form (C a), at least one numeric class [Num, Integral, Fractional, ...] is involved, all involved classes come from the Prelude or the standard libraries) as specified in the report (section 4.3, iirc).
I'm reading it right now.
... These conditions are not met by your 'right' function, but ghci uses extended default rules, a type variable a with a Show constraint [and no other] gets defaulted to (). But ghci has no rule how to default your Dumb class, so it reports the ambiguous type variable there. [Without extended defaulting rules, ambiguous type variable errors would be too frequent at the ghci prompt.]
God! It seems like I'm reading the small-character lines of a contract :) Seriously, now it makes sense... Thank you. -- Cristiano GPG Key: 4096R/C17E53C6 2010-02-22 Fingerprint = 4575 4FB5 DC8E 7641 D3D8 8EBE DF59 B4E9 C17E 53C6

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 2/11/11 14:20 , Cristiano Paris wrote:
God! It seems like I'm reading the small-character lines of a contract :)
Wait until you encounter the equivalent of rules-lawyering in the type system :) - -- brandon s. allbery [linux,solaris,freebsd,perl] allbery.b@gmail.com system administrator [openafs,heimdal,too many hats] kf8nh -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk1XTzAACgkQIn7hlCsL25U+RACgzzqpTAJLum60odY8dpOi6dGQ kyIAnRp5cM3QbrkzYu7OzTQgOzeXWdXw =P20S -----END PGP SIGNATURE-----
participants (3)
-
Brandon S Allbery KF8NH
-
Cristiano Paris
-
Daniel Fischer