[Ur] stateful structure?

Adam Chlipala adamc at impredicative.com
Thu Nov 3 08:05:50 EDT 2011


Gergely Buday wrote:
> Marc Weber wrote:
>
>    
>> 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"
>>      
> Adam, is it possible to write a new monad in Ur now? I vaguely
> remember that now only the transaction and the signal monad is
> supported.
>    

Yes; I added that feature fairly recently.  It's [Basis.mkMonad].

However, I wouldn't advise defining a new monad here.

> Marc wrote:
>    
>> >  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:
>>      
> Ok, but there is another problem here. How can I refer from an
> identifier to a value? A handy solution would be a hash table in perl
> or an array in other languages, but these are not available in Ur.
>    

I wouldn't advise taking Marc's choice of identifier literally.  You 
would represent [counter_id] as the data you need, rather than a key 
into a table containing that data.  I would just call the type [t], 
personally, and definitely not include "ID" anywhere in the name.

This seems just like what one often does in SML or OCaml, so you might 
want to consult resources on those languages.



More information about the Ur mailing list