[Haskell-cafe] The Transient monad

Alberto G. Corona agocorona at gmail.com
Tue Dec 8 07:21:41 EST 2009

Hi haskell cafe:

concerning Stable Names


makeStableName :: a -> IO (StableName a)

I Did not test fully my proposal, and I´m thinking aloud, Just to inpire
others and fish some ideas;

The IO in makeStableName  suggest more side effects than makeStableName
really do. But still the call isn't pure.

For calls such are makeStableName that gives a different result the FIRST
time they are called but return the same result every time in the same
session,  I suggest to use a Transient monad:

makeStableName :: a -> Transient (StableName a)

The key here is to maintain the programmer aware that it is not pure, but
there are no IO and that  the results have no meaning from session to

Instance Monad Transient where
     Transient x ↠ f =  f  x
     return x = Transient x

We can Transient`ize IO calls by means of an implicit memoization:

liftT:: IO a -> Transient a
liftT= < whatever memoization code>

Memorization then is embedded in the monad transformation
This may be more elegant than  IO plus unsafePerformIO and is more
informative for the programmer. The transition form IO to pure can be done
in two steps, see below

  Instead of unsafePerformIO,  we can use:

unsafePurifyTransient :: Transient a -> a
unsafePurifyTransient  (Transient x) = x

for the inherently transient calls

A safer version of unsafePerformIO using implicit memoization could be:
unsafePerformIOT :: IO a -> a
unsafePerformIOT = unsafePurifyTransient  .  liftT

unsafePerformIOT guatantee that it returns the same value in the same

2009/12/8 Vladimir Zlatanov <vlado at dikini.net>

> <jeanchristophe.mincke at gmail.com> wrote:
> > I think lisp like symbols could be quite useful in the context of
> embedded
> > DSL to create ... well... symbols that can be interpreted as variables in
> > that DSL.
> Well for such use-case you could use either stable names, or recode
> them into a state monad- you will get either a global i.e. lisp like
> unique symbols, or their equivalent within a state context. Or some
> variation of the code posted previously in this thread.
> http://www.haskell.org/ghc/docs/6.10.4/html/libraries/base/System-Mem-StableName.html
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091208/22d521b2/attachment.html

More information about the Haskell-Cafe mailing list