Repository navigation
Expand file tree
/
Copy pathComonadLaws.java
More file actions
59 lines (52 loc) · 2.62 KB
/
Copy pathComonadLaws.java
File metadata and controls
59 lines (52 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package testsupport.traits;
import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.comonad.Comonad;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.io.IO;
import com.jnape.palatable.lambda.monoid.builtin.Present;
import com.jnape.palatable.traitor.traits.Trait;
import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Upcast.upcast;
import static java.util.Arrays.asList;
public class ComonadLaws<W extends Comonad<?, W>> implements Trait<Comonad<?, W>> {
@Override
public void test(Comonad<?, W> w) {
Present.<String>present((x, y) -> x + "\n\t - " + y)
.<Fn1<Comonad<?, W>, Maybe<String>>>foldMap(f -> f.apply(w), asList(
this::testLeftIdentity,
this::testRightIdentity,
this::testAssociativity,
this::testDuplicate)
)
.peek(s -> IO.throwing(new AssertionError("The following Comonad laws did not hold for instance of " +
w.getClass() + ": \n\t - " + s)));
}
private Maybe<String> testLeftIdentity(Comonad<?, W> w) {
Fn1<Comonad<?, W>, ?> fn = Comonad::extract;
return w.extend(fn).equals(w)
? nothing()
: just("left identity w.extend(wa -> wa.extract()).equals(w)");
}
private Maybe<String> testRightIdentity(Comonad<?, W> w) {
Fn1<Comonad<?, W>, Object> fn = constantly(new Object());
return w.extend(fn).extract().equals(fn.apply(w))
? nothing()
: just("right identity: w.extend(f).extract().equals(f.apply(w))");
}
private Maybe<String> testAssociativity(Comonad<?, W> w) {
Fn1<Comonad<?, W>, Object> f = constantly(new Object());
Fn1<Comonad<?, W>, Object> g = constantly(new Object());
return w.extend(f).extend(g).equals(w.extend((Comonad<?, W> wa) -> g.apply(wa.extend(f).fmap(upcast()))))
? nothing()
: just("associativity: w.extend(f).extend(g).equals(w.extend(wa -> g(wa.extend(f))))");
}
private Maybe<String> testDuplicate(Comonad<?, W> w) {
Comonad<Comonad<Object, W>, W> wwa = Comonad.duplicate(w).fmap(wa -> wa.fmap(upcast()));
boolean equals = wwa.extract().equals(w);
return equals
? nothing()
: just("duplicate: (Comonad.duplicate(w).extract()).equals(w)");
}
}