mirror of
https://gitlab.kit.edu/uskyk/typicalc.git
synced 2024-11-09 10:50:42 +00:00
Type: constrainEqualTo* implementation
This commit is contained in:
parent
aafb8ead44
commit
dee5e2b3f0
@ -1,5 +1,6 @@
|
||||
package edu.kit.typicalc.model;
|
||||
|
||||
public enum UnificationError {
|
||||
INFINITE_TYPE
|
||||
INFINITE_TYPE,
|
||||
DIFFERENT_TYPES
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ import java.util.Objects;
|
||||
*/
|
||||
public class FunctionType extends Type {
|
||||
|
||||
private Type parameter;
|
||||
private Type output;
|
||||
private final Type parameter;
|
||||
private final Type output;
|
||||
/**
|
||||
* Initializes a new FunctionType with the given parameter and output types.
|
||||
* @param parameter the type of this function’s parameter
|
||||
@ -60,9 +60,9 @@ public class FunctionType extends Type {
|
||||
* @param type the other type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
return type.constrainEqualToFunction(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,9 +71,9 @@ public class FunctionType extends Type {
|
||||
* @param type the function type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
|
||||
return UnificationUtil.functionFunction(this, type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,9 +82,9 @@ public class FunctionType extends Type {
|
||||
* @param type the named type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.functionNamed(this, type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,9 +93,9 @@ public class FunctionType extends Type {
|
||||
* @param type the type variable
|
||||
* @return the unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.functionVariable(this, type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ public class NamedType extends Type {
|
||||
*/
|
||||
public static final NamedType INT = new NamedType("int");
|
||||
|
||||
private String name;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Initializes a new NamedType with the given name.
|
||||
@ -72,9 +72,9 @@ public class NamedType extends Type {
|
||||
* @param type the other type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
return type.constrainEqualToNamedType(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,9 +83,9 @@ public class NamedType extends Type {
|
||||
* @param type the function type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
|
||||
return UnificationUtil.functionNamed(type, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,9 +94,9 @@ public class NamedType extends Type {
|
||||
* @param type the named type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.namedNamed(this, type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -105,9 +105,9 @@ public class NamedType extends Type {
|
||||
* @param type the type variable
|
||||
* @return the unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.variableNamed(type, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -42,7 +42,7 @@ public abstract class Type {
|
||||
* @param type the function type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
public abstract Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type);
|
||||
public abstract Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type);
|
||||
|
||||
/**
|
||||
* Computes the necessary constraints (and substitution) to unify this type with a
|
||||
|
@ -56,6 +56,7 @@ public class TypeVariable extends Type {
|
||||
* @param b the type to insert
|
||||
* @return itself, or b if a is equal to this object
|
||||
*/
|
||||
@Override
|
||||
public Type substitute(TypeVariable a, Type b) {
|
||||
if (this.equals(a)) {
|
||||
return b;
|
||||
@ -79,9 +80,9 @@ public class TypeVariable extends Type {
|
||||
* @param type the other type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
return type.constrainEqualToVariable(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,9 +91,9 @@ public class TypeVariable extends Type {
|
||||
* @param type the function type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) {
|
||||
//TODO
|
||||
return null;
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
|
||||
return UnificationUtil.functionVariable(type, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,9 +102,9 @@ public class TypeVariable extends Type {
|
||||
* @param type the named type
|
||||
* @return unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.variableNamed(this, type);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,9 +113,9 @@ public class TypeVariable extends Type {
|
||||
* @param type the type variable
|
||||
* @return the unification steps necessary, or an error if that is impossible
|
||||
*/
|
||||
@Override
|
||||
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
|
||||
//TODO
|
||||
return null;
|
||||
return UnificationUtil.variableVariable(this, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,14 +4,15 @@ import edu.kit.typicalc.model.Constraint;
|
||||
import edu.kit.typicalc.model.Substitution;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Models the necessary actions to process a constraint.
|
||||
*/
|
||||
public class UnificationActions {
|
||||
private Collection<Constraint> constraints;
|
||||
private Optional<Substitution> substitution;
|
||||
private final Collection<Constraint> constraints;
|
||||
private final Optional<Substitution> substitution;
|
||||
|
||||
/**
|
||||
* Initializes this object using the provided constraints and substitution.
|
||||
@ -23,6 +24,14 @@ public class UnificationActions {
|
||||
this.substitution = substitution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes an empty object.
|
||||
*/
|
||||
protected UnificationActions() {
|
||||
this.constraints = Collections.emptyList();
|
||||
this.substitution = Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for constraints
|
||||
* @return the constraints stored in this object
|
||||
|
@ -0,0 +1,55 @@
|
||||
package edu.kit.typicalc.model.type;
|
||||
|
||||
import edu.kit.typicalc.model.Constraint;
|
||||
import edu.kit.typicalc.model.Substitution;
|
||||
import edu.kit.typicalc.model.UnificationError;
|
||||
import edu.kit.typicalc.util.Result;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Utility class to avoid unification logic duplication in type methods.
|
||||
*/
|
||||
final class UnificationUtil {
|
||||
// TODO: document class? implementation detail?
|
||||
private UnificationUtil() {
|
||||
|
||||
}
|
||||
|
||||
// TODO: check for infinite types
|
||||
|
||||
static Result<UnificationActions, UnificationError> functionFunction(FunctionType a, FunctionType b) {
|
||||
return new Result<>(new UnificationActions(List.of(
|
||||
new Constraint(a.getParameter(), b.getParameter()),
|
||||
new Constraint(a.getOutput(), b.getOutput())), Optional.empty()));
|
||||
}
|
||||
|
||||
static Result<UnificationActions, UnificationError> functionNamed(FunctionType a, NamedType b) {
|
||||
return new Result<>(null, UnificationError.DIFFERENT_TYPES);
|
||||
}
|
||||
|
||||
static Result<UnificationActions, UnificationError> functionVariable(FunctionType a, TypeVariable b) {
|
||||
return new Result<>(new UnificationActions(Collections.emptyList(),
|
||||
Optional.of(new Substitution(b, a))));
|
||||
}
|
||||
|
||||
static Result<UnificationActions, UnificationError> variableNamed(TypeVariable a, NamedType b) {
|
||||
return new Result<>(new UnificationActions(Collections.emptyList(),
|
||||
Optional.of(new Substitution(a, b))));
|
||||
}
|
||||
|
||||
static Result<UnificationActions, UnificationError> variableVariable(TypeVariable a, TypeVariable b) {
|
||||
return new Result<>(new UnificationActions(Collections.emptyList(),
|
||||
Optional.of(new Substitution(a, b))));
|
||||
}
|
||||
|
||||
static Result<UnificationActions, UnificationError> namedNamed(NamedType a, NamedType b) {
|
||||
if (a != b) {
|
||||
return new Result<>(null, UnificationError.DIFFERENT_TYPES);
|
||||
} else {
|
||||
return new Result<>(new UnificationActions());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user