Basic Examples (2)
Evaluate a symbol with a value:
A symbol without value gives $Failed:
This expression only evaluates in its arguments, the head f has no effect:
FailIfUnevaluated detects this and considers this a trivial evaluation:
It does not simply test if the head remains the same. This is an example of a non-trivial evaluation that FailIfUnevaluated allows:
Scope (3)
If the head evaluates on its own but afterwards no further evaluation takes place, FailIfUnevaluated will give $Failed:
This fails because f is taken to be no more than a simple alias for g:
This will not fail:
However, if the whole expression transforms into another inert expression, this is still counted as a non-trivial evaluation:
Use a different default:
The default does not evaluate if it's not needed:
FailIfUnevaluated respects attributes of the head while testing for evaluation semantics:
This expression evaluates:
This expression doesn't evaluate because of the HoldFirst attribute, which causes FailIfUnevaluated to fail:
Applications (3)
Due to the symbolic nature of the language, many Wolfram Language functions stay unevaluated when given unsupported arguments:
This can be easily detected by FailIfUnevaluated:
Note that FailIfUnevaluated does not test for a specific return value. In the following examples, Or returns the same result, but the second example represents a partial evaluation which is considered non-trivial:
Symbolic math functions usually return unevaluated if they can't find a simpler result:
Detect this with FailIfUnevaluated: