All Tutorials Tutorial

The Magma Language & Data Structures

Handbook survey

Every Magma computation, no matter the area of mathematics, is built on the same foundation: a small imperative language for writing statements and functions, an environment that controls how sessions behave, and a handful of general-purpose data structures for collecting and organizing objects. This card is a bird's-eye survey of that foundation — "THE MAGMA LANGUAGE" and "SETS, SEQUENCES, AND MAPPINGS" parts of the Handbook — so that when you meet a [ ... ], a { ... }, or a map< ... > later, you already know roughly what family it belongs to and why it exists.

The Language Core: Statements, Functions, Semantics

At its heart Magma is an imperative, call-by-value, statically scoped, but dynamically typed language: identifiers don't have a fixed type, values do. The statement-and-expression layer covers the basics — identifiers, assignments, control statements (if, while, for), and how expressions are parsed and evaluated. Built on top of this, the functions, procedures, and packages layer describes the two flavors of callable code: functions (function ... end function; or the compact func< x | expr > form) which return a value, and procedures, which don't but may modify arguments passed by reference. This layer also covers packages for grouping user code into loadable files, and intrinsics, user-defined functions that behave like Magma's own built-ins. A separate semantics chapter goes deeper, pinning down how expressions are evaluated and scoped, and the distinction between a function expression (the textual function ... end function) and a function value (the run-time object it denotes).

> f := function(x)
>   return x^2 + 1;
> end function;
> f(3);
10

Controlling the Session: Environment, I/O, Parallelism

Several chapters govern how a Magma session behaves rather than how you compute. The environment and options chapter covers command-line flags, startup files, and the Set... family of procedures that toggle global behavior (verbosity, memory limits, output formatting), plus history and line-editing. Input and output covers character and binary strings, the various forms of print, the IO type hierarchy for files and pipes, and system calls for invoking external programs. The parallelism chapter describes exploiting multiple cores or machines — both built-in parallelization of key kernel algorithms (e.g. matrix multiplication over finite fields) and a manager/worker framework for parallelizing your own code.

> SetVerbose("User1", 1);
> printf "n = %o\n", 42;

Diagnostics: Profiler and Debugger

Two small but practical chapters round out the tooling story. The profiler records timing information for every function, procedure, map, and intrinsic call once switched on with SetProfile(true), letting you build a call graph and find real bottlenecks instead of guessing. The debugger is a prototype GDB-style command-line debugger: when SetDebugOnError(true) is set, an error drops you into a session where you can inspect the call stack (backtrace) and print local variables at the point of failure.

Aggregates: The Common Vocabulary

The "Aggregates" part of the Handbook introduces four general-purpose container types — sets, sequences, tuples, and lists — plus associative arrays, coproducts, and records, all of which exist so that you rarely need to invent your own bookkeeping structures. Sets collect objects from a common universe with no ordering and no repetition; the key operation is membership testing. Magma distinguishes enumerated sets (all elements stored, possibly built from an arithmetic progression), indexed sets (a set with a numbering, supporting positional access), and multisets (sets that track multiplicity via the ^^ operator). Sequences also draw from a common universe, but ordering — and access by position — is the point, and repeated elements are allowed; Magma distinguishes finite enumerated sequences from possibly-infinite formal sequences defined by a predicate.

> S := { x^2 : x in [1..5] };
> Q := [ x^2 : x in [1..5] | IsOdd(x) ];
> S; Q;
{ 1, 4, 9, 16, 25 }
[ 1, 9, 25 ]

Tuples and Cartesian products are the odd one out: a tuple's components can come from different structures, its parent Cartesian product (built with car< R1, ..., Rk >) is fixed once and for all, and every component must be defined — unlike a sequence, which can grow, shrink, and leave gaps. Lists, by contrast, drop nearly all restrictions: elements need no common parent, and lists exist purely as a lightweight bag for temporarily gathering assorted objects — convenience over structure.

Keyed and Tagged Data: Associative Arrays, Coproducts, Records

Three more aggregate-like types handle data that isn't naturally a flat collection. Associative arrays (AssociativeArray(), category Assoc) generalize sequences by allowing arbitrary keys instead of just integer positions — A[x] := y associates a value to any key x coercible into the array's index universe, and IsDefined(A, x) tests whether a key is currently set. Coproducts (cop< S1, ..., Sk >) let you build a single structure that can hold objects from several entirely different parents at once, remembering each element's true parent internally and restoring it on retrieval — useful when you need one container for genuinely heterogeneous data. Records, built from a recformat< ... > template, are like tuples in that fields can hold different kinds of values, but fields are addressed by name rather than position and may be left unassigned or deleted at any time; because of this flexibility, records cannot be compared with eq.

> A := AssociativeArray();
> A["cat"] := 4; A["spider"] := 8;
> A["cat"];
4

Mappings

Finally, the mappings chapter ties the whole picture together: a Map in Magma is a genuine first-class object representing a function f: A -> B, distinct from a Magma function value. The three constructors — map< A -> B | rule >, hom< A -> B | images >, and pmap< A -> B | rule > for partial maps — cover general mappings, algebraic homomorphisms, and partial functions respectively. Once built, a map supports forward evaluation with @ and, when invertible or partially invertible, preimage computation with @@ — the same mechanism you've already seen used for group homomorphisms.

Why This Matters

None of these chapters teach you new mathematics — they teach you the vocabulary every other chapter of the Handbook assumes you already have. When a later chapter says a function "returns a sequence of subgroups" or "takes an associative array of options," it is relying on the concepts surveyed here. Knowing which container fits which job — set for membership, sequence for order, tuple for fixed heterogeneous data, associative array for keyed lookup, record for named optional fields — is often the difference between fighting Magma's type system and using it fluently.

Quiz