package rename + Mapping Klasse

This commit is contained in:
Florian Steurer 2015-12-27 00:29:23 +01:00
parent 039dd3b3f4
commit 98cbe9b389
24 changed files with 136 additions and 121 deletions

View File

@ -3,11 +3,11 @@ package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
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.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.Type;
public interface IFiniteClosure {

View File

@ -3,8 +3,8 @@ package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
import de.dhbwstuttgart.typinference.unify.model.MPair;
public interface IRuleSet {

View File

@ -1,7 +0,0 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
import de.dhbwstuttgart.typeinference.Menge;
public interface ITypeMapper<T> {
}

View File

@ -3,8 +3,8 @@ package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
import de.dhbwstuttgart.typinference.unify.model.MPair;
/**
* Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari, Ruzicka-Privara or Suciu)

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.HashMap;
import java.util.HashSet;
@ -6,7 +6,7 @@ import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
public class FiniteClosure implements IFiniteClosure {

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
public class MPair {

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.HashSet;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.typinference.unify.model;
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Arrays;
import java.util.Iterator;

View File

@ -2,68 +2,90 @@ package de.dhbwstuttgart.typeinference.unifynew;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.FunN;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
/**
* First three bits indicate the meta type:
* 000b = 0 = Simpletype
* 001b = 1 = Extends
* 010b = 2 = Super
* 011b = 3 = Type Placeholder
* 100b = 4 = Function
*
* @author DH10STF
*
*/
public class Mapping {
private static final int ST_MASK = 0;
private static final int EXTENDS_MASK = 536870912;
private static final int SUPER_MASK = 1073741824;
private static final int TPH_MASK = 1610612736;
private static final int FUN_MASK = -2147483648;
private HashMap<de.dhbwstuttgart.typeinference.unify.model.Type, de.dhbwstuttgart.syntaxtree.type.Type> backwardMap = new HashMap<>();
private HashMap<de.dhbwstuttgart.syntaxtree.type.Type, de.dhbwstuttgart.typeinference.unify.model.Type> forwardMap = new HashMap<>();
private Set<de.dhbwstuttgart.typeinference.unify.model.Type> irreversible = new HashSet<>();
private static HashMap<Type, Type> mapping;
public Set<Type> createMapping(Iterable<Type> types) {
mapping = new HashMap<>();
Iterator<Type> iterator = types.iterator();
while(iterator.hasNext() && mapping.size() <= 536870911)
createMapping(iterator.next());
return mapping.keySet();
public Mapping(Set<de.dhbwstuttgart.syntaxtree.type.Type> types) {
for(de.dhbwstuttgart.syntaxtree.type.Type t : types) {
// TODO
}
}
private void createMapping(Type type) {
/*if(type instanceof RefType) {
Set<Type> params = ((RefType) type).get_ParaList();
params.stream().forEach(x -> createMapping(x));
public de.dhbwstuttgart.typeinference.unify.model.Type map(de.dhbwstuttgart.syntaxtree.type.Type type) {
return forwardMap.get(type);
}
public de.dhbwstuttgart.typeinference.unify.model.MPair map(de.dhbwstuttgart.typeinference.Pair pair) {
return new de.dhbwstuttgart.typeinference.unify.model.MPair(forwardMap.get(pair.TA1), forwardMap.get(pair.TA2), mapOp(pair.GetOperator()));
}
public Set<de.dhbwstuttgart.typeinference.unify.model.Type> mapTypeSet(Set<de.dhbwstuttgart.syntaxtree.type.Type> types) {
return types.stream().map(this::map).collect(Collectors.toCollection(HashSet::new));
}
public Set<de.dhbwstuttgart.typeinference.unify.model.MPair> mapPairSet(Set<de.dhbwstuttgart.typeinference.Pair> pairs) {
return pairs.stream().map(this::map).collect(Collectors.toCollection(HashSet::new));
}
public Optional<de.dhbwstuttgart.syntaxtree.type.Type> unmap(de.dhbwstuttgart.typeinference.unify.model.Type type) {
return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty();
}
public Optional<de.dhbwstuttgart.typeinference.Pair> unmap(de.dhbwstuttgart.typeinference.unify.model.MPair mpair) {
de.dhbwstuttgart.typeinference.unify.model.Type lhs = mpair.getLhsType();
de.dhbwstuttgart.typeinference.unify.model.Type rhs = mpair.getRhsType();
if(irreversible.contains(lhs) || irreversible.contains(rhs))
return Optional.empty();
return Optional.of(new de.dhbwstuttgart.typeinference.Pair(backwardMap.get(lhs), backwardMap.get(rhs), unmapOp(mpair.getPairOp())));
}
public Optional<Set<de.dhbwstuttgart.syntaxtree.type.Type>> unmapTypeSet(Set<de.dhbwstuttgart.typeinference.unify.model.Type> types) {
Set<de.dhbwstuttgart.syntaxtree.type.Type> result = types.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new));
return result.size() == types.size() ? Optional.of(result) : Optional.empty();
}
public Optional<Set<de.dhbwstuttgart.typeinference.Pair>> unmapPairSet(Set<de.dhbwstuttgart.typeinference.unify.model.MPair> pairs) {
Set<de.dhbwstuttgart.typeinference.Pair> result = pairs.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new));
return result.size() == pairs.size() ? Optional.of(result) : Optional.empty();
}
private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) {
/*
* TODO
* Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt?
*/
switch(op) {
case Equal:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
case Smaller:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER;
case SmallerExtends:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT;
default:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
}
}
private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) {
switch(op) {
case EQUALS:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
case SMALLER:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
case SMALLERDOT:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends;
default:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
}
*/
int typeId = mapping.size();
if(type instanceof RefType)
typeId |= ST_MASK;
else if(type instanceof SuperWildcardType)
typeId |= SUPER_MASK;
else if(type instanceof ExtendsWildcardType)
typeId |= EXTENDS_MASK;
else if(type instanceof TypePlaceholder)
typeId |= TPH_MASK;
else if(type instanceof FunN)
typeId |= FUN_MASK;
//mapping.put(new MType())
}
}

View File

@ -9,11 +9,11 @@ import java.util.Stack;
import java.util.stream.Collectors;
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
/**
* Implementation of the Martelli-Montanari unification algorithm.

View File

@ -13,14 +13,14 @@ import java.util.stream.Collectors;
import junit.framework.Assert;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
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;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
public class RuleSet implements IRuleSet{

View File

@ -4,9 +4,9 @@ import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
public class Unifier implements Function<Type, Type> {
private Type source;

View File

@ -16,12 +16,12 @@ 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.ISetOperations;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typinference.unify.model.SuperType;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
/**

View File

@ -4,10 +4,10 @@ import java.util.HashSet;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
public class FiniteClosureBuilder {

View File

@ -6,8 +6,8 @@ import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.Type;
public class FiniteClosureTest {

View File

@ -9,12 +9,12 @@ import junit.framework.Assert;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unifynew.RuleSet;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
import de.dhbwstuttgart.typinference.unify.model.SuperType;
public class RuleSetTest {

View File

@ -8,10 +8,10 @@ import junit.framework.Assert;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.Type;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unifynew.MartelliMontanariUnify;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
public class StandardUnifyTest {

View File

@ -3,11 +3,11 @@ package unify;
import java.util.Arrays;
import java.util.stream.Collectors;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
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.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.SimpleType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.Type;
public class TypeFactory {

View File

@ -6,9 +6,9 @@ import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unifynew.Unify;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
public class UnifyTest extends Unify {