[Haskell-beginners] how do typeclasses work again?
Sylvain Henry
sylvain at haskus.fr
Fri Feb 10 17:17:08 UTC 2017
Your `foo4`:
1) uses the instance `Isx isx a` to convert an `isx` into an `a`
2) then uses the instance `Show a` to convert an `a` into a String
The problem is that the compiler cannot infer the actual `a` type.
E.g., suppose you have the following instances:
data X = X
data Y = Y
instance Is (x,y) x where ...
instance Is (x,y) y where ...
instance Show X where ...
instance Show Y where ...
If you write "foo4 (X,Y)", the compiler can't decide which instance to use.
A solution: use AllowAmbiguousTypes as the compiler suggests and then
use TypeApplications to select the "a" type:
foo4 :: forall a isx. (Is isx a, Show a) => isx -> String
foo4 = apply (\(i :: a) -> show a)
main = print (foo4 @Y (X,Y))
--
Sylvain
On 10/02/2017 17:27, Nicholls, Mark wrote:
> lovely
>
> so if I now go....
>
>> foo4 = apply (\i -> show i)
> And :t foo4 is...
>
>> foo4 :: (Is isx a, Show a) => isx -> String
> So add that as a type...and we get the same sort of awfulness...."could not deduce" bla bla
>
> But how do you make this disappear
>
>> foo4 = apply (\(i :: a) -> show i)
> Doesn’t work... "could not deduce" bla bla
>
> I'd instinctively like to go....
>
>> foo4 = apply (\(i :: ((Show a) => a)) -> show i)
> "Illegal qualified type: Show a => a"
>
> And this is really just
>
>> foo4 = apply show
> Where we end with "could not deduce"
>
> Sorry....I'm struggling.
>
>
>> -----Original Message-----
>> From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of David
>> McBride
>> Sent: 09 February 2017 5:31 PM
>> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level
>> topics related to Haskell
>> Subject: Re: [Haskell-beginners] how do typeclasses work again?
>>
>> foo2 :: (Is isx Integer) => isx -> String
>>
>>
>> isx -> String - That means that this function takes anything and returns a string.
>> Is isx Integer => - That just means that whatever isx is, there should be an Is isx
>> Integer instance that satisfies it.
>>
>> Putting those together this function takes anything and returns a string, so long
>> as the anything (isx) satisfies the constraint I isx Integer.
>>
>> But there's nothing in the type or code that says what type x actually is. The
>> Integer in the constraint just constrains what isx can be.
>>
>> To fix it add the ScopedTypeVariables extension and try this:
>>
>> foo2 :: (Is isx Integer) => isx -> String
>> foo2 = apply (\(i :: Integer) -> "")
>>
>> Alternatively if you are using ghc 8, you can turn on TypeApplications and use
>> this:
>>
>> foo2 :: (Is isx Integer) => isx -> String
>> foo2 = apply @_ @Integer (\i -> "")
>>
>> On Thu, Feb 9, 2017 at 11:59 AM, Nicholls, Mark <nicholls.mark at vimn.com>
>> wrote:
>>> Sorry..I do haskell about once every 6 months for 2 hours...and then get on
>> with my life.
>>> I always forget some nuance of typeclasses.
>>>
>>> Consider some simple typeclass
>>>
>>>> class Is isx x where
>>>> apply :: (x -> y) -> isx -> y
>>>
>>> We can make any type a member of it...mapping to itself
>>>
>>>> instance Is x x where
>>>> apply f = f
>>> But we can also make a tuple a member of it...and pull the 1st member..
>>>
>>>> instance Is (x,y) x where
>>>> apply f (x,y) = f x
>>> Weird and largey useless...but I'm playing.
>>>
>>> Then construct a function to operate on it
>>>
>>>> foo2 :: (Is isx Integer) => isx -> String
>>>> foo2 = apply (\i -> "")
>>> And...
>>>
>>> • Could not deduce (Is isx x0) arising from a use of ‘apply’
>>> from the context: Is isx Integer
>>> bound by the type signature for:
>>> foo2 :: Is isx Integer => isx -> String
>>> at prop.lhs:51:3-43
>>> The type variable ‘x0’ is ambiguous
>>> Relevant bindings include
>>> foo2 :: isx -> String (bound at prop.lhs:52:3)
>>> These potential instances exist:
>>> instance Is x x -- Defined at prop.lhs:41:12
>>> instance Is (x, y) x -- Defined at prop.lhs:45:12
>>> • In the expression: apply (\ i -> "")
>>> In an equation for ‘foo2’: foo2 = apply (\ i -> "")
>>>
>>>
>>> What's it going on about?
>>> (my brain is locked in F# OO type mode)
>>>
>>> I've told it to expect a function "Integer -> String"...surely?
>>> Whats the problem.
>>>
>>> CONFIDENTIALITY NOTICE
>>>
>>> This e-mail (and any attached files) is confidential and protected by copyright
>> (and other intellectual property rights). If you are not the intended recipient
>> please e-mail the sender and then delete the email and any attached files
>> immediately. Any further use or dissemination is prohibited.
>>> While MTV Networks Europe has taken steps to ensure that this email and any
>> attachments are virus free, it is your responsibility to ensure that this message
>> and any attachments are virus free and do not affect your systems / data.
>>> Communicating by email is not 100% secure and carries risks such as delay,
>> data corruption, non-delivery, wrongful interception and unauthorised
>> amendment. If you communicate with us by e-mail, you acknowledge and
>> assume these risks, and you agree to take appropriate measures to minimise
>> these risks when e-mailing us.
>>> MTV Networks International, MTV Networks UK & Ireland, Greenhouse,
>> Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions
>> International, Be Viacom, Viacom International Media Networks and VIMN and
>> Comedy Central are all trading names of MTV Networks Europe. MTV Networks
>> Europe is a partnership between MTV Networks Europe Inc. and Viacom
>> Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley
>> Crescent, London, NW1 8TT.
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> CONFIDENTIALITY NOTICE
>
> This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
>
> While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
>
> Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
>
> MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
More information about the Beginners
mailing list