Builtin data types

In this section, we describe the basic types of Mmx-light. For the other types, provided by the extension packages, see their respective documentation.

1.Generic

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

Mmx] 

a := x

Mmx] 

type a

2.Boolean

The usual boolean constants are true and false. The equality and inequality tests are = and !=. To build boolean expressions, we use the operators and, or and the negation operator !.

Mmx] 

a = a

Mmx] 

a != a

Mmx] 

a = b and a != c

Mmx] 

a = b or a != c

Mmx] 

!( a = b or a != c)

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"

"This is a string"

Mmx] 

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

"Print "foo" "

Mmx] 

s2 := /" Print "foo" "/

" Print "foo" "

Mmx] 

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

" Print "foo" and "fii" "

Mmx] 

s2 << /" and "fuu" "/

" Print "foo" and "fuu" "

Mmx] 

#s1

Mmx] 

search_forwards (s2, "Print", 0)

Mmx] 

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

" Print "foo" and "haha" "

4.Machine integers

An integer literal is a sequence of digits, possibly preceded by a minus sign. It matches the regular expression [-]?[0-9]+. Examples: 123456789123456789, -123. The default type for integers is Int. It corresponds to machine type int. The usual arithmetic operators +, -, * are available, as well as the inplace operators +=, -=, *=.

Mmx] 

a := 2

Mmx] 

a+3; a-5; a*a

Mmx] 

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

Mmx] 

5 div 2

Mmx] 

5 rem 2

5.Double

By default, the floating point literals are converted to Double types. 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]+]?. Note that 0. is not permitted, one must write 0.0;

Mmx] 

2.1

Mmx] 

2.1*3

Mmx] 

2.3/2-1

6.Syntactic

The Syntactic type can be used to produce document outputs, represented as lisp-type expressions. It is automatically parsed by TeXmacs to display these outputs.

Mmx] 

type_mode?:=true

:

Mmx] 

'x

:

Mmx] 

'(f (x, y, z))

:

Mmx] 

'(f (x, y, z)) [2]

:

Mmx] 

$document ("Some ", $with ("color", "red", "red"), " text.";

"Pythagoras said ", $math ('(a^2 + b^2 = c^2)), ".")

Some red text.

Pythagoras said .

7.Vectors

Vectors are sequences 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. The indices start from 0. The length of a vector is given by the prefix operator #. The concatenation of vectors is performed by the operator ><. The inplace concatenation of vectors is done by the operator <<. The classical operations car, cdr, cons are available on vectors.

Mmx] 

v := [1,2,3]

:

Mmx] 

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

:

Mmx] 

#v

:

Mmx] 

w := v >< [4,5]

:

Mmx] 

v << [1,2]

:

Mmx] 

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

:

Mmx] 

reverse v

:

Mmx] 

contains?(v,1)

:

8.Tuples

Tuples are written inside (...). Elements are separated by ,, which is associative, so that (1, (2, 3)) is the same as (1, 2, 3).

Mmx] 

v := (1, 2, 3)

Mmx] 

(1, (2, 3))

Mmx] 

v:= [1, 2]; (v, ((v)))

Row-tuples rows are separated by ;, as exemplified with the constructions of matrices (defined in the Algebramix package):

Mmx] 

use "algebramix"

Mmx] 

(1, 2; 3, 4; 5, 6)

:

Mmx] 

[1, 2; 3, 4; 5, 6]

:

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

Mmx] 

( i^2 | i in 1..3 )

:

Mmx] 

[ i * j | i in 1..3, j in 1..4 ]

:

Mmx] 

matrix ( i*j | i in 1..5 || j in 1..5 )

:

9.Iterators

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

Mmx] 

(1 to 4)

:

Mmx] 

1 .. 4

:

Mmx] 

[1..6]

:

Mmx] 

[i*i | i in 1 to 10]

:

10.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. The default type for tables used in the interpreter is Table(Generic,Generic). 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)

11.Ports

There is an output stream, which is called mmout. It can be used with the operator << to print strings:

Mmx] 

type_mode?:= true;

Mmx] 

mmout

:

Mmx] 

mmout << "Hello\n";

Hello

Mmx] 

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

The square of is

Output streams can also be defined from files. Here we write a string into the file toto.txt, and we load the contents of this file into a string:

Mmx] 

output_file_port ("toto.txt") << "Hi there\n";

Mmx] 

load ("toto.txt")

"Hi there\n"
:

12.Handling file

Here are some useful commands to read and save data in files. The command to read a Mathemagix file and to evaluate it is include. The command to save a String in a file is save.

The file names 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.

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

Mmx] 

include "example.mmx"

Mmx] 

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

Mmx] 

load "tmp.txt"

"A string is stored in the file \n in two lines"
:

Mmx] 

load_directory "."

:

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

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

Mmx] 

get_env "PWD"

Mmx] 

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

Mmx] 

system "ls"

emacs_mode.en.tm

how_to.en.tm index.en.tm installation.en.tm quick_start.en.tm shell.en.tm shell_tutorial.en.tm syntax.en.tm tmp.txt toto.txt

:

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.