From b0153be1cd89cee6fe9bc8f5db79206965bb11d5 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 24 Oct 2015 17:47:46 +0200 Subject: [PATCH] . --- notizen/stf/Notes | 14 +++- .../unify/interfaces/IFiniteClosure.java | 5 ++ .../unify/interfaces/ITypeMapper.java | 7 ++ .../typeinference/unifynew/TypeMapper.java | 31 +++++++ .../typeinference/unifynew/Unify.java | 68 +++++++++++++++ .../unify/model/FiniteClosure.java | 7 ++ .../typinference/unify/model/MPair.java | 22 +++++ .../typinference/unify/model/MType.java | 84 +++++++++++++++++++ test/unify/UnifyTest.java | 1 - 9 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java create mode 100644 src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java create mode 100644 src/de/dhbwstuttgart/typeinference/unifynew/TypeMapper.java create mode 100644 src/de/dhbwstuttgart/typeinference/unifynew/Unify.java create mode 100644 src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java create mode 100644 src/de/dhbwstuttgart/typinference/unify/model/MPair.java create mode 100644 src/de/dhbwstuttgart/typinference/unify/model/MType.java diff --git a/notizen/stf/Notes b/notizen/stf/Notes index c9fe69a5..81d1c211 100644 --- a/notizen/stf/Notes +++ b/notizen/stf/Notes @@ -13,4 +13,16 @@ - Wie kommen die Mengen des Unify-Algorithmus zustande? Siehe test: /* * Test b <. a, a <. b - */ \ No newline at end of file + */ + + + + + +- 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 \ No newline at end of file diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java new file mode 100644 index 00000000..1270c8ce --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -0,0 +1,5 @@ +package de.dhbwstuttgart.typeinference.unify.interfaces; + +public interface IFiniteClosure { + +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java new file mode 100644 index 00000000..85efc20a --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/ITypeMapper.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.typeinference.unify.interfaces; + +import de.dhbwstuttgart.typeinference.Menge; + +public interface ITypeMapper { + +} diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/TypeMapper.java b/src/de/dhbwstuttgart/typeinference/unifynew/TypeMapper.java new file mode 100644 index 00000000..e1fa193f --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unifynew/TypeMapper.java @@ -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 + * Menge createMapping(Menge) + * Menge resolveMapping(Menge) + * 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 types) { + return new int[0]; + } + +} diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java new file mode 100644 index 00000000..789feacb --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -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> unify(Menge eq, IFiniteClosure fc) { + /* + * Preparations: Create Mapping + */ + Set eq0 = null; + + + /* + * Step 1: Repeated application of reduce, adapt, erase, swap + */ + Set 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 applyTypeUnificationRules(Set eq, IFiniteClosure fc) { + + boolean changedInLastIteration = true; + + HashSet + + + throw new NotImplementedException(); + } +} diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java new file mode 100644 index 00000000..f0e90fdc --- /dev/null +++ b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.typinference.unify.model; + +import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; + +public class FiniteClosure implements IFiniteClosure { + +} diff --git a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java new file mode 100644 index 00000000..49e905af --- /dev/null +++ b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java @@ -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; + } +} diff --git a/src/de/dhbwstuttgart/typinference/unify/model/MType.java b/src/de/dhbwstuttgart/typinference/unify/model/MType.java new file mode 100644 index 00000000..c24b549d --- /dev/null +++ b/src/de/dhbwstuttgart/typinference/unify/model/MType.java @@ -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(); + } +} diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 59671c90..93f0d9c0 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -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();