Posts Tagged ‘extensions’

Intervals in MPS

Wednesday, March 25th, 2009

Language of mathematics contains a lot of domain specific constructs. Almost every part of mathematics has its own domain specific “extensions”. So it’s a good idea to borrow from there. In MPS we have a special base language extension that contains some mathematical notation. In this post we will consider one of such notations that we borrowed from mathematics.

We quite often write code that checks whether a value lies in a specific range. This is especially widespread in a code working with graphics. We might write something like this:

This piece of code checks whether a point lies inside a rectangle. Quite boring, isn’t it? In our jetbrains.mps.baseLanguage.math language we have support for intervals. Instead of the code above, you can write this:

As you can see it is shorter and much easier to read. Intervals, in the language can be open or closed. For example you can write this:

They can even contain infinities:

This language feature will be available in MPS 1.0. In addition to the intervals MPS will provide other mathematical notations, for example sums. We will write about them in one of the following posts.

Posted by Konstantin Solomatov, Lead MPS developer

Tuples Support in the MPS Base Language

Thursday, February 26th, 2009

Many functional and dynamic languages have “tuples” in their syntax. Tuple is a sequence of a fixed length. For example, pair, triple and quadruple are tuples. They are very useful when you have algorithms operating on entities which have two, three or more parts and you don’t want to create devoted classes for them, or if you want to return multiple instances from a method.

Let’s take a closer look at how tuples are implemented in MPS.

We have a tuple type. Here is a pair of String and int:


We have a special syntax for tuple construction: we added a new type of literal to the base language:

Each element of a tuple has a name by which it can be accessed:


Tuples are immutable, so you can only create new tuples but can’t change the existing ones. If two tuples contain same element types but different names they are compatible. I.e. you can write:

It took us only about half a day to implement such a cool features in MPS, so it isn’t really hard to do. This feature is going to be available in MPS Beta 2.

MPS Beta 2 should be available next week. Check back at MPS web site or at this blog for the announcement.

Posted by Konstantin Solomatov, Lead MPS developer