The Mathemagix syntax

This document describes the syntax of the Mathemagix interpreter, provided by the executable mmx-light, whose usage can be obtained with the –help option. Input is read on the standard input by default. If Readline has been enabled then newline insertion in interactive mode is possible by typing Meta-Return. Let us recall here that a special editing mode for the Emacs editor is available for installation from mmx/mmxlight/emacs/mmx-mode.el.

1.Variables and constants

1.1.Identifiers

Identifiers are formed using letters, digits and the special characters _ and ?. Identifiers must start with a letter or _. Identifiers match the [a-zA-Z_]+[a-zA-Z0-9_?]*. Examples of identifiers are x, y2, my_var and test?.

Some special predefined constants are:

true
The boolean constant true.
false
The boolean constant false.
mmout
The standard output stream.
mmerr
The standard error stream.

Notice that streams can be used with the operators << as in C++:

1.2.Literals

In addition to identifiers, Mathemagix provides three types of literal constants:

String literals

Strings can be braced into double quotes " ... ". Inside such a string a double quote must be blackslashed. In order to avoid blackslashing one can use the stronger string delimiters /" ... "/.

Integer literals
An integer literal is a sequence of digits, possible preceded by a minus sign. It matches the regular expression [-]?[0-9]+. Examples: 123456789123456789, -123.
Floating literals

A floating literal is a sequence of digits with a decimal point inside and an optional exponent. It matches the regular expression [-]?[0-9]+[.][0-9]+[[eE][-]?[0-9]+]?. Some examples:

Note that 0. is not permited, one must write 0.0.

2.Operators

Here we list the operators in increasing priority order, together with their internal names:

== DEFINE
:= ASSIGN
==> DEFINE_MACRO
:=> ASSIGN_MACRO
+= PLUS_ASSIGN
-= MINUS_ASSIGN
*= TIMES_ASSIGN
/= OVER_ASSIGN
<< LEFT_FLUX
<<* LEFT_FLUX_VAR
<<% LEFT_FLUX_STR
>> RIGHT_FLUX
=> IMPLIES
<=> EQUIVALENT
or SEQOR
OR
xor XOR
and SEQAND
AND
= EQUAL
< LESS
<= LEQ
> GREATER
>= GEQ
!= NOT_EQUAL
!< NOT_LESS
!<= NOT_LEQ
!> NOT_GREATER
!>= NOT_GEQ
-> INTO
:-> MAPSTO
.. RANGE
to RANGEEQ
+ PLUS
- MINUS
@+ OPLUS
@- OMINUS
* TIMES
/ OVER
@* OTIMES
@/ OOVER
div DIV
mod MOD
@ COMPOSE
! NOT infix unitary
- MINUS infix unitary
@- OMINUS infix unitary
++ INC infix unitary
DEC infix unitary
# SIZE infix unitary
^ POWER
++ INC postfix unitary
DEC postfix unitary
! NOT postfix unitary
' QUOTE postfix unitary
BACKQUOTE postfix unitary
~ TILDA postfix unitary
. ACCESS

Whenever writing a complex expression you should carefully consider these priority rules in order to avoid using extra '(...)'. For example the expession

should be better written

2.1.Ranges

a..b means the range [a,b], while a::b stands for the half open range [a,b).

3.Sequences

Tuples are written inside (...). Elements are separated by ,. Row-tuples rows are separated by ;.

Lists are written inside [...].

Automatic constructions are possible through the | resp. || notation:

The default type of lists used in the interpreter is List(Generic).

4.Tables

Tables allow to store the association between keys of one type to values of another type. They are defined by providing a default value. In the following exemple, the default value is 1:

The definition of specific values is done as follows:

The default type of tables used in the interpreter is Table(Generic,Generic).

5.Functions and macros

5.1.Function definitions

Any function definition can be preceded by quantifiers forall(...) or exists(...).

A function definition is done as follows:

Mathemagix also provides a syntax for lambda expressions:

Notice that any of the type specifications can be omitted, in which case the type is assumed to be Generic. Expression macros can be defined by using macro instead of lambda.

When function or macro have only one argument, () can be omited.

Concerning the coding style, notice that we prefer a space before ( and another after :.

5.2.Function Calls

A function or a macro foo is called in the usual way: foo(arg1, arg2,...).

If foo is unitary then () can be omited, but note that foo a b c is equivalent to foo(a(b(c))). Function call is always by value.

6.Control flow

6.1.Sequence of Instructions

Instructions are separated with ;.

6.2.Conditionals

The construction is as follows: if condition then block1 else block2.

condition is any expression that evaluates to a boolean. block1 and block2 are either one instruction or a block of instructions braced into {...}. The else part is optional.

For the sake of readability do not write

but

instead, since {...} are not needed for a block with one instruction only. Notice that, according to the priority rules of the operators listed above, the expression

should be written

for the sake of readability.

6.3.Case separation

6.4.Loops

Loops are constructed as follows: [for E1] [while E2] [until E3] [step E4] do block.

Here [...] means that the expression is optional. block is an instruction or a sequence of instruction delimited by {...}. break exits the loop, and continue goes to the next step of the loop.

7.Comments

Comments starting with // extend to the end of the physical line. Such a comment may appear at the start of a line or following whitespace or code, but not within a string literal. Multi-line comments must be braced into /{ ... }/ and can be nested.

8.Further Rules on the Coding Style

9.Technical Notes

The Mathemagix grammar is specified in the file mmx-parser.ypp of the basix module.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License. If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.