
Scala problem
By:
ultiprosan on
Feb 25th, 2012 | syntax:
Scala | size: 1.71 KB | hits: 12 | expires: Never
/* Test score: 5/6
*
* Test testSimplify1 failed:
* Expected Or(Or(Or(And(Atom(x1),And(Not(Atom(x2)),Not(Atom(x3)))),Impl(And(Atom(x3),Atom(x4)),Atom(x1))),Equiv(Atom(x2),Or(Atom(x1),Atom(x2)))),Or(Not(Atom(x4)),Or(Not(Atom(x3)),Not(Atom(x4)))))
* but got Or(Or(Or(And(Atom(x1),And(Not(Atom(x2)),Not(Atom(x3)))),Impl(Not(Or(Not(Atom(x3)),Not(Atom(x4)))),Atom(x1))),Equiv(Atom(x2),Or(Atom(x1),Atom(x2)))),Or(Not(Atom(x4)),Not(And(Atom(x3),Atom(x4)))))
*/
//--------------------------------------------
abstract case class Prop
case class True() extends Prop
case class False() extends Prop
case class Atom(s : String) extends Prop
case class Not(Arg1 : Prop) extends Prop
case class And(Arg1 : Prop, Arg2 : Prop) extends Prop
case class Or(Arg1 : Prop, Arg2 : Prop) extends Prop
case class Impl(Arg1 : Prop, Arg2 : Prop) extends Prop
case class Equiv(Arg1 : Prop, Arg2 : Prop) extends Prop
object Prop {
/*def simplify(prop : Prop) : Prop = {
prop match {
case Not(And(Not(a), Not(b))) => Or(simplify(a), simplify(b))
case Not(Or(Not(a), Not(b))) => And(simplify(a), simplify(b))
case Not(Or(a,b)) => simplify(And(Not(a),Not(b)))
case Not(And(a,b)) => simplify(Or(Not(a),Not(b)))
case Not(Not(a)) => simplify(a)
case _ => prop
}
}*/
val deMorganAndDoubleNegation: Prop => Prop = {
case Not(Or(a, b)) => And(Not(a), Not(b))
case Not(And(a, b)) => Or(Not(a), Not(b))
case Not(Not(a)) => a
case a => a
}
val simplify: Prop => Prop = deMorganAndDoubleNegation andThen {
case And(a, b) => And(simplify(a), simplify(b))
case Or(a, b) => Or(simplify(a), simplify(b))
case Not(a) => Not(simplify(a))
case a => a
}
}