# How do you set attributes on SubValues?

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?

-
Question raised on StackOverflow: Currying with Mathematica –  Mr.Wizard Jan 24 '12 at 12:12
@Mr.Wizard, forgot about that one, and the word "currying" is not in my vocabulary. –  rcollyer Jan 24 '12 at 14:16

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.

-
Incidentally, you get a +1 for the phrase, "once you run into a limitation like this, it is best to reconsider the design" as it is very true. :) –  rcollyer Jan 23 '12 at 18:40
@rcollyer I wish I always knew which single phrase will bring me upvotes - this would allow me to significantly shorten my posts :) –  Leonid Shifrin Jan 23 '12 at 20:41
@Mr.Wizard Sorry to tell you this, but you are spot on - my main purpose of being here is in collecting the votes. I'd even go further and say that this is my main purpose in life. As they say, the more, the merrier, isn't it? –  Leonid Shifrin Jan 24 '12 at 12:33
Quoted in my profile. –  Mr.Wizard Jan 24 '12 at 12:38
@Mr.Wizard No,no - by all means, keep it if you wish. I was just trying to be consistent with the ways of scientific communication, regarding citations. –  Leonid Shifrin Jan 24 '12 at 12:55