object Prop {
def simplify(prop : Prop) : Prop = {
prop match {
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 _ => {
if (simplify(prop) == prop) prop
else prop
}
}
}
}
I’m pretty sure I’ve an infinite loop caused by my ‘default’ case. I’m using recursion in all cases. Which is meant to be, but, only if the Prop can be simplified. As soon as the Prop can’t be simplified, it should return the whole thing.
I don’t see how I can test for any further simplification. (I’m not allowed to use other libraries, as suggested in freenodes #scala channel).
Can someone explain whether it IS the ‘case _’ causing the loop, and how to solve it? How can I test for possible simplification without making a loop?
Thanks in advance!
The problem is that you’re trying to do two things in one step that need to happen in sequence—applying De Morgan’s law (and removing double negation) and recursively simplifying any children. This is why just dropping a
case And(a, b) => And(simplify(a), simplify(b))into yourmatchwon’t work.Try the following: