I want to define a function that only operates on expressions with a head from a given list, such as
f[expr:(_And|_Or)] := Print["LHS is ", expr[[1]]];
This of course works fine when x is just an expression such as expr = p == q || p == q + 1. However, now I'd like to be able to print out parts of expr even when p and q both have values defined. That is, if f and expr are defined as above, then
p = 1;
q = 2;
f[expr]
should print "LHS is True". The obvious thing to try is of course to add
SetAttributes[f, HoldFirst]
but now f doesn't match expr, i.e. f[expr] simply returns f[expr]. I've tried variations of this, but nothing I can come up with seems to work, expr is always either evaluated before matching the head or the correct head is not found.
analyzeConditional[p == q || p == q+1], which would print "p == q is False", "p == q + 1 is True", and so on, decomposing the conditional at boolean operators and printing out each subcondition, and then finally returning the value of the whole conditional. The purpose is to debug a situation where an algorithm terminates at a reasonably complex combination of conditions, and I want to know at which subcondition. – Timo Jun 03 '14 at 05:49