From 8e198679c76abdabb81fb8dec051f5c4825e369e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 23 Feb 2018 00:37:59 +0100 Subject: [PATCH] Instabiler Zustand reduce funktionert nicht --- .../SyntaxTreeGenerator/FCGenerator.java | 2 +- .../typeinference/unify/RuleSet.java | 37 ++++++++- .../typeinference/unify/TypeUnifyTask.java | 82 ++++++++++++++----- .../unify/model/FiniteClosure.java | 1 + 4 files changed, 99 insertions(+), 23 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 7863c8ed6..37bfb4da1 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -24,7 +24,7 @@ public class FCGenerator { for(ClassOrInterface cly : availableClasses){ pairs.addAll(getSuperTypes(cly, availableClasses)); } - System.out.println(pairs); + System.out.println("Pairs: " + pairs); return pairs; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index b3cb0dec0..f5ddd32a1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -25,12 +25,25 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.WildcardType; +import java.io.FileWriter; +import java.io.IOException; + /** * Implementation of the type inference rules. * @author Florian Steurer * */ -public class RuleSet implements IRuleSet{ +public class RuleSet implements IRuleSet{ + + FileWriter logFile; + + RuleSet() { + super(); + } + + RuleSet(FileWriter logFile) { + this.logFile = logFile; + } @Override public Optional reduceUp(UnifyPair pair) { @@ -236,10 +249,17 @@ public class RuleSet implements IRuleSet{ ReferenceType lhsSType = (ReferenceType) c; ReferenceType rhsSType = (ReferenceType) d; + try { + logFile.write("PAIR Rules: " + pair + "\n"); + logFile.flush(); + } + catch (IOException e) { } + if(lhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size()) return Optional.empty(); UnifyType cFromFc = fc.getLeftHandedType(c.getName()).orElse(null); + 2018-02-23: liefert Vector>: Das kann nicht sein. //NOCHMAL UEBERPRUEFEN //PL 18-02-09 Eingfuegt Anfang @@ -255,14 +275,27 @@ public class RuleSet implements IRuleSet{ } //PL 18-02-09 Eingfuegt ENDE + try { + logFile.write("cFromFc: " + cFromFc); + logFile.flush(); + } + catch (IOException e) { } + if(cFromFc == null || !cFromFc.getTypeParams().arePlaceholders()) return Optional.empty(); UnifyType dFromFc = fc.getAncestors(cFromFc).stream().filter(x -> x.getName().equals(d.getName())).findAny().orElse(null); + try { + logFile.write("cFromFc: " + cFromFc); + logFile.flush(); + } + catch (IOException e) { } + if(dFromFc == null || !dFromFc.getTypeParams().arePlaceholders() || dFromFc.getTypeParams().size() != cFromFc.getTypeParams().size()) return Optional.empty(); - + System.out.println("cFromFc: " + cFromFc); + System.out.println("dFromFc: " + dFromFc); int[] pi = pi(cFromFc.getTypeParams(), dFromFc.getTypeParams()); if(pi.length == 0) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 4c559a63d..bd1dad1ef 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -26,15 +26,23 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + /** * Implementation of the type unification algorithm * @author Florian Steurer */ -public class TypeUnifyTask extends RecursiveTask>> { +public class TypeUnifyTask extends RecursiveTask>> { private static final long serialVersionUID = 1L; private static int i = 0; + private boolean printtag = false; + + public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/"; + FileWriter logFile; /** * The implementation of setOps that will be used during the unification @@ -49,7 +57,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * The implementation of the rules that will be used during the unification. */ - protected IRuleSet rules = new RuleSet(); + protected IRuleSet rules; protected Set eq; @@ -61,6 +69,13 @@ public class TypeUnifyTask extends RecursiveTask>> { this.eq = eq; this.fc = fc; this.parallel = parallel; + try { + logFile = new FileWriter(new File(rootDirectory+"log")); + logFile.write("xxx"); + logFile.flush(); + rules = new RuleSet(logFile); + } + catch (IOException e) { } } @Override @@ -74,11 +89,11 @@ public class TypeUnifyTask extends RecursiveTask>> { * @param fc The finite closure * @return The set of all principal type unifiers */ - protected Set> unify(Set eq, IFiniteClosure fc, boolean parallel) { + protected Set> unify(Set eq, IFiniteClosure fc, boolean parallel) { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ - System.out.println("Unifikation: " + eq); + if (printtag) System.out.println("Unifikation: " + eq); Set eq0 = applyTypeUnificationRules(eq, fc); /* @@ -122,17 +137,17 @@ public class TypeUnifyTask extends RecursiveTask>> { // Sets that originate from pair pattern matching // Sets of the "second level" Set undefinedPairs = new HashSet<>(); - System.out.println("eq2s " + eq2s); + if (printtag) System.out.println("eq2s " + eq2s); Set>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs); //PL 2017-09-20: Im calculatePairSets wird möglicherweise O .< java.lang.Integer //nicht ausgewertet Faculty Beispiel im 1. Schritt //PL 2017-10-03 geloest, muesste noch mit FCs mit kleineren //Typen getestet werden. - System.out.println("secondLevelSets:" +secondLevelSets); + if (printtag) System.out.println("secondLevelSets:" +secondLevelSets); // If pairs occured that did not match one of the cartesian product cases, // those pairs are contradictory and the unification is impossible. if(!undefinedPairs.isEmpty()) { - System.out.println(undefinedPairs); + if (printtag) System.out.println("UndefinedPairs; " + undefinedPairs); return new HashSet<>(); } @@ -160,7 +175,7 @@ public class TypeUnifyTask extends RecursiveTask>> { topLevelSets.add(flat); } //System.out.println(topLevelSets); - System.out.println(); + //System.out.println(); //Aufruf von computeCartesianRecursive ANFANG @@ -173,7 +188,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //Set> unify2(Set> setToFlatten, Set eq, IFiniteClosure fc, boolean parallel) { //Aufruf von computeCartesianRecursive ENDE - + //keine Ahnung woher das kommt //Set> setToFlatten = topLevelSets.stream().map(x -> x.iterator().next()).collect(Collectors.toCollection(HashSet::new)); //Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG @@ -223,16 +238,31 @@ public class TypeUnifyTask extends RecursiveTask>> { } } else { // sequentiell (Step 6b is included) - System.out.println("nextStep: " + eqPrime); + if (printtag) System.out.println("nextStep: " + eqPrimePrime); if (eqPrime.equals(eq)) //PL 2017-09-29 auskommentiert und durch //(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt //Begruendung: Wenn in der Substitution keine Veraenderung //(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt. eqPrimePrimeSet.add(eqPrime); - else if(eqPrimePrime.isPresent()) - eqPrimePrimeSet.addAll(unify(eqPrimePrime.get(), fc, false)); - else - eqPrimePrimeSet.addAll(unify(eqPrime, fc, false)); + else if(eqPrimePrime.isPresent()) { + Set> unifyres = unify(eqPrimePrime.get(), fc, false); + try { + logFile.write(unifyres.toString()+"\n"); + //logFile.flush(); + } + catch (IOException e) { } + eqPrimePrimeSet.addAll(unifyres); + } + else { + Set> unifyres = unify(eqPrime, fc, false); + try { + //logFile.write(unifyres.toString()+"\n"); + logFile.flush(); + } + catch (IOException e) { } + + eqPrimePrimeSet.addAll(unifyres); + } } //Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG } @@ -352,6 +382,12 @@ public class TypeUnifyTask extends RecursiveTask>> { } // Reduce1, Reduce2, ReduceExt, ReduceSup, ReduceEq + try { + logFile.write("PAIR1 " + pair + "\n"); + logFile.flush(); + } + catch (IOException e) { } + Optional> optSet = rules.reduce1(pair, fc); optSet = optSet.isPresent() ? optSet : rules.reduce2(pair); optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair, fc); @@ -375,6 +411,11 @@ public class TypeUnifyTask extends RecursiveTask>> { } // Adapt, AdaptExt, AdaptSup + try { + logFile.write("PAIR2 " + pair + "\n"); + logFile.flush(); + } + catch (IOException e) { } opt = rules.adapt(pair, fc); opt = opt.isPresent() ? opt : rules.adaptExt(pair, fc); opt = opt.isPresent() ? opt : rules.adaptSup(pair, fc); @@ -445,7 +486,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 1: (a <. Theta') if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { //System.out.println(pair); - if (first) {System.out.println(pair); + if (first) {if (printtag) System.out.println(pair); Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc); //System.out.println(x1); result.get(0).add(x1); @@ -461,7 +502,7 @@ public class TypeUnifyTask extends RecursiveTask>> { } // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType) - if (first) {System.out.println(pair); + if (first) {if (printtag) System.out.println(pair); result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), fc)); } else { @@ -474,7 +515,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 3: (a <.? ? sup Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) - if (first) {System.out.println(pair); + if (first) {if (printtag) System.out.println(pair); result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, fc)); } else { @@ -492,7 +533,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 5: (Theta <. a) else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) - if (first) {System.out.println(pair); + if (first) {if (printtag) System.out.println(pair); result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, fc)); } else { @@ -515,8 +556,9 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 8: (Theta <.? a) else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) - if (first) {System.out.println(pair); - result.get(7).add(unifyCase8(lhsType, (PlaceholderType) rhsType, fc)); + if (first) {if (printtag) System.out.println(pair); + result.get(7).add( + unifyCase8(lhsType, (PlaceholderType) rhsType, fc)); } else { Set s1 = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index dcffbee6f..704f600fd 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -37,6 +37,7 @@ public class FiniteClosure implements IFiniteClosure { * The initial pairs of that define the inheritance tree */ private Set pairs; + 2018-02-23: Muessten hier NUR die initiale Piars rein? /** * Creates a new instance using the inheritance tree defined in the pairs.