2 ms·
As others have said, the key is to solve the expression problem without discarding type safety. :) I think that this problem is basically a simple structural
by kthielen 12y ago
As others have said, the key is to solve the expression problem without discarding type safety. :)
I think that this problem is basically a simple structural relation on sum types in disguise. That's how I interpret the "Data Types a la Carte" paper anyway.
So if you have:
class Foo a where
foo :: a -> String
And say:
instance Foo A+B where
foo A = "a"
foo B = "b"
And:
instance Foo C+D where
foo C = "c"
foo D = "d"
Then if you extend your data type to A+B+C+D, then a simple instance generator should be enough to tie it all together:
instance (Foo v, Foo w) => Foo v+w where
foo (inl v') = foo v'
foo (inr w') = foo w'
I have a (currently internal to my company) Haskell-like language with structural sum/product/variant/record types where we can do this kind of thing. Otherwise "Data Types a la Carte" has the solution in the typed setting of vanilla Haskell (even if it's a little wordy).