mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
experimenting with kinds and higher order types, not done yet
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface Applicative<T, A extends K<A, T>> extends Functor<T> {
|
||||
<U, A1 extends K<A1, U>> Applicative<U, A1> pure(U t);
|
||||
|
||||
<U, A1 extends K<A1, U>> Applicative<U, A1> apply(K<A, Function<T, U>> amap);
|
||||
}
|
||||
@@ -3,6 +3,6 @@ package com.dfsek.terra.api.util.generic;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface Functor<T, F extends Functor<?, F>> {
|
||||
<U> Functor<U, F> map(Function<T, U> map);
|
||||
public interface Functor<T> {
|
||||
<U> Functor<U> map(Function<T, U> map);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
/**
|
||||
* Kind
|
||||
*/
|
||||
public interface K<T, U> {
|
||||
@SuppressWarnings("unchecked")
|
||||
default T self() {
|
||||
return (T) this;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,27 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public interface Maybe<T> {
|
||||
public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
@Override
|
||||
<T2> Maybe<T2> bind(Function<T, Monad<T2, Maybe<?>>> map);
|
||||
|
||||
@Override
|
||||
<T2> Maybe<T2> identity();
|
||||
|
||||
@Override
|
||||
<U> Maybe<U> map(Function<T, U> map);
|
||||
|
||||
@Override
|
||||
Monad<T, Maybe<?>> multiply(Maybe<?> t);
|
||||
|
||||
default Optional<T> toOptional() {
|
||||
|
||||
}
|
||||
|
||||
record Just<T>(T value) implements Maybe<T> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,24 @@ import java.util.function.Function;
|
||||
/**
|
||||
* A monad is a monoid in the category of endofunctors.
|
||||
*/
|
||||
public interface Monad<T, M extends Monad<?, M>> extends Functor<T, M>, Monoid<T, M> {
|
||||
<T2> Monad<T2, M> bind(Function<T, Monad<T2, M>> map);
|
||||
<T2> Monad<T2, M> pure(T2 t);
|
||||
public interface Monad<T, M extends K<M, T>> extends Applicative<T, M>, Monoid<T, M>{
|
||||
<T2, M2 extends K<M2, T2>> Monad<T2, M2> bind(Function<T, Monad<T2, M2>> map);
|
||||
@Override
|
||||
<T2, M2 extends K<M2, T2>> Monad<T2, M2> identity();
|
||||
|
||||
@Override
|
||||
default <U> Monad<U, M> map(Function<T, U> map) {
|
||||
<U, M2 extends K<M2, U>> Monad<U, M2> pure(U t);
|
||||
|
||||
@Override
|
||||
Monad<T, M> multiply(M t);
|
||||
|
||||
@Override
|
||||
default <U, M2 extends K<M2, U>> Monad<U, M2> map(Function<T, U> map) {
|
||||
return bind(m -> pure(map.apply(m)));
|
||||
}
|
||||
|
||||
@Override
|
||||
default <U, A1 extends K<A1, U>> Monad<U, A1> apply(K<M, Function<T, U>> amap) {
|
||||
amap.self()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
public interface Monoid<T, M extends Monoid<?, M>> extends Semigroup<T, M>{
|
||||
<T1> Monoid<T1, M> pure(T1 t);
|
||||
public interface Monoid<T, M extends K<M, T>> extends Semigroup<T, M>{
|
||||
<T1, M1 extends K<M1, T1>> Monoid<T1, M1> identity();
|
||||
|
||||
@Override
|
||||
Monoid<T, M> multiply(M t);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.dfsek.terra.api.util.generic;
|
||||
|
||||
public interface Semigroup<T, S extends Semigroup<?, S>> {
|
||||
public interface Semigroup<T, S extends K<S, T>> {
|
||||
Semigroup<T, S> multiply(S t);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user