[Haskell-cafe] Re: Monads in Java, Joy, OCaml, Perl, Prolog, Python, Ruby, and Scheme was Re: Other languages using monads?

Geoffrey Alan Washburn geoffw at cis.upenn.edu
Fri Nov 25 21:56:46 EST 2005


Shae Matijs Erisson wrote:
> Gregory Woodhouse <gregory.woodhouse at sbcglobal.net> writes:
> 
>> My knowledge of functional programming is pretty much limited to Haskell,
>> Scheme, and a smattering of Common Lisp. Are there languages other than
>> Haskell that explicitly use monads? How about "not so explicitly"?
> 
> Java http://www.ccs.neu.edu/home/dherman/code/monads/JavaMonads.tar.gz
> Joy http://permalink.gmane.org/gmane.comp.lang.concatenative/1506
> OCaml https://mailman.rice.edu/pipermail/metaocaml-users-l/2005-March/000057.html
> Perl http://sleepingsquirrel.org/monads/monads.html
> Prolog http://logic.csci.unt.edu/tarau/research/PapersHTML/monadic.html
> Python http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439361
> Ruby http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html
> Scheme http://www.ccs.neu.edu/home/dherman/research/tutorials/monads-for-schemers.txt
> 
> Please respond with any language implementations I've missed.

	The Java implementation is actually a little baroque because it doesn't 
make use of generics.  In June when there was a question here at Penn 
about how critical type-classes were to the use of Monads, I put 
together this quick example:

interface Monad<A> {
   public <B> Monad<B> unit(B e);
   public <B,C> Monad<C> bind(Monad<B> e, MonadBindFun<B,C> f);
}

interface MonadBindFun<A,B> {
   public Monad<B> fun(A x);
}

class OptionMonad<A> implements Monad<A> {

   public <B> Monad<B> unit(B e) {
     return new Some<B>(e);
   }

   public <B,C> Monad<C> bind(Monad<B> e, MonadBindFun<B,C> f) {
     if(e instanceof Some) {
       Some<B> eb = (Some<B>)e;
       return f.fun(eb.get());
     } else if (e instanceof None) {
       return new None<C>();
     } else {
       throw new Error();
     }
   }
}

public class None<A> extends OptionMonad<A> implements Monad<A> {
   public None() { }
}

public class Some<A> extends OptionMonad<A> implements Monad<A> {
   A x = null;
   public Some(A x) {
     this.x = x;
   }

   public A get() { return x; }
}


Just a few minutes ago I wrote, but haven't tested the following:


import java.util.*;

public class ListMonad<A> implements Monad<A> {

     List<A> list = null;

     private ListMonad(List<A> list) {
       this.list = list;
     }

     public <B> ListMonad<B> unit(B e) {
       List<B> nlist = new LinkedList<B>();
       nlist.add(e);
       return new ListMonad(nlist);
     }

     public <B,C> Monad<C> bind(Monad<B> e, MonadBindFun<B,C> f) {
       if(e instanceof ListMonad) {
         ListMonad<B> be = (ListMonad<B>)e;
         List<List<C>> reslists = new LinkedList<List<C>>();
         for(B x : be.list) {
            Monad<C> res = f.fun(x);
            if(res instanceof ListMonad) {
              ListMonad<C> ce = (ListMonad<C>)res;
              reslists.add(ce.list);
            } else {
              throw new Error();
            }
         }
         List<C> flat = new LinkedList<C>();
         for(List<C> x : reslists) {
           flat.addAll(x);
         }
         return new ListMonad(flat);
       } else {
         throw new Error();
       }
     }
}


I also threw together a slightly cleaner version of these examples using 
Scala, but I'm sure the Scala folks could do better than me.



More information about the Haskell-Cafe mailing list