Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created December 23, 2020 17:48
Show Gist options
  • Save natefaubion/398fcd7fa4c9415e8950235b7d15f113 to your computer and use it in GitHub Desktop.
Save natefaubion/398fcd7fa4c9415e8950235b7d15f113 to your computer and use it in GitHub Desktop.
CoreFn Java Codecs
package org.purescript.corefn;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
public class CoreFn {
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({ "key", "value" })
public static class KeyValuePair<A> {
public final String key;
public final A value;
public KeyValuePair(@JsonProperty("key") String key, @JsonProperty("value") A value) {
this.key = key;
this.value = value;
}
}
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "literalType")
@JsonSubTypes({ @JsonSubTypes.Type(value = IntLiteral.class, name = "IntLiteral"),
@JsonSubTypes.Type(value = NumberLiteral.class, name = "NumberLiteral"),
@JsonSubTypes.Type(value = StringLiteral.class, name = "StringLiteral"),
@JsonSubTypes.Type(value = CharLiteral.class, name = "CharLiteral"),
@JsonSubTypes.Type(value = BooleanLiteral.class, name = "BooleanLiteral"),
@JsonSubTypes.Type(value = ArrayLiteral.class, name = "ArrayLiteral"),
@JsonSubTypes.Type(value = ObjectLiteral.class, name = "ObjectLiteral") })
public static abstract class LiteralValue<A> {
public abstract <Z> Z fold(Fold<A, Z> cases);
public interface Fold<A, Z> {
public Z Int(int value);
public Z Number(double value);
public Z String(String value);
public Z Char(char value);
public Z Boolean(boolean value);
public Z Array(A[] value);
public Z Object(KeyValuePair<A>[] value);
}
public static class DefaultFold<A, Z> implements Fold<A, Z> {
private Z value;
public DefaultFold(Z value) {
this.value = value;
}
public Z Int(int value) {
return this.value;
}
public Z Number(double value) {
return this.value;
}
public Z String(String value) {
return this.value;
}
public Z Char(char value) {
return this.value;
}
public Z Boolean(boolean value) {
return this.value;
}
public Z Array(A[] value) {
return this.value;
}
public Z Object(KeyValuePair<A>[] value) {
return this.value;
}
}
}
public static final class IntLiteral<A> extends LiteralValue<A> {
public final int value;
public IntLiteral(@JsonProperty("value") int value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Int(value);
}
}
public static final class NumberLiteral<A> extends LiteralValue<A> {
public final double value;
public NumberLiteral(@JsonProperty("value") double value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Number(value);
}
}
public static final class StringLiteral<A> extends LiteralValue<A> {
public final String value;
public StringLiteral(@JsonProperty("value") String value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.String(value);
}
}
public static final class CharLiteral<A> extends LiteralValue<A> {
public final char value;
public CharLiteral(@JsonProperty("value") char value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Char(value);
}
}
public static final class BooleanLiteral<A> extends LiteralValue<A> {
public final boolean value;
public BooleanLiteral(@JsonProperty("value") boolean value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Boolean(value);
}
}
public static final class ArrayLiteral<A> extends LiteralValue<A> {
public final A[] value;
public ArrayLiteral(@JsonProperty("value") A[] value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Array(value);
}
}
public static final class ObjectLiteral<A> extends LiteralValue<A> {
public final KeyValuePair<A>[] value;
public ObjectLiteral(@JsonProperty("value") KeyValuePair<A>[] value) {
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Object(value);
}
}
public static class Ann {
public final SourceSpan sourceSpan;
public final Meta meta;
public Ann(SourceSpan sourceSpan, Meta meta) {
this.sourceSpan = sourceSpan;
this.meta = meta;
}
}
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "metaType")
@JsonSubTypes({ @JsonSubTypes.Type(value = IsConstructor.class, name = "IsConstructor"),
@JsonSubTypes.Type(value = IsNewtype.class, name = "IsNewtype"),
@JsonSubTypes.Type(value = IsTypeClassConstructor.class, name = "IsTypeClassConstructor"),
@JsonSubTypes.Type(value = IsForeign.class, name = "IsForeign"),
@JsonSubTypes.Type(value = IsWhere.class, name = "IsWhere") })
public static abstract class Meta {
public abstract <Z> Z fold(Fold<Z> cases);
public interface Fold<Z> {
public Z Constructor(ConstructorType constructorType, String[] identifiers);
public Z Newtype();
public Z TypeClassConstructor();
public Z Foreign();
public Z Where();
}
public static class DefaultFold<Z> implements Fold<Z> {
private Z value;
public DefaultFold(Z value) {
this.value = value;
}
public Z Constructor(ConstructorType constructorType, String[] identifiers) {
return this.value;
}
public Z Newtype() {
return this.value;
}
public Z TypeClassConstructor() {
return this.value;
}
public Z Foreign() {
return this.value;
}
public Z Where() {
return this.value;
}
}
}
public static final class IsConstructor extends Meta {
public final ConstructorType constructorType;
public final String[] identifiers;
public IsConstructor(ConstructorType constructorType, String[] identifiers) {
this.constructorType = constructorType;
this.identifiers = identifiers;
}
public final <Z> Z fold(Fold<Z> cases) {
return cases.Constructor(constructorType, identifiers);
}
}
public static final class IsNewtype extends Meta {
public final <Z> Z fold(Fold<Z> cases) {
return cases.Newtype();
}
}
public static final class IsTypeClassConstructor extends Meta {
public final <Z> Z fold(Fold<Z> cases) {
return cases.TypeClassConstructor();
}
}
public static final class IsForeign extends Meta {
public final <Z> Z fold(Fold<Z> cases) {
return cases.Foreign();
}
}
public static final class IsWhere extends Meta {
public final <Z> Z fold(Fold<Z> cases) {
return cases.Where();
}
}
public static enum ConstructorType {
ProductType, SumType;
}
public static class SourceSpan {
public final int start;
public final int end;
public SourceSpan(@JsonProperty("start") int start, @JsonProperty("end") int end) {
this.start = start;
this.end = end;
}
}
public static class QualifiedIdent {
public final String[] moduleName;
public final String identifier;
public QualifiedIdent(@JsonProperty("moduleName") String[] moduleName,
@JsonProperty("identifier") String identifier) {
this.moduleName = moduleName;
this.identifier = identifier;
}
}
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Var.class, name = "Var"),
@JsonSubTypes.Type(value = Literal.class, name = "Literal"),
@JsonSubTypes.Type(value = Constructor.class, name = "Constructor"),
@JsonSubTypes.Type(value = Accessor.class, name = "Accessor"),
@JsonSubTypes.Type(value = ObjectUpdate.class, name = "ObjectUpdate"),
@JsonSubTypes.Type(value = Abs.class, name = "Abs"), @JsonSubTypes.Type(value = App.class, name = "App"),
@JsonSubTypes.Type(value = Case.class, name = "Case"),
@JsonSubTypes.Type(value = Let.class, name = "Let") })
public static abstract class Expr<A> {
public abstract <Z> Z fold(Fold<A, Z> cases);
public interface Fold<A, Z> {
public Z Var(A ann, QualifiedIdent var);
public Z Literal(A ann, LiteralValue<Expr<A>> value);
public Z Constructor(A ann, String typeName, String constructorName, String[] fieldNames);
public Z Accessor(A ann, String fieldName, Expr<A> expression);
public Z ObjectUpdate(A ann, Expr<A> expression, KeyValuePair<Expr<A>>[] updates);
public Z Abs(A ann, String argument, Expr<A> body);
public Z App(A ann, Expr<A> abstraction, Expr<A> argument);
public Z Case(A ann, Expr<A>[] caseExpressions, CaseAlternative<A>[] caseAlternatives);
public Z Let(A ann, Bind<A>[] binds, Expr<A> expression);
}
public static class DefaultFold<A, Z> implements Fold<A, Z> {
private final Z value;
public DefaultFold(Z value) {
this.value = value;
}
public Z Var(A ann, QualifiedIdent var) {
return this.value;
}
public Z Literal(A ann, LiteralValue<Expr<A>> value) {
return this.value;
}
public Z Constructor(A ann, String typeName, String constructorName, String[] fieldNames) {
return this.value;
}
public Z Accessor(A ann, String fieldName, Expr<A> expression) {
return this.value;
}
public Z ObjectUpdate(A ann, Expr<A> expression, KeyValuePair<Expr<A>>[] updates) {
return this.value;
}
public Z Abs(A ann, String argument, Expr<A> body) {
return this.value;
}
public Z App(A ann, Expr<A> abstraction, Expr<A> argument) {
return this.value;
}
public Z Case(A ann, Expr<A>[] caseExpressions, CaseAlternative<A>[] caseAlternatives) {
return this.value;
}
public Z Let(A ann, Bind<A>[] binds, Expr<A> expression) {
return this.value;
}
}
}
public static final class Var<A> extends Expr<A> {
public final A annotation;
public final QualifiedIdent value;
public Var(@JsonProperty("annotation") A annotation, @JsonProperty("value") QualifiedIdent value) {
this.annotation = annotation;
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Var(annotation, value);
}
}
public static final class Literal<A> extends Expr<A> {
public final A annotation;
public final LiteralValue<Expr<A>> value;
public Literal(@JsonProperty("annotation") A annotation, @JsonProperty("value") LiteralValue<Expr<A>> value) {
this.annotation = annotation;
this.value = value;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Literal(annotation, value);
}
}
public static final class Constructor<A> extends Expr<A> {
public final A annotation;
public final String typeName;
public final String constructorName;
public final String[] fieldNames;
public Constructor(@JsonProperty("annotation") A annotation, @JsonProperty("typeName") String typeName,
@JsonProperty("constructorName") String constructorName,
@JsonProperty("fieldNames") String[] fieldNames) {
this.annotation = annotation;
this.typeName = typeName;
this.constructorName = constructorName;
this.fieldNames = fieldNames;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Constructor(annotation, typeName, constructorName, fieldNames);
}
}
public static final class Accessor<A> extends Expr<A> {
public final A annotation;
public final String fieldName;
public final Expr<A> expression;
public Accessor(@JsonProperty("annotation") A annotation, @JsonProperty("fieldName") String fieldName,
@JsonProperty("expression") Expr<A> expression) {
this.annotation = annotation;
this.fieldName = fieldName;
this.expression = expression;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Accessor(annotation, fieldName, expression);
}
}
public static final class ObjectUpdate<A> extends Expr<A> {
public final A annotation;
public final Expr<A> expression;
public final KeyValuePair<Expr<A>>[] updates;
public ObjectUpdate(@JsonProperty("annotation") A annotation, @JsonProperty("expression") Expr<A> expression,
@JsonProperty("updates") KeyValuePair<Expr<A>>[] updates) {
this.annotation = annotation;
this.expression = expression;
this.updates = updates;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.ObjectUpdate(annotation, expression, updates);
}
}
public static final class Abs<A> extends Expr<A> {
public final A annotation;
public final String argument;
public final Expr<A> body;
public Abs(@JsonProperty("annotation") A annotation, @JsonProperty("argument") String argument,
@JsonProperty("body") Expr<A> body) {
this.annotation = annotation;
this.argument = argument;
this.body = body;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Abs(annotation, argument, body);
}
}
public static final class App<A> extends Expr<A> {
public final A annotation;
public final Expr<A> abstraction;
public final Expr<A> argument;
public App(@JsonProperty("annotation") A annotation, @JsonProperty("abstraction") Expr<A> abstraction,
@JsonProperty("argument") Expr<A> argument) {
this.annotation = annotation;
this.abstraction = abstraction;
this.argument = argument;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.App(annotation, abstraction, argument);
}
}
public static final class Case<A> extends Expr<A> {
public final A annotation;
public final Expr<A>[] caseExpressions;
public final CaseAlternative<A>[] caseAlternatives;
public Case(@JsonProperty("annotation") A annotation,
@JsonProperty("caseExpressions") Expr<A>[] caseExpressions,
@JsonProperty("caseAlternatives") CaseAlternative<A>[] caseAlternatives) {
this.annotation = annotation;
this.caseExpressions = caseExpressions;
this.caseAlternatives = caseAlternatives;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Case(annotation, caseExpressions, caseAlternatives);
}
}
public static final class Let<A> extends Expr<A> {
public final A annotation;
public final Bind<A>[] binds;
public final Expr<A> expression;
public Let(@JsonProperty("annotation") A annotation, @JsonProperty("binds") Bind<A>[] binds,
@JsonProperty("expression") Expr<A> expression) {
this.annotation = annotation;
this.binds = binds;
this.expression = expression;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Let(annotation, binds, expression);
}
}
public static final class CaseAlternative<A> {
public final Binder<A>[] binders;
public final boolean isGuarded;
public final Expr<A> expression;
public final Guard<A>[] expressions;
public CaseAlternative(@JsonProperty("binders") Binder<A>[] binders,
@JsonProperty("isGuarded") boolean isGuarded, @JsonProperty("expression") Expr<A> expression,
@JsonProperty("expressions") Guard<A>[] expressions) {
this.binders = binders;
this.isGuarded = isGuarded;
this.expression = expression;
this.expressions = expressions;
}
}
public static final class Guard<A> {
public final Expr<A> guard;
public final Expr<A> expression;
public Guard(@JsonProperty("guard") Expr<A> guard, @JsonProperty("expression") Expr<A> expression) {
this.guard = guard;
this.expression = expression;
}
}
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "binderType")
@JsonSubTypes({ @JsonSubTypes.Type(value = VarBinder.class, name = "VarBinder"),
@JsonSubTypes.Type(value = NullBinder.class, name = "NullBinder"),
@JsonSubTypes.Type(value = LiteralBinder.class, name = "LiteralBinder"),
@JsonSubTypes.Type(value = ConstructorBinder.class, name = "ConstructorBinder"),
@JsonSubTypes.Type(value = NamedBinder.class, name = "NamedBinder") })
public static abstract class Binder<A> {
public abstract <Z> Z fold(Fold<A, Z> cases);
public interface Fold<A, Z> {
public Z VarBinder(A ann, String identifier);
public Z NullBinder(A ann);
public Z LiteralBinder(A ann, LiteralValue<Binder<A>> literal);
public Z ConstructorBinder(A ann, QualifiedIdent typeName, QualifiedIdent constructorName,
Binder<A>[] binders);
public Z NamedBinder(A ann, String identifier, Binder<A> binder);
}
}
public static final class VarBinder<A> extends Binder<A> {
public final A annotation;
public final String identifier;
public VarBinder(@JsonProperty("annotation") A annotation, @JsonProperty("identifier") String identifier) {
this.annotation = annotation;
this.identifier = identifier;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.VarBinder(annotation, identifier);
}
}
public static final class NullBinder<A> extends Binder<A> {
public final A annotation;
public NullBinder(@JsonProperty("annotation") A annotation) {
this.annotation = annotation;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.NullBinder(annotation);
}
}
public static final class LiteralBinder<A> extends Binder<A> {
public final A annotation;
public final LiteralValue<Binder<A>> literal;
public LiteralBinder(@JsonProperty("annotation") A annotation,
@JsonProperty("literal") LiteralValue<Binder<A>> literal) {
this.annotation = annotation;
this.literal = literal;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.LiteralBinder(annotation, literal);
}
}
public static final class ConstructorBinder<A> extends Binder<A> {
public final A annotation;
public final QualifiedIdent typeName;
public final QualifiedIdent constructorName;
public final Binder<A>[] binders;
public ConstructorBinder(@JsonProperty("annotation") A annotation,
@JsonProperty("typeName") QualifiedIdent typeName,
@JsonProperty("constructorName") QualifiedIdent constructorName,
@JsonProperty("binders") Binder<A>[] binders) {
this.annotation = annotation;
this.typeName = typeName;
this.constructorName = constructorName;
this.binders = binders;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.ConstructorBinder(annotation, typeName, constructorName, binders);
}
}
public static final class NamedBinder<A> extends Binder<A> {
public final A annotation;
public final String identifier;
public final Binder<A> binder;
public NamedBinder(@JsonProperty("annotation") A annotation, @JsonProperty("identifier") String identifier,
@JsonProperty("binder") Binder<A> binder) {
this.annotation = annotation;
this.identifier = identifier;
this.binder = binder;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.NamedBinder(annotation, identifier, binder);
}
}
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "bindType")
@JsonSubTypes({ @JsonSubTypes.Type(value = NonRec.class, name = "NonRec"),
@JsonSubTypes.Type(value = Rec.class, name = "Rec") })
public static abstract class Bind<A> {
public abstract <Z> Z fold(Fold<A, Z> cases);
public interface Fold<A, Z> {
public Z NonRec(A ann, String identifier, Expr<A> expression);
public Z Rec(Binding<A>[] binds);
}
}
public static final class NonRec<A> extends Bind<A> {
public final A annotation;
public final String identifier;
public final Expr<A> expression;
public NonRec(@JsonProperty("annotation") A annotation, @JsonProperty("identifier") String identifier,
@JsonProperty("expression") Expr<A> expression) {
this.annotation = annotation;
this.identifier = identifier;
this.expression = expression;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.NonRec(annotation, identifier, expression);
}
}
public static final class Rec<A> extends Bind<A> {
public final Binding<A>[] binds;
public Rec(@JsonProperty("binds") Binding<A>[] binds) {
this.binds = binds;
}
public final <Z> Z fold(Fold<A, Z> cases) {
return cases.Rec(binds);
}
}
public static final class Binding<A> {
public final A annotation;
public final String identifier;
public final Expr<A> expression;
public Binding(@JsonProperty("annotation") A annotation, @JsonProperty("identifier") String identifier,
@JsonProperty("expression") Expr<A> expression) {
this.annotation = annotation;
this.identifier = identifier;
this.expression = expression;
}
}
public static final class Module<A> {
public final SourceSpan sourceSpan;
public final String[] moduleName;
public final String modulePath;
public final String[][] imports;
public final String[] exports;
public final String[] foreign;
public final Bind<A>[] decls;
public final String builtWith;
public Module(@JsonProperty("sourceSpan") SourceSpan sourceSpan,
@JsonProperty("moduleName") String[] moduleName, @JsonProperty("modulePath") String modulePath,
@JsonProperty("imports") String[][] imports, @JsonProperty("exports") String[] exports,
@JsonProperty("foreign") String[] foreign, @JsonProperty("decls") Bind<A>[] decls,
@JsonProperty("builtWith") String builtWith) {
this.sourceSpan = sourceSpan;
this.moduleName = moduleName;
this.modulePath = modulePath;
this.imports = imports;
this.exports = exports;
this.foreign = foreign;
this.decls = decls;
this.builtWith = builtWith;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment