The |
New functionality can be added to
In both cases, the user has to write a “glue definition routine”, which declares the functionality provided by the glue, using a standard API. This routine can then be called from some central point (at startup or when loading a dynamically linked glue library) in order to make the glue functionality available from within the current evaluator.
The
Let us first consider writing a glue definition routine for a simple
C++ class named color. Assume also that the color
class comes with two routines which we wish to export to
color named_color (const string& name);
color invert_color (const color& c);
Then the glue definition routine for color typically looks as follows:
void
define_color () {
define_type<color> ("Color");
define ("named_color", named_color);
define ("invert", invert_color);
}
The define_type instruction exports the type color to
Whenever a new type is exported to
nat hash (const color& c);
generic flatten (const color& c);
bool operator == (const color& c1, const color& c2);
bool operator != (const color& c1, const color& c2);
The first routine computes a hash value for c, where
nat stands for unsigned int. The
second flattening routine converts c to a generic
expression; this routine will be used when printing a color. The last
two routines implement equality testing. Notice that there are several
types of equality in
bool hard_eq (const color& c1, const color& c2);
bool hard_neq (const color& c1, const color& c2);
bool eq (const color& c1, const color& c2);
bool neq (const color& c1, const color& c2);
The routine hard_eq is a fast test whether c1
and c2 are represented in the same way in memory. In
the case of pointer objects like vectors, one typically tests whether
the pointers match; in particular, two identical vectors which are
stored at different locations would not be “hard equal”.
The routine eq tests whether c1 and
c2 are syntactically equal. Typically, the rational
number might be equal to the integer
, without being syntactically equal.
The main routines for defining new types and routines are specified in glue.hpp. Here follows a short description:
void define_type<C> (const generic& name)
This routine declares a new C++ type C and exports as name. Whenever a new type is defined, a few other derived types are defined as well (see section ? below).
void define_constant<C> (const generic& name, const C& x)
This routine exports the constant C x as name.
void define_constructor<C> (generic (*fun) (const C&))
This routine exports a constructor fun for instances of type C.
void define<D> (const generic& name, D (*fun) ())
void define<D,S1> (const generic& name, D (*fun) (const S1&)) void define<D,S1,S2> (const generic& name, D (*fun) (const S1&, const S2&))
...
This routine exports a function fun as name.
void define_converter<D,S> (const generic& name, D (*fun) (const S&), nat pen)
This routine exports a type conversion routine fun: S->D (which defaults to the default converter from S to D) with a given penalty pen. In cases of ambiguity, conversion chains with the least penalty are preferred. The name of the converter should be one among "convert", "upgrade" and "downgrade", depending on the nature and transitivity properties of the converter. Upgraders are used for automatic constructors (such as integer->rational) and downgraders for type inheritance (such as circle->shape). Plain converters cannot be composed with other converters.
void define_primitive<Cond> (const generic& name, generic (*f) (const generic&))
This routine exports a language primitive f. The argument to the primitive is a tuple which is not evaluated before f is called. The primitive should take care of the possible evaluation of its arguments itself.
Whenever a new type C is defined by the user, a few
other related types are added automatically. More precisely,
alias<C>
This type is used for aliases to instances of type C
(see alias.hpp). The alias<C>
type plays a similar role as the C++ reference type C&,
but there are some subtle differences. In
v: Vector Generic := vector (1, 2, 3, 4, 5)
Mmx]
x: Alias Generic == v[4];
Mmx]
v := vector (6, 7, 8, 9, 10)
Mmx]
x
Mmx]
In
tuple<C>
This type stands for a tuple of elements of type C. By defining a vector constructor using
define<Cond> ("vector", make_vector<C>);
where
template<typename C> vector<C> make_vector (const tuple<C>& t);
this will allow you to enter vectors in the
vector(a,b,c,d,e,f)
alias<tuple<C> >
This type is also added, for coherence.
template<typename C,typename T> C
table<C,T>::operator [] (const T& key) const;
template<typename C,typename T> C&
table<C,T>::operator [] (const T& key);
The overloading will allow for both read-access and write-access
using the same syntax. However, it sometimes happens that you have a
(non constant) table which corresponds to a global environment. In
that case, any access to this table will be a write-access
independently if you really perform some modifications of the table.
This may lead to subtle errors if you really wanted to perform a
read-access, since a write-access might actually modify the table
(allocating a non existant key-value pair, for instance). This
subtlety does not occur for the
The routine define_converter allow the user to define automatic converters between different types. This facility is quite powerful, but has to be used with care: since automatic converters are applied in a very systematic way, they may even be applied in sitations which the user did not foresee.
First of all, the user has to carefully select between the three
different types of converters: upgraders, downgraders and plain
converters. These types differ in the way they may be composed: plain
converters are neither left nor right composable, upgraders are left
composable, and downgraders are right composable. Given a left
composable converter B->C and an arbitrary
converter A->B,
Typically, upgraders correspond to type constructors, such as Integer -> Rational or C -> Matrix(C). Similarly, downgraders correspond to type inheritance, such as Rectangle -> Shape, Sum_series(C) -> Series(C), etc. Plain converters are often used for converters which may involve some loss of data, such as Integer->Double.
A second important property of a converter is the correponding penalty: when several conversion schemes can be used in order to apply a function to some arguments, the scheme with the lowest penalty will be preferred (here we notice that the penalty of a conversion (A,B)->(C,D) is the maximum of the penalties of the conversions A->C and B->D). Among all possible schemes with the lowest penalty, the most specialized function will be chosen (a type T is strictly more specialized than U if there exists a converter T->U but no converter U->T). If no conversion schemes can be found to apply the function, then it will be applied symbolically.
Currently, the following penalties are provided:
Some common sources of bugs when using overloading and automatic conversions are the following:
You specified an upgrader Generic->Polynomial(Generic), but addition on generic polynomials can not be applied so as to add one to a polynomial. The point here is that the penalty of the conversion Generic->Polynomial(Generic) should be the maximal penalty PENALTY_PROMOTE_GENERIC, which is larger than the penalty PENALTY_FALL_BACK for the symbolic addition of expressions. The solution to this problem is to implement the following two specialized additions:
+: (Polynomial (Generic), Generic) -> Polynomial (Generic) +: (Generic, Polynomial (Generic)) -> Polynomial (Generic)
Actually, these operations may be useful for other coefficient types than Generic, since they can usually be implemented in a particularly efficient way.
A package with a collection of types, routines and glue definition
routines should be compiled into a dynamic library, which can then be
loaded on the fly into the interpreter. Names of
In the subdirectories examples/fibonacci and examples/gaussian, you may find two simple examples on how
to glue new code to
Assume that we want to add a routine fibonacci for computing Fibonacci numbers to the glue. The file fibonnacci.cpp with the routine fibonacci and the corresponding glue definition routine would typically look at follows:
#include "glue.hpp"
using namespace mmx;
int
fibonacci (const int& n) {
if (n <= 1) return 1;
return fibonacci (n-1) + fibonacci (n-2);
}
void
define_standard_fibonacci () {
define<always> ("fibonacci", fibonacci);
}
void (*define_fibonacci) () = define_standard_fibonacci;
The very last line is added to prevent name mangling of define_fibonacci. We may now compile the file fibonnacci.cpp into a shared library libmmxfibonacci.so:
g++ --shared ‘basix-config --cppflags --libs‘
fibonacci.cpp -o libmmxfibonacci.so
After putting the directory which contains libmmxfibonacci.so
in your LD_LIBRARY_PATH, you may now use the library
from within
use "fibonacci"
Mmx]
fibonacci(37)
Mmx]
To be completed.
It is possible to catch exceptions occurring in glued C++ routines
within the
In fact, it is better not to directly raise exceptions by yourself,
but rather use the convenience macros defined in basix.hpp.
Indeed,
The two above types of exceptions both come with their corresponding macros. The following macros should be used for raising exceptions:
ERROR(message)
Raises the error message message.
ASSERT(condition,message)
Raises the error message message if the condition is not satisfied.
VERIFY(condition,message)
When compiling using –enable-verify, this macro raises the error message message if the condition is not satisfied.
Still to be written and documented.