From 6b1896f58cb66fc54315eb67a72cc545bd2f0937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 18 May 2018 09:55:45 +0200 Subject: [PATCH] modified: src/de/dhbwstuttgart/typeinference/unify/RuleSet.java modified: src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java greaterFUNN und smallFUNN fuer generierte TPHs Variance gesetzt modified: test/javFiles/Lambda.jav vom bytecode ruebergeholt --- .../typeinference/unify/RuleSet.java | 35 +++++++++++++++---- .../unify/distributeVariance.java | 23 +++++++----- test/javFiles/Lambda.jav | 22 +++++------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index a576edc3c..5631724b5 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -24,6 +24,7 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.WildcardType; +import de.dhbwstuttgart.typeinference.unify.distributeVariance; import java.io.FileWriter; import java.io.IOException; @@ -778,7 +779,8 @@ public class RuleSet implements IRuleSet{ } ); return Optional.of(result); } - + + @Override public Optional> greaterFunN(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) @@ -794,13 +796,22 @@ public class RuleSet implements IRuleSet{ Set result = new HashSet(); - UnifyType[] freshPlaceholders = new UnifyType[funNLhsType.getTypeParams().size()]; - for(int i = 0; i < freshPlaceholders.length; i++) - freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); + Integer variance = ((PlaceholderType)rhsType).getVariance(); + Integer inversVariance = distributeVariance.inverseVariance(variance); + UnifyType[] freshPlaceholders = new UnifyType[funNLhsType.getTypeParams().size()]; + for(int i = 0; i < freshPlaceholders.length-1; i++) { + freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); + ((PlaceholderType)freshPlaceholders[i]).setVariance(inversVariance); + } + freshPlaceholders[freshPlaceholders.length-1] = PlaceholderType.freshPlaceholder(); + ((PlaceholderType)freshPlaceholders[freshPlaceholders.length-1]).setVariance(variance); result.add(new UnifyPair(funNLhsType.getTypeParams().get(funNLhsType.getTypeParams().size()-1), freshPlaceholders[funNLhsType.getTypeParams().size()-1], PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); - for(int i = 0; i < funNLhsType.getTypeParams().size()-1; i++) + + for(int i = 0; i < funNLhsType.getTypeParams().size()-1; i++) { result.add(new UnifyPair(freshPlaceholders[i], funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); + } + result.add(new UnifyPair(rhsType, funNLhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT, pair.getSubstitution(), pair.getBasePair())); result.stream().forEach(x -> { UnifyType l = x.getLhsType(); @@ -826,13 +837,23 @@ public class RuleSet implements IRuleSet{ Set result = new HashSet(); + Integer variance = ((PlaceholderType)lhsType).getVariance(); + Integer inversVariance = distributeVariance.inverseVariance(variance); + UnifyType[] freshPlaceholders = new UnifyType[funNRhsType.getTypeParams().size()]; - for(int i = 0; i < freshPlaceholders.length; i++) + for(int i = 0; i < freshPlaceholders.length-1; i++) { freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); + ((PlaceholderType)freshPlaceholders[i]).setVariance(inversVariance); + } + freshPlaceholders[freshPlaceholders.length-1] = PlaceholderType.freshPlaceholder(); + ((PlaceholderType)freshPlaceholders[freshPlaceholders.length-1]).setVariance(variance); result.add(new UnifyPair(freshPlaceholders[funNRhsType.getTypeParams().size()-1], funNRhsType.getTypeParams().get(funNRhsType.getTypeParams().size()-1), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); - for(int i = 0; i < funNRhsType.getTypeParams().size()-1; i++) + + for(int i = 0; i < funNRhsType.getTypeParams().size()-1; i++) { result.add(new UnifyPair(funNRhsType.getTypeParams().get(i), freshPlaceholders[i], PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); + } + result.add(new UnifyPair(lhsType, funNRhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT, pair.getSubstitution(), pair.getBasePair())); result.stream().forEach(x -> { UnifyType l = x.getLhsType(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java b/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java index fe2e02d4a..1779c9410 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java +++ b/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java @@ -11,6 +11,18 @@ import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class distributeVariance extends visitUnifyTypeVisitor { + + public static int inverseVariance(int variance) { + Integer ret = 0; + if (variance == 1) { + ret = -1; + } + if (variance == -1) { + ret = 1; + } + return ret; + } + @Override public PlaceholderType visit(PlaceholderType phty, Integer ht) { @@ -30,16 +42,9 @@ public class distributeVariance extends visitUnifyTypeVisitor { List param = new ArrayList<>(funnty.getTypeParams().get().length); param.addAll(Arrays.asList(funnty.getTypeParams().get())); UnifyType resultType = param.remove(param.size()-1); - Integer htInverse = 0; - if (ht == 1) { - htInverse = -1; - } - if (ht == -1) { - htInverse = 1; - } - Integer htInverse1 = htInverse; + Integer htInverse = inverseVariance(ht); param = param.stream() - .map(x -> x.accept(this, htInverse1)) + .map(x -> x.accept(this, htInverse)) .collect(Collectors.toCollection(ArrayList::new)); param.add(resultType.accept(this, ht)); return FunNType.getFunNType(new TypeParams(param)); diff --git a/test/javFiles/Lambda.jav b/test/javFiles/Lambda.jav index 2a2d57150..3ff77fd18 100644 --- a/test/javFiles/Lambda.jav +++ b/test/javFiles/Lambda.jav @@ -1,18 +1,12 @@ -class Lambda { +import java.lang.Integer; - method(){ - return ((f) -> f); +public class Lambda { + + m () { + var lam1 = (Integer x) -> { + return x; + }; + return lam1; } } -/* -interface Fun0{ - A apply(); -} -interface Fun1{ - A apply(B b); -} -interface Fun2{ - A apply(B b, C c); -} -*/