Skip to content

Commit 7c79edb

Browse files
author
Aleksander Boruch-Gruszecki
committed
Add NaiveConstraintChecker
1 parent dc19e6a commit 7c79edb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dotty.tools.dotc.transform.patmat
2+
package `new logic`
3+
4+
import dotty.tools.dotc.core.Contexts.Context
5+
6+
/** Checks satisfiability of constraints in an extremely naive way */
7+
object NaiveConstraintChecker {
8+
def hasUnsatisfiableConstraints(s: ConstrainedSpace)(implicit ctx: Context): Boolean =
9+
!termsPotentiallySatisfiable(s.termConstraints) ||
10+
!typesPotentiallySatisfiable(s.typeConstraints)
11+
12+
def termsPotentiallySatisfiable(constraints: List[TermConstraint]): Boolean =
13+
constraints.forall {
14+
case Dummy | Neg(Dummy) => true
15+
case AlwaysSatisfied => true
16+
case Neg(AlwaysSatisfied) => false
17+
}
18+
19+
@annotation.tailrec
20+
def typesPotentiallySatisfiable(constraints: List[TypeConstraint])(implicit ctx: Context): Boolean = {
21+
def comparePair(tpeq1: TypeEquality, tpeq2: TypeEquality) =
22+
if (tpeq1.tp1 =:= tpeq2.tp1) tpeq1.tp2 =:= tpeq2.tp2 else true
23+
24+
@annotation.tailrec
25+
def compareOneVsList(tpeq: TypeEquality, constraints: List[TypeConstraint]): Boolean =
26+
constraints match {
27+
case Nil => true
28+
case (tpeq2: TypeEquality) :: rest =>
29+
comparePair(tpeq, tpeq2) && compareOneVsList(tpeq, rest)
30+
}
31+
32+
constraints match {
33+
case Nil => true
34+
case (tpeq: TypeEquality) :: tail =>
35+
compareOneVsList(tpeq, tail) && typesPotentiallySatisfiable(tail)
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)