This commit is contained in:
Andreas Stadelmeier 2024-02-13 22:25:43 +01:00
commit 1e9db6fdfd

View File

@ -4,58 +4,78 @@ import unify.model.{ExtendsRelations, LessDot}
import unify.model.*
object Rules:
def applyRules(cs: ConstraintSet, tvFactory: TypeVariableFactory, fc: ExtendsRelations): Unit = {
class Rules(cs: ConstraintSet, wildcardEnvironment: WildcardEnvironment, fc: ExtendsRelations):
def applyRules(tvFactory: TypeVariableFactory, fc: ExtendsRelations): Unit = {
cs.getEqualsDotCons.foreach(cons => {
if (cons.left.equals(cons.right)) { //erase rule
cs.remove(cons)
return
}
/*
else if (cons.left.isInstanceOf[Wildcard] && cons.right.isInstanceOf[Wildcard]) {
normalizeRule(cons, cs)
return
}
else if (cons.left.isInstanceOf[Wildcard] && !cons.right.isInstanceOf[WTypeVariable]) {
//Tame rule:
tameRule(cons, cs)
return
//Tame rule:
tameRule(cons, cs)
return
}else if(cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[RefType]){
val left = cons.left.asInstanceOf[RefType]
val right = cons.right.asInstanceOf[RefType]
if(left.params.size == right.params.size && left.name == right.name){
cs.remove(cons)
left.params.zip(right.params).map(pp => EqualsDot(pp._1, pp._2))foreach {
cs.add(_)
}
}else{
//TODO: insolvable constraint set!
}
}
else if (cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[Wildcard]) {
//Swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if (!cons.left.isInstanceOf[TypeVariable] && cons.right.isInstanceOf[TypeVariable]) {
//swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if ((cons.left.isInstanceOf[RefType] || cons.left.isInstanceOf[Wildcard]) && cons.right.isInstanceOf[WTypeVariable]) {
//swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if (cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[RefType] &&
cs.fv(cons.left).isEmpty && cs.fv(cons.right).isEmpty) { // reduceEq rule
cs.remove(cons)
cs.add(LessDot(cons.left, cons.right))
cs.add(LessDot(cons.right, cons.left))
return
}
else if (cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[RefType]
&& cons.left.asInstanceOf[RefType].name.equals(cons.right.asInstanceOf[RefType].name)
&& cons.left.asInstanceOf[RefType].wildcards.equals(cons.right.asInstanceOf[RefType].wildcards)) {
reduceEqRule(cons, cs, tvFactory)
return
}
*/
/*
else if (cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[Wildcard]) {
//Swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if (!cons.left.isInstanceOf[TypeVariable] && cons.right.isInstanceOf[TypeVariable]) {
//swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if ((cons.left.isInstanceOf[RefType] || cons.left.isInstanceOf[Wildcard]) && cons.right.isInstanceOf[WTypeVariable]) {
//swap rule:
cs.remove(cons)
cs.add(EqualsDot(cons.right, cons.left))
return
}
else if (cons.left.isInstanceOf[RefType] && cons.right.isInstanceOf[RefType]
&& cons.left.asInstanceOf[RefType].name.equals(cons.right.asInstanceOf[RefType].name)
&& cons.left.asInstanceOf[RefType].wildcards.equals(cons.right.asInstanceOf[RefType].wildcards)) {
reduceEqRule(cons, cs, tvFactory)
return
}
*/
})
}
private def normalizeRule(cons: EqualsDot, cs: ConstraintSet): Unit = {
cs.remove(cons)
val left = cons.left.asInstanceOf[Wildcard]
val right = cons.right.asInstanceOf[Wildcard]
cs.add(EqualsDot(wildcardEnvironment.getLowerBound(left), wildcardEnvironment.getUpperBound(left)))
cs.add(EqualsDot(wildcardEnvironment.getLowerBound(right), wildcardEnvironment.getUpperBound(right)))
cs.add(EqualsDot(wildcardEnvironment.getLowerBound(left), wildcardEnvironment.getUpperBound(right)))
}
def tameRule(cons: EqualsDot, cs: ConstraintSet) = {
cs.remove(cons)
val left = cons.left.asInstanceOf[Wildcard]
val right = cons.right
cs.add(EqualsDot(wildcardEnvironment.getUpperBound(left), right))
cs.add(EqualsDot(wildcardEnvironment.getLowerBound(left), right))
}
def adaptRule(cons: LessDot, cs: ConstraintSet, fc: ExtendsRelations) = {
}