Monads in LispyScript

74 views
Skip to first unread message

Dominikus Herzberg

unread,
Mar 5, 2013, 9:44:58 AM3/5/13
to lispy...@googlegroups.com, dominikus. herzberg
I never got a deep understanding of monads -- just a rough idea. I think, LispyScript starts helping me to get into monads. From reading the code in


I learnt that the macro doMonad transforms any code of the form

(doMonad <monad>
  (<var1> <val1>
   <var2> <val2>)
  <expr>)

into 

(mBind <val1>
  (function (<var1>)
    (mBind <val2>
      (function (<var2>)
        (mResult <expr>) | mZero ))))

For the sake of brevity I limited the number of bindings to two. The last line of the resolved expression indicates that mZero is being used as a value if possible; otherwise mResult is applied on <expr>.

With this understanding the identityMonad

(macro identityMonad ()
  (object
    "mBind" (function (mv mf) (mf mv))
    "mResult" (function (v) v)))

makes perfect sense. The following interaction

lispy> (doMonad identityMonad (a 1 b (* a 2)) (+ a b))
3

corresponds to

lispy> ((function (a) ((function (b) ((function (v) v) (+ a b))) (* a 2))) 1)
3

Two questions:

Writing mondas seems to be hard, even somewhat counterintuitive. How does one come up with writing the code for the stateMonad or the arrayMonad? Is there any trick?

Can anybody help me formulating the monadic laws in LispyScript?

Any help is appreciated! It's really fun reading LispyScript code to learn monads!

Dominikus

Santosh Rajan

unread,
Mar 5, 2013, 10:01:48 AM3/5/13
to lispy...@googlegroups.com
Monads are abstractions of computations. eg. the arrayMonad is a specific abstraction of array computations. Using monads are easy once you get it. Monads can also be used to abstract away program structure.

The "lispy" command line parser is implemented using the maybeMonad (lispy.ls). Usually writing a command line parser involves using iterators, if conditions and switch statements. See how the maybeMonad has abstracted away all that and reduced the computations into a simple serial elimination of all possibilities.


Writing mondas seems to be hard, even somewhat counterintuitive. How does one come up with writing the code for the stateMonad or the arrayMonad? Is there any trick?

I didn't quite understand the above questions.

 

Can anybody help me formulating the monadic laws in LispyScript?


This will be a good exercise.



 
om it, send an email to lispyscript...@googlegroups.com.
To post to this group, send an email to lispy...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msg/lispyscript/-/gv3moew8o9oJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Santosh Rajan
@About.Me

Dominikus Herzberg

unread,
Mar 6, 2013, 5:09:20 AM3/6/13
to lispy...@googlegroups.com

Writing mondas seems to be hard, even somewhat counterintuitive. How does one come up with writing the code for the stateMonad or the arrayMonad? Is there any trick?

I didn't quite understand the above questions.

What I meant is the following: Writing the code for say the StateMonad or ContinuationMonad doesn't feel obvious to me. I still have to wrap my mind around these Monads -- but I'm making progress. 
 
 

Can anybody help me formulating the monadic laws in LispyScript?


This will be a good exercise.

Ok, I did the exercise. The monad laws demand equality of the following terms of LispyScript:

(mBind (mResult m) f) = (f m)
(mBind m mResult) = m
(mBind (mBind m f) g) = (mBind m (function (x) (mBind (f x) g)))

Last night I learnt a lot more about monads. Today I'm lacking time but it's not unlikely that I'll post some of my insights during the next days -- I still do have some questions.

Dominikus

Santosh Rajan

unread,
Mar 6, 2013, 5:31:01 AM3/6/13
to lispy...@googlegroups.com

Good work. I might use some of your stuff to add to the docs.


What I meant is the following: Writing the code for say the StateMonad or ContinuationMonad doesn't feel obvious to me. I still have to wrap my mind around these Monads -- but I'm making progress. 
 

Dominikus Herzberg

unread,
Mar 6, 2013, 8:32:58 AM3/6/13
to lispy...@googlegroups.com
Thanks a lot for the links, Santosh. Since I'm familiar with Clojure, I hope the postings to be helpful.
 
Dominikus

Dominikus Herzberg

unread,
Mar 7, 2013, 4:21:49 PM3/7/13
to lispy...@googlegroups.com
When I started reading this tutorial today


it clicked. Since I love concatenative programming languages which are based on function composition, the example in the tutorial helped me immediately getting the point. Essentially, a monad applied on a sequence of functions

(f,g,h) monad do-monad

transforms into

m-result ; 'f ; m-bind ; 'g ; m-bind ; 'h ; m-bind

(Note: ; notes function composition and ' quotes the function preventing it from being executed).

That's easy to understand. Now your statement "Monads are abstractions of computations." makes sense to me. m-bind can e.g. collect the functions, rearrange them etc. It's the old paradigm that functions treated as data can be manipulated in any way (obeying the monadic laws).

Thanks a lot,

Dominikus
Reply all
Reply to author
Forward
0 new messages