# Tag Protected Question

What I want to do is take a variable SigmaC and access it like a set and an array. Ideally, SigmaC[s] would give 4 and SigmaC[[4]] would give s.

This is the code I have tried so far, but it is giving a Set::write: Tag List ... is Protected error. How can I fix it?

Clear[x, f, u, s, c, b];
SigmaD = SigmaC = {u, f, c, s, b};
PiD = PiC = {1, 2, 3, 4, 5, 6};
PiD[x_] = Position[PiD, x];
RelD = {{0, 1, 0, 0, 1, 0},
{0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 1, 0}};
SigmaC[y_] = Position[SigmaC, y];
PiC[y_] = Position[PiC, y];
RelC = {{0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 1, 0},
{1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 1, 0}};


I haven't been able to find any specific advice on it.

Edit: Actually, none of the function definitions work. Extra Edit: Is there a way to overload symbols in Mathematica?

• What do you want to accomplish with this code? I can't understand it. – MarcoB May 4 '18 at 4:43
• Well, what I want to do is take a variable SigmaC and access it like a set and an array. So SigmaC[s] would give 4 and Sigma[[4]] would give s. – simplet May 4 '18 at 4:46
• In your current code, SigmaD already has a value when you write SigmaD[x_] = Position[SigmaD, x];, so that is interpreted to {u, f, c, s, b}[x_] = Position[{u, f, c, s, b}, x];. That does not make much sense, and fails. Similarly with the other definitions. – MarcoB May 4 '18 at 4:47
• Thanks. Is there a way around that? – simplet May 4 '18 at 4:48
• PiD evals to a List because it was defined using Set. Avoiding that requires usage of SetDelayed. – Daniel Lichtblau May 4 '18 at 15:38

You could use an underlying association, and interrogate it through a function whose behavior depends on the input "type":

assoc = AssociationThread[# -> Range[Length[#]]] &@{u, f, c, s, b};

ClearAll[a]
a[x_Symbol] := assoc[x]
a[n_Integer] := Keys[assoc][[n]]


Then you can try:

a[c]    (* Out:  3 *)
a[3]    (* Out:  c *)


If your lists contain duplicates, then assoc = PositionIndex[yourList] would be a safer choice to associate symbols with their positions. However, positions are encoded as lists of values in that case, and it is unclear to me what you might want your function to return then.

Clear[x,f,u,s,c,b];
PiD=PiC={1,2,3,4,5,6};
PiD[x_]=Position[PiD,x];
RelD={{0,1,0,0,1,0},{0,0,0,1,1,0},{0,0,0,0,0,1},{0,0,0,0,1,0},{0,0,0,1,1,0}};
SigmaC[y_]=Position[SigmaC,y];
PiC[y_]=Position[PiC,y];
RelC={{0,0,1,0,1,0},{0,0,0,1,1,0},{0,0,0,0,1,0},{1,0,0,0,1,0},{0,0,0,1,1,0}};


Set::write: Tag List in {u,f,c,s,b}[x_] is Protected.

Set::write: Tag List in {1,2,3,4,5,6}[x_] is Protected.

Set::write: Tag List in {u,f,c,s,b}[y_] is Protected.

Set::write: Tag List in {1,2,3,4,5,6}[y_] is Protected.

If you click on the "..." next to the error message, and then select "Show Stack Trace" you will see the following (I clicked on the first message):

Message[Set::write,List,{u,f,c,s,b}[x_]]

The entries are displayed in reverse order, with the Message shown first. Here is a recap:

1. Mathematica evaluated the RHS of SigmaD[x_] = Position[SigmaD, x]; and discovered that x was not a member of the list {u,f,c,s,b}. So, the Position call evaluated to an empty list {}.

2. Now, Mathematica tries to evaluate SigmaD[x_] = {}. Of course, SigmaD equals {u,f,c,s,b} and so a message is generated

3. The message says that SigmaD[x_] evaluated to {u, f, c, s, b}[x_] but this is not valid syntax for the LHS of a Set statement.

Assuming you just want the code to work, don't use the name of a list as a name of a new function. I have changed the names of the position finding functions by suffixing them with a P. Also use SetDelayed for function definition here.

Clear[x, f, u, s, c, b];
SigmaD = SigmaC = {u, f, c, s, b};
PiD = PiC = {1, 2, 3, 4, 5, 6};