Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.highj.data.instance.maybe;

import org.derive4j.hkt.__;
import org.highj.Hkt;
import org.highj.data.Either;
import org.highj.data.Maybe;
import org.highj.data.tuple.T0;
import org.highj.typeclass2.injective.Isomorphic1;

public interface MaybeEitherIsomorphic1 extends Isomorphic1<Maybe.µ, __<Either.µ, T0>> {
@Override
default <A> Maybe<A> from(__<__<Either.µ, T0>, A> input) {
return Hkt.asEither(input).either(u -> Maybe.Nothing(), Maybe::Just);
}

@Override
default <A> Either<T0, A> to(__<Maybe.µ, A> input) {
return Hkt.asMaybe(input).cata(Either.Left(T0.unit), Either::<T0, A>Right);
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/highj/typeclass2/injective/Injective.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.highj.typeclass2.injective;

import java.util.function.Function;

/**
* An injective relationship.
*
* Law:
* if x != y then injective.to(x) != injective.to(y)
*
* @param <A> source type
* @param <B> target type
*/
public interface Injective<A, B> extends Function<A, B> {

B to(A input);

default B apply(A input) {
return to(input);
}

static <A,B> Injective<A,B> of(Function<A,B> f) {
return f::apply;
}

static <A> Injective<A,A> identity() {
return a -> a;
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/highj/typeclass2/injective/Injective1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.highj.typeclass2.injective;

import org.derive4j.hkt.__;

/**
* An injective relationship of type constructors.
*
* Law:
* if x != y then injective1.to(x) != injective1.to(y)
*
* @param <F> the source type constructor
* @param <G> the target type constructor
*/
public interface Injective1<F, G> {

<A> __<G, A> to(__<F, A> input);

default <A> Injective<__<F,A>, __<G,A>> to() {
return this::to;
}

default <H> Injective1<F, H> andThen(Injective1<G, H> that) {
return new Injective1<F, H>() {
@Override
public <A> __<H, A> to(__<F, A> input) {
return that.to(Injective1.this.to(input));
}
};
}

static <M> Injective1<M, M> identity() {
return new Injective1<M, M>() {
@Override
public <A> __<M, A> to(__<M, A> input) {
return input;
}
};
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/highj/typeclass2/injective/Isomorphic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.highj.typeclass2.injective;

import java.util.function.Function;

/**
* The class of isomorphic types, i.e. those which can be cast to each other without loss of information.
* @param <A> source type
* @param <B> target type
*/
public interface Isomorphic<A, B> extends Injective<A, B> {

A from(B input);

static <A, B> Isomorphic<A, B> of(Function<A, B> fnTo, Function<B, A> fnFrom) {
return new Isomorphic<A, B>() {
@Override
public A from(B input) {
return fnFrom.apply(input);
}

@Override
public B to(A input) {
return fnTo.apply(input);
}
};
}

static <A> Isomorphic<A, A> identity() {
return Isomorphic.of(a -> a, a -> a);
}

default Isomorphic<B, A> inverse() {
return Isomorphic.of(Isomorphic.this::from, Isomorphic.this::to);
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/highj/typeclass2/injective/Isomorphic1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.highj.typeclass2.injective;

import org.derive4j.hkt.__;

/**
* The class of isomorphic type constructors, i.e. those which can be cast to each other without loss of information.
* @param <F>
* @param <G>
*/
public interface Isomorphic1<F,G> extends Injective1<F,G> {

<A> __<F, A> from(__<G, A> input);

default <A> Injective<__<G,A>, __<F,A>> from() {
return this::from;
}

default <A> Isomorphic<__<F,A>, __<G,A>> isomorphic() {
return Isomorphic.of(f -> Isomorphic1.this.to(f), g -> Isomorphic1.this.from(g));
}

default Isomorphic1<G,F> inverse() {
return new Isomorphic1<G, F>() {
@Override
public <A> __<G, A> from(__<F, A> input) {
return Isomorphic1.this.to(input);
}

@Override
public <A> __<F, A> to(__<G, A> input) {
return Isomorphic1.this.from(input);
}
};
}

static <F> Isomorphic1<F,F> identity() {
return new Isomorphic1<F, F>() {
@Override
public <A> __<F, A> from(__<F, A> input) {
return input;
}

@Override
public <A> __<F, A> to(__<F, A> input) {
return input;
}
};
}

}
37 changes: 37 additions & 0 deletions src/test/java/org/highj/typeclass2/injective/InjectiveLaw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.highj.typeclass2.injective;

import org.highj.data.eq.Eq;
import org.highj.util.Gen;
import org.highj.util.Law;

import static org.assertj.core.api.Assertions.assertThat;

public class InjectiveLaw<A,B> implements Law {

private final Injective<A,B> injective;
protected final Gen<A> genA;
protected final Eq<A> eqA;
protected final Eq<B> eqB;

public InjectiveLaw(Injective<A, B> injective, Gen<A> genA, Eq<A> eqA, Eq<B> eqB) {
this.injective = injective;
this.genA = genA;
this.eqA = eqA;
this.eqB = eqB;
}

public void injectivity(){
for(A a1 : genA.get(10)) {
B b1 = injective.to(a1);
for(A a2 : genA.get(10)) {
B b2 = injective.to(a2);
assertThat(eqA.eq(a1, a2) == eqB.eq(b1, b2)).isTrue();
}
}
}

@Override
public void test() {
injectivity();
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/highj/typeclass2/injective/IsomorphicLaw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.highj.typeclass2.injective;

import org.highj.data.eq.Eq;
import org.highj.util.Gen;

import static org.assertj.core.api.Assertions.assertThat;

public class IsomorphicLaw<A,B> extends InjectiveLaw<A,B> {

private final Isomorphic<A,B> isomorphic;
protected final Gen<B> genB;

public IsomorphicLaw(Isomorphic<A,B> isomorphic, Gen<A> genA, Gen<B> genB, Eq<A> eqA, Eq<B> eqB) {
super(isomorphic, genA, eqA, eqB);
this.isomorphic = isomorphic;
this.genB = genB;
}

public void isomorphism() {
for(A a : genA.get(20)) {
assertThat(eqA.eq(a, isomorphic.from(isomorphic.to(a)))).isTrue();
}
for(B b : genB.get(20)) {
assertThat(eqB.eq(b, isomorphic.to(isomorphic.from(b)))).isTrue();
}
}

public void inverseInjectivity() {
new InjectiveLaw<>(isomorphic.inverse(), genB, eqB, eqA).test();
}

@Override
public void test() {
isomorphism();
inverseInjectivity();
super.test();
}

}