Compiler bug? (was RE: A bit of fun with the Y combinator)

Samuel Dennis Borlongan srborlongan at gmail.com
Thu Sep 12 02:37:17 PDT 2013


Minor change to accomodate larger return values.
Please note that I have not been able to make the BiFunction work as a
replacement for the corresponding static function.
It only exists as a "proof" of concept.

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.LongFunction;

public interface Zhong {
  public static <A,B> void main(String args[]) {
    BiFunction<Class<A>, Class<B>, Function<Function<Function<A,B>,
Function<A,B>>, Function<A,B>>> fix = (a, b) -> f ->
      new Object() {
        Function<A,B> r = a -> f.apply(r).apply(a);
      }
      .r
    ;

    LongFunction factorialZhong = n -> fix(Long.class, Long.class).apply(
        // Recursive function generator
        (Function<Long,Long> f) -> (Long x) -> (x == 0) ? 1 : x * f.apply(x
- 1)
      )
      .apply(
        // Argument
        n
      )
    ;

    long n = Long.parseLong(args[0]);
    System.out.println(n + "! = " + factorialZhong.apply(n));
  }

  public static <A, B> Function<Function<Function<A,B>, Function<A,B>>,
Function<A,B>> fix(Class<A> a, Class<B> b) {
    return f ->
      new Object() {
        Function<A,B> r = a -> f.apply(r).apply(a);
      }
      .r
    ;
  }
}

Samuel Dennis R. Borlongan


On Thu, Sep 12, 2013 at 2:22 AM, Zhong Yu <zhong.j.yu at gmail.com> wrote:

> Probably a bug. However I cannot reproduce the problem; your code
> compiles fine on my jdk8 b106 windows 64 bit.
>
>
>
> On Wed, Sep 11, 2013 at 12:41 PM, Millies, Sebastian
> <Sebastian.Millies at softwareag.com> wrote:
> > Hello Zhong,
> >
> > I was about to test your cheat, but the compiler throws an exception
> (b106).
> >
> > Here's the code:
> >
> > import java.util.function.Function;
> >
> > public class Zhong {
> >
> >     public static void main(String args[]) {
> >         int n = Integer.parseInt(args[0]);
> >         System.out.println(n + "! = " + factorialZhong(n));
> >     }
> >
> >     // Zhong Yu's variation, which is *really* cheating :-)
> >     private static <A,B> Function<A,B> fix2(Function<Function<A,B>,
> Function<A,B>> f)
> >     {
> >         return new Object()
> >         {
> >             Function<A,B> r = a -> f.apply(r).apply(a);
> >         }.r;
> >     }
> >
> >     private static int factorialZhong(int n) {
> >         return  fix2(
> >                         // Recursive function generator
> >                         (Function<Integer,Integer> f) -> (Integer x) ->
> (x == 0) ? 1 : x * f.apply(x - 1)
> >                 )
> >                 .apply(
> >                         // Argument
> >                         n);
> >     }
> > }
> >
> > Here's the error message:
> >
> > Using javac 1.8.0-ea to compile java sources
> > java: An exception has occurred in the compiler (1.8.0-ea). Please file
> a bug at the Java Developer Connection (
> http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for
> duplicates. Include your program and the following diagnostic in your
> report.  Thank you.
> > java: java.lang.ClassCastException:
> com.sun.tools.javac.code.Symbol$ClassSymbol cannot be cast to
> com.sun.tools.javac.code.Symbol$MethodSymbol
> >
> > Can anyone reproduce that?
> >
> > -- Sebastian
> >
> > -----Original Message-----
> > From: Zhong Yu [mailto:zhong.j.yu at gmail.com]
> > Sent: Wednesday, September 11, 2013 6:36 PM
> > To: Millies, Sebastian
> > Cc: lambda-dev at openjdk.java.net
> > Subject: Re: A bit of fun with the Y combinator
> >
> > My variation, which is *really* cheating :D
> >
> >     public static <A,B> Function<A,B> fix2(Function<Function<A,B>,
> Function<A,B>> f)
> >     {
> >         return new Object()
> >         {
> >             Function<A,B> r = a -> f.apply(r).apply(a);
> >         }.r;
> >     }
> >
> > Zhong Yu
> >
> > Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt,
> Germany – Registergericht/Commercial register: Darmstadt HRB 1562 -
> Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman),
> Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of
> the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
> >
>
>


More information about the lambda-dev mailing list