how do typeclasses work again?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

how do typeclasses work again?

Nicholls, Mark-2

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Sylvain Henry-2
 > I've told it to expect a function "Integer -> String"...surely?

No. The constraints only indicates that an instance matching "Is isx
Integer" must exist but that's not what the compiler expects.

You have:
(\i -> "") :: x -> String
the type `x` cannot be inferred.

Hence when you write `apply (\i -> "")` the compiler expects an instance
"Is isx x" for the ambiguous x.

You have to declare the type of `i` to be Integer for your code to work:
foo = apply (\(i :: Integer) -> "")

--
Sylvain


On 09/02/2017 17:59, Nicholls, Mark 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

David McBride
In reply to this post by Nicholls, Mark-2
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 <[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Dimitri DeFigueiredo
In reply to this post by Nicholls, Mark-2
I would like to make a general comment that is likely not useful to the
specific question asked (which I think has already been nicely answered
by others).
But I would like to point out this video of an excellent lecture on
typeclasses given by Simon Peyton-Jones a few years ago.

https://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but-not-as-we-know-them

Cheers,

Dimitri

--
2E45 D376 A744 C671 5100 A261 210B 8461 0FB0 CA1F

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Nicholls, Mark-2
In reply to this post by David McBride
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:[hidden email]] 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 <[hidden email]>
>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
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>_______________________________________________
>Beginners mailing list
>[hidden email]
>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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Nicholls, Mark-2
In reply to this post by Dimitri DeFigueiredo
I'll take a look

Thanks.


>-----Original Message-----
>From: Beginners [mailto:[hidden email]] On Behalf Of Dimitri
>DeFigueiredo
>Sent: 10 February 2017 12:45 PM
>To: [hidden email]
>Subject: Re: [Haskell-beginners] how do typeclasses work again?
>
>I would like to make a general comment that is likely not useful to the specific
>question asked (which I think has already been nicely answered by others).
>But I would like to point out this video of an excellent lecture on typeclasses
>given by Simon Peyton-Jones a few years ago.
>
>https://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but-not-as-we-
>know-them
>
>Cheers,
>
>Dimitri
>
>--
>2E45 D376 A744 C671 5100 A261 210B 8461 0FB0 CA1F
>
>_______________________________________________
>Beginners mailing list
>[hidden email]
>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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Sylvain Henry-2
In reply to this post by Nicholls, Mark-2
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:[hidden email]] 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 <[hidden email]>
>> 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
>>> [hidden email]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Nicholls, Mark-2
ah

that works...now the challenge is for me to understand why.

I'll watch the SPJ video to get a feel for whats going on, I don't understand the difference

forall a isx. (Is isx a, Show a) => isx -> String
and
(Is isx a, Show a) => isx -> String

but I'll look it up.

I think my understanding of type classes is naïve, I just thought it meant that secretly a dictionary was being passed.
the compiler would identify the specific dictionary from the call site
but this might be an OO minded error.

I also look up TypeApplications...thanks v much
________________________________________
From: Beginners [[hidden email]] on behalf of Sylvain Henry [[hidden email]]
Sent: 10 February 2017 17:17
To: [hidden email]
Subject: Re: [Haskell-beginners] how do typeclasses work again?

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:[hidden email]] 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 <[hidden email]>
>> 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
>>> [hidden email]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Sylvain Henry-2
On 11/02/2017 09:50, Nicholls, Mark wrote:
> I don't understand the difference
>
> forall a isx. (Is isx a, Show a) => isx -> String
> and
> (Is isx a, Show a) => isx -> String
I have used the `forall` explicitly only to fix the order of the type
parameters (`a` and `isx`) so that we are sure to set the type of `a`
when we write (using TypeApplications): foo4 @Y (X,Y)

In the second declaration, the `forall` is implicit.

> I think my understanding of type classes is naïve, I just thought it meant that secretly a dictionary was being passed.
Yes your understanding is correct. The issue here is that the compiler
doesn't know the type of `a`, hence it can't select and pass the
appropriate instances.

> the compiler would identify the specific dictionary from the call site
Even at call site, the compiler can't infer the `a` type from the `isx`
type (nor from the return type of `foo4`).

Do you want the `a` type to be dependent on the `isx` type? I.e., to
only be allowed to define a single `Is isx a` instance for each `isx` type.

--
Sylvain
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how do typeclasses work again?

Nicholls, Mark-2
I'm naively capable of messing around with type families...so I know how to define the types statically...that's not really what I want...that's too strong.

I think I'm trying to work in the universe of typeclasses and not data types....in my OO head these two things overlap (if you see what I mean)....in Haskell they are distinct...which I'm beginning to feel makes type inference easy....but is actually quite "weak".

so lets start again...

> class Is isx x where
>   apply ::(x -> y) -> isx -> y

> instance Is x x where
>   apply f = f

i.e. lets create our tuple instance like this!

> instance (Is m x) => Is (m,y) x where
>   apply f (m,y) = apply f m

> data X = X deriving (Show)
> data Y = Y deriving (Show)

> foo4 :: forall a isx. (Is isx a, Show a) => isx -> String
> foo4 = apply (\(i :: a) -> show i)

now...this line said....

> main = print (foo4 @X (X,Y))

and that works!....which I think is what I want....in an OO world this feels like a "cast"....where Ive said (X,Y) <: X....I'm getting the compiler to extract fst for me...I'm lazy.

so lets tell the compiler it could do snd for me.

> instance (Is m x) => Is (y,m) x where
>   apply f (y,m) = apply f m

gives....."Duplicate instance declarations "

which is unfortunate as I wanted to then write

> main = print (foo4 @Y (X,Y))

to "cast" to Y....which feels perfectly reasonable

then I look this up on the interweb...and magically found some noob has been here before!
"noob “Duplicate instance declarations” (again)"

that noob was me!...about a year ago...and someone said I was misunderstanding how to define these sort of recursive structures....and It should be done in the class declaration...which doesn't seem to work in this case...as I want to do something recursive

the answer said.....

"Haskell requires that there be only one instance for each class and type. Thus is determined only from the part to the right of the =>. "

ok, I buy that...IF I want to guarantee Haskell to automatically derive type class dictionaries that functional restriction is perfectly reasonable...as long as its resolved at some point.

I tbink (naively)...Haskell is saying...
"if I match things against (x,y) I've got 2 instance declarations....so how do I decide which one?"

what I'm saying is....that's fine but I'm telling you which one at the call site....using these "@" things...

so what's the problem?...the functional restriction is too restrictive.












________________________________________
From: Beginners [[hidden email]] on behalf of Sylvain Henry [[hidden email]]
Sent: 11 February 2017 13:36
To: [hidden email]
Subject: Re: [Haskell-beginners] how do typeclasses work again?

On 11/02/2017 09:50, Nicholls, Mark wrote:
> I don't understand the difference
>
> forall a isx. (Is isx a, Show a) => isx -> String
> and
> (Is isx a, Show a) => isx -> String
I have used the `forall` explicitly only to fix the order of the type
parameters (`a` and `isx`) so that we are sure to set the type of `a`
when we write (using TypeApplications): foo4 @Y (X,Y)

In the second declaration, the `forall` is implicit.

> I think my understanding of type classes is naïve, I just thought it meant that secretly a dictionary was being passed.
Yes your understanding is correct. The issue here is that the compiler
doesn't know the type of `a`, hence it can't select and pass the
appropriate instances.

> the compiler would identify the specific dictionary from the call site
Even at call site, the compiler can't infer the `a` type from the `isx`
type (nor from the return type of `foo4`).

Do you want the `a` type to be dependent on the `isx` type? I.e., to
only be allowed to define a single `Is isx a` instance for each `isx` type.

--
Sylvain
_______________________________________________
Beginners mailing list
[hidden email]
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners