From 89b53351ce19b46308aa7b931e432b34e7cdca9d Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Wed, 23 Dec 2015 13:25:43 +0100 Subject: [PATCH 01/11] martelli montanari unifikation --- .../unify/interfaces/IUnify.java | 2 +- .../unifynew/MartelliMontanariUnify.java | 77 +++++++++++++++---- .../typeinference/unifynew/RuleSet.java | 6 +- .../typeinference/unifynew/Unifier.java | 7 ++ 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index 34e39738..92dd941d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -10,5 +10,5 @@ import de.dhbwstuttgart.typinference.unify.model.MPair; * @author Florian Steurer */ public interface IUnify { - public Unifier unify(Set terms); + public Set unify(Set terms); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 3631ed24..89e02cbb 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -1,7 +1,12 @@ package de.dhbwstuttgart.typeinference.unifynew; import java.util.HashSet; +import java.util.LinkedList; +import java.util.Optional; +import java.util.Queue; import java.util.Set; +import java.util.Stack; +import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typinference.unify.model.MPair; @@ -16,43 +21,85 @@ import de.dhbwstuttgart.typinference.unify.model.TypeParams; public class MartelliMontanariUnify implements IUnify { @Override - public Unifier unify(Set terms) { - // TODO Auto-generated method stub - return null; + public Set unify(Set terms) { + Queue termsQ = new LinkedList<>(terms); + Set result = new HashSet<>(); + + + while(!termsQ.isEmpty()) { + MPair pair = termsQ.poll(); + + if(delete(pair)) + continue; + + Optional> optSet = decompose(pair); + + if(optSet == null) + return new HashSet<>(); // Unification failed + + if(optSet.isPresent()) { + termsQ.addAll(optSet.get()); + continue; + } + + Optional optPair = swap(pair); + + if(optPair.isPresent()) { + termsQ.add(optPair.get()); + continue; + } + + Optional optUni = eliminate(pair); + + if(optUni.isPresent()) { + Unifier uni = optUni.get(); + termsQ = termsQ.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); + result = result.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new)); + continue; + } + + // TODO occurs check? + } + + return result; } private boolean delete(MPair pair) { return pair.getRhsType().equals(pair.getLhsType()); } - private Set decompose(MPair pair) { + private Optional> decompose(MPair pair) { Set result = new HashSet<>(); Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); - - if(!rhs.getName().equals(lhs.getName()) || rhs.getTypeParams().size() != lhs.getTypeParams().size()) - return null; // conflict - + TypeParams rhsTypeParams = rhs.getTypeParams(); TypeParams lhsTypeParams = lhs.getTypeParams(); + + if(rhsTypeParams.size() == 0 || lhsTypeParams.size() == 0) + return Optional.empty(); + + if(!rhs.getName().equals(lhs.getName()) || rhsTypeParams.size() != lhsTypeParams.size()) + return null; // conflict for(int i = 0; i < rhsTypeParams.size(); i++) result.add(new MPair(rhsTypeParams.get(i), lhsTypeParams.get(i), PairOperator.EQUALSDOT)); - return result; + return Optional.of(result); } - private MPair swap(MPair pair) { + private Optional swap(MPair pair) { Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); if(lhs.getTypeParams().size() != 0 && rhs.getTypeParams().size() == 0) - return new MPair(rhs, lhs, PairOperator.EQUALSDOT); - return pair; + return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); + + return Optional.empty(); } - private Unifier eliminate(MPair pair) { + private Optional eliminate(MPair pair) { Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); @@ -60,9 +107,9 @@ public class MartelliMontanariUnify implements IUnify { for(Type t : rhsTypeParams) if(lhs.equals(t)) - return new Unifier(); //identity-"unifier" + return Optional.empty(); - return new Unifier(lhs, rhs); + return Optional.of(new Unifier(lhs, rhs)); } private boolean check(MPair pair) { diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 3dff9384..1f5cd265 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -346,7 +346,7 @@ public class RuleSet implements IRuleSet{ 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), typeDs, PairOperator.SMALLERDOT)); + return Optional.of(new MPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT)); } @Override @@ -392,7 +392,7 @@ public class RuleSet implements IRuleSet{ 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), typeExtDs, PairOperator.SMALLERDOTWC)); + return Optional.of(new MPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC)); } @Override @@ -444,7 +444,7 @@ public class RuleSet implements IRuleSet{ for(int i = 1; i < typeDParams.size(); i++) unif.andThen(new Unifier(typeSupDsgenParams.get(i), typeDParams.get(i))); - return Optional.of(new MPair(newLhs.apply(unif), newRhs, PairOperator.SMALLERDOTWC)); + return Optional.of(new MPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC)); } /** diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java index 4528a934..0428a84e 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java @@ -1,7 +1,10 @@ package de.dhbwstuttgart.typeinference.unifynew; +import java.util.Set; import java.util.function.Function; +import java.util.stream.Collectors; +import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.Type; import de.dhbwstuttgart.typinference.unify.model.TypeParams; @@ -26,6 +29,10 @@ public class Unifier implements Function { return t.apply(this); } + public MPair apply(MPair p) { + return new MPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp()); + } + public Type getSource() { return source; } From 264910bfa45761a95026955cb192b78d25664435 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Wed, 23 Dec 2015 22:53:24 +0100 Subject: [PATCH 02/11] tests --- .../unify/interfaces/IUnify.java | 3 +- .../unifynew/MartelliMontanariUnify.java | 15 +++- .../typeinference/unifynew/Unifier.java | 6 ++ test/unify/StandardUnifyTest.java | 74 +++++++++++++++++++ 4 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 test/unify/StandardUnifyTest.java diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index 92dd941d..1f73b550 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; +import java.util.Optional; import java.util.Set; import de.dhbwstuttgart.typeinference.unifynew.Unifier; @@ -10,5 +11,5 @@ import de.dhbwstuttgart.typinference.unify.model.MPair; * @author Florian Steurer */ public interface IUnify { - public Set unify(Set terms); + public Optional> unify(Set terms); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 89e02cbb..0f626010 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -11,6 +11,7 @@ import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; +import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typinference.unify.model.Type; import de.dhbwstuttgart.typinference.unify.model.TypeParams; @@ -21,7 +22,7 @@ import de.dhbwstuttgart.typinference.unify.model.TypeParams; public class MartelliMontanariUnify implements IUnify { @Override - public Set unify(Set terms) { + public Optional> unify(Set terms) { Queue termsQ = new LinkedList<>(terms); Set result = new HashSet<>(); @@ -35,7 +36,7 @@ public class MartelliMontanariUnify implements IUnify { Optional> optSet = decompose(pair); if(optSet == null) - return new HashSet<>(); // Unification failed + return Optional.empty(); // Unification failed if(optSet.isPresent()) { termsQ.addAll(optSet.get()); @@ -55,13 +56,16 @@ public class MartelliMontanariUnify implements IUnify { Unifier uni = optUni.get(); termsQ = termsQ.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); result = result.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new)); + result.add(pair); continue; } - // TODO occurs check? + // TODO occurs check notwendig? + if(!check(pair)) // Occurs-Check + return Optional.empty(); } - return result; + return Optional.of(result); } private boolean delete(MPair pair) { @@ -96,6 +100,9 @@ public class MartelliMontanariUnify implements IUnify { if(lhs.getTypeParams().size() != 0 && rhs.getTypeParams().size() == 0) return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); + if(!(lhs instanceof PlaceholderType) && (rhs instanceof PlaceholderType)) + return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); + return Optional.empty(); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java index 0428a84e..db821905 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java @@ -40,4 +40,10 @@ public class Unifier implements Function { public Type getTarget() { return target; } + + @Override + public String toString() { + return source.toString() + " -> " + target.toString(); + } } + diff --git a/test/unify/StandardUnifyTest.java b/test/unify/StandardUnifyTest.java new file mode 100644 index 00000000..41aff15f --- /dev/null +++ b/test/unify/StandardUnifyTest.java @@ -0,0 +1,74 @@ +package unify; + +import java.util.HashSet; +import java.util.Set; + +import junit.framework.Assert; + +import org.junit.Test; + +import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; +import de.dhbwstuttgart.typeinference.unifynew.MartelliMontanariUnify; +import de.dhbwstuttgart.typinference.unify.model.MPair; +import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; + +public class StandardUnifyTest { + + @Test + public void testUnify() { + IUnify unify = new MartelliMontanariUnify(); + TypeFactory tf = new TypeFactory(); + + /* + * Positive Tests + */ + + Type x = tf.getPlaceholderType("x"); + Type y = tf.getPlaceholderType("y"); + Type z = tf.getPlaceholderType("z"); + Type f = tf.getSimpleType("f", x); + + // {x =. z, f = y} + Set terms = new HashSet(); + terms.add(new MPair(x, z, PairOperator.EQUALSDOT)); + terms.add(new MPair(f, y, PairOperator.EQUALSDOT)); + + System.out.println(unify.unify(terms).get()); + + // {f,x> = f} + Type g = tf.getSimpleType("g", "x"); + Type f1 = tf.getSimpleType("f", g, x); + Type a = tf.getExtendsType(tf.getPlaceholderType("a")); + Type f2 = tf.getSimpleType("f", y, a); + + terms = new HashSet<>(); + terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); + + System.out.println(unify.unify(terms).get()); + + /* + * Negative Tests + */ + + // {f(x) =. x} + f = tf.getSimpleType("f", x); + terms = new HashSet<>(); + terms.add(new MPair(f, x, PairOperator.EQUALSDOT)); + Assert.assertFalse(unify.unify(terms).isPresent()); + + // {f(x) =. f(x,y)} + f1 = tf.getSimpleType("f", "x"); + f2 = tf.getSimpleType("f", "x", "y"); + terms = new HashSet<>(); + terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); + Assert.assertFalse(unify.unify(terms).isPresent()); + + // {f(x) =. g(x)} + f1 = tf.getSimpleType("f", "x"); + f2 = tf.getSimpleType("g", "x"); + terms = new HashSet<>(); + terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); + Assert.assertFalse(unify.unify(terms).isPresent()); + } +} From 3ff45a01de29fea5652384e64c8c1293cdb9b3e8 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 26 Dec 2015 16:22:35 +0100 Subject: [PATCH 03/11] fixed cartesian products in step 4 --- .../typeinference/unifynew/Unify.java | 54 ++++++++----------- test/unify/UnifyTest.java | 8 +-- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index d8354f53..64313fb7 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -47,14 +47,13 @@ public class Unify { /* * Step 4: Create possible typings + * + * "Manche Autoren identifizieren die Paare (a, (b,c)) und ((a,b),c) + * mit dem geordneten Tripel (a,b,c), wodurch das kartesische Produkt auch assoziativ wird." - Wikipedia */ - // Sets that originate from pair pattern matching - // Sets of the "second level" - List>> pairSetsSet = calculatePairSets(eq2s, fc); - - // The sets of the "first level" - Set> sets = new HashSet>(); + // All Sets + List> sets = new ArrayList>(); if(eq1s.size() != 0) sets.add(eq1s); // Add Eq1' @@ -67,26 +66,23 @@ public class Unify { if(bufferSet.size() != 0) sets.add(bufferSet); + // Sets that originate from pair pattern matching + // Sets of the "second level" + sets.addAll(calculatePairSets(eq2s, fc)); + + /* Up to here, no cartesian products are calculated. * Around here, filters for pairs and sets can be applied */ ISetOperations setOps = new GuavaSetOperations(); - // Calculate the inner cartesian products - // Cartesian products of the second level - - // AddAll -> nur add - for(List> pairSets : pairSetsSet) // Prüfen ob addAll stimmt oder ob hier eigentlich nur 1 set sein sollte - sets.add(setOps.cartesianProduct(pairSets).stream().map(x -> new HashSet<>(x)).collect(Collectors.toSet())); - - System.out.println(sets); - - // Calculate the outer cartesian products - // Cartesian products of the first level - Set> eqsSet = setOps.cartesianProduct(new ArrayList<>(sets)); + // Calculate the cartesian products - System.out.println(eqsSet); + Set> result = setOps.cartesianProduct(sets).stream().map(x -> new HashSet(x)).collect(Collectors.toCollection(HashSet::new)); + + System.out.println(result); + /* * Step 5: Substitution */ @@ -196,11 +192,8 @@ public class Unify { } - protected List>> calculatePairSets(Set eq2s, IFiniteClosure fc) { - List>> result = new ArrayList>>(); - for(int i = 0; i < 8; i++) - result.add(new ArrayList>()); - + protected List> calculatePairSets(Set eq2s, IFiniteClosure fc) { + List> result = new ArrayList>(); for(MPair pair : eq2s) { @@ -223,15 +216,14 @@ public class Unify { Set set = new HashSet<>(); for(Type theta : fc.smArg(rhsType)) set.add(new MPair(lhsType, theta, PairOperator.EQUALSDOT)); - - result.get(2).add(set); + result.add(set); } // Case 4: (a <.? Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) { Set set = new HashSet<>(); set.add(new MPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT)); - result.get(3).add(set); + result.add(set); } // Case 5: (Theta <. a) @@ -239,7 +231,7 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.greater(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(4).add(set); + result.add(set); } // Case 6: (? ext Theta <.? a) @@ -247,7 +239,7 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(5).add(set); + result.add(set); } // Case 7: (? sup Theta <.? a) @@ -260,10 +252,10 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(7).add(set); + result.add(set); } } - return result.stream().filter(x -> !x.isEmpty()).collect(Collectors.toList()); + return result; } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 38d5a1b9..bab837df 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -18,7 +18,7 @@ public class UnifyTest extends Unify { FiniteClosureBuilder fcb = new FiniteClosureBuilder(); Set eq = new HashSet(); - fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object")); + //fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object")); fcb.add(tf.getSimpleType("Integer"), tf.getSimpleType("Number")); fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number")); @@ -26,15 +26,15 @@ public class UnifyTest extends Unify { // Vector <. Vector // Vector - // A <. Number + // A <. Integer // Double <. B // B <. Object eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Number"), PairOperator.SMALLERDOT)); + eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); + //eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); this.unify(eq, fc); From 8eecda2a8ff557a34b17064005b799b2bb770594 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 26 Dec 2015 16:52:18 +0100 Subject: [PATCH 04/11] subst rule --- .../typeinference/unifynew/RuleSet.java | 42 +++++++++++++++++-- .../typeinference/unifynew/Unify.java | 12 +++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 1f5cd265..6352aa07 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -2,9 +2,12 @@ package de.dhbwstuttgart.typeinference.unifynew; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; +import java.util.Queue; import java.util.Set; +import java.util.stream.Collectors; import junit.framework.Assert; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; @@ -516,9 +519,42 @@ public class RuleSet implements IRuleSet{ } @Override - public Set subst(Set pair) { - // TODO Auto-generated method stub - return null; + public Set subst(Set pairs) { + HashSet allTypes = new HashSet<>(); + pairs.forEach(x -> { allTypes.add(x.getLhsType()); allTypes.add(x.getRhsType()); }); + + Queue result = new LinkedList(pairs); + + for(int i = 0; i < result.size(); i++) { + MPair pair = result.poll(); + Type lhsType; + Type rhsType; + if(pair.getPairOp() == PairOperator.EQUALSDOT + && ((lhsType = pair.getLhsType()) instanceof PlaceholderType) + && !((rhsType = pair.getRhsType()) instanceof PlaceholderType) + && occursInSet(lhsType, allTypes) + && !occurs(lhsType, rhsType)) { + Unifier uni = new Unifier(lhsType, rhsType); + result = result.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); + } + result.add(pair); + } + + return new HashSet<>(result); + } + + private boolean occursInSet(Type t, Set types) { + int origSize = types.size(); + types.add(t); + return types.size() == origSize; + } + + private boolean occurs(Type t1, Type t2) { + TypeParams t2Params = t2.getTypeParams(); + for(Type t2Param : t2Params) + if(t1.equals(t2Param) || occurs(t1, t2Param)) + return true; + return false; } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 64313fb7..3626fd05 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -88,11 +88,21 @@ public class Unify { */ + /* + * TODO + * Im Paper wird Eq'' genannt, es wird also von einer Menge in einer Menge in einer Menge ausgegangen. + * Durch das flache Kartesische Produkt gibt es hier aber nur Mengen in Mengen. + * Richtig so? + */ + + + + /* * Step 6: a) Restart for pairs where subst was applied * b) Union over everything */ - + /* * Step 7: Filter result for solved pairs */ From 039dd3b3f495011ce248c13e797fac9d29752dc4 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 26 Dec 2015 18:49:11 +0100 Subject: [PATCH 05/11] step 5 and 6 implemented --- .../unify/interfaces/IRuleSet.java | 2 +- .../typeinference/unifynew/RuleSet.java | 30 ++++++++++------- .../typeinference/unifynew/Unify.java | 32 ++++++++++++------- test/unify/UnifyTest.java | 2 +- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index 02611fd9..c997b4ff 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -27,5 +27,5 @@ public interface IRuleSet { public Optional adaptExt(MPair pair); public Optional adaptSup(MPair pair); - public Set subst(Set pair); + public Optional> subst(Set pair); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 6352aa07..c401be4a 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.typeinference.unifynew; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -519,12 +520,24 @@ public class RuleSet implements IRuleSet{ } @Override - public Set subst(Set pairs) { - HashSet allTypes = new HashSet<>(); - pairs.forEach(x -> { allTypes.add(x.getLhsType()); allTypes.add(x.getRhsType()); }); + public Optional> subst(Set pairs) { + HashMap typeMap = new HashMap<>(); + + for(MPair pair : pairs) { + Type t1 = pair.getLhsType(); + Type t2 = pair.getRhsType(); + if(!typeMap.containsKey(t1)) + typeMap.put(t1, 0); + if(!typeMap.containsKey(t2)) + typeMap.put(t2, 0); + typeMap.put(t1, typeMap.get(t1)+1); + typeMap.put(t2, typeMap.get(t2)+1); + } Queue result = new LinkedList(pairs); + boolean applied = false; + for(int i = 0; i < result.size(); i++) { MPair pair = result.poll(); Type lhsType; @@ -532,21 +545,16 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() == PairOperator.EQUALSDOT && ((lhsType = pair.getLhsType()) instanceof PlaceholderType) && !((rhsType = pair.getRhsType()) instanceof PlaceholderType) - && occursInSet(lhsType, allTypes) + && typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair. && !occurs(lhsType, rhsType)) { Unifier uni = new Unifier(lhsType, rhsType); result = result.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); + applied = true; } result.add(pair); } - return new HashSet<>(result); - } - - private boolean occursInSet(Type t, Set types) { - int origSize = types.size(); - types.add(t); - return types.size() == origSize; + return applied ? Optional.of(new HashSet<>(result)) : Optional.empty(); } private boolean occurs(Type t1, Type t2) { diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 3626fd05..49efd39d 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -30,7 +30,7 @@ import de.dhbwstuttgart.typinference.unify.model.Type; */ public class Unify { - public Menge> unify(Set eq, IFiniteClosure fc) { + public Set> unify(Set eq, IFiniteClosure fc) { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ @@ -77,37 +77,47 @@ public class Unify { ISetOperations setOps = new GuavaSetOperations(); // Calculate the cartesian products - - - Set> result = setOps.cartesianProduct(sets).stream().map(x -> new HashSet(x)).collect(Collectors.toCollection(HashSet::new)); - - System.out.println(result); + Set> result = setOps.cartesianProduct(sets).stream() + .map(x -> new HashSet(x)).collect(Collectors.toCollection(HashSet::new)); + //System.out.println(result); /* * Step 5: Substitution */ - /* * TODO * Im Paper wird Eq'' genannt, es wird also von einer Menge in einer Menge in einer Menge ausgegangen. * Durch das flache Kartesische Produkt gibt es hier aber nur Mengen in Mengen. * Richtig so? */ - + IRuleSet rules = new RuleSet(fc); + Set> changed = new HashSet<>(); + Set> unchanged = new HashSet<>(); + + for(Set eqss : result) { + Optional> newEqss = rules.subst(eqss); + if(newEqss.isPresent()) + changed.add(newEqss.get()); + else + unchanged.add(eqss); + } /* - * Step 6: a) Restart for pairs where subst was applied - * b) Union over everything + * Step 6 a) Restart for pairs where subst was applied + * b) Build the union over everything */ + for(Set eqss : changed) + unchanged.addAll(this.unify(eqss, fc)); + /* * Step 7: Filter result for solved pairs */ - return null; + return unchanged; } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index bab837df..356f6029 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -36,7 +36,7 @@ public class UnifyTest extends Unify { eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); - this.unify(eq, fc); + System.out.println(this.unify(eq, fc)); } From 98cbe9b3890f695a20e9443632020527fc05f626 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 27 Dec 2015 00:29:23 +0100 Subject: [PATCH 06/11] package rename + Mapping Klasse --- .../unify/interfaces/IFiniteClosure.java | 10 +- .../unify/interfaces/IRuleSet.java | 2 +- .../unify/interfaces/ITypeMapper.java | 7 - .../unify/interfaces/IUnify.java | 2 +- .../unify/model/ExtendsType.java | 2 +- .../unify/model/FiniteClosure.java | 4 +- .../unify/model/MPair.java | 2 +- .../unify/model/Node.java | 2 +- .../unify/model/PlaceholderType.java | 2 +- .../unify/model/SimpleType.java | 2 +- .../unify/model/SuperType.java | 2 +- .../unify/model/Type.java | 2 +- .../unify/model/TypeParams.java | 2 +- .../typeinference/unifynew/Mapping.java | 130 ++++++++++-------- .../unifynew/MartelliMontanariUnify.java | 10 +- .../typeinference/unifynew/RuleSet.java | 16 +-- .../typeinference/unifynew/Unifier.java | 6 +- .../typeinference/unifynew/Unify.java | 12 +- test/unify/FiniteClosureBuilder.java | 8 +- test/unify/FiniteClosureTest.java | 4 +- test/unify/RuleSetTest.java | 10 +- test/unify/StandardUnifyTest.java | 6 +- test/unify/TypeFactory.java | 10 +- test/unify/UnifyTest.java | 4 +- 24 files changed, 136 insertions(+), 121 deletions(-) delete mode 100644 src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/ExtendsType.java (91%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/FiniteClosure.java (94%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/MPair.java (92%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/Node.java (91%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/PlaceholderType.java (89%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/SimpleType.java (91%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/SuperType.java (91%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/Type.java (90%) rename src/de/dhbwstuttgart/{typinference => typeinference}/unify/model/TypeParams.java (92%) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 0601d70f..3e8d8d7d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -3,11 +3,11 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; import java.util.Optional; import java.util.Set; -import de.dhbwstuttgart.typinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.SimpleType; -import de.dhbwstuttgart.typinference.unify.model.SuperType; -import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.SimpleType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.Type; public interface IFiniteClosure { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index c997b4ff..d2a95f3d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -3,8 +3,8 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; import java.util.Optional; import java.util.Set; +import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unifynew.Unifier; -import de.dhbwstuttgart.typinference.unify.model.MPair; public interface IRuleSet { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java deleted file mode 100644 index 85efc20a..00000000 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify.interfaces; - -import de.dhbwstuttgart.typeinference.Menge; - -public interface ITypeMapper { - -} diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index 1f73b550..05b7e8da 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -3,8 +3,8 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; import java.util.Optional; import java.util.Set; +import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unifynew.Unifier; -import de.dhbwstuttgart.typinference.unify.model.MPair; /** * Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari, Ruzicka-Privara or Suciu) diff --git a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java similarity index 91% rename from src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java rename to src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index 2b0facaf..4eceb240 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java similarity index 94% rename from src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java rename to src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index ae36f465..9899bef4 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.HashMap; import java.util.HashSet; @@ -6,7 +6,7 @@ import java.util.Optional; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class FiniteClosure implements IFiniteClosure { diff --git a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java similarity index 92% rename from src/de/dhbwstuttgart/typinference/unify/model/MPair.java rename to src/de/dhbwstuttgart/typeinference/unify/model/MPair.java index 9a76f2a9..6b740c46 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; public class MPair { diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java similarity index 91% rename from src/de/dhbwstuttgart/typinference/unify/model/Node.java rename to src/de/dhbwstuttgart/typeinference/unify/model/Node.java index e42a2608..7010ec32 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.HashSet; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java similarity index 89% rename from src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java rename to src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 7b7503ee..12ecac01 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java similarity index 91% rename from src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java rename to src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java index 8e9e8a3f..301d5d91 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java similarity index 91% rename from src/de/dhbwstuttgart/typinference/unify/model/SuperType.java rename to src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 15c432d2..5cc8ad63 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Type.java b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java similarity index 90% rename from src/de/dhbwstuttgart/typinference/unify/model/Type.java rename to src/de/dhbwstuttgart/typeinference/unify/model/Type.java index a9e94862..137541e6 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java similarity index 92% rename from src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java rename to src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index d723395d..d56a80b0 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typinference.unify.model; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.Arrays; import java.util.Iterator; diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java index d21c5b60..97c2823f 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java @@ -2,68 +2,90 @@ package de.dhbwstuttgart.typeinference.unifynew; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; -import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; -import de.dhbwstuttgart.syntaxtree.type.FunN; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; -import de.dhbwstuttgart.syntaxtree.type.Type; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; - -/** - * First three bits indicate the meta type: - * 000b = 0 = Simpletype - * 001b = 1 = Extends - * 010b = 2 = Super - * 011b = 3 = Type Placeholder - * 100b = 4 = Function - * - * @author DH10STF - * - */ public class Mapping { - private static final int ST_MASK = 0; - private static final int EXTENDS_MASK = 536870912; - private static final int SUPER_MASK = 1073741824; - private static final int TPH_MASK = 1610612736; - private static final int FUN_MASK = -2147483648; + private HashMap backwardMap = new HashMap<>(); + private HashMap forwardMap = new HashMap<>(); + private Set irreversible = new HashSet<>(); - private static HashMap mapping; - - public Set createMapping(Iterable types) { - mapping = new HashMap<>(); - - Iterator iterator = types.iterator(); - while(iterator.hasNext() && mapping.size() <= 536870911) - createMapping(iterator.next()); - - return mapping.keySet(); + public Mapping(Set types) { + for(de.dhbwstuttgart.syntaxtree.type.Type t : types) { + // TODO + } } - private void createMapping(Type type) { - /*if(type instanceof RefType) { - Set params = ((RefType) type).get_ParaList(); - params.stream().forEach(x -> createMapping(x)); + public de.dhbwstuttgart.typeinference.unify.model.Type map(de.dhbwstuttgart.syntaxtree.type.Type type) { + return forwardMap.get(type); + } + + public de.dhbwstuttgart.typeinference.unify.model.MPair map(de.dhbwstuttgart.typeinference.Pair pair) { + return new de.dhbwstuttgart.typeinference.unify.model.MPair(forwardMap.get(pair.TA1), forwardMap.get(pair.TA2), mapOp(pair.GetOperator())); + } + + public Set mapTypeSet(Set types) { + return types.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); + } + + public Set mapPairSet(Set pairs) { + return pairs.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); + } + + public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.Type type) { + return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty(); + } + + public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.MPair mpair) { + de.dhbwstuttgart.typeinference.unify.model.Type lhs = mpair.getLhsType(); + de.dhbwstuttgart.typeinference.unify.model.Type rhs = mpair.getRhsType(); + + if(irreversible.contains(lhs) || irreversible.contains(rhs)) + return Optional.empty(); + return Optional.of(new de.dhbwstuttgart.typeinference.Pair(backwardMap.get(lhs), backwardMap.get(rhs), unmapOp(mpair.getPairOp()))); + } + + public Optional> unmapTypeSet(Set types) { + Set result = types.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new)); + return result.size() == types.size() ? Optional.of(result) : Optional.empty(); + } + + public Optional> unmapPairSet(Set pairs) { + Set result = pairs.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new)); + return result.size() == pairs.size() ? Optional.of(result) : Optional.empty(); + } + + private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) { + /* + * TODO + * Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt? + */ + + switch(op) { + case Equal: + return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; + case Smaller: + return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER; + case SmallerExtends: + return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT; + default: + return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; + } + } + + private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) { + switch(op) { + case EQUALS: + return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal; + case SMALLER: + return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller; + case SMALLERDOT: + return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends; + default: + return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal; } - */ - int typeId = mapping.size(); - - if(type instanceof RefType) - typeId |= ST_MASK; - else if(type instanceof SuperWildcardType) - typeId |= SUPER_MASK; - else if(type instanceof ExtendsWildcardType) - typeId |= EXTENDS_MASK; - else if(type instanceof TypePlaceholder) - typeId |= TPH_MASK; - else if(type instanceof FunN) - typeId |= FUN_MASK; - - //mapping.put(new MType()) } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 0f626010..12666fe4 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -9,11 +9,11 @@ import java.util.Stack; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.Type; -import de.dhbwstuttgart.typinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; /** * Implementation of the Martelli-Montanari unification algorithm. diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index c401be4a..a045a9d2 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -13,14 +13,14 @@ import java.util.stream.Collectors; import junit.framework.Assert; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; -import de.dhbwstuttgart.typinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.SimpleType; -import de.dhbwstuttgart.typinference.unify.model.SuperType; -import de.dhbwstuttgart.typinference.unify.model.Type; -import de.dhbwstuttgart.typinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.SimpleType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class RuleSet implements IRuleSet{ diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java index db821905..82e8d31e 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java @@ -4,9 +4,9 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.Type; -import de.dhbwstuttgart.typinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; public class Unifier implements Function { private Type source; diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 49efd39d..800ce767 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -16,12 +16,12 @@ import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; -import de.dhbwstuttgart.typinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.SuperType; -import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; /** diff --git a/test/unify/FiniteClosureBuilder.java b/test/unify/FiniteClosureBuilder.java index 815d637d..20552468 100644 --- a/test/unify/FiniteClosureBuilder.java +++ b/test/unify/FiniteClosureBuilder.java @@ -4,10 +4,10 @@ import java.util.HashSet; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class FiniteClosureBuilder { diff --git a/test/unify/FiniteClosureTest.java b/test/unify/FiniteClosureTest.java index d3744abe..04017908 100644 --- a/test/unify/FiniteClosureTest.java +++ b/test/unify/FiniteClosureTest.java @@ -6,8 +6,8 @@ import java.util.Set; import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.Type; public class FiniteClosureTest { diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index fa08b1e7..1fe1359d 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -9,12 +9,12 @@ import junit.framework.Assert; import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.SimpleType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unifynew.RuleSet; -import de.dhbwstuttgart.typinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typinference.unify.model.SimpleType; -import de.dhbwstuttgart.typinference.unify.model.SuperType; public class RuleSetTest { diff --git a/test/unify/StandardUnifyTest.java b/test/unify/StandardUnifyTest.java index 41aff15f..33b4bde9 100644 --- a/test/unify/StandardUnifyTest.java +++ b/test/unify/StandardUnifyTest.java @@ -8,10 +8,10 @@ import junit.framework.Assert; import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unifynew.MartelliMontanariUnify; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.Type; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; public class StandardUnifyTest { diff --git a/test/unify/TypeFactory.java b/test/unify/TypeFactory.java index 46b6f7bf..7fb896ae 100644 --- a/test/unify/TypeFactory.java +++ b/test/unify/TypeFactory.java @@ -3,11 +3,11 @@ package unify; import java.util.Arrays; import java.util.stream.Collectors; -import de.dhbwstuttgart.typinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.SimpleType; -import de.dhbwstuttgart.typinference.unify.model.SuperType; -import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.SimpleType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.Type; public class TypeFactory { diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 356f6029..b1580d75 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -6,9 +6,9 @@ import java.util.Set; import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unifynew.Unify; -import de.dhbwstuttgart.typinference.unify.model.MPair; -import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; public class UnifyTest extends Unify { From bdd018d922e0f11933ad1e99e91102da6f63fd9b Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 27 Dec 2015 15:01:59 +0100 Subject: [PATCH 07/11] =?UTF-8?q?regel=204=20f=C3=BCr=20paare=20(a=20<.=20?= =?UTF-8?q?Theta')=20(funktioniert=20zu=2099=20Prozent=20noch=20nicht=20ri?= =?UTF-8?q?chtig)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unify/interfaces/IFiniteClosure.java | 1 + .../unify/interfaces/IUnify.java | 3 ++ .../unify/model/FiniteClosure.java | 8 +++++ .../unifynew/MartelliMontanariUnify.java | 8 ++++- .../typeinference/unifynew/Unify.java | 36 ++++++++++++++++++- test/unify/UnifyTest.java | 6 ++-- 6 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 3e8d8d7d..62b65b27 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -50,4 +50,5 @@ public interface IFiniteClosure { public Set smArg(PlaceholderType type); public Optional getGenericType(String typeName); + public Set getAllTypes(String typeName); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index 05b7e8da..d3c8b6d7 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -4,6 +4,7 @@ import java.util.Optional; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unifynew.Unifier; /** @@ -12,4 +13,6 @@ import de.dhbwstuttgart.typeinference.unifynew.Unifier; */ public interface IUnify { public Optional> unify(Set terms); + + public Optional> unify(Type t1, Type t2); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 9899bef4..e618482d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; @@ -205,4 +206,11 @@ public class FiniteClosure implements IFiniteClosure { return Optional.empty(); } + + @Override + public Set getAllTypes(String typeName) { + if(!strInheritanceGraph.containsKey(typeName)) + return new HashSet<>(); + return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new)); + } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 12666fe4..900af874 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -5,7 +5,6 @@ import java.util.LinkedList; import java.util.Optional; import java.util.Queue; import java.util.Set; -import java.util.Stack; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; @@ -21,6 +20,13 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; */ public class MartelliMontanariUnify implements IUnify { + @Override + public Optional> unify(Type t1, Type t2) { + Set terms = new HashSet(); + terms.add(new MPair(t1, t2, PairOperator.EQUALSDOT)); + return unify(terms); + } + @Override public Optional> unify(Set terms) { Queue termsQ = new LinkedList<>(terms); diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 800ce767..94ee1f0d 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -16,6 +16,7 @@ import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; +import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; @@ -223,7 +224,36 @@ public class Unify { // Case 1: (a <. Theta') if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { - // TODO + IUnify unify = new MartelliMontanariUnify(); + + Set possibleCs = fc.getAllTypes(rhsType.getName()); + Set possibleThetas = possibleCs.stream() + .flatMap(x -> fc.smaller(x).stream()) + .collect(Collectors.toCollection(HashSet::new)); + Set possibleThetaPrimes = possibleThetas.stream() + .flatMap(x -> getAllInstantiations(x).stream()) + .collect(Collectors.toCollection(HashSet::new)); + + Set unifiers = possibleThetaPrimes.stream() + .map(x -> unify.unify(x, rhsType)) + .filter(x -> x.isPresent()) + .flatMap(x -> x.get().stream()) + .map(x -> new Unifier(x.getLhsType(), x.getRhsType())) + .collect(Collectors.toCollection(HashSet::new)); + + Set thetas = new HashSet<>(); + + for(Unifier sigma : unifiers) + for(Type thetaQuer : possibleThetas) + thetas.addAll(fc.smaller(sigma.apply(thetaQuer))); + + Set resultSet = new HashSet<>(); + thetas.forEach(x -> resultSet.add(new MPair(lhsType, x, PairOperator.EQUALSDOT))); + unifiers.forEach(x -> resultSet.add(new MPair(x.getSource(), x.getTarget(), PairOperator.EQUALSDOT))); + + result.add(resultSet); + // TODO Speedup - Potenzial durch auschließen unmöglicher kombinationen (z.B. wenn in theta' eine Variable festgelegt ist) + // TODO speedup durch pipelining mit streams } // Case 2: (a <.? ? ext Theta') @@ -278,4 +308,8 @@ public class Unify { return result; } + + private Set getAllInstantiations(Type t) { + return new HashSet<>(); + } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index b1580d75..3c27ea0d 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -27,13 +27,15 @@ public class UnifyTest extends Unify { // Vector <. Vector // Vector // A <. Integer + // Number <. A // Double <. B // B <. Object eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); + //eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); + eq.add(new MPair(tf.getSimpleType("Number"), tf.getPlaceholderType("A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); + //eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); System.out.println(this.unify(eq, fc)); From 27acee33853ef9feb4b5dfdf06dc121cfd9ade09 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 13 Mar 2016 16:00:00 +0100 Subject: [PATCH 08/11] standard martelli montanari unifiy fixed --- .../unify/interfaces/IUnify.java | 13 ++- .../unify/model/PlaceholderType.java | 5 +- .../typeinference/unify/model/SimpleType.java | 3 - .../typeinference/unify/model/TypeParams.java | 7 ++ .../typeinference/unifynew/Mapping.java | 1 - .../unifynew/MartelliMontanariUnify.java | 95 ++++++++++--------- .../typeinference/unifynew/RuleSet.java | 14 +-- .../typeinference/unifynew/Unifier.java | 38 +++++--- .../typeinference/unifynew/Unify.java | 18 +++- test/unify/StandardUnifyTest.java | 30 +++--- test/unify/UnifyTest.java | 2 +- 11 files changed, 126 insertions(+), 100 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index d3c8b6d7..a573b5d2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -1,18 +1,23 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; +import java.util.Arrays; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; -import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unifynew.Unifier; /** - * Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari, Ruzicka-Privara or Suciu) + * Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari) * @author Florian Steurer */ public interface IUnify { - public Optional> unify(Set terms); - public Optional> unify(Type t1, Type t2); + public Optional unify(Set terms); + + default public Optional unify(Type... terms) { + return unify(Arrays.stream(terms).collect(Collectors.toSet())); + } + } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 12ecac01..0514517d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -36,9 +36,8 @@ public final class PlaceholderType extends Type{ @Override public Type apply(Unifier unif) { - if(this.equals(unif.getSource())) - return unif.getTarget(); - + if(unif.hasSubstitute(this)) + return unif.getSubstitute(this); return this; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java index 301d5d91..27581016 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java @@ -44,9 +44,6 @@ public final class SimpleType extends Type { @Override public Type apply(Unifier unif) { - if(this.equals(unif.getSource())) - return unif.getTarget(); - return new SimpleType(typeName, typeParams.apply(unif)); } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index d56a80b0..3fdd8679 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -52,6 +52,13 @@ public final class TypeParams implements Iterable{ public Type get(int i) { return typeParams[i]; } + + public TypeParams set(Type t, int idx) { + Type[] newparams = Arrays.copyOf(typeParams, typeParams.length); + newparams[idx] = t; + return new TypeParams(newparams); + } + @Override public Iterator iterator() { diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java index 97c2823f..c8996dc8 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java @@ -14,7 +14,6 @@ public class Mapping { public Mapping(Set types) { for(de.dhbwstuttgart.syntaxtree.type.Type t : types) { - // TODO } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 900af874..101f861a 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -1,18 +1,20 @@ package de.dhbwstuttgart.typeinference.unifynew; +import java.util.AbstractMap; +import java.util.ArrayList; import java.util.HashSet; -import java.util.LinkedList; +import java.util.Iterator; +import java.util.Map.Entry; import java.util.Optional; -import java.util.Queue; import java.util.Set; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; -import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; /** * Implementation of the Martelli-Montanari unification algorithm. @@ -21,23 +23,29 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class MartelliMontanariUnify implements IUnify { @Override - public Optional> unify(Type t1, Type t2) { - Set terms = new HashSet(); - terms.add(new MPair(t1, t2, PairOperator.EQUALSDOT)); - return unify(terms); - } - - @Override - public Optional> unify(Set terms) { - Queue termsQ = new LinkedList<>(terms); - Set result = new HashSet<>(); + public Optional unify(Set terms) { + if(terms.size() < 2) + return Optional.of(Unifier.IDENTITY); + ArrayList termsQ = new ArrayList(); + Iterator iter = terms.iterator(); + Type prev = iter.next(); + while(iter.hasNext()) { + Type next = iter.next(); + termsQ.add(new MPair(prev, next, PairOperator.EQUALS)); + prev = next; + } - while(!termsQ.isEmpty()) { - MPair pair = termsQ.poll(); + Unifier mgu = Unifier.IDENTITY; + + int idx = 0; + while(idx < termsQ.size()) { + MPair pair = termsQ.get(idx); - if(delete(pair)) + if(delete(pair)) { + termsQ.remove(idx); continue; + } Optional> optSet = decompose(pair); @@ -46,6 +54,7 @@ public class MartelliMontanariUnify implements IUnify { if(optSet.isPresent()) { termsQ.addAll(optSet.get()); + idx = idx+1 == termsQ.size() ? 0 : idx+1; continue; } @@ -53,25 +62,27 @@ public class MartelliMontanariUnify implements IUnify { if(optPair.isPresent()) { termsQ.add(optPair.get()); + idx = idx+1 == termsQ.size() ? 0 : idx+1; continue; } - Optional optUni = eliminate(pair); - - if(optUni.isPresent()) { - Unifier uni = optUni.get(); - termsQ = termsQ.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); - result = result.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new)); - result.add(pair); - continue; - } - - // TODO occurs check notwendig? if(!check(pair)) // Occurs-Check return Optional.empty(); + + Optional> optUni = eliminate(pair); + + if(optUni.isPresent()) { + Entry substitution = optUni.get(); + mgu.Add(substitution.getKey(), substitution.getValue()); + termsQ = termsQ.stream().map(mgu::apply).collect(Collectors.toCollection(ArrayList::new)); + idx = idx+1 == termsQ.size() ? 0 : idx+1; + continue; + } + + idx++; } - return Optional.of(result); + return Optional.of(mgu); } private boolean delete(MPair pair) { @@ -103,28 +114,12 @@ public class MartelliMontanariUnify implements IUnify { Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); - if(lhs.getTypeParams().size() != 0 && rhs.getTypeParams().size() == 0) - return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); - if(!(lhs instanceof PlaceholderType) && (rhs instanceof PlaceholderType)) return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); return Optional.empty(); } - private Optional eliminate(MPair pair) { - Type rhs = pair.getRhsType(); - Type lhs = pair.getLhsType(); - - TypeParams rhsTypeParams = rhs.getTypeParams(); - - for(Type t : rhsTypeParams) - if(lhs.equals(t)) - return Optional.empty(); - - return Optional.of(new Unifier(lhs, rhs)); - } - private boolean check(MPair pair) { Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); @@ -137,4 +132,16 @@ public class MartelliMontanariUnify implements IUnify { return true; } + + private Optional> eliminate(MPair pair) { + Type rhs = pair.getRhsType(); + Type lhs = pair.getLhsType(); + + // TODO only apply when lhs is element of vars(termsQ)? + + if(!(lhs instanceof PlaceholderType)) + return Optional.empty(); + + return Optional.of(new AbstractMap.SimpleImmutableEntry((PlaceholderType) lhs, rhs)); + } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index a045a9d2..4c14cdaf 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -346,9 +346,9 @@ public class RuleSet implements IRuleSet{ TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); - Unifier unif = new Unifier(typeDgenParams.get(0), typeDParams.get(0)); + Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0)); for(int i = 1; i < typeDParams.size(); i++) - unif.andThen(new Unifier(typeDgenParams.get(i), typeDParams.get(i))); + unif.andThen(new Unifier((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i))); return Optional.of(new MPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT)); } @@ -392,9 +392,9 @@ public class RuleSet implements IRuleSet{ TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); - Unifier unif = new Unifier(typeDgenParams.get(0), typeDParams.get(0)); + Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0)); for(int i = 1; i < typeDParams.size(); i++) - unif.andThen(new Unifier(typeDgenParams.get(i), typeDParams.get(i))); + unif.andThen(new Unifier((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i))); return Optional.of(new MPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC)); } @@ -444,9 +444,9 @@ public class RuleSet implements IRuleSet{ TypeParams typeDParams = typeSupD.getTypeParams(); TypeParams typeSupDsgenParams = typeSupDgen.getTypeParams(); - Unifier unif = new Unifier(typeSupDsgenParams.get(0), typeDParams.get(0)); + Unifier unif = new Unifier((PlaceholderType) typeSupDsgenParams.get(0), typeDParams.get(0)); for(int i = 1; i < typeDParams.size(); i++) - unif.andThen(new Unifier(typeSupDsgenParams.get(i), typeDParams.get(i))); + unif.andThen(new Unifier((PlaceholderType) typeSupDsgenParams.get(i), typeDParams.get(i))); return Optional.of(new MPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC)); } @@ -547,7 +547,7 @@ public class RuleSet implements IRuleSet{ && !((rhsType = pair.getRhsType()) instanceof PlaceholderType) && typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair. && !occurs(lhsType, rhsType)) { - Unifier uni = new Unifier(lhsType, rhsType); + Unifier uni = new Unifier((PlaceholderType) lhsType, rhsType); result = result.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); applied = true; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java index 82e8d31e..8979575d 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java @@ -1,20 +1,20 @@ package de.dhbwstuttgart.typeinference.unifynew; -import java.util.Set; +import java.util.HashMap; +import java.util.Map.Entry; import java.util.function.Function; -import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.Type; -import de.dhbwstuttgart.typeinference.unify.model.TypeParams; public class Unifier implements Function { - private Type source; - private Type target; + private HashMap substitutions = new HashMap<>(); - public Unifier(Type source, Type target) { - this.source = source; - this.target = target; + public static Unifier IDENTITY = new Unifier(); + + public Unifier(PlaceholderType source, Type target) { + substitutions.put(source, target); } /** @@ -23,6 +23,10 @@ public class Unifier implements Function { public Unifier() { } + + public void Add(PlaceholderType source, Type target) { + substitutions.put(source, target); + } @Override public Type apply(Type t) { @@ -33,17 +37,23 @@ public class Unifier implements Function { return new MPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp()); } - public Type getSource() { - return source; + public boolean hasSubstitute(PlaceholderType t) { + return substitutions.containsKey(t); } - public Type getTarget() { - return target; + public Type getSubstitute(Type t) { + return substitutions.get(t); } - + @Override public String toString() { - return source.toString() + " -> " + target.toString(); + String result = "{ "; + for(Entry entry : substitutions.entrySet()) + result += "(" + entry.getKey() + " -> " + entry.getValue() + "), "; + if(!substitutions.isEmpty()) + result = result.substring(0, result.length()-2); + result += " }"; + return result; } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 94ee1f0d..65193117 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -23,6 +23,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; /** @@ -231,10 +232,12 @@ public class Unify { .flatMap(x -> fc.smaller(x).stream()) .collect(Collectors.toCollection(HashSet::new)); Set possibleThetaPrimes = possibleThetas.stream() - .flatMap(x -> getAllInstantiations(x).stream()) + .flatMap(x -> getAllInstantiations(x, fc).stream()) .collect(Collectors.toCollection(HashSet::new)); - Set unifiers = possibleThetaPrimes.stream() + // TODO + + /*Set unifiers = possibleThetaPrimes.stream() .map(x -> unify.unify(x, rhsType)) .filter(x -> x.isPresent()) .flatMap(x -> x.get().stream()) @@ -254,6 +257,8 @@ public class Unify { result.add(resultSet); // TODO Speedup - Potenzial durch auschließen unmöglicher kombinationen (z.B. wenn in theta' eine Variable festgelegt ist) // TODO speedup durch pipelining mit streams + // TODO speedup teure berechnungen mit proxy rauszögern + */ } // Case 2: (a <.? ? ext Theta') @@ -309,7 +314,12 @@ public class Unify { return result; } - private Set getAllInstantiations(Type t) { - return new HashSet<>(); + private Set getAllInstantiations(Type t, IFiniteClosure fc) { + Set result = new HashSet<>(); + result.add(t); + return result; + + // TODO } + } diff --git a/test/unify/StandardUnifyTest.java b/test/unify/StandardUnifyTest.java index 33b4bde9..e08176ed 100644 --- a/test/unify/StandardUnifyTest.java +++ b/test/unify/StandardUnifyTest.java @@ -26,26 +26,24 @@ public class StandardUnifyTest { Type x = tf.getPlaceholderType("x"); Type y = tf.getPlaceholderType("y"); - Type z = tf.getPlaceholderType("z"); Type f = tf.getSimpleType("f", x); - // {x =. z, f = y} + // {f = y} Set terms = new HashSet(); - terms.add(new MPair(x, z, PairOperator.EQUALSDOT)); - terms.add(new MPair(f, y, PairOperator.EQUALSDOT)); + + System.out.println(unify.unify(f, y).get()); - System.out.println(unify.unify(terms).get()); - - // {f,x> = f} + // TODO ist das ergebnis { (x -> ? extends a), (y -> g) } in der richtigen form oder + // muss es { (x -> ? extends a), (y -> g) } sein? + // {f,x> = f} Type g = tf.getSimpleType("g", "x"); Type f1 = tf.getSimpleType("f", g, x); Type a = tf.getExtendsType(tf.getPlaceholderType("a")); Type f2 = tf.getSimpleType("f", y, a); terms = new HashSet<>(); - terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); - - System.out.println(unify.unify(terms).get()); + + System.out.println(unify.unify(f1, f2).get()); /* * Negative Tests @@ -53,22 +51,16 @@ public class StandardUnifyTest { // {f(x) =. x} f = tf.getSimpleType("f", x); - terms = new HashSet<>(); - terms.add(new MPair(f, x, PairOperator.EQUALSDOT)); - Assert.assertFalse(unify.unify(terms).isPresent()); + Assert.assertFalse(unify.unify(f, x).isPresent()); // {f(x) =. f(x,y)} f1 = tf.getSimpleType("f", "x"); f2 = tf.getSimpleType("f", "x", "y"); - terms = new HashSet<>(); - terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); - Assert.assertFalse(unify.unify(terms).isPresent()); + Assert.assertFalse(unify.unify(f1, f2).isPresent()); // {f(x) =. g(x)} f1 = tf.getSimpleType("f", "x"); f2 = tf.getSimpleType("g", "x"); - terms = new HashSet<>(); - terms.add(new MPair(f1, f2, PairOperator.EQUALSDOT)); - Assert.assertFalse(unify.unify(terms).isPresent()); + Assert.assertFalse(unify.unify(f1, f2).isPresent()); } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 3c27ea0d..eb7f9f5b 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -33,7 +33,7 @@ public class UnifyTest extends Unify { eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("Number"), tf.getPlaceholderType("A"), PairOperator.SMALLERDOT)); + //eq.add(new MPair(tf.getSimpleType("Number"), tf.getPlaceholderType("A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); From 6778160315d001ddeee4bd3c00cccf6a2d7bbf8e Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 13 Mar 2016 23:01:28 +0100 Subject: [PATCH 09/11] various --- .../unify/interfaces/IRuleSet.java | 1 - .../unify/interfaces/IUnify.java | 2 +- .../unify/model/ExtendsType.java | 36 +++++++++++++------ .../unify/model/PlaceholderType.java | 19 +++++----- .../typeinference/unify/model/SimpleType.java | 15 ++++---- .../typeinference/unify/model/SuperType.java | 15 ++++---- .../typeinference/unify/model/Type.java | 7 ++-- .../typeinference/unify/model/TypeParams.java | 16 +++++++-- .../{unifynew => unify/model}/Unifier.java | 6 +--- .../unifynew/MartelliMontanariUnify.java | 1 + .../typeinference/unifynew/RuleSet.java | 30 +++++++--------- .../typeinference/unifynew/Unify.java | 20 +++++++---- test/unify/FiniteClosureTest.java | 7 ++++ test/unify/UnifyTest.java | 3 +- 14 files changed, 103 insertions(+), 75 deletions(-) rename src/de/dhbwstuttgart/typeinference/{unifynew => unify/model}/Unifier.java (80%) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index d2a95f3d..840d7259 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -4,7 +4,6 @@ import java.util.Optional; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.model.MPair; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; public interface IRuleSet { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index a573b5d2..f286e3b6 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -6,7 +6,7 @@ import java.util.Set; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.model.Type; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; /** * Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index 4eceb240..da59af1a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -3,16 +3,30 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; +/** + * An extends wildcard type "? extends T". + */ public final class ExtendsType extends Type { + + /** + * The extended type + */ private Type extendedType; + /** + * Creates a new extends wildcard type. + * @param extendedType The extended type e.g. Integer in "? extends Integer" + */ public ExtendsType(Type extendedType) { super("? extends " + extendedType.getName(), extendedType.getTypeParams()); this.extendedType = extendedType; } + /** + * Gets the type extended by this wildcard e.g. "Integer" for "? extends Integer" + * @return The extended type. + */ public Type getExtendedType() { return extendedType; } @@ -21,21 +35,21 @@ public final class ExtendsType extends Type { public TypeParams getTypeParams() { return extendedType.getTypeParams(); } - - @Override - public String toString() { - return "? extends " + extendedType; - } @Override - public Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - public Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + + @Override + Type apply(Unifier unif) { + return new ExtendsType(extendedType.apply(unif)); + } @Override public int hashCode() { @@ -50,9 +64,9 @@ public final class ExtendsType extends Type { ExtendsType other = (ExtendsType) obj; return other.getExtendedType().equals(extendedType); } - + @Override - public Type apply(Unifier unif) { - return new ExtendsType(extendedType.apply(unif)); + public String toString() { + return "? extends " + extendedType; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 0514517d..e1f1697a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; public final class PlaceholderType extends Type{ @@ -12,12 +11,12 @@ public final class PlaceholderType extends Type{ } @Override - public Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - public Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } @@ -26,6 +25,13 @@ public final class PlaceholderType extends Type{ return typeName.hashCode(); } + @Override + Type apply(Unifier unif) { + if(unif.hasSubstitute(this)) + return unif.getSubstitute(this); + return this; + } + @Override public boolean equals(Object obj) { if(!(obj instanceof PlaceholderType)) @@ -33,11 +39,4 @@ public final class PlaceholderType extends Type{ return ((PlaceholderType) obj).getName().equals(typeName); } - - @Override - public Type apply(Unifier unif) { - if(unif.hasSubstitute(this)) - return unif.getSubstitute(this); - return this; - } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java index 27581016..1283ceb6 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; public final class SimpleType extends Type { public SimpleType(String name, Type... typeParams) { @@ -15,15 +14,20 @@ public final class SimpleType extends Type { } @Override - public Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - public Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + @Override + Type apply(Unifier unif) { + return new SimpleType(typeName, typeParams.apply(unif)); + } + @Override public int hashCode() { return typeName.hashCode(); @@ -41,9 +45,4 @@ public final class SimpleType extends Type { return other.getTypeParams().equals(typeParams); } - - @Override - public Type apply(Unifier unif) { - return new SimpleType(typeName, typeParams.apply(unif)); - } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 5cc8ad63..7090d86a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; public final class SuperType extends Type { @@ -29,15 +28,20 @@ public final class SuperType extends Type { } @Override - public Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - public Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + @Override + Type apply(Unifier unif) { + return new SuperType(superedType.apply(unif)); + } + @Override public int hashCode() { return superedType.hashCode() + 17; @@ -51,9 +55,4 @@ public final class SuperType extends Type { SuperType other = (SuperType) obj; return other.getSuperedType().equals(superedType); } - - @Override - public Type apply(Unifier unif) { - return new SuperType(superedType.apply(unif)); - } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java index 137541e6..52977fbf 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; public abstract class Type { @@ -28,11 +27,11 @@ public abstract class Type { return typeParams; } - public abstract Set smArg(IFiniteClosure fc); + abstract Set smArg(IFiniteClosure fc); - public abstract Set grArg(IFiniteClosure fc); + abstract Set grArg(IFiniteClosure fc); - public abstract Type apply(Unifier unif); + abstract Type apply(Unifier unif); @Override public String toString() { diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index 3fdd8679..806a310a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -3,8 +3,6 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Arrays; import java.util.Iterator; -import de.dhbwstuttgart.typeinference.unifynew.Unifier; - public final class TypeParams implements Iterable{ private final Type[] typeParams; @@ -42,6 +40,20 @@ public final class TypeParams implements Iterable{ return new TypeParams(newParams); } + public boolean occurs(PlaceholderType t) { + for(Type p : typeParams) { + if(p instanceof PlaceholderType) { + if(p.equals(t)) + return true; + } else { + if(p.getTypeParams().occurs(t)) + return true; + } + } + + return false; + } + public boolean contains(Type t) { for(Type t1 : typeParams) if(t1.equals(t1)) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java similarity index 80% rename from src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java rename to src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 8979575d..cb0f52d4 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -1,13 +1,9 @@ -package de.dhbwstuttgart.typeinference.unifynew; +package de.dhbwstuttgart.typeinference.unify.model; import java.util.HashMap; import java.util.Map.Entry; import java.util.function.Function; -import de.dhbwstuttgart.typeinference.unify.model.MPair; -import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typeinference.unify.model.Type; - public class Unifier implements Function { private HashMap substitutions = new HashMap<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 101f861a..6c55d0be 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -15,6 +15,7 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; /** * Implementation of the Martelli-Montanari unification algorithm. diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 4c14cdaf..e41525f5 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -20,6 +20,7 @@ import de.dhbwstuttgart.typeinference.unify.model.SimpleType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class RuleSet implements IRuleSet{ @@ -534,35 +535,28 @@ public class RuleSet implements IRuleSet{ typeMap.put(t2, typeMap.get(t2)+1); } - Queue result = new LinkedList(pairs); + ArrayList result = new ArrayList(pairs); boolean applied = false; for(int i = 0; i < result.size(); i++) { - MPair pair = result.poll(); - Type lhsType; + MPair pair = result.get(i); + PlaceholderType lhsType = null; Type rhsType; - if(pair.getPairOp() == PairOperator.EQUALSDOT - && ((lhsType = pair.getLhsType()) instanceof PlaceholderType) + + if(pair.getPairOp() == PairOperator.EQUALSDOT + && pair.getLhsType() instanceof PlaceholderType) + lhsType = (PlaceholderType) pair.getLhsType(); + if(lhsType != null && !((rhsType = pair.getRhsType()) instanceof PlaceholderType) && typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair. - && !occurs(lhsType, rhsType)) { - Unifier uni = new Unifier((PlaceholderType) lhsType, rhsType); - result = result.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); + && !rhsType.getTypeParams().occurs(lhsType)) { + Unifier uni = new Unifier(lhsType, rhsType); + result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new)); applied = true; } - result.add(pair); } return applied ? Optional.of(new HashSet<>(result)) : Optional.empty(); } - - private boolean occurs(Type t1, Type t2) { - TypeParams t2Params = t2.getTypeParams(); - for(Type t2Param : t2Params) - if(t1.equals(t2Param) || occurs(t1, t2Param)) - return true; - return false; - } - } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 65193117..6b05338b 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -70,8 +70,8 @@ public class Unify { // Sets that originate from pair pattern matching // Sets of the "second level" - sets.addAll(calculatePairSets(eq2s, fc)); + sets.addAll(calculatePairSets(eq2s, fc)); /* Up to here, no cartesian products are calculated. * Around here, filters for pairs and sets can be applied */ @@ -105,7 +105,7 @@ public class Unify { else unchanged.add(eqss); } - + /* * Step 6 a) Restart for pairs where subst was applied @@ -116,7 +116,7 @@ public class Unify { unchanged.addAll(this.unify(eqss, fc)); /* - * Step 7: Filter result for solved pairs + * Step 7: Filter result for solved pairs TODO wie? */ return unchanged; @@ -228,13 +228,21 @@ public class Unify { IUnify unify = new MartelliMontanariUnify(); Set possibleCs = fc.getAllTypes(rhsType.getName()); + + + + + + /*IUnify unify = new MartelliMontanariUnify(); + + Set possibleCs = fc.getAllTypes(rhsType.getName()); Set possibleThetas = possibleCs.stream() .flatMap(x -> fc.smaller(x).stream()) .collect(Collectors.toCollection(HashSet::new)); Set possibleThetaPrimes = possibleThetas.stream() .flatMap(x -> getAllInstantiations(x, fc).stream()) .collect(Collectors.toCollection(HashSet::new)); - + */ // TODO /*Set unifiers = possibleThetaPrimes.stream() @@ -263,7 +271,7 @@ public class Unify { // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType){ - // TODO + throw new NotImplementedException(); // TODO } // Case 3: (a <.? ? sup Theta') @@ -299,7 +307,7 @@ public class Unify { // Case 7: (? sup Theta <.? a) else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType) { - // TODO + throw new NotImplementedException(); // TODO } // Case 8: (Theta <.? a) diff --git a/test/unify/FiniteClosureTest.java b/test/unify/FiniteClosureTest.java index 04017908..85819ecd 100644 --- a/test/unify/FiniteClosureTest.java +++ b/test/unify/FiniteClosureTest.java @@ -7,6 +7,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.Type; public class FiniteClosureTest { @@ -54,4 +55,10 @@ public class FiniteClosureTest { System.out.println("SmArg(? extends List) = " + fc.smArg(tf.getExtendsType(tf.getSimpleType("List", "T")))); System.out.println("SmArg(? super List) = " + fc.smArg(tf.getSuperType(tf.getSimpleType("List", "T")))); } + + @Test + public void testGetGenericType() { + + // TODO + } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index eb7f9f5b..be759f6e 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -31,8 +31,9 @@ public class UnifyTest extends Unify { // Double <. B // B <. Object eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); + eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Number")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); + //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Number"), tf.getPlaceholderType("A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); From 863904a4e82f4d6128d1957f7d6512024c5a3d7a Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Mon, 14 Mar 2016 12:59:31 +0100 Subject: [PATCH 10/11] unifier fixed (no iterated application necessary anymore) --- .../typeinference/unify/model/TypeParams.java | 9 +++------ .../typeinference/unify/model/Unifier.java | 3 +++ .../unifynew/MartelliMontanariUnify.java | 19 ++++--------------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index 806a310a..966a561b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -41,16 +41,13 @@ public final class TypeParams implements Iterable{ } public boolean occurs(PlaceholderType t) { - for(Type p : typeParams) { - if(p instanceof PlaceholderType) { + for(Type p : typeParams) + if(p instanceof PlaceholderType) if(p.equals(t)) return true; - } else { + else if(p.getTypeParams().occurs(t)) return true; - } - } - return false; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index cb0f52d4..e9c26e59 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -21,6 +21,9 @@ public class Unifier implements Function { } public void Add(PlaceholderType source, Type target) { + Unifier tempU = new Unifier(source, target); + for(PlaceholderType pt : substitutions.keySet()) + substitutions.put(pt, substitutions.get(pt).apply(tempU)); substitutions.put(source, target); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 6c55d0be..6db1a512 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -67,7 +67,9 @@ public class MartelliMontanariUnify implements IUnify { continue; } - if(!check(pair)) // Occurs-Check + // Occurs-Check + if(pair.getLhsType() instanceof PlaceholderType + && pair.getRhsType().getTypeParams().occurs((PlaceholderType) pair.getLhsType())) return Optional.empty(); Optional> optUni = eliminate(pair); @@ -120,20 +122,7 @@ public class MartelliMontanariUnify implements IUnify { return Optional.empty(); } - - private boolean check(MPair pair) { - Type rhs = pair.getRhsType(); - Type lhs = pair.getLhsType(); - - TypeParams rhsTypeParams = rhs.getTypeParams(); - - for(Type t : rhsTypeParams) - if(lhs.equals(t)) - return false; - - return true; - } - + private Optional> eliminate(MPair pair) { Type rhs = pair.getRhsType(); Type lhs = pair.getLhsType(); From 6acc3c1366e4ac2a7a7c04269e73960b9b89e5a9 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Mon, 14 Mar 2016 14:46:10 +0100 Subject: [PATCH 11/11] step 4 rule 1 --- .../unify/model/ExtendsType.java | 5 ++ .../unify/model/PlaceholderType.java | 5 ++ .../typeinference/unify/model/SimpleType.java | 5 ++ .../typeinference/unify/model/SuperType.java | 5 ++ .../typeinference/unify/model/Type.java | 2 + .../typeinference/unify/model/Unifier.java | 5 ++ .../typeinference/unifynew/Unify.java | 88 +++++++++++-------- test/unify/UnifyTest.java | 36 +++++++- 8 files changed, 113 insertions(+), 38 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index da59af1a..9fb3e454 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -35,6 +35,11 @@ public final class ExtendsType extends Type { public TypeParams getTypeParams() { return extendedType.getTypeParams(); } + + @Override + public Type setTypeParams(TypeParams newTp) { + return new ExtendsType(extendedType.setTypeParams(newTp)); + } @Override Set smArg(IFiniteClosure fc) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index e1f1697a..5a3d5341 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -20,6 +20,11 @@ public final class PlaceholderType extends Type{ return fc.grArg(this); } + @Override + public Type setTypeParams(TypeParams newTp) { + return this; + } + @Override public int hashCode() { return typeName.hashCode(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java index 1283ceb6..ef01652a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java @@ -28,6 +28,11 @@ public final class SimpleType extends Type { return new SimpleType(typeName, typeParams.apply(unif)); } + @Override + public Type setTypeParams(TypeParams newTp) { + return new SimpleType(new String(typeName), newTp); + } + @Override public int hashCode() { return typeName.hashCode(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 7090d86a..2503e748 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -27,6 +27,11 @@ public final class SuperType extends Type { return superedType.getTypeParams(); } + @Override + public Type setTypeParams(TypeParams newTp) { + return new SuperType(superedType.setTypeParams(newTp)); + } + @Override Set smArg(IFiniteClosure fc) { return fc.smArg(this); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java index 52977fbf..3c179110 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Type.java @@ -27,6 +27,8 @@ public abstract class Type { return typeParams; } + public abstract Type setTypeParams(TypeParams newTp); + abstract Set smArg(IFiniteClosure fc); abstract Set grArg(IFiniteClosure fc); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index e9c26e59..0a1fde25 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.HashMap; import java.util.Map.Entry; +import java.util.Set; import java.util.function.Function; public class Unifier implements Function { @@ -43,6 +44,10 @@ public class Unifier implements Function { public Type getSubstitute(Type t) { return substitutions.get(t); } + + public Set> getSubstitutions() { + return substitutions.entrySet(); + } @Override public String toString() { diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 6b05338b..548340b3 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -1,11 +1,13 @@ package de.dhbwstuttgart.typeinference.unifynew; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map.Entry; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -24,6 +26,7 @@ import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.Type; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; /** @@ -225,48 +228,47 @@ public class Unify { // Case 1: (a <. Theta') if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { + Type thetaPrime = pair.getRhsType(); + Set set = new HashSet<>(); IUnify unify = new MartelliMontanariUnify(); - Set possibleCs = fc.getAllTypes(rhsType.getName()); + //Set cs = fc.getAllTypes(rhsType.getName()); + Type c = rhsType; + //Set thetaQs = cs.stream().flatMap(x -> fc.smaller(x).stream()).collect(Collectors.toCollection(HashSet::new)); + Set thetaQs = fc.smaller(c); + Set thetaQPrimes = new HashSet<>(); + TypeParams cParams = c.getTypeParams(); + if(cParams.size() == 0) + thetaQPrimes.add(c); + else { + ArrayList> candidateParams = new ArrayList<>(); + for(Type param : cParams) + candidateParams.add(fc.grArg(param)); + Set permutations = new HashSet(); + permuteParams(candidateParams, 0, permutations, new Type[candidateParams.size()]); + + for(TypeParams tp : permutations) + thetaQPrimes.add(c.setTypeParams(tp)); + } + for(Type tqp : thetaQPrimes) { + Optional opt = unify.unify(tqp, thetaPrime); + if(opt.isPresent()) { + Unifier unifier = opt.get(); + Set> substitutions = unifier.getSubstitutions(); + for(Entry sigma : substitutions) + set.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); + for(Type tq : thetaQs) { + Set smaller = fc.smaller(unifier.apply(tq)); + smaller.stream().map(x -> new MPair(lhsType, x, PairOperator.EQUALSDOT)).forEach(x -> set.add(x)); + } + } + } - /*IUnify unify = new MartelliMontanariUnify(); - - Set possibleCs = fc.getAllTypes(rhsType.getName()); - Set possibleThetas = possibleCs.stream() - .flatMap(x -> fc.smaller(x).stream()) - .collect(Collectors.toCollection(HashSet::new)); - Set possibleThetaPrimes = possibleThetas.stream() - .flatMap(x -> getAllInstantiations(x, fc).stream()) - .collect(Collectors.toCollection(HashSet::new)); - */ - // TODO - - /*Set unifiers = possibleThetaPrimes.stream() - .map(x -> unify.unify(x, rhsType)) - .filter(x -> x.isPresent()) - .flatMap(x -> x.get().stream()) - .map(x -> new Unifier(x.getLhsType(), x.getRhsType())) - .collect(Collectors.toCollection(HashSet::new)); - - Set thetas = new HashSet<>(); - - for(Unifier sigma : unifiers) - for(Type thetaQuer : possibleThetas) - thetas.addAll(fc.smaller(sigma.apply(thetaQuer))); - - Set resultSet = new HashSet<>(); - thetas.forEach(x -> resultSet.add(new MPair(lhsType, x, PairOperator.EQUALSDOT))); - unifiers.forEach(x -> resultSet.add(new MPair(x.getSource(), x.getTarget(), PairOperator.EQUALSDOT))); - - result.add(resultSet); - // TODO Speedup - Potenzial durch auschließen unmöglicher kombinationen (z.B. wenn in theta' eine Variable festgelegt ist) - // TODO speedup durch pipelining mit streams - // TODO speedup teure berechnungen mit proxy rauszögern - */ + result.add(set); } // Case 2: (a <.? ? ext Theta') @@ -285,7 +287,7 @@ public class Unify { // Case 4: (a <.? Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) { Set set = new HashSet<>(); - set.add(new MPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT)); + set.add(new MPair(lhsType, rhsType, PairOperator.EQUALSDOT)); result.add(set); } @@ -322,6 +324,20 @@ public class Unify { return result; } + protected void permuteParams(ArrayList> candidates, int idx, Set result, Type[] current) { + if(candidates.size() == idx) { + result.add(new TypeParams(Arrays.copyOf(current, current.length))); + return; + } + + Set localCandidates = candidates.get(idx); + + for(Type t : localCandidates) { + current[idx] = t; + permuteParams(candidates, idx+1, result, current); + } + } + private Set getAllInstantiations(Type t, IFiniteClosure fc) { Set result = new HashSet<>(); result.add(t); diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index be759f6e..51e178f4 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -1,5 +1,6 @@ package unify; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -8,6 +9,8 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unifynew.Unify; public class UnifyTest extends Unify { @@ -21,6 +24,7 @@ public class UnifyTest extends Unify { //fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object")); fcb.add(tf.getSimpleType("Integer"), tf.getSimpleType("Number")); fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number")); + //fcb.add(tf.getSimpleType("List", "T")); IFiniteClosure fc = fcb.getCollectionExample(); @@ -31,9 +35,9 @@ public class UnifyTest extends Unify { // Double <. B // B <. Object eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Number")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); + //eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Number")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); - //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); + eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("List", tf.getSimpleType("Number")), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Number"), tf.getPlaceholderType("A"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); @@ -53,4 +57,32 @@ public class UnifyTest extends Unify { } + @Test + public void permuteParamsTest() { + TypeFactory tf = new TypeFactory(); + ArrayList> candidates = new ArrayList<>(); + + Set p1 = new HashSet<>(); + p1.add(tf.getPlaceholderType("p11")); + p1.add(tf.getExtendsType(tf.getSimpleType("p12"))); + p1.add(tf.getSimpleType("p13")); + + Set p2 = new HashSet<>(); + p2.add(tf.getPlaceholderType("p21")); + p2.add(tf.getPlaceholderType("p22")); + + Set p3 = new HashSet<>(); + p3.add(tf.getSimpleType("p31", "T")); + p3.add(tf.getSimpleType("p32")); + + candidates.add(p1); + candidates.add(p2); + candidates.add(p3); + + Set result = new HashSet<>(); + permuteParams(candidates, 0, result, new Type[candidates.size()]); + + System.out.println(result); + } + }