[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
Assignment

Assignment

Subsections

Identifiers

Identifiers (names for user variables, functions etc.) must begin with a letter, and this letter may be followed by any combination of letters or digits, provided that the name is not a reserved word (see the Appendix for a list of reserved words). In this definition the underscore _ is treated as a letter; but note that a single underscore is a reserved word. Identifier names are case-sensitive, that is, they are distinguished from one another by lower and upper case.

Intrinsic Magma functions usually have names beginning with capital letters (current exceptions are pCore, pQuotient and the like, where the p indicates a prime). Note that these identifiers are not reserved words, that is, one may use names of intrinsic functions for variables.

Assignment and Deletion

Here, we describe the basic forms of assignment to and deletion of variables.

x := expression;
This is the basic form of assignment of a single value to a single variable. Here x may be any identifier, and expression must be a legal Magma expression returning a value.

Example Lang_Identifiers (H1E4)

> x := 13;
> y := x^2-2;
> print x, y;
13 167
Intrinsic function names are identifiers just like the x and y above. Therefore it is possible to reassign them to your own variable.

> f := PreviousPrime;
> print f(y);
163
In fact, the same can also be done with the infix operators, except that it is necessary to enclose their names in quotes. Thus it is possible to define your own function Plus to be the function taking the arguments of the intrinsic + operator.

> Plus := '+';
> print Plus(1/2, 2);
5/2
Note that redefining the infix operator will not change the corresponding mutation assignment operator (in this case +:=).
x_1, x_2, ..., x_n := expression;
Assignment of n >= 1 values, returned by the expression on the right hand side. Here the x_i are identifiers, and the right hand side expression must return m >= n values; the first n of these will be assigned to x_1, x_2, ..., x_n respectively.
_ := expression;
Ignore the value(s) returned by the expression on the right hand side.

Example Lang_MultipleReturns (H1E5)

The extended greatest common divisor function Xgcd returns 3 values: the gcd d of the arguments m and n, as well as multipliers x and y such that d=xm + yn. If one is only interested in the gcd of the integers m=12 and n=15, say, one could use:

> d := Xgcd(12, 15);
To obtain the multipliers as well, type

> d, x, y := Xgcd(12, 15);
while the following offers ways to retrieve two of the three return values.

> d, x := Xgcd(12, 15);
> d, _, y := Xgcd(12, 15);
> _, x, y := Xgcd(12, 15);

x[expr_1][expr_2]...[expr_n] := expression;
x[expr_1,expr_2,...,expr_n] := expression;
If the argument on the left hand side allows indexing at least n levels deep, and if this indexing can be used to modify the argument, this offers two equivalent ways of accessing and modifying the entry indicated by the expressions expr_i. The most important case is that of (nested) sequences.

Example Lang_Indexing (H1E6)

Left hand side indexing can be used (as is explained in more detail in the chapter on sequences) to modify existing entries.

> s := [ [1], [1, 2], [1, 2, 3] ];
> print s;
[
       [ 1 ],
       [ 1, 2 ],
       [ 1, 2, 3 ]
]
> s[2, 2] := -1;
> print s;
[
       [ 1 ],
       [ 1, -1 ],
       [ 1, 2, 3 ]
]

E<x_1, x_2, ...x_n> := expression;
If the right hand side expression returns a structure that allows naming of `generators', such as finitely generated groups or algebras, polynomial rings, this assigns the first n names to the variables x_1, x_2, ..., x_n. Naming of generators usually has two aspects; firstly, the strings x_1, x_2, ...x_n are used for printing of the generators, and secondly, to the identifiers x_1, x_2, ...x_n are assigned the values of the generators. Thus, except for this side effect regarding printing, the above assignment is equivalent to the n + 1 assignments:

E := expression;
x_1 := E.1; x_2 := E.2; ... x_n := E.n;
AssignNames(~S, [s_1, ... s_n] ) : Struct, [ MonStgElt ] ->
Procedure. If S is a structure that allows naming of `generators' (see the Appendix for a complete list), this assigns the names specified by the strings to these generators. The number of generators has to match the length of the sequence. This will result in the creation of a new structure.

Example Lang_GeneratorNaming (H1E7)

> G<a, b> := Group<a, b | a^2 = b^3 = a^b*b^2>;
> w := a * b;
> print w;
a * b
> AssignNames( G, ["c", "d"]);
> print G;
Finitely presented group G on 2 generators
Relations
    c^2 = d^-1 * c * d^3
    d^3 = d^-1 * c * d^3
> print w;
a * b
> print Parent(w);
Finitely presented group on 2 generators
Relations
    a^2 = b^-1 * a * b^3
    b^3 = b^-1 * a * b^3
> print G eq Parent(w);
true

x o:= expression;
This is the mutation assignment: the expression is evaluated and the operator o is applied on the result and the current value of x, and assigned to x again. So the result is equivalent to (but an optimized version of): x := x o expression;. The operator may be any of the operations join, meet, diff, sdiff, cat, *, +, -, /, ^, div, mod, and, or, xor provided that the operation is legal on its arguments of course.

Example Lang_MutationAssignment (H1E8)

A simple example of two different mutation assignments to produce the set consisting of the first 10 powers of 2.

> x := 1;
> S := { };
> for i := 1 to 10 do
>    S join:= { x };
>    x *:= 2;
> end for;
> print S;
{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }

delete x : Var -> Nil
(Statement.) Delete the current value of the identifier x. The memory occupied is freed, unless other variables still refer to it. If x is the name of an intrinsic Magma function that has been reassigned to, the identifier will after deletion again refer to that intrinsic function. Intrinsic functions cannot be deleted.
assigned x : Var -> BoolElt
Returns true if the `local' x has a current value, that is, if x has been assigned to and its value has not been deleted after that; otherwise it returns false. (This will return false for intrinsic function names, since they are not `local'.)
[Next] [Prev] [Right] [Left] [Up] [Index] [Root]