forked from JavaTX/JavaCompilerCore
Merge branch 'bytecode' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into bytecode
This commit is contained in:
commit
b68107286a
@ -53,7 +53,7 @@ public class ClassGenerator extends ClassGen{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DHBWInstructionFactory getInstructionFactory() {
|
public DHBWInstructionFactory getInstructionFactory() {
|
||||||
return factory ;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,4 +158,8 @@ public class DHBWInstructionFactory extends InstructionFactory{
|
|||||||
public Attribute createSignatureAttribute(String signature) {
|
public Attribute createSignatureAttribute(String signature) {
|
||||||
return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool());
|
return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void resetStoreIndexes() {
|
||||||
|
storeIndexes = new HashMap<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -301,11 +301,7 @@ public class SourceFile
|
|||||||
* Erstellt die Finite Closure
|
* Erstellt die Finite Closure
|
||||||
* @return FC_TTO-Object, welches die Finite Closure repräsentiert
|
* @return FC_TTO-Object, welches die Finite Closure repräsentiert
|
||||||
*/
|
*/
|
||||||
// ino.end
|
|
||||||
// ino.method.makeFC.21403.definition
|
|
||||||
public FC_TTO makeFC( TypeAssumptions ass )
|
public FC_TTO makeFC( TypeAssumptions ass )
|
||||||
// ino.end
|
|
||||||
// ino.method.makeFC.21403.body
|
|
||||||
{
|
{
|
||||||
|
|
||||||
// Menge FC bilden
|
// Menge FC bilden
|
||||||
@ -770,7 +766,7 @@ public class SourceFile
|
|||||||
.map(i -> constraintsClone.elementAt(i))
|
.map(i -> constraintsClone.elementAt(i))
|
||||||
.<Menge<Pair>>collect(Menge::new, Menge::add, Menge::addAll));
|
.<Menge<Pair>>collect(Menge::new, Menge::add, Menge::addAll));
|
||||||
//Menge<Menge<Pair>> vecconstraintsclone = streamconstraintsclone.collect(Menge::new, Menge::add, Menge::addAll);
|
//Menge<Menge<Pair>> vecconstraintsclone = streamconstraintsclone.collect(Menge::new, Menge::add, Menge::addAll);
|
||||||
System.out.println();
|
//System.out.println();
|
||||||
//Schritt 4: Unifikation
|
//Schritt 4: Unifikation
|
||||||
Menge<Menge<Menge<Pair>>> vecunifyResult =
|
Menge<Menge<Menge<Pair>>> vecunifyResult =
|
||||||
//streamconstraintsclone.map(x -> Unify.unify(x, finiteClosure)).collect(Menge::new, Menge::add, Menge::addAll);
|
//streamconstraintsclone.map(x -> Unify.unify(x, finiteClosure)).collect(Menge::new, Menge::add, Menge::addAll);
|
||||||
@ -814,6 +810,7 @@ public class SourceFile
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Dann den Ergebnissen anfügen
|
//Dann den Ergebnissen anfügen
|
||||||
|
typinferenzLog.debug("\nErgebnis der Unifizierung:\n"+unifyResult, Section.TYPEINFERENCE);
|
||||||
result.addAll(unifyResult);
|
result.addAll(unifyResult);
|
||||||
|
|
||||||
typinferenzLog.debug("\nJavaFiles:\n", Section.TYPEINFERENCE);
|
typinferenzLog.debug("\nJavaFiles:\n", Section.TYPEINFERENCE);
|
||||||
@ -841,281 +838,7 @@ public class SourceFile
|
|||||||
if(!this.KlassenVektor.isEmpty())throw new TypeinferenceException("Fehler in Typinferierung", this.KlassenVektor.firstElement());
|
if(!this.KlassenVektor.isEmpty())throw new TypeinferenceException("Fehler in Typinferierung", this.KlassenVektor.firstElement());
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
/*
|
|
||||||
// HOTI: Nur zur Info.Ich habe den Loglevel auf Info geschaltet, damit
|
|
||||||
// in der GUI (Eclipse-Plugin) die Console nicht zugemüllt wird.
|
|
||||||
// Wers braucht kanns natürlich ausschalten
|
|
||||||
|
|
||||||
// inferencelog.setLevel(Level.INFO);
|
|
||||||
|
|
||||||
Menge<TypeinferenceResultSet> A = new Menge<TypeinferenceResultSet>();
|
|
||||||
|
|
||||||
TypeAssumptions basics;
|
|
||||||
|
|
||||||
basics = this.makeBasicAssumptions();
|
|
||||||
|
|
||||||
//A.addElement(basics); //auskommentiert von Andreas Stadelmeier
|
|
||||||
|
|
||||||
// PL 05-07-31 alle GenericTypeVars werden ueberprueft, ob sie nicht
|
|
||||||
// deklarierte Classen sind und dann ggfs. gewandelt.
|
|
||||||
for (int i = 0; i < this.KlassenVektor.size(); i++) {
|
|
||||||
Class tempKlasse = this.KlassenVektor.elementAt(i);
|
|
||||||
MyCompiler.wandleGeneric2RefType(tempKlasse.getContainedTypes(),
|
|
||||||
this.KlassenVektor);
|
|
||||||
if(tempKlasse.getSuperInterfaces()!=null){
|
|
||||||
for(int k=0;k<tempKlasse.getSuperInterfaces().size();k++){
|
|
||||||
MyCompiler.wandleGeneric2RefType(tempKlasse.getSuperInterfaces().elementAt(k).get_ParaList(),this.KlassenVektor);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < this.InterfaceVektor.size(); i++) {
|
|
||||||
Interface tempIntf = this.InterfaceVektor.elementAt(i);
|
|
||||||
MyCompiler.wandleGeneric2RefType(tempIntf.getContainedTypes(),
|
|
||||||
this.KlassenVektor);
|
|
||||||
}
|
|
||||||
|
|
||||||
// HOTI 04-13-06 Alle Methoden der Klassen überprüfen, ob sie als
|
|
||||||
// RefType deklarierte Attribute haben, die aber GenericTypeVars sind
|
|
||||||
// Bsp.:
|
|
||||||
// bei public E elementAt(i){...} wird E vorerst als RefType erkannt
|
|
||||||
|
|
||||||
for (int i = 0; i < this.KlassenVektor.size(); i++) {
|
|
||||||
Class tempKlasse = this.KlassenVektor.elementAt(i);
|
|
||||||
tempKlasse.wandleRefTypeAttributes2GenericAttributes();
|
|
||||||
}
|
|
||||||
for (int i = 0;i< this.InterfaceVektor.size(); i++){
|
|
||||||
Interface tempInterface = this.InterfaceVektor.elementAt(i);
|
|
||||||
tempInterface.wandleRefTypeAttributes2GenericAttributes();
|
|
||||||
}
|
|
||||||
|
|
||||||
// HOT 8.5.06 Wandelt alle Referenzen auf p.ex. Menge in de.dhbwstuttgart.typeinference.Menge
|
|
||||||
for (int i = 0; i < this.KlassenVektor.size(); i++) {
|
|
||||||
Class tempKlasse = this.KlassenVektor.elementAt(i);
|
|
||||||
MyCompiler.makeRefTypesFullyQualified(tempKlasse.getContainedTypes(), this.imports);
|
|
||||||
String newSuperclass=MyCompiler.getFullyQualifiedNameFromClassname(tempKlasse.get_Superclass_Name(),this.imports);
|
|
||||||
if(newSuperclass!=null){
|
|
||||||
// Hier nicht setUsedID, sondern nur den Namen updaten. Sonst gehen die Parameter der Superklasse verloren
|
|
||||||
tempKlasse.superclassid.name=UsedId.createFromQualifiedName(newSuperclass,-1).name;
|
|
||||||
}
|
|
||||||
if(tempKlasse.getSuperInterfaces()!=null && tempKlasse.getSuperInterfaces().size()>0){
|
|
||||||
for(int j=0;j<tempKlasse.getSuperInterfaces().size();j++){
|
|
||||||
UsedId uid=tempKlasse.getSuperInterfaces().elementAt(j);
|
|
||||||
String newSuperif=MyCompiler.getFullyQualifiedNameFromClassname(uid.getQualifiedName(),this.imports);
|
|
||||||
if(newSuperif!=null){
|
|
||||||
UsedId newuid=UsedId.createFromQualifiedName(newSuperif,uid.getOffset());
|
|
||||||
uid.name=newuid.name;
|
|
||||||
}
|
|
||||||
MyCompiler.makeRefTypesFullyQualified(uid.get_ParaList(),this.imports);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(int j=0;j<tempKlasse.getUsedIdsToCheck().size();j++){
|
|
||||||
UsedId id=tempKlasse.getUsedIdsToCheck().elementAt(j);
|
|
||||||
String newClassname=MyCompiler.getFullyQualifiedNameFromClassname(id.getQualifiedName(),this.imports);
|
|
||||||
if(newClassname!=null)
|
|
||||||
id.name=UsedId.createFromQualifiedName(newClassname,-1).name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < this.InterfaceVektor.size(); i++) {
|
|
||||||
Interface tempIntf = this.InterfaceVektor.elementAt(i);
|
|
||||||
MyCompiler.makeRefTypesFullyQualified(tempIntf.getContainedTypes(), this.imports);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inferencelog.info("Rufe \"SourceFile.makeFC()\"...");
|
|
||||||
inferencelog.info("������������������������������������");
|
|
||||||
FC_TTO finiteClosure = this.makeFC();
|
|
||||||
inferencelog.info("������������������������������������");
|
|
||||||
inferencelog.info("Bin aus \"SourceFile.makeFC()\" zur�ck.");
|
|
||||||
this.removeBasicAssumptions();
|
|
||||||
|
|
||||||
// PL 05-08-02
|
|
||||||
// verschoben nach Class.java am Ende von TRProg
|
|
||||||
// this.addClassNamesAndGenericsToRR(basics);
|
|
||||||
|
|
||||||
// HOTI 04-22-06 Fuer jede Klasse die Methoden und Instanzvariablen in die Assumptions aufnehmen
|
|
||||||
//
|
|
||||||
Iterator<Interface> intf_it = InterfaceVektor.iterator();
|
|
||||||
while (intf_it.hasNext()) {
|
|
||||||
Interface intf = intf_it.next();
|
|
||||||
|
|
||||||
// HOTI In diesem Moment gibt es nur _eine_ potentielle CTypeReconstructionResult, d.h.
|
|
||||||
// dort können die Definitionen der Interfaces (Methodintersectiontypes, FieldDecls) abgelegt werden
|
|
||||||
|
|
||||||
|
|
||||||
//intf.addThisToAssumptions(basics);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fuer jede Klasse die Assumptions der öffentlichen Felder zusammentragen:
|
|
||||||
TypeAssumptions publicFieldsAssumptions = new TypeAssumptions();
|
|
||||||
for(Class cl : KlassenVektor){
|
|
||||||
publicFieldsAssumptions.add(cl.getPublicFieldAssumptions());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Die BasicAssumptions anfügen:
|
|
||||||
publicFieldsAssumptions.add(this.getBasicAssumptions());
|
|
||||||
|
|
||||||
// Fuer jede Klasse separat den TRA aufrufen
|
|
||||||
Iterator<Class> class_it = KlassenVektor.iterator();
|
|
||||||
while (class_it.hasNext()) {
|
|
||||||
Class cl = class_it.next();
|
|
||||||
CSupportData supportData = new CSupportData(finiteClosure, A, cl.getName(), cl.get_ParaList());
|
|
||||||
inferencelog.info("Rufe " + cl.getName() + ".TRProg()...");
|
|
||||||
A.addAll(cl.typeReconstruction(supportData, publicFieldsAssumptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
return A;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
// ino.end
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt die Basic Assumptions (siehe MakeBasicAssumptions) als AssumptionSet
|
|
||||||
* @return
|
|
||||||
|
|
||||||
@Deprecated //angefügt von Andreas Stadelmeier. Grund: Die Funktion wurde neu als makeBasicAssumptionsFromJRE angelegt
|
|
||||||
private TypeAssumptions getBasicAssumptions() {
|
|
||||||
TypeAssumptions ret = new TypeAssumptions(null);
|
|
||||||
|
|
||||||
// AB hier der Teil aus makeBasicAssumptionsFromJRE:
|
|
||||||
Menge<UsedId> doneImports=new Menge<UsedId>();
|
|
||||||
|
|
||||||
//CTypeReconstructionResult basicAssumptions = new CTypeReconstructionResult(null);
|
|
||||||
|
|
||||||
Modifiers mod = new Modifiers();
|
|
||||||
mod.addModifier(new Public());
|
|
||||||
|
|
||||||
|
|
||||||
// Für jede einzelne Klasse
|
|
||||||
while (imports.size()>0) {
|
|
||||||
UsedId importDecl = imports.get(0);
|
|
||||||
|
|
||||||
// Properties laden
|
|
||||||
java.lang.Class<?> x;
|
|
||||||
try {
|
|
||||||
x = java.lang.Class.forName(importDecl.getQualifiedName().toString());
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
throw new CTypeReconstructionException("Fehlerhafte Import-Declaration: "+e.getMessage(),importDecl);
|
|
||||||
}
|
|
||||||
|
|
||||||
java.lang.reflect.Field[] fields=x.getDeclaredFields();
|
|
||||||
java.lang.reflect.Method[] methods=x.getDeclaredMethods();
|
|
||||||
java.lang.reflect.Constructor[] constructors=x.getConstructors();
|
|
||||||
java.lang.reflect.TypeVariable[] tvs=x.getTypeParameters();
|
|
||||||
//String className=x.getSimpleName();
|
|
||||||
String className=x.getName();
|
|
||||||
|
|
||||||
// Generische Typen erzeugen
|
|
||||||
|
|
||||||
|
|
||||||
Hashtable<String,GenericTypeVar> jreSpiderRegistry=new Hashtable<String,GenericTypeVar>();
|
|
||||||
Menge<GenericTypeVar> typeGenPara = new Menge<GenericTypeVar>();
|
|
||||||
for(int j=0;j<tvs.length;j++){
|
|
||||||
GenericTypeVar gtv=new GenericTypeVar(tvs[j].getName(),-1);
|
|
||||||
typeGenPara.addElement(gtv);
|
|
||||||
jreSpiderRegistry.put(tvs[j].getName(),gtv);
|
|
||||||
}
|
|
||||||
|
|
||||||
//BasicAssumptionClass myCl = new BasicAssumptionClass(className, mod);
|
|
||||||
|
|
||||||
if(typeGenPara.size()>0){
|
|
||||||
//basicAssumptions.addGenericTypeVars(className, typeGenPara);
|
|
||||||
//myCl.set_ParaList((Menge)typeGenPara);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if(x.getSuperclass()!=null){
|
|
||||||
//boolean isObject=x.getSuperclass().getSimpleName().equalsIgnoreCase("Object");
|
|
||||||
boolean isObject=x.getSuperclass().getName().equalsIgnoreCase("java.lang.Object");
|
|
||||||
boolean isBaseType=isBaseType(className);
|
|
||||||
|
|
||||||
//if((!isObject || READ_OBJECT_SUPERCLASSES_FROM_JRE) && (!isBaseType|| READ_BASE_TYPE_SUPERCLASSES_FROM_JRE))
|
|
||||||
if (((!isObject || READ_OBJECT_SUPERCLASSES_FROM_JRE) && READ_IMPORTED_SUPERCLASSES_FROM_JRE) //eingefuegt 07-08-11
|
|
||||||
|| (isBaseType && READ_BASE_TYPE_SUPERCLASSES_FROM_JRE))
|
|
||||||
{
|
|
||||||
String superclassFullyQualifiedName = x.getSuperclass().getCanonicalName();
|
|
||||||
//Andere Methode, da Menge.contains bei Strings nicht richtig vergleicht.
|
|
||||||
if(!containsString(imports,superclassFullyQualifiedName) && !containsString(doneImports,superclassFullyQualifiedName)){
|
|
||||||
imports.addElement(UsedId.createFromQualifiedName(superclassFullyQualifiedName,-1));
|
|
||||||
}
|
|
||||||
//UsedId ui = new UsedId();
|
|
||||||
//ui.set_Name(x.getSuperclass().getSimpleName());
|
|
||||||
UsedId ui=UsedId.createFromQualifiedName(x.getSuperclass().getName(),-1);
|
|
||||||
java.lang.Class superClass=x.getSuperclass();
|
|
||||||
java.lang.reflect.TypeVariable[] superclassTVS=superClass.getTypeParameters();
|
|
||||||
Menge<Type> supertypeGenPara = new Menge<Type>();
|
|
||||||
for(int tvi=0;tvi<superclassTVS.length;tvi++){
|
|
||||||
GenericTypeVar newGTV=new GenericTypeVar(superclassTVS[tvi].getName(),-1);
|
|
||||||
supertypeGenPara.addElement(newGTV);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(supertypeGenPara.size()==0){
|
|
||||||
supertypeGenPara=null;
|
|
||||||
}
|
|
||||||
ui.set_ParaList(supertypeGenPara);
|
|
||||||
ui.vParaOrg=supertypeGenPara;
|
|
||||||
//myCl.set_UsedId(ui);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//this.addElement(myCl);
|
|
||||||
|
|
||||||
//basicAssumptions.addClassName(className);
|
|
||||||
|
|
||||||
for(int j=0;j<fields.length;j++){
|
|
||||||
if(java.lang.reflect.Modifier.isPublic(fields[j].getModifiers())){
|
|
||||||
//CInstVarTypeAssumption instVar = new CInstVarTypeAssumption(className, fields[j].getName(), new RefType(fields[j].getType().getSimpleName()), MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>());
|
|
||||||
CInstVarTypeAssumption instVar = new CInstVarTypeAssumption(className, fields[j].getName(), new RefType(fields[j].getType().getName(),-1), MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>());
|
|
||||||
//basicAssumptions.addFieldOrLocalVarAssumption(instVar);
|
|
||||||
//ret.add(instVar); //auskommentiert von Andreas Stadelmeier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(int j=0;j<methods.length;j++){
|
|
||||||
if(java.lang.reflect.Modifier.isPublic(methods[j].getModifiers())){
|
|
||||||
String methodName=methods[j].getName();
|
|
||||||
java.lang.reflect.Type genericReturnType=methods[j].getGenericReturnType();
|
|
||||||
Type returnType=createTypeFromJavaGenericType(genericReturnType,methods[j].getReturnType(),jreSpiderRegistry);
|
|
||||||
|
|
||||||
java.lang.reflect.Type[] gpt=methods[j].getGenericParameterTypes();
|
|
||||||
java.lang.Class[] pt=methods[j].getParameterTypes();
|
|
||||||
|
|
||||||
CMethodTypeAssumption method = new CMethodTypeAssumption(new RefType(className, 0), methodName, returnType, pt.length,MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>(),null);
|
|
||||||
|
|
||||||
|
|
||||||
for(int k=0;k<gpt.length;k++){
|
|
||||||
Type type=createTypeFromJavaGenericType(gpt[k],pt[k],jreSpiderRegistry);
|
|
||||||
// Fixme HOTI beachte overloaded id
|
|
||||||
method.addParaAssumption(new CParaTypeAssumption(className, methodName, pt.length,0,type.getName().toString(), type, MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>()));
|
|
||||||
}
|
|
||||||
//basicAssumptions.addMethodIntersectionType(new CIntersectionType(method));
|
|
||||||
//ret.add(method); //auskommentiert von Andreas Stadelmeier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int j=0;j<constructors.length;j++){
|
|
||||||
if(java.lang.reflect.Modifier.isPublic(constructors[j].getModifiers())){
|
|
||||||
String methodName="<init>";
|
|
||||||
CMethodTypeAssumption constructor = new CMethodTypeAssumption(new RefType(className, 0), methodName, new RefType(className,-1), constructors[j].getParameterTypes().length,MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>(),null);
|
|
||||||
for(int k=0;k<constructors[j].getParameterTypes().length;k++){
|
|
||||||
String paraType=constructors[j].getParameterTypes()[k].getName();
|
|
||||||
//String paraType=constructors[j].getParameterTypes()[k].getSimpleName();
|
|
||||||
// Fixme HOTI beachte overloaded id
|
|
||||||
constructor.addParaAssumption(new CParaTypeAssumption(className, methodName, constructors[j].getParameterTypes().length,0,paraType, new RefType(paraType,-1), MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Menge<Integer>()));
|
|
||||||
}
|
|
||||||
//basicAssumptions.addMethodIntersectionType(new CIntersectionType(constructor));
|
|
||||||
//ret.add(constructor); //auskommentiert von Andreas Stadelmeier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
imports.removeElement(importDecl);
|
|
||||||
doneImports.addElement(importDecl);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
imports.addAll(doneImports);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erstellt die Assumptions der standardmäÃig importierten Packages (java.lang.) sowie der von imports übergebenen Klassen zusammen.
|
* Erstellt die Assumptions der standardmäÃig importierten Packages (java.lang.) sowie der von imports übergebenen Klassen zusammen.
|
||||||
@ -1123,7 +846,7 @@ public class SourceFile
|
|||||||
* @param withSuptypes - Gibt an, ob auch die subklassen der Packages den Assumptions angefügt werden sollen.
|
* @param withSuptypes - Gibt an, ob auch die subklassen der Packages den Assumptions angefügt werden sollen.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private TypeAssumptions makeBasicAssumptionsFromJRE(Menge<UsedId> imports, boolean withSubtypes)
|
public TypeAssumptions makeBasicAssumptionsFromJRE(Menge<UsedId> imports, boolean withSubtypes)
|
||||||
// ino.end
|
// ino.end
|
||||||
// ino.method.makeBasicAssumptionsFromJRE.21409.body
|
// ino.method.makeBasicAssumptionsFromJRE.21409.body
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.factory;
|
package de.dhbwstuttgart.syntaxtree.factory;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.myexception.NotImplementedException;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
|
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
||||||
@ -8,34 +12,71 @@ import de.dhbwstuttgart.syntaxtree.type.Type;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
||||||
import de.dhbwstuttgart.typeinference.Menge;
|
import de.dhbwstuttgart.typeinference.Menge;
|
||||||
|
import de.dhbwstuttgart.typeinference.assumptions.ClassAssumption;
|
||||||
|
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
|
||||||
|
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.UnifyType;
|
||||||
|
|
||||||
public class UnifyTypeFactory {
|
public class UnifyTypeFactory {
|
||||||
|
|
||||||
public RefType GetSimpleType(String name, Type... parameters) {
|
public static FiniteClosure generateFC(TypeAssumptions fromAss){
|
||||||
if(parameters.length == 0)
|
HashSet<MPair> pairs = new HashSet<>();
|
||||||
return new RefType(name, null, 0);
|
for(ClassAssumption cAss : fromAss.getClassAssumptions()){
|
||||||
|
UnifyType tl = UnifyTypeFactory.convert(cAss.getAssumedClass().getType());
|
||||||
Menge<Type> typeParams = new Menge<Type>();
|
Type superClass = cAss.getAssumedClass().getSuperClass();
|
||||||
|
if(superClass != null){
|
||||||
for(Type t : parameters)
|
UnifyType tr = UnifyTypeFactory.convert(superClass);
|
||||||
typeParams.add(t);
|
pairs.add(smaller(tl, tr));
|
||||||
|
}
|
||||||
return new RefType(name, typeParams, null, 0);
|
}
|
||||||
|
return new FiniteClosure(pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExtendsWildcardType GetExtendsType(ObjectType extendedType) {
|
public static MPair smaller(UnifyType tl, UnifyType tr){
|
||||||
return new ExtendsWildcardType(extendedType);
|
return new MPair(tl, tr,MPair.PairOperator.SMALLER);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuperWildcardType GetSuperType(ObjectType superedType) {
|
public static UnifyType convert(Type t){
|
||||||
return new SuperWildcardType(superedType);
|
//Es wurde versucht ein Typ umzuwandeln, welcher noch nicht von der Factory abgedeckt ist
|
||||||
|
if(t instanceof GenericTypeVar){
|
||||||
|
return UnifyTypeFactory.convert((GenericTypeVar)t);
|
||||||
|
}
|
||||||
|
System.out.println("Der Typ "+t+" kann nicht umgewandelt werden");
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public WildcardType GetWildcardType() {
|
public static UnifyType convert(RefType t){
|
||||||
return new WildcardType(null, null, 0);
|
UnifyType ret;
|
||||||
|
if(t.getParaList() != null && t.getParaList().size() > 0){
|
||||||
|
Menge<UnifyType> params = new Menge<>();
|
||||||
|
for(Type pT : t.getParaList()){
|
||||||
|
params.add(UnifyTypeFactory.convert(pT));
|
||||||
|
}
|
||||||
|
ret = new SimpleType(t.get_Name(),params.toArray());
|
||||||
|
}else{
|
||||||
|
ret = new SimpleType(t.get_Name());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypePlaceholder GetTypePlaceholder(String name) {
|
public static UnifyType convert(TypePlaceholder tph){
|
||||||
return TypePlaceholder.backdoorCreate(name);
|
return new PlaceholderType(tph.get_Name());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UnifyType convert(ExtendsWildcardType t){
|
||||||
|
return new ExtendsType(UnifyTypeFactory.convert(t.get_ExtendsType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UnifyType convert(SuperWildcardType t){
|
||||||
|
return new SuperType(UnifyTypeFactory.convert(t.get_SuperType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UnifyType convert(GenericTypeVar t){
|
||||||
|
return new SimpleType(t.get_Name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import org.apache.commons.bcel6.generic.InstructionList;
|
|||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.Menge;
|
import de.dhbwstuttgart.typeinference.Menge;
|
||||||
import de.dhbwstuttgart.bytecode.ClassGenerator;
|
import de.dhbwstuttgart.bytecode.ClassGenerator;
|
||||||
|
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
|
||||||
import de.dhbwstuttgart.logger.Logger;
|
import de.dhbwstuttgart.logger.Logger;
|
||||||
import de.dhbwstuttgart.myexception.JVMCodeException;
|
import de.dhbwstuttgart.myexception.JVMCodeException;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
@ -324,7 +325,8 @@ public class MethodCall extends Expr
|
|||||||
@Override
|
@Override
|
||||||
public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) {
|
public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) {
|
||||||
InstructionList il = new InstructionList();
|
InstructionList il = new InstructionList();
|
||||||
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool());
|
DHBWInstructionFactory _factory = cg.getInstructionFactory();
|
||||||
|
_factory.resetStoreIndexes();
|
||||||
|
|
||||||
il.append(receiver.get_Expr().genByteCode(cg, rs));
|
il.append(receiver.get_Expr().genByteCode(cg, rs));
|
||||||
|
|
||||||
@ -343,6 +345,8 @@ public class MethodCall extends Expr
|
|||||||
argumentTypen = new org.apache.commons.bcel6.generic.Type[this.getArgumentList().size()];
|
argumentTypen = new org.apache.commons.bcel6.generic.Type[this.getArgumentList().size()];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(Expr argument : this.arglist.expr){
|
for(Expr argument : this.arglist.expr){
|
||||||
|
_factory.getStoreIndex(argument.get_Name());
|
||||||
|
|
||||||
argumentTypen[i] = argument.getType().getBytecodeType(cg, rs);
|
argumentTypen[i] = argument.getType().getBytecodeType(cg, rs);
|
||||||
//Das Argument auf den Stack legen:
|
//Das Argument auf den Stack legen:
|
||||||
il.append(argument.genByteCode(cg, rs));
|
il.append(argument.genByteCode(cg, rs));
|
||||||
|
@ -89,33 +89,6 @@ public class TypeAssumptions {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ermittelt alle bekannten Methoden mit dem Namen withName
|
|
||||||
* @param withName
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public Menge<MethodAssumption> getMethods2(String withName){
|
|
||||||
//TODO: Implementieren
|
|
||||||
return new Menge<MethodAssumption>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Liefert den Typ einer lokalen Variable. Zuerst werden die Parameter dieses AssumptionSets durchsucht, dann die lokalen Variablen. AnschlieÃend die Felder der, "this" repräsentierenden Klasse
|
|
||||||
* @param withName
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public Type getTypeOfLocalVar2(String withName){
|
|
||||||
//TODO: Implementieren
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sucht nach Assumptions zu einer Methode mit dem Namen methodName und parameterCount Parametern.
|
* Sucht nach Assumptions zu einer Methode mit dem Namen methodName und parameterCount Parametern.
|
||||||
* @param methodName
|
* @param methodName
|
||||||
|
@ -3,11 +3,11 @@ package de.dhbwstuttgart.typeinference.unify.interfaces;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
|
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
|
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SuperType;
|
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
|
||||||
public interface IFiniteClosure {
|
public interface IFiniteClosure {
|
||||||
|
|
||||||
@ -15,39 +15,40 @@ public interface IFiniteClosure {
|
|||||||
* Returns all types of the finite closure that are subtypes of the argument.
|
* Returns all types of the finite closure that are subtypes of the argument.
|
||||||
* @return The set of subtypes of the argument.
|
* @return The set of subtypes of the argument.
|
||||||
*/
|
*/
|
||||||
public Set<Type> smaller(Type type);
|
public Set<UnifyType> smaller(UnifyType type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all types of the finite closure that are supertypes of the argument.
|
* Returns all types of the finite closure that are supertypes of the argument.
|
||||||
* @return The set of supertypes of the argument.
|
* @return The set of supertypes of the argument.
|
||||||
*/
|
*/
|
||||||
public Set<Type> greater(Type type);
|
public Set<UnifyType> greater(UnifyType type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wo passt Type rein?
|
* Wo passt Type rein?
|
||||||
* @param type
|
* @param type
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<Type> grArg(Type type);
|
public Set<UnifyType> grArg(UnifyType type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Was passt in Type rein?
|
* Was passt in Type rein?
|
||||||
* @param type
|
* @param type
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<Type> smArg(Type type);
|
public Set<UnifyType> smArg(UnifyType type);
|
||||||
|
|
||||||
public Set<Type> grArg(SimpleType type);
|
public Set<UnifyType> grArg(SimpleType type);
|
||||||
public Set<Type> smArg(SimpleType type);
|
public Set<UnifyType> smArg(SimpleType type);
|
||||||
|
|
||||||
public Set<Type> grArg(ExtendsType type);
|
public Set<UnifyType> grArg(ExtendsType type);
|
||||||
public Set<Type> smArg(ExtendsType type);
|
public Set<UnifyType> smArg(ExtendsType type);
|
||||||
|
|
||||||
public Set<Type> grArg(SuperType type);
|
public Set<UnifyType> grArg(SuperType type);
|
||||||
public Set<Type> smArg(SuperType type);
|
public Set<UnifyType> smArg(SuperType type);
|
||||||
|
|
||||||
public Set<Type> grArg(PlaceholderType type);
|
public Set<UnifyType> grArg(PlaceholderType type);
|
||||||
public Set<Type> smArg(PlaceholderType type);
|
public Set<UnifyType> smArg(PlaceholderType type);
|
||||||
|
|
||||||
public Optional<Type> getGenericType(String typeName);
|
public Optional<UnifyType> getGenericType(String typeName);
|
||||||
|
public Set<UnifyType> getAllTypes(String typeName);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,7 @@ package de.dhbwstuttgart.typeinference.unify.interfaces;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
|
||||||
|
|
||||||
public interface IRuleSet {
|
public interface IRuleSet {
|
||||||
|
|
||||||
@ -27,5 +26,5 @@ public interface IRuleSet {
|
|||||||
public Optional<MPair> adaptExt(MPair pair);
|
public Optional<MPair> adaptExt(MPair pair);
|
||||||
public Optional<MPair> adaptSup(MPair pair);
|
public Optional<MPair> adaptSup(MPair pair);
|
||||||
|
|
||||||
public Set<MPair> subst(Set<MPair> pair);
|
public Optional<Set<MPair>> subst(Set<MPair> pair);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.Menge;
|
|
||||||
|
|
||||||
public interface ITypeMapper<T> {
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +1,23 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.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
|
* @author Florian Steurer
|
||||||
*/
|
*/
|
||||||
public interface IUnify {
|
public interface IUnify {
|
||||||
public Unifier unify(Set<MPair> terms);
|
|
||||||
|
public Optional<Unifier> unify(Set<UnifyType> terms);
|
||||||
|
|
||||||
|
default public Optional<Unifier> unify(UnifyType... terms) {
|
||||||
|
return unify(Arrays.stream(terms).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,77 @@
|
|||||||
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An extends wildcard type "? extends T".
|
||||||
|
*/
|
||||||
|
public final class ExtendsType extends UnifyType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The extended type
|
||||||
|
*/
|
||||||
|
private UnifyType extendedType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new extends wildcard type.
|
||||||
|
* @param extendedType The extended type e.g. Integer in "? extends Integer"
|
||||||
|
*/
|
||||||
|
public ExtendsType(UnifyType 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 UnifyType getExtendedType() {
|
||||||
|
return extendedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeParams getTypeParams() {
|
||||||
|
return extendedType.getTypeParams();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnifyType setTypeParams(TypeParams newTp) {
|
||||||
|
return new ExtendsType(extendedType.setTypeParams(newTp));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Set<UnifyType> smArg(IFiniteClosure fc) {
|
||||||
|
return fc.smArg(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Set<UnifyType> grArg(IFiniteClosure fc) {
|
||||||
|
return fc.grArg(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
UnifyType apply(Unifier unif) {
|
||||||
|
return new ExtendsType(extendedType.apply(unif));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return extendedType.hashCode() + 17;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(!(obj instanceof ExtendsType))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ExtendsType other = (ExtendsType) obj;
|
||||||
|
return other.getExtendedType().equals(extendedType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "? extends " + extendedType;
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +1,22 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
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 {
|
public class FiniteClosure implements IFiniteClosure {
|
||||||
|
|
||||||
private HashMap<Type, Node<Type>> inheritanceGraph;
|
private HashMap<UnifyType, Node<UnifyType>> inheritanceGraph;
|
||||||
private HashMap<String, HashSet<Node<Type>>> strInheritanceGraph;
|
private HashMap<String, HashSet<Node<UnifyType>>> strInheritanceGraph;
|
||||||
|
|
||||||
public FiniteClosure(Set<MPair> pairs) {
|
public FiniteClosure(Set<MPair> pairs) {
|
||||||
|
|
||||||
inheritanceGraph = new HashMap<Type, Node<Type>>();
|
inheritanceGraph = new HashMap<UnifyType, Node<UnifyType>>();
|
||||||
|
|
||||||
// Build the transitive closure of the inheritance tree
|
// Build the transitive closure of the inheritance tree
|
||||||
for(MPair pair : pairs) {
|
for(MPair pair : pairs) {
|
||||||
@ -24,12 +25,12 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
|
|
||||||
// Add nodes if not already in the graph
|
// Add nodes if not already in the graph
|
||||||
if(!inheritanceGraph.containsKey(pair.getLhsType()))
|
if(!inheritanceGraph.containsKey(pair.getLhsType()))
|
||||||
inheritanceGraph.put(pair.getLhsType(), new Node<Type>(pair.getLhsType()));
|
inheritanceGraph.put(pair.getLhsType(), new Node<UnifyType>(pair.getLhsType()));
|
||||||
if(!inheritanceGraph.containsKey(pair.getRhsType()))
|
if(!inheritanceGraph.containsKey(pair.getRhsType()))
|
||||||
inheritanceGraph.put(pair.getRhsType(), new Node<Type>(pair.getRhsType()));
|
inheritanceGraph.put(pair.getRhsType(), new Node<UnifyType>(pair.getRhsType()));
|
||||||
|
|
||||||
Node<Type> childNode = inheritanceGraph.get(pair.getLhsType());
|
Node<UnifyType> childNode = inheritanceGraph.get(pair.getLhsType());
|
||||||
Node<Type> parentNode = inheritanceGraph.get(pair.getRhsType());
|
Node<UnifyType> parentNode = inheritanceGraph.get(pair.getRhsType());
|
||||||
|
|
||||||
// Add edge
|
// Add edge
|
||||||
parentNode.AddDescendant(childNode);
|
parentNode.AddDescendant(childNode);
|
||||||
@ -42,7 +43,7 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
// Build the alternative representation with strings as keys
|
// Build the alternative representation with strings as keys
|
||||||
|
|
||||||
strInheritanceGraph = new HashMap<>();
|
strInheritanceGraph = new HashMap<>();
|
||||||
for(Type key : inheritanceGraph.keySet()) {
|
for(UnifyType key : inheritanceGraph.keySet()) {
|
||||||
if(!strInheritanceGraph.containsKey(key.getName()))
|
if(!strInheritanceGraph.containsKey(key.getName()))
|
||||||
strInheritanceGraph.put(key.getName(), new HashSet<>());
|
strInheritanceGraph.put(key.getName(), new HashSet<>());
|
||||||
|
|
||||||
@ -55,11 +56,11 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
* @return The set of subtypes of the argument.
|
* @return The set of subtypes of the argument.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smaller(Type type) {
|
public Set<UnifyType> smaller(UnifyType type) {
|
||||||
if(!inheritanceGraph.containsKey(type))
|
if(!inheritanceGraph.containsKey(type))
|
||||||
return new HashSet<>();
|
return new HashSet<>();
|
||||||
|
|
||||||
Set<Type> result = inheritanceGraph.get(type).getContentOfDescendants();
|
Set<UnifyType> result = inheritanceGraph.get(type).getContentOfDescendants();
|
||||||
result.add(type);
|
result.add(type);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -70,26 +71,26 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
* @return The set of supertypes of the argument.
|
* @return The set of supertypes of the argument.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> greater(Type type) {
|
public Set<UnifyType> greater(UnifyType type) {
|
||||||
if(!inheritanceGraph.containsKey(type))
|
if(!inheritanceGraph.containsKey(type))
|
||||||
return new HashSet<>();
|
return new HashSet<>();
|
||||||
|
|
||||||
Set<Type> result = inheritanceGraph.get(type).getContentOfPredecessors();
|
Set<UnifyType> result = inheritanceGraph.get(type).getContentOfPredecessors();
|
||||||
result.add(type);
|
result.add(type);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(Type type) {
|
public Set<UnifyType> grArg(UnifyType type) {
|
||||||
return type.grArg(this);
|
return type.grArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(SimpleType type) {
|
public Set<UnifyType> grArg(SimpleType type) {
|
||||||
if(!inheritanceGraph.containsKey(type))
|
if(!inheritanceGraph.containsKey(type))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
result.add(type);
|
result.add(type);
|
||||||
smaller(type).forEach(x -> result.add(new SuperType(x)));
|
smaller(type).forEach(x -> result.add(new SuperType(x)));
|
||||||
@ -99,13 +100,13 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(ExtendsType type) {
|
public Set<UnifyType> grArg(ExtendsType type) {
|
||||||
if(!inheritanceGraph.containsKey(type.getExtendedType()))
|
if(!inheritanceGraph.containsKey(type.getExtendedType()))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
Type t = type.getExtendedType();
|
UnifyType t = type.getExtendedType();
|
||||||
|
|
||||||
greater(t).forEach(x -> result.add(new ExtendsType(x)));
|
greater(t).forEach(x -> result.add(new ExtendsType(x)));
|
||||||
|
|
||||||
@ -113,13 +114,13 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(SuperType type) {
|
public Set<UnifyType> grArg(SuperType type) {
|
||||||
if(!inheritanceGraph.containsKey(type.getSuperedType()))
|
if(!inheritanceGraph.containsKey(type.getSuperedType()))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
Type t = type.getSuperedType();
|
UnifyType t = type.getSuperedType();
|
||||||
|
|
||||||
smaller(t).forEach(x -> result.add(new SuperType(x)));
|
smaller(t).forEach(x -> result.add(new SuperType(x)));
|
||||||
|
|
||||||
@ -127,21 +128,21 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(PlaceholderType type) {
|
public Set<UnifyType> grArg(PlaceholderType type) {
|
||||||
return new HashSet<>();
|
return new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(Type type) {
|
public Set<UnifyType> smArg(UnifyType type) {
|
||||||
return type.smArg(this);
|
return type.smArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(SimpleType type) {
|
public Set<UnifyType> smArg(SimpleType type) {
|
||||||
if(!inheritanceGraph.containsKey(type))
|
if(!inheritanceGraph.containsKey(type))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
result.add(type);
|
result.add(type);
|
||||||
smaller(type).forEach(x -> result.add(new ExtendsType(x)));
|
smaller(type).forEach(x -> result.add(new ExtendsType(x)));
|
||||||
@ -149,13 +150,13 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Type> smArg(ExtendsType type) {
|
public Set<UnifyType> smArg(ExtendsType type) {
|
||||||
if(!inheritanceGraph.containsKey(type.getExtendedType()))
|
if(!inheritanceGraph.containsKey(type.getExtendedType()))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
Type t = type.getExtendedType();
|
UnifyType t = type.getExtendedType();
|
||||||
|
|
||||||
result.add(t);
|
result.add(t);
|
||||||
smaller(t).forEach(x -> {
|
smaller(t).forEach(x -> {
|
||||||
@ -168,13 +169,13 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(SuperType type) {
|
public Set<UnifyType> smArg(SuperType type) {
|
||||||
if(!inheritanceGraph.containsKey(type.getSuperedType()))
|
if(!inheritanceGraph.containsKey(type.getSuperedType()))
|
||||||
return new HashSet<Type>();
|
return new HashSet<UnifyType>();
|
||||||
|
|
||||||
Set<Type> result = new HashSet<Type>();
|
Set<UnifyType> result = new HashSet<UnifyType>();
|
||||||
|
|
||||||
Type t = type.getSuperedType();
|
UnifyType t = type.getSuperedType();
|
||||||
|
|
||||||
result.add(t);
|
result.add(t);
|
||||||
greater(t).forEach(x -> {
|
greater(t).forEach(x -> {
|
||||||
@ -186,23 +187,30 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(PlaceholderType type) {
|
public Set<UnifyType> smArg(PlaceholderType type) {
|
||||||
return new HashSet<>();
|
return new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Type> getGenericType(String typeName) {
|
public Optional<UnifyType> getGenericType(String typeName) {
|
||||||
if(!strInheritanceGraph.containsKey(typeName))
|
if(!strInheritanceGraph.containsKey(typeName))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
HashSet<Node<Type>> candidates = strInheritanceGraph.get(typeName);
|
HashSet<Node<UnifyType>> candidates = strInheritanceGraph.get(typeName);
|
||||||
|
|
||||||
for(Node<Type> node : candidates) {
|
for(Node<UnifyType> node : candidates) {
|
||||||
Type candidate = node.getContent();
|
UnifyType candidate = node.getContent();
|
||||||
if(candidate.getTypeParams().arePlaceholders())
|
if(candidate.getTypeParams().arePlaceholders())
|
||||||
return Optional.of(candidate);
|
return Optional.of(candidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<UnifyType> getAllTypes(String typeName) {
|
||||||
|
if(!strInheritanceGraph.containsKey(typeName))
|
||||||
|
return new HashSet<>();
|
||||||
|
return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
public class MPair {
|
public class MPair {
|
||||||
|
|
||||||
@ -26,8 +26,8 @@ public class MPair {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private Type lhs;
|
private UnifyType lhs;
|
||||||
private Type rhs;
|
private UnifyType rhs;
|
||||||
private PairOperator pairOp;
|
private PairOperator pairOp;
|
||||||
|
|
||||||
/*public MPair(Type t1, Type t2) {
|
/*public MPair(Type t1, Type t2) {
|
||||||
@ -36,17 +36,17 @@ public class MPair {
|
|||||||
pairOp = PairOperator.SMALLER;
|
pairOp = PairOperator.SMALLER;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
public MPair(Type t1, Type t2, PairOperator op) {
|
public MPair(UnifyType t1, UnifyType t2, PairOperator op) {
|
||||||
lhs = t1;
|
lhs = t1;
|
||||||
rhs = t2;
|
rhs = t2;
|
||||||
pairOp = op;
|
pairOp = op;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getLhsType() {
|
public UnifyType getLhsType() {
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getRhsType() {
|
public UnifyType getRhsType() {
|
||||||
return rhs;
|
return rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,11 +72,11 @@ public class MPair {
|
|||||||
* @param subst The type replacing t.
|
* @param subst The type replacing t.
|
||||||
* @return A pair where occurrences of t are replaced by subst.
|
* @return A pair where occurrences of t are replaced by subst.
|
||||||
*/
|
*/
|
||||||
public MPair substitute(Type t, Type subst) {
|
public MPair substitute(UnifyType t, UnifyType subst) {
|
||||||
Type newlhs = lhs;
|
UnifyType newlhs = lhs;
|
||||||
if(lhs.equals(t)) newlhs = subst;
|
if(lhs.equals(t)) newlhs = subst;
|
||||||
|
|
||||||
Type newrhs = rhs;
|
UnifyType newrhs = rhs;
|
||||||
if(rhs.equals(t)) newrhs = subst;
|
if(rhs.equals(t)) newrhs = subst;
|
||||||
|
|
||||||
if(newlhs == lhs && newrhs == rhs) return this;
|
if(newlhs == lhs && newrhs == rhs) return this;
|
@ -1,4 +1,4 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
@ -1,31 +1,42 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
|
||||||
|
|
||||||
public final class PlaceholderType extends Type{
|
public final class PlaceholderType extends UnifyType{
|
||||||
|
|
||||||
public PlaceholderType(String name) {
|
public PlaceholderType(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(IFiniteClosure fc) {
|
Set<UnifyType> smArg(IFiniteClosure fc) {
|
||||||
return fc.smArg(this);
|
return fc.smArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(IFiniteClosure fc) {
|
Set<UnifyType> grArg(IFiniteClosure fc) {
|
||||||
return fc.grArg(this);
|
return fc.grArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnifyType setTypeParams(TypeParams newTp) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return typeName.hashCode();
|
return typeName.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
UnifyType apply(Unifier unif) {
|
||||||
|
if(unif.hasSubstitute(this))
|
||||||
|
return unif.getSubstitute(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(!(obj instanceof PlaceholderType))
|
if(!(obj instanceof PlaceholderType))
|
||||||
@ -33,12 +44,4 @@ public final class PlaceholderType extends Type{
|
|||||||
|
|
||||||
return ((PlaceholderType) obj).getName().equals(typeName);
|
return ((PlaceholderType) obj).getName().equals(typeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type apply(Unifier unif) {
|
|
||||||
if(this.equals(unif.getSource()))
|
|
||||||
return unif.getTarget();
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,12 +1,11 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
|
||||||
|
|
||||||
public final class SimpleType extends Type {
|
public final class SimpleType extends UnifyType {
|
||||||
public SimpleType(String name, Type... typeParams) {
|
public SimpleType(String name, UnifyType... typeParams) {
|
||||||
super(name, new TypeParams(typeParams));
|
super(name, new TypeParams(typeParams));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,15 +14,25 @@ public final class SimpleType extends Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(IFiniteClosure fc) {
|
Set<UnifyType> smArg(IFiniteClosure fc) {
|
||||||
return fc.smArg(this);
|
return fc.smArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(IFiniteClosure fc) {
|
Set<UnifyType> grArg(IFiniteClosure fc) {
|
||||||
return fc.grArg(this);
|
return fc.grArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
UnifyType apply(Unifier unif) {
|
||||||
|
return new SimpleType(typeName, typeParams.apply(unif));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnifyType setTypeParams(TypeParams newTp) {
|
||||||
|
return new SimpleType(new String(typeName), newTp);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return typeName.hashCode();
|
return typeName.hashCode();
|
||||||
@ -41,12 +50,4 @@ public final class SimpleType extends Type {
|
|||||||
|
|
||||||
return other.getTypeParams().equals(typeParams);
|
return other.getTypeParams().equals(typeParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type apply(Unifier unif) {
|
|
||||||
if(this.equals(unif.getSource()))
|
|
||||||
return unif.getTarget();
|
|
||||||
|
|
||||||
return new SimpleType(typeName, typeParams.apply(unif));
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,20 +1,19 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
|
||||||
|
|
||||||
public final class SuperType extends Type {
|
public final class SuperType extends UnifyType {
|
||||||
|
|
||||||
private Type superedType;
|
private UnifyType superedType;
|
||||||
|
|
||||||
public SuperType(Type superedType) {
|
public SuperType(UnifyType superedType) {
|
||||||
super("? super " + superedType.getName(), superedType.getTypeParams());
|
super("? super " + superedType.getName(), superedType.getTypeParams());
|
||||||
this.superedType = superedType;
|
this.superedType = superedType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getSuperedType() {
|
public UnifyType getSuperedType() {
|
||||||
return superedType;
|
return superedType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,15 +28,25 @@ public final class SuperType extends Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> smArg(IFiniteClosure fc) {
|
public UnifyType setTypeParams(TypeParams newTp) {
|
||||||
|
return new SuperType(superedType.setTypeParams(newTp));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Set<UnifyType> smArg(IFiniteClosure fc) {
|
||||||
return fc.smArg(this);
|
return fc.smArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> grArg(IFiniteClosure fc) {
|
Set<UnifyType> grArg(IFiniteClosure fc) {
|
||||||
return fc.grArg(this);
|
return fc.grArg(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
UnifyType apply(Unifier unif) {
|
||||||
|
return new SuperType(superedType.apply(unif));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return superedType.hashCode() + 17;
|
return superedType.hashCode() + 17;
|
||||||
@ -51,9 +60,4 @@ public final class SuperType extends Type {
|
|||||||
SuperType other = (SuperType) obj;
|
SuperType other = (SuperType) obj;
|
||||||
return other.getSuperedType().equals(superedType);
|
return other.getSuperedType().equals(superedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type apply(Unifier unif) {
|
|
||||||
return new SuperType(superedType.apply(unif));
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,19 +1,17 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
public final class TypeParams implements Iterable<UnifyType>{
|
||||||
|
private final UnifyType[] typeParams;
|
||||||
|
|
||||||
public final class TypeParams implements Iterable<Type>{
|
public TypeParams(UnifyType... types) {
|
||||||
private final Type[] typeParams;
|
|
||||||
|
|
||||||
public TypeParams(Type... types) {
|
|
||||||
typeParams = types;
|
typeParams = types;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean arePlaceholders() {
|
public boolean arePlaceholders() {
|
||||||
for(Type t : typeParams)
|
for(UnifyType t : typeParams)
|
||||||
if(!(t instanceof PlaceholderType))
|
if(!(t instanceof PlaceholderType))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
@ -22,7 +20,7 @@ public final class TypeParams implements Iterable<Type>{
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String res = "";
|
String res = "";
|
||||||
for(Type t : typeParams)
|
for(UnifyType t : typeParams)
|
||||||
res += t + ",";
|
res += t + ",";
|
||||||
return "<" + res.substring(0, res.length()-1) + ">";
|
return "<" + res.substring(0, res.length()-1) + ">";
|
||||||
}
|
}
|
||||||
@ -36,25 +34,43 @@ public final class TypeParams implements Iterable<Type>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TypeParams apply(Unifier unif) {
|
public TypeParams apply(Unifier unif) {
|
||||||
Type[] newParams = new Type[typeParams.length];
|
UnifyType[] newParams = new UnifyType[typeParams.length];
|
||||||
for(int i = 0; i < typeParams.length; i++)
|
for(int i = 0; i < typeParams.length; i++)
|
||||||
newParams[i] = typeParams[i].apply(unif);
|
newParams[i] = typeParams[i].apply(unif);
|
||||||
return new TypeParams(newParams);
|
return new TypeParams(newParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(Type t) {
|
public boolean occurs(PlaceholderType t) {
|
||||||
for(Type t1 : typeParams)
|
for(UnifyType 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(UnifyType t) {
|
||||||
|
for(UnifyType t1 : typeParams)
|
||||||
if(t1.equals(t1))
|
if(t1.equals(t1))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type get(int i) {
|
public UnifyType get(int i) {
|
||||||
return typeParams[i];
|
return typeParams[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TypeParams set(UnifyType t, int idx) {
|
||||||
|
UnifyType[] newparams = Arrays.copyOf(typeParams, typeParams.length);
|
||||||
|
newparams[idx] = t;
|
||||||
|
return new TypeParams(newparams);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Type> iterator() {
|
public Iterator<UnifyType> iterator() {
|
||||||
return Arrays.stream(typeParams).iterator();
|
return Arrays.stream(typeParams).iterator();
|
||||||
}
|
}
|
||||||
|
|
63
src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java
Normal file
63
src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
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<UnifyType, UnifyType> {
|
||||||
|
private HashMap<PlaceholderType, UnifyType> substitutions = new HashMap<>();
|
||||||
|
|
||||||
|
public static Unifier IDENTITY = new Unifier();
|
||||||
|
|
||||||
|
public Unifier(PlaceholderType source, UnifyType target) {
|
||||||
|
substitutions.put(source, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identity function as an "unifier".
|
||||||
|
*/
|
||||||
|
public Unifier() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(PlaceholderType source, UnifyType target) {
|
||||||
|
Unifier tempU = new Unifier(source, target);
|
||||||
|
for(PlaceholderType pt : substitutions.keySet())
|
||||||
|
substitutions.put(pt, substitutions.get(pt).apply(tempU));
|
||||||
|
substitutions.put(source, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnifyType apply(UnifyType t) {
|
||||||
|
return t.apply(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MPair apply(MPair p) {
|
||||||
|
return new MPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasSubstitute(PlaceholderType t) {
|
||||||
|
return substitutions.containsKey(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnifyType getSubstitute(UnifyType t) {
|
||||||
|
return substitutions.get(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Entry<PlaceholderType, UnifyType>> getSubstitutions() {
|
||||||
|
return substitutions.entrySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String result = "{ ";
|
||||||
|
for(Entry<PlaceholderType, UnifyType> entry : substitutions.entrySet())
|
||||||
|
result += "(" + entry.getKey() + " -> " + entry.getValue() + "), ";
|
||||||
|
if(!substitutions.isEmpty())
|
||||||
|
result = result.substring(0, result.length()-2);
|
||||||
|
result += " }";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,21 +1,20 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
|
||||||
|
|
||||||
public abstract class Type {
|
public abstract class UnifyType {
|
||||||
|
|
||||||
protected final String typeName;
|
protected final String typeName;
|
||||||
protected final TypeParams typeParams;
|
protected final TypeParams typeParams;
|
||||||
|
|
||||||
protected Type(String name, Type... typeParams) {
|
protected UnifyType(String name, UnifyType... typeParams) {
|
||||||
typeName = name;
|
typeName = name;
|
||||||
this.typeParams = new TypeParams(typeParams);
|
this.typeParams = new TypeParams(typeParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Type(String name, TypeParams p) {
|
protected UnifyType(String name, TypeParams p) {
|
||||||
typeName = name;
|
typeName = name;
|
||||||
typeParams = p;
|
typeParams = p;
|
||||||
}
|
}
|
||||||
@ -28,17 +27,19 @@ public abstract class Type {
|
|||||||
return typeParams;
|
return typeParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Set<Type> smArg(IFiniteClosure fc);
|
public abstract UnifyType setTypeParams(TypeParams newTp);
|
||||||
|
|
||||||
public abstract Set<Type> grArg(IFiniteClosure fc);
|
abstract Set<UnifyType> smArg(IFiniteClosure fc);
|
||||||
|
|
||||||
public abstract Type apply(Unifier unif);
|
abstract Set<UnifyType> grArg(IFiniteClosure fc);
|
||||||
|
|
||||||
|
abstract UnifyType apply(Unifier unif);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String params = "";
|
String params = "";
|
||||||
if(typeParams.size() != 0) {
|
if(typeParams.size() != 0) {
|
||||||
for(Type param : typeParams)
|
for(UnifyType param : typeParams)
|
||||||
params += param.toString() + ",";
|
params += param.toString() + ",";
|
||||||
params = "<" + params.substring(0, params.length()-1) + ">";
|
params = "<" + params.substring(0, params.length()-1) + ">";
|
||||||
}
|
}
|
@ -2,68 +2,89 @@ package de.dhbwstuttgart.typeinference.unifynew;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
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 {
|
public class Mapping {
|
||||||
|
|
||||||
private static final int ST_MASK = 0;
|
private HashMap<de.dhbwstuttgart.typeinference.unify.model.UnifyType, de.dhbwstuttgart.syntaxtree.type.Type> backwardMap = new HashMap<>();
|
||||||
private static final int EXTENDS_MASK = 536870912;
|
private HashMap<de.dhbwstuttgart.syntaxtree.type.Type, de.dhbwstuttgart.typeinference.unify.model.UnifyType> forwardMap = new HashMap<>();
|
||||||
private static final int SUPER_MASK = 1073741824;
|
private Set<de.dhbwstuttgart.typeinference.unify.model.UnifyType> irreversible = new HashSet<>();
|
||||||
private static final int TPH_MASK = 1610612736;
|
|
||||||
private static final int FUN_MASK = -2147483648;
|
|
||||||
|
|
||||||
private static HashMap<Type, Type> mapping;
|
public Mapping(Set<de.dhbwstuttgart.syntaxtree.type.Type> types) {
|
||||||
|
for(de.dhbwstuttgart.syntaxtree.type.Type t : types) {
|
||||||
public Set<Type> createMapping(Iterable<Type> types) {
|
}
|
||||||
mapping = new HashMap<>();
|
|
||||||
|
|
||||||
Iterator<Type> iterator = types.iterator();
|
|
||||||
while(iterator.hasNext() && mapping.size() <= 536870911)
|
|
||||||
createMapping(iterator.next());
|
|
||||||
|
|
||||||
return mapping.keySet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMapping(Type type) {
|
public de.dhbwstuttgart.typeinference.unify.model.UnifyType map(de.dhbwstuttgart.syntaxtree.type.Type type) {
|
||||||
/*if(type instanceof RefType) {
|
return forwardMap.get(type);
|
||||||
Set<Type> params = ((RefType) type).get_ParaList();
|
}
|
||||||
params.stream().forEach(x -> createMapping(x));
|
|
||||||
|
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<de.dhbwstuttgart.typeinference.unify.model.UnifyType> mapTypeSet(Set<de.dhbwstuttgart.syntaxtree.type.Type> types) {
|
||||||
|
return types.stream().map(this::map).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<de.dhbwstuttgart.typeinference.unify.model.MPair> mapPairSet(Set<de.dhbwstuttgart.typeinference.Pair> pairs) {
|
||||||
|
return pairs.stream().map(this::map).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<de.dhbwstuttgart.syntaxtree.type.Type> unmap(de.dhbwstuttgart.typeinference.unify.model.UnifyType type) {
|
||||||
|
return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<de.dhbwstuttgart.typeinference.Pair> unmap(de.dhbwstuttgart.typeinference.unify.model.MPair mpair) {
|
||||||
|
de.dhbwstuttgart.typeinference.unify.model.UnifyType lhs = mpair.getLhsType();
|
||||||
|
de.dhbwstuttgart.typeinference.unify.model.UnifyType 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<Set<de.dhbwstuttgart.syntaxtree.type.Type>> unmapTypeSet(Set<de.dhbwstuttgart.typeinference.unify.model.UnifyType> types) {
|
||||||
|
Set<de.dhbwstuttgart.syntaxtree.type.Type> 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<Set<de.dhbwstuttgart.typeinference.Pair>> unmapPairSet(Set<de.dhbwstuttgart.typeinference.unify.model.MPair> pairs) {
|
||||||
|
Set<de.dhbwstuttgart.typeinference.Pair> 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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unifynew;
|
package de.dhbwstuttgart.typeinference.unifynew;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the Martelli-Montanari unification algorithm.
|
* Implementation of the Martelli-Montanari unification algorithm.
|
||||||
@ -16,65 +24,114 @@ import de.dhbwstuttgart.typinference.unify.model.TypeParams;
|
|||||||
public class MartelliMontanariUnify implements IUnify {
|
public class MartelliMontanariUnify implements IUnify {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Unifier unify(Set<MPair> terms) {
|
public Optional<Unifier> unify(Set<UnifyType> terms) {
|
||||||
// TODO Auto-generated method stub
|
if(terms.size() < 2)
|
||||||
return null;
|
return Optional.of(Unifier.IDENTITY);
|
||||||
|
|
||||||
|
ArrayList<MPair> termsQ = new ArrayList<MPair>();
|
||||||
|
Iterator<UnifyType> iter = terms.iterator();
|
||||||
|
UnifyType prev = iter.next();
|
||||||
|
while(iter.hasNext()) {
|
||||||
|
UnifyType next = iter.next();
|
||||||
|
termsQ.add(new MPair(prev, next, PairOperator.EQUALS));
|
||||||
|
prev = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
Unifier mgu = Unifier.IDENTITY;
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
while(idx < termsQ.size()) {
|
||||||
|
MPair pair = termsQ.get(idx);
|
||||||
|
|
||||||
|
if(delete(pair)) {
|
||||||
|
termsQ.remove(idx);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<Set<MPair>> optSet = decompose(pair);
|
||||||
|
|
||||||
|
if(optSet == null)
|
||||||
|
return Optional.empty(); // Unification failed
|
||||||
|
|
||||||
|
if(optSet.isPresent()) {
|
||||||
|
termsQ.addAll(optSet.get());
|
||||||
|
idx = idx+1 == termsQ.size() ? 0 : idx+1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<MPair> optPair = swap(pair);
|
||||||
|
|
||||||
|
if(optPair.isPresent()) {
|
||||||
|
termsQ.add(optPair.get());
|
||||||
|
idx = idx+1 == termsQ.size() ? 0 : idx+1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Occurs-Check
|
||||||
|
if(pair.getLhsType() instanceof PlaceholderType
|
||||||
|
&& pair.getRhsType().getTypeParams().occurs((PlaceholderType) pair.getLhsType()))
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
Optional<Entry<PlaceholderType, UnifyType>> optUni = eliminate(pair);
|
||||||
|
|
||||||
|
if(optUni.isPresent()) {
|
||||||
|
Entry<PlaceholderType, UnifyType> 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(mgu);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean delete(MPair pair) {
|
private boolean delete(MPair pair) {
|
||||||
return pair.getRhsType().equals(pair.getLhsType());
|
return pair.getRhsType().equals(pair.getLhsType());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<MPair> decompose(MPair pair) {
|
private Optional<Set<MPair>> decompose(MPair pair) {
|
||||||
Set<MPair> result = new HashSet<>();
|
Set<MPair> result = new HashSet<>();
|
||||||
|
|
||||||
Type rhs = pair.getRhsType();
|
UnifyType rhs = pair.getRhsType();
|
||||||
Type lhs = pair.getLhsType();
|
UnifyType lhs = pair.getLhsType();
|
||||||
|
|
||||||
if(!rhs.getName().equals(lhs.getName()) || rhs.getTypeParams().size() != lhs.getTypeParams().size())
|
|
||||||
return null; // conflict
|
|
||||||
|
|
||||||
TypeParams rhsTypeParams = rhs.getTypeParams();
|
TypeParams rhsTypeParams = rhs.getTypeParams();
|
||||||
TypeParams lhsTypeParams = lhs.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++)
|
for(int i = 0; i < rhsTypeParams.size(); i++)
|
||||||
result.add(new MPair(rhsTypeParams.get(i), lhsTypeParams.get(i), PairOperator.EQUALSDOT));
|
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<MPair> swap(MPair pair) {
|
||||||
Type rhs = pair.getRhsType();
|
UnifyType rhs = pair.getRhsType();
|
||||||
Type lhs = pair.getLhsType();
|
UnifyType lhs = pair.getLhsType();
|
||||||
|
|
||||||
if(lhs.getTypeParams().size() != 0 && rhs.getTypeParams().size() == 0)
|
if(!(lhs instanceof PlaceholderType) && (rhs instanceof PlaceholderType))
|
||||||
return new MPair(rhs, lhs, PairOperator.EQUALSDOT);
|
return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT));
|
||||||
return pair;
|
|
||||||
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Unifier eliminate(MPair pair) {
|
private Optional<Entry<PlaceholderType, UnifyType>> eliminate(MPair pair) {
|
||||||
Type rhs = pair.getRhsType();
|
UnifyType rhs = pair.getRhsType();
|
||||||
Type lhs = pair.getLhsType();
|
UnifyType lhs = pair.getLhsType();
|
||||||
|
|
||||||
TypeParams rhsTypeParams = rhs.getTypeParams();
|
// TODO only apply when lhs is element of vars(termsQ)?
|
||||||
|
|
||||||
for(Type t : rhsTypeParams)
|
if(!(lhs instanceof PlaceholderType))
|
||||||
if(lhs.equals(t))
|
return Optional.empty();
|
||||||
return new Unifier(); //identity-"unifier"
|
|
||||||
|
return Optional.of(new AbstractMap.SimpleImmutableEntry<PlaceholderType, UnifyType>((PlaceholderType) lhs, rhs));
|
||||||
return new Unifier(lhs, rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,27 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unifynew;
|
package de.dhbwstuttgart.typeinference.unifynew;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
|
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
|
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SuperType;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
|
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
|
|
||||||
public class RuleSet implements IRuleSet{
|
public class RuleSet implements IRuleSet{
|
||||||
|
|
||||||
@ -31,11 +36,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SuperType))
|
if(!(rhsType instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType))
|
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -47,11 +52,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(!(lhsType instanceof ExtendsType))
|
if(!(lhsType instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType))
|
if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -63,11 +68,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(!(lhsType instanceof ExtendsType))
|
if(!(lhsType instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SuperType))
|
if(!(rhsType instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -79,12 +84,12 @@ 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();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
|
|
||||||
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof ExtendsType))
|
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
|
|
||||||
if(!(rhsType instanceof ExtendsType))
|
if(!(rhsType instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@ -112,12 +117,12 @@ 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();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
|
|
||||||
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof SuperType))
|
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
|
|
||||||
if(!(rhsType instanceof SuperType))
|
if(!(rhsType instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@ -145,11 +150,11 @@ 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();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(lhsType instanceof PlaceholderType || lhsType.getTypeParams().empty())
|
if(lhsType instanceof PlaceholderType || lhsType.getTypeParams().empty())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
|
|
||||||
if(!rhsType.getName().equals(lhsType.getName()))
|
if(!rhsType.getName().equals(lhsType.getName()))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@ -176,11 +181,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(!(lhsType instanceof SimpleType))
|
if(!(lhsType instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SimpleType))
|
if(!(rhsType instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -210,7 +215,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.EQUALSDOT)
|
if(pair.getPairOp() != PairOperator.EQUALSDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
SimpleType lhsSType;
|
SimpleType lhsSType;
|
||||||
|
|
||||||
if(lhsType instanceof SimpleType)
|
if(lhsType instanceof SimpleType)
|
||||||
@ -225,7 +230,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(lhsSType.getTypeParams().empty())
|
if(lhsSType.getTypeParams().empty())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type rhsType = pair.getLhsType();
|
UnifyType rhsType = pair.getLhsType();
|
||||||
SimpleType rhsSType;
|
SimpleType rhsSType;
|
||||||
|
|
||||||
if(rhsType instanceof SimpleType)
|
if(rhsType instanceof SimpleType)
|
||||||
@ -259,11 +264,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType))
|
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType))
|
if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -275,8 +280,8 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
|
|
||||||
return finiteClosure.grArg(lhsType).contains(rhsType);
|
return finiteClosure.grArg(lhsType).contains(rhsType);
|
||||||
}
|
}
|
||||||
@ -308,11 +313,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeD = pair.getLhsType();
|
UnifyType typeD = pair.getLhsType();
|
||||||
if(!(typeD instanceof SimpleType))
|
if(!(typeD instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeDs = pair.getRhsType();
|
UnifyType typeDs = pair.getRhsType();
|
||||||
if(!(typeDs instanceof SimpleType))
|
if(!(typeDs instanceof SimpleType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -322,31 +327,31 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(typeD.getName().equals(typeDs.getName()))
|
if(typeD.getName().equals(typeDs.getName()))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Optional<Type> opt = finiteClosure.getGenericType(typeD.getName());
|
Optional<UnifyType> opt = finiteClosure.getGenericType(typeD.getName());
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
// The generic Version of Type D (D<a1, a2, a3, ... >)
|
// The generic Version of Type D (D<a1, a2, a3, ... >)
|
||||||
Type typeDgen = opt.get();
|
UnifyType typeDgen = opt.get();
|
||||||
|
|
||||||
// Actually greater+ because the types are ensured to have different names
|
// Actually greater+ because the types are ensured to have different names
|
||||||
Set<Type> greater = finiteClosure.greater(typeDgen);
|
Set<UnifyType> greater = finiteClosure.greater(typeDgen);
|
||||||
opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
|
opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type newLhs = opt.get();
|
UnifyType newLhs = opt.get();
|
||||||
|
|
||||||
TypeParams typeDParams = typeD.getTypeParams();
|
TypeParams typeDParams = typeD.getTypeParams();
|
||||||
TypeParams typeDgenParams = typeDgen.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++)
|
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(newLhs.apply(unif), typeDs, PairOperator.SMALLERDOT));
|
return Optional.of(new MPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -354,45 +359,45 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeD = pair.getLhsType();
|
UnifyType typeD = pair.getLhsType();
|
||||||
if(!(typeD instanceof SimpleType) && !(typeD instanceof ExtendsType))
|
if(!(typeD instanceof SimpleType) && !(typeD instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeExtDs = pair.getRhsType();
|
UnifyType typeExtDs = pair.getRhsType();
|
||||||
if(!(typeExtDs instanceof ExtendsType))
|
if(!(typeExtDs instanceof ExtendsType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
if(typeD.getTypeParams().size() == 0 || typeExtDs.getTypeParams().size() == 0)
|
if(typeD.getTypeParams().size() == 0 || typeExtDs.getTypeParams().size() == 0)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeDgen;
|
UnifyType typeDgen;
|
||||||
if(typeD instanceof SimpleType)
|
if(typeD instanceof SimpleType)
|
||||||
typeDgen = finiteClosure.getGenericType(typeD.getName()).orElse(null);
|
typeDgen = finiteClosure.getGenericType(typeD.getName()).orElse(null);
|
||||||
else {
|
else {
|
||||||
Optional<Type> opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName());
|
Optional<UnifyType> opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName());
|
||||||
typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null;
|
typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeDgen == null)
|
if(typeDgen == null)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Set<Type> grArg = finiteClosure.grArg(typeDgen);
|
Set<UnifyType> grArg = finiteClosure.grArg(typeDgen);
|
||||||
|
|
||||||
Optional<Type> opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny();
|
Optional<UnifyType> opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny();
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type newLhs = ((ExtendsType) opt.get()).getExtendedType();
|
UnifyType newLhs = ((ExtendsType) opt.get()).getExtendedType();
|
||||||
|
|
||||||
TypeParams typeDParams = typeD.getTypeParams();
|
TypeParams typeDParams = typeD.getTypeParams();
|
||||||
TypeParams typeDgenParams = typeDgen.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++)
|
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(newLhs.apply(unif), typeExtDs, PairOperator.SMALLERDOTWC));
|
return Optional.of(new MPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -400,11 +405,11 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeDs = pair.getLhsType();
|
UnifyType typeDs = pair.getLhsType();
|
||||||
if(!(typeDs instanceof SimpleType) && !(typeDs instanceof SuperType))
|
if(!(typeDs instanceof SimpleType) && !(typeDs instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeSupD = pair.getRhsType();
|
UnifyType typeSupD = pair.getRhsType();
|
||||||
if(!(typeSupD instanceof SuperType))
|
if(!(typeSupD instanceof SuperType))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
@ -412,39 +417,39 @@ public class RuleSet implements IRuleSet{
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
|
|
||||||
Optional<Type> opt = finiteClosure.getGenericType(((SuperType) typeSupD).getSuperedType().getName());
|
Optional<UnifyType> opt = finiteClosure.getGenericType(((SuperType) typeSupD).getSuperedType().getName());
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
Type typeDgen = opt.get();
|
UnifyType typeDgen = opt.get();
|
||||||
Type typeSupDgen = new SuperType(typeDgen);
|
UnifyType typeSupDgen = new SuperType(typeDgen);
|
||||||
|
|
||||||
// Use of smArg instead of grArg because
|
// Use of smArg instead of grArg because
|
||||||
// a in grArg(b) => b in smArg(a)
|
// a in grArg(b) => b in smArg(a)
|
||||||
Set<Type> smArg = finiteClosure.smArg(typeSupDgen);
|
Set<UnifyType> smArg = finiteClosure.smArg(typeSupDgen);
|
||||||
opt = smArg.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
|
opt = smArg.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny();
|
||||||
|
|
||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
||||||
// New RHS
|
// New RHS
|
||||||
Type newRhs = null;
|
UnifyType newRhs = null;
|
||||||
if(typeDs instanceof SimpleType)
|
if(typeDs instanceof SimpleType)
|
||||||
newRhs = new ExtendsType(typeDs);
|
newRhs = new ExtendsType(typeDs);
|
||||||
else
|
else
|
||||||
newRhs = new ExtendsType(((SuperType) typeDs).getSuperedType());
|
newRhs = new ExtendsType(((SuperType) typeDs).getSuperedType());
|
||||||
|
|
||||||
// New LHS
|
// New LHS
|
||||||
Type newLhs = opt.get();
|
UnifyType newLhs = opt.get();
|
||||||
TypeParams typeDParams = typeSupD.getTypeParams();
|
TypeParams typeDParams = typeSupD.getTypeParams();
|
||||||
TypeParams typeSupDsgenParams = typeSupDgen.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++)
|
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(newLhs.apply(unif), newRhs, PairOperator.SMALLERDOTWC));
|
return Optional.of(new MPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -453,23 +458,23 @@ public class RuleSet implements IRuleSet{
|
|||||||
* @param D The other type
|
* @param D The other type
|
||||||
* @return An array containing the values of pi for every type argument of C or an empty array if the search failed.
|
* @return An array containing the values of pi for every type argument of C or an empty array if the search failed.
|
||||||
*/
|
*/
|
||||||
private int[] pi(Type C, Type D) {
|
private int[] pi(UnifyType C, UnifyType D) {
|
||||||
Type cFromFc = null;
|
UnifyType cFromFc = null;
|
||||||
if(C instanceof SimpleType)
|
if(C instanceof SimpleType)
|
||||||
cFromFc = finiteClosure.getGenericType(C.getName()).orElse(null);
|
cFromFc = finiteClosure.getGenericType(C.getName()).orElse(null);
|
||||||
else if(C instanceof ExtendsType) {
|
else if(C instanceof ExtendsType) {
|
||||||
Optional<Type> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
|
Optional<UnifyType> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
|
||||||
if(opt.isPresent()) cFromFc = new ExtendsType(opt.get());
|
if(opt.isPresent()) cFromFc = new ExtendsType(opt.get());
|
||||||
}
|
}
|
||||||
else if(C instanceof SuperType) {
|
else if(C instanceof SuperType) {
|
||||||
Optional<Type> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
|
Optional<UnifyType> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
|
||||||
if(opt.isPresent()) cFromFc = new SuperType(opt.get());
|
if(opt.isPresent()) cFromFc = new SuperType(opt.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cFromFc == null)
|
if(cFromFc == null)
|
||||||
return new int[0];
|
return new int[0];
|
||||||
|
|
||||||
Optional<Type> opt = Optional.empty();
|
Optional<UnifyType> opt = Optional.empty();
|
||||||
if(D instanceof ExtendsType) {
|
if(D instanceof ExtendsType) {
|
||||||
SimpleType dSType = (SimpleType) ((ExtendsType) D).getExtendedType();
|
SimpleType dSType = (SimpleType) ((ExtendsType) D).getExtendedType();
|
||||||
opt = finiteClosure.grArg(cFromFc).stream()
|
opt = finiteClosure.grArg(cFromFc).stream()
|
||||||
@ -489,7 +494,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
if(!opt.isPresent())
|
if(!opt.isPresent())
|
||||||
return new int[0];
|
return new int[0];
|
||||||
|
|
||||||
Type dFromFc = opt.get();
|
UnifyType dFromFc = opt.get();
|
||||||
|
|
||||||
Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size());
|
Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size());
|
||||||
Assert.assertTrue(dFromFc.getTypeParams().size() > 0);
|
Assert.assertTrue(dFromFc.getTypeParams().size() > 0);
|
||||||
@ -501,7 +506,7 @@ public class RuleSet implements IRuleSet{
|
|||||||
|
|
||||||
boolean succ = true;
|
boolean succ = true;
|
||||||
for (int dArgIdx = 0; dArgIdx < dArgs.size() && succ; dArgIdx++) {
|
for (int dArgIdx = 0; dArgIdx < dArgs.size() && succ; dArgIdx++) {
|
||||||
Type dArg = dArgs.get(dArgIdx);
|
UnifyType dArg = dArgs.get(dArgIdx);
|
||||||
succ = false;
|
succ = false;
|
||||||
for (int pi = 0; pi < cArgs.size(); pi++)
|
for (int pi = 0; pi < cArgs.size(); pi++)
|
||||||
if (cArgs.get(pi).getName().equals(dArg.getName())) {
|
if (cArgs.get(pi).getName().equals(dArg.getName())) {
|
||||||
@ -516,9 +521,42 @@ public class RuleSet implements IRuleSet{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<MPair> subst(Set<MPair> pair) {
|
public Optional<Set<MPair>> subst(Set<MPair> pairs) {
|
||||||
// TODO Auto-generated method stub
|
HashMap<UnifyType, Integer> typeMap = new HashMap<>();
|
||||||
return null;
|
|
||||||
|
for(MPair pair : pairs) {
|
||||||
|
UnifyType t1 = pair.getLhsType();
|
||||||
|
UnifyType 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<MPair> result = new ArrayList<MPair>(pairs);
|
||||||
|
|
||||||
|
boolean applied = false;
|
||||||
|
|
||||||
|
for(int i = 0; i < result.size(); i++) {
|
||||||
|
MPair pair = result.get(i);
|
||||||
|
PlaceholderType lhsType = null;
|
||||||
|
UnifyType rhsType;
|
||||||
|
|
||||||
|
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.
|
||||||
|
&& !rhsType.getTypeParams().occurs(lhsType)) {
|
||||||
|
Unifier uni = new Unifier(lhsType, rhsType);
|
||||||
|
result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new));
|
||||||
|
applied = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return applied ? Optional.of(new HashSet<>(result)) : Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unifynew;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
|
||||||
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
|
|
||||||
|
|
||||||
public class Unifier implements Function<Type, Type> {
|
|
||||||
private Type source;
|
|
||||||
private Type target;
|
|
||||||
|
|
||||||
public Unifier(Type source, Type target) {
|
|
||||||
this.source = source;
|
|
||||||
this.target = target;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Identity function as an "unifier".
|
|
||||||
*/
|
|
||||||
public Unifier() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type apply(Type t) {
|
|
||||||
return t.apply(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type getSource() {
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type getTarget() {
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,13 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unifynew;
|
package de.dhbwstuttgart.typeinference.unifynew;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -16,12 +18,15 @@ import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
|
|||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
|
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SuperType;
|
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +35,7 @@ import de.dhbwstuttgart.typinference.unify.model.Type;
|
|||||||
*/
|
*/
|
||||||
public class Unify {
|
public class Unify {
|
||||||
|
|
||||||
public Menge<Menge<Pair>> unify(Set<MPair> eq, IFiniteClosure fc) {
|
public Set<Set<MPair>> unify(Set<MPair> eq, IFiniteClosure fc) {
|
||||||
/*
|
/*
|
||||||
* Step 1: Repeated application of reduce, adapt, erase, swap
|
* Step 1: Repeated application of reduce, adapt, erase, swap
|
||||||
*/
|
*/
|
||||||
@ -47,14 +52,13 @@ public class Unify {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Step 4: Create possible typings
|
* 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
|
// All Sets
|
||||||
// Sets of the "second level"
|
List<Set<MPair>> sets = new ArrayList<Set<MPair>>();
|
||||||
List<List<Set<MPair>>> pairSetsSet = calculatePairSets(eq2s, fc);
|
|
||||||
|
|
||||||
// The sets of the "first level"
|
|
||||||
Set<Set<?>> sets = new HashSet<Set<?>>();
|
|
||||||
|
|
||||||
if(eq1s.size() != 0)
|
if(eq1s.size() != 0)
|
||||||
sets.add(eq1s); // Add Eq1'
|
sets.add(eq1s); // Add Eq1'
|
||||||
@ -67,41 +71,58 @@ public class Unify {
|
|||||||
if(bufferSet.size() != 0)
|
if(bufferSet.size() != 0)
|
||||||
sets.add(bufferSet);
|
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.
|
/* Up to here, no cartesian products are calculated.
|
||||||
* Around here, filters for pairs and sets can be applied */
|
* Around here, filters for pairs and sets can be applied */
|
||||||
|
|
||||||
ISetOperations setOps = new GuavaSetOperations();
|
ISetOperations setOps = new GuavaSetOperations();
|
||||||
|
|
||||||
// Calculate the inner cartesian products
|
// Calculate the cartesian products
|
||||||
// Cartesian products of the second level
|
Set<Set<MPair>> result = setOps.cartesianProduct(sets).stream()
|
||||||
|
.map(x -> new HashSet<MPair>(x)).collect(Collectors.toCollection(HashSet::new));
|
||||||
// AddAll -> nur add
|
//System.out.println(result);
|
||||||
for(List<Set<MPair>> 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<List<Object>> eqsSet = setOps.cartesianProduct(new ArrayList<>(sets));
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println(eqsSet);
|
|
||||||
/*
|
/*
|
||||||
* Step 5: Substitution
|
* 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<Set<MPair>> changed = new HashSet<>();
|
||||||
|
Set<Set<MPair>> unchanged = new HashSet<>();
|
||||||
|
|
||||||
|
for(Set<MPair> eqss : result) {
|
||||||
|
Optional<Set<MPair>> 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
|
* Step 6 a) Restart for pairs where subst was applied
|
||||||
* b) Union over everything
|
* b) Build the union over everything
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
for(Set<MPair> eqss : changed)
|
||||||
|
unchanged.addAll(this.unify(eqss, fc));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Step 7: Filter result for solved pairs
|
* Step 7: Filter result for solved pairs TODO wie?
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return null;
|
return unchanged;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,74 +217,133 @@ public class Unify {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected List<List<Set<MPair>>> calculatePairSets(Set<MPair> eq2s, IFiniteClosure fc) {
|
protected List<Set<MPair>> calculatePairSets(Set<MPair> eq2s, IFiniteClosure fc) {
|
||||||
List<List<Set<MPair>>> result = new ArrayList<List<Set<MPair>>>();
|
List<Set<MPair>> result = new ArrayList<Set<MPair>>();
|
||||||
for(int i = 0; i < 8; i++)
|
|
||||||
result.add(new ArrayList<Set<MPair>>());
|
|
||||||
|
|
||||||
|
|
||||||
for(MPair pair : eq2s) {
|
for(MPair pair : eq2s) {
|
||||||
|
|
||||||
PairOperator pairOp = pair.getPairOp();
|
PairOperator pairOp = pair.getPairOp();
|
||||||
Type lhsType = pair.getLhsType();
|
UnifyType lhsType = pair.getLhsType();
|
||||||
Type rhsType = pair.getRhsType();
|
UnifyType rhsType = pair.getRhsType();
|
||||||
|
|
||||||
// Case 1: (a <. Theta')
|
// Case 1: (a <. Theta')
|
||||||
if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) {
|
if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) {
|
||||||
// TODO
|
UnifyType thetaPrime = pair.getRhsType();
|
||||||
|
Set<MPair> set = new HashSet<>();
|
||||||
|
IUnify unify = new MartelliMontanariUnify();
|
||||||
|
|
||||||
|
//Set<Type> cs = fc.getAllTypes(rhsType.getName());
|
||||||
|
UnifyType c = rhsType;
|
||||||
|
|
||||||
|
//Set<Type> thetaQs = cs.stream().flatMap(x -> fc.smaller(x).stream()).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
Set<UnifyType> thetaQs = fc.smaller(c);
|
||||||
|
|
||||||
|
Set<UnifyType> thetaQPrimes = new HashSet<>();
|
||||||
|
|
||||||
|
TypeParams cParams = c.getTypeParams();
|
||||||
|
if(cParams.size() == 0)
|
||||||
|
thetaQPrimes.add(c);
|
||||||
|
else {
|
||||||
|
ArrayList<Set<UnifyType>> candidateParams = new ArrayList<>();
|
||||||
|
for(UnifyType param : cParams)
|
||||||
|
candidateParams.add(fc.grArg(param));
|
||||||
|
Set<TypeParams> permutations = new HashSet<TypeParams>();
|
||||||
|
permuteParams(candidateParams, 0, permutations, new UnifyType[candidateParams.size()]);
|
||||||
|
|
||||||
|
for(TypeParams tp : permutations)
|
||||||
|
thetaQPrimes.add(c.setTypeParams(tp));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(UnifyType tqp : thetaQPrimes) {
|
||||||
|
Optional<Unifier> opt = unify.unify(tqp, thetaPrime);
|
||||||
|
if(opt.isPresent()) {
|
||||||
|
Unifier unifier = opt.get();
|
||||||
|
Set<Entry<PlaceholderType, UnifyType>> substitutions = unifier.getSubstitutions();
|
||||||
|
for(Entry<PlaceholderType, UnifyType> sigma : substitutions)
|
||||||
|
set.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT));
|
||||||
|
for(UnifyType tq : thetaQs) {
|
||||||
|
Set<UnifyType> smaller = fc.smaller(unifier.apply(tq));
|
||||||
|
smaller.stream().map(x -> new MPair(lhsType, x, PairOperator.EQUALSDOT)).forEach(x -> set.add(x));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.add(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2: (a <.? ? ext Theta')
|
// Case 2: (a <.? ? ext Theta')
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType){
|
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType){
|
||||||
// TODO
|
throw new NotImplementedException(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 3: (a <.? ? sup Theta')
|
// Case 3: (a <.? ? sup Theta')
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) {
|
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) {
|
||||||
Set<MPair> set = new HashSet<>();
|
Set<MPair> set = new HashSet<>();
|
||||||
for(Type theta : fc.smArg(rhsType))
|
for(UnifyType theta : fc.smArg(rhsType))
|
||||||
set.add(new MPair(lhsType, theta, PairOperator.EQUALSDOT));
|
set.add(new MPair(lhsType, theta, PairOperator.EQUALSDOT));
|
||||||
|
result.add(set);
|
||||||
result.get(2).add(set);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 4: (a <.? Theta')
|
// Case 4: (a <.? Theta')
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) {
|
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) {
|
||||||
Set<MPair> set = new HashSet<>();
|
Set<MPair> set = new HashSet<>();
|
||||||
set.add(new MPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT));
|
set.add(new MPair(lhsType, rhsType, PairOperator.EQUALSDOT));
|
||||||
result.get(3).add(set);
|
result.add(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 5: (Theta <. a)
|
// Case 5: (Theta <. a)
|
||||||
else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) {
|
else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) {
|
||||||
Set<MPair> set = new HashSet<>();
|
Set<MPair> set = new HashSet<>();
|
||||||
for(Type thetaS : fc.greater(lhsType))
|
for(UnifyType thetaS : fc.greater(lhsType))
|
||||||
set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT));
|
set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT));
|
||||||
result.get(4).add(set);
|
result.add(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 6: (? ext Theta <.? a)
|
// Case 6: (? ext Theta <.? a)
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType) {
|
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType) {
|
||||||
Set<MPair> set = new HashSet<>();
|
Set<MPair> set = new HashSet<>();
|
||||||
for(Type thetaS : fc.grArg(lhsType))
|
for(UnifyType thetaS : fc.grArg(lhsType))
|
||||||
set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT));
|
set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT));
|
||||||
result.get(5).add(set);
|
result.add(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 7: (? sup Theta <.? a)
|
// Case 7: (? sup Theta <.? a)
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType) {
|
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType) {
|
||||||
// TODO
|
throw new NotImplementedException(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 8: (Theta <.? a)
|
// Case 8: (Theta <.? a)
|
||||||
else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) {
|
else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) {
|
||||||
Set<MPair> set = new HashSet<>();
|
Set<MPair> set = new HashSet<>();
|
||||||
for(Type thetaS : fc.grArg(lhsType))
|
for(UnifyType thetaS : fc.grArg(lhsType))
|
||||||
set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT));
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void permuteParams(ArrayList<Set<UnifyType>> candidates, int idx, Set<TypeParams> result, UnifyType[] current) {
|
||||||
|
if(candidates.size() == idx) {
|
||||||
|
result.add(new TypeParams(Arrays.copyOf(current, current.length)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<UnifyType> localCandidates = candidates.get(idx);
|
||||||
|
|
||||||
|
for(UnifyType t : localCandidates) {
|
||||||
|
current[idx] = t;
|
||||||
|
permuteParams(candidates, idx+1, result, current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<UnifyType> getAllInstantiations(UnifyType t, IFiniteClosure fc) {
|
||||||
|
Set<UnifyType> result = new HashSet<>();
|
||||||
|
result.add(t);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
package de.dhbwstuttgart.typinference.unify.model;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
|
|
||||||
|
|
||||||
public final class ExtendsType extends Type {
|
|
||||||
private Type extendedType;
|
|
||||||
|
|
||||||
public ExtendsType(Type extendedType) {
|
|
||||||
super("? extends " + extendedType.getName(), extendedType.getTypeParams());
|
|
||||||
this.extendedType = extendedType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Type getExtendedType() {
|
|
||||||
return extendedType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeParams getTypeParams() {
|
|
||||||
return extendedType.getTypeParams();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "? extends " + extendedType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Type> smArg(IFiniteClosure fc) {
|
|
||||||
return fc.smArg(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Type> grArg(IFiniteClosure fc) {
|
|
||||||
return fc.grArg(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return extendedType.hashCode() + 17;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if(!(obj instanceof ExtendsType))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ExtendsType other = (ExtendsType) obj;
|
|
||||||
return other.getExtendedType().equals(extendedType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Type apply(Unifier unif) {
|
|
||||||
return new ExtendsType(extendedType.apply(unif));
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,16 +4,16 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.FiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
|
|
||||||
public class FiniteClosureBuilder {
|
public class FiniteClosureBuilder {
|
||||||
|
|
||||||
private Set<MPair> pairs = new HashSet<>();
|
private Set<MPair> pairs = new HashSet<>();
|
||||||
|
|
||||||
public void add(Type sub, Type sup) {
|
public void add(UnifyType sub, UnifyType sup) {
|
||||||
pairs.add(new MPair(sub, sup, PairOperator.SMALLER));
|
pairs.add(new MPair(sub, sup, PairOperator.SMALLER));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,19 +28,19 @@ public class FiniteClosureBuilder {
|
|||||||
public IFiniteClosure getCollectionExample() {
|
public IFiniteClosure getCollectionExample() {
|
||||||
TypeFactory tf = new TypeFactory();
|
TypeFactory tf = new TypeFactory();
|
||||||
|
|
||||||
Type collection = tf.getSimpleType("Collection");
|
UnifyType collection = tf.getSimpleType("Collection");
|
||||||
Type set = tf.getSimpleType("Set", "T");
|
UnifyType set = tf.getSimpleType("Set", "T");
|
||||||
Type sortedSet = tf.getSimpleType("Set", "T");
|
UnifyType sortedSet = tf.getSimpleType("Set", "T");
|
||||||
Type TreeSet = tf.getSimpleType("TreeSet", "T");
|
UnifyType TreeSet = tf.getSimpleType("TreeSet", "T");
|
||||||
Type hashSet = tf.getSimpleType("HashSet", "T");
|
UnifyType hashSet = tf.getSimpleType("HashSet", "T");
|
||||||
Type linkedHashSet = tf.getSimpleType("LinkedHashSet", "T");
|
UnifyType linkedHashSet = tf.getSimpleType("LinkedHashSet", "T");
|
||||||
Type queue = tf.getSimpleType("Queue", "T");
|
UnifyType queue = tf.getSimpleType("Queue", "T");
|
||||||
Type deque = tf.getSimpleType("Deque", "T");
|
UnifyType deque = tf.getSimpleType("Deque", "T");
|
||||||
Type linkedList = tf.getSimpleType("LinkedList", "T");
|
UnifyType linkedList = tf.getSimpleType("LinkedList", "T");
|
||||||
Type list = tf.getSimpleType("List", "T");
|
UnifyType list = tf.getSimpleType("List", "T");
|
||||||
Type vector = tf.getSimpleType("Vector", "T");
|
UnifyType vector = tf.getSimpleType("Vector", "T");
|
||||||
Type stack = tf.getSimpleType("Stack", "T");
|
UnifyType stack = tf.getSimpleType("Stack", "T");
|
||||||
Type arrayList = tf.getSimpleType("ArrayList", "T");
|
UnifyType arrayList = tf.getSimpleType("ArrayList", "T");
|
||||||
|
|
||||||
add(set, collection);
|
add(set, collection);
|
||||||
add(sortedSet, set);
|
add(sortedSet, set);
|
||||||
|
@ -6,8 +6,9 @@ import java.util.Set;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.MPair;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
|
||||||
public class FiniteClosureTest {
|
public class FiniteClosureTest {
|
||||||
|
|
||||||
@ -54,4 +55,10 @@ public class FiniteClosureTest {
|
|||||||
System.out.println("SmArg(? extends List<T>) = " + fc.smArg(tf.getExtendsType(tf.getSimpleType("List", "T"))));
|
System.out.println("SmArg(? extends List<T>) = " + fc.smArg(tf.getExtendsType(tf.getSimpleType("List", "T"))));
|
||||||
System.out.println("SmArg(? super List<T>) = " + fc.smArg(tf.getSuperType(tf.getSimpleType("List", "T"))));
|
System.out.println("SmArg(? super List<T>) = " + fc.smArg(tf.getSuperType(tf.getSimpleType("List", "T"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetGenericType() {
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
47
test/unify/GenerateFiniteClosure.java
Normal file
47
test/unify/GenerateFiniteClosure.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package unify;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.ImportDeclarations;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
|
import de.dhbwstuttgart.typeinference.Menge;
|
||||||
|
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
|
||||||
|
|
||||||
|
public class GenerateFiniteClosure {
|
||||||
|
|
||||||
|
private TypeAssumptions generateAssumptionsFromImport(String importClass){
|
||||||
|
SourceFile sf = new SourceFile();
|
||||||
|
ImportDeclarations imports = new ImportDeclarations();
|
||||||
|
imports.add(new UsedId(importClass,0));
|
||||||
|
TypeAssumptions ass = sf.makeBasicAssumptionsFromJRE(imports, true);
|
||||||
|
return ass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateTypeAssumptions(){
|
||||||
|
Menge<String> imports = new Menge<>();
|
||||||
|
imports.add("java.util.Vector");
|
||||||
|
imports.add("java.lang.Boolean");
|
||||||
|
imports.add("java.util.ArrayList");
|
||||||
|
|
||||||
|
for(String importClass : imports){
|
||||||
|
TypeAssumptions ass = generateAssumptionsFromImport(importClass);
|
||||||
|
assertTrue(ass.getClassAssumptionFor(new RefType(importClass,null,0))!=null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String importClass = "java.util.Vector";
|
||||||
|
TypeAssumptions ass = generateAssumptionsFromImport(importClass);
|
||||||
|
FiniteClosure fc = UnifyTypeFactory.generateFC(ass);
|
||||||
|
System.out.println(fc);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,12 +9,12 @@ import junit.framework.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
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.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 {
|
public class RuleSetTest {
|
||||||
|
66
test/unify/StandardUnifyTest.java
Normal file
66
test/unify/StandardUnifyTest.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
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.unify.model.MPair;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
||||||
|
import de.dhbwstuttgart.typeinference.unifynew.MartelliMontanariUnify;
|
||||||
|
|
||||||
|
public class StandardUnifyTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnify() {
|
||||||
|
IUnify unify = new MartelliMontanariUnify();
|
||||||
|
TypeFactory tf = new TypeFactory();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Positive Tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
UnifyType x = tf.getPlaceholderType("x");
|
||||||
|
UnifyType y = tf.getPlaceholderType("y");
|
||||||
|
UnifyType f = tf.getSimpleType("f", x);
|
||||||
|
|
||||||
|
// {f<x> = y}
|
||||||
|
Set<MPair> terms = new HashSet<MPair>();
|
||||||
|
|
||||||
|
System.out.println(unify.unify(f, y).get());
|
||||||
|
|
||||||
|
// TODO ist das ergebnis { (x -> ? extends a), (y -> g<x>) } in der richtigen form oder
|
||||||
|
// muss es { (x -> ? extends a), (y -> g<? extends a>) } sein?
|
||||||
|
// {f<g<x>,x> = f<y, ? extends a>}
|
||||||
|
UnifyType g = tf.getSimpleType("g", "x");
|
||||||
|
UnifyType f1 = tf.getSimpleType("f", g, x);
|
||||||
|
UnifyType a = tf.getExtendsType(tf.getPlaceholderType("a"));
|
||||||
|
UnifyType f2 = tf.getSimpleType("f", y, a);
|
||||||
|
|
||||||
|
terms = new HashSet<>();
|
||||||
|
|
||||||
|
System.out.println(unify.unify(f1, f2).get());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Negative Tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
// {f(x) =. x}
|
||||||
|
f = tf.getSimpleType("f", x);
|
||||||
|
Assert.assertFalse(unify.unify(f, x).isPresent());
|
||||||
|
|
||||||
|
// {f(x) =. f(x,y)}
|
||||||
|
f1 = tf.getSimpleType("f", "x");
|
||||||
|
f2 = tf.getSimpleType("f", "x", "y");
|
||||||
|
Assert.assertFalse(unify.unify(f1, f2).isPresent());
|
||||||
|
|
||||||
|
// {f(x) =. g(x)}
|
||||||
|
f1 = tf.getSimpleType("f", "x");
|
||||||
|
f2 = tf.getSimpleType("g", "x");
|
||||||
|
Assert.assertFalse(unify.unify(f1, f2).isPresent());
|
||||||
|
}
|
||||||
|
}
|
@ -3,19 +3,19 @@ package unify;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
|
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
|
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
|
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.SuperType;
|
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
|
||||||
import de.dhbwstuttgart.typinference.unify.model.Type;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||||
|
|
||||||
public class TypeFactory {
|
public class TypeFactory {
|
||||||
|
|
||||||
public ExtendsType getExtendsType(Type extendedType) {
|
public ExtendsType getExtendsType(UnifyType extendedType) {
|
||||||
return new ExtendsType(extendedType);
|
return new ExtendsType(extendedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuperType getSuperType(Type superedType) {
|
public SuperType getSuperType(UnifyType superedType) {
|
||||||
return new SuperType(superedType);
|
return new SuperType(superedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,12 +23,12 @@ public class TypeFactory {
|
|||||||
return new SimpleType(name);
|
return new SimpleType(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleType getSimpleType(String name, Type... typeParams) {
|
public SimpleType getSimpleType(String name, UnifyType... typeParams) {
|
||||||
return new SimpleType(name, typeParams);
|
return new SimpleType(name, typeParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleType getSimpleType(String name, String... typeParams) {
|
public SimpleType getSimpleType(String name, String... typeParams) {
|
||||||
return new SimpleType(name, Arrays.stream(typeParams).map(x -> getPlaceholderType(x)).collect(Collectors.toList()).toArray(new Type[0]));
|
return new SimpleType(name, Arrays.stream(typeParams).map(x -> getPlaceholderType(x)).collect(Collectors.toList()).toArray(new UnifyType[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlaceholderType getPlaceholderType(String name) {
|
public PlaceholderType getPlaceholderType(String name) {
|
||||||
|
@ -7,8 +7,13 @@ import org.junit.Test;
|
|||||||
import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder;
|
import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder;
|
||||||
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
|
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
|
||||||
import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder;
|
import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
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.syntaxtree.type.TypePlaceholder;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
|
||||||
import de.dhbwstuttgart.typeinference.Menge;
|
import de.dhbwstuttgart.typeinference.Menge;
|
||||||
import de.dhbwstuttgart.typeinference.Pair;
|
import de.dhbwstuttgart.typeinference.Pair;
|
||||||
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
|
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
|
||||||
@ -19,7 +24,6 @@ public class UnifyOldTest {
|
|||||||
@Test
|
@Test
|
||||||
public void unifyTestSimpleTypes() {
|
public void unifyTestSimpleTypes() {
|
||||||
// Init Factories and Builders
|
// Init Factories and Builders
|
||||||
UnifyTypeFactory typeFactory = new UnifyTypeFactory();
|
|
||||||
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
||||||
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
||||||
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
||||||
@ -29,13 +33,13 @@ public class UnifyOldTest {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Init Types
|
// Init Types
|
||||||
RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean");
|
RefType boolT = this.GetSimpleType("java.lang.Boolean");
|
||||||
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
|
TypePlaceholder aTph = this.GetTypePlaceholder("a");
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
|
resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
|
||||||
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT),
|
resultBuilder.addPair(aTph, this.GetExtendsType(boolT),
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||||
|
|
||||||
@ -55,17 +59,17 @@ public class UnifyOldTest {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Init Types
|
// Init Types
|
||||||
boolT = typeFactory.GetSimpleType("java.lang.Boolean");
|
boolT = this.GetSimpleType("java.lang.Boolean");
|
||||||
aTph = typeFactory.GetTypePlaceholder("a");
|
aTph = this.GetTypePlaceholder("a");
|
||||||
TypePlaceholder bTph = typeFactory.GetTypePlaceholder("b");
|
TypePlaceholder bTph = this.GetTypePlaceholder("b");
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
|
resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
|
||||||
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT),
|
resultBuilder.addPair(aTph, this.GetExtendsType(boolT),
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
resultBuilder.addPair(bTph, boolT, PairOperator.Equal);
|
resultBuilder.addPair(bTph, boolT, PairOperator.Equal);
|
||||||
resultBuilder.addPair(bTph, typeFactory.GetExtendsType(boolT),
|
resultBuilder.addPair(bTph, this.GetExtendsType(boolT),
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
expectedResult = resultBuilder.getNestedPairMenge();
|
expectedResult = resultBuilder.getNestedPairMenge();
|
||||||
|
|
||||||
@ -86,8 +90,8 @@ public class UnifyOldTest {
|
|||||||
* Test b <. a, a <. b
|
* Test b <. a, a <. b
|
||||||
*/
|
*/
|
||||||
|
|
||||||
aTph = typeFactory.GetTypePlaceholder("a");
|
aTph = this.GetTypePlaceholder("a");
|
||||||
bTph = typeFactory.GetTypePlaceholder("b");
|
bTph = this.GetTypePlaceholder("b");
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
@ -114,10 +118,10 @@ public class UnifyOldTest {
|
|||||||
* Test Integer <. a, a <. Boolean
|
* Test Integer <. a, a <. Boolean
|
||||||
*/
|
*/
|
||||||
|
|
||||||
RefType intT = typeFactory.GetSimpleType("java.lang.Integer");
|
RefType intT = this.GetSimpleType("java.lang.Integer");
|
||||||
boolT = typeFactory.GetSimpleType("java.lang.Boolean");
|
boolT = this.GetSimpleType("java.lang.Boolean");
|
||||||
aTph = typeFactory.GetTypePlaceholder("a");
|
aTph = this.GetTypePlaceholder("a");
|
||||||
bTph = typeFactory.GetTypePlaceholder("b");
|
bTph = this.GetTypePlaceholder("b");
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
@ -141,7 +145,6 @@ public class UnifyOldTest {
|
|||||||
public void unifyTestGenerics() {
|
public void unifyTestGenerics() {
|
||||||
|
|
||||||
// Init Factories and Builders
|
// Init Factories and Builders
|
||||||
UnifyTypeFactory typeFactory = new UnifyTypeFactory();
|
|
||||||
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
||||||
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
||||||
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
||||||
@ -150,15 +153,15 @@ public class UnifyOldTest {
|
|||||||
* Test a <. MyClass<T, F>
|
* Test a <. MyClass<T, F>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
|
TypePlaceholder aTph = this.GetTypePlaceholder("a");
|
||||||
RefType myType = typeFactory.GetSimpleType("MyClass",
|
RefType myType = this.GetSimpleType("MyClass",
|
||||||
typeFactory.GetTypePlaceholder("T"),
|
this.GetTypePlaceholder("T"),
|
||||||
typeFactory.GetTypePlaceholder("F"));
|
this.GetTypePlaceholder("F"));
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
resultBuilder.addPair(aTph, myType, PairOperator.Equal);
|
resultBuilder.addPair(aTph, myType, PairOperator.Equal);
|
||||||
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(myType));
|
resultBuilder.addPair(aTph, this.GetExtendsType(myType));
|
||||||
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||||
|
|
||||||
// Actual Result
|
// Actual Result
|
||||||
@ -176,13 +179,13 @@ public class UnifyOldTest {
|
|||||||
* Test List<List<T>> <. List<T>
|
* Test List<List<T>> <. List<T>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T");
|
TypePlaceholder tTph = this.GetTypePlaceholder("T");
|
||||||
RefType list = typeFactory.GetSimpleType("List", tTph);
|
RefType list = this.GetSimpleType("List", tTph);
|
||||||
RefType listlist = typeFactory.GetSimpleType("List", list);
|
RefType listlist = this.GetSimpleType("List", list);
|
||||||
|
|
||||||
// Expected Result
|
// Expected Result
|
||||||
resultBuilder.clear();
|
resultBuilder.clear();
|
||||||
resultBuilder.addPair(typeFactory.GetExtendsType(list), tTph,
|
resultBuilder.addPair(this.GetExtendsType(list), tTph,
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
expectedResult = resultBuilder.getNestedPairMenge();
|
expectedResult = resultBuilder.getNestedPairMenge();
|
||||||
|
|
||||||
@ -206,16 +209,15 @@ public class UnifyOldTest {
|
|||||||
public void unifyTestInheritance() {
|
public void unifyTestInheritance() {
|
||||||
|
|
||||||
// Init Factories and Builders
|
// Init Factories and Builders
|
||||||
UnifyTypeFactory typeFactory = new UnifyTypeFactory();
|
|
||||||
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
|
||||||
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
|
||||||
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
|
||||||
|
|
||||||
// Init Types
|
// Init Types
|
||||||
RefType tBool = typeFactory.GetSimpleType("java.lang.Boolean");
|
RefType tBool = this.GetSimpleType("java.lang.Boolean");
|
||||||
RefType tString = typeFactory.GetSimpleType("java.lang.String");
|
RefType tString = this.GetSimpleType("java.lang.String");
|
||||||
RefType tInt = typeFactory.GetSimpleType("java.lang.Integer");
|
RefType tInt = this.GetSimpleType("java.lang.Integer");
|
||||||
TypePlaceholder tphA = typeFactory.GetTypePlaceholder("a");
|
TypePlaceholder tphA = this.GetTypePlaceholder("a");
|
||||||
|
|
||||||
// Build inheritance hierachy
|
// Build inheritance hierachy
|
||||||
// Bool <. String <. Int
|
// Bool <. String <. Int
|
||||||
@ -227,10 +229,10 @@ public class UnifyOldTest {
|
|||||||
|
|
||||||
// Build expected result
|
// Build expected result
|
||||||
resultBuilder.addPair(tphA, tBool, PairOperator.Equal);
|
resultBuilder.addPair(tphA, tBool, PairOperator.Equal);
|
||||||
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tBool),
|
resultBuilder.addPair(tphA, this.GetExtendsType(tBool),
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
resultBuilder.addPair(tphA, tString, PairOperator.Equal);
|
resultBuilder.addPair(tphA, tString, PairOperator.Equal);
|
||||||
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString),
|
resultBuilder.addPair(tphA, this.GetExtendsType(tString),
|
||||||
PairOperator.Equal);
|
PairOperator.Equal);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@ -299,5 +301,34 @@ public class UnifyOldTest {
|
|||||||
return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2))
|
return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2))
|
||||||
|| (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1));
|
|| (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private RefType GetSimpleType(String name, Type... parameters) {
|
||||||
|
if(parameters.length == 0)
|
||||||
|
return new RefType(name, null, 0);
|
||||||
|
|
||||||
|
Menge<Type> typeParams = new Menge<Type>();
|
||||||
|
|
||||||
|
for(Type t : parameters)
|
||||||
|
typeParams.add(t);
|
||||||
|
|
||||||
|
return new RefType(name, typeParams, null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExtendsWildcardType GetExtendsType(ObjectType extendedType) {
|
||||||
|
return new ExtendsWildcardType(extendedType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SuperWildcardType GetSuperType(ObjectType superedType) {
|
||||||
|
return new SuperWildcardType(superedType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private WildcardType GetWildcardType() {
|
||||||
|
return new WildcardType(null, null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TypePlaceholder GetTypePlaceholder(String name) {
|
||||||
|
return TypePlaceholder.backdoorCreate(name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
package unify;
|
package unify;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
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.UnifyType;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
||||||
import de.dhbwstuttgart.typeinference.unifynew.Unify;
|
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 {
|
public class UnifyTest extends Unify {
|
||||||
|
|
||||||
@ -18,25 +21,29 @@ public class UnifyTest extends Unify {
|
|||||||
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
|
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
|
||||||
Set<MPair> eq = new HashSet<MPair>();
|
Set<MPair> eq = new HashSet<MPair>();
|
||||||
|
|
||||||
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("Integer"), tf.getSimpleType("Number"));
|
||||||
fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number"));
|
fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number"));
|
||||||
|
//fcb.add(tf.getSimpleType("List", "T"));
|
||||||
|
|
||||||
IFiniteClosure fc = fcb.getCollectionExample();
|
IFiniteClosure fc = fcb.getCollectionExample();
|
||||||
|
|
||||||
// Vector<Integer> <. Vector<A>
|
// Vector<Integer> <. Vector<A>
|
||||||
// Vector<Integer <. Vector<C>
|
// Vector<Integer <. Vector<C>
|
||||||
// A <. Number
|
// A <. Integer
|
||||||
|
// Number <. A
|
||||||
// Double <. B
|
// Double <. B
|
||||||
// B <. Object
|
// 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", "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("Number")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT));
|
||||||
eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Number"), 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("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.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));
|
//eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT));
|
||||||
|
|
||||||
this.unify(eq, fc);
|
System.out.println(this.unify(eq, fc));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,4 +57,32 @@ public class UnifyTest extends Unify {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void permuteParamsTest() {
|
||||||
|
TypeFactory tf = new TypeFactory();
|
||||||
|
ArrayList<Set<UnifyType>> candidates = new ArrayList<>();
|
||||||
|
|
||||||
|
Set<UnifyType> p1 = new HashSet<>();
|
||||||
|
p1.add(tf.getPlaceholderType("p11"));
|
||||||
|
p1.add(tf.getExtendsType(tf.getSimpleType("p12")));
|
||||||
|
p1.add(tf.getSimpleType("p13"));
|
||||||
|
|
||||||
|
Set<UnifyType> p2 = new HashSet<>();
|
||||||
|
p2.add(tf.getPlaceholderType("p21"));
|
||||||
|
p2.add(tf.getPlaceholderType("p22"));
|
||||||
|
|
||||||
|
Set<UnifyType> p3 = new HashSet<>();
|
||||||
|
p3.add(tf.getSimpleType("p31", "T"));
|
||||||
|
p3.add(tf.getSimpleType("p32"));
|
||||||
|
|
||||||
|
candidates.add(p1);
|
||||||
|
candidates.add(p2);
|
||||||
|
candidates.add(p3);
|
||||||
|
|
||||||
|
Set<TypeParams> result = new HashSet<>();
|
||||||
|
permuteParams(candidates, 0, result, new UnifyType[candidates.size()]);
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user