Archive for the ‘Java’ Category

Novocode Application Framework 0.4 released

Thursday, May 8th, 2008

I’d like to announce the release of Novocode Application Framework 0.4.

NAF provides a layer of MVC-based GUI components on top of SWT and JFace. A GUI is specified as a tree of NAF components which can then be instantiated as SWT controls. More than just a one to one wrapper of SWT component properties, NAF includes resource management (e.g. shared images and fonts), simplified configuration (e.g. CSS-like size units for layout managers) and model binding. Component trees can be described in a simple XML notation or in JavaScript with an extended JSON format. Applications can also be written entirely in JavaScript. The resource management framework can be used to instantiate and  configure your own classes with a few simple annotations, and it is easily extended for other description or scripting languages.

It’s been a while since the last release which is in part due to design problems with the previous versions, which took me quite some time to resolve. The project still doesn’t have a catchier name :-) but it is now a lot easier to write custom components and the resource management framework can be extended for formats other than XML. The major new feature from an application programmer’s point of view is the JavaScript support.

NAF is licensed under EPL 1.0 and can be downloaded from http://novocode.com/naf/.

Y Combinator in Java With Generics

Sunday, August 12th, 2007

After reading Y Combinator for Dysfunctional Non-Schemers I wondered how far you could go with implementing a Y Combinator in Java with generic types. And I had a strong feeling that it would be rather ugly!

I started with the call-by-value version of the Y Combinator as given by the following expression, straight from the Wikipedia page:

Z = λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))

This one innocent line translates directly to the following mess in untyped Java:

static interface F { Object call(Object arg1); }
 
static F z(final F f)
{
  return (F)
  (
    new F()
    {
      public Object call(final Object x)
      {
        return f.call
        (
          new F()
          {
            public Object call(final Object y)
            {
              return ((F)((F)x).call(x)).call(y);
            }
          }
        );
      }
    }
  )
  .call
  (
    new F()
    {
      public Object call(final Object x)
      {
        return f.call
        (
          new F()
          {
            public Object call(final Object y)
            {
              return ((F)((F)x).call(x)).call(y);
            }
          }
        );
      }
    }
  );
}

It could be simplified further (it’s not necessary to create an F object at the beginning because it is called immediately) but the simplified version turned out to be even harder to annotate with generic types.

The next step was to gradually add type annotations where possible and remove the type casts. I finally arrived at this point where I could not add any more consistent annotations:

static interface F<R,P> { R call(P arg1); }
 
static <R,P> F<R,P> z(final F<F<R,P>,F<R,P>> f)
{
  return
  (
    new F<F<R,P>,F>()
    {
      public F<R,P> call(final F x)
      {
        return f.call
        (
          new F<R,P>()
          {
            public R call(final P y)
            {
              return (R) ((F) x.call(x) ) .call(y);
            }
          }
        );
      }
    }
  )
  .call
  (
    new F<F<R,P>,F<F<R,P>,F<?,?>>>()
    {
      public F<R,P> call(final F<F<R,P>,F<?,?>> x)
      {
        return f.call
        (
          new F<R,P>()
          {
            public R call(final P y)
            {
              return x.call(x).call(y);
            }
          }
        );
      }
    }
  );
}

This version still contains two casts and several type warnings due to the use of raw types. I am not sure if it would be possible to get closer to the ambiguously typed part and I didn’t want to spend any more time trying to. It was already clear that this can only serve as a proof of concept and that you would be ill advised to ever use something like this in production code.

Finally, here’s some code to show how the z function is used and that it really works:

final F<F<Integer,Integer>,F<Integer,Integer>> fac = new F<F<Integer,Integer>,F<Integer,Integer>>()
{
  public F<Integer,Integer> call(final F<Integer,Integer> rec)
  {
    return new F<Integer,Integer>()
    {
      public Integer call(final Integer i)
      {
        if(i <= 1) return 1;
        else return i * rec.call(i-1);
      }
    };
  }
};
 
System.out.println("5! = " + z(fac).call(5));

Close
E-mail It