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.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<A, B> --- class D<B, A>
// 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 ¯Â¿Â½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 ¯Â¿Â½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<Menge<Pair>> Uni, int nDebug )

View File

@ -20,7 +20,7 @@ public interface IRuleSet {
public boolean erase2(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 adaptExt(MPair pair);

View File

@ -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<MPair> 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<Set<MPair>> 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<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
@ -169,9 +223,17 @@ public class RuleSet implements IRuleSet{
}
@Override
public MPair swap(MPair pair) {
// TODO Auto-generated method stub
return null;
public Optional<MPair> 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];

View File

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

View File

@ -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 + ")";
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

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);
}
}