From saurabhnanda at gmail.com Fri Jun 9 10:15:08 2017 From: saurabhnanda at gmail.com (Saurabh Nanda) Date: Fri, 9 Jun 2017 15:45:08 +0530 Subject: =?UTF-8?Q?=E2=80=9CIf_typeclass_a=2C_then_a_is_also_an_instance_of_b_b?= =?UTF-8?Q?y_this_definition=2E=E2=80=9D?= Message-ID: Hi, At the outset I would like to mention that I do not completely understand how type-class instance resolution works, and why it works that way. I have tried reading up, but I don't completely understand WHY it works that way. So, please feel free to ask me to RTFM if what I'm about to say doesn't make any sense, although TFM doesn't make sense to me! >From a pragmatic standpoint is there any way to express the following idea in Haskell (or a future version of Haskell), **with minimal amount of boilerplate** If a type has an instance of type-class `a`, then here's how you can get an instance of type-class `b` Without the ability to express this idea, you end up with a **lot** of type-class boilerplate, (even if you use GHC Generics) eg: data State = data State = Inactive | Incomplete | Deleted | Trial | Unpaid | Paid deriving (Eq, Show, Generic) instance AppEnum State where toString = gEnumToString fromString = gEnumFromString -- You are still forced to do the following for **every single type** even though, conceptually, this can be driven by a rule instance ToJSON State where toJSON s = toJSON $ toString x instance FromJSON State where parseJSON (Aeson.String s) = fromString s parseJSON _ = fail "unexpected type" instance FromFIeld State where fromField _ mBS = case mBS of Nothing -> fail "not expecting a NULL/Nothing" (Just bs) -> pure $ fromString bs instance ToField State where toField s = toField $ toString s I tried talking to people on IRC and also trawling StackOverflow [1] and my take-away is essentially, that Haskell cannot express this idea. However, my submission is, that it is a very useful idea to express and probably the next version of the language should do something to make the lives of programmers a little easier. [1] https://stackoverflow.com/questions/3213490/how-do-i-write-if-typeclass-a-then-a-is-also-an-instance-of-b-by-this-definit -- Saurabh. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike at izbicki.me Fri Jun 9 14:44:13 2017 From: mike at izbicki.me (Mike Izbicki) Date: Fri, 9 Jun 2017 07:44:13 -0700 Subject: =?UTF-8?Q?Re=3A_=E2=80=9CIf_typeclass_a=2C_then_a_is_also_an_instance_of?= =?UTF-8?Q?_b_by_this_definition=2E=E2=80=9D?= In-Reply-To: References: Message-ID: I've also wanted this feature in the past. I think the DefaultSignatures language extension is the closest thing. There's a recent answer to your stackoverflow question that explains how it works: https://stackoverflow.com/questions/3213490/how-do-i-write-if-typeclass-a-then-a-is-also-an-instance-of-b-by-this-definit/44456791#44456791 On Fri, Jun 9, 2017 at 3:15 AM, Saurabh Nanda wrote: > Hi, > > At the outset I would like to mention that I do not completely understand > how type-class instance resolution works, and why it works that way. I have > tried reading up, but I don't completely understand WHY it works that way. > So, please feel free to ask me to RTFM if what I'm about to say doesn't make > any sense, although TFM doesn't make sense to me! > > From a pragmatic standpoint is there any way to express the following idea > in Haskell (or a future version of Haskell), **with minimal amount of > boilerplate** > > If a type has an instance of type-class `a`, then here's how you can get > an instance of type-class `b` > > Without the ability to express this idea, you end up with a **lot** of > type-class boilerplate, (even if you use GHC Generics) eg: > > data State = data State = Inactive | Incomplete | Deleted | Trial | > Unpaid | Paid > deriving (Eq, Show, Generic) > > instance AppEnum State where > toString = gEnumToString > fromString = gEnumFromString > > -- You are still forced to do the following for **every single type** > even though, conceptually, this can be driven by a rule > > instance ToJSON State where > toJSON s = toJSON $ toString x > > instance FromJSON State where > parseJSON (Aeson.String s) = fromString s > parseJSON _ = fail "unexpected type" > > instance FromFIeld State where > fromField _ mBS = case mBS of > Nothing -> fail "not expecting a NULL/Nothing" > (Just bs) -> pure $ fromString bs > > instance ToField State where > toField s = toField $ toString s > > I tried talking to people on IRC and also trawling StackOverflow [1] and my > take-away is essentially, that Haskell cannot express this idea. However, my > submission is, that it is a very useful idea to express and probably the > next version of the language should do something to make the lives of > programmers a little easier. > > [1] > https://stackoverflow.com/questions/3213490/how-do-i-write-if-typeclass-a-then-a-is-also-an-instance-of-b-by-this-definit > > -- Saurabh. > > > _______________________________________________ > Haskell-prime mailing list > Haskell-prime at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime > From mail at joachim-breitner.de Fri Jun 9 20:11:54 2017 From: mail at joachim-breitner.de (Joachim Breitner) Date: Fri, 09 Jun 2017 22:11:54 +0200 Subject: =?UTF-8?Q?=E2=80=9CIf?= typeclass a, then a is also an instance of b by this =?UTF-8?Q?definition=2E=E2=80=9D?= In-Reply-To: References: Message-ID: <1497039114.6233.1.camel@joachim-breitner.de> Hi, Am Freitag, den 09.06.2017, 15:45 +0530 schrieb Saurabh Nanda: >     If a type has an instance of type-class `a`, then here's how you > can get an instance of type-class `b` this has been proposed as “default superclass instances”, see https://ghc.haskell.org/trac/ghc/wiki/DefaultSuperclassInstances I am not sure what the status is, and how actively it is pushed for. It is certainly compelling. Joachim -- Joachim Breitner mail at joachim-breitner.de http://www.joachim-breitner.de/ -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: This is a digitally signed message part URL: