// This is executable rex code.

// isTaut checks an expression tree for being a tautology

isTaut(E) => hasVar(E) ?                  // If E has a variable
               V = getVar(E),             // then it is a tautology
               E0 = subst(0, V, E),       // iff both E0 and E1 are
               E1 = subst(1, V, E),       // (Boole-Shannon Expansion).
               (isTaut(E0) && isTaut(E1))
                                          // If E has no variable, it is a
           : eval(E);                     // tautology iff it evaluates to 1.


// eval evaluates an expression tree having no variables

eval(0)             => 0;
eval(1)             => 1;
eval(['+' | Trees]) => some(eval, Trees);
eval(['*' | Trees]) => all(eval, Trees);
eval(['!' , Tree])  => !eval(Tree);

 

Extra Credit Part:

Let n be the number of distinct variables in the expression. Then for some constants c and d,

T(0) => c;
T(n+1) => d + 2*T(n);

For general n

T(n) = d + 2*T(n-1)
     = d + 2*(d+2*T(n-2))
   = d + 2*(d+2*(d+2*T(n-3)))
   …

   = d + 2*(d+2*(d+2*(d+… +2*T(0)… )))
   = d + 2*(d+2*(d+2*(d+… +2*c… )))
   = d + d*d + 4*d + 8*d + …. + 2n*d + 2n*c

Therefore

T(n) is O(2n).