Do you really need a class? Maybe, a simple data type would do?

So, instead of

class MyMonad m where
    myVal1 :: m a
    myVal2 :: m a -> m [a]
instance Monad m => MyMonad (MyMonadT m) where
    myVal1 = foo
    myVal2 = bar

you can write (in your first package) something like

data MyMonad m = MyMonad {myVal1 :: forall a. m a, myVal2 :: forall a. m a -> m 
[a]}
myMonadT :: Monad m => MyMonad m
myMonadT = MyMonad {myVal1 = foo, myVal2 = bar}

Then you can define something like

myMonadParsec :: MyMonad Parser
myMonadParsec = ...

and use it wherever you want your instance.

There are two disadvantages:

1) It's not Haskell98, since we use forall's.

2) You have to explicitly state what instance you define.

Personally, I don't care about the first one, and the second one doesn't seem bad enough to outweight the benefit of not having orphan instances. Usually, you can restore most of Haskell's automatic choice of instance by using some upper-level classes.

Note also that you can have both the class and the data type.

Martijn van Steenbergen wrote on 05.06.2009 13:54:
Hello,

Suppose I have two projects: 1) one that defines a monad transformer and an accompanying type class that captures my monad-specific operations and 2) one that uses the other project, combining the monad transformer with, say, Parsec.

Now while writing my Parsec parser I want to use my monad transformer operations without using lift: I need an instance MyMonadT Parsec. Where should this instance go? I can think of three answers, all unsatisfactory:

1) For obvious reasons it shouldn't go in the Parsec package.

2) For pretty much the same reasons it shouldn't go in my monad transformer package, either. Also, it is undesirable to add a dependency on Parsec just for this instance, and the package should not have to know about the projects that are going to use it.

3) If I put it in the second project it is an orphan instance, which is undesirable for well-known reasons.

What is the best solution?

Thank you,

Martijn.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to