Vala and Monads

I've been reading around lately about functional monads written in imperative languages, then I've tried to bring them to Vala. I'm not going to explain what a monad is, there's a lot of papers out there.

If you want to test the code below you need Vala git master (more recent than 0.9.2).

First of all we need a basic framework and some monads you can immediately read here.
Notice some comments are in do notation and that related functions are really only utilities to let the example code below be more readable, they respect the functional semantics of monads without adding more imperative power.

The only difference with other implementations is that BindFunc not only passes the underlying value but also the return function. This is helpful to avoid keeping monads around in termporary variables.

Now let's port some Haskell typic examples to Vala:

safe_div a b = if b == 0 then Nothing else Just (div a b)

Monad safe_div (int a, int b) { if (b == 0) return nothing; return new Just (a/b); }

The nothing variable is initialized with Nothing.instance which is a singleton.
Now let's map a simple multiplication function:

bind_and_mul monad factor = do { x <- monad; return (x*factor); }

Monad bind_and_mul (Monad monad, double factor) {
return monad.bind ((v,unit) => unit (((int)v) * factor)) as Monad;

Pretty easy, we get the unpacked value and multiply it with the given value.

Things get a bit longer with list monads. We want that given an x the function returns [x,x+1]:

bind_and_list_plus monad val = do { x <- monad; return [x,x+1]; }

Monad bind_and_list_plus (Monad monad, int val) {
return monad.bind ((x,unit) => {
var l = new List (); l.add (x); l.add (((int)x)+val); return unit(l);
Ok, but what is the result of those weird definitions?

Haskell: do { x <- safe_div 5 2; return (x*2); }
Vala: bind_and_mul (safe_div(5,2), 2);
Result: Just (4)
What if we divide by zero?

do { x <- safe_div 5 0; return (x*2); }
bind_and_mul (safe_div(5,0), 2);
Result: Nothing
Transform -10 into some useless list:

do { x <- -10; return [x,x+1]; }
bind_and_list_plus (new Just (-10), 1);
Result: [-10,-9]

Now do the same transformation for a list:

Haskell: do { x <- [1,2,3]; return [x,x+2]; }
Vala: var l = new List(); l.add(1); l.add(2); l.add(3);
bind_and_list_plus (l, 2);
Result: [[1,3],[2,4],[3,5]]

Again, the complete code with test cases can be found here.


Vala 0.9.2 has been released

this new release follows the huge changes coming along with GLib and GTK+ latest releases:
  • Initial support for GDBus-based clients and servers.
  • Support implicit and explicit GVariant casts.
  • Add support for [Deprecated] attribute.
  • Add GenericArray as alternative binding to GPtrArray.
  • Add gdk-pixbuf-3.0, gdk-3.0, gdk-x11-3.0, and gtk+-3.0 bindings.
  • Updates to the GLib bindings.
  • Many bug fixes.
Stay tuned with furter updates.


Vala 0.9.1 is out

yesterday the announce of the 0.9.1 release of Vala. You can find release notes here.
This is more like a transitional development release torward a new 0.8.x branch stable release.


  • Support constants in enums.
  • Deprecate +=/-= syntax to connect/disconnect signal handlers.
  • Add experimental support for Dova profile.
  • Update Genie parser (Jamie McCracken).

  • Add clutter-gst-1.0 bindings (Ali Sabil).
  • Add gdu and gdu-gtk bindings.
  • Add libesmtp bindings (Adrien Bustany).
  • Add mx-1.0 bindings (Evan Nemerson).
  • Add orc-0.4 bindings (Fabian Deutsch).
  • Add rest-extras-0.6 bindings (Adrien Bustany).
  • Updates to the GLib, GStreamer, Linux, SQLite, and other bindings.
  • Many bug fixes.