Home | About | Partners | Contact Us

SourceForge Logo

Quick Links
Building XL
XL Mailing List

Understanding XL
Conceptual overview
XL examples
Inside XL
Concept Programming

In depth
Browse GIT
SourceForge Info

Other projects
GNU Project
The Mozart Project

XLR: Extensible Language and Runtime

The art of turning ideas into code

XL, an extensible programming language, implements concept programming
If you want to know more, you should start here.

The new C++ standard

Friday, September 26, 2008

Bjarne Stroustrup, inventor of C++

DevX.com has a special report on the new C++ standard, currently referred to as C++ 0x because it was due sometime between 2000 and 2009. Well, the standard was quite late compared to early expectations, so an easy joke was that we might end up with 0xA or 0xB, the C++ hexadecimal notation for 10 or 11. Ultimately, chances are that the standard will make it for 2009, so we will probably refer to it as C++ 09...

This new iteration of the language is of interest to all programmers, because it brings a number of major changes to one of the most popular programming languages today, and one that is already very complex (and therefore hard to extend). But for me in particular, it is all the more interesting to consider how various "innovations" in that new standard compare to features that play the same role in XL.


One of the major features in C++ is concepts. DevX has a dedicated article about concepts. In short, concepts in C++ are a way to describe categories of templates, and to help the compiler figure out what the programmer intended for a given template. This new aspect of the language makes it easier to define a real contract between the users of a template and its implementers.

Concepts vs. code

C++ concepts, however, are somewhat annoying to me. One reason is that XL has been for a long time based on an approach that I dubbed concept programming. Concept programming, in the XL sense, is about the relationship between concepts that exist only in our head, and concept representations that exist in the computer. The key idea is to make sure that implementations look and feel like the concepts they represent.

One key consequence of that idea is that a programming language should comfortably support arbitrary concepts, not some finite set (e.g. functions or objects), because the set of concepts we manipulate is not a-priori limited. This is the key reason so much effort was put into making XL extensible.

To summarize, "concepts" in XL are only very remotely related to "concepts" in C++, although, arguably, the XL usage of the word is closer to the standard meaning.

XL generic validation = C++ concepts

Does that template or generic apply?

Many aspects of XL are a direct consequence of the concept programming design philosophy. For example, XL implemented, since at least 2002, the idea that one can describe how a generic type can be used. This feature is called generic validation in XL terminology. I invite the reader to compare the XL implementation of a minimum function with the C++-with-concepts implementation of the same. This should convince you that the two ideas are basically almost identical.

So where are the differences between C++ concepts and XL generic validation? One of them is how the contract is being specified. In C++, you specify the kind of operators and functions that define the concept. For example, you would write something like the following to indicate that a min function requires a less-than operator:

concept LessThanComparable<typename T> { bool operator<(const T& x, const T& y); }

template<typename T> requires LessThanComparable<T> const T& min(const T& x, const T& y) { return x < y? x : y; }

In XL, by contrast, you give an example of code that has to compile with the generic type you want to validate. For example, in XL, you would write something like:

generic type ordered where A, B : ordered Test : boolean := A < B

function Min (X, Y : ordered) return ordered is if X < Y then return X else return Y

Now, as you can see from this simple example, a significant difference is that XL considers the validation to be tied to a generic type, which can then be used to declare a function like Min directly. In other words, since you declared that ordered is generic, Min becomes implicitly generic. By contrast, in C++, LessThanComparable is a kind of predicate that applies to template classes, so you need one additional "connection" using the require statement, to let the compiler relate the T in the definition of min with the T in LessThanComparable. As a result, the C++ code for that example is more verbose and more convoluted. This becomes more visible as the code becomes more complex.

Another drawback is that the C++ concept specification as written doesn't work for, say, int because the less-than operator in that case doesn't have the right signature. So you need an additional concept_map in that case, making the code even more verbose, as shown below:

concept_map LessThanComparable<int> { }

One benefit of the C++ approach, however, is that the specification of the concept makes it easier to validate early that the implementation actually doesn't require anything besides what is declared in the concept. For example, if the body of min attempts to refer to an operator that is not present in the concept specification, the compiler may detect this. Doing this with the kind of specification given in XL is much more complicated. I am considering various ways to fix this problem, which is much easier in XL since practically nobody uses it yet.

Multitasking and Threads

C++ 0x also adds standard support for threads. In my opinion, it is ironic that they manage to shoe-in support for a thread model that is so "last century". Today, the difficult problem is not threading on a SMP system, but threading on non-uniform architectures, for example threading between a CPU and a GPU, or between the components of a Cell microprocessor, or threads that cooperate on machines with different architectures across the Internet.

This kind of problem is much more complicated, and is already, to some extent, solved by other languages such as Java or Erlang.

At this point, XL has little to offer in that space, because what is needed is not coded yet. However, I am confident that XL's extensibility will make it easy to implement not one, but a multitude of tasking models. Among the top candidates are rendez-vous based mechanisms similar to Ada, message-passing protocols similar to Erlang, or data-driven parallelism similar to several functional languages. Stay tuned.

Variadic templates

C++ 0x will, at long last, implement variadic templates. This feature will make it possible to write functions that take a variable number of arguments, yet are type-safe.

This is, again, something that existed in XL since 2001 or earlier. You can see that XL implementation of the Max function takes advantage of this feature.

The C++ implementation is more complete, however, as it makes it possible to create not just variadic functions, but also variadic classes. This is something that is planned, but not currently implemented in XL.

Range-based iterations

A new range-based iteration mechanism was also added to C++ 0x. XL has a more general form of iteration, that already covers this specific case. Here is for example how for loops are declared in XL:

iterator IntegerIterator(var It : integer; Low, High : integer) written It in Low..High is It := Low while It <= High loop yield It := It + 1

The notation It in Low..High is how you will invoke the iterator, and the yield statement in the iterator is where the body of the loop will go. The usage of the iterator is very natural:

for I in 1..5 loop for J in 1..I loop WriteLn "I=", I, " and J=", J

The benefit of this more general approach is that you can for example define two-variable iterators:

iterator MatrixIterator ( var I : integer; LI, HI : integer; var J : integer; LJ, HJ : integer) written I,J in [LI..HI, LJ..HJ] is I := LI while I <= HI loop J := LJ while J <= HJ loop yield J := J + 1 I := I + 1

for A, B in [3..5, 7..9] loop WriteLn "A=", A, " and B=", B

You can also define iterators over any kind of data structure, using any syntax you need for this particular data structure.

Constant Expressions

C++ 0x introduces the notion of generalized constant expression. This makes it possible to declare functions that the compiler will be able to evaluate at compile time.

Once again, the XL approach is very different. The XL compiler has various phases, implemented as "plug-ins" for the compiler. One of them deals with constant folding (i.e. evaluation of constant expressions). Here is an example showing how to compute factorials at compile-time using that technique.

The XL pre-processor also makes it easy to implement compile-time assertions, something that is also a new feature of C++ 0x. The XL implementation, however, will automatically optimize a static assertion if it can evaluate the argument at compile time, instead of requiring a specific keyword.


C++ is an extremely complex language, and extending it took a lot of effort. Many of the new features have already existed in XL for a while, and are much easier to implement. However, the implementation in C++ points out some weaknesses in the way things are currently done in XL, something that is fortunately still easy to change that early in the language's life.

Note: This article was originally published here.

September 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Aug  Oct

Copyright 2008 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)