This commit is contained in:
Florian Steurer 2015-10-24 17:47:46 +02:00
parent 05187aa49f
commit b0153be1cd
9 changed files with 237 additions and 2 deletions

View File

@ -14,3 +14,15 @@
/*
* Test b <. a, a <. b
*/
- Equals der Typen schreiben um instanceof Prüfungen zu vermeiden
SPEED UP
- Anwendungsreihenfolge der Regeln (wahrscheinlichste zuerst, evtl ist nach regel 1 regel 2 nie möglich etc...)
- Erase vor Reduce
- Rechenarm vor rechenintensiv

View File

@ -0,0 +1,5 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
public interface IFiniteClosure {
}

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
import de.dhbwstuttgart.typeinference.Menge;
public interface ITypeMapper<T> {
}

View File

@ -0,0 +1,31 @@
package de.dhbwstuttgart.typeinference.unifynew;
import de.dhbwstuttgart.syntaxtree.type.Type;
/*
* TODO: Ist es möglich das Mapping austauschbar zu machen indem man ein IMapper Interface erstellt?
* Zu einem späteren Zeitpunkt prüfen.
*
* interface IMapper<T>
* Menge<T> createMapping(Menge<Type>)
* Menge<Type> resolveMapping(Menge<T>)
* bool isTPH(T)
* bool isSimpleType(T)
* ...
*
*
* Mapping Pair To long
*
* 0-1 Pair Operator
* 2-3 LHS type (00 = Simple, 01 = extends, 02 = super, 03 = function)
* 4-5 RHS type (00 = Simple, 01 = extends, 02 = super, 03 = function)
* 6-35 LHS Simple Type (500.000.000 possible simple types)
* 36-64 RHS Simple Type (500.000.000 possible simple types)
*/
public class TypeMapper {
public int[] createMapping(Iterable<Type> types) {
return new int[0];
}
}

View File

@ -0,0 +1,68 @@
package de.dhbwstuttgart.typeinference.unifynew;
import java.util.HashSet;
import java.util.Set;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.MPair;
/**
* Implementierung des Unifikationsalgorithmus.
* @author Florian Steurer
*/
public class Unify {
public Menge<Menge<Pair>> unify(Menge<Pair> eq, IFiniteClosure fc) {
/*
* Preparations: Create Mapping
*/
Set<MPair> eq0 = null;
/*
* Step 1: Repeated application of reduce, adapt, erase, swap
*/
Set<MPair> eq1 = applyTypeUnificationRules(eq0, fc);
/*
* Step 2: Create subset of pairs where both sides are TPH
*/
/*
* Step 3: Create subset of pairs that where not included in Step 2
*/
/*
* Step 4: Magic
*/
/*
* Step 5: Repeated substitution
*/
/*
* Step 6: a) Restart for pairs where subst was applied
* b) Union over everything
*/
/*
* Step 7: Filter result for solved pairs
*/
throw new NotImplementedException();
}
private Set<MPair> applyTypeUnificationRules(Set<MPair> eq, IFiniteClosure fc) {
boolean changedInLastIteration = true;
HashSet<MPair>
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.typinference.unify.model;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class FiniteClosure implements IFiniteClosure {
}

View File

@ -0,0 +1,22 @@
package de.dhbwstuttgart.typinference.unify.model;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
public class MPair {
public final MType TYPE_1;
public final MType TYPE_2;
public final PairOperator PAIR_OP;
public MPair(MType t1, MType t2) {
TYPE_1 = t1;
TYPE_2 = t2;
PAIR_OP = PairOperator.SmallerExtends;
}
public MPair(MType t1, MType t2, PairOperator pairOp) {
TYPE_1 = t1;
TYPE_2 = t2;
PAIR_OP = pairOp;
}
}

View File

@ -0,0 +1,84 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Arrays;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
/**
*
* @author DH10STF
*
* SPEEDUP
* - Caching of the results
*/
public class MType {
/**
* First three bits indicate the meta type:
* 000b = 0 = Simpletype
* 001b = 1 = Extends
* 010b = 2 = Super
* 011b = 3 = Type Placeholder
* 100b = 4 = Function
*
* Advantages of using a unique identifier for mapping:
* - Fast checks (Equality and Membership)
* - Perfect Hashing
*/
private int identifier = 0;
private int[] typeArgs = null;
/**
* Used to mask the first three bits of an integer.
* -53870912d == 1110000...00b
*/
private final int mask = -53870912;
public MType(int identifier, int...typeArgs) {
this.identifier = identifier;
if(typeArgs.length != 0) {
this.typeArgs = Arrays.copyOf(typeArgs, typeArgs.length);
Arrays.sort(typeArgs);
}
}
public boolean isSimpleType() {
return (identifier & mask) == 0;
}
public boolean isExtendsType() {
return (identifier & mask) == 1;
}
public boolean isSuperType() {
return (identifier & mask) == 2;
}
public boolean isTypePlaceholder() {
return (identifier & mask) == 3;
}
public boolean isFunctionType() {
return (identifier & mask) == 4;
}
public int getIdentifier() {
return identifier;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof MType))
return false;
throw new NotImplementedException();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
}

View File

@ -18,7 +18,6 @@ public class UnifyTest {
@Test
public void unifyTestSimpleTypes() {
// Init Factories and Builders
UnifyTypeFactory typeFactory = new UnifyTypeFactory();
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();