8000 SI-6582 Use Tseitin's algorithm for CNF conversion in pattern matching analysis. by adriaanm · Pull Request #2796 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

SI-6582 Use Tseitin's algorithm for CNF conversion in pattern matching analysis. #2796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bincompat-forward.whitelist.conf
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ filter {
{
matchName="scala.reflect.internal.annotations.uncheckedBounds"
problemName=MissingClassProblem
},
{
matchName="scala.reflect.internal.util.package$Sequenceable"
problemName=MissingClassProblem
}
]
}
96 changes: 96 additions & 0 deletions src/compiler/scala/tools/nsc/transform/patmat/CNFBuilder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* NSC -- new Scala compiler
*
* @author Gerard Basler
*/

package scala.tools.nsc.transform.patmat

/** A literal.
*/
class Lit(val v: Int) extends AnyVal {
def unary_- : Lit = Lit(-v)
def variable: Int = Math.abs(v)
def positive = v >= 0
}

object Lit {
def apply(v: Int): Lit = new Lit(v)
}

/** Conjunctive normal form (of a Boolean formula).
* A formula in this form is amenable to a SAT solver
* (i.e., solver that decides satisfiability of a formula).
*/
class CNFBuilder {

import scala.collection.mutable.ArrayBuffer

// a clause is a disjunction of distinct literals
type Clause = Set[Lit]
type ClauseBuilder = ArrayBuffer[Clause]
private[this] val buff = ArrayBuffer[Clause]()
def clauses: Array[Clause] = buff.toArray

private[this] var myLiteralCount = 0
def allLiterals: Set[Lit] = (1 to myLiteralCount).map(Lit(_)).toSet
def newLiteral(): Lit = {
myLiteralCount += 1
Lit(myLiteralCount)
}

lazy val constTrue: Lit = {
val constTrue = newLiteral()
addClauseProcessed(constTrue)
constTrue
}

lazy val constFalse: Lit = -constTrue

def isConst(l: Lit): Boolean = l == constTrue || l == constFalse

def addClauseRaw(clause: Clause): Unit = buff += clause

/** Add literals vector, ignores clauses that are trivially satisfied
*
* @param bv
*/
def addClauseProcessed(bv: Lit*) {
val clause = processClause(bv: _*)
if (clause.nonEmpty)
addClauseRaw(clause)
}

/** @return empty clause, if clause trivially satisfied
*/
private def processClause(bv: Lit*): Clause = {
val clause = bv.distinct

val isTautology = clause.combinations(2).exists {
case Seq(a, b) => a == -b
}

if (isTautology)
Set()
else
clause.toSet
}

override def toString: String = {
for {
clause <- buff
} yield {
clause.mkString("(", " ", ")")
}
}.mkString("\n")

def dimacs: String = {
val header = s"p cnf ${myLiteralCount} ${buff.length}\n"
header + {
for {
clause <- buff
} yield {
clause.toSeq.sortBy(_.variable) mkString ("", " ", " 0")
}
}.mkString("\n")
}
}
Loading
0