A quick introduction to Mathemagix

This document is a tutorial introduction to the Mathemagix language. The examples can be tested in the interpreter, or in an interactive session through the editor TeXmacs.

1.The interpretor

To start the interpretor, type mmx-light. The instructions that you enter will be evaluated once the key [] is pressed. Several instructions are separated by ;. If an instruction ends by a ;, its evaluation is not printed. To exit the session, type quit.

Mmx] 

1+2

If you use the interpretor in a textual context, several shortcuts can be very usefull such as

  • [Ctr-A], [Ctr-E] to go at the beginning or end of the instruction line,
  • , for the previous and next instruction line,
  • [tab] for name completion.

The default mode is a quiet mode. It does not print any additional information. If you want to turn the type mode on or off, use the function type_mode.

Mmx] 

type_mode()

: Boolean

Mmx] 

23432*2

: Int

Mmx] 

type_mode()

Mmx] 

a :=1

The other modes are

  • time_mode: print the time needed to evaluate an instruction line. when it is bigger than 1 ms;
  • debug_mode: print debug information when an error occurs;
  • quiet_mode: print nothing.

Mmx] 

2.Variables and constants

Identifiers of variables are formed using letters, digits and the special characters _ and ?. Identifiers must start with a letter or _. Identifiers should match the regular expression [a-zA-Z_]+[a-zA-Z0-9_?]*.

To assign a value to a variable, we use the operator := :

Mmx] 

a1 := 1

Constant values can be assigned by the operator ==, and cannot be modified hereafter:

Mmx] 

a1_? == 2; a1_?

Mmx] 

The operator == will be used for instance to define functions which definition should be fixed (see section ?) or constant values.

3.Predefined types

In this section, we describe the type, which exists by default in the interpreter. For the other types provided by the extension packages, see their documentation.

3.1.Generic

The default type of an object is Generic and the corresponding variable type is Alias Generic:

Mmx] 

a := x

Mmx] 

type a

Mmx] 

3.2.Boolean

The usual boolean constant are true and false. The equality and inequality tests are = and !=.

Mmx] 

a = a

Mmx] 

a != a

To build boolean expression, we use the operators and, or and the negation operator ! :

Mmx] 

a = b and a != c

Mmx] 

a = b or a != c

Mmx] 

!( a = b or a != c)

Mmx] 

3.3.Strings

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 /" ... "/.

Mmx] 

s1 := "This is a string"

Mmx] 

s2 : String := "Print \"foo\" "

Not that in this definition, we specify that the variable s2 is of type String.

Mmx] 

s2 := /"Print "foo" "/

The concatenation of strings can be done by the operator ><

Mmx] 

s3 := s2 ><" and \"fii\" "

It is also possible to use the in place concatenation operator << on a variable:

Mmx] 

s2 <</" and "fuu" "/

The length of a string is given by the operator #

Mmx] 

#s1

Mmx] 

contains? (s2, "Print")

Mmx] 

replace(s2, "fuu", "haha")

Mmx] 

3.4.Integers

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

Mmx] 

a := 2

The usual arithmetic operators +, -, * are available:

Mmx] 

a+3; a-5; a*a

as well as the inplace operators +=, -=, *= :

Mmx] 

a += 1; a *= 2; a -= 3

Mmx] 

5 div 2

Mmx] 

5 mod 2

The default type for integers is Int. It corresponds to machine type int.

Mmx] 

type(1)

In order to have access to the extended arithmetic, one can use the package numerix. In this case, the integer literals will yield numbers of type Integer, based on gmp integers.

Mmx] 

use "numerix"; type(1)

For this type of extended integers, the same operations are available and there is no size restriction:

Mmx] 

2343312351435131235124354123*2116514651624164612456124

Mmx] 

2234000000000000000000 div 2234

Mmx] 

2234000000000000000002 mod 2234

Machine precision integers can still be used by specifying explicitly the type, but with the caution due to their limited size:

Mmx] 

i : Int := 12155461246541265421

Mmx] 

1233444343433434345 :> Int

Notice the conversion operator :>, used to cast a integer litteral into a machine integer Int.

Mmx] 

3.5.Floating point numbers

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]+]?.

Mmx] 

z :=0.0

Mmx] 

w := -3.14159

Mmx] 

x := 1.11e07

Mmx] 

type(1.1)

Mmx] 

a := 1; a/=2

Mmx] 

Note that 0. is not permitted, one must write 0.0;

4.Sequences

4.1.Vectors

Vectors are sequence of elements, stored in an array and with a direct access through their index. Their type is parametrized by the type of the elements. The default type is Vector Generic.

Mmx] 

v := [1,2,3]

The indices start from 0 :

Mmx] 

v[0]+v[1]+v[2]

The length of a vector is given by the prefix operator #:

Mmx] 

#v

The concatenation of vectors is performed by the operator ><:

Mmx] 

w := v >< [4,5]

The inplace concatenation of vectors is done by the operator <<:

Mmx] 

v << [1,2]

The classical operations car, cdr, cons on lists are available also on vectors:

Mmx] 

[car(v), cdr(v), cons(3,v)]

Mmx] 

reverse v

Mmx] 

contains?(v,1)

Mmx] 

4.2.Tuples

Tuples are written inside (...). Elements are separated by ,.

Mmx] 

v := (1,2,3)

Note the associativity of tuples:

Mmx] 

(1,(2,3))

Mmx] 

(v,((v)))

Mmx] 

4.3.Ranges

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

Mmx] 

(1..4)

Mmx] 

1 to 4

Note that 1..4 or 1 to 4 are not tuples but iterators, that can be used to produce sequences:

Mmx] 

[1..6]

Mmx] 

[i*i | i in 1 to 10]

Mmx] 

[i*j | i in 1 to 10 || j in 1 to 10]

Mmx] 

5.Tables

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

Mmx] 

t := table(1);

Mmx] 

t[1] := -3; t[34] := 2

Mmx] 

t[0]

Mmx] 

contains? (t,2)

Mmx] 

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

6.Functions

A function definition is done as follows:

name (arg1: type1, arg2: type2, ...): returned_type ==  block

The block is either one instruction or several instructions in between { }. Mathemagix also provides a syntax for lambda expressions:

lambda (arg1: type1, arg2: type2, ...): returned_type do ...

Notice that any of the type specifications can be omitted, in which case the type is assumed to be Generic.

Mmx] 

f (x: String) :String == return x><x

Mmx] 

f("ab")

In the following definition, no type are specified so that the input and output type Generic are assumed:

Mmx] 

f (x) == { a := 2*x; return a }

When a function has only one argument, the parenthesis () can be omitted.

Mmx] 

f 3.1

The same name f is used for the definition of two functions of types lambda: Int Int, lambda: Generic Generic. The polymorphism of f is resolved by the type of its argument:

Mmx] 

f "xx "

Mmx] 

f 2.1

The definition of a postfix function is preceded by the keyword postfix. The name of the function

should start with a .:

Mmx] 

postfix .sq(i : Int) : Int == { return i*i }

Mmx] 

3.sq

Mmx] 

operator .m (a: Int , b: Int) : Double == { return 1.0*a*b }

Mmx] 

3 .m 3

Mmx] 

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

forall (R) gcd (p: Polynomial(R), q: Polynomial(R)): Polynomial(R) == {...}

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

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

7.Macros

A macro corresponds to a syntactic definition. No type is needed:

Mmx] 

square x ==> x*x

Mmx] 

square 2.1

Mmx] 

square 2

Mmx] 

disc(a,b,c) ==> b*b - 4*a*c

Mmx] 

disc (1,b,3*x)

Macros can be usefull to define short and convenient names for types for instance:

Mmx] 

VG ==> Vector Generic

Mmx] 

f(a : VG) == car a

Mmx] 

f ([1,2])

Mmx] 

8.Control flow

8.1.Conditionals

The condition construction is as follows:

if condition then block1 [else block2]

where condition is any expression that evaluates to a boolean. block1 and block2 are either one instruction or a block of instructions braced into {...}. The [...] (here the else part) means that the expression is optional.

Mmx] 

if true then 1;

Mmx] 

if a = b then 1 else 2;

Mmx] 

if i = 0 then { x := 1; y := 2} else { z := 3; mmerr << "error" };

Mmx] 

8.2.Loops

Loops are constructed as follows:

[for E1] [while E2] [until E3] [step E4] do block

The block is an instruction or a sequence of instructions delimited by {...}. break exits the loop, and continue goes to the next step of the loop.

Mmx] 

for i in 1..3 do mmout << i << " ";

1 2

Mmx] 

i := 0; while i < 5 do { mmout << i << " "; i := i + 1 }

0 1 2 3 4

Mmx] 

// do mmout << "no end "; // This loop has no end

9.Output streams

As in C++, there is an output stream, which is called mmout. It can be used with the operator << to print strings:

Mmx] 

i := 3; mmout<<"The square of "<<i<<" is "<<i*i<<"\n";

The square of 3 is 9

The error stream, used to catch error message, is mmerr:

Mmx] 

mmerr <<"An error message";

Mmx] 

10.Handling file

Here are some usefull command to read and save contents in files. The command to read a Mathemagix file and to evaluate it is include:

Mmx] 

include "example.mmx"

The command to save a String in a file is save:

Mmx] 

save ("tmp.txt", "A string is stored in the file \n in two lines");

If now, you want to recover the content of a file, you can use the command load:

Mmx] 

load "tmp.txt"

The files in a directory can be recovered by the command load_directory. The result is a vector of strings, which corresponds to the name of a file or a subdirectory:

Mmx] 

load_directory "."

To check if a file or a directory exists, one can use the predicate readable?

Mmx] 

readable? "../example.mmx"

The function use allows to load and use the types and functions exported in an external dynamic library:

Mmx] 

use "numerix"

If this example, the library libmmxnumerix.so should be in the loading path.

Mmx] 

11.Environment

Several functions are available to interact with the environment. To get the value of a variable defined in the environment, one can use get_env:

Mmx] 

get_env "PWD"

Mmx] 

set_env ("DISPLAY", "viviane:0")

To run a command in this environement, one can use the function system:

Mmx] 

system("ls")

example.mmx

index.en.tm quick_start.en.tm shell.en.tm syntax.en.tm tmp.txt

Mmx] 

12.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.

x := 1; // Assign 1 to x.

y := 2;
/{ This
   is multi-line
   comment about y.
}/
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.