[Haskell-cafe] manage effects in a DSL
roma at ro-che.info
Mon Feb 10 17:22:20 UTC 2014
* Daniel Trstenjak <daniel.trstenjak at gmail.com> [2014-02-10 17:10:01+0100]
> Hi Dominique,
> On Mon, Feb 10, 2014 at 04:55:52PM +0100, Dominique Devriese wrote:
> > Hm. Interesting point, I guess this is the same problem as the whole
> > orphan instances debate... I didn't think of the connection to that
> > problem. Still, I'm convinced there are situations where local
> > instances are *exactly* what we need, so there must be some way to
> > avoid this problem...
> If a type class has a clear semantical meaning, what should then
> be the point of having multiple instances for the same data type?
> A clear semantical meaning contradicts multiple instances, they would
> only make reasoning about your code harder.
> The few use cases where it might be nice to be able to define a new
> instance aren't IMHO worth the drawbacks.
> You would just open Haskell for Ruby like monkey patching.
How about a compromise. We already have a way to introduce fresh type
names: using existential types. They look like good candidates for local
instances (by definition, they can't have global instances, because the
names themselves are necessarily local). Of course, you can define an
instance only once, as for usual types.
This would cover the case when "dynamic" instances are needed without
compromising soundness. Example:
data IsoInt = forall a . IsoInt (Int -> a) (a -> Int)
foo modulus =
case IsoInt id id of
IsoInt (fromInt :: Int -> a) toInt ->
eqMod x1 x2 = (toInt x1 - toInt x2) `mod` modulus == 0
-- note: a is rigid here
instance Eq a where
(==) = eqMod
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 819 bytes
Desc: Digital signature
More information about the Haskell-Cafe