experimental Implementation of Haskell List Functions with Java 8

Brian Goetz brian.goetz at oracle.com
Sat May 19 10:11:27 PDT 2012


Do you know what the types involved are?  What are we trying to cast to 
what, and where?

On 5/19/2012 7:02 AM, Sven Eric Panitz wrote:
> Hello all,
> this is my first post to this list and I am new to the list. So please
> excuse, if I bring up topics,
> which have allready been discussed ages ago.
>
> After a first look at the current status of project Lambda
> <http://openjdk.java.net/projects/lambda/>  JSR 335, I found it a nice
> exercise to implement the complete Haskell Prelude list functions with
> java 8.
>
> The result of my efforts can be found on my web site at:
> http://panitz.name/paper/haskellPrelude
>
> With my class haskell.data.Iterables, now objects of type Iterable<A>
> can make use of all Haskell list functions.
> Mayby this quick implementation is interesting for those guys, that
> develope the library for Java 8.
>
>
>
> I am not sure, but I might have found a bug in the current lambda
> compiler. At least I get a
> ClassCastException during runtime, which I cannot explain.
>
> It comes up in my implementation of the function »permutations« (which
> is the most complicated
> of all the Haskell functions I implemented).
>
> The following implementation in terms of a private auxiliary method
> interleave_ compiles und runs
> without problem:
>
>     static public<A>  Iterable<Iterable<A>>  permutations(Iterable<A>  xs0){
>       Function2<Iterable<A>,Iterable<A>,Iterable<Iterable<A>>>  perms =
>       (tso,is) ->  {
>         if (tso.isEmpty()) return new Nil<Iterable<A>>();
>         A t = head(tso);
>         Iterable<A>  ts = tail(tso);
>
>         return
> foldr((xs,r)->interleave_(t,ts,(u)->u,xs,r).e2,perms.eval(ts,cons(t,is)),permutations(is));
>       };
>
>       return cons(xs0,perms.eval(xs0,new Nil<A>()));
>     }
>     static private<A>  Tuple2<Iterable<A>,Iterable<Iterable<A>>>  interleave_
>            (A t,Iterable<A>  ts,Function1<Iterable<A>  ,Iterable<A>>f
> ,Iterable<A>  xs,Iterable<Iterable<A>>  r){
>       if (xs.isEmpty()) return new
> Tuple2<Iterable<A>,Iterable<Iterable<A>>>(ts,r);
>       A y = head(xs);
>       Iterable<A>  ys = tail(xs);
>       Tuple2<Iterable<A>,Iterable<Iterable<A>>>
> uszs=interleave_(t,ts,(x)->f.eval(cons(y,x)),ys,r);
>       return new
> Tuple2<Iterable<A>,Iterable<Iterable<A>>>(cons(y,uszs.e1),cons(f.eval(cons(t,cons(y,uszs.e1))),uszs.e2));
>     }
>
> (Tuple2 is a generic class for pairs, Function(1-5) are interfaces for
> functions of arity 1 to 5.
> These are found as well on my web site.)
>
> The following alternative Implementation with a local function for the
> auxiliary function interleave_,
> compiles but gets a ClassCastException during runtime:
>
>     static public<A>  Iterable<Iterable<A>>  permutations2(Iterable<A>  xs0){
>       Function2<Iterable<A>,Iterable<A>,Iterable<Iterable<A>>>  perms =
>       (tso,is) ->  {
>         if (tso.isEmpty()) return new Nil<Iterable<A>>();
>
>         Function5<A,Iterable<A>,Function1<Iterable<A>
> ,Iterable<A>>,Iterable<A>,Iterable<Iterable<A>>,Tuple2<Iterable<A>,Iterable<Iterable<A>>>>
> interleave_ =
>         (t,ts,f,xs,r) ->  {
>           if (xs.isEmpty()) return new
> Tuple2<Iterable<A>,Iterable<Iterable<A>>>(ts,r);
>           A y = head(xs);
>           Iterable<A>  ys = tail(xs);
>           Tuple2<Iterable<A>,Iterable<Iterable<A>>>
> uszs=interleave_.eval(t,ts,(x)->f.eval(cons(y,x)),ys,r);
>           return new
> Tuple2<Iterable<A>,Iterable<Iterable<A>>>(cons(y,uszs.e1),cons(f.eval(cons(t,cons(y,uszs.e1))),uszs.e2));
>         };
>
>         A t = head(tso);
>         Iterable<A>  ts = tail(tso);
>
>         return
> foldr((xs,r)->interleave_.eval(t,ts,(u)->u,xs,r).e2,perms.eval(ts,cons(t,is)),permutations(is));
>       };
>
>       return cons(xs0,perms.eval(xs0,new Nil<A>()));
>     }
>
> Regards,
> Sven Eric
>


More information about the lambda-dev mailing list