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. Type class instance with Num (Galaxy Being) 2. Re: Type class instance with Num (Bob Ippolito) ---------------------------------------------------------------------- Message: 1 Date: Sat, 3 Apr 2021 23:26:09 -0500 From: Galaxy Being <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Type class instance with Num Message-ID: <cafahfsvd8spajofah5+voktgaqhzlejzeo4kleev3xav8mv...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I'm following LYHFGG and I have this class YesNo a where yesno :: a -> Bool instance YesNo Int where yesno 0 = False yesno _ = True but then I have to specify Int here > yesno (5 :: Int) True Just with 5 gives this error Ambiguous type variable ‘a0’ arising from the literal ‘5’ prevents the constraint ‘(Num a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. I tried this instance YesNo (Num a) where yesno 0 = False yesno _ = True but got cryptic errors. What can I do to make yesno take any of Num's numbers? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210403/8c2f8a3d/attachment-0001.html> ------------------------------ Message: 2 Date: Sat, 3 Apr 2021 21:39:23 -0700 From: Bob Ippolito <b...@redivi.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Type class instance with Num Message-ID: <cacwmpm93hhk4-eyi9fxnmer2bm86xpzuz_h-6hmlkoczwye...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" You need something like this: {-# LANGUAGE FlexibleInstances, UndecidableInstances #-} class YesNo a where yesno :: a -> Bool instance (Num a, Eq a) => YesNo a where yesno = (/= 0) The reason this doesn't work without turning on some "scary" flags is that you can easily write code that is ambiguous since typeclasses are open. Open means that some other file can define a data type that has an instance of Num and an instance for YesNo and then there's no obvious choice which instance should be used. If you want a bit more detail, here's a relevant StackOverflow question: https://stackoverflow.com/questions/8877541/how-to-write-an-instance-for-all-types-in-another-type-class On Sat, Apr 3, 2021 at 9:26 PM Galaxy Being <borg...@gmail.com> wrote: > I'm following LYHFGG and I have this > > class YesNo a where > yesno :: a -> Bool > > instance YesNo Int where > yesno 0 = False > yesno _ = True > > but then I have to specify Int here > > > yesno (5 :: Int) > True > > Just with 5 gives this error > > Ambiguous type variable ‘a0’ arising from the literal ‘5’ > prevents the constraint ‘(Num a0)’ from being solved. > Probable fix: use a type annotation to specify what ‘a0’ should be. > > I tried this > > instance YesNo (Num a) where > yesno 0 = False > yesno _ = True > > but got cryptic errors. What can I do to make yesno take any of Num's > numbers? > > LB > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210403/473ae724/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 153, Issue 1 *****************************************