Want more features on Pastebin? Sign Up, it's FREE!

Scala problem

By: ultiprosan on Feb 25th, 2012  |  syntax: Scala  |  size: 1.71 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* Test score: 5/6
  2.  *
  3.  * Test testSimplify1 failed:
  4.  * 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)))))
  5.  * 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)))))
  6.  */
  7. //--------------------------------------------
  8. abstract case class Prop
  9.  
  10. case class True() extends Prop
  11. case class False() extends Prop
  12. case class Atom(s : String) extends Prop
  13. case class Not(Arg1 : Prop) extends Prop
  14. case class And(Arg1 : Prop, Arg2 : Prop) extends Prop
  15. case class Or(Arg1 : Prop, Arg2 : Prop) extends Prop
  16. case class Impl(Arg1 : Prop, Arg2 : Prop) extends Prop
  17. case class Equiv(Arg1 : Prop, Arg2 : Prop) extends Prop
  18.  
  19. object Prop {
  20.   /*def simplify(prop : Prop) : Prop = {
  21.     prop match {
  22.       case Not(And(Not(a), Not(b))) => Or(simplify(a), simplify(b))
  23.       case Not(Or(Not(a), Not(b))) => And(simplify(a), simplify(b))
  24.       case Not(Or(a,b)) => simplify(And(Not(a),Not(b)))
  25.       case Not(And(a,b)) => simplify(Or(Not(a),Not(b)))
  26.       case Not(Not(a)) => simplify(a)
  27.       case _ => prop
  28.     }
  29.   }*/
  30.   val deMorganAndDoubleNegation: Prop => Prop = {
  31.     case Not(Or(a, b)) => And(Not(a), Not(b))
  32.     case Not(And(a, b)) => Or(Not(a), Not(b))
  33.     case Not(Not(a)) => a
  34.     case a => a
  35.   }
  36.  
  37.   val simplify: Prop => Prop = deMorganAndDoubleNegation andThen {
  38.     case And(a, b) => And(simplify(a), simplify(b))
  39.     case Or(a, b) => Or(simplify(a), simplify(b))
  40.     case Not(a) => Not(simplify(a))
  41.     case a => a
  42.   }
  43. }
clone this paste RAW Paste Data