From 9f3439a15e000a0908e360340c0a7c6c42e7b699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 2 Mar 2018 14:12:06 +0100 Subject: [PATCH] x --- .../typeinference/unify/RuleSet.java | 8 +- .../typeinference/unify/TypeUnifyTask.java | 27 +------ .../unify/model/OrderingUnifyPair.java | 75 ++++++++++++++++--- 3 files changed, 71 insertions(+), 39 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index d739fdb6..d74ef01f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -454,14 +454,14 @@ public class RuleSet implements IRuleSet{ TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); - System.out.println("Pair: " +pair); - System.out.println("typeD: " +typeD); + //System.out.println("Pair: " +pair); + //System.out.println("typeD: " +typeD); //System.out.println("typeDParams: " +typeDParams); - System.out.println("typeDgen: " +typeD); + //System.out.println("typeDgen: " +typeD); //System.out.println("typeDgenParams: " +typeDgenParams); Unifier unif = Unifier.identity(); for(int i = 0; i < typeDParams.size(); i++) { - System.out.println("ADAPT" +typeDgenParams); + //System.out.println("ADAPT" +typeDgenParams); if (typeDgenParams.get(i) instanceof PlaceholderType) unif.add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i)); else System.out.println("ERROR"); diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 418a75b8..091f7aef 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -203,31 +203,6 @@ public class TypeUnifyTask extends RecursiveTask>> { } - Set simplifyEq (Set eqs) { - Set leftVarSmaller = eqs.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType) - && !(x.getRhsType() instanceof PlaceholderType) - && x.getPairOp().equals(PairOperator.SMALLERDOT)).collect(Collectors.toCollection(HashSet::new)); - Set leftVarEq = eqs.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType) - && !(x.getRhsType() instanceof PlaceholderType) - && x.getPairOp().equals(PairOperator.EQUALSDOT)).collect(Collectors.toCollection(HashSet::new)); - Set rightVarSmaller = eqs.stream() - .filter(x -> (x.getRhsType() instanceof PlaceholderType) - && !(x.getLhsType() instanceof PlaceholderType) - && x.getPairOp().equals(PairOperator.SMALLERDOT)).collect(Collectors.toCollection(HashSet::new)); - Set rightVarEq = eqs.stream() - .filter(x -> (x.getRhsType() instanceof PlaceholderType) - && !(x.getLhsType() instanceof PlaceholderType) - && x.getPairOp().equals(PairOperator.EQUALSDOT)).collect(Collectors.toCollection(HashSet::new)); - Set ret = new HashSet<>(); - for (UnifyPair eq : leftVarSmaller) { - eqs.remove(eq); HIER WEITERMACHEN - Stream eqEqualLHS = leftVarSmaller.stream().filter(x -> x.getLhsType().equals(eq.getLhsType())); - } - return ret; - } - Set> unify2(Set> setToFlatten, Set eq, IFiniteClosure fc, boolean parallel) { //Aufruf von computeCartesianRecursive ENDE @@ -635,7 +610,7 @@ public class TypeUnifyTask extends RecursiveTask>> { undefined.add(pair); break; } - first = true; + first = false; } // Filter empty sets or sets that only contain an empty set. diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 5cf69f76..6aa0e7d8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -1,13 +1,20 @@ package de.dhbwstuttgart.typeinference.unify.model; +import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Optional; import java.util.Set; import java.util.function.BinaryOperator; +import java.util.stream.Collectors; import java.util.stream.Stream; +import javafx.util.Pair; + import com.google.common.collect.Ordering; +import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; @@ -26,14 +33,64 @@ public class OrderingUnifyPair extends Ordering> { return fc.compare(left.getRhsType(), right.getRhsType()); } - public int compare (Set left, Set right) { - Stream ls = left.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); - Stream rs = right.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); - BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; - HashMap hm = rs.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); - ls = ls.filter(x -> !(hm.get(x.getLhsType()) == null)); - Optional si = ls.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); - if (!si.isPresent()) return 0; - else return si.get(); + public Pair> compare (UnifyType left, UnifyType right) { + UnifyPair up = new UnifyPair(left, right, PairOperator.SMALLERDOT); + TypeUnifyTask unifyTask = new TypeUnifyTask(); + HashSet hs = new HashSet<>(); + hs.add(up); + Set smallerRes = unifyTask.applyTypeUnificationRules(hs, fc); + long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof PlaceholderType)).count(); + if (smallerLen == 0) return new Pair<>(-1, smallerRes); + else { + up = new UnifyPair(right, left, PairOperator.SMALLERDOT); + //TypeUnifyTask unifyTask = new TypeUnifyTask(); + hs = new HashSet<>(); + hs.add(up); + Set greaterRes = unifyTask.applyTypeUnificationRules(hs, fc); + long greaterLen = greaterRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof PlaceholderType)).count(); + if (greaterLen == 0) return new Pair<>(1, smallerRes); + else return new Pair<>(0, new HashSet<>()); + } } + + public int compare (Set left, Set right) { + if (!true) { + Stream lseq = left.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); + Stream rseq = right.stream().filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); + BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; + HashMap hm = rseq.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); + lseq = lseq.filter(x -> !(hm.get(x.getLhsType()) == null)); + Optional si = lseq.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + if (!si.isPresent()) return 0; + else return si.get(); + } + else { + Optional lseq = left.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)).findFirst(); + Optional rseq = right.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)).findFirst(); + Set lsle = left.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + Set rsle = right.stream() + .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) + .collect(Collectors.toCollection(HashSet::new)); + Pair> int_Unifier = compare(lseq.get().getRhsType(), rseq.get().getRhsType()); + Unifier uni = new Unifier(); + int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); + if (lsle.size() == 0) return int_Unifier.getKey(); + else { + Stream lslestr = lsle.stream().map(uni::apply); + Stream rslestr = rsle.stream().map(uni::apply); + BinaryOperator> combiner = (x,y) -> { x.putAll(y); return x;}; + HashMap hm = rslestr.reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); + lseq = lseq.filter(x -> !(hm.get(x.getLhsType()) == null)); + Optional si = lslestr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + if (!si.isPresent()) return 0; + else return si.get(); + } + + } + } } +