Last active
January 22, 2020 20:28
-
-
Save Superstar64/23baf9bdf3dd9425010d2c339a38ebba to your computer and use it in GitHub Desktop.
Catamorphisms in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
public class Cata{ | |
static interface Ast<A> { | |
<B> Ast<B> fmap(Function<A,B> function); | |
<B> B match(Function<AstInt<A>,B> integer, Function<AstPlus<A>,B> plus, Function<AstMul<A>,B> mul); | |
} | |
static class AstInt<A> implements Ast<A> { | |
int x; | |
AstInt(int x){ | |
this.x = x; | |
} | |
public <B> Ast<B> fmap(Function<A,B> function){ | |
return new AstInt<B>(x); | |
} | |
public <B> B match(Function<AstInt<A>,B> integer, Function<AstPlus<A>,B> plus, Function<AstMul<A>,B> mul){ | |
return integer.apply(this); | |
} | |
} | |
static AstFix astInt(int x){ | |
return new AstFix(new AstInt<AstFix>(x)); | |
} | |
static class AstPlus<A> implements Ast<A> { | |
A left; | |
A right; | |
AstPlus(A left,A right){ | |
this.left = left; | |
this.right = right; | |
} | |
public <B> Ast<B> fmap(Function<A,B> function){ | |
return new AstPlus<B>(function.apply(left),function.apply(right)); | |
} | |
public <B> B match(Function<AstInt<A>,B> integer, Function<AstPlus<A>,B> plus, Function<AstMul<A>,B> mul){ | |
return plus.apply(this); | |
} | |
} | |
static AstFix astPlus(AstFix left,AstFix right){ | |
return new AstFix(new AstPlus<AstFix>(left,right)); | |
} | |
static class AstMul<A> implements Ast<A> { | |
A left; | |
A right; | |
AstMul(A left,A right){ | |
this.left = left; | |
this.right = right; | |
} | |
public <B> Ast<B> fmap(Function<A,B> function){ | |
return new AstMul<B>(function.apply(left),function.apply(right)); | |
} | |
public <B> B match(Function<AstInt<A>,B> integer, Function<AstPlus<A>,B> plus, Function<AstMul<A>,B> mul){ | |
return mul.apply(this); | |
} | |
} | |
static AstFix astMul(AstFix left,AstFix right){ | |
return new AstFix(new AstMul<AstFix>(left,right)); | |
} | |
/* should be | |
static class Fix<F> { | |
F<Fix<F>> get; | |
} | |
but that's not valid java | |
*/ | |
static class AstFix { | |
Ast<AstFix> get; | |
AstFix(Ast<AstFix> get){ | |
this.get = get; | |
} | |
} | |
static <A> A cata(Function<Ast<A>,A> map, AstFix ast){ | |
return map.apply( ast.get.fmap(a -> cata(map,a)) ); | |
} | |
static Integer eval(AstFix ast){ | |
return cata(a -> a.match(b -> b.x, b -> b.left + b.right, b -> b.left * b.right) , ast); | |
} | |
public static void main(String[] args){ | |
// 1 + (2 * 3) | |
AstFix ast = astPlus( | |
astInt(1), | |
astMul( | |
astInt(2), | |
astInt(3) | |
) | |
); | |
System.out.println(eval(ast)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment