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. -- Saurabh. _______________________________________________ Haskell-prime mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime |
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 <[hidden email]> 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 > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime > Haskell-prime mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime |
In reply to this post by Saurabh Nanda
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 [hidden email] http://www.joachim-breitner.de/ _______________________________________________ Haskell-prime mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-prime signature.asc (849 bytes) Download Attachment |
Free forum by Nabble | Edit this page |