This commit is contained in:
Florian Steurer 2015-11-07 10:57:17 +01:00
parent a86b70e672
commit 2483044e0c
10 changed files with 319 additions and 149 deletions

View File

@ -2738,105 +2738,56 @@ tempKlasse.get_Superclass_Name() ); System.out.println( "P. S.:
*/ */
// ino.end // ino.end
// ino.method.pi.28118.definition // ino.method.pi.28118.definition
public static int pi( int n, String C, String D, Menge tto ) public static int pi(int n, String C, String D, Menge tto)
throws SCException throws SCException {
// ino.end
// ino.method.pi.28118.body if (C.equals(D))
{ return n; // Reduktion mit gleichen Typkonstruktoren
// otth: Permutation, benoetigt fuer Unifikation
// C, D: Namen von Klassen RefType KlasseC = null;
// z.B. class C<A, B> --- class D<B, A> RefType KlasseD = null;
// pi(1) = 2
for (int i = 0; i < tto.size(); i++) {
// MyCompiler.printDebugInfo("---------------- Permutation: n = " + String.valueOf(n) + " - C = " + C + " - D = " + D, 6);
KlasseC = (RefType) ((Pair) tto.elementAt(i)).TA1;
// Klassen suchen KlasseD = (RefType) ((Pair) tto.elementAt(i)).TA2;
//Class KlasseC = null;
//Class KlasseD = null; if (KlasseC.getTypeName().equals(C)
//Class tempKlasse = null; && KlasseD.getTypeName().equals(D))
// for( int i = 0; i < KlassenVektor.size(); i++ ) break;
// { else {
// // MyCompiler.printDebugInfo("---------------- Permutation: i = " + String.valueOf(i) + " - Classname = " + ((Class)KlassenVektor.elementAt(i)).get_classname(), 6); KlasseC = null;
KlasseD = null;
// if( KlasseC != null && KlasseD != null ) }
// break; }
// tempKlasse = (Class)KlassenVektor.elementAt(i); SCException F = new SCException();
// if( C.equals( tempKlasse.get_classname() ) ) if (KlasseC == null && KlasseD == null)
// { throw F; // Fehler
// KlasseC = tempKlasse;
// // MyCompiler.printDebugInfo("---------------- Permutation: C-Klasse gefunden!", 6); // Vektorlisten extrahieren
// } Menge vC = KlasseC.get_ParaList();
Menge vD = KlasseD.get_ParaList();
// if( D.equals( tempKlasse.get_classname() ) )
// { if (vC == null || vD == null)
// KlasseD = tempKlasse; throw F;
// // MyCompiler.printDebugInfo("---------------- Permutation: D-Klasse gefunden!", 6);
// } if (n >= vD.size())
// } throw F;
if (C.equals(D)) return n; //Reduktion mit gleichen Typkonstruktoren // Permuationswert ¯Â¿Â½r 'n' berechnen
Type TV = (Type) vD.elementAt(n);
else { int nPos = -1;
RefType KlasseC = null; for (int i = 0; i < vC.size(); i++)
RefType KlasseD = null; if (((Type) vC.elementAt(i)).getName().equals(TV.getName())) {
//RefType tempKlasse = null; nPos = i;
break;
for( int i = 0; i < tto.size(); i++ ) { }
KlasseC = (RefType)((Pair)tto.elementAt(i)).TA1; return nPos;
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 ¯Â¿Â½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.end // ino.end
// ino.method.printMengeUnifier.28121.definition // ino.method.printMengeUnifier.28121.definition
public static void printMengeUnifier(String strMenge, Menge<Menge<Pair>> Uni, int nDebug ) public static void printMengeUnifier(String strMenge, Menge<Menge<Pair>> Uni, int nDebug )

View File

@ -20,7 +20,7 @@ public interface IRuleSet {
public boolean erase2(MPair pair); public boolean erase2(MPair pair);
public boolean erase3(MPair pair); public boolean erase3(MPair pair);
public MPair swap(MPair pair); public Optional<MPair> swap(MPair pair);
public MPair adapt(MPair pair); public MPair adapt(MPair pair);
public MPair adaptExt(MPair pair); public MPair adaptExt(MPair pair);

View File

@ -36,7 +36,7 @@ public class RuleSet implements IRuleSet{
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType)) if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType))
return Optional.empty(); return Optional.empty();
return Optional.of(new MPair(lhsType, ((SuperType) rhsType).GetSuperedType())); return Optional.of(new MPair(lhsType, ((SuperType) rhsType).GetSuperedType(), PairOperator.SMALLERDOT));
} }
@Override @Override
@ -52,7 +52,7 @@ public class RuleSet implements IRuleSet{
if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType)) if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType))
return Optional.empty(); return Optional.empty();
return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(), rhsType)); return Optional.of(new MPair(((ExtendsType) lhsType).GetExtendedType(), rhsType, PairOperator.SMALLERDOT));
} }
@Override @Override
@ -68,7 +68,7 @@ public class RuleSet implements IRuleSet{
if(!(rhsType instanceof SuperType)) if(!(rhsType instanceof SuperType))
return Optional.empty(); 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 @Override
@ -76,6 +76,14 @@ public class RuleSet implements IRuleSet{
if(pair.getPairOp() != PairOperator.SMALLERDOTWC) if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
return Optional.empty(); 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; return null;
} }
@ -108,24 +116,70 @@ public class RuleSet implements IRuleSet{
SimpleType lhsSType = (SimpleType) lhsType; SimpleType lhsSType = (SimpleType) lhsType;
SimpleType rhsSType = (SimpleType) rhsType; 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(); return Optional.empty();
int[] pi = pi(lhsSType, rhsSType); 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<MPair> result = new HashSet<>(); Set<MPair> result = new HashSet<>();
for(int rhsIdx = 0; rhsIdx < rhsTypeArgs.length; rhsIdx++) for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++)
result.add(new MPair(lhsTypeArgs[pi[rhsIdx]], rhsTypeArgs[rhsIdx])); result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOT));
return Optional.of(result); return Optional.of(result);
} }
@Override @Override
public Optional<Set<MPair>> reduce2(MPair pair) { public Optional<Set<MPair>> reduce2(MPair pair) {
// TODO Auto-generated method stub if(pair.getPairOp() != PairOperator.EQUALSDOT)
return null; 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<MPair> 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 @Override
@ -169,9 +223,17 @@ public class RuleSet implements IRuleSet{
} }
@Override @Override
public MPair swap(MPair pair) { public Optional<MPair> swap(MPair pair) {
// TODO Auto-generated method stub if(pair.getPairOp() != PairOperator.EQUALSDOT)
return null; 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 @Override
@ -211,11 +273,11 @@ public class RuleSet implements IRuleSet{
Type cFromFc = opt.get(); Type cFromFc = opt.get();
Assert.assertEquals(cFromFc.getTypeArgs().length, dFromFc.getTypeArgs().length); Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size());
Assert.assertTrue(dFromFc.getTypeArgs().length > 0); Assert.assertTrue(dFromFc.getTypeParams().size() > 0);
Type[] cArgs = cFromFc.getTypeArgs(); Type[] cArgs = cFromFc.getTypeParams().asArray();
Type[] dArgs = dFromFc.getTypeArgs(); Type[] dArgs = dFromFc.getTypeParams().asArray();
int[] permutation = new int[dArgs.length]; int[] permutation = new int[dArgs.length];

View File

@ -28,8 +28,8 @@ public class FiniteClosure implements IFiniteClosure {
if(!inheritanceGraph.containsKey(pair.getRhsType().getName())) if(!inheritanceGraph.containsKey(pair.getRhsType().getName()))
inheritanceGraph.put(pair.getRhsType().getName(), new Node<Type>(pair.getRhsType())); inheritanceGraph.put(pair.getRhsType().getName(), new Node<Type>(pair.getRhsType()));
Node<Type> childNode = inheritanceGraph.get(pair.getRhsType()); Node<Type> childNode = inheritanceGraph.get(pair.getLhsType().getName());
Node<Type> parentNode = inheritanceGraph.get(pair.getLhsType()); Node<Type> parentNode = inheritanceGraph.get(pair.getRhsType().getName());
// Add edge // Add edge
parentNode.AddDescendant(childNode); parentNode.AddDescendant(childNode);

View File

@ -7,31 +7,47 @@ public class MPair {
SMALLERDOT, SMALLERDOT,
SMALLERDOTWC, SMALLERDOTWC,
EQUALS, 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 lhs;
private Type type2; private Type rhs;
private PairOperator pairOp; private PairOperator pairOp;
public MPair(Type t1, Type t2) { /*public MPair(Type t1, Type t2) {
type1 = t1; lhs = t1;
type2 = t2; rhs = t2;
pairOp = PairOperator.SMALLER; pairOp = PairOperator.SMALLER;
} }*/
public MPair(Type t1, Type t2, PairOperator op) { public MPair(Type t1, Type t2, PairOperator op) {
type1 = t1; lhs = t1;
type2 = t2; rhs = t2;
pairOp = op; pairOp = op;
} }
public Type getLhsType() { public Type getLhsType() {
return type1; return lhs;
} }
public Type getRhsType() { public Type getRhsType() {
return type2; return rhs;
} }
public PairOperator getPairOp() { public PairOperator getPairOp() {
@ -48,12 +64,19 @@ public class MPair {
MPair other = (MPair) obj; MPair other = (MPair) obj;
return other.getPairOp() == pairOp return other.getPairOp() == pairOp
&& other.getLhsType().equals(type1) && other.getLhsType().equals(lhs)
&& other.getRhsType().equals(type2); && other.getRhsType().equals(rhs);
} }
@Override @Override
public int hashCode() { 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 + ")";
} }
} }

View File

@ -1,5 +1,8 @@
package de.dhbwstuttgart.typinference.unify.model; package de.dhbwstuttgart.typinference.unify.model;
public class SimpleType extends Type { public class SimpleType extends Type {
public SimpleType(String name, Type... typeParams) {
this.typeName = name;
this.typeParams = new TypeParams(typeParams);
}
} }

View File

@ -2,7 +2,18 @@ package de.dhbwstuttgart.typinference.unify.model;
public class SuperType extends Type { public class SuperType extends Type {
private Type superedType;
public SuperType(Type superedType) {
this.superedType = superedType;
}
public Type GetSuperedType() { public Type GetSuperedType() {
return null; return superedType;
}
@Override
public String toString() {
return "? super " + superedType;
} }
} }

View File

@ -1,16 +1,18 @@
package de.dhbwstuttgart.typinference.unify.model; package de.dhbwstuttgart.typinference.unify.model;
import java.util.Arrays;
public abstract class Type { public abstract class Type {
protected String typeName = ""; protected String typeName = "";
protected Type[] typeArgs = null; protected TypeParams typeParams;
public String getName() { public String getName() {
return typeName; return typeName;
} }
public Type[] getTypeArgs() { public TypeParams getTypeParams() {
return typeArgs; return typeParams;
} }
@Override @Override
@ -24,17 +26,8 @@ public abstract class Type {
if(!other.getName().equals(typeName)) if(!other.getName().equals(typeName))
return false; return false;
Type[] otherTypeArgs = other.getTypeArgs(); return other.getTypeParams().equals(typeParams);
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;
} }
@ -42,4 +35,16 @@ public abstract class Type {
public int hashCode() { public int hashCode() {
return typeName.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;
}
} }

View File

@ -0,0 +1,37 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Arrays;
import java.util.Iterator;
public class TypeParams implements Iterable<Type>{
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<Type> iterator() {
return Arrays.stream(typeParams).iterator();
}
}

View File

@ -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<MPair> pairs = new HashSet<MPair>();
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<MPair> res = rules.reduceUp(test);
System.out.println(res);
}
@Test
public void testReduce1() {
Set<MPair> pairs = new HashSet<MPair>();
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<Set<MPair>> res = rules.reduce1(test);
System.out.println(res);
}
}