12
$\begingroup$

I have been looking at various usages of Pattern objects. Here's a number of ways to define the same thing:

mod[x_] := {x}

which gives mod DownValues. Nothing unexpected.

e : mod[_] := ReleaseHold @ (Hold[e] /. mod -> List)

This is nice, as we don't need to give the argument of mod a name now.

e_mod := ReleaseHold @ (Hold[e] /. mod -> List)

Now we don't need to specify the pattern of arguments to mod at all. However, all three lines of code give DownValues to the symbol mod.

Let's look at the FullForm:

SetDelayed[mod[Pattern[x, Blank[]]], List[x]]
SetDelayed[Pattern[e, mod[Blank[]]], ReleaseHold[ReplaceAll[Hold[e], Rule[mod, List]]]]
SetDelayed[Pattern[e, Blank[mod]], ReleaseHold[ReplaceAll[Hold[e], Rule[mod, List]]]]

The first example is nothing special, the first argument of SetDelayed has the mod head, thus mod gets the DownValues. However the next two examples have head Pattern. If we use someOtherHead instead of Pattern, (in the second definition) we will get a "function" that does this:

someOtherHead[e, mod[x]]
(* e *)
someOtherHead[e, mod[x, y]]
(*     someOtherHead[e, mod[x, y]] *)

and if we add the third definition as well, then:

someOtherHead[e, mod[x, y]]
(* e *)

So clearly there are some special rules for the head Pattern.

My question is three-fold:

  • How does MMA choose, which Symbol gets its DownValues in a Set or a SetDelayed? Especially I am interested in the non-standard procedure, as in my last two examples, but will be happy to get better insight in the standard case presented in my first example.

  • Where is this behavior documented, if at all? I am primarily looking for references, but will happily accept answers from those who know how this works even if they cannot point me to a specific reference.

  • A sub-question of the previous two: where can I find the special rules specifically for the head Pattern?

$\endgroup$
2
$\begingroup$

Your constructions were very instructive. Are you, to put your first question differently, asking why it is mod gets the downvalues in all three case; especially when examples 2 and 3 clearly show that Pattern is the head?

This may not help but the documentation of the symbol ":" says that s_h is equivalent to s: _h--the name s is given to the pattern with head _h. Also the definition of _ (Blank) says that _h is any expression with head h. Presumably this means as well that s_h is also an expression s with head h. In that case we have something like f[x_]=x^2 in which the head, f, gets the rule (x^2) as a DownValue. Therefore your 2nd and 3rd definitions apply to any expression with head mod, so mod gets the downvalues.

Again, this may not be informative but I decided to try to address your question because I've been trying to understand ownvalues, downvalues and upvalues for some time.

$\endgroup$
  • $\begingroup$ That's an interesting take. Are you trying to say, that regardless of the FullForm, the expression e_mod is treated as an expression with Head mod, rather than Pattern? (+1 anyway) $\endgroup$ – LLlAMnYP Apr 28 '16 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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