From 3f4f2cd27b51fe2a8c1ce8b814514a401b8cb555 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 22 Nov 2015 15:26:17 +0100 Subject: [PATCH] adapt revisit --- .../unify/interfaces/IFiniteClosure.java | 3 +- .../typeinference/unifynew/RuleSet.java | 51 +++++++++++-------- .../unify/model/FiniteClosure.java | 15 +++--- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 796a2706..e469f41a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -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 grArg(PlaceholderType type); public Set smArg(PlaceholderType type); - public Type getType(Type type); + public Optional getGenericType(String typeName); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index f2fd7bc4..0d36131b 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -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 opt = finiteClosure.getGenericType(typeD.getName()); - if(lhsFromFc == null) + if(!opt.isPresent()) return Optional.empty(); - Set greater = finiteClosure.greater(lhsFromFc); - - Optional opt = greater.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny(); + // The generic Version of Type D (D) + Type typeDgen = opt.get(); + + // Actually greater+ because the types are ensured to have different names + Set 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 opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName()); + if(opt.isPresent()) cFromFc = new ExtendsType(opt.get()); + } + else if(C instanceof SuperType) { + Optional opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName()); + if(opt.isPresent()) cFromFc = new SuperType(opt.get()); + } if(cFromFc == null) return new int[0]; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java index 92d04518..ae36f465 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java @@ -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 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 getGenericType(String typeName) { + if(!strInheritanceGraph.containsKey(typeName)) + return Optional.empty(); - HashSet> candidates = strInheritanceGraph.get(type.getName()); + HashSet> candidates = strInheritanceGraph.get(typeName); for(Node node : candidates) { Type candidate = node.getContent(); if(candidate.getTypeParams().arePlaceholders()) - return candidate; + return Optional.of(candidate); } - return null; + return Optional.empty(); } }