From 93f7edf467a741c9c657d6d581cadd460fb9f93c Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Mon, 11 Apr 2022 17:32:18 +0200 Subject: [PATCH] Rafactoring: Remove unused code. Add duplicated method filtering --- src/main/scala/hb/dhbw/EqSet.scala | 8 ------ src/main/scala/hb/dhbw/FJTypeinference.scala | 29 ++++++++------------ src/main/scala/hb/dhbw/InsertTypes.scala | 16 ----------- src/main/scala/hb/dhbw/Unify.scala | 26 ------------------ src/test/scala/IntegrationTest.scala | 2 +- 5 files changed, 12 insertions(+), 69 deletions(-) delete mode 100644 src/main/scala/hb/dhbw/EqSet.scala diff --git a/src/main/scala/hb/dhbw/EqSet.scala b/src/main/scala/hb/dhbw/EqSet.scala deleted file mode 100644 index 67f28f9..0000000 --- a/src/main/scala/hb/dhbw/EqSet.scala +++ /dev/null @@ -1,8 +0,0 @@ -package hb.dhbw - -class EqSet { - - def addAndConstraint(cons: Set[Constraint]) {} - -} - diff --git a/src/main/scala/hb/dhbw/FJTypeinference.scala b/src/main/scala/hb/dhbw/FJTypeinference.scala index ee6d80f..d8fc68a 100644 --- a/src/main/scala/hb/dhbw/FJTypeinference.scala +++ b/src/main/scala/hb/dhbw/FJTypeinference.scala @@ -41,26 +41,23 @@ object FJTypeinference { ).map(it => (convertRefType(it._1), convertRefType(it._2))).toSet) private def cToUnifyType(c: Class): UnifyRefType = UnifyRefType(c.name, c.genericParams.map(it => convertType(it._1))) + /* + Die generics sind für alle Methoden die gleichen. Falls dies der Fall ist, kann man einfach nach Sub typen in den Parametern und return-Typ filtern + */ private def removeOverloadedSubtypeMethods(in: Class, finiteClosure: FiniteClosure) = { def convertToFJType(in: Type): FJNamedType = in match { case GenericType(name) => FJNamedType(name, List()) case RefType(n, p) => FJNamedType(n,p.map(convertToFJType)) } def methodIsSupertype(m : Method, superMethod: Method) = { - def getBound(t: Type): 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).map(getBound).get - case x => x - } - if(m.params.size != superMethod.params.size){ - false - }else{ - val returnIsSub = finiteClosure.aIsSubtypeOfb(convertToFJType(getBound(m.retType)), convertToFJType(getBound(superMethod.retType))) + if(m.genericParams.equals(superMethod.genericParams)) { + val returnIsSub = finiteClosure.aIsSubtypeOfb(convertToFJType(m.retType), convertToFJType(superMethod.retType)) val paramsAreSup = m.params.zip(superMethod.params).foldLeft(true)((isSub, m2) => { - isSub && finiteClosure.aIsSubtypeOfb(convertToFJType(getBound(m2._2._1)), convertToFJType(getBound(m2._1._1))) + isSub && finiteClosure.aIsSubtypeOfb(convertToFJType(m2._2._1), convertToFJType(m2._1._1)) }) returnIsSub && paramsAreSup + }else{ + false } } @@ -78,23 +75,19 @@ object FJTypeinference { Class(in.name, in.genericParams, in.superType, in.fields, newMethods.toList) } - def sigmaReplace(sigma:Map[String, Type], unifyType: UnifyType): Type = unifyType match { - case UnifyRefType(n, ps) => RefType(n, ps.map(it => sigmaReplace(sigma, it))) - case UnifyTV(a) => sigma(a) - } - def typeinference(str: String): Either[String, List[Class]] = { val ast = Parser.parse(str).map(ASTBuilder.fromParseTree(_)) var typedClasses: List[Class] = List() ast.map(ast => { ast.foldLeft(List[Class]())((cOld, c) => { val newClassList = cOld :+ c - val typeResult = TYPE.generateConstraints(newClassList, generateFC(newClassList)) + val fc = generateFC(newClassList) + val typeResult = TYPE.generateConstraints(newClassList, fc) val unifyResult = Unify.unifyIterative(convertOrConstraints(typeResult._1), typeResult._2) //Insert intersection types //val typeInsertedC = InsertTypes.applyResult(sigma, generics, c)//InsertTypes.insert(unifyResult, c) - val typeInsertedC = InsertTypes.applyUnifyResult(unifyResult, c) + val typeInsertedC = removeOverloadedSubtypeMethods(InsertTypes.applyUnifyResult(unifyResult, c), fc) typedClasses = typedClasses :+ typeInsertedC cOld :+ typeInsertedC }) diff --git a/src/main/scala/hb/dhbw/InsertTypes.scala b/src/main/scala/hb/dhbw/InsertTypes.scala index 451b779..667b58c 100644 --- a/src/main/scala/hb/dhbw/InsertTypes.scala +++ b/src/main/scala/hb/dhbw/InsertTypes.scala @@ -108,22 +108,6 @@ object InsertTypes { val constraints = flatted.map(_.map(refTypeInConsToGenerics(_))) - /* - - def extractTVNames(unifyType: UnifyType): Set[String] = unifyType match { - case UnifyTV(name) => Set(name) - case UnifyRefType(_, params) => params.flatMap(extractTVNames(_)).toSet - } - - val genericNames:Set[String] = into.genericParams.map(_._1).flatMap(_ match { - case GenericType(name) => Some(name) - case _ => None - }).toSet ++ unifyResult.flatMap(_.flatMap(_ match{ - case UnifyLessDot(a,b) => Set(a, b) - case UnifyEqualsDot(a,b) => Set(a,b) - })).flatMap(extractTVNames(_)) - val constraints = normalized.map(_.map(replaceRefTypeWithGeneric(_, genericNames))) - */ val newMethods = into.methods.flatMap(m => constraints.map(cons => insert(cons, m))) Class(into.name, into.genericParams, into.superType, into.fields, newMethods) } diff --git a/src/main/scala/hb/dhbw/Unify.scala b/src/main/scala/hb/dhbw/Unify.scala index 9eea652..ede9ef8 100644 --- a/src/main/scala/hb/dhbw/Unify.scala +++ b/src/main/scala/hb/dhbw/Unify.scala @@ -43,13 +43,6 @@ object Unify { }) ret = ret.filter(it => !alessdotb.contains(it)) alessdotb.foreach(it => ret = subst(it.left.asInstanceOf[UnifyTV], it.right, ret)) - /* - .filter(_ match{ - case UnifyEqualsDot(UnifyTV(a), UnifyTV(b)) => a != b - case UnifyLessDot(UnifyTV(_), UnifyTV(_)) => false - case _ => true - }) - */ ret ++ alessdotb.map(_ match {case UnifyLessDot(a, b) => UnifyEqualsDot(a,b)}) } @@ -80,25 +73,6 @@ object Unify { results } - /* - def unify(orCons: Set[Set[Set[UnifyConstraint]]], fc: FiniteClosure) : Set[Set[UnifyConstraint]] = { - val eqSets = cartesianProduct(orCons) - val step2Results = eqSets.flatMap(eqSet => { - val rulesResult = applyRules(fc)(eqSet.flatten) - step2(rulesResult, fc) - }) - step2Results.flatMap(eqSet => { - val (substResult, unifier) = substStep(eqSet) - if(!unifier.isDefined){ - if(isSolvedForm(substResult)) - Set(substResult) - else Set() - }else{ - unify(Set(Set(substResult)), fc).map(s => s + unifier.get) - } - }) - } -*/ def step2(eq : Set[UnifyConstraint], fc: FiniteClosure) ={ val eq1 = eq.filter(c => c match{ case UnifyLessDot(UnifyTV(_), UnifyTV(_)) => true diff --git a/src/test/scala/IntegrationTest.scala b/src/test/scala/IntegrationTest.scala index aca588b..b844f16 100644 --- a/src/test/scala/IntegrationTest.scala +++ b/src/test/scala/IntegrationTest.scala @@ -22,7 +22,7 @@ class IntegrationTest extends FunSuite { test("ListAddDefinition"){ val result = FJTypeinference.typeinference("class List extends Object{\n add(a){\n return this;\n}\n}") - println(result) + println(result.map(Main.prettyPrintAST(_))) } /* test("PaperExample"){