Skip to content
Open

Java #29

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions languages/j/java/LambdaCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ interface UnaryBoolOp extends Function<Bool, Bool> {
interface BinaryBoolOp extends Function<Bool, Function<Bool, Bool>> {
}

interface ChurchNumeral extends Function<Function<Integer, Integer>, Function<Integer, Integer>> {
// PRED applies a numeral to functions over functions, so numerals cannot be
// pinned to Function<Integer, Integer>. Term is the untyped lambda calculus'
// single universal type: everything is a function from Term to Term.
interface Term extends Function<Term, Term> {
}

Bool TRUE = x -> y -> x;
Expand All @@ -21,13 +24,13 @@ interface ChurchNumeral extends Function<Function<Integer, Integer>, Function<In
BinaryBoolOp AND = b1 -> b2 -> b1.apply(b2).apply(FALSE);
BinaryBoolOp OR = b1 -> b2 -> b1.apply(TRUE).apply(b2);

ChurchNumeral ZERO = x -> y -> y;
Function<ChurchNumeral, ChurchNumeral> SUCC = w -> y -> x -> y.apply(w.apply(y).apply(x));

// Function<ChurchNumeral, ChurchNumeral> PRED = n -> f -> x ->
// n.apply(g -> h -> h.apply(g.apply(f)))
// .apply(u -> x)
// .apply(u -> u);
Term ZERO = f -> x -> x;
Term SUCC = n -> f -> x -> f.apply(n.apply(f).apply(x));
Term PRED = n -> f -> x ->
n.apply(g -> h -> h.apply(g.apply(f)))
.apply(u -> x)
.apply(u -> u);
Term ONE = SUCC.apply(ZERO);

static void main(String[] args) {
printBool(TRUE); // TRUE
Expand All @@ -47,7 +50,11 @@ static void main(String[] args) {
printBool(OR.apply(TRUE).apply(TRUE)); // TRUE

printChurchNumeral(ZERO); // 0
printChurchNumeral(SUCC.apply(ZERO)); // 1
printChurchNumeral(ONE); // 1
printChurchNumeral(SUCC.apply(ONE)); // 2
printChurchNumeral(PRED.apply(SUCC.apply(ONE))); // 1
printChurchNumeral(PRED.apply(ONE)); // 0
printChurchNumeral(PRED.apply(ZERO)); // 0
}

static void printBool(Bool b) {
Expand All @@ -59,7 +66,13 @@ else if (b == FALSE)
throw new IllegalStateException();
}

static void printChurchNumeral(ChurchNumeral n) {
System.out.println(n.apply(x -> x + 1).apply(0));
static void printChurchNumeral(Term n) {
int[] count = {0};
Term inc = t -> {
count[0]++;
return t;
};
n.apply(inc).apply(t -> t);
System.out.println(count[0]);
}
}