Fixed Construct. Added Structure for TUnify. Implemented refl.

This commit is contained in:
Aldaron7 2018-05-06 19:06:41 +02:00
parent 48e22982c8
commit ab46709390
13 changed files with 364 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.Token;
@ -27,6 +28,7 @@ import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class Construct extends DefaultASTVisitor {
@ -57,6 +59,10 @@ public class Construct extends DefaultASTVisitor {
public List<SubTypeConstraint> getSubTypeConstraints() {
return subTypeConstraints;
}
public Set<UnifyPair> getSubTypeConstraintsAsUnifyPairs(){
return subTypeConstraints.stream().map(SubTypeConstraint::getAsUnifyPair).collect(Collectors.toSet());
}
private void gatherSubTypeConstraints(ConstraintsSet constraintsSet) {
this.subTypeConstraints.addAll(constraintsSet.getSubTypeConstraints());

View File

@ -0,0 +1,94 @@
package de.dhbwstuttgart.strucTypes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.unify.RuleSet;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import javafx.util.Pair;
public class RuleSetStrucType extends RuleSet {
public Optional<Set<UnifyPair>> reduce(UnifyPair pair) {
return super.reduce2(pair);
}
public Optional<UnifyPair> adapt1(UnifyPair pair, IFiniteClosure fc) {
return super.adapt(pair, fc);
}
public Optional<Set<UnifyPair>> adapt2(Set<UnifyPair> pairs, IFiniteClosure fc) {
// TODO
return null;
}
public boolean erase1(UnifyPair pair, IFiniteClosure fc) {
return super.erase1(pair, fc);
}
public boolean erase2(UnifyPair pair) {
return super.erase3(pair);
}
public Optional<UnifyPair> swap(UnifyPair pair) {
return super.swap(pair);
}
public Optional<Set<UnifyPair>> subst(Set<UnifyPair> pairs) {
return super.subst(pairs);
}
public Optional<Set<UnifyPair>> refl(Set<UnifyPair> pairs) {
// Filter SMALLERDOT Pairs
final Set<UnifyPair> pairsSmallerDot = pairs.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp()))
.collect(Collectors.toSet());
// Filter Tuples of Pairs for a potential Linking (t<T1,T1<T2,...,Tn<t)
List<Pair<UnifyPair, UnifyPair>> tupleOfPotentialLinkings = new ArrayList<>();
for (UnifyPair pair : pairsSmallerDot) {
if (!(pair.getRhsType() instanceof PlaceholderType)) {
continue;
}
tupleOfPotentialLinkings = pairsSmallerDot.stream()
.filter(p -> p.getLhsType() instanceof PlaceholderType && p.getRhsType().equals(pair.getLhsType()))
.map(p -> new Pair<>(pair, p)).collect(Collectors.toList());
}
// Build Linking between the Pairs. Empty if no Linking is possible.
for (Pair<UnifyPair, UnifyPair> tuple : tupleOfPotentialLinkings) {
UnifyPair outerLeftPair = tuple.getKey();
UnifyPair outerRightPair = tuple.getValue();
Optional<Set<UnifyPair>> opt = StrucTypeUnifyUtils.linkPairs(outerLeftPair, outerRightPair,
pairsSmallerDot);
// Remove Pairs of the Linking incl. outerLeft and outerRight, add
// EQUALSDOT Pairs instead.
if (opt.isPresent()) {
pairs.remove(outerLeftPair);
pairs.remove(outerRightPair);
pairs.removeAll(opt.get());
// Get all Placeholder in Linking.
Set<UnifyType> placeholderInLinking = new HashSet<>();
opt.get().forEach(p -> {
placeholderInLinking.add(p.getLhsType());
placeholderInLinking.add(p.getRhsType());
});
// Generate and add new EQUALSDOT Pairs of Placeholder and
// outermost UnifyType to the result.
final UnifyType outermostUnifyType = outerLeftPair.getLhsType();
placeholderInLinking.forEach(placeholder -> pairs
.add(UnifyTypeFactory.generateEqualDotPair(placeholder, outermostUnifyType)));
return Optional.of(pairs);
}
}
// No Linking was found.
return Optional.empty();
}
}

View File

@ -0,0 +1,89 @@
package de.dhbwstuttgart.strucTypes;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class StrucTypeUnify {
private Set<UnifyPair> pairs;
private IFiniteClosure fc;
private RuleSetStrucType rules = new RuleSetStrucType();
public StrucTypeUnify(Set<UnifyPair> unifyPairs, IFiniteClosure finiteClosure) {
this.pairs = unifyPairs;
this.fc = finiteClosure;
}
public Set<UnifyPair> unify() {
Set<UnifyPair> pairs = this.applyTypeUnificationRules(this.pairs, fc);
return pairs;
}
private Set<UnifyPair> applyTypeUnificationRules(Set<UnifyPair> pairs, IFiniteClosure fc) {
Set<UnifyPair> resultSet = new HashSet<>();
LinkedList<UnifyPair> pairQueue = new LinkedList<>();
pairs.forEach(p -> this.swapAddOrErase(p, fc, pairQueue));
while (!pairQueue.isEmpty()) {
UnifyPair pair = pairQueue.pollFirst();
// reduce
Optional<Set<UnifyPair>> optSet = rules.reduce(pair);
if (optSet.isPresent()) {
optSet.get().forEach(p -> swapAddOrErase(p, fc, pairQueue));
continue;
}
// adapt1
Optional<UnifyPair> opt = rules.adapt1(pair, fc);
if (opt.isPresent()) {
this.swapAddOrErase(opt.get(), fc, pairQueue);
continue;
}
// no rule applied
resultSet.add(pair);
}
// adapt2
Optional<Set<UnifyPair>> adapt2 = rules.adapt2(resultSet, fc);
if(adapt2.isPresent()){
resultSet = this.applyTypeUnificationRules(adapt2.get(), fc);
}
// subst
Optional<Set<UnifyPair>> subst = rules.subst(resultSet);
if(subst.isPresent()){
resultSet = this.applyTypeUnificationRules(subst.get(), fc);
}
// refl
Optional<Set<UnifyPair>> refl = rules.refl(resultSet);
if(refl.isPresent()){
resultSet = this.applyTypeUnificationRules(refl.get(), fc);
}
return resultSet;
}
private void swapAddOrErase(UnifyPair pair, IFiniteClosure fc, Collection<UnifyPair> collection) {
// swap
Optional<UnifyPair> opt = rules.swap(pair);
UnifyPair unifyPair = opt.isPresent() ? opt.get() : pair;
// erase
if (rules.erase1(unifyPair, fc) || rules.erase2(unifyPair))
return;
collection.add(unifyPair);
}
}

View File

@ -0,0 +1,36 @@
package de.dhbwstuttgart.strucTypes;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class StrucTypeUnifyUtils {
public static Optional<Set<UnifyPair>> linkPairs(UnifyPair outerLeft, UnifyPair outerRight,
Collection<UnifyPair> pairs) {
Set<UnifyPair> resultLinking = new HashSet<>();
if (outerLeft.getRhsType().equals(outerRight.getLhsType())) {
resultLinking.add(outerLeft);
return Optional.of(resultLinking);
}
List<UnifyPair> nextLinks = pairs.stream().filter(p -> outerLeft.getRhsType().equals(p.getLhsType()))
.collect(Collectors.toList());
for (UnifyPair nextLink : nextLinks) {
Optional<Set<UnifyPair>> opt = linkPairs(nextLink, outerRight, pairs);
if (opt.isPresent()) {
resultLinking = opt.get();
resultLinking.add(nextLink);
return Optional.of(resultLinking);
}
}
return Optional.empty();
}
}

View File

@ -9,6 +9,7 @@ import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Block;
@ -21,7 +22,9 @@ import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class TYPEExpr extends DefaultASTVisitor {
@ -167,9 +170,19 @@ public class TYPEExpr extends DefaultASTVisitor {
"The number of fields (%d) in %s doesn't match the number of arguments (%d) passed to its constructor.",
fieldsSize, newClass.name, argumentsSize));
}
// List of Generics of newClass
List<RefTypeOrTPHOrWildcardOrGeneric> paraList = ((RefType)newClass.getType()).getParaList();
List<FormalParameter> formalparalist = typeExtract.getConstructors().get(0).getParameterList().getFormalparalist();
for (int i = 0; i < argumentsSize; i++) {
SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(),
fields.get(i).getType());
// Bei Fields wird kein GenericRefType sondern Object als Typ geparst.
// SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(),
// fields.get(i).getType());
// Es darf nur einen Konstruktor mit allen Feldern geben.
RefTypeOrTPHOrWildcardOrGeneric supertype = formalparalist.get(i).getType();
supertype = supertype instanceof GenericRefType ? paraList.remove(0) : supertype;
SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(), supertype);
this.constraints.addConstraint(subTypeConstraint);
}
}
@ -204,5 +217,10 @@ public class TYPEExpr extends DefaultASTVisitor {
public void visit(Return aReturn) {
aReturn.retexpr.accept(this);
}
@Override
public void visit(GenericRefType genericRefType){
}
}

View File

@ -2,8 +2,11 @@ package de.dhbwstuttgart.strucTypes.constraint;
import de.dhbwstuttgart.strucTypes.InferredTypes;
import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
/**
* c <. c' : c ist subtyp von c'
@ -52,6 +55,12 @@ public class SubTypeConstraint {
}
return true;
}
public UnifyPair getAsUnifyPair(){
UnifyType tl = UnifyTypeFactory.convert(this.subtype);
UnifyType tr = UnifyTypeFactory.convert(this.supertype);
return UnifyTypeFactory.generateSmallerDotPair(tl, tr);
}
private Class<?> createClass(String name) {
try {

View File

@ -1,11 +1,13 @@
package de.dhbwstuttgart.strucTypes.printutils;
import java.util.List;
import java.util.Set;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class PrintConstraints {
@ -22,6 +24,11 @@ public class PrintConstraints {
// .println(String.format("%s <* %s", c.getSubtype().toString(),
// c.getSupertype().toString())));
}
public void printSubTypeConstraints(Set<UnifyPair> constraints){
System.out.println("\n SubTypeConstraints:");
constraints.forEach(System.out::println);
}
public void printFieldConstraints(List<FieldConstraint> constraints) {
System.out.println("\n FieldConstraints:");

View File

@ -390,7 +390,7 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override
public void visit(GenericRefType genericRefType) {
System.out.println("GenRT " + genericRefType.getParsedName());
System.out.println("GRT " + genericRefType.getParsedName());
}
@Override

View File

@ -17,8 +17,15 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
public String getParsedName(){
return name.toString();
}
@Override
public String toString() {
return "GRT " + this.name ;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}

View File

@ -4,19 +4,20 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.strucTypes.Construct;
import de.dhbwstuttgart.strucTypes.InferredTypes;
import de.dhbwstuttgart.strucTypes.StrucTYPE;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException;
import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints;
import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes;
import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class TestConstruct {
public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/";
@ -52,7 +53,8 @@ public class TestConstruct {
Construct construct = new Construct(constraints, inferredTypesType);
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
final List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
// final List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
Set<UnifyPair> subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs();
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
final SyntaxTreePrinter syntaxTreePrinterInferred = new SyntaxTreePrinter(inferredTypesConstruct);

View File

@ -0,0 +1,43 @@
package strucType;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.strucTypes.RuleSetStrucType;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.ReferenceType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import junit.framework.Assert;
public class TestRuleSetStrucType {
@Test
public void testRefl() {
Set<UnifyPair> pairs = new HashSet<>();
ReferenceType t = new ReferenceType("t");
PlaceholderType T1 = new PlaceholderType("T1");
PlaceholderType T2 = new PlaceholderType("T2");
PlaceholderType T3 = new PlaceholderType("T3");
UnifyPair p1 = UnifyTypeFactory.generateSmallerDotPair(t, T1);
UnifyPair p2 = UnifyTypeFactory.generateSmallerDotPair(T1, T3);
UnifyPair p3 = UnifyTypeFactory.generateSmallerDotPair(T1, T2);
UnifyPair p4 = UnifyTypeFactory.generateSmallerDotPair(T2, t);
pairs.add(p1);
pairs.add(p2);
pairs.add(p3);
pairs.add(p4);
RuleSetStrucType rules = new RuleSetStrucType();
Optional<Set<UnifyPair>> opt = rules.refl(pairs);
// System.out.println(opt.get());
Assert.assertTrue("Refl: " + opt.get(),opt.isPresent());
}
}

View File

@ -0,0 +1,46 @@
package strucType;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.strucTypes.StrucTypeUnifyUtils;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.ReferenceType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import junit.framework.Assert;
public class TestStrucTypeUnifyUtils {
@Test
public void testLinkPairs() {
List<UnifyPair> pairs = new ArrayList<>();
ReferenceType t = new ReferenceType("t");
PlaceholderType T1 = new PlaceholderType("T1");
PlaceholderType T2 = new PlaceholderType("T2");
PlaceholderType T3 = new PlaceholderType("T3");
UnifyPair p1 = UnifyTypeFactory.generateSmallerDotPair(t, T1);
UnifyPair p2 = UnifyTypeFactory.generateSmallerDotPair(T1, T3);
UnifyPair p3 = UnifyTypeFactory.generateSmallerDotPair(T1, T2);
UnifyPair p4 = UnifyTypeFactory.generateSmallerDotPair(T2, t);
pairs.add(p1);
pairs.add(p2);
pairs.add(p3);
pairs.add(p4);
Optional<Set<UnifyPair>> opt = StrucTypeUnifyUtils.linkPairs(p1, p4, pairs);
Assert.assertTrue(opt.isPresent());
Assert.assertTrue(p3 + "is not in LinkedPairs: " + opt.get(), opt.get().contains(p3));
Assert.assertEquals("Expected size = 1; Actual size = " + opt.get().size(), 1, opt.get().size());
}
}

View File

@ -5,6 +5,8 @@ class N
{
a;
N(a){ a = a; }
mA(){ return new A(a); }
mB(x){ return new B(x,a); }
mN(a) {return new N(a); }