[Haskell-cafe] One-shot? (was: Global variables and stuff)

Graham Klyne GK at ninebynine.org
Thu Nov 11 05:24:24 EST 2004


Keean,

As far as I can tell, both your solutions to the "one-shot" problem require 
that:

(a) the expression to be one-shotted is in the IO monad.  That seems 
reasonable, since why else does one care (semantically speaking)?

(b) they depend on the host operating system platform (semaphores, process 
id, environment variables) rather than pure Haskell language features.

Wouldn't it be easier to simply define "once" as a common Haskell library 
function?

#g
--

At 23:36 10/11/04 +0000, Keean Schupke wrote:
>Hi,
>    Here's another completely safe (and simpler way) to limit
>a computation to only happen once:
>
>    once' :: IO () -> IO ()
>    once' f = do
>       k <- getProcessID
>       a <- getEnv (showString "MyApp.Main" $ show k)
>       case a of
>          Just _ -> return ()
>          _ -> do
>             f
>             setEnv (showString "MyApp.Main" $ show k) "" False
>
>Actually both this and the semaphore example show that there is probably
>an alternative to allowing top-level '<-' type definitions - and that would be
>to have named-MVars in Haskell. It would be quite easy to code these as a
>small C library - and then FFI import the bindings to Haskell. I don't know
>whether from a 'purists' point of view whether this represents anything better
>than module-initialisations - but it does remove the diamond-inheritance style
>problem you get (if B imports A and C imports A and Main imports B and C,
>does A's init get run twice? are there two copies of the variables, each 
>initialised
>once, or one copy that gets initialised twice?). But either way the idea could
>be implemented without requiring changes to the language spec or the
>compilers, and would just be a library which you could use.
>
>    Keean.
>
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe

------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell-Cafe mailing list