forked from JavaTX/JavaCompilerCore
adapt Rule
This commit is contained in:
parent
8a40acb73e
commit
82c0736fee
@ -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)
|
||||
@ -43,3 +47,4 @@ EED UP
|
||||
- Anwendungsreihenfolge der Regeln (wahrscheinlichste zuerst, evtl ist nach regel 1 regel 2 nie möglich etc...)
|
||||
- Erase vor Reduce
|
||||
- Rechenarm vor rechenintensiv
|
||||
|
||||
|
@ -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<MPair> swap(MPair pair);
|
||||
|
||||
public MPair adapt(MPair pair);
|
||||
public MPair adaptExt(MPair pair);
|
||||
public MPair adaptSup(MPair pair);
|
||||
public Optional<MPair> adapt(MPair pair);
|
||||
public Optional<MPair> adaptExt(MPair pair);
|
||||
public Optional<MPair> adaptSup(MPair pair);
|
||||
|
||||
public Optional<IUnifier> subst(MPair pair);
|
||||
public Optional<Unifier> subst(MPair pair);
|
||||
}
|
||||
|
@ -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<MPair, MPair>{
|
||||
|
||||
}
|
@ -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<MPair> 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<MPair> 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<MPair> 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<MPair> 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<MPair> 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<MPair> 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<Type> greater = finiteClosure.greater(lhsFromFc);
|
||||
|
||||
Optional<Type> 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<MPair> 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<MPair> 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<IUnifier> subst(MPair pair) {
|
||||
public Optional<Unifier> subst(MPair pair) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user