[Haskell] Top Level <-
ashley at semantic.org
Sun Aug 24 19:12:18 EDT 2008
Is there any interest in implementing a top level "<-" to run monadic code?
Currently this sort of thing is done with unsafePerformIO and switching
off inlining with some pragma. Indeed, the 'atomically' haddock actually
advises doing this to declare top-level TVars. The same trick is used in
the source of Data.Unique and System.Random. This is bad Haskell.
To avoid observation of effects, etc., we would need a new monad rather
than IO. There would be equivalents of IO functions such as these:
I can think of two uses:
1. Global mutable state. For instance, here's the count variable for
uniqSource :: MVar Integer
uniqSource <- newMVarTL 0
Isn't that much nicer?
2. Solving the expression problem using open witnesses, a recent
hobby-horse of mine. For instance, here's a simple scheme for extensible
exceptions using top-level "<-":
-- declare exception carrying an Int
myException :: Exn Int <- newExn
-- throw with 5
foo = do
throw myException 5
-- catch, print the Int
bar = catch foo myException (\i -> putStrLn (show i))
I already have code for this, except with an unsafe hack to do the "<-"
My open-witness package, which also shows how to do Typeable/Dynamic safely:
More information about the Haskell