Skip to content

Commit 5bfd1c8

Browse files
committed
WIP
1 parent 12bd564 commit 5bfd1c8

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.jnape.palatable.lambda.recursionschemes;
2+
3+
import com.jnape.palatable.lambda.functor.Functor;
4+
5+
import java.util.function.Function;
6+
import java.util.function.Supplier;
7+
8+
public final class Thunk<A, F extends Functor> implements Functor<A, Thunk<?, F>>, Supplier<Functor<A, F>> {
9+
private final Supplier<Functor<A, F>> supplier;
10+
11+
public Thunk(Supplier<Functor<A, F>> supplier) {
12+
this.supplier = supplier;
13+
}
14+
15+
@Override
16+
public <B> Thunk<B, F> fmap(Function<? super A, ? extends B> fn) {
17+
return new Thunk<>(() -> supplier.get().fmap(fn));
18+
}
19+
20+
@Override
21+
public Functor<A, F> get() {
22+
return supplier.get();
23+
}
24+
}

src/main/java/com/jnape/palatable/lambda/recursionschemes/builtin/Anamorphism.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import com.jnape.palatable.lambda.functions.Fn2;
55
import com.jnape.palatable.lambda.functor.Functor;
66
import com.jnape.palatable.lambda.recursionschemes.Fix;
7+
import com.jnape.palatable.lambda.recursionschemes.Thunk;
78
import fix.Coalgebra;
89

9-
import static com.jnape.palatable.lambda.recursionschemes.Fix.fix;
10+
import static com.jnape.palatable.lambda.recursionschemes.builtin.Hylomorphism.hylo;
1011

1112
public final class Anamorphism<A, F extends Functor, FA extends Functor<A, F>> implements Fn2<Coalgebra<A, FA>, A, Fix<F, Functor<Fix<F, ?>, F>>> {
1213

@@ -17,7 +18,12 @@ private Anamorphism() {
1718

1819
@Override
1920
public Fix<F, Functor<Fix<F, ?>, F>> apply(Coalgebra<A, FA> coalgebra, A a) {
20-
return fix(coalgebra.apply(a).fmap(x -> ana(coalgebra, x)));
21+
return hylo(f -> () -> new Thunk<>(() -> {
22+
f.fmap(fixed -> coalgebra.apply(fixed.unfix()))
23+
FA apply = coalgebra.apply(a);
24+
return apply;
25+
}).<Fix<F, ?>>fmap(a1 -> ana(coalgebra, a1)).get(), coalgebra, a);
26+
2127
}
2228

2329
@SuppressWarnings("unchecked")

src/main/java/com/jnape/palatable/lambda/recursionschemes/builtin/Hylomorphism.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,36 @@
77
import com.jnape.palatable.lambda.recursionschemes.Algebra;
88
import fix.Coalgebra;
99

10-
import static com.jnape.palatable.lambda.recursionschemes.builtin.Anamorphism.ana;
11-
import static com.jnape.palatable.lambda.recursionschemes.builtin.Catamorphism.cata;
12-
1310
public final class Hylomorphism<A, B, F extends Functor, FA extends Functor<A, F>, FB extends Functor<B, F>> implements Fn3<Algebra<FB, B>, Coalgebra<A, FA>, A, B> {
1411

1512
private static final Hylomorphism INSTANCE = new Hylomorphism();
1613

1714
private Hylomorphism() {
1815
}
1916

17+
@SuppressWarnings("unchecked")
18+
public A fapply(Algebra<FA, A> algebra, Coalgebra<A, FA> coalgebra, A a) {
19+
A x = a;
20+
FA fa = coalgebra.apply(x);
21+
FA previous = null;
22+
while (fa != previous) {
23+
previous = fa;
24+
if (fa.fmap(a1 -> algebra.apply(coalgebra.apply(a1))).equals(fa))
25+
return x;
26+
x = algebra.apply(previous);
27+
fa = coalgebra.apply(algebra.apply(fa));
28+
}
29+
30+
return x;
31+
//
32+
// return algebra.apply((FB) coalgebra.apply(a).fmap(hylo(algebra, coalgebra)));
33+
// return cata(algebra).compose(ana(coalgebra)).apply(a);
34+
}
35+
2036
@Override
2137
@SuppressWarnings("unchecked")
2238
public B apply(Algebra<FB, B> algebra, Coalgebra<A, FA> coalgebra, A a) {
23-
return cata(algebra).compose(ana(coalgebra)).apply(a);
39+
return algebra.apply((FB) coalgebra.apply(a).fmap(hylo(algebra, coalgebra)));
2440
}
2541

2642
@SuppressWarnings("unchecked")

src/test/java/com/jnape/palatable/lambda/recursionschemes/builtin/AnamorphismTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jnape.palatable.lambda.recursionschemes.builtin;
22

3+
import com.jnape.palatable.lambda.recursionschemes.Algebra;
34
import com.jnape.palatable.lambda.recursionschemes.Fix;
45
import fix.Coalgebra;
56
import org.junit.Test;
@@ -23,4 +24,17 @@ public void unfoldingToGreatestFixedPoint() {
2324
assertEquals(Fix.<ListF<Integer, ?>, ListF<Integer, Fix<ListF<Integer, ?>, ?>>>fix(ListF.cons(0, fix(ListF.cons(1, fix(ListF.cons(2, fix(ListF.nil()))))))),
2425
ana(elements, 0));
2526
}
27+
28+
@Test
29+
public void unfoldingInfiniteCodata() {
30+
Coalgebra<Integer, NatF<Integer>> naturals = i -> {
31+
if (i % 10000 == 0)
32+
System.out.println("i = " + i);
33+
return NatF.s(i + 1);
34+
};
35+
Algebra<NatF<Integer>, Integer> sum = nat -> nat.match(z -> 0, s -> s.carrier() + 1);
36+
Integer x = Hylomorphism.<Integer, Integer, NatF, NatF<Integer>, NatF<Integer>>hylo().fapply(sum, naturals, 0);
37+
System.out.println(x);
38+
39+
}
2640
}

0 commit comments

Comments
 (0)