SubValues
, as discussed in a previous question, are declared as follows
f[x_][y_] := {ToString[Unevaluated[x]], ToString[Unevaluated[y]]}
But, attempting to use SetAttributes
on f
only affects the DownValues
of f
during evaluation, not the SubValues
. In other words, if HoldAll
is set on f
, then only x
, in the above code, is held. In code,
SetAttributes[f, HoldAll]
f[ 1 + 2 ][ 3 + 4 ]
(*
==> { "1 + 2", "7" }
*)
Attempting to use SetAttributes
on f[x]
results in the error
SetAttributes::sym: "Argument f[x] at position 1 is expected to be a symbol."
and, similarly, for f[x_]
simply because neither are symbols.
A work around is not to set a SubValue
directly, but, instead, return a pure function and use the third argument to set the attribute, as follows
SetAttributes[g, HoldAll]
g[x_] := Function[{y},
{ToString[Unevaluated[x]], ToString[Unevaluated[y]]},
{HoldAll}
]
g[ 1 + 2 ][ 3 + 4 ]
(*
==> {"1 + 2", "3 + 4"}
*)
But, SubValues[g]
returns an empty list, indicating that while equivalent, this construct is not processed in the same manner.
So, how does one set the attributes on f
such that the SubValues
are affected during evaluation?
Answer
Your question really is about how to make attributes of f
affect also the evaluation of other groups of elements, like y
and z
in f[x___][y___][z___]
. To my knowledge, you can not do it other than using tricks like returning a pure function and the like.
This is because, the only tool you have to intercept the stages of evaluation sequence when y
and z
are evaluated, is the fact the heads are evaluated first. So, anything you can do to divert the evaluation from its standard form (regarding y
and z
), must be related to evaluation of f[x]
, in particular substituting it by something like a pure function. Once you pass that stage of head evaluation, you have no more control of how y
and z
will be evaluated, as far as I know.
Generally, I see only a few possibilities to imitate this:
- return a pure function with relevant attributes (as discussed in the linked answer)
- return an auxiliary symbol with relevant attributes (similar to the first route)
- play with evaluation stack. An example of this last possibility can be found in my answer here
Here is another example with Stack
, closer to those used in the question:
ClearAll[f];
f :=
With[{stack = Stack[_]},
With[{fcallArgs =
Cases[stack, HoldForm[f[x_][y_]] :>
{ToString[Unevaluated[x]], ToString[Unevaluated[y]]}]},
(First@fcallArgs &) & /; fcallArgs =!= {}]];
And:
In[34]:= f[1 + 2][3 + 4] // InputForm
Out[34]//InputForm= {"1 + 2", "3 + 4"}
Perhaps, there are other ways I am not aware of. The general conclusion I made for myself from considering cases like this is that the extent to which one can manipulate evaluation sequence is large but limited, and once you run into a limitation like this, it is best to reconsider the design and find some other approach to the problem, since things will quickly get quite complex and go out of control.
Comments
Post a Comment