
Re: The Importance of Recursive Types

Posted: Oct 15, 2006 5:03 AM


You can write your example in Java, but note types of eval, f, and g are not exactly the same as you propose.
package cat;
import static java.lang.System.*;
public class Main {
static < A > T2< A, A > dup( final A a ) { return t( a, a ); }
static < R, A > R eval( final A a, final F< ? extends R, ? super A > f ) { return f.call( a ); }
static < R, A > T2< R, R > g( final A a, final F< R, ? super A > f ) { return dup( eval( a, f ) ); }
static < A extends F< A, A > > A f( final A f ) {
final T2< A, A > d = dup( f );
return eval( d.e1, d.e2 );
}
public static void main( final String[] notUsed ) {
out.println( g( 1, inc ) );
out.println( f( id ) );
}
final static F< Integer, Integer > inc = new F< Integer, Integer >() {
public Integer call( final Integer a ) { return a + 1; }
};
final static Id id = new Id() {
public Id call( final Id a ) { return a; }
public String toString() { return "id"; }
};
interface Id extends F< Id, Id > {};
interface F< R, A > { R call( A a ); }
static class T1< E1 > {
E1 e1;
T1( final E1 e1 ) { this.e1 = e1; }
public String toString() { return "t(" + e1 + ")"; }
}
static < E1 > T1< E1 > t( final E1 e1 ) { return new T1< E1 >( e1 ); }
static class T2< E1, E2 > extends T1< E1 > {
E2 e2;
T2( final E1 e1, final E2 e2 ) {
super( e1 );
this.e2 = e2;
}
public String toString() { return "t(" + e1 + ", " + e2 + ")"; }
}
static < E1, E2 > T2< E1, E2 > t( final E1 e1, final E2 e2 ) { return new T2< E1, E2 >( e1, e2 ); }
}

