Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active November 26, 2021 05:45
Show Gist options
  • Save Superstar64/055cae5562e543000fed35c4e9db82fd to your computer and use it in GitHub Desktop.
Save Superstar64/055cae5562e543000fed35c4e9db82fd to your computer and use it in GitHub Desktop.
Typeclasses in Java
/*
Copyright (C) Freddy A Cubas "superstar64" 2019
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
import java.util.function.Function;
import java.util.ArrayList;
import java.util.Arrays;
interface TypeNum<A> {
A add(A left, A right);
A sub(A left, A right);
A mul(A left, A right);
A div(A left, A right);
A fromInt(int i);
}
class TypeInteger implements TypeNum<Integer> {
public Integer add(Integer left, Integer right) {
return left + right;
}
public Integer sub(Integer left, Integer right) {
return left - right;
}
public Integer mul(Integer left, Integer right) {
return left * right;
}
public Integer div(Integer left, Integer right) {
return left / right;
}
public Integer fromInt(int i) {
return new Integer(i);
}
}
// java doesn't have native support for higher kinded polymorphism so use a workaround
// based on "Lightweight higher-kinded polymorphism"
// http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
interface App<F,A> {
}
// App<ListBrand,A> = ArrayList<A>
interface ListBrand {
static <A> App<ListBrand,A> inject(ArrayList<A> list) {
return new ListInstance<A>(list);
}
static <A> ArrayList<A> project(App<ListBrand,A> list) {
return ((ListInstance<A>)list).get;
}
static class ListInstance<A> implements App<ListBrand,A> {
ArrayList<A> get;
ListInstance(ArrayList<A> get) {
this.get = get;
}
};
}
interface Functor<F> {
<A,B> App<F,B> map(Function<A,B> function, App<F,A> functor);
}
class ListFunctor implements Functor<ListBrand> {
public <A,B> App<ListBrand,B> map(Function<A,B> function, App<ListBrand,A> functor) {
ArrayList<A> list = ListBrand.project(functor);
ArrayList<B> result = new ArrayList<>();
for(A elm: list) {
result.add(function.apply(elm));
}
return ListBrand.inject(result);
}
}
public class TypeClass {
static <A> A square(TypeNum<A> type, A x) {
return type.mul(x, x);
}
static <F,A> App<F,A> squareAll(TypeNum<A> typeNum, Functor<F> typeFunctor,App<F,A> functor) {
return typeFunctor.map(a -> square(typeNum,a),functor);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
ArrayList<Integer> squares = ListBrand.project( squareAll(new TypeInteger(), new ListFunctor(), ListBrand.inject(list)) );
System.out.println(squares);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment