I have a following problem.
f[1]=1;
f[2]=2;
f[_]:=0;
dvs = DownValues[f];
this gives
dvs =
{
HoldPattern[f[1]] :> 1,
HoldPattern[f[2]] :> 2,
HoldPattern[f[_]] :> 0
}
My problem is that I would like to extract only definitions for f[1] and f[2] etc but not the general definition f[_], and I do not know how to do this.
I tried,
Cases[dvs, HoldPattern[ f[_Integer] :> _ ]] (*)
but it gives me nothing, i.e. the empty list.
Interestingly, changing HoldPattern into temporary^footnote
dvs1 = {temporary[1] :> 1, temporary[2] :> 2, temporary[_] :> 0}
and issuing
Cases[dvs1, HoldPattern[temporary[_Integer] :> _]]
gives
{temporary[1] :> 1, temporary[2] :> 2}
and it works. This means that (*) is almost a solution.
I do not not understand why does it work with temporary and not with HoldPattern? How can I make it work directly with HoldPattern?
Of course, the question is what gets evaluated and what not etc. The ethernal problem when coding in Mathematica. Something for real gurus...
With best regards Zoran
footnote = I typed it by hand as replacement "/. HoldPattern -> temporary" actually executes the f[_]:=0 rule and gives someting strange, this excecution I certainly would like to avoid.
The reason is that you have to escape the
HoldPattern
, perhaps with Verbatim:There are just a few heads for which this is necessary, and
HoldPattern
is one of them, precisely because it is normally "invisible" to the pattern-matcher. For yourtemporary
, or other heads, this wouldn't be necessary. Note by the way that the patternf[_Integer]
is wrapped inHoldPattern
- this timeHoldPattern
is used for its direct purpose - to protect the pattern from evaluation. Note thatRuleDelayed
is also wrapped inVerbatim
- this is in fact another common case forVerbatim
- this is needed becauseCases
has a syntax involving a rule, and we do not wantCases
to use this interpretation here. So, this is IMO an overall very good example to illustrate bothHoldPattern
andVerbatim
. Note also that it is possible to achieve the goal entirely withHoldPattern
, like so:However, using
HoldPattern
for escaping purposes (in place ofVerbatim
) is IMO conceptually wrong.EDIT
To calrify a little the situation with
Cases
, here is a simple example where we use the syntax ofCases
involving transformation rules. This extended syntax instructsCases
to not only find and collect matching pieces, but also transform them according to the rules, right after they were found, so the resulting list contains the transformed pieces.But what if we need to find elements that are themselves rules? If we just try this:
It doesn't work since
Cases
interprets the rule in the second argument as an instruction to use extended syntax, find a symbol and replace it with_
. Since it searches on level 1 by default, and symbols are on level 2 here, it finds nothing. Observe:In any case, this is not what we wanted. Therefore, we have to force
Cases
to consider the second argument as a plain pattern (simple, rather than extended, syntax). There are several ways to do that, but all of them "escape"RuleDelayed
(orRule
) in some way:In all cases, we either avoid the extended syntax for
Cases
(last two examples), or manage to use it to our advantage (first case).Leonid, of course, completely answered the question about why your
temporary
solution works butHoldPattern
does not. However, as an answer to your original problem of extracting thef[1]
andf[2]
type terms, his code is a bit ugly. To solve just the problem of extracting these terms, I would just concentrate on the structure of the left-hand-side of the definition and use the fact thatFreeQ
searches at all levels. So, definingAll of the following
yield the result
Provided there are no
f[...]
(or, for the first version,Blank[]
) terms on the right-hand-side of the downvalues off
, then one of the above will probably be suitable.Based on Simon's excellent solution here, I suggest: