dex-lang icon indicating copy to clipboard operation
dex-lang copied to clipboard

Automatically deriving instances

Open midfield opened this issue 3 years ago • 1 comments

Similar to Haskell's deriving Eq et al, it would be nice to be able to derive boilerplate type class implementations.

See https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/deriving.html

midfield avatar Jun 09 '22 16:06 midfield

Yes, let's do it. I suggest we make it more like Haskell's "standalone deriving" at first because it will be simpler to implement if the compiler isn't responsible for figuring out what constraints are required. If the feature is able to derive superclasses automatically, in the spirit of #481, then there'll be less boilerplate anyway - e.g. you just write deriving Monoid instead of deriving (Monoid, Semigroup).

User-defined ADTs are implemented as a "newtype" wrapper around an underlying representation of sums and products, so the mechanism for deriving can just look for instances for those representations and adapt them accordingly. Rather than trying to insert appropriate coercions in the method implementations, let's just add another case to DictExpr, DictExpr n = ... | NewtypeDict (DictType n) (DictExpr n). Then when we actually project out the methods in simplification, the newtype coercion becomes a no-op.

dougalm avatar Feb 15 '23 21:02 dougalm