From a263ba5fd493167f78d06f6c98911b326233429b Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 7 Nov 2015 16:49:20 +0100 Subject: [PATCH] reduceSup --- notizen/stf/Notes | 3 + .../typeinference/unifynew/RuleSet.java | 72 +++++++++++++-- test/unify/RuleSetTest.java | 91 +++++++++++++++++++ 3 files changed, 160 insertions(+), 6 deletions(-) diff --git a/notizen/stf/Notes b/notizen/stf/Notes index ddca19be..14e847e5 100644 --- a/notizen/stf/Notes +++ b/notizen/stf/Notes @@ -36,6 +36,9 @@ Gilt reduce f - Typen sind anhand ihres identifiers durchgängig identifizierbar +- ReduceEq nur auf SimpleTypes oder auf alles anwenden? (Momentan alles) +- ReduceEq Permutation? + EED UP - Anwendungsreihenfolge der Regeln (wahrscheinlichste zuerst, evtl ist nach regel 1 regel 2 nie möglich etc...) - Erase vor Reduce diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 2b642f43..608f5584 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -116,14 +116,72 @@ public class RuleSet implements IRuleSet{ @Override public Optional> reduceSup(MPair pair) { - // TODO Auto-generated method stub - return null; + if(pair.getPairOp() != PairOperator.SMALLERDOTWC) + return Optional.empty(); + + Type lhsType = pair.getLhsType(); + SimpleType lhsSType; + + if(lhsType instanceof SimpleType) + lhsSType = (SimpleType) lhsType; + else if(lhsType instanceof SuperType) + lhsSType = (SimpleType) ((SuperType) lhsType).getSuperedType(); + else + return Optional.empty(); + + if(lhsSType.getTypeParams().empty()) + return Optional.empty(); + + Type rhsType = pair.getRhsType(); + + if(!(rhsType instanceof SuperType)) + return Optional.empty(); + + SimpleType rhsSType = (SimpleType) ((SuperType) rhsType).getSuperedType(); + + if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) + return Optional.empty(); + + int[] pi = pi(lhsSType, rhsType); + + if(pi.length == 0) + return Optional.empty(); + + Type[] rhsTypeParams = rhsSType.getTypeParams().asArray(); + Type[] lhsTypeParams = lhsSType.getTypeParams().asArray(); + Set result = new HashSet<>(); + + for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++) + result.add(new MPair(lhsTypeParams[rhsIdx], rhsTypeParams[pi[rhsIdx]], PairOperator.SMALLERDOTWC)); + + return Optional.of(result); } @Override public Optional> reduceEq(MPair pair) { - // TODO Auto-generated method stub - return null; + if(pair.getPairOp() != PairOperator.SMALLERDOTWC) + return Optional.empty(); + + Type lhsType = pair.getLhsType(); + if(lhsType instanceof PlaceholderType || lhsType.getTypeParams().empty()) + return Optional.empty(); + + Type rhsType = pair.getRhsType(); + if(rhsType instanceof PlaceholderType || rhsType.getTypeParams().empty()) + return Optional.empty(); + + if(rhsType.getTypeParams().size() != lhsType.getTypeParams().size()) + return Optional.empty(); + + // TODO Permutation? + Set result = new HashSet<>(); + Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); + Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); + + for(int i = 0; i < lhsTypeParams.length; i++) + result.add(new MPair(lhsTypeParams[i], rhsTypeParams[i], PairOperator.EQUALSDOT)); + + return Optional.of(result); } @Override @@ -295,15 +353,17 @@ public class RuleSet implements IRuleSet{ Optional opt = Optional.empty(); if(D instanceof ExtendsType) { SimpleType dSType = (SimpleType) ((ExtendsType) D).getExtendedType(); + + Set t = finiteClosure.grArg(cFromFc); opt = finiteClosure.grArg(cFromFc).stream() .filter(x -> x instanceof ExtendsType) - .filter(x -> ((ExtendsType) x).getExtendedType().equals(dSType.getName())).findAny(); + .filter(x -> ((ExtendsType) x).getExtendedType().getName().equals(dSType.getName())).findAny(); } else if(D instanceof SuperType) { SimpleType dSType = (SimpleType) ((SuperType) D).getSuperedType(); opt = finiteClosure.grArg(cFromFc).stream() .filter(x -> x instanceof SuperType) - .filter(x -> ((SuperType) x).getSuperedType().equals(dSType.getName())).findAny(); + .filter(x -> ((SuperType) x).getSuperedType().getName().equals(dSType.getName())).findAny(); } else if (D instanceof SimpleType) opt = finiteClosure.greater(cFromFc).stream() diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index 5a8ee772..e561dfee 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unifynew.RuleSet; +import de.dhbwstuttgart.typinference.unify.model.ExtendsType; import de.dhbwstuttgart.typinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; @@ -42,6 +43,16 @@ public class RuleSetTest { System.out.println(res); } + @Test + public void testReduceLow() { + + } + + @Test + public void testReduceUpLow() { + + } + @Test public void testReduce1() { Set pairs = new HashSet(); @@ -74,5 +85,85 @@ public class RuleSetTest { System.out.println(res); } + @Test + public void testReduce2() { + + } + @Test + public void testReduceExt() { + Set pairs = new HashSet(); + + SimpleType t1 = new SimpleType("t1"); + SimpleType t2 = new SimpleType("t2"); + SimpleType t3 = new SimpleType("t3"); + SimpleType t4 = new SimpleType("t4"); + SimpleType t5 = new SimpleType("t5"); + SimpleType t6 = new SimpleType("t6"); + SimpleType t7 = new SimpleType("t7"); + SimpleType t8 = new SimpleType("t8"); + SimpleType t9 = new SimpleType("t9"); + + SimpleType ta = new SimpleType("a", t1, t2, t3); + SimpleType taa = new SimpleType("a", t4, t5, t6); + SimpleType tb = new SimpleType("b", t3, t1, t2); + SimpleType tbb = new SimpleType("b", t7, t8, t9); + + + pairs.add(new MPair(ta, tb, PairOperator.SMALLER)); + FiniteClosure fc = new FiniteClosure(pairs); + + IRuleSet rules = new RuleSet(fc); + + MPair test = new MPair(taa, new ExtendsType(tbb), PairOperator.SMALLERDOTWC); + + Optional> res = rules.reduceExt(test); + + System.out.println(res); + } + + @Test + public void testReduceSup() { + + } + + @Test + public void testReduceEq() { + + } + + @Test + public void testErase1() { + + } + + @Test + public void testErase2() { + + } + + @Test + public void testErase3() { + + } + + @Test + public void testSwap() { + + } + + @Test + public void testAdapt() { + + } + + @Test + public void testAdaptExt() { + + } + + @Test + public void testAdaptSup() { + + } }