[Haskell-beginners] Type Operator

Daniel Fischer daniel.is.fischer at web.de
Wed Jan 21 17:51:26 EST 2009


Am Mittwoch, 21. Januar 2009 23:30 schrieb Kellen J. McClain:
> 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?
>
I think you can't.
If it's possible, the best option would be to change the order of type 
parameters of Sample. If that's not possible, you can define

newtype FSample b a = FS (Sample a b)

and make that an instance of Monad.

Somebody remind me, why does Haskell not have type-lambdas?


More information about the Beginners mailing list