Certainly nice, but it can be done in a different way, which seems more elegant for me: data OrdRest m a where OrdReturn :: a -> OrdRest m a OrdBind :: Ord a => m a -> (a -> OrdRest m b) -> OrdRest m b instance Monad (OrdRest m) where return = OrdReturn OrdReturn x >>= f = f x OrdBind mx g >>= f = OrdBind mx $ \x -> g x >>= f embed mx = OrdBind mx OrdReturn unembed (OrdReturn x) = ordReturn x unembed (OrdBind mx g) = ordBind mx $ unembed . gAnyway, it's kinda pattern, and being such it shows that Haskell isn't powerful enough.
I don't see any particular difference (in terms of elegance or otherwise) between doing the collapsing early or late, but obviously that's a matter of personal taste.
> I don't see any particular difference (in terms of elegance or otherwise) between doing the collapsing early or late, but obviously that's a matter of personal taste.
Reply
data OrdRest m a where
OrdReturn :: a -> OrdRest m a
OrdBind :: Ord a => m a -> (a -> OrdRest m b) -> OrdRest m b
instance Monad (OrdRest m) where
return = OrdReturn
OrdReturn x >>= f = f x
OrdBind mx g >>= f = OrdBind mx $ \x -> g x >>= f
embed mx = OrdBind mx OrdReturn
unembed (OrdReturn x) = ordReturn x
unembed (OrdBind mx g) = ordBind mx $ unembed . gAnyway, it's kinda pattern, and being such it shows that Haskell isn't powerful enough.
Reply
embed :: (Ord a) => m a -> OrdRest m a
unembed :: (OrdMonad m, Ord b) => OrdRest m b -> m b
Reply
It turns out that it is possible to use associated data types (GHC 6.8+) to abstract over this pattern: http://www.haskell.org/pipermail/haskell-cafe/2008-March/041084.html. I merged this with the above idea to make a general restricted monad library: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rmonad, but didn't get round to blogging about it.
Reply
Me neither, it's only a matter of program length.
> http://www.haskell.org/pipermail/haskell-cafe/2008-March/041084.html
Wow, missed that somehow. Beautiful.
Reply
Leave a comment