Here's a simple method for checking if any of the down values of a function matches f[5]
literally. It's a good use case for Verbatim
:
KeyExistsForFunctionsQ[fun_, arg_] := AnyTrue[
Keys[DownValues[fun]],
MatchQ[#, Verbatim[HoldPattern[fun[arg]]]] &
]
This method can be extended to checking other types of down values of functions as well. For example:
ValueQWithoutEval[fun_, arg_] := AnyTrue[
Keys[DownValues[fun]],
MatchQ[Hold[fun[arg]], Hold[#]] &
]
This will check if any of the down values of a function will match fun[arg]
without actually trying to evaluate it (because it might be expensive, for example). Note that this will not work for assignments that use Condition
in the r.h.s. of :=
like
f[x_] := With[{y = 2 x + 1}, y /; PrimeQ[y]]
For functions like that, you can't avoid evaluating the function at least partially to find out if the argument matches.
DownValues[f]
andassoc = Association[Extract[#, {1, 1}, Hold] -> Extract[#, 2, Hold] & /@ DownValues[f]]
. It's then possible to checkKeyExistsQ[assoc, Hold[f[5]]]
$\endgroup$