> <\body> Identifiers are used as names for variables, functions, macros, types and categories. Regular identifiers should match the regular expression >. That is, identifiers should <\itemize> only contain letters, digits and the special characters , and >; and start with a letter or or . In addition, it is customary to use the following guidelines when choosing names: <\itemize> Use lowercase names for variables and functions. For names of types and categories, capitalize the first letter of each word categories ( or ). Capitalize all letters in macro names. Use the suffix for names of predicates. Besides the regular identifiers, allows the programmer to use several types of special identifiers for the names of operators and special objects. First of all, identifiers corresponding to the built-in operators (see section below) are formed by prefixing them by one of the keywords >, >, > and >. For instance: <\mmx-code> postfix ! (n: Int): Int == if n=0 then 1 else n * (n-1)!; Similarly, the instruction <\mmx-code> mmout \\ map (infix *, [ 1, 2, 3 ], [ 4, 5, 6 ]) \\ lf; prints the vector >. The operator <\mmx-code> operator []: (t: Tuple T) -\ Vector T; is used as a shorthand for the constructor of vectors (so that we may write instead of ). Similarly, the operator is used for accessing entries of vectors and matrices. <\remark> TODO: in place operators formed with the keyword . In addition to the builtin operators, any regular identifier can also be turned into a postfix operator . For instance, when defining a class by <\mmx-code> class Point == { \ \ x: Double; \ \ y: Double; \ \ constructor point (x2: Double, y2: Double) == { \ \ \ \ x == x2; \ \ \ \ y == y2; \ \ } } automatically creates two such postfix operators for accessing the fields and : <\mmx-code> postfix .x: Point -\ Double; postfix .y: Point -\ Double; Hence, we may define an addition on points using <\mmx-code> infix + (p: Point, q: Point): Point == \ \ point (p.x + q.x, p.y + q.y); Additional operators similar to and can be defined outside the class <\mmx-code> postfix .length (p: Point): Double == \ \ sqrt (square p.x + square p.y); Given a point , we then write for its length as a vector. The keywords, such as , , can also be turned into identifiers by prefixing them with >. Hence, stands for the keyword . This notation is mainly using during formal manipulations of programs. More generally, any valid string can be turned into an identifier by putting it between quotes and prefixing it by the keyword >. For instance, is equivalent to the regular identifier , is equivalent to the infix operator , and is an identifier which can only be written using the keyword . We finally notice that > is a special identifier which denotes the underlying instance inside aclass method. provides three types of literal constants: string literals, integer literals and floating literals. In addition, there are several important constants, such as and , which are really identifiers from the syntactic point of view. Short string constants are either written inside a pair of double quotes "...">: <\mmx-code> mmout \\ "Hello world" \\ lf; Double quotes and backslashes inside strings should be escaped using backslashes: <\mmx-code> quote_char \ \ \ : String == "\\""; backslash_char: String == "\\\\"; Long string constants which avoid this kind of escaping can be formed using the delimiters /"..."/>>, as in the following example: <\mmx-code> hello_world_example: String == /" include "basix/fundamental.mmx"; mmout \\ "Hello world!" \\ lf; "/ mmout \\ hello_world_example \\ lf; An integer literal is a sequence of digits, possible preceded by a minus sign. It matches the regular expression . Examples are: , . The user should define aroutine <\mmx-code> literal_integer: Literal -\ T; in order to allow literal integers to be interpreted as constants of type . The file of the standard library defines the routine <\mmx-code> literal_integer: Literal -\ Int; which allows literal integers to be interpreted as machine integer constants. Arbitrary precision integers are supported by importing <\mmx-code> literal_integer: Literal -\ Integer; for . A literal floating point constant is a sequence of digits with a decimal point inside, an optional sign and an optionalexponent. It matches the regular expression <\indent> [-]?[0-9]+[.][0-9]+[[eE][-]?[0-9]+]? The user should define aroutine <\mmx-code> literal_floating: Literal -\ T; in order to allow literal floating poiunt numbers to be interpreted as constants of type . In particular, the files and from the standard library define the routines <\mmx-code> literal_floating: Literal -\ Double; \ \ \ // in basix/double.mmx literal_floating: Literal -\ Floating; \ // in numerix/floating.mmx For instance, <\mmx-code> zero : Double \ \ == 0.0; pi \ \ : Double \ \ == -3.14159; funny: Floating == 1.2345679012345678901234567890e2012; Notice that is not permited: one must write . Some constants are encountered so frequently, that it is useful to mention them here, even though they are really identifiers from the syntactic point of view: <\itemize> The boolean constants > and >. The standard input, output and error ports >, > and >. Several special control symbols for formatted output: <\compact> <\itemize> > for linefeed. > for indenting in. > for indenting out. > for a horizontal ruler. Table summarizes all standard operators, together with their binding forces. For instance, the expression <\mmx-code> a*b + c \ d is naturally parsed as +c|)>\d>. The operators can roughly be divided into four groups: <\enumerate> Infix operators such as apply to one argument on the left and one argument on the right. Prefix operators such as negation apply to one argument on the right. Postfix operators such as the factorial apply to one argument on the left. Other special operators, such as for writing vectors . There are also some special postfix operators, such as function application and named access operators such as (see section). Most operators are infix operators, so infix notation is assumed to be the default, unless stated otherwise. In the remainder of this section, we will quickly survey the intended purpose of the various operators. <\big-table> >>>>>>\=>>\=>>>>>>|>|>|>|>|>|>|\=>>|\=>>|>>|>>>|, , , , , , \=>, \=>, >, >>>|>>>|>|>>>|, >>>|\>>\>>\\>>\\>>\*>>\%>>|\>>|\>>|\\>>|\\>>|\*>>|\%>>>|\>, \>, \\>, \\>, \*>, \%>>>|>>=\>>|>>|=\>>>|>, =\>>>|>>>|>|>|>>|, , >>|>>|>|>>|, >>|>>>>>=>>=>>>>>>>=>>=>>>>|>|>>|>>|=>>|=>>|>|>>|>>|=>>|=>>|>|>>|, >, >, =>, =>, , >, >, =>, =>, , >>|>>>>>|>>|>|>>>|>, , >>>|>>>>|>>|>>>|>, >>>|>>>|>|>|>>|, , >>|>>>>|>|>|>|>>|, , , >>|>>>>>>>>>\>>>>|>|>|>|>|>|>|>|>|>|\>>|>|>>|, , , , , , , , , \>, , >>|>>>>>>>>|>|>|>|>|>|>|>|>>|, , , , , , , >>||>|>|>>|>>|>>>>>>>>>|>|>|>|>|>|>|>|>|>>|, , , , , , , , >>|>>|>|>>|, >>>>> <|big-table> Overview of all operators listed by increasing binding force. >>>>>>\=>>\=>>>>>>|>|>|>|>|>|>|\=>>|\=>>|>>|>> The operators and are used for declarations of constants and mutable variables, as described in the . The operator > is used for macro definitions (see the section about ). The operator > is reserved for future use. The operator can always be used for the assignment of mutable variables. The operators , , , , \=> and \=> are not yet exploited in the standard libraries, but there intended use is \Passignment of the left hand expression with the result of the corresponding outplace operator applied to both arguments\Q. For instance, the instruction <\mmx-code> a += b; should considered to be equivalent to the assignment <\mmx-code> a := a + b; <\remark> As a future extension of the compiler, we also intend to support assignment to tuples, in order to assign several mutable variables at once. For instance, <\mmx-code> (a, b) := (b, a) should swap the variables and , and <\mmx-code> (a, b) += (x, y) should respectively increase and with and . >>>|>|>> The special operators > and are used for writing functions directly as expressions. The expressions <\mmx-code> (a_1: T_1, ..., a_n: T_n) :-\ (val: Ret_Type) lambda (a_1: T_1, ..., a_n: T_n): Ret_Type do val can both be used as a notation for the function with arguments , >, of types , >,, which returns the value of type . \>>\>>\\>>\\>>\*>>\%>>|\>>|\>>|\\>>|\\>>|\*>>|\%>> The operators \> and \> are respectively used for sending data to an output port and retrieving data from an input port. The same notation is useful in analogous circumstances, such as appending data to a vector or popping data from a stack. The operators \\> and \\> are used for sending and retrieving data in binary form. This allows for instance for the implementation of efficient communication protocols between different processes on the same or distant computers. The operators \*> and \%> are reserved for future use. <\remark> Sometimes, we also use the operators \> and \> as shift operators. For instance, given a power series in , we might write \ n> as a shorthand for the multiplication of with >. However, it should be noticed that the binding force of \> and \> is not really appropriate for this type of use (a binding force similar to the one of multiplication would be better for this kind of use), so one carefully has to put brackets whereever necessary in this case. In future versions of , this kind of overuse of notations might be discouraged. >>=\>>|>>|=\>>>>>|>|>|>>>|>|>>|> The operators >, =\>, , stand for the standard logical connectors >, >, > and >. The prefix operator stands for logical negation >. These operators are functions which can be redefined by the user, so both arguments are evaluated in case of the logical connectors. also provides the built-in operators and which must take arguments of type . Moreover the second argument of is evaluated only if the first argument evaluates to. Similarly, the second argument of is evaluated only if the first argument evaluates to . <\remark> The operators >, =\>, , and can also be useful for bitwise operations on numbers, even though the binding force is someone inappropriate for this kind of use. One might also want to use as a notation for exterior products, again with an inappropriate binding force. In future versions of , this kind of overuse of notations might be discouraged. >>>>>=>>=>>>>>>>=>>=>>>>|>|>>|>>|=>>|=>>|>|>>|>>|=>>|=>>|>|> The operators , > , >, => and => correspond to the standard mathematical relations , >, >, > and>. When prefixing these relations with , we obtain the operators , >, >, =>, => which correspond to their negations >, >, >, > and >. In computational contexts, mathematical relations often admit a very asymmetric nature: typically, it can be very easy to prove inequality, but very hard to prove equality. It can even happen that we have an algorithm for proving inequality, but no known algorithm for proving equality. This is for instance the case for the class of so called exp-log constants, constructed from the rational numbers using the field operations, exponentiation and logarithm. In contexts where equality testing is hard, it is therefore useful to make a notational distinction between various types of equality, such as proven equality, probable equality, syntactic equality, etc. In , the intention of the notations , >, >, => and => is that they stand for proven relations. On the other hand, the negations , >, >, => and => are intended to be mere shortcuts for their (not necessarily proven) negations. Hence, should always be equivalent to . We are working on a comprehensive set of additional relations for proven negations; they will probably be denoted by , />, />, =/>, =/>. As an additional rule, it is our intention that => ( =>) is always equivalent to the disjunction of > ( >) and . Thus = b> should always be equivalent to the statement b or a = b>. The operator should be read \Pis of type\Q. For instance, stands for \P is of type \Q. The operator occurs inside the - construct (see the section about ). >>>>>|>>|>|>> The operator > can be used to convert an expression of a given type into another, provided that an appropriate converter was defined. More precisely, assume that has type and that we defined a converter D>. Then D> stands for the explicit conversion of into an expression of type . More information about type conversions can be found in the section on and . >>>>|>>|>> The operator > is used as an efficient notation for function types, such as Int>. One typical use case of this notation is when a function is passed as an argument to another function: <\mmx-code> iterate (f: Int -\ Int, n: Int) (k: Int) == \ \ if n = 0 then k else iterate (f, n-1) (f k); The operator > is mainly used as a notation for key-value bindings whenever we explicitly wish to create a table with given entries. For instance: <\mmx-code> basic_colors: Table (String, Color) == \ \ table ("red" ~\ rgb (1, 0, 0), \ \ \ \ \ \ \ \ \ "green" ~\ rgb (0, 1, 0), \ \ \ \ \ \ \ \ \ "blue" ~\ rgb (0, 0, 1), \ \ \ \ \ \ \ \ \ "white" ~\ rgb (1, 1, 1)); or <\mmx-code> forall (T: Type) invert (t: Table (T, T)): Table (T, T) == \ \ table (t[key] ~\ key \| key: T in t); >>>|>|>|> There are three standard kinds of range operators: <\indent> >|>| up to included>>|>|| up to not included>>|>|| down to included>>>>> >>>>|>|>|>|>>>>>>>>>>\>>>>|>|>|>|>|>|>|>|>|>|\>>|>|>>|> The standard arithmetic operations , , , and stand for addition, subtraction, multiplication, division and powering. The -prefixed variants , , , stand for >, >, > and >. Notice that and can either be infix or prefix operators. provides the additional operators , , and for division-related operations in rings which are not necessarily fields. The operator stands for (usually partially defined) exact division. For instance, provides the operation <\mmx-code> infix div: (Integer, Integer): Integer; but is undefined and might raise an error. The operators and stand for quotient and remainder in euclidean domains. Hence, we should always have <\eqnarray*> ||*b+.>>>> The operator stands for modular reduction, so that the return type is usually different from the source types. For instance would typically belong to or >. There are a few other operations with the same binding force as multiplication. The append operator \>, also denoted by >, is typically used for appending strings, vectors and table. For instance \ "b"> yields . The operator is used for functional composition, whereas the operators and are reserved for future use. >>>>>>>>|>|>|>|>|>|>|>|> The standard prefix operators in are (negation >), (increment), (decrement), (unary ), (unary >), (explode), (size), (reserved for future use). In addition, operator application of the form parses in a similar way as when behaves as a prefix operator. For instance, should be parsed as |)>>. >>>>>>>>>|>|>|>|>|>|>|>|>|> The standard postfix operators in are (post increment), (post decrement), (factorial ), (quote or derivative), (unquote), and (reserved for future use). , , , , , , , , In addition, provides the special postfix operators and for which we are allowed to put additional arguments between the brackets. Hence, stands for the traditional notation of function application, whereas is typically used as an accessor for compound data structures. Notice that is parsed as |)>>, whereas > is parsed as |)>>. Using the operator , we may generalize the classical notation for function application to user defined types, such as vectors of functions: <\mmx-code> postfix () (v: Vector (Int -\ Int), x: Int): Vector Int == \ \ [ f x \| f: Int -\ Int in v ]; >>|>|> The reserved special operator is used for building tuples of expressions (of possibly different types), such as . The special operator is used as a notation for explicit vectors, such as , but it might be used for other purposes. > Generators are an elegant way for representing a stream of data of the same type. For instance, the expression of type allows us to write <\mmx-code> for i: Int in 1 to 10 do \ \ mmout \\ i \\ " * " \\ 7 \\ " = " \\ 7*i \\ lf; provides several constructs for forming generators. There are three standard kinds of range operators: <\indent> >>|>| up to included>>|>>|| up to not included>>|>>|| down to included>>>>> Many container types come with aprefix operator > which returns a generator. For instance, given a vector of type , the expression has type . Whenever is an expression such that has type , we are still allowed to use as the -part of the - construct. For instance: <\mmx-code> for i: Int in [ 2, 3, 5, 7, 11, 13, 19 ] do \ \ mmout \\ i*i \\ lf; One other important construct for forming generators is the \Psuch that\Q operator >. Given a generator of type , the expression <\mmx-code> (var: T in g \| predicate? var) stands for the generator of all items in which satisfy the predicate . For instance, consider the following naive implementation of the predicate which checks whether a number is prime <\mmx-code> prime? (n: Int): Boolean == { \ \ for i: Int in 2..n do \ \ \ \ if i rem i = 0 then return false; \ \ return true; } Then we may display the vector of all prime numbers below using <\mmx-code> mmout \\ [ p: Int in 1 to 1000 \| prime? p ] \\ lf; Notice that this vector is constructed from the expression <\mmx-code> (p: Int in 1 to 1000 \| prime? p) of type using the bracket operator . The vertical bar > can also be used as the \Pwhere\Q operator, using the following syntax: <\mmx-code> (expr var \| var: T in g, predicate_1? var, ..., predicate_n? var) Here is again a generator of type , any expressions which involves , and , ..., an arbitrary number of predicates which involve. If has type , then the resulting expression has type . For instance, <\mmx-code> mmout \\ [ i^2 \| i: in 1 to 100 ] \\ lf; displays the vector of all squares of numbers from to , and <\mmx-code> mmout \\ [ i^2 \| i: in 1 to 1000, prime? (4*i + 3) ] displays the square of each number such that is prime. actually supports a generalization of the where construct with multiple generators and predicates at the right-hand side. This generalization is best illustrated with an example: <\mmx-code> mmout \\ [ p^i \| p: Int in 1 to 1000, prime? p, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i: Int in 1 to 10, p^i \ 1000 ] \\ lf; This code will print the unordered vector of all prime powers below . A special where notation > is used for generators which allow to build rows of matrices or similar two dimensional structures. Again, this notation is best illustrated with an example. Assuming that the file was included, the expression <\mmx-code> [i+j \| i: Int in 0 to 9 \|\| j: Int in 0..10] computes the following matrix: <\equation*> |||||||||>>|]> Most containers implement a mapping construct >. This construct is used for applying one or more functions to all entries of one or more containers. Two simple examples for containers with a single parameter are <\mmx-code> mmout \\ map (square, [ 1, 2; 3, 4 ]) \\ lf; mmout \\ map (infix *, [ 1, 2, 3], [4, 5, 6]) \\ lf; These instruction respectively output the matrix |>||>>>>> and the vector >. In the case of containers with more than one type parameter, one usually has to provide one mapping function for every such parameter. Consider for instance the following table: <\mmx-code> t: Table (Int, String) == table (3 ~\ "Hello", 4 ~\ "Hi", 8 ~\ "Bonjour"); Then the instruction <\mmx-code> mmout \\ map (square, prefix #, t) \\ lf; prints the table 5,16\2,64\7|]>>. Syntactically speaking, the construct is an ordinary identifier. For instance, assuming that we defined a container (see the section on ), a unary mapper for this container can be defined as follows: <\mmx-code> forall (R1: Ring, R2: Ring) map (f: R1 -\ R2, z: Complex R1): Complex R2 == \ \ complex (f z.re, f z.im); When providing your own containers, it is actually important to define unary mappers of this kind, because such mappers automatically induce converters between containers of the same kind but with different type parameters. For instance, given a converter from to , the above mapper for complex numbers automatically induces a converter from to . This allows the user to write <\mmx-code> z: Complex Rational == complex (1, 2); In general, such a converter is constructed whenever the user provides a unary mapper which takes one mapping function for each parameter on input together with a single container. <\remark> We notice that the existence of a unary mapper is mandatory if a program both uses thecontainer in an generic and in a specialized way, and if conversions between the generic and specialized versions of the container indeed occur in the program. For instance, some mathematical library might provide a generic function <\mmx-code> forall (R: Ring) conj (z: Complex R): Complex R == complex (z.re, -z.im); Now assume that we a client program which only works with complex numbers of type and which has specialized this type for better performance. In memory, this means that such complex numbers are represented by pairs of double precision numbers rather than pairs of pointers to double precision numbers numbers as would be the case for generic complex numbers. However, the routine from only applies to generic complex numbers, so conversions between the specialized and the generic view are necessary if we want to use this routine in . As soon as the required unary mapper is defined, these conversions are automatic. . If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.> >