# Function that outputs the dual of the formula

The exercise is simple: define a function that gives the dual of the formula.

While I have experience in mathematics and logic, I do not have real experience in programming.

Logic is propositional logic, formulas can have only $\wedge, \vee$ and $\neg$.

I have two choices:

1) The formula is somehow transferred into list, and then, one by one I go though the items in the list and check their Head. If the output of Head is And, replace it with Or. And so on.

2) As we all know, formula in proposition logic can be expressed as a tree. In particular you can calculate the depth of the tree. Mathematica has Depth for that. So for example in $\neg p\wedge q$ you would start from the $\wedge$, which is at depth 1 and then go deeper at level 2, and so on. The problem with this approach is that how I can move 'sideways' while in some level? I can write a loop that goes through the depth of the formula, but how I can know how many subformulas there are in particular depth?

All tricks and tips are very welcomed!

• See BooleanConvert[ ] – Dr. belisarius Mar 28 '16 at 16:37
• Maybe something like this : a && (c || !d) /. {Or -> And, And -> Or, Not -> Not, (x_)?AtomQ :> !x} ---Output---> !a || ( !c && d) – andre314 Mar 28 '16 at 19:58