# 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 Jan 24 '12 at 12:12
• @Mr.Wizard, forgot about that one, and the word "currying" is not in my vocabulary. Jan 24 '12 at 14:16
• There is a way to use the attribute of f on each following sub value but again f will be a DownValue. Not sure if this helpes or valid to be an answer but I thought to share it. ClearAll[f2]; SetAttributes[f2, HoldAll]; f2[x_] := (AppendTo[l, ToString@Unevaluated[x]]; f2); and then l = {}; f2[3 + 4][2 + 5][3 + 34]; l which gives {"3 + 4", "2 + 5", "3 + 34"} Aug 4 '16 at 17:51
• @Algohi I'd write it up as an answer. But, it does go against the usual idea of functional code being side effect free. Aug 4 '16 at 18:20
• @rcollyer, can you give my an example where this answer generates a side effect. I am trying to think of one but I couldn't .Thanks Aug 4 '16 at 18:52

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. :) 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 :) Jan 23 '12 at 20:41
• I like to think of you as a generous guru, freely sharing your abundant knowledge. If you only care about votes I'll have to imagine you as a crackpot genius who attaches great import to the immaterial. (But I presume you were joking, as am I.) Jan 24 '12 at 12:28
• @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? Jan 24 '12 at 12:33
• Quoted in my profile. Jan 24 '12 at 12:38

This is an old discussion but is about an issue that resurfaces every now and then. One of the best (for a given sense of elegance) answers is the one posted on Stack Overflow

ClearAll[f]
SetAttributes[f, HoldAllComplete]
f[a_, b_, c_] := {
ToString@Unevaluated@a,
ToString@Unevaluated@b,
ToString@Unevaluated@c
}
f[a__] := Function[x, f[a, x], HoldAll]


However it contains some extra code to handle the case f[a][b, c], and this because the definition with Function can only handle one argument. There is a little undocumented feature though, that lets one define attributes for variadic functions. The last definition have to be replaced by

f[a__] := Function[Null, f[a, ##], HoldAll]


This leads to the desired result in all cases

f[1 + 1, 2 + 2, 6 + 1]
f[1 + 1, 2 + 2][6 + 1]
f[1 + 1][2 + 2, 6 + 1]
f[1 + 1][2 + 2][6 + 1]

(*
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
*)

• The trick with Function[Null, ..., {Attributes__}] is really neat. Thanks for sharing that! May 17 '17 at 9:39