Skip to content

Commit 1a2d0fa

Browse files
committed
adding unification parameter to Functor
1 parent cda40e3 commit 1a2d0fa

File tree

19 files changed

+35
-32
lines changed

19 files changed

+35
-32
lines changed

src/main/java/com/jnape/palatable/lambda/adt/Either.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @param <L> The left parameter type
2626
* @param <R> The right parameter type
2727
*/
28-
public abstract class Either<L, R> implements CoProduct2<L, R>, Functor<R>, Bifunctor<L, R> {
28+
public abstract class Either<L, R> implements CoProduct2<L, R>, Functor<R, Either<L, ?>>, Bifunctor<L, R> {
2929

3030
private Either() {
3131
}

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see Either
1919
* @see Choice3
2020
*/
21-
public abstract class Choice2<A, B> implements CoProduct2<A, B>, Functor<B>, Bifunctor<A, B> {
21+
public abstract class Choice2<A, B> implements CoProduct2<A, B>, Functor<B, Choice2<A, ?>>, Bifunctor<A, B> {
2222

2323
private Choice2() {
2424
}

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see Choice2
1818
* @see Choice4
1919
*/
20-
public abstract class Choice3<A, B, C> implements CoProduct3<A, B, C>, Functor<C>, Bifunctor<B, C> {
20+
public abstract class Choice3<A, B, C> implements CoProduct3<A, B, C>, Functor<C, Choice3<A, B, ?>>, Bifunctor<B, C> {
2121

2222
private Choice3() {
2323
}

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see Choice3
1919
* @see Choice5
2020
*/
21-
public abstract class Choice4<A, B, C, D> implements CoProduct4<A, B, C, D>, Functor<D>, Bifunctor<C, D> {
21+
public abstract class Choice4<A, B, C, D> implements CoProduct4<A, B, C, D>, Functor<D, Choice4<A, B, C, ?>>, Bifunctor<C, D> {
2222

2323
private Choice4() {
2424
}

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @param <E> a type parameter representing the fifth possible type of this choice
1919
* @see Choice4
2020
*/
21-
public abstract class Choice5<A, B, C, D, E> implements CoProduct5<A, B, C, D, E>, Functor<E>, Bifunctor<D, E> {
21+
public abstract class Choice5<A, B, C, D, E> implements CoProduct5<A, B, C, D, E>, Functor<E, Choice5<A, B, C, D, ?>>, Bifunctor<D, E> {
2222

2323
private Choice5() {
2424
}

src/main/java/com/jnape/palatable/lambda/adt/hlist/SingletonHList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @see Tuple4
1717
* @see Tuple5
1818
*/
19-
public class SingletonHList<_1> extends HCons<_1, HNil> implements Functor<_1> {
19+
public class SingletonHList<_1> extends HCons<_1, HNil> implements Functor<_1, SingletonHList> {
2020

2121
SingletonHList(_1 _1) {
2222
super(_1, nil());

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see Tuple4
2020
* @see Tuple5
2121
*/
22-
public class Tuple2<_1, _2> extends HCons<_1, SingletonHList<_2>> implements Map.Entry<_1, _2>, Functor<_2>, Bifunctor<_1, _2> {
22+
public class Tuple2<_1, _2> extends HCons<_1, SingletonHList<_2>> implements Map.Entry<_1, _2>, Functor<_2, Tuple2<_1, ?>>, Bifunctor<_1, _2> {
2323

2424
private final _1 _1;
2525
private final _2 _2;

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see Tuple4
2020
* @see Tuple5
2121
*/
22-
public class Tuple3<_1, _2, _3> extends HCons<_1, Tuple2<_2, _3>> implements Functor<_3>, Bifunctor<_2, _3> {
22+
public class Tuple3<_1, _2, _3> extends HCons<_1, Tuple2<_2, _3>> implements Functor<_3, Tuple3<_1, _2, ?>>, Bifunctor<_2, _3> {
2323
private final _1 _1;
2424
private final _2 _2;
2525
private final _3 _3;

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @see Tuple3
2121
* @see Tuple5
2222
*/
23-
public class Tuple4<_1, _2, _3, _4> extends HCons<_1, Tuple3<_2, _3, _4>> implements Functor<_4>, Bifunctor<_3, _4> {
23+
public class Tuple4<_1, _2, _3, _4> extends HCons<_1, Tuple3<_2, _3, _4>> implements Functor<_4, Tuple4<_1, _2, _3, ?>>, Bifunctor<_3, _4> {
2424
private final _1 _1;
2525
private final _2 _2;
2626
private final _3 _3;

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @see Tuple3
2121
* @see Tuple4
2222
*/
23-
public class Tuple5<_1, _2, _3, _4, _5> extends HCons<_1, Tuple4<_2, _3, _4, _5>> implements Functor<_5>, Bifunctor<_4, _5> {
23+
public class Tuple5<_1, _2, _3, _4, _5> extends HCons<_1, Tuple4<_2, _3, _4, _5>> implements Functor<_5, Tuple5<_1, _2, _3, _4, ?>>, Bifunctor<_4, _5> {
2424
private final _1 _1;
2525
private final _2 _2;
2626
private final _3 _3;

src/main/java/com/jnape/palatable/lambda/functions/Fn1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @param <B> The result type
1414
*/
1515
@FunctionalInterface
16-
public interface Fn1<A, B> extends Functor<B>, Profunctor<A, B>, Function<A, B> {
16+
public interface Fn1<A, B> extends Functor<B, Fn1<A, ?>>, Profunctor<A, B>, Function<A, B> {
1717

1818
/**
1919
* Invoke this function with the given argument.

src/main/java/com/jnape/palatable/lambda/functor/Functor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see com.jnape.palatable.lambda.adt.Either
2020
*/
2121
@FunctionalInterface
22-
public interface Functor<A> {
22+
public interface Functor<A, F extends Functor> {
2323

2424
/**
2525
* Covariantly transmute this functor's parameter using the given mapping function. Generally this method is
@@ -29,5 +29,5 @@ public interface Functor<A> {
2929
* @param fn the mapping function
3030
* @return a functor over B (the new parameter type)
3131
*/
32-
<B> Functor<B> fmap(Function<? super A, ? extends B> fn);
32+
<B> Functor<B, F> fmap(Function<? super A, ? extends B> fn);
3333
}

src/main/java/com/jnape/palatable/lambda/functor/Profunctor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ default <C> Profunctor<A, C> diMapR(Function<B, C> fn) {
5252
* @param <C> the new right parameter type
5353
* @param lFn the left parameter mapping function
5454
* @param rFn the right parameter mapping function
55-
* @return a profunctor over Z (the new left parameter type) and C (the new right parameter tyep)
55+
* @return a profunctor over Z (the new left parameter type) and C (the new right parameter type)
5656
*/
5757
<Z, C> Profunctor<Z, C> diMap(Function<Z, A> lFn, Function<B, C> rFn);
5858
}

src/main/java/com/jnape/palatable/lambda/functor/builtin/Const.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @param <A> the left parameter type, and the type of the stored value
1515
* @param <B> the right (phantom) parameter type
1616
*/
17-
public final class Const<A, B> implements Functor<B>, Bifunctor<A, B> {
17+
public final class Const<A, B> implements Functor<B, Const<A, ?>>, Bifunctor<A, B> {
1818

1919
private final A a;
2020

src/main/java/com/jnape/palatable/lambda/functor/builtin/Identity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @param <A> the value type
1111
*/
12-
public final class Identity<A> implements Functor<A> {
12+
public final class Identity<A> implements Functor<A, Identity> {
1313

1414
private final A a;
1515

src/main/java/com/jnape/palatable/lambda/lens/Lens.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@
132132
* @param <B> the type of the "smaller" update value
133133
*/
134134
@FunctionalInterface
135-
public interface Lens<S, T, A, B> extends Functor<T> {
135+
public interface Lens<S, T, A, B> extends Functor<T, Lens<S, ?, A, B>> {
136136

137-
<FT extends Functor<T>, FB extends Functor<B>> FT apply(Function<? super A, ? extends FB> fn, S s);
137+
<F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>> FT apply(
138+
Function<? super A, ? extends FB> fn, S s);
138139

139140
/**
140141
* Fix this lens against some functor, producing a non-polymorphic runnable lens as an {@link Fn2}.
@@ -146,7 +147,7 @@ public interface Lens<S, T, A, B> extends Functor<T> {
146147
* @param <FB> the type of the lifted B
147148
* @return the lens, "fixed" to the functor
148149
*/
149-
default <FT extends Functor<T>, FB extends Functor<B>> Fixed<S, T, A, B, FT, FB> fix() {
150+
default <F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>> Fixed<S, T, A, B, F, FT, FB> fix() {
150151
return this::apply;
151152
}
152153

@@ -239,8 +240,9 @@ static <S, T, A, B> Lens<S, T, A, B> lens(Function<? super S, ? extends A> gette
239240
return new Lens<S, T, A, B>() {
240241
@Override
241242
@SuppressWarnings("unchecked")
242-
public <FT extends Functor<T>, FB extends Functor<B>> FT apply(Function<? super A, ? extends FB> fn,
243-
S s) {
243+
public <F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>> FT apply(
244+
Function<? super A, ? extends FB> fn,
245+
S s) {
244246
return (FT) fn.apply(getter.apply(s)).fmap(b -> setter.apply(s, b));
245247
}
246248
};
@@ -272,7 +274,7 @@ static <S, A> Lens.Simple<S, A> simpleLens(Function<? super S, ? extends A> gett
272274
interface Simple<S, A> extends Lens<S, S, A, A> {
273275

274276
@Override
275-
default <FS extends Functor<S>, FA extends Functor<A>> Fixed<S, A, FS, FA> fix() {
277+
default <F extends Functor, FS extends Functor<S, F>, FA extends Functor<A, F>> Fixed<S, A, F, FS, FA> fix() {
276278
return this::apply;
277279
}
278280

@@ -294,8 +296,8 @@ default <B> Lens.Simple<S, B> andThen(Lens.Simple<A, B> f) {
294296
* @param <FA> the type of the lifted A
295297
*/
296298
@FunctionalInterface
297-
interface Fixed<S, A, FS extends Functor<S>, FA extends Functor<A>>
298-
extends Lens.Fixed<S, S, A, A, FS, FA> {
299+
interface Fixed<S, A, F extends Functor, FS extends Functor<S, F>, FA extends Functor<A, F>>
300+
extends Lens.Fixed<S, S, A, A, F, FS, FA> {
299301
}
300302
}
301303

@@ -307,11 +309,12 @@ interface Fixed<S, A, FS extends Functor<S>, FA extends Functor<A>>
307309
* @param <T> the type of the "larger" value for putting
308310
* @param <A> the type of the "smaller" value that is read
309311
* @param <B> the type of the "smaller" update value
312+
* @param <F> the functor unification type between FT and FB
310313
* @param <FT> the type of the lifted T
311314
* @param <FB> the type of the lifted B
312315
*/
313316
@FunctionalInterface
314-
interface Fixed<S, T, A, B, FT extends Functor<T>, FB extends Functor<B>>
317+
interface Fixed<S, T, A, B, F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>>
315318
extends Fn2<Function<? super A, ? extends FB>, S, FT> {
316319
}
317320
}

src/main/java/com/jnape/palatable/lambda/lens/functions/Over.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private Over() {
3030

3131
@Override
3232
public T apply(Lens<S, T, A, B> lens, Function<? super A, ? extends B> fn, S s) {
33-
return lens.<Identity<T>, Identity<B>>fix()
33+
return lens.<Identity, Identity<T>, Identity<B>>fix()
3434
.apply(fn.andThen((Function<B, Identity<B>>) Identity::new), s)
3535
.runIdentity();
3636
}

src/main/java/com/jnape/palatable/lambda/lens/functions/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private View() {
2525

2626
@Override
2727
public A apply(Lens<S, T, A, B> lens, S s) {
28-
return lens.<Const<A, T>, Const<A, B>>fix().apply(Const::new, s).runConst();
28+
return lens.<Const<A, ?>, Const<A, T>, Const<A, B>>fix().apply(Const::new, s).runConst();
2929
}
3030

3131
public static <S, T, A, B> View<S, T, A, B> view() {

src/test/java/com/jnape/palatable/lambda/lens/LensTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class LensTest {
2727

2828
@Test
2929
public void setsUnderIdentity() {
30-
Set<Integer> ints = LENS.<Identity<Set<Integer>>, Identity<Integer>>apply(s -> new Identity<>(s.length()), asList("foo", "bar", "baz")).runIdentity();
30+
Set<Integer> ints = LENS.<Identity, Identity<Set<Integer>>, Identity<Integer>>apply(s -> new Identity<>(s.length()), asList("foo", "bar", "baz")).runIdentity();
3131
assertEquals(singleton(3), ints);
3232
}
3333

3434
@Test
3535
public void viewsUnderConst() {
36-
Integer i = LENS.<Const<Integer, Set<Integer>>, Const<Integer, Integer>>apply(s -> new Const<>(s.length()), asList("foo", "bar", "baz")).runConst();
36+
Integer i = LENS.<Const<Integer, ?>, Const<Integer, Set<Integer>>, Const<Integer, Integer>>apply(s -> new Const<>(s.length()), asList("foo", "bar", "baz")).runConst();
3737
assertEquals((Integer) 3, i);
3838
}
3939

@@ -42,8 +42,8 @@ public void fix() {
4242
Fn1<String, Const<Integer, Integer>> fn = s -> new Const<>(s.length());
4343
List<String> s = singletonList("foo");
4444

45-
Integer fixedLensResult = LENS.<Const<Integer, Set<Integer>>, Const<Integer, Integer>>fix().apply(fn, s).runConst();
46-
Integer unfixedLensResult = LENS.<Const<Integer, Set<Integer>>, Const<Integer, Integer>>apply(fn, s).runConst();
45+
Integer fixedLensResult = LENS.<Const<Integer, ?>, Const<Integer, Set<Integer>>, Const<Integer, Integer>>fix().apply(fn, s).runConst();
46+
Integer unfixedLensResult = LENS.<Const<Integer, ?>, Const<Integer, Set<Integer>>, Const<Integer, Integer>>apply(fn, s).runConst();
4747

4848
assertEquals(unfixedLensResult, fixedLensResult);
4949
}
@@ -62,7 +62,7 @@ public void mapsIndividuallyOverParameters() {
6262
.mapA(Optional::ofNullable)
6363
.mapB((Optional<Integer> optI) -> optI.orElse(-1));
6464

65-
Lens.Fixed<Optional<String>, Optional<Boolean>, Optional<Character>, Optional<Integer>, Identity<Optional<Boolean>>, Identity<Optional<Integer>>> fixed = theGambit.fix();
65+
Lens.Fixed<Optional<String>, Optional<Boolean>, Optional<Character>, Optional<Integer>, Identity, Identity<Optional<Boolean>>, Identity<Optional<Integer>>> fixed = theGambit.fix();
6666
assertEquals(Optional.of(true), fixed.apply(optC -> new Identity<>(optC.map(c -> parseInt(Character.toString(c)))), Optional.of("321")).runIdentity());
6767
}
6868

0 commit comments

Comments
 (0)