1
$\begingroup$

First we define a context:

Begin["Ctx`"];

ClearAll[f];
f[m_][n_] := n // (Range /* Append[m]);

End[];

We can check that

Ctx`f[2][3] == {1, 2, 3, 2} (* True *)

as expected. Now we define another function:

ClearAll[g];
g[f_] := f // (Head /* SubValues);

When we try to call g on Ctx``f we get the following error:

g[Ctx`f[2]]

SubValues: Argument Head[Ctx`f[2]] at position 1 is expected to be a symbol.

However, Head clearly returns a Symbol. Moreover, if I define a new function h as follows:

ClearAll[h]
h[f_] := f // (Head /* {SubValues} /* Through /* First);

I get exactly what I expect from g above:

h[Ctx`f[2]]

(* {HoldPattern[Ctx`f[Ctx`m_][Ctx`n_]] :> (Range /* Append[Ctx`m])[Ctx`n]} *)

What am I failing to understand about contexts? Why is g not behaving like h?

$\endgroup$
2
  • 1
    $\begingroup$ SubValues is HoldAll, so Head is not being evaluated. You could try using something like g[f_]:=f//(Head/*Evaluate/*SubValues) $\endgroup$
    – Carl Woll
    Jan 15, 2021 at 22:17
  • $\begingroup$ @CarlWoll If you post your comment as an answer, I will accept it, because it works. Thanks! $\endgroup$ Jan 15, 2021 at 22:20

1 Answer 1

2
$\begingroup$

SubValues is HoldAll, so Head is not being evaluated. You could try using something like:

g[f_] := f // (Head /* Evaluate /* SubValues)

or:

g[f_] := With[{h = Head[f]}, SubValues[h]]

Then:

g[Ctx`f[2]]

{HoldPattern[Ctx`f[m_][n_]] :> (Range /* Append[m])[n]}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.