4
$\begingroup$

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run:

example

Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is such a thing possible? To go a little bit further, I'd like:

bob
to return BobKet and

alice

to return AliceKet so that I could make a function that recognizes these heads like: function

Thanks!

Here are the corresponding lines of code

Head[Ket[\[Placeholder]]]

Head[Ket[Subscript[x, B]]]

Head[Ket[Subscript[x, A]]]

combineBobAndAlice[b_BobKet, a_AliceKet] := Ket[{ {Subscript[b[[1, 1]], B], Subscript[a[[1, 1]], A]} }]

$\endgroup$
  • $\begingroup$ Please post your code as text that we can copy and paste to duplicate your results. $\endgroup$ – bill s Jan 28 '14 at 5:29
  • $\begingroup$ Apply is usually used to change the head of an expression. $\endgroup$ – Mike Honeychurch Jan 28 '14 at 5:32
  • 1
    $\begingroup$ that is true. I do not think you can change head of an Atom. Try Clear[bob]; r=1; r[[0]]=bob you'll get an error. But r = Sin[x]; r[[0]]=bob works. Now Head[r] returns bob. I think playing games with Heads to do what is asked here is the wrong way to go about writing a program. $\endgroup$ – Nasser Jan 28 '14 at 5:37
  • $\begingroup$ Nasser, thanks for that idea, it has pointed me in the right direction I think. $\endgroup$ – John Smith Jan 28 '14 at 5:45
7
$\begingroup$

I don't believe it is possible to implement your literal request. While you can make an UpSet definition for Head:

Unprotect[Ket];
Head[Ket[_]] ^:= Bob
Head[Ket[Subscript[x, B]]] ^:= BobKet
Head[Ket[Subscript[x, A]]] ^:= AliceKet

Head[Ket[Subscript[x, B]]]
BobKet

Since Head is not actually used by the pattern matcher this does not produce your desired behavior:

f[_BobKet] := "hit!"
f[Ket[Subscript[x, B]]]
f[Ket[Subscript[x, B]]]

(I don't actually have Ket in version 7, but I see no reason for this not to work unless Ket is atomic.)

However, I can see no need for this behavior. Instead you should simply use a pattern that matches what you want it to match. You can assign patterns to global Symbols to use them easily, if that is your concern:

bob = HoldPattern @ Ket[Subscript[x, B]];
alice = HoldPattern @ Ket[Subscript[x, A]];

f[x : bob, y : alice] := {"it worked!", x, y}

f[Ket[Subscript[x, B]], Ket[Subscript[x, A]]]
{"it worked!", Ket[Subscript[x, B]], Ket[Subscript[x, A]]}

You can even use named patterns within a pattern, e.g.:

bob = HoldPattern @ Ket[Subscript[u_, B]];
alice = HoldPattern @ Ket[Subscript[v_, A]];

f[x : bob, y : alice] := {u, v}

f[Ket[Subscript[foo, B]], Ket[Subscript[bar, A]]]
{foo, bar}

I hope this helps you implement what you need.

$\endgroup$
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – Mr.Wizard Mar 27 '16 at 9:55

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.