[Haskell-beginners] Type Operator

Kellen J. McClain kjmcclain at comcast.net
Wed Jan 21 17:30:12 EST 2009


I have a quick question.

Recall that:
class Monad m where
	(>>=) :: m a -> (a -> m b) -> m b
	...

and suppose I have a data type Sample:

data Sample a b = ...

how could I define Sample to be an instance of Monad such that:

(>>=) :: Sample a c -> (a -> Sample b c) -> Sample b c

?

I would like to use a (\a -> ...)-like operator, but for types. 
So, something like this:

instance Monad (\a -> Sample a c) where
	(>>=) :: Sample a c -> (a -> Sample b c) -> Sample b c
	a >>= f = ...

but that obviously doesn't work. Alternatively I would 
like to use a type declaration and partially apply it:

type SampleFlip b a = Sample a b
instance Monad (SampleFlip c) where
	(>>=) :: SampleFlip c a -> (a -> SampleFlip c b) -> SampleFlip c b

which translates to:

	(>>=) :: Sample a c -> (a -> Sample b c) -> Sample b c

But this doesn't work either, and ghc extensions don't add this functionality.  
Can I do this in Haskell?



More information about the Beginners mailing list