Merge branch 'master' of ssh://gohorb.ba-horb.de/bahome/staff/stan/git/FJTypeinference

This commit is contained in:
JanUlrich 2021-12-02 22:49:40 +01:00
commit 0467bec4b3
3 changed files with 40582 additions and 0 deletions

50
README.md Normal file
View File

@ -0,0 +1,50 @@
## Typeinference for Featherweight Java
[Try it here](https://janulrich.github.io/FeatherweightTypeInference/)
### Input Examples
```
class Identity extends Object{
id(a){
return a;
}
}
```
```
class Overloading extends Object{
m(a, b){return a;}
m(a,b){return b;}
}
class TestOverloading extends Object{
test(a){
return new Test().m(this,a);
}
}
```
```
class List<A extends Object> extends Object{
A head;
List<A> tail;
add( a){
return new List(a, this);
}
get(){
return this.head;
}
}
class PrincipleType extends Object {
function(a){
return a.add(this).get();
}
}
```

View File

@ -34,6 +34,32 @@ object FJTypeinference {
).toSet)
private def cToUnifyType(c: Class): UnifyRefType = UnifyRefType(c.name, c.genericParams.map(it => convertType(it._1)))
private def removeOverloadedSubtypeMethods(in: Class, finiteClosure: FiniteClosure) = {
def methodIsSupertype(m : Method, superMethod: Method) = {
def getBound(t: Type) = t match {
case GenericType(x) =>
(in.genericParams ++ m.genericParams.map(c => (c.asInstanceOf[LessDot].l, c.asInstanceOf[LessDot].r)))
.find(p => p._1.equals(GenericType(x))).map(_._2).get
case x => x
}
//finiteClosure.superTypes(getBound(m.retType))
false
}
val methodNames = in.methods.map(_.name)
val newMethods = methodNames.flatMap(mName => {
val overloadedMethods = in.methods.filter(_.name.equals(mName))
overloadedMethods.foldLeft(Set[Method]())((ms, m)=>{
if(ms.find(methodIsSupertype(_, m)).isDefined) { //If there is a method which is more general
ms //do not add this method
}else { //otherwise check if this method shadows another method
ms.filter(methodIsSupertype(m, _))
}
})
})
Class(in.name, in.genericParams, in.superType, in.fields, newMethods)
}
def typeinference(str: String): Either[String, (Set[Set[UnifyConstraint]], List[Class])] = {
val ast = Parser.parse(str).map(ASTBuilder.fromParseTree(_))
var typedClasses: List[Class] = List()
@ -51,6 +77,11 @@ object FJTypeinference {
})
unifyResults
})
val fc = generateFC(typedClasses)
//typedClasses =
typedClasses.map(cl => {
removeOverloadedSubtypeMethods(cl, fc)
})
typeResult.map(it => (it.flatten, typedClasses))
}
}

File diff suppressed because it is too large Load Diff