Home > Archive > Mathematica > October 2006 > DownValues and Cases
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
DownValues and Cases
|
|
| Bruce Colletti 2006-10-30, 7:43 pm |
| Re Mathematica 5.2 under WinXP.
The results of the first two Cases statements leads me to anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third.
The output of the fourth Cases statement confirms this expectation.
Unfortunately, the output of the third statement is the empty list.
Why so? I've reviewed a related mid-July MathGroup thread, but don't see the answer (if it's there at all).
Thankx.
Bruce
--------------------------
f@1=3.5;
f@2=5;
f[_]:=0;
h=DownValues@f
Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, HoldPattern[f[_]] :> 0}
Cases[h,HoldPattern[f[x_]] -> x,Infinity]
Out[5]={1,2,_}
Cases[h,(_ :> y_) -> y,Infinity]
Out[6]={3.5,5,0}
Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity]
Out[7]={}
Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}]
Out[8]={{a,4},{b,5},{c,6}}
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
|
On 26 Oct 2006, at 22:54, Andrzej Kozlowski wrote:
>
> On 26 Oct 2006, at 15:39, Bruce Colletti wrote:
>
>
> This is rather tricky. The problem is how to force the
> PatternMatcher to interpret HoldPattenr literally rather than as
> the pattern to be held. Note that:
>
> Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y},
> Infinity]
>
> {{x, 5}}
>
> works, but:
>
>
> Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y},
> Infinity]
>
> {}
>
> doesn't, because now x_ is also interpreted literally rather than
> as a pattern. So you have got to somehow to avoid using HoldPattern
> directly in your pattern (this is now rally getting confusing), for
> example like this:
>
>
> Cases[h, (p_ :> y_) /; Head[p] === HoldPattern :>
> {p[[1,1]], y}, Infinity]
>
> {{1, 3.5}, {2, 5}, {_, 0}}
>
> There are probably more elegant ways but none comes to my mind just
> now.
>
> Andrzej Kozlowski
A more compact way to do the above is:
Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity]
But, it seems to me that the most accurate way to match expressions
with Head HoldPattern, as you wanted to do, is to first replace
HoldPattern by something like Hold or Unevaluated:
Cases[h /. HoldPattern -> Hold,
(Hold[f[x_]] :> y_) -> {x, y}, Infinity]
{{1, 3.5}, {2, 5}, {_, 0}}
Andrzej Kozlowski
| |
| Bruce Colletti 2006-10-30, 7:43 pm |
| Andrezj
Your reply, and that of another, has gripped and led me the statement below (crafted with Wolfram Tech Support's help):
Cases[h, (x_ :> _) :> Level[x, -1]]
This is likewise appealing...but does it suffer a flaw not found in your solutions?
Thanks.
Bruce
=====================
From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
Subject: Re: DownValues and Cases
On 26 Oct 2006, at 22:54, Andrzej Kozlowski wrote:
>
> On 26 Oct 2006, at 15:39, Bruce Colletti wrote:
>
>
> This is rather tricky. The problem is how to force the
> PatternMatcher to interpret HoldPattenr literally rather than as
> the pattern to be held. Note that:
>
> Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y},
> Infinity]
>
> {{x, 5}}
>
> works, but:
>
>
> Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y},
> Infinity]
>
> {}
>
> doesn't, because now x_ is also interpreted literally rather than
> as a pattern. So you have got to somehow to avoid using HoldPattern
> directly in your pattern (this is now rally getting confusing), for
> example like this:
>
>
> Cases[h, (p_ :> y_) /; Head[p] === HoldPattern :>
> {p[[1,1]], y}, Infinity]
>
> {{1, 3.5}, {2, 5}, {_, 0}}
>
> There are probably more elegant ways but none comes to my mind just
> now.
>
> Andrzej Kozlowski
A more compact way to do the above is:
Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity]
But, it seems to me that the most accurate way to match expressions
with Head HoldPattern, as you wanted to do, is to first replace
HoldPattern by something like Hold or Unevaluated:
Cases[h /. HoldPattern -> Hold,
(Hold[f[x_]] :> y_) -> {x, y}, Infinity]
{{1, 3.5}, {2, 5}, {_, 0}}
Andrzej Kozlowski
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
| That will, of course, solve the problem you asked about, concerning
DownValues. But it would not solve the more general problem with
HoldPattern (admittedly one that is not very likely to occur
naturally) . Suppose we have the definitions
f[x_] := 1; g[x_] := 2;
and consider the list
h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b -> 3};
Suppose we are only interesting in extracting information about f, in
the way you originally wanted. This will do it:
Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) ->
{x, y}, Infinity]
{{x_, 1}}
but this will do something rather different:
Cases[h, (x_ :> _) :> Level[x, -1]]
{{x, _, x_, 1}, {x, _, x_, 2}}
That's what I meant when I wrote "the most accurate way" - the way
that will allow you to pick up exactly the pattern you want when
similar ones are present.
Andrzej Kozlowski
On 27 Oct 2006, at 06:19, Bruce Colletti wrote:
> Andrezj
>
> Your reply, and that of another, has gripped and led me the
> statement below (crafted with Wolfram Tech Support's help):
>
> Cases[h, (x_ :> _) :> Level[x, -1]]
>
> This is likewise appealing...but does it suffer a flaw not found in
> your solutions?
>
> Thanks.
>
> Bruce
>
> =====================
> From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
> Subject: Re: DownValues and Cases
>
> (tm) Pro*
>
> On 26 Oct 2006, at 22:54, Andrzej Kozlowski wrote:
>
>
>
> A more compact way to do the above is:
>
> Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity]
>
>
> But, it seems to me that the most accurate way to match expressions
> with Head HoldPattern, as you wanted to do, is to first replace
> HoldPattern by something like Hold or Unevaluated:
>
>
> Cases[h /. HoldPattern -> Hold,
> (Hold[f[x_]] :> y_) -> {x, y}, Infinity]
>
>
> {{1, 3.5}, {2, 5}, {_, 0}}
>
>
> Andrzej Kozlowski
>
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
| Of course I really meant:
h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b :> 3};
in which case you get:
Cases[h, (x_ :> _) :> Level[x, -1]]
{{x, _, x_, 1}, {x, _, x_, 2}, {}}
Of course one can construct even more artificial examples where even
my method will not produce the wanted result, namely, when the list h
already contains something of the form Hold[f[a]]:>b, which we do not
wish to pick up. One could modify it for such cases but then one
could again construct examples where the new method would fail. I
doubt that a completely universal and fail proof method could be
found to deal with all such cases.
Andrzej
On 27 Oct 2006, at 11:43, Andrzej Kozlowski wrote:
> That will, of course, solve the problem you asked about, concerning
> DownValues. But it would not solve the more general problem with
> HoldPattern (admittedly one that is not very likely to occur
> naturally) . Suppose we have the definitions
>
> f[x_] := 1; g[x_] := 2;
>
> and consider the list
>
> h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b -> 3};
>
> Suppose we are only interesting in extracting information about f,
> in the way you originally wanted. This will do it:
>
>
>
> Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) ->
> {x, y}, Infinity]
>
>
> {{x_, 1}}
>
>
>
> but this will do something rather different:
>
>
>
> Cases[h, (x_ :> _) :> Level[x, -1]]
>
>
> {{x, _, x_, 1}, {x, _, x_, 2}}
>
>
>
> That's what I meant when I wrote "the most accurate way" - the way
> that will allow you to pick up exactly the pattern you want when
> similar ones are present.
>
> Andrzej Kozlowski
>
>
> On 27 Oct 2006, at 06:19, Bruce Colletti wrote:
>
>
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
|
On 26 Oct 2006, at 15:39, Bruce Colletti wrote:
> Re Mathematica 5.2 under WinXP.
>
> The results of the first two Cases statements leads me to
> anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third.
>
> The output of the fourth Cases statement confirms this expectation.
>
> Unfortunately, the output of the third statement is the empty list.
>
> Why so? I've reviewed a related mid-July MathGroup thread, but
> don't see the answer (if it's there at all).
>
> Thankx.
>
> Bruce
>
> --------------------------
>
> f@1=3.5;
> f@2=5;
> f[_]:=0;
> h=DownValues@f
>
> Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5,
> HoldPattern[f[_]] :> 0}
>
> Cases[h,HoldPattern[f[x_]] -> x,Infinity]
> Out[5]={1,2,_}
>
> Cases[h,(_ :> y_) -> y,Infinity]
> Out[6]={3.5,5,0}
>
> Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity]
> Out[7]={}
>
> Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}]
> Out[8]={{a,4},{b,5},{c,6}}
>
This is rather tricky. The problem is how to force the PatternMatcher
to interpret HoldPattenr literally rather than as the pattern to be
held. Note that:
Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y},
Infinity]
{{x, 5}}
works, but:
Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y},
Infinity]
{}
doesn't, because now x_ is also interpreted literally rather than as
a pattern. So you have got to somehow to avoid using HoldPattern
directly in your pattern (this is now rally getting confusing), for
example like this:
Cases[h, (p_ :> y_) /; Head[p] === HoldPattern :>
{p[[1,1]], y}, Infinity]
{{1, 3.5}, {2, 5}, {_, 0}}
There are probably more elegant ways but none comes to my mind just now.
Andrzej Kozlowski
| |
|
|
Hi Bruce,
I do not know the exact answer, I can only guess.
It looks like nested HoldPattern are stripped, they have the same effect
as a single HoldPattern. This prevents the use of "HoldPattern" as a
pattern. What we can nevertheless do to get {x,y }is a 2 step replacement:
Cases[h, (x_ :> y_) -> {x, y}] /. _[_[x_]] -> x
Daniel
Bruce Colletti wrote:
> Re Mathematica 5.2 under WinXP.
>
> The results of the first two Cases statements leads me to anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third.
>
> The output of the fourth Cases statement confirms this expectation.
>
> Unfortunately, the output of the third statement is the empty list.
>
> Why so? I've reviewed a related mid-July MathGroup thread, but don't see the answer (if it's there at all).
>
> Thankx.
>
> Bruce
>
> --------------------------
>
> f@1=3.5;
> f@2=5;
> f[_]:=0;
> h=DownValues@f
>
> Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, HoldPattern[f[_]] :> 0}
>
> Cases[h,HoldPattern[f[x_]] -> x,Infinity]
> Out[5]={1,2,_}
>
> Cases[h,(_ :> y_) -> y,Infinity]
> Out[6]={3.5,5,0}
>
> Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity]
> Out[7]={}
>
> Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}]
> Out[8]={{a,4},{b,5},{c,6}}
>
| |
| Carl Woll 2006-10-30, 7:43 pm |
| Andrzej Kozlowski wrote:
>On 26 Oct 2006, at 15:39, Bruce Colletti wrote:
>
>
>
>
>This is rather tricky. The problem is how to force the PatternMatcher
>to interpret HoldPattenr literally rather than as the pattern to be
>held. Note that:
>
>Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y},
> Infinity]
>
>{{x, 5}}
>
>works, but:
>
>
>Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y},
> Infinity]
>
>{}
>
>doesn't, because now x_ is also interpreted literally rather than as
>a pattern.
>
In this case you can apply Verbatim to just the head HoldPattern, but
then you still need a HoldPattern to prevent f[x_] from evaluating. So:
In[18]:=
Cases[h, (Verbatim[HoldPattern][HoldPattern[f[x_]
]] :> y_) -> {x, y},
Infinity]
Out[18]=
{{1, 3.5}, {2, 5}, {_, 0}}
Carl Woll
Wolfram Research
>So you have got to somehow to avoid using HoldPattern
>directly in your pattern (this is now rally getting confusing), for
>example like this:
>
>
>Cases[h, (p_ :> y_) /; Head[p] === HoldPattern :>
> {p[[1,1]], y}, Infinity]
>
>{{1, 3.5}, {2, 5}, {_, 0}}
>
>There are probably more elegant ways but none comes to my mind just now.
>
>Andrzej Kozlowski
>
>
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
|
On 27 Oct 2006, at 20:58, Carl Woll wrote:
> Andrzej Kozlowski wrote:
>
> In this case you can apply Verbatim to just the head HoldPattern,
> but then you still need a HoldPattern to prevent f[x_] from
> evaluating. So:
>
> In[18]:=
> Cases[h, (Verbatim[HoldPattern][HoldPattern[f[x_]
]] :> y_) -> {x,
> y}, Infinity]
> Out[18]=
> {{1, 3.5}, {2, 5}, {_, 0}}
>
> Carl Woll
> Wolfram Research
>
Yes, of course! . I actually thought about wrapping Verbatim around
the Head, but then realized that f[x] would then evaluate and at this
point gave up the idea. It did not occur to me to use a second
HoldPattern.
Andrzej Kozlowski
| |
| Andrzej Kozlowski 2006-10-30, 7:43 pm |
| Actually I was wrong in the last line of my last post: I think there
is a fool proof and universal way of doing this. However, as it does
not seem to me important I will only describe it without
implementing. All you need to do is define a unique symbol (Using
the Mathematica function Unique) and give it the attribute HoldAll
(using SetAttributes). Then simply do what I did below but using this
new symbol instead of Hold. Because the symbol is unique it will not
interfere with any other symbols that might possibly be in the list.
Andrzej Kozlowski
On 27 Oct 2006, at 12:32, Andrzej Kozlowski wrote:
> Of course I really meant:
>
> h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b :> 3};
>
> in which case you get:
>
>
> Cases[h, (x_ :> _) :> Level[x, -1]]
>
>
> {{x, _, x_, 1}, {x, _, x_, 2}, {}}
>
> Of course one can construct even more artificial examples where
> even my method will not produce the wanted result, namely, when the
> list h already contains something of the form Hold[f[a]]:>b, which
> we do not wish to pick up. One could modify it for such cases but
> then one could again construct examples where the new method would
> fail. I doubt that a completely universal and fail proof method
> could be found to deal with all such cases.
>
> Andrzej
>
> On 27 Oct 2006, at 11:43, Andrzej Kozlowski wrote:
>
>
|
|
|
|
|