[Haskell-cafe] Replacing [a] with (Set c a) in Monad instance.

Bryan Donlan bd.haskell at uguu.us
Tue Jan 30 20:06:03 EST 2007


Daniel McAllansmith wrote:
> Hello.
> 
> Given:
> 
> newtype Dist a = D {unD :: [(a,Int)]}
> 
> instance Monad Dist where
>   return x = D [(x,1)]
>   d >>= f  = D [(y,q*p) | (x,p) <- unD d, (y,q) <- unD (f x)]
>   fail _   = D []
> 
> 
> How would one change Dist to wrap an instance of the (Data.Edison.Set c a) 
> typeclass so that the Monad instance could be implemented in terms of e.g. 
> singleton, unionWith, empty, etc?

I don't know about Data.Edison.Set, but if it's anything like 
base/Data.Set, then there's an Ord constraint on the elements, making it 
impossible to directly transform into a monad. However, Roberto Zunino 
came up with a clever way to bypass this problem with GADTS:
http://article.gmane.org/gmane.comp.lang.haskell.cafe/18118

You may be able to apply this to your situation, using various Edison 
collections depending on which typeclasses your monad argument implements.


More information about the Haskell-Cafe mailing list