Type: constrainEqualTo* implementation

This commit is contained in:
Arne Keller 2021-01-30 09:49:13 +01:00
parent aafb8ead44
commit dee5e2b3f0
7 changed files with 100 additions and 34 deletions

View File

@ -1,5 +1,6 @@
package edu.kit.typicalc.model; package edu.kit.typicalc.model;
public enum UnificationError { public enum UnificationError {
INFINITE_TYPE INFINITE_TYPE,
DIFFERENT_TYPES
} }

View File

@ -10,8 +10,8 @@ import java.util.Objects;
*/ */
public class FunctionType extends Type { public class FunctionType extends Type {
private Type parameter; private final Type parameter;
private Type output; private final Type output;
/** /**
* Initializes a new FunctionType with the given parameter and output types. * Initializes a new FunctionType with the given parameter and output types.
* @param parameter the type of this functions parameter * @param parameter the type of this functions parameter
@ -60,9 +60,9 @@ public class FunctionType extends Type {
* @param type the other type * @param type the other type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) { public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
//TODO return type.constrainEqualToFunction(this);
return null;
} }
/** /**
@ -71,9 +71,9 @@ public class FunctionType extends Type {
* @param type the function type * @param type the function type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) { @Override
//TODO public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
return null; return UnificationUtil.functionFunction(this, type);
} }
/** /**
@ -82,9 +82,9 @@ public class FunctionType extends Type {
* @param type the named type * @param type the named type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) { public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
//TODO return UnificationUtil.functionNamed(this, type);
return null;
} }
/** /**
@ -93,9 +93,9 @@ public class FunctionType extends Type {
* @param type the type variable * @param type the type variable
* @return the unification steps necessary, or an error if that is impossible * @return the unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) { public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
//TODO return UnificationUtil.functionVariable(this, type);
return null;
} }
/** /**

View File

@ -18,7 +18,7 @@ public class NamedType extends Type {
*/ */
public static final NamedType INT = new NamedType("int"); public static final NamedType INT = new NamedType("int");
private String name; private final String name;
/** /**
* Initializes a new NamedType with the given name. * Initializes a new NamedType with the given name.
@ -72,9 +72,9 @@ public class NamedType extends Type {
* @param type the other type * @param type the other type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) { public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
//TODO return type.constrainEqualToNamedType(this);
return null;
} }
/** /**
@ -83,9 +83,9 @@ public class NamedType extends Type {
* @param type the function type * @param type the function type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) { @Override
//TODO public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
return null; return UnificationUtil.functionNamed(type, this);
} }
/** /**
@ -94,9 +94,9 @@ public class NamedType extends Type {
* @param type the named type * @param type the named type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) { public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
//TODO return UnificationUtil.namedNamed(this, type);
return null;
} }
/** /**
@ -105,9 +105,9 @@ public class NamedType extends Type {
* @param type the type variable * @param type the type variable
* @return the unification steps necessary, or an error if that is impossible * @return the unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) { public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
//TODO return UnificationUtil.variableNamed(type, this);
return null;
} }
@Override @Override

View File

@ -42,7 +42,7 @@ public abstract class Type {
* @param type the function type * @param type the function type
* @return unification steps necessary, or an error if that is impossible * @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 * Computes the necessary constraints (and substitution) to unify this type with a

View File

@ -56,6 +56,7 @@ public class TypeVariable extends Type {
* @param b the type to insert * @param b the type to insert
* @return itself, or b if a is equal to this object * @return itself, or b if a is equal to this object
*/ */
@Override
public Type substitute(TypeVariable a, Type b) { public Type substitute(TypeVariable a, Type b) {
if (this.equals(a)) { if (this.equals(a)) {
return b; return b;
@ -79,9 +80,9 @@ public class TypeVariable extends Type {
* @param type the other type * @param type the other type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) { public Result<UnificationActions, UnificationError> constrainEqualTo(Type type) {
//TODO return type.constrainEqualToVariable(this);
return null;
} }
/** /**
@ -90,9 +91,9 @@ public class TypeVariable extends Type {
* @param type the function type * @param type the function type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
public Result<UnificationActions, UnificationError> constrainEqualToFunction(Type type) { @Override
//TODO public Result<UnificationActions, UnificationError> constrainEqualToFunction(FunctionType type) {
return null; return UnificationUtil.functionVariable(type, this);
} }
/** /**
@ -101,9 +102,9 @@ public class TypeVariable extends Type {
* @param type the named type * @param type the named type
* @return unification steps necessary, or an error if that is impossible * @return unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) { public Result<UnificationActions, UnificationError> constrainEqualToNamedType(NamedType type) {
//TODO return UnificationUtil.variableNamed(this, type);
return null;
} }
/** /**
@ -112,9 +113,9 @@ public class TypeVariable extends Type {
* @param type the type variable * @param type the type variable
* @return the unification steps necessary, or an error if that is impossible * @return the unification steps necessary, or an error if that is impossible
*/ */
@Override
public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) { public Result<UnificationActions, UnificationError> constrainEqualToVariable(TypeVariable type) {
//TODO return UnificationUtil.variableVariable(this, type);
return null;
} }
@Override @Override

View File

@ -4,14 +4,15 @@ import edu.kit.typicalc.model.Constraint;
import edu.kit.typicalc.model.Substitution; import edu.kit.typicalc.model.Substitution;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Optional; import java.util.Optional;
/** /**
* Models the necessary actions to process a constraint. * Models the necessary actions to process a constraint.
*/ */
public class UnificationActions { public class UnificationActions {
private Collection<Constraint> constraints; private final Collection<Constraint> constraints;
private Optional<Substitution> substitution; private final Optional<Substitution> substitution;
/** /**
* Initializes this object using the provided constraints and substitution. * Initializes this object using the provided constraints and substitution.
@ -23,6 +24,14 @@ public class UnificationActions {
this.substitution = substitution; this.substitution = substitution;
} }
/**
* Initializes an empty object.
*/
protected UnificationActions() {
this.constraints = Collections.emptyList();
this.substitution = Optional.empty();
}
/** /**
* Getter for constraints * Getter for constraints
* @return the constraints stored in this object * @return the constraints stored in this object

View File

@ -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());
}
}
}