From bb016e6417c6487cdb400d1db30452b1f8774d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 5 Mar 2018 20:04:10 +0100 Subject: [PATCH] modified: src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java modified: src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java modified: src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java --- .../unify/interfaces/IFiniteClosure.java | 3 +- .../unify/model/FiniteClosure.java | 8 ++- .../unify/model/OrderingUnifyPair.java | 70 ++++++++++++++----- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 110ff4b8..e97a40e8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -5,6 +5,7 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.FunNType; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; @@ -62,5 +63,5 @@ public interface IFiniteClosure { public Set getChildren(UnifyType t); public Set getAllTypesByName(String typeName); - public int compare(UnifyType rhsType, UnifyType rhsType2); + public int compare(UnifyType rhsType, UnifyType rhsType2, PairOperator pairop); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 6500abcf..c7e197c3 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -435,7 +435,11 @@ public class FiniteClosure extends Ordering implements IFiniteClosure } public int compare (UnifyType left, UnifyType right) { - UnifyPair up = new UnifyPair(left, right, PairOperator.SMALLERDOT); + return compare(left, right, PairOperator.SMALLERDOT); + } + + public int compare (UnifyType left, UnifyType right, PairOperator pairop) { + UnifyPair up = new UnifyPair(left, right, pairop); TypeUnifyTask unifyTask = new TypeUnifyTask(); HashSet hs = new HashSet<>(); hs.add(up); @@ -443,7 +447,7 @@ public class FiniteClosure extends Ordering implements IFiniteClosure long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof PlaceholderType)).count(); if (smallerLen == 0) return -1; else { - up = new UnifyPair(right, left, PairOperator.SMALLERDOT); + up = new UnifyPair(right, left, pairop); //TypeUnifyTask unifyTask = new TypeUnifyTask(); hs = new HashSet<>(); hs.add(up); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 7762af78..a95a790f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -31,10 +31,10 @@ public class OrderingUnifyPair extends Ordering> { if (left == null || right == null) System.out.println("Fehler"); if (left.getLhsType() instanceof PlaceholderType) { - return fc.compare(left.getRhsType(), right.getRhsType()); + return fc.compare(left.getRhsType(), right.getRhsType(), left.getPairOp()); } else { - return fc.compare(left.getLhsType(), right.getLhsType()); + return fc.compare(left.getLhsType(), right.getLhsType(), left.getPairOp()); } } @@ -66,12 +66,43 @@ public class OrderingUnifyPair extends Ordering> { .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) .collect(Collectors.toCollection(HashSet::new)); Set leftle = left.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType) + && x.getPairOp() == PairOperator.SMALLERDOT)) .collect(Collectors.toCollection(HashSet::new)); Set rightle = right.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType) + && x.getPairOp() == PairOperator.SMALLERDOT)) .collect(Collectors.toCollection(HashSet::new)); - if (lefteq.size() > 1) { + Set leftlewc = left.stream() + .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType) + && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + Set rightlewc = right.stream() + .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType) + && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + //Fall 2 und 3 + if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 0 && rightle.size() == 1) { + return 1; + } + //Fall 2 und 3 + if (lefteq.size() == 0 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 1) { + return -1; + } + //Fall 5 + if (lefteq.size() == 1 && leftle.size() == 0 && righteq.size() == 1 && rightle.size() == 1) { + return -1; + } + //Fall 5 + if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 0) { + return 1; + } + //Fall 5 + if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 1) { + return 0; + } + // Nur Paare a =. Theta + if (leftle.size() == 0 && rightle.size() == 0 && leftlewc.size() == 0 && rightlewc.size() ==0) { Stream lseq = lefteq.stream(); //left.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); Stream rseq = righteq.stream(); //right.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; @@ -81,7 +112,8 @@ public class OrderingUnifyPair extends Ordering> { if (!si.isPresent()) return 0; else return si.get(); } - if (lefteq.size() == 1 && righteq.size() == 1) { + //Fall 1 und 4 + if (lefteq.size() == 1 && righteq.size() == 1 && (leftlewc.size() > 0 || rightlewc.size() > 0)) { //TODO 2018-03-02: hier muessen mehree =. eingefuegt werden und alle weiteren Faelle pruefen UnifyPair lseq = lefteq.iterator().next(); UnifyPair rseq = righteq.iterator().next(); @@ -95,26 +127,28 @@ public class OrderingUnifyPair extends Ordering> { Pair> int_Unifier = compare(lseq.getRhsType(), rseq.getRhsType()); Unifier uni = new Unifier(); int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); - if (leftle.size() == 0) return int_Unifier.getKey(); + if (lseq.equals(rseq) || leftlewc.size() == 0 || rightlewc.size() == 0) return int_Unifier.getKey(); else { - Stream lslestr = leftle.stream().map(uni::apply); - Stream rslestr = rightle.stream().map(uni::apply); + Set lsleuni = leftlewc.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new)); + Set rsleuni = rightlewc.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new)); BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; HashMap hm; Optional si; - if (leftle.iterator().next().getLhsType() instanceof PlaceholderType) { - hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); - lslestr = lslestr.filter(x -> !(hm.get(x.getLhsType()) == null)); - si = lslestr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); - } + //1. Fall + if (leftlewc.iterator().next().getLhsType() instanceof PlaceholderType) { + hm = rsleuni.stream().reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); + Stream lslewcstr = lsleuni.stream().filter(x -> !(hm.get(x.getLhsType()) == null)); + si = lslewcstr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + } + //4. Fall else { - hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getRhsType(),y); return x; }, combiner); - rslestr = rslestr.filter(x -> !(hm.get(x.getRhsType()) == null)); - si = rslestr.map(x -> compare(x, hm.get(x.getRhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + hm = rsleuni.stream().reduce(new HashMap(), (x, y)-> { x.put(y.getRhsType(),y); return x; }, combiner); + Stream lslewcstr = lsleuni.stream().filter(x -> !(hm.get(x.getRhsType()) == null)); + si = lslewcstr.map(x -> compare(x, hm.get(x.getRhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); } if (!si.isPresent()) return 0; - else return si.get(); + else if (si.get() == int_Unifier.getKey()) return int_Unifier.getKey(); else return 0; } }