an erlang monad?

Ulf Wiger ulf@REDACTED
Wed Mar 2 21:15:06 CET 2005


Den 2005-03-02 18:25:47 skrev Matthias Kretschmer <mccratch@REDACTED>:

> This is something I use monads for, too. Hiding some sort of state or  
> other information (serialization of operation or doing i/o is not that  
> much of a concern using Erlang :-)). I wrote a little parse_transform'er  
> to make the notation easier:
>
> monad(MT,
>   Action0,
>   Action1,
>   1 = mreturn(1),
>   Action2,
>   X = Action3,
>   Action4,
>   Action5(X)).
>
> this would be roughly like (assuming MT.>>= is an infix operation):
>
> Action0 MT.>>= fun (_) ->
> Action1 MT.>>= fun (_) ->
> MT.return(1) MT.>>= fun (1) ->
> Action2 MT.>>= fun (_) ->
> Action3 MT.>>= fun (X) ->
> Action4 MT.>>= fun (_) ->
> Action5(X) end end end end end
>
> trying to resemble basically Haskell's do-notation -- except that  
> instead of "X <- Action" "X = Action" (the MT is the monad type  
> specification -- basically a tuple containing the "bind"/">>=",  
> "return", etc. functions).

Great! Couple this with John Hughes' ?DELAY and ?FORCE macros,
and you've almost got yourself a real language (except for the
lack of static typing, of course)  ;-)


> This way you may even drop the fun (D) -> ... end and just write the
> expressions, using appropriate monad values for reading and writing the
> state (e.g. wrapping the basic dictionary operations).

I played around for a while with macros in order to avoid
having to write fun(D) -> ... end all the time, but eventually
I gave up.

/Uffe



More information about the erlang-questions mailing list