Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. how do typeclasses work again? (Nicholls, Mark) 2. Re: how do typeclasses work again? (Sylvain Henry) 3. Re: how do typeclasses work again? (David McBride) ---------------------------------------------------------------------- Message: 1 Date: Thu, 9 Feb 2017 16:59:00 +0000 From: "Nicholls, Mark" <nicholls.m...@vimn.com> To: "The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell" <beginners@haskell.org> Subject: [Haskell-beginners] how do typeclasses work again? Message-ID: <e7e7fdf32472ff48bb0e4d9dc4283d0e8bd01...@mtvne-exmb02.mtvne.ad.viacom.com> Content-Type: text/plain; charset="utf-8" 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. ------------------------------ Message: 2 Date: Thu, 9 Feb 2017 18:29:04 +0100 From: Sylvain Henry <sylv...@haskus.fr> To: beginners@haskell.org Subject: Re: [Haskell-beginners] how do typeclasses work again? Message-ID: <f4d2c60e-1537-ca7c-9c2e-d87ce8216...@haskus.fr> Content-Type: text/plain; charset=utf-8; format=flowed > 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 > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 3 Date: Thu, 9 Feb 2017 12:31:14 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] how do typeclasses work again? Message-ID: <can+tr437gzpozray52tpz6otqwcoakzubawewi0lpgq1t2h...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 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.m...@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@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 104, Issue 5 *****************************************