<div dir="ltr">Hi,<div>
</div><div><br></div><div>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!</div><div><br></div><div>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**</div><div><br></div><div>    If a type has an instance of type-class `a`, then here's how you can get an instance of type-class `b`</div><div><br></div><div>Without the ability to express this idea, you end up with a **lot** of type-class boilerplate, (even if you use GHC Generics) eg:</div><div><br></div><div>    data State = data State = Inactive | Incomplete | Deleted | Trial | Unpaid | Paid</div><div>      deriving (Eq, Show, Generic)</div><div><br></div><div>   instance AppEnum State where</div><div>       toString = gEnumToString</div><div>       fromString = gEnumFromString</div><div><br></div><div>   -- You are still forced to do the following for **every single type** even though, conceptually, this can be driven by a rule</div><div><br></div><div>    instance ToJSON State where</div><div>      toJSON s = toJSON $ toString x</div><div><br></div><div><div>    instance FromJSON State where</div><div>      parseJSON (Aeson.String s) = fromString s</div></div><div>      parseJSON _ = fail "unexpected type"</div><div><br></div><div><div><div>    instance FromFIeld State where</div></div></div><div>      fromField _ mBS = case mBS of </div><div>        Nothing -> fail "not expecting a NULL/Nothing"</div><div>        (Just bs) -> pure $ fromString bs</div><div><br></div><div>    instance ToField State where</div><div>      toField s = toField $ toString s</div><div><br></div><div>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.</div><div><br></div><div>[1] <a href="https://stackoverflow.com/questions/3213490/how-do-i-write-if-typeclass-a-then-a-is-also-an-instance-of-b-by-this-definit">https://stackoverflow.com/questions/3213490/how-do-i-write-if-typeclass-a-then-a-is-also-an-instance-of-b-by-this-definit</a></div><div><br></div><div>-- Saurabh.</div><div><br></div></div>