Interfacing Mathemagix with C++∗
Total Page:16
File Type:pdf, Size:1020Kb
Interfacing Mathemagix with C++ ∗ Joris van der Hoeven Grégoire Lecerf Laboratoire d’Informatique Laboratoire d’Informatique UMR 7161 CNRS UMR 7161 CNRS Campus de l’École polytechnique Campus de l’École polytechnique 91128 Palaiseau Cedex 91128 Palaiseau Cedex France France Email: [email protected] Email: [email protected] Preliminary version of January 8, 2013 Abstract system would evolve. This has discouraged many of the programmers who did care about the novel programming In this paper, we give a detailed description of the inter- language concepts in these systems. face between the Mathemagix language and C++. In In our opinion, this has led to an unpleasant current particular, we describe the mechanism which allows us to situation in computer algebra: there is a dramatic lack of import a C++ template library (which only permits static a modern, sound and fast general purpose programming tm instantiation) as a fully generic Mathemagix template language. The major systems Mathematica [31] and tm library. Maple [20] are both interpreted, weakly typed, besides being proprietary and expensive. The Sage system [26] Keywords: Mathemagix, C++, generic programming, relies on Python and merely contents itself to glue template library together various existing libraries and other software com- A.M.S. subject classification: 68W30 ponents. The absence of modern languages for computer algebra 1 Introduction is even more critical whenever performance is required. Nowadays, many important computer algebra libraries 1.1 Motivation behind Mathemagix (such as Gmp [10], Mpfr [6], Flint [12], FGb [5], etc.) are directly written in C or C++. Performance issues Until the mid nineties, the development of computer are also important whenever computer algebra is used in algebra systems tended to exploit advances in the area combination with numerical algorithms. We would like to of programming languages, and sometimes even influenced emphasize that high level ideas can be important even the design of new languages. The Formac system [2] for traditionally low level applications. For instance, in was developed shortly after the introduction of Fortran. a suitable high level language it should be easy to operate Symbolic algebra was an important branch of the arti- on SIMD vectors of, say, 256 bit floating point num- ficial intelligence project at Mit during the sixties. During bers. Unfortunately, Mpfr would have to be completely a while, the Macsyma system [21, 23, 25] was the largest redesigned in order to make such a thing possible. program written in Lisp, and motivated the development For these reasons, we have started the design of a new of better Lisp compilers. software, Mathemagix [14, 15], based on a compiled and The Scratchpad system [11, 16] was at the origin of strongly typed language, featuring signatures, dependent yet another interesting family of computer algebra sys- types, and overloading. Mathemagix is intended as a gen- tems, especially after the introduction of domains and eral purpose language, which supports both functional and categories as function values and dependent types in imperative programming styles. Although the design has Modlisp and Scratchpad II [17, 19, 27]. These devel- greatly been influenced by Scratchpad II and its succes- opments were at the forefront of language design and type sors Axiom and Aldor, the type system of Mathemagix theory [9, 22, 24]. Scratchpad later evolved into the contains several novel aspects, as described in [13]. Math- Axiom system [1, 18]. In the A# project [29, 30], later emagix is also a free software, which can be downloaded renamed into Aldor, the language and compiler were from http://www.mathemagix.org. redesigned from scratch and further purified. After this initial period, computer algebra systems 1.2 Interfacing Mathemagix with C++ have been less keen on exploiting new ideas in language design. One important reason is that a good language One major design goal of the Mathemagix compiler is for computer algebra is more important for developers to admit a good compatibility with existing program- than for end users. Indeed, typical end users tend to use ming languages. For the moment, we have focussed on C computer algebra systems as enhanced pocket calculators, and C++. Indeed, on the one hand, in parallel with the and rarely write programs of substantial complexity them- development of the compiler, we have written several high selves. Another reason is specific to the family of systems performance C++ template libraries for various basic that grew out of Scratchpad: after IBM’s decision to mathematical structures (polynomials, matrices, series, no longer support the development, there has been a long etc.). On the other hand, the compiler currently gener- period of uncertainty for developers and users on how the ates C++ code. ∗ . This work has been partly supported by the Digiteo 2009-36HD grant of the Région Ile-de-France. 1 We already stated that Mathemagix was inspired 2 Basic interface principles to C++ by Axiom and Aldor in many respects. Some early work on interfacing with C++ was done in the context 2.1 Preparing imports from C++ of Aldor [4, 7]. There are two major differences between Different programming languages have different conven- C++ and Aldor which are important in this context. tions for compiling programs, organizing projects into On the one hand, Aldor provides support for gen- libraries, and mechanisms for separate compilation. uine generic functional programming: not only functions, C++ is particularly complex, since the language does but also data types can be passed as function arguments. not provide any direct support for the management of big For instance, one may write a routine which takes a ring projects. Instead, this task is delegated to separate con- R and an integer n on input and which returns the ring figuration and Makefile systems, which are responsible for R[ X1 ] [ Xn ] . The language also provides support for the detection and specification of external and internal dependent types: a function may very well take a ring R dependencies, and the determination of the correct com- together with an instance x of R as its arguments or return pilation flags. Although these tasks may be facilitated up value. to a certain extent when using integrated development tm On the other hand, C++ provides support for tem- environments such as Eclipse (trademark of Eclipse tm plates. We may write a routine cube which takes an Foundation, Inc.), Xcode (trademark of Apple Inc.), tm instance x of an arbitrary type R on input and returns or C++ Builder (trademark of Embarcadero Tech- x*x*x. However, and even though there is some work in nologies, Inc.), they usually remain non trivial for projects this direction [8], it is currently not possible to add the of a certain size. requirement that R must be a ring when declaring the tem- Mathemagix uses a different philosophy for managing plate cube. Hence, the correctness of the template body big projects. Roughly speaking, any source file contains x*x*x can only be checked at the moment when the tem- all information which is necessary for building the cor- plate is instantiated for a particular type R. Furthermore, responding binary. Consequently, there is no need for only a finite number of these instantiations can occur in external configuration or Makefile systems. a program or library, and template parameters cannot be Whenever we import functionality from C++ into passed to functions as objects. Mathemagix, our design philosophy implies that we have In Aldor, there is no direct equivalent of templates. to specify the necessary instructions for compiling and/or Mathemagix Nevertheless, it is possible to implement a function cube linking the imported code. To this effect, which takes a ring R and an instance x of R on input, provides special primitives cpp_flags, cpp_libs and cpp_include and which returns x*x*x. It thus makes sense to consider for specifying the compilation and linking the importation of C++ template libraries into Aldor. flags, and C++ header files to be included. numerix Mathemagix Although [4, 7] contain a precise strategy for realizing such For instance, the library of interfacing, part of the interface still had to be written by contains implementation for various numerical types. In particular, it contains wrappers for the Gmp and hand. Mpfr libraries [6, 10] with implementations of arbitrary Mathemagix features two main novelties with respect precision integers, rational numbers and floating point to the previous work which was done in the context of numbers. The Mathemagix interface for importing the Axiom and Aldor. First of all, the language itself admits wrapper for arbitrary precision integers starts as follows: full support for templates with typed parameters; see our paper [13] on the type system for more details. Secondly, foreign cpp import { C++ template libraries can be imported into Math- cpp_flags "`numerix-config --cppflags`" ; emagix in a straightforward way, without the need to cpp_libs "`numerix-config --libs`" ; write any non trivial parts of the interface by hand. cpp_include "numerix/integer.hpp" ; The ability to transform a C++ template library which ... } only permits static instantiation into a fully generic tem- plate library is somewhat surprising. Part of the magic On installation of the numerix library, a special script occurs in the specification of the interface itself. Indeed, numerix-config is installed in the user’s path. In the the interface should in particular provide the missing type above example, we use this script in order to retrieve information about the parameters of the C++ templates. the compilation and linking flags. Notice also that In this paper, we will describe in more details how this numerix/integer.hpp is the C++ header file for basic mechanism works. We think that similar techniques can arbitrary precision integer arithmetic. be applied for the generic importation of C++ templates into other languages such as Aldor or OCaml.