Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   fun eval(interpretation : Map<String, Boolean>) : Boolean {
  2.         return when ( this ) {
  3.             is Variable -> interpretation.get(variable)!!
  4.             is Conjunction -> left.eval(interpretation) && right.eval(interpretation)
  5.             is Disjunction -> left.eval(interpretation) || right.eval(interpretation)
  6.             is Implication -> left.eval(interpretation).not() || right.eval(interpretation)
  7.             is Equivalence -> left.eval(interpretation) == right.eval(interpretation)
  8.             is Negation -> formula.eval(interpretation).not()
  9.  
  10.             else -> throw Exception("something bad happened in Eval")
  11.         }
  12.     }
  13.  
  14.     fun negate() : Formula {
  15.         return when ( this ) {
  16.             is Variable -> Negation(Variable(variable))
  17.             is Conjunction -> Disjunction(left.negate(), right.negate())
  18.             is Disjunction -> Conjunction(left.negate(), right.negate())
  19.             is Equivalence -> Disjunction(Conjunction(left.negate(), right), Conjunction(left, right.negate()))
  20.             is Implication -> Conjunction(left, right.negate())
  21.             is Negation ->  formula
  22.  
  23.             else -> throw Exception("something bad happened in Negate")
  24.         }
  25.     }
  26.  
  27.     fun simplify() : Formula {
  28.         when {
  29.                 ( this is Conjunction ) -> {
  30.                     return when {
  31.                         this.right is True -> this.left.simplify()
  32.                         this.left is False || this.right is False -> False()
  33.                         this.left is True -> this.right.simplify()
  34.                         this.left == this.right -> this.left.simplify()
  35.                         this.left == this.right.negate() -> False()
  36.                         else -> this
  37.                     }
  38.                 }
  39.                 ( this is Disjunction ) -> {
  40.                     return when {
  41.                         this.left is True || this.right is True -> True()
  42.                         this.left is False -> this.right.simplify()
  43.                         this.right is False -> this.left.simplify()
  44.                         this.left == this.right.negate() -> False()
  45.                         this.left == this.right -> this.left.simplify()
  46.                         else -> this
  47.                     }
  48.                 }
  49.                 ( this is Negation ) -> {
  50.                     return when {
  51.                         this.formula is Negation -> this.formula.formula.simplify()
  52.                         else -> return this
  53.                     }
  54.                 }
  55.                 else -> return this
  56.             }
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement