freshPlaceholder / implemented funn rules

This commit is contained in:
Florian Steurer 2016-03-23 11:44:31 +01:00
parent 11bcf5735a
commit a5b86dc84c
3 changed files with 79 additions and 7 deletions

View File

@ -8,13 +8,21 @@ public class FunNType extends Type {
public FunNType(TypeParams p) {
super("FuN", p);
if(p.size() == 0)
throw new IllegalArgumentException("Function types need at least one type parameter");
}
@Override
public Type setTypeParams(TypeParams newTp) {
if(newTp.size() == 0)
throw new IllegalArgumentException("Function types need at least one type parameter");
return new FunNType(newTp);
}
public int getN() {
return typeParams.size()-1;
}
@Override
Set<Type> smArg(IFiniteClosure fc) {
return fc.smArg(this);
@ -31,4 +39,6 @@ public class FunNType extends Type {
return null;
}
// TODO equals und hashcode
}

View File

@ -28,7 +28,6 @@ public final class PlaceholderType extends Type{
/**
* Returns random char between 'a' and 'z'
* @return
*/
private static char randomChar() {
return (char) (new Random().nextInt(22) + 97);

View File

@ -14,6 +14,7 @@ import junit.framework.Assert;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.FunNType;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
@ -660,19 +661,81 @@ public class RuleSet implements IRuleSet{
@Override
public Optional<Set<MPair>> reduceFunN(MPair pair) {
// TODO Auto-generated method stub
return null;
if(pair.getPairOp() != PairOperator.SMALLERDOT)
return Optional.empty();
Type lhsType = pair.getLhsType();
Type rhsType = pair.getRhsType();
if(!(lhsType instanceof FunNType) || !(rhsType instanceof FunNType))
return Optional.empty();
FunNType funNLhsType = (FunNType) lhsType;
FunNType funNRhsType = (FunNType) rhsType;
if(funNLhsType.getN() != funNRhsType.getN())
return Optional.empty();
Set<MPair> result = new HashSet<MPair>();
result.add(new MPair(funNLhsType.getTypeParams().get(0), funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT));
for(int i = 1; i < funNLhsType.getTypeParams().size(); i++)
result.add(new MPair(funNRhsType.getTypeParams().get(i), funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT));
return Optional.of(result);
}
@Override
public Optional<Set<MPair>> greaterFunN(MPair pair) {
// TODO Auto-generated method stub
return null;
if(pair.getPairOp() != PairOperator.SMALLERDOT)
return Optional.empty();
Type lhsType = pair.getLhsType();
Type rhsType = pair.getRhsType();
if(!(lhsType instanceof FunNType) || !(rhsType instanceof PlaceholderType))
return Optional.empty();
FunNType funNLhsType = (FunNType) lhsType;
Set<MPair> result = new HashSet<MPair>();
Type[] freshPlaceholders = new Type[funNLhsType.getTypeParams().size()];
for(int i = 0; i < freshPlaceholders.length; i++)
freshPlaceholders[i] = PlaceholderType.freshPlaceholder();
result.add(new MPair(funNLhsType.getTypeParams().get(0), freshPlaceholders[0], PairOperator.SMALLERDOT));
for(int i = 1; i < funNLhsType.getTypeParams().size(); i++)
result.add(new MPair(freshPlaceholders[i], funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT));
result.add(new MPair(rhsType, funNLhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT));
return Optional.of(result);
}
@Override
public Optional<Set<MPair>> smallerFunN(MPair pair) {
// TODO Auto-generated method stub
return null;
if(pair.getPairOp() != PairOperator.SMALLERDOT)
return Optional.empty();
Type lhsType = pair.getLhsType();
Type rhsType = pair.getRhsType();
if(!(lhsType instanceof PlaceholderType) || !(rhsType instanceof FunNType))
return Optional.empty();
FunNType funNRhsType = (FunNType) rhsType;
Set<MPair> result = new HashSet<MPair>();
Type[] freshPlaceholders = new Type[funNRhsType.getTypeParams().size()];
for(int i = 0; i < freshPlaceholders.length; i++)
freshPlaceholders[i] = PlaceholderType.freshPlaceholder();
result.add(new MPair(freshPlaceholders[0], funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT));
for(int i = 1; i < funNRhsType.getTypeParams().size(); i++)
result.add(new MPair(funNRhsType.getTypeParams().get(i), freshPlaceholders[i], PairOperator.SMALLERDOT));
result.add(new MPair(lhsType, funNRhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT));
return Optional.of(result);
}
}