From 52f480147ec54e2d97826fbaf841ae407b36b1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Wed, 4 Apr 2018 01:14:30 +0200 Subject: [PATCH] modified: src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java modified: src/de/dhbwstuttgart/typeinference/unify/RuleSet.java modified: src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java modified: src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java modified: src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java Subsitution auf UnifyPair umgestellt --- .../unify/MartelliMontanariUnify.java | 2 +- .../typeinference/unify/RuleSet.java | 4 ++-- .../unify/model/OrderingUnifyPair.java | 8 +++---- .../typeinference/unify/model/Unifier.java | 23 +++++++++++++++---- .../typeinference/unify/model/UnifyPair.java | 14 ++++++++--- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java index aa5f6ddd..087b8943 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java @@ -95,7 +95,7 @@ public class MartelliMontanariUnify implements IUnify { if(lhsType instanceof PlaceholderType) { mgu.add((PlaceholderType) lhsType, rhsType); //PL 2018-04-01 nach checken, ob es richtig ist, dass keine Substitutionen uebergeben werden muessen. - termsList = termsList.stream().map(x -> mgu.apply(new HashSet<>(), x)).collect(Collectors.toCollection(ArrayList::new)); + termsList = termsList.stream().map(x -> mgu.apply(x)).collect(Collectors.toCollection(ArrayList::new)); idx = idx+1 == termsList.size() ? 0 : idx+1; continue; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 62473eff..0d77d48f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -636,8 +636,8 @@ public class RuleSet implements IRuleSet{ && typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair. && !rhsType.getTypeParams().occurs(lhsType)) { Unifier uni = new Unifier(lhsType, rhsType); - result = result.stream().map(x -> uni.apply(pair.getSubstitution(),x)).collect(Collectors.toCollection(ArrayList::new)); - result1 = result1.stream().map(x -> uni.apply(pair.getSubstitution(),x)).collect(Collectors.toCollection(LinkedList::new)); + result = result.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(ArrayList::new)); + result1 = result1.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(LinkedList::new)); applied = true; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index e9896667..505c3c2b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -151,8 +151,8 @@ public class OrderingUnifyPair extends Ordering> { if (!lseq.getRhsType().getName().equals(rseq.getRhsType().getName()) || leftlewc.size() == 0 || rightlewc.size() == 0) return int_Unifier.getKey(); else { - Set lsleuni = leftlewc.stream().map(x -> uni.apply(new HashSet<>(),x)).collect(Collectors.toCollection(HashSet::new)); - Set rsleuni = rightlewc.stream().map(x -> uni.apply(new HashSet<>(),x)).collect(Collectors.toCollection(HashSet::new)); + Set lsleuni = leftlewc.stream().map(x -> uni.apply(x)).collect(Collectors.toCollection(HashSet::new)); + Set rsleuni = rightlewc.stream().map(x -> uni.apply(x)).collect(Collectors.toCollection(HashSet::new)); BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; HashMap hm; @@ -183,7 +183,7 @@ public class OrderingUnifyPair extends Ordering> { } Unifier uni = new Unifier(); subst.stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); - lseq = uni.apply(new HashSet<>(), lseq); + lseq = uni.apply(lseq); } else { Set subst; @@ -195,7 +195,7 @@ public class OrderingUnifyPair extends Ordering> { } Unifier uni = new Unifier(); subst.stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); - rseq = uni.apply(new HashSet<>(), rseq); + rseq = uni.apply(rseq); } return compareEq(lseq, rseq); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index d9b31e24..cbe0053f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -64,13 +64,26 @@ public class Unifier implements Function, Iterable suniSubstitution, UnifyPair p) { + public UnifyPair apply(UnifyPair p) { UnifyType newLhs = this.apply(p.getLhsType()); UnifyType newRhs = this.apply(p.getRhsType()); - if (!(p.getLhsType().equals(newLhs)) || !(p.getRhsType().equals(newRhs))) {//Die Anwedung von this hat was veraendert PL 2018-04-01 - Set suniUnifyPair = new HashSet<>(); - suniUnifyPair.addAll(suniSubstitution); - suniUnifyPair.add(this); + return new UnifyPair(newLhs, newRhs, p.getPairOp()); + } + + /** + * Applies the unifier to the two terms of the pair. + * @return A new pair where the left and right-hand side are applied + */ + public UnifyPair apply(UnifyPair thisAsPair, UnifyPair p) { + UnifyType newLhs = this.apply(p.getLhsType()); + UnifyType newRhs = this.apply(p.getRhsType()); + if (!(p.getLhsType().equals(newLhs)) || !(p.getRhsType().equals(newRhs))) {//Die Anwendung von this hat was veraendert PL 2018-04-01 + Set suniUnifyPair = new HashSet<>(); + suniUnifyPair.addAll(thisAsPair.getSubstitution()); + suniUnifyPair.add(thisAsPair); + if (p.getLhsType() instanceof PlaceholderType && newLhs instanceof PlaceholderType && p.getPairOp() == PairOperator.EQUALSDOT) { + suniUnifyPair.add(p); //p koennte auch subsitution sein + } return new UnifyPair(newLhs, newRhs, p.getPairOp(), suniUnifyPair, p); } return new UnifyPair(newLhs, newRhs, p.getPairOp(), p.getSubstitution(), p.getBasePair()); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index 11d11ba1..8f1c6d57 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -43,7 +43,7 @@ public class UnifyPair { * Unifier/substitute that generated this pair * PL 2018-03-15 */ - private Set substitution; + private Set substitution; /** * Base on which the the unifier is applied @@ -69,7 +69,7 @@ public class UnifyPair { hashCode = 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode(); } - public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set uni, UnifyPair base) { + public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set uni, UnifyPair base) { this.lhs = lhs; this.rhs = rhs; pairOp = op; @@ -114,7 +114,7 @@ public class UnifyPair { public void setUndefinedPair() { undefinedPair = true; } - public Set getSubstitution() { + public Set getSubstitution() { return substitution; } @@ -124,6 +124,14 @@ public class UnifyPair { public boolean isUndefinedPair() { return undefinedPair; } + + Set getAllSubstitutions () { + Set ret = new HashSet<>(); + ret.addAll(getSubstitution()); + + return ret; + } + @Override public boolean equals(Object obj) { if(!(obj instanceof UnifyPair))