From 904ca012071c6415f1a561034cd092c12d44d3a1 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Tue, 19 Apr 2016 11:42:15 +0200 Subject: [PATCH] cleaned up --- .../typeinference/unify/TypeUnify.java | 187 +++--------------- 1 file changed, 22 insertions(+), 165 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index b023790b..805d461c 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -153,20 +153,10 @@ public class TypeUnify { if (eqPrime.equals(eq)) eqPrimePrimeSet.add(eqPrime); - else if(eqPrimePrime.isPresent()) { + else if(eqPrimePrime.isPresent()) restartSet.add(eqPrimePrime.get()); - /*Set> subUnifyResult = unify(eqPrimePrime.get(), fc); - eqPrimePrimeSet.addAll(subUnifyResult);*/ - } else restartSet.add(eqPrime); - //eqPrimePrimeSet.addAll(this.unify(eqPrime, fc)); - /*if(eqPrimePrime.isPresent()) - restartSet.add(eqPrimePrime.get()); - else if(!isSolvedForm(eqPrime)) - restartSet.add(eqPrime); - else - eqPrimePrimeSet.add(eqPrime);*/ } /* @@ -176,7 +166,8 @@ public class TypeUnify { // TODO parallelisierung möglich (lohnt sich vermutlich) for(Set eqss : restartSet) eqPrimePrimeSet.addAll(this.unify(eqss, fc)); - + /*restartSet.parallelStream().forEach( + x -> eqPrimePrimeSet.addAll(unify(x, fc)));*/ /* * Step 7: Filter empty sets; */ @@ -400,9 +391,8 @@ public class TypeUnify { cs.add(thetaPrime); for(UnifyType c : cs) { - Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); + Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); thetaQs.add(thetaPrime); - Set thetaQPrimes = new HashSet<>(); TypeParams cParams = c.getTypeParams(); if(cParams.size() == 0) @@ -410,25 +400,6 @@ public class TypeUnify { else { ArrayList> candidateParams = new ArrayList<>(); - /* - * TODO Optimierungsmöglichkeit: - * - * An dieser Stelle gibt es Raum für Optimierung. - * Z.B. resultiert (a <. C) durch Permutation der Parameter mit grArg und smaller in: - * (a = C, b' <.? ? extends Integer) - * (a = C, b' <.? ? extends Integer) - * (a = C, b' <.? Integer) - * usw... - * - * Der erste Fall ist am allgemeinsten und schließt alle anderen Fälle mit ein. Jeder Fall resultiert in einem - * rekursiven Aufruf des Unify. Es würde reichen nur den allgemeinsten Fall zu betrachten da dieser den Lösungraum - * der anderen Fälle miteinschließt. - * - * Prüfen: - * Gibt es einen einzigen maximalen Fall? - * Wahrscheinlich gibt es 2: ? super x (x möglichst klein) und ? ext y (y möglichst groß)) - */ - for(UnifyType param : cParams) candidateParams.add(fc.grArg(param)); @@ -453,7 +424,6 @@ public class TypeUnify { for(UnifyType theta : smaller) { Set resultPrime = new HashSet<>(); - //UnifyType[] freshTphs = new UnifyType[theta.getTypeParams().size()]; for(int i = 0; i < theta.getTypeParams().size(); i++) { if(freshTphs.size()-1 < i) freshTphs.add(PlaceholderType.freshPlaceholder()); @@ -482,70 +452,15 @@ public class TypeUnify { UnifyType aPrime = PlaceholderType.freshPlaceholder(); UnifyType extAPrime = new ExtendsType(aPrime); UnifyType thetaPrime = extThetaPrime.getExtendedType(); - //for(UnifyType theta : fc.smArg(subThetaPrime)) { - Set resultPrime = new HashSet<>(); - //resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(a, thetaPrime, PairOperator.SMALLERDOT)); - result.add(resultPrime); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, thetaPrime, PairOperator.SMALLERDOT)); + result.add(resultPrime); - resultPrime = new HashSet<>(); - resultPrime.add(new UnifyPair(a, extAPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(aPrime, thetaPrime, PairOperator.SMALLERDOT)); - result.add(resultPrime); - //} - + resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, extAPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(aPrime, thetaPrime, PairOperator.SMALLERDOT)); + result.add(resultPrime); return result; - -// Set> result = new HashSet<>(); -// IUnify unify = new MartelliMontanariUnify(); -// -// UnifyType thetaPrime = extThetaPrime.getExtendedType(); -// Set cs = fc.getAllTypesByName(thetaPrime.getName()); -// cs.add(thetaPrime); -// -// for(UnifyType c : cs) { -// Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); -// thetaQs.add(thetaPrime); -// -// Set thetaQPrimes = new HashSet<>(); -// TypeParams cParams = c.getTypeParams(); -// if(cParams.size() == 0) -// thetaQPrimes.add(c); -// else { -// ArrayList> candidateParams = new ArrayList<>(); -// for(UnifyType param : cParams) -// candidateParams.add(fc.grArg(param)); -// -// for(TypeParams tp : permuteParams(candidateParams)) -// thetaQPrimes.add(c.setTypeParams(tp)); -// } -// -// for(UnifyType tqp : thetaQPrimes) { -// Optional opt = unify.unify(tqp, thetaPrime); -// if (!opt.isPresent()) -// continue; -// -// Unifier unifier = opt.get(); -// -// Set substitutionSet = new HashSet<>(); -// for (Entry sigma : unifier.getSubstitutions()) -// substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); -// -// for (UnifyType tq : thetaQs) { -// ExtendsType extTq = new ExtendsType(tq); -// Set smArg = fc.smArg(unifier.apply(extTq)); -// for(UnifyType theta : smArg) { -// Set resultPrime = new HashSet<>(); -// resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT)); -// resultPrime.addAll(substitutionSet); -// result.add(resultPrime); -// } -// } -// -// } -// } -// -// return result; } /** @@ -557,17 +472,14 @@ public class TypeUnify { UnifyType aPrime = PlaceholderType.freshPlaceholder(); UnifyType supAPrime = new SuperType(aPrime); UnifyType thetaPrime = subThetaPrime.getSuperedType(); - //for(UnifyType theta : fc.smArg(subThetaPrime)) { - Set resultPrime = new HashSet<>(); - //resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(thetaPrime, a, PairOperator.SMALLERDOT)); - result.add(resultPrime); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(thetaPrime, a, PairOperator.SMALLERDOT)); + result.add(resultPrime); - resultPrime = new HashSet<>(); - resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); - result.add(resultPrime); - //} + resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); + result.add(resultPrime); return result; } @@ -610,7 +522,6 @@ public class TypeUnify { */ protected Set> unifyCase6(ExtendsType extTheta, PlaceholderType a, IFiniteClosure fc) { Set> result = new HashSet<>(); - //for(UnifyType thetaS : fc.smaller(extTheta.getExtendedType())) { UnifyType freshTph = PlaceholderType.freshPlaceholder(); UnifyType extFreshTph = new ExtendsType(freshTph); @@ -618,7 +529,6 @@ public class TypeUnify { resultPrime.add(new UnifyPair(a, extFreshTph, PairOperator.EQUALSDOT)); resultPrime.add(new UnifyPair(extTheta.getExtendedType(), freshTph, PairOperator.SMALLERDOT)); result.add(resultPrime); - //} return result; } @@ -632,65 +542,12 @@ public class TypeUnify { UnifyType aPrime = PlaceholderType.freshPlaceholder(); UnifyType supAPrime = new SuperType(aPrime); UnifyType theta = supTheta.getSuperedType(); - //for(UnifyType theta : fc.smArg(subThetaPrime)) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(aPrime, theta, PairOperator.SMALLERDOT)); - result.add(resultPrime); - //} + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(aPrime, theta, PairOperator.SMALLERDOT)); + result.add(resultPrime); return result; - -// Set> result = new HashSet<>(); -// IUnify unify = new MartelliMontanariUnify(); -// -// UnifyType theta = supTheta.getSuperedType(); -// Set cs = fc.getAllTypesByName(theta.getName()); -// cs.add(theta); -// -// for(UnifyType c : cs) { -// Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); -// thetaQs.add(theta); -// -// Set thetaQPrimes = new HashSet<>(); -// TypeParams cParams = c.getTypeParams(); -// if(cParams.size() == 0) -// thetaQPrimes.add(c); -// else { -// ArrayList> candidateParams = new ArrayList<>(); -// for(UnifyType param : cParams) -// candidateParams.add(fc.grArg(param)); -// -// for(TypeParams tp : permuteParams(candidateParams)) -// thetaQPrimes.add(c.setTypeParams(tp)); -// } -// -// for(UnifyType tqp : thetaQPrimes) { -// Optional opt = unify.unify(tqp, theta); -// if (!opt.isPresent()) -// continue; -// -// Unifier unifier = opt.get(); -// -// Set substitutionSet = new HashSet<>(); -// for (Entry sigma : unifier.getSubstitutions()) -// substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); -// -// for (UnifyType tq : thetaQs) { -// Set smaller = fc.smaller(unifier.apply(tq)); -// -// for(UnifyType thetaPrime : smaller) { -// Set resultPrime = new HashSet<>(); -// resultPrime.add(new UnifyPair(a, new SuperType(thetaPrime), PairOperator.EQUALSDOT)); -// resultPrime.addAll(substitutionSet); -// result.add(resultPrime); -// } -// } -// -// } -// } -// -// return result; } /**