adapt revisit

This commit is contained in:
Florian Steurer 2015-11-22 15:26:17 +01:00
parent be6a719433
commit 3f4f2cd27b
3 changed files with 38 additions and 31 deletions

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
@ -48,5 +49,5 @@ public interface IFiniteClosure {
public Set<Type> grArg(PlaceholderType type);
public Set<Type> smArg(PlaceholderType type);
public Type getType(Type type);
public Optional<Type> getGenericType(String typeName);
}

View File

@ -308,42 +308,45 @@ public class RuleSet implements IRuleSet{
if(pair.getPairOp() != PairOperator.SMALLERDOT)
return Optional.empty();
Type lhsType = pair.getLhsType();
if(!(lhsType instanceof SimpleType))
Type typeD = pair.getLhsType();
if(!(typeD instanceof SimpleType))
return Optional.empty();
Type rhsType = pair.getRhsType();
if(!(rhsType instanceof SimpleType))
Type typeDs = pair.getRhsType();
if(!(typeDs instanceof SimpleType))
return Optional.empty();
if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0)
if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0)
return Optional.empty();
if(lhsType.getName().equals(rhsType.getName()))
if(typeD.getName().equals(typeDs.getName()))
return Optional.empty();
Type lhsFromFc = finiteClosure.getType(lhsType);
Optional<Type> opt = finiteClosure.getGenericType(typeD.getName());
if(lhsFromFc == null)
if(!opt.isPresent())
return Optional.empty();
Set<Type> greater = finiteClosure.greater(lhsFromFc);
Optional<Type> opt = greater.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny();
// The generic Version of Type D (D<a1, a2, a3, ... >)
Type typeDgen = opt.get();
// Actually greater+ because the types are ensured to have different names
Set<Type> greater = finiteClosure.greater(typeDgen);
opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
if(!opt.isPresent())
return Optional.empty();
Type newLhs = opt.get();
TypeParams lhsTypeParams = lhsType.getTypeParams();
TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams();
TypeParams typeDParams = typeD.getTypeParams();
TypeParams typeDgenParams = typeDgen.getTypeParams();
Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0));
for(int i = 1; i < lhsTypeParams.size(); i++)
unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i)));
Unifier unif = new Unifier(typeDgenParams.get(0), typeDParams.get(0));
for(int i = 1; i < typeDParams.size(); i++)
unif.andThen(new Unifier(typeDgenParams.get(i), typeDParams.get(i)));
return Optional.of(new MPair(newLhs.apply(unif), rhsType, PairOperator.SMALLERDOT));
return Optional.of(new MPair(newLhs.apply(unif), typeDs, PairOperator.SMALLERDOT));
}
@Override
@ -451,11 +454,15 @@ public class RuleSet implements IRuleSet{
private int[] pi(Type C, Type D) {
Type cFromFc = null;
if(C instanceof SimpleType)
cFromFc = finiteClosure.getType(C);
else if(C instanceof ExtendsType)
cFromFc = new ExtendsType(finiteClosure.getType(((ExtendsType) C).getExtendedType()));
else if(C instanceof SuperType)
cFromFc = new SuperType(finiteClosure.getType(((SuperType) C).getSuperedType()));
cFromFc = finiteClosure.getGenericType(C.getName()).orElse(null);
else if(C instanceof ExtendsType) {
Optional<Type> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
if(opt.isPresent()) cFromFc = new ExtendsType(opt.get());
}
else if(C instanceof SuperType) {
Optional<Type> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
if(opt.isPresent()) cFromFc = new SuperType(opt.get());
}
if(cFromFc == null)
return new int[0];

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.typinference.unify.model;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
@ -64,7 +65,6 @@ public class FiniteClosure implements IFiniteClosure {
return result;
}
/**
* Returns all types of the finite closure that are supertypes of the argument.
* @return The set of supertypes of the argument.
@ -79,7 +79,6 @@ public class FiniteClosure implements IFiniteClosure {
return result;
}
@Override
public Set<Type> grArg(Type type) {
return type.grArg(this);
@ -192,18 +191,18 @@ public class FiniteClosure implements IFiniteClosure {
}
@Override
public Type getType(Type type) {
if(!strInheritanceGraph.containsKey(type.getName()))
return null;
public Optional<Type> getGenericType(String typeName) {
if(!strInheritanceGraph.containsKey(typeName))
return Optional.empty();
HashSet<Node<Type>> candidates = strInheritanceGraph.get(type.getName());
HashSet<Node<Type>> candidates = strInheritanceGraph.get(typeName);
for(Node<Type> node : candidates) {
Type candidate = node.getContent();
if(candidate.getTypeParams().arePlaceholders())
return candidate;
return Optional.of(candidate);
}
return null;
return Optional.empty();
}
}