From 82c0736feea0c464f85e7113e1b76af86d2e3082 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Fri, 13 Nov 2015 23:24:22 +0100 Subject: [PATCH] adapt Rule --- notizen/stf/Notes | 7 +- .../unify/interfaces/IRuleSet.java | 9 +- .../unify/interfaces/IUnifier.java | 9 -- .../typeinference/unifynew/RuleSet.java | 110 ++++++++++++------ .../typeinference/unifynew/Unify.java | 11 -- 5 files changed, 84 insertions(+), 62 deletions(-) delete mode 100644 src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java diff --git a/notizen/stf/Notes b/notizen/stf/Notes index 14e847e51..35b148713 100644 --- a/notizen/stf/Notes +++ b/notizen/stf/Notes @@ -34,6 +34,10 @@ Gilt reduce f +++++++++++++++++++++++++++++++++++++++++++++++ +HashCode implementierungen testen (type paramerte mit einbeziehen, hashcodes cachen -> da immutable) + + ++++++++++++++++++++++++++++++++++++++++++++++++ - Typen sind anhand ihres identifiers durchgängig identifizierbar - ReduceEq nur auf SimpleTypes oder auf alles anwenden? (Momentan alles) @@ -42,4 +46,5 @@ Gilt reduce f EED UP - Anwendungsreihenfolge der Regeln (wahrscheinlichste zuerst, evtl ist nach regel 1 regel 2 nie möglich etc...) - Erase vor Reduce - - Rechenarm vor rechenintensiv \ No newline at end of file + - Rechenarm vor rechenintensiv + diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index ad0655ef2..a3f279f6e 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; import java.util.Optional; import java.util.Set; +import de.dhbwstuttgart.typeinference.unifynew.Unifier; import de.dhbwstuttgart.typinference.unify.model.MPair; public interface IRuleSet { @@ -22,9 +23,9 @@ public interface IRuleSet { public Optional swap(MPair pair); - public MPair adapt(MPair pair); - public MPair adaptExt(MPair pair); - public MPair adaptSup(MPair pair); + public Optional adapt(MPair pair); + public Optional adaptExt(MPair pair); + public Optional adaptSup(MPair pair); - public Optional subst(MPair pair); + public Optional subst(MPair pair); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java deleted file mode 100644 index da18fa8fa..000000000 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify.interfaces; - -import java.util.function.Function; - -import de.dhbwstuttgart.typinference.unify.model.MPair; - -public interface IUnifier extends Function{ - -} diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 43e33640b..203bbee09 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -1,13 +1,14 @@ package de.dhbwstuttgart.typeinference.unifynew; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Optional; import java.util.Set; import junit.framework.Assert; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; -import de.dhbwstuttgart.typeinference.unify.interfaces.IUnifier; import de.dhbwstuttgart.typinference.unify.model.ExtendsType; import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator; @@ -15,6 +16,7 @@ import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typinference.unify.model.SimpleType; import de.dhbwstuttgart.typinference.unify.model.SuperType; import de.dhbwstuttgart.typinference.unify.model.Type; +import de.dhbwstuttgart.typinference.unify.model.TypeParams; public class RuleSet implements IRuleSet{ @@ -95,12 +97,12 @@ public class RuleSet implements IRuleSet{ if(pi.length == 0) return Optional.empty(); - Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); - Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); Set result = new HashSet<>(); - for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++) - result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOTWC)); + for(int rhsIdx = 0; rhsIdx < rhsTypeParams.size(); rhsIdx++) + result.add(new MPair(lhsTypeParams.get(pi[rhsIdx]), rhsTypeParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @@ -128,12 +130,12 @@ public class RuleSet implements IRuleSet{ if(pi.length == 0) return Optional.empty(); - Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); - Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); Set result = new HashSet<>(); - for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++) - result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOTWC)); + for(int rhsIdx = 0; rhsIdx < rhsTypeParams.size(); rhsIdx++) + result.add(new MPair(lhsTypeParams.get(pi[rhsIdx]), rhsTypeParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @@ -160,11 +162,11 @@ public class RuleSet implements IRuleSet{ // TODO Permutation? Set result = new HashSet<>(); - Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); - Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); - for(int i = 0; i < lhsTypeParams.length; i++) - result.add(new MPair(lhsTypeParams[i], rhsTypeParams[i], PairOperator.EQUALSDOT)); + for(int i = 0; i < lhsTypeParams.size(); i++) + result.add(new MPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); return Optional.of(result); } @@ -193,12 +195,12 @@ public class RuleSet implements IRuleSet{ if(pi.length == 0) return Optional.empty(); - Type[] rhsTypeParams = rhsType.getTypeParams().asArray(); - Type[] lhsTypeParams = lhsType.getTypeParams().asArray(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); Set result = new HashSet<>(); - for(int rhsIdx = 0; rhsIdx < rhsTypeParams.length; rhsIdx++) - result.add(new MPair(lhsTypeParams[pi[rhsIdx]], rhsTypeParams[rhsIdx], PairOperator.SMALLERDOTWC)); + for(int rhsIdx = 0; rhsIdx < rhsTypeParams.size(); rhsIdx++) + result.add(new MPair(lhsTypeParams.get(pi[rhsIdx]), rhsTypeParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @@ -244,10 +246,10 @@ public class RuleSet implements IRuleSet{ Set result = new HashSet<>(); - Type[] rhsTypeParams = rhsSType.getTypeParams().asArray(); - Type[] lhsTypeParams = lhsSType.getTypeParams().asArray(); - for(int i = 0; i < rhsTypeParams.length; i++) - result.add(new MPair(lhsTypeParams[i], rhsTypeParams[i], PairOperator.EQUALSDOT)); + TypeParams rhsTypeParams = rhsSType.getTypeParams(); + TypeParams lhsTypeParams = lhsSType.getTypeParams(); + for(int i = 0; i < rhsTypeParams.size(); i++) + result.add(new MPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); return Optional.of(result); } @@ -302,19 +304,53 @@ public class RuleSet implements IRuleSet{ } @Override - public MPair adapt(MPair pair) { + public Optional adapt(MPair pair) { + if(pair.getPairOp() != PairOperator.SMALLERDOT) + return Optional.empty(); + + Type lhsType = pair.getLhsType(); + if(!(lhsType instanceof SimpleType)) + return Optional.empty(); + + Type rhsType = pair.getRhsType(); + if(!(rhsType instanceof SimpleType)) + return Optional.empty(); + + if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0) + return Optional.empty(); + + Type lhsFromFc = finiteClosure.getType(lhsType.getName()); + + if(lhsFromFc == null) + return Optional.empty(); + + Set greater = finiteClosure.greater(lhsFromFc); + + Optional opt = greater.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny(); + + if(!opt.isPresent()) + return Optional.empty(); + + Type newLhs = opt.get(); + + TypeParams lhsTypeParams = lhsType.getTypeParams(); + TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams(); + + Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0)); + for(int i = 0; i < lhsTypeParams.size(); i++) + unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i))); + + return Optional.of(new MPair(newLhs.applyToTypeParams(unif), rhsType, PairOperator.SMALLERDOT)); + } + + @Override + public Optional adaptExt(MPair pair) { // TODO Auto-generated method stub return null; } @Override - public MPair adaptExt(MPair pair) { - // TODO Auto-generated method stub - return null; - } - - @Override - public MPair adaptSup(MPair pair) { + public Optional adaptSup(MPair pair) { // TODO Auto-generated method stub return null; } @@ -362,17 +398,17 @@ public class RuleSet implements IRuleSet{ Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size()); Assert.assertTrue(dFromFc.getTypeParams().size() > 0); - Type[] cArgs = cFromFc.getTypeParams().asArray(); - Type[] dArgs = dFromFc.getTypeParams().asArray(); + TypeParams cArgs = cFromFc.getTypeParams(); + TypeParams dArgs = dFromFc.getTypeParams(); - int[] permutation = new int[dArgs.length]; + int[] permutation = new int[dArgs.size()]; boolean succ = true; - for (int dArgIdx = 0; dArgIdx < dArgs.length && succ; dArgIdx++) { - Type dArg = dArgs[dArgIdx]; + for (int dArgIdx = 0; dArgIdx < dArgs.size() && succ; dArgIdx++) { + Type dArg = dArgs.get(dArgIdx); succ = false; - for (int pi = 0; pi < cArgs.length; pi++) - if (cArgs[pi].getName().equals(dArg.getName())) { + for (int pi = 0; pi < cArgs.size(); pi++) + if (cArgs.get(pi).getName().equals(dArg.getName())) { permutation[dArgIdx] = pi; succ = true; break; @@ -384,7 +420,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional subst(MPair pair) { + public Optional subst(MPair pair) { // TODO Auto-generated method stub return null; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 1313b6fa8..4f88ccd2b 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.typeinference.unifynew; -import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; @@ -13,11 +12,8 @@ import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; -import de.dhbwstuttgart.typeinference.unify.interfaces.IUnifier; import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typinference.unify.model.SimpleType; -import de.dhbwstuttgart.typinference.unify.model.Type; /** * Implementierung des Unifikationsalgorithmus. @@ -145,11 +141,4 @@ public class Unify { collection.add(pair2); } - - private void test() { - Type t = new PlaceholderType("T"); - Type subst = new SimpleType("Subst"); - - IUnifier u = x -> x.substitute(t, subst); - } }