[Haskell-cafe] A curios monad
ryani.spam at gmail.com
Fri Dec 12 13:22:23 EST 2008
On Fri, Dec 12, 2008 at 2:48 AM, Martijn van Steenbergen
<martijn at van.steenbergen.nl> wrote:
> I've worried about this but I couldn't find a good code example of when this
> goes wrong. Can you? Without using any of the unsafeXxx functions, of
> Maybe I should build my monad on top of the ST monad if that makes things
Here's a simple example:
runMud :: Mud a -> a
runMud = flip evalState emptyMud
main = do
let v = runMud (mkVar "hello")
let crash = runMud $ do
v2 <- mkVar True -- v2 :: Var Bool
res <- readVar v -- v :: Var String
print crash -- boom!
Both v2 and v are the same variable index (0), but different types.
Since there's nothing preventing the variable from escaping from the
first "runMud", we can import it into the second one where it fails.
The key is that both use the same "initial state", so you have lost
the uniqueness of variables.
ST is safer, although you can make these systems just as safe with the
phantom "state" type like ST does. It's also faster; variables turn
directly into pointer references, instead of traversing an IntMap.
But it gets kind of annoying writing all the extra "s" on your type
More information about the Haskell-Cafe