From 9552831d4e1795f1e23555a24d8e51b7aef2e5f5 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 22 Nov 2015 15:54:18 +0100 Subject: [PATCH] AdaptExt and Sup revisit --- .../typeinference/unifynew/RuleSet.java | 96 ++++++++++--------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 0d36131b..ddf365be 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -354,43 +354,45 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type lhsType = pair.getLhsType(); - if(!(lhsType instanceof SimpleType) && !(lhsType instanceof ExtendsType)) + Type typeD = pair.getLhsType(); + if(!(typeD instanceof SimpleType) && !(typeD instanceof ExtendsType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); - if(!(rhsType instanceof ExtendsType)) + Type typeExtDs = pair.getRhsType(); + if(!(typeExtDs instanceof ExtendsType)) return Optional.empty(); - if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0) + if(typeD.getTypeParams().size() == 0 || typeExtDs.getTypeParams().size() == 0) return Optional.empty(); - Type lhsFromFc; - if(lhsType instanceof SimpleType) - lhsFromFc = finiteClosure.getType(lhsType); - else - lhsFromFc = new ExtendsType(finiteClosure.getType(((ExtendsType) lhsType).getExtendedType())); - - if(lhsFromFc == null) + Type typeDgen; + if(typeD instanceof SimpleType) + typeDgen = finiteClosure.getGenericType(typeD.getName()).orElse(null); + else { + Optional opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName()); + typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null; + } + + if(typeDgen == null) return Optional.empty(); - Set grArg = finiteClosure.grArg(lhsFromFc); + Set grArg = finiteClosure.grArg(typeDgen); - Optional opt = grArg.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny(); + Optional opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny(); if(!opt.isPresent()) return Optional.empty(); Type newLhs = ((ExtendsType) opt.get()).getExtendedType(); - TypeParams lhsTypeParams = lhsType.getTypeParams(); - TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams(); + TypeParams typeDParams = typeD.getTypeParams(); + TypeParams typeDgenParams = typeDgen.getTypeParams(); - Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0)); - for(int i = 1; i < lhsTypeParams.size(); i++) - unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i))); + Unifier unif = new Unifier(typeDgenParams.get(0), typeDParams.get(0)); + for(int i = 1; i < typeDParams.size(); i++) + unif.andThen(new Unifier(typeDgenParams.get(i), typeDParams.get(i))); - return Optional.of(new MPair(newLhs.apply(unif), rhsType, PairOperator.SMALLERDOTWC)); + return Optional.of(new MPair(newLhs.apply(unif), typeExtDs, PairOperator.SMALLERDOTWC)); } @Override @@ -398,51 +400,51 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type lhsType = pair.getLhsType(); - if(!(lhsType instanceof SimpleType) && !(lhsType instanceof SuperType)) + Type typeDs = pair.getLhsType(); + if(!(typeDs instanceof SimpleType) && !(typeDs instanceof SuperType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); - if(!(rhsType instanceof SuperType)) + Type typeSupD = pair.getRhsType(); + if(!(typeSupD instanceof SuperType)) return Optional.empty(); - if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0) + if(typeDs.getTypeParams().size() == 0 || typeSupD.getTypeParams().size() == 0) return Optional.empty(); - Type lhsFromFc; - Type newRhs; - if(lhsType instanceof SimpleType) { - lhsFromFc = finiteClosure.getType(lhsType); - newRhs = new ExtendsType(lhsType); - } - else { - lhsFromFc = new SuperType(finiteClosure.getType(((SuperType) lhsType).getSuperedType())); - newRhs = new ExtendsType(((SuperType) lhsType).getSuperedType()); - } - Type rhsFromFc = finiteClosure.getType(((SuperType) rhsType).getSuperedType()); + Optional opt = finiteClosure.getGenericType(((SuperType) typeDs).getSuperedType().getName()); - if(lhsFromFc == null || rhsFromFc == null) + if(!opt.isPresent()) return Optional.empty(); - Set smArg = finiteClosure.smArg(new SuperType(rhsFromFc)); - - Optional opt = smArg.stream().filter(x -> x.getName().equals(lhsType.getName())).findAny(); + Type typeDsgen = opt.get(); + Type typeSupDsgen = new SuperType(typeDsgen); + + // Use of smArg instead of grArg because + // a in grArg(b) => b in smArg(a) + Set smArg = finiteClosure.smArg(typeSupDsgen); + opt = smArg.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny(); if(!opt.isPresent()) return Optional.empty(); + // New RHS + Type newRhs = null; + if(typeDs instanceof SimpleType) + newRhs = new ExtendsType(typeDs); + else + newRhs = new ExtendsType(((SuperType) typeDs).getSuperedType()); + + // New LHS Type newLhs = opt.get(); + TypeParams typeDParams = typeSupD.getTypeParams(); + TypeParams typeSupDsgenParams = typeSupDsgen.getTypeParams(); - TypeParams lhsTypeParams = lhsType.getTypeParams(); - TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams(); - - Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0)); - for(int i = 1; i < lhsTypeParams.size(); i++) - unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i))); + Unifier unif = new Unifier(typeSupDsgenParams.get(0), typeDParams.get(0)); + for(int i = 1; i < typeDParams.size(); i++) + unif.andThen(new Unifier(typeSupDsgenParams.get(i), typeDParams.get(i))); return Optional.of(new MPair(newLhs.apply(unif), newRhs, PairOperator.SMALLERDOTWC)); - } /**