-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tcr/num: Introduce Num typeclass and Int64 type
Towards #6
- Loading branch information
1 parent
b34f3b4
commit c17d425
Showing
14 changed files
with
335 additions
and
56 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
src/main/java/xyz/leutgeb/lorenz/atlas/ast/ArithmeticOperator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xyz.leutgeb.lorenz.atlas.ast; | ||
|
||
import java.io.PrintStream; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
|
||
import static java.util.function.Predicate.isEqual; | ||
|
||
public enum ArithmeticOperator { | ||
PLUS(List.of("+")), | ||
MINUS(List.of("-")), | ||
TIMES(List.of("*", "⨯")), | ||
DIV(List.of("/")); | ||
|
||
private final List<String> tokens; | ||
|
||
ArithmeticOperator(List<String> tokens) { | ||
if (tokens.isEmpty()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.tokens = Collections.unmodifiableList(tokens); | ||
} | ||
|
||
public static ArithmeticOperator fromToken(String token) { | ||
for (ArithmeticOperator op : ArithmeticOperator.values()) { | ||
if (op.tokens.stream().anyMatch(isEqual(token))) { | ||
return op; | ||
} | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
public void printTo(PrintStream out) { | ||
out.print(tokens.get(0)); | ||
} | ||
|
||
public void printHaskellTo(PrintStream out) { | ||
printTo(out); | ||
} | ||
|
||
public void printJavaTo(String x, String y, PrintStream out) { | ||
out.print(x); | ||
out.print(" "); | ||
printTo(out); | ||
out.print(" "); | ||
out.print(y); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return tokens.get(0); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
src/main/java/xyz/leutgeb/lorenz/atlas/ast/expressions/ArithmeticExpression.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package xyz.leutgeb.lorenz.atlas.ast.expressions; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import xyz.leutgeb.lorenz.atlas.ast.ArithmeticOperator; | ||
import xyz.leutgeb.lorenz.atlas.ast.Normalization; | ||
import xyz.leutgeb.lorenz.atlas.ast.sources.Derived; | ||
import xyz.leutgeb.lorenz.atlas.ast.sources.Source; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeClass; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeConstraint; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeError; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.types.BoolType; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.types.Type; | ||
import xyz.leutgeb.lorenz.atlas.unification.UnificationContext; | ||
import xyz.leutgeb.lorenz.atlas.util.IntIdGenerator; | ||
|
||
import java.io.PrintStream; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
import java.util.stream.Stream; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class ArithmeticExpression extends Expression { | ||
|
||
@NonNull Expression left; | ||
@NonNull ArithmeticOperator operator; | ||
@NonNull Expression right; | ||
|
||
public ArithmeticExpression( | ||
Source source, | ||
@NonNull Expression left, | ||
@NonNull ArithmeticOperator operator, | ||
@NonNull Expression right) { | ||
super(source); | ||
this.left = left; | ||
this.operator = operator; | ||
this.right = right; | ||
} | ||
|
||
private ArithmeticExpression( | ||
Source source, Expression left, ArithmeticOperator operator, Expression right, Type type) { | ||
super(source); | ||
this.left = left; | ||
this.operator = operator; | ||
this.right = right; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public Stream<? extends Expression> getChildren() { | ||
return Stream.of(left, right); | ||
} | ||
|
||
@Override | ||
public Type inferInternal(UnificationContext context) throws TypeError { | ||
var ty = context.fresh(); | ||
context.addEquivalenceIfNotEqual(right.infer(context), ty, source); | ||
context.addEquivalenceIfNotEqual(left.infer(context), ty, source); | ||
context | ||
.getSignature(context.getFunctionInScope(), source) | ||
.addConstraint(new TypeConstraint(TypeClass.NUM, ty)); | ||
return ty; | ||
} | ||
|
||
@Override | ||
public Expression normalize(Stack<Normalization> context, IntIdGenerator idGenerator) { | ||
// TODO(lorenzleutgeb): Only create new expression if necessary! | ||
return new ArithmeticExpression( | ||
Derived.anf(this), | ||
left.normalize(context, idGenerator), | ||
operator, | ||
right.normalize(context, idGenerator)); | ||
} | ||
|
||
@Override | ||
public void printTo(PrintStream out, int indentation) { | ||
left.printTo(out, indentation); | ||
out.print(" "); | ||
operator.printTo(out); | ||
out.print(" "); | ||
right.printTo(out, indentation); | ||
} | ||
|
||
@Override | ||
public void printHaskellTo(PrintStream out, int indentation, String currentFunction) { | ||
left.printHaskellTo(out, indentation, currentFunction); | ||
out.print(" "); | ||
operator.printHaskellTo(out); | ||
out.print(" "); | ||
right.printHaskellTo(out, indentation, currentFunction); | ||
} | ||
|
||
@Override | ||
public void printJavaTo(PrintStream out, int indentation, String currentFunction) { | ||
operator.printJavaTo( | ||
((IdentifierExpression) left).getName(), ((IdentifierExpression) right).getName(), out); | ||
} | ||
|
||
@Override | ||
public Expression unshare(IntIdGenerator idGenerator, boolean lazy) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean isImmediate() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Expression rename(Map<String, String> renaming) { | ||
if (freeVariables().stream() | ||
.map(IdentifierExpression::getName) | ||
.anyMatch(renaming::containsKey)) { | ||
return new ArithmeticExpression( | ||
Derived.rename(this), left.rename(renaming), operator, right.rename(renaming), type); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return left + " " + operator + " " + right; | ||
} | ||
|
||
@Override | ||
public boolean isTerminal() { | ||
return true; | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/xyz/leutgeb/lorenz/atlas/ast/expressions/NumberExpression.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package xyz.leutgeb.lorenz.atlas.ast.expressions; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import xyz.leutgeb.lorenz.atlas.ast.sources.Source; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeClass; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeConstraint; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.TypeError; | ||
import xyz.leutgeb.lorenz.atlas.typing.simple.types.Type; | ||
import xyz.leutgeb.lorenz.atlas.unification.UnificationContext; | ||
import xyz.leutgeb.lorenz.atlas.util.IntIdGenerator; | ||
|
||
import java.util.stream.Stream; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class NumberExpression extends Expression { | ||
@NonNull String number; | ||
|
||
public NumberExpression(String number, Source source) { | ||
super(source); | ||
this.number = number; | ||
} | ||
|
||
@Override | ||
protected Stream<? extends Expression> getChildren() { | ||
return Stream.of(); | ||
} | ||
|
||
@Override | ||
protected Type inferInternal(UnificationContext context) throws TypeError { | ||
var ty = context.fresh(); | ||
context | ||
.getSignature(context.getFunctionInScope(), source) | ||
.addConstraint(new TypeConstraint(TypeClass.NUM, ty)); | ||
return ty; | ||
} | ||
|
||
@Override | ||
public Expression unshare(IntIdGenerator idGenerator, boolean lazy) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean isImmediate() { | ||
return true; | ||
} | ||
} |
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
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
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
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
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
24 changes: 0 additions & 24 deletions
24
src/main/java/xyz/leutgeb/lorenz/atlas/typing/simple/types/BaseType.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.