From ae9220c04b43e0536cd345767e4cf602e9a6e8b4 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Wed, 13 Apr 2016 11:13:20 +0200 Subject: [PATCH] made the ruleSet interface functional (added fc in arguments) --- .../typeinference/unify/RuleSet.java | 58 ++++++++----------- .../typeinference/unify/Unify.java | 35 ++++++----- .../unify/interfaces/IRuleSet.java | 16 ++--- 3 files changed, 49 insertions(+), 60 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 8fb5ddf2..a9f82eff 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -30,17 +30,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PairOperator; * @author Florian Steurer * */ -public class RuleSet implements IRuleSet{ - - protected IFiniteClosure finiteClosure; - - /** - * Creates a new instance that uses the specified FC for greater, grArg, etc. - * @param fc The FC that is used for greater, grArg, etc. - */ - public RuleSet(IFiniteClosure fc) { - finiteClosure = fc; - } +public class RuleSet implements IRuleSet{ @Override public Optional reduceUp(UnifyPair pair) { @@ -97,7 +87,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional> reduceExt(UnifyPair pair) { + public Optional> reduceExt(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -119,7 +109,7 @@ public class RuleSet implements IRuleSet{ if(x.getTypeParams().empty() || extY.getTypeParams().size() != x.getTypeParams().size()) return Optional.empty(); - UnifyType xFromFc = finiteClosure.getLeftHandedType(sTypeX.getName()).orElse(null); + UnifyType xFromFc = fc.getLeftHandedType(sTypeX.getName()).orElse(null); if(xFromFc == null || !xFromFc.getTypeParams().arePlaceholders()) return Optional.empty(); @@ -127,7 +117,7 @@ public class RuleSet implements IRuleSet{ if(x instanceof ExtendsType) xFromFc = new ExtendsType(xFromFc); - UnifyType extYFromFc = finiteClosure.grArg(xFromFc).stream().filter(t -> t.getName().equals(extY.getName())).filter(t -> t.getTypeParams().arePlaceholders()).findAny().orElse(null); + UnifyType extYFromFc = fc.grArg(xFromFc).stream().filter(t -> t.getName().equals(extY.getName())).filter(t -> t.getTypeParams().arePlaceholders()).findAny().orElse(null); if(extYFromFc == null || extYFromFc.getTypeParams() != xFromFc.getTypeParams()) return Optional.empty(); @@ -149,7 +139,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional> reduceSup(UnifyPair pair) { + public Optional> reduceSup(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -171,7 +161,7 @@ public class RuleSet implements IRuleSet{ if(x.getTypeParams().empty() || supY.getTypeParams().size() != x.getTypeParams().size()) return Optional.empty(); - UnifyType xFromFc = finiteClosure.getLeftHandedType(sTypeX.getName()).orElse(null); + UnifyType xFromFc = fc.getLeftHandedType(sTypeX.getName()).orElse(null); if(xFromFc == null || !xFromFc.getTypeParams().arePlaceholders()) return Optional.empty(); @@ -179,7 +169,7 @@ public class RuleSet implements IRuleSet{ if(x instanceof SuperType) xFromFc = new SuperType(xFromFc); - UnifyType supYFromFc = finiteClosure.grArg(xFromFc).stream().filter(t -> t.getName().equals(supY.getName())).filter(t -> t.getTypeParams().arePlaceholders()).findAny().orElse(null); + UnifyType supYFromFc = fc.grArg(xFromFc).stream().filter(t -> t.getName().equals(supY.getName())).filter(t -> t.getTypeParams().arePlaceholders()).findAny().orElse(null); if(supYFromFc == null || supYFromFc.getTypeParams() != xFromFc.getTypeParams()) return Optional.empty(); @@ -231,7 +221,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional> reduce1(UnifyPair pair) { + public Optional> reduce1(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -249,12 +239,12 @@ public class RuleSet implements IRuleSet{ if(lhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size()) return Optional.empty(); - UnifyType cFromFc = finiteClosure.getLeftHandedType(c.getName()).orElse(null); + UnifyType cFromFc = fc.getLeftHandedType(c.getName()).orElse(null); if(cFromFc == null || !cFromFc.getTypeParams().arePlaceholders()) return Optional.empty(); - UnifyType dFromFc = finiteClosure.getAncestors(cFromFc).stream().filter(x -> x.getName().equals(d.getName())).findAny().orElse(null); + UnifyType dFromFc = fc.getAncestors(cFromFc).stream().filter(x -> x.getName().equals(d.getName())).findAny().orElse(null); if(dFromFc == null || !dFromFc.getTypeParams().arePlaceholders() || dFromFc.getTypeParams().size() != cFromFc.getTypeParams().size()) return Optional.empty(); @@ -330,7 +320,7 @@ public class RuleSet implements IRuleSet{ } @Override - public boolean erase1(UnifyPair pair) { + public boolean erase1(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return false; @@ -342,18 +332,18 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof ReferenceType) && !(rhsType instanceof PlaceholderType)) return false; - return finiteClosure.greater(lhsType).contains(rhsType); + return fc.greater(lhsType).contains(rhsType); } @Override - public boolean erase2(UnifyPair pair) { + public boolean erase2(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return false; UnifyType lhsType = pair.getLhsType(); UnifyType rhsType = pair.getRhsType(); - return finiteClosure.grArg(lhsType).contains(rhsType); + return fc.grArg(lhsType).contains(rhsType); } @Override @@ -379,7 +369,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional adapt(UnifyPair pair) { + public Optional adapt(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -398,7 +388,7 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); - Optional opt = finiteClosure.getLeftHandedType(typeD.getName()); + Optional opt = fc.getLeftHandedType(typeD.getName()); if(!opt.isPresent()) return Optional.empty(); @@ -406,7 +396,7 @@ public class RuleSet implements IRuleSet{ UnifyType typeDgen = opt.get(); // Actually greater+ because the types are ensured to have different names - Set greater = finiteClosure.getAncestors(typeDgen); + Set greater = fc.getAncestors(typeDgen); opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny(); if(!opt.isPresent()) @@ -425,7 +415,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional adaptExt(UnifyPair pair) { + public Optional adaptExt(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -442,16 +432,16 @@ public class RuleSet implements IRuleSet{ UnifyType typeDgen; if(typeD instanceof ReferenceType) - typeDgen = finiteClosure.getLeftHandedType(typeD.getName()).orElse(null); + typeDgen = fc.getLeftHandedType(typeD.getName()).orElse(null); else { - Optional opt = finiteClosure.getLeftHandedType(((ExtendsType) typeD).getExtendedType().getName()); + Optional opt = fc.getLeftHandedType(((ExtendsType) typeD).getExtendedType().getName()); typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null; } if(typeDgen == null) return Optional.empty(); - Set grArg = finiteClosure.grArg(typeDgen); + Set grArg = fc.grArg(typeDgen); Optional opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny(); @@ -471,7 +461,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional adaptSup(UnifyPair pair) { + public Optional adaptSup(UnifyPair pair, IFiniteClosure fc) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -487,7 +477,7 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); - Optional opt = finiteClosure.getLeftHandedType(((SuperType) typeSupD).getSuperedType().getName()); + Optional opt = fc.getLeftHandedType(((SuperType) typeSupD).getSuperedType().getName()); if(!opt.isPresent()) return Optional.empty(); @@ -497,7 +487,7 @@ public class RuleSet implements IRuleSet{ // Use of smArg instead of grArg because // a in grArg(b) => b in smArg(a) - Set smArg = finiteClosure.smArg(typeSupDgen); + Set smArg = fc.smArg(typeSupDgen); opt = smArg.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny(); if(!opt.isPresent()) diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index 71734187..c1c81ed0 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -33,7 +33,9 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; public class Unify { protected ISetOperations setOps = new GuavaSetOperations(); - + protected IUnify stdUnify = new MartelliMontanariUnify(); + protected IRuleSet rules = new RuleSet(); + public Set> unify(Set eq, IFiniteClosure fc) { /* * Step 1: Repeated application of reduce, adapt, erase, swap @@ -123,7 +125,6 @@ public class Unify { eqPrimeSetFlat.add(buffer); } - IRuleSet rules = new RuleSet(fc); Set> restartSet = new HashSet<>(); Set> eqPrimePrimeSet = new HashSet<>(); @@ -193,12 +194,11 @@ public class Unify { LinkedHashSet targetSet = new LinkedHashSet(); LinkedList eqQueue = new LinkedList<>(); - IRuleSet rules = new RuleSet(fc); /* * Swap all pairs and erase all erasable pairs */ - eq.forEach(x -> swapAddOrErase(x, rules, eqQueue)); + eq.forEach(x -> swapAddOrErase(x, fc, eqQueue)); /* * Apply rules until the queue is empty @@ -220,31 +220,31 @@ public class Unify { // One of the rules has been applied if(opt.isPresent()) { - swapAddOrErase(opt.get(), rules, eqQueue); + swapAddOrErase(opt.get(), fc, eqQueue); continue; } // Reduce1, Reduce2, ReduceExt, ReduceSup, ReduceEq - Optional> optSet = rules.reduce1(pair); + Optional> optSet = rules.reduce1(pair, fc); optSet = optSet.isPresent() ? optSet : rules.reduce2(pair); - optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair); - optSet = optSet.isPresent() ? optSet : rules.reduceSup(pair); + optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair, fc); + optSet = optSet.isPresent() ? optSet : rules.reduceSup(pair, fc); optSet = optSet.isPresent() ? optSet : rules.reduceEq(pair); // One of the rules has been applied if(optSet.isPresent()) { - optSet.get().forEach(x -> swapAddOrErase(x, rules, eqQueue)); + optSet.get().forEach(x -> swapAddOrErase(x, fc, eqQueue)); continue; } // Adapt, AdaptExt, AdaptSup - opt = rules.adapt(pair); - opt = opt.isPresent() ? opt : rules.adaptExt(pair); - opt = opt.isPresent() ? opt : rules.adaptSup(pair); + opt = rules.adapt(pair, fc); + opt = opt.isPresent() ? opt : rules.adaptExt(pair, fc); + opt = opt.isPresent() ? opt : rules.adaptSup(pair, fc); // One of the rules has been applied if(opt.isPresent()) { - swapAddOrErase(opt.get(), rules, eqQueue); + swapAddOrErase(opt.get(), fc, eqQueue); continue; } @@ -255,11 +255,11 @@ public class Unify { return targetSet; } - protected void swapAddOrErase(UnifyPair pair, IRuleSet rules, Collection collection) { + protected void swapAddOrErase(UnifyPair pair, IFiniteClosure fc, Collection collection) { Optional opt = rules.swap(pair); UnifyPair pair2 = opt.isPresent() ? opt.get() : pair; - if(rules.erase1(pair2) || rules.erase3(pair2) || rules.erase2(pair2)) + if(rules.erase1(pair2, fc) || rules.erase3(pair2) || rules.erase2(pair2, fc)) return; collection.add(pair2); @@ -332,8 +332,7 @@ public class Unify { protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { Set> result = new HashSet<>(); - IUnify unify = new MartelliMontanariUnify(); - + Set cs = fc.getAllTypesByName(thetaPrime.getName()); cs.add(thetaPrime); @@ -375,7 +374,7 @@ public class Unify { } for(UnifyType tqp : thetaQPrimes) { - Optional opt = unify.unify(tqp, thetaPrime); + Optional opt = stdUnify.unify(tqp, thetaPrime); if (!opt.isPresent()) continue; diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index 26ea0dce..714cb989 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -14,10 +14,10 @@ public interface IRuleSet { public Optional reduceUp(UnifyPair pair); public Optional reduceLow(UnifyPair pair); public Optional reduceUpLow(UnifyPair pair); - public Optional> reduceExt(UnifyPair pair); - public Optional> reduceSup(UnifyPair pair); + public Optional> reduceExt(UnifyPair pair, IFiniteClosure fc); + public Optional> reduceSup(UnifyPair pair, IFiniteClosure fc); public Optional> reduceEq(UnifyPair pair); - public Optional> reduce1(UnifyPair pair); + public Optional> reduce1(UnifyPair pair, IFiniteClosure fc); public Optional> reduce2(UnifyPair pair); /* @@ -42,13 +42,13 @@ public interface IRuleSet { * Checks whether the erase1-Rule applies to the pair. * @return True if the pair is erasable, false otherwise. */ - public boolean erase1(UnifyPair pair); + public boolean erase1(UnifyPair pair, IFiniteClosure fc); /** * Checks whether the erase2-Rule applies to the pair. * @return True if the pair is erasable, false otherwise. */ - public boolean erase2(UnifyPair pair); + public boolean erase2(UnifyPair pair, IFiniteClosure fc); /** * Checks whether the erase3-Rule applies to the pair. @@ -58,9 +58,9 @@ public interface IRuleSet { public Optional swap(UnifyPair pair); - public Optional adapt(UnifyPair pair); - public Optional adaptExt(UnifyPair pair); - public Optional adaptSup(UnifyPair pair); + public Optional adapt(UnifyPair pair, IFiniteClosure fc); + public Optional adaptExt(UnifyPair pair, IFiniteClosure fc); + public Optional adaptSup(UnifyPair pair, IFiniteClosure fc); /** * Applies the subst-Rule to a set of pairs (usually Eq').