[Ur] stateful structure?

Marc Weber marco-oweber at gmx.de
Wed Nov 2 12:00:54 EDT 2011


Excerpts from Gergely Buday's message of Wed Nov 02 16:39:52 +0100 2011:
> is it possible to have a stateful structure in Ur, using the transaction monad?
> [...]
> structure Stateful =
> struct
>   val init : {} -> transaction {}
>   val increment : {} -> transaction xbody
> end

Possible? Sure: You can serialize to string/binary and store somewhere
in C .. (ugly, broken, because you can use it only once per request thus
one counter).

You want this interface instead:

structure Stateful =
struct
  val init : {} -> transaction counter_id
  val increment : counter_id -> transaction xbody
end

There is no big difference whether you pass a counter_id or the counter
value. Thus you can use the state monad way:

Usually you do

fun do_work args state = 
  let
    result = state +1 # state is a counter in this simple example
    new_state = state 
  in
    (result, new_state) // tuple
  end
end

Then you return new state. Because passing state is annoying Haskell
introduces the State monad. So all you have to do is rewrite the state
monad in Ur. the init function is then called something like
"runWithState f initialState"

That's the pure functional way of handling state. If you have a second
glance at the manually you'll notice that urweb interferes with JS. You
can mark JS functions as statefull and and ur compile will treat is
specially (I hope that Adam can correct me if I'm wrong).
But still you'll notice that your definition is kind of broken if you
need many counters.

I hope that points you into the right direction.

Marc Weber



More information about the Ur mailing list