From 2483044e0c8b02a002e47ed7706eb2a49b0935e2 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 7 Nov 2015 10:57:17 +0100 Subject: [PATCH] rules --- .../typeinference/unify/Unify.java | 149 ++++++------------ .../unify/interfaces/IRuleSet.java | 2 +- .../typeinference/unifynew/RuleSet.java | 96 +++++++++-- .../unify/model/FiniteClosure.java | 4 +- .../typinference/unify/model/MPair.java | 51 ++++-- .../typinference/unify/model/SimpleType.java | 5 +- .../typinference/unify/model/SuperType.java | 13 +- .../typinference/unify/model/Type.java | 33 ++-- .../typinference/unify/model/TypeParams.java | 37 +++++ test/unify/RuleSetTest.java | 78 +++++++++ 10 files changed, 319 insertions(+), 149 deletions(-) create mode 100644 src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java create mode 100644 test/unify/RuleSetTest.java diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index c5afe522..f11b8f3d 100755 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -2738,105 +2738,56 @@ tempKlasse.get_Superclass_Name() ); System.out.println( "P. S.: */ // ino.end - // ino.method.pi.28118.definition - public static int pi( int n, String C, String D, Menge tto ) - throws SCException - // ino.end - // ino.method.pi.28118.body - { - // otth: Permutation, benoetigt fuer Unifikation - // C, D: Namen von Klassen - // z.B. class C --- class D - // pi(1) = 2 - - // MyCompiler.printDebugInfo("---------------- Permutation: n = " + String.valueOf(n) + " - C = " + C + " - D = " + D, 6); - - // Klassen suchen - //Class KlasseC = null; - //Class KlasseD = null; - //Class tempKlasse = null; - // for( int i = 0; i < KlassenVektor.size(); i++ ) - // { - // // MyCompiler.printDebugInfo("---------------- Permutation: i = " + String.valueOf(i) + " - Classname = " + ((Class)KlassenVektor.elementAt(i)).get_classname(), 6); - - // if( KlasseC != null && KlasseD != null ) - // break; - - // tempKlasse = (Class)KlassenVektor.elementAt(i); - // if( C.equals( tempKlasse.get_classname() ) ) - // { - // KlasseC = tempKlasse; - // // MyCompiler.printDebugInfo("---------------- Permutation: C-Klasse gefunden!", 6); - // } - - // if( D.equals( tempKlasse.get_classname() ) ) - // { - // KlasseD = tempKlasse; - // // MyCompiler.printDebugInfo("---------------- Permutation: D-Klasse gefunden!", 6); - // } - // } - - if (C.equals(D)) return n; //Reduktion mit gleichen Typkonstruktoren - - else { - RefType KlasseC = null; - RefType KlasseD = null; - //RefType tempKlasse = null; - - for( int i = 0; i < tto.size(); i++ ) { - - KlasseC = (RefType)((Pair)tto.elementAt(i)).TA1; - KlasseD = (RefType)((Pair)tto.elementAt(i)).TA2; - - if (KlasseC.getTypeName().equals(C) && KlasseD.getTypeName().equals(D)) { - break; - } - else { - KlasseC = null; - KlasseD = null; - } - - } - - SCException F = new SCException(); - if( KlasseC == null && KlasseD == null ) - throw F; // Fehler - - // Vektorlisten extrahieren - Menge vC = KlasseC.get_ParaList(); - Menge vD = KlasseD.get_ParaList(); - - if( vC == null || vD == null ) - throw F; - - if( n >= vD.size() ) - throw F; - - // Permuationswert f�r 'n' berechnen - Type TV = (Type)vD.elementAt(n); - - int nPos = -1; - - // MyCompiler.printDebugInfo("---------------- Permutation: vC.size() = " + String.valueOf( vC.size() ), 6); - - for( int i = 0; i < vC.size(); i++ ) - { - // MyCompiler.printDebugInfo("---------------- Permutation: TV = " + ((Type)vC.elementAt(i)).getName_(), 6); - if( ((Type)vC.elementAt(i)).getName().equals( TV.getName() ) ) - { - nPos = i; - break; - } - } - - if (nPos == -1) - throw F; - - // MyCompiler.printDebugInfo("---------------- Permutation: = " + String.valueOf( nPos ), 6); - - return nPos; - } - } + // ino.method.pi.28118.definition + public static int pi(int n, String C, String D, Menge tto) + throws SCException { + + if (C.equals(D)) + return n; // Reduktion mit gleichen Typkonstruktoren + + RefType KlasseC = null; + RefType KlasseD = null; + + for (int i = 0; i < tto.size(); i++) { + + KlasseC = (RefType) ((Pair) tto.elementAt(i)).TA1; + KlasseD = (RefType) ((Pair) tto.elementAt(i)).TA2; + + if (KlasseC.getTypeName().equals(C) + && KlasseD.getTypeName().equals(D)) + break; + else { + KlasseC = null; + KlasseD = null; + } + } + + SCException F = new SCException(); + if (KlasseC == null && KlasseD == null) + throw F; // Fehler + + // Vektorlisten extrahieren + Menge vC = KlasseC.get_ParaList(); + Menge vD = KlasseD.get_ParaList(); + + if (vC == null || vD == null) + throw F; + + if (n >= vD.size()) + throw F; + + // Permuationswert f�r 'n' berechnen + Type TV = (Type) vD.elementAt(n); + int nPos = -1; + for (int i = 0; i < vC.size(); i++) + if (((Type) vC.elementAt(i)).getName().equals(TV.getName())) { + nPos = i; + break; + } + + return nPos; + + } // ino.end // ino.method.printMengeUnifier.28121.definition public static void printMengeUnifier(String strMenge, Menge> Uni, int nDebug ) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index 5fdb53d3..88f98ac7 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -20,7 +20,7 @@ public interface IRuleSet { public boolean erase2(MPair pair); public boolean erase3(MPair pair); - public MPair swap(MPair pair); + public Optional swap(MPair pair); public MPair adapt(MPair pair); public MPair adaptExt(MPair pair); diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index bbf6a3bf..fca0e120 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -36,7 +36,7 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new MPair(lhsType, ((SuperType) rhsType).GetSuperedType())); + return Optional.of(new MPair(lhsType, ((SuperType) rhsType).GetSuperedType(), PairOperator.SMALLERDOT)); } @Override @@ -52,7 +52,7 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(), rhsType)); + return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(), rhsType, PairOperator.SMALLERDOT)); } @Override @@ -68,7 +68,7 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof SuperType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(),((SuperType) rhsType).GetSuperedType())); + return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(),((SuperType) rhsType).GetSuperedType(), PairOperator.SMALLERDOT)); } @Override @@ -76,6 +76,14 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); + Type lhsType = pair.getLhsType(); + SimpleType lhsSType; + + if(lhsType instanceof SimpleType) + lhsSType = (SimpleType) lhsType; + else if(lhsType instanceof ExtendsType) + lhsSType = (SimpleType) ((ExtendsType) lhsType).GetExtendedType(); + else return null; } @@ -108,24 +116,70 @@ public class RuleSet implements IRuleSet{ SimpleType lhsSType = (SimpleType) lhsType; SimpleType rhsSType = (SimpleType) rhsType; - if(lhsSType.getTypeArgs().length == 0 || rhsSType.getTypeArgs().length == 0 || lhsSType.getTypeArgs() != rhsSType.getTypeArgs()) + if(lhsSType.getTypeParams().empty()|| rhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size()) return Optional.empty(); int[] pi = pi(lhsSType, rhsSType); - Type[] rhsTypeArgs = rhsType.getTypeArgs(); - Type[] lhsTypeArgs = lhsType.getTypeArgs(); + + if(pi.length == 0) + return Optional.empty(); + + Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); + Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); Set result = new HashSet<>(); - for(int rhsIdx = 0; rhsIdx < rhsTypeArgs.length; rhsIdx++) - result.add(new MPair(lhsTypeArgs[pi[rhsIdx]], rhsTypeArgs[rhsIdx])); + for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++) + result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOT)); return Optional.of(result); } @Override public Optional> reduce2(MPair pair) { - // TODO Auto-generated method stub - return null; + if(pair.getPairOp() != PairOperator.EQUALSDOT) + return Optional.empty(); + + Type lhsType = pair.getLhsType(); + SimpleType lhsSType; + + if(lhsType instanceof SimpleType) + lhsSType = (SimpleType) lhsType; + else if(lhsType instanceof ExtendsType) + lhsSType = (SimpleType) ((ExtendsType) lhsType).GetExtendedType(); + else if(lhsType instanceof SuperType) + lhsSType = (SimpleType) ((SuperType) lhsType).GetSuperedType(); + else + return Optional.empty(); + + if(lhsSType.getTypeParams().empty()) + return Optional.empty(); + + Type rhsType = pair.getLhsType(); + SimpleType rhsSType; + + if(rhsType instanceof SimpleType) + rhsSType = (SimpleType) rhsType; + else if(rhsType instanceof ExtendsType) + rhsSType = (SimpleType) ((ExtendsType) rhsType).GetExtendedType(); + else if(rhsType instanceof SuperType) + rhsSType = (SimpleType) ((SuperType) rhsType).GetSuperedType(); + else + return Optional.empty(); + + if(rhsSType.getTypeParams().empty()) + return Optional.empty(); + + if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) + return Optional.empty(); + + Set result = new HashSet<>(); + + Type[] rhsTypeParams = rhsSType.getTypeParams().asArray(); + Type[] lhsTypeParams = lhsSType.getTypeParams().asArray(); + for(int i = 0; i < rhsTypeParams.length; i++) + result.add(new MPair(lhsTypeParams[i], rhsTypeParams[i], PairOperator.SMALLERDOT)); + + return Optional.of(result); } @Override @@ -169,9 +223,17 @@ public class RuleSet implements IRuleSet{ } @Override - public MPair swap(MPair pair) { - // TODO Auto-generated method stub - return null; + public Optional swap(MPair pair) { + if(pair.getPairOp() != PairOperator.EQUALSDOT) + return Optional.empty(); + + if(pair.getLhsType() instanceof PlaceholderType) + return Optional.empty(); + + if(!(pair.getRhsType() instanceof PlaceholderType)) + return Optional.empty(); + + return Optional.of(new MPair(pair.getRhsType(), pair.getLhsType(), PairOperator.EQUALSDOT)); } @Override @@ -211,11 +273,11 @@ public class RuleSet implements IRuleSet{ Type cFromFc = opt.get(); - Assert.assertEquals(cFromFc.getTypeArgs().length, dFromFc.getTypeArgs().length); - Assert.assertTrue(dFromFc.getTypeArgs().length > 0); + Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size()); + Assert.assertTrue(dFromFc.getTypeParams().size() > 0); - Type[] cArgs = cFromFc.getTypeArgs(); - Type[] dArgs = dFromFc.getTypeArgs(); + Type[] cArgs = cFromFc.getTypeParams().asArray(); + Type[] dArgs = dFromFc.getTypeParams().asArray(); int[] permutation = new int[dArgs.length]; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java index 6509060c..4ca7b250 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java @@ -28,8 +28,8 @@ public class FiniteClosure implements IFiniteClosure { if(!inheritanceGraph.containsKey(pair.getRhsType().getName())) inheritanceGraph.put(pair.getRhsType().getName(), new Node(pair.getRhsType())); - Node childNode = inheritanceGraph.get(pair.getRhsType()); - Node parentNode = inheritanceGraph.get(pair.getLhsType()); + Node childNode = inheritanceGraph.get(pair.getLhsType().getName()); + Node parentNode = inheritanceGraph.get(pair.getRhsType().getName()); // Add edge parentNode.AddDescendant(childNode); diff --git a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java index ea32f9a7..4a811940 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java @@ -7,31 +7,47 @@ public class MPair { SMALLERDOT, SMALLERDOTWC, EQUALS, - EQUALSDOT + EQUALSDOT; + + @Override + public String toString() { + switch (this) { + case SMALLER: + return "<"; + case SMALLERDOT: + return "<."; + case SMALLERDOTWC: + return "<.?"; + case EQUALS: + return "="; + default: + return "=."; + } + }; } - private Type type1; - private Type type2; + private Type lhs; + private Type rhs; private PairOperator pairOp; - public MPair(Type t1, Type t2) { - type1 = t1; - type2 = t2; + /*public MPair(Type t1, Type t2) { + lhs = t1; + rhs = t2; pairOp = PairOperator.SMALLER; - } + }*/ public MPair(Type t1, Type t2, PairOperator op) { - type1 = t1; - type2 = t2; + lhs = t1; + rhs = t2; pairOp = op; } public Type getLhsType() { - return type1; + return lhs; } public Type getRhsType() { - return type2; + return rhs; } public PairOperator getPairOp() { @@ -48,12 +64,19 @@ public class MPair { MPair other = (MPair) obj; return other.getPairOp() == pairOp - && other.getLhsType().equals(type1) - && other.getRhsType().equals(type2); + && other.getLhsType().equals(lhs) + && other.getRhsType().equals(rhs); } @Override public int hashCode() { - return 17 + 31 * type1.hashCode() + 31 * type2.hashCode() + 31 * pairOp.hashCode(); + return 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode(); + } + + @Override + public String toString() { + return "(" + lhs + " " + pairOp + " " + rhs + ")"; } } + + diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java index 0ff65f96..8104202d 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java @@ -1,5 +1,8 @@ package de.dhbwstuttgart.typinference.unify.model; public class SimpleType extends Type { - + public SimpleType(String name, Type... typeParams) { + this.typeName = name; + this.typeParams = new TypeParams(typeParams); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java index fcd8534c..a5377bb8 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java @@ -2,7 +2,18 @@ package de.dhbwstuttgart.typinference.unify.model; public class SuperType extends Type { + private Type superedType; + + public SuperType(Type superedType) { + this.superedType = superedType; + } + public Type GetSuperedType() { - return null; + return superedType; + } + + @Override + public String toString() { + return "? super " + superedType; } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Type.java b/src/de/dhbwstuttgart/typinference/unify/model/Type.java index 201dc80d..ed317e73 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/Type.java @@ -1,16 +1,18 @@ package de.dhbwstuttgart.typinference.unify.model; +import java.util.Arrays; + public abstract class Type { protected String typeName = ""; - protected Type[] typeArgs = null; + protected TypeParams typeParams; public String getName() { return typeName; } - public Type[] getTypeArgs() { - return typeArgs; + public TypeParams getTypeParams() { + return typeParams; } @Override @@ -24,17 +26,8 @@ public abstract class Type { if(!other.getName().equals(typeName)) return false; - - Type[] otherTypeArgs = other.getTypeArgs(); - - if(otherTypeArgs.length != typeArgs.length) - return false; - - for(int i = 0; i < typeArgs.length; i++) - if(!typeArgs[i].equals(otherTypeArgs[i])) - return false; - - return true; + + return other.getTypeParams().equals(typeParams); } @@ -42,4 +35,16 @@ public abstract class Type { public int hashCode() { return typeName.hashCode(); } + + @Override + public String toString() { + String params = ""; + if(typeParams.size() != 0) { + for(Type param : typeParams) + params += param.toString() + ","; + params = "<" + params.substring(0, params.length()-1) + ">"; + } + + return typeName + params; + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java new file mode 100644 index 00000000..bf2bdde9 --- /dev/null +++ b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java @@ -0,0 +1,37 @@ +package de.dhbwstuttgart.typinference.unify.model; + +import java.util.Arrays; +import java.util.Iterator; + +public class TypeParams implements Iterable{ + private Type[] typeParams; + + public TypeParams(Type... types) { + typeParams = types; + } + + @Override + public String toString() { + String res = ""; + for(Type t : typeParams) + res += t + ","; + return "<" + res.substring(0, res.length()-1) + ">"; + } + + public int size() { + return typeParams.length; + } + + public boolean empty() { + return typeParams.length == 0; + } + + public Type[] asArray() { + return typeParams; + } + + @Override + public Iterator iterator() { + return Arrays.stream(typeParams).iterator(); + } +} diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java new file mode 100644 index 00000000..5a8ee772 --- /dev/null +++ b/test/unify/RuleSetTest.java @@ -0,0 +1,78 @@ +package unify; + + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +import org.junit.Test; + +import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; +import de.dhbwstuttgart.typeinference.unifynew.RuleSet; +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.SimpleType; +import de.dhbwstuttgart.typinference.unify.model.SuperType; + + +public class RuleSetTest { + + @Test + public void testReduceUp() { + Set pairs = new HashSet(); + + SimpleType t1 = new SimpleType("Set"); + SimpleType t2 = new SimpleType("HashSet"); + SimpleType t3 = new SimpleType("Something"); + + SuperType s = new SuperType(t2); + + pairs.add(new MPair(t1, t2, PairOperator.SMALLER)); + + FiniteClosure fc = new FiniteClosure(pairs); + + IRuleSet rules = new RuleSet(fc); + + MPair test = new MPair(t3, s, PairOperator.SMALLERDOT); + + + Optional res = rules.reduceUp(test); + + System.out.println(res); + } + + @Test + public void testReduce1() { + Set pairs = new HashSet(); + + SimpleType t1 = new SimpleType("t1"); + SimpleType t2 = new SimpleType("t2"); + SimpleType t3 = new SimpleType("t3"); + SimpleType t4 = new SimpleType("t4"); + SimpleType t5 = new SimpleType("t5"); + SimpleType t6 = new SimpleType("t6"); + SimpleType t7 = new SimpleType("t7"); + SimpleType t8 = new SimpleType("t8"); + SimpleType t9 = new SimpleType("t9"); + + SimpleType ta = new SimpleType("a", t1, t2, t3); + SimpleType taa = new SimpleType("a", t4, t5, t6); + SimpleType tb = new SimpleType("b", t3, t1, t2); + SimpleType tbb = new SimpleType("b", t7, t8, t9); + + + pairs.add(new MPair(ta, tb, PairOperator.SMALLER)); + FiniteClosure fc = new FiniteClosure(pairs); + + IRuleSet rules = new RuleSet(fc); + + MPair test = new MPair(taa, tbb, PairOperator.SMALLERDOT); + + Optional> res = rules.reduce1(test); + + System.out.println(res); + } + + +}