[Haskell] Top Level <-

Ashley Yakeley 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:

   newIORef
   newMVar
   newTVarIO
   newUnique

I can think of two uses:

1. Global mutable state. For instance, here's the count variable for 
Data.Unique rewritten:

   uniqSource :: MVar Integer
   uniqSource <- newMVarTL 0

Isn't that much nicer?
<http://haskell.org/haskellwiki/Top_level_mutable_state>

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 "<-" 
declaration:
<http://hackage.haskell.org/packages/archive/open-witness/0.1.1/doc/html/Data-OpenWitness-Exception.html>

My open-witness package, which also shows how to do Typeable/Dynamic safely:
<http://hackage.haskell.org/cgi-bin/hackage-scripts/package/open-witness>

-- 
Ashley Yakeley
Seattle, WA



More information about the Haskell mailing list