Skip to content
Snippets Groups Projects
Commit e0b90e8e authored by Viktor Kuncak's avatar Viktor Kuncak
Browse files

added test to nnf, re-arranged some functions

parent c4526f22
Branches
Tags
No related merge requests found
...@@ -9,7 +9,15 @@ object NNF { ...@@ -9,7 +9,15 @@ object NNF {
case class Not(f: Formula) extends Formula case class Not(f: Formula) extends Formula
case class Literal(id: Int) extends Formula case class Literal(id: Int) extends Formula
def nnf(formula: Formula): Formula = formula match { def isNNF(f: Formula): Boolean = f match {
case And(lhs, rhs) => isNNF(lhs) && isNNF(rhs)
case Or(lhs, rhs) => isNNF(lhs) && isNNF(rhs)
case Not(Literal(_)) => true
case Not(_) => false
case Literal(_) => true
}
def nnf(formula: Formula): Formula = (formula match {
case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) case And(lhs, rhs) => And(nnf(lhs), nnf(rhs))
case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs))
case Not(And(lhs, rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) case Not(And(lhs, rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs)))
...@@ -17,10 +25,10 @@ object NNF { ...@@ -17,10 +25,10 @@ object NNF {
case Not(Not(f)) => nnf(f) case Not(Not(f)) => nnf(f)
case Not(Literal(_)) => formula case Not(Literal(_)) => formula
case Literal(_) => formula case Literal(_) => formula
} }) ensuring(f => isNNF(f))
def freeVars(f: Formula): Set[Int] = { def freeVars(f: Formula): Set[Int] = {
require(isNNF(f)) require(isNNF(f))
f match { f match {
case And(lhs, rhs) => freeVars(lhs) ++ freeVars(rhs) case And(lhs, rhs) => freeVars(lhs) ++ freeVars(rhs)
case Or(lhs, rhs) => freeVars(lhs) ++ freeVars(rhs) case Or(lhs, rhs) => freeVars(lhs) ++ freeVars(rhs)
...@@ -28,12 +36,5 @@ object NNF { ...@@ -28,12 +36,5 @@ object NNF {
case Literal(i) => Set[Int](i) case Literal(i) => Set[Int](i)
} }
} }
def fv(f : Formula) = { freeVars(nnf(f)) }
def isNNF(f: Formula): Boolean = f match {
case And(lhs, rhs) => isNNF(lhs) && isNNF(rhs)
case Or(lhs, rhs) => isNNF(lhs) && isNNF(rhs)
case Not(Literal(_)) => true
case Not(_) => false
case Literal(_) => true
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment