« Knowledge Representation in MPS | Main | Collections in Base Language [2] »

September 30, 2006

Collections in Base Language

Currently, it is almost not possible to write code in Base Language and avoid using of java collections and other JDK classes like Object and String.
Such a dependency on Java is not good for BL (in the end, MPS is not a java development tool) and this is going to change, so far with respect of collections.

The Collections DSL is a domain-specific extension for the Base Language. The central concept here is the sequence, which strongly resembles concepts of iterable (Java) and enumerable (C#).

Collections DSL allows to define a sequence in very simple terms.
For instance, an infinite sequence of natural numbers can be defined as:

collections-new-natural-numbers-sequence.PNG


Collections DSL also defines foreach loop suitable for iteration of a sequence:

collections-foreach-loop.PNG


...and set of operations applicable to a sequence.

In the following example I will take the nums sequence (see above) as an input, then accept only even numbers and generate a string for each such number:

collections-where-select-oper.PNG


The last example (as it becomes a tradition) is the Fibonacci sequence:

collections-fibonacci-sequence.PNG


Note that the method parameter max is used inside the code-block in large square brackets. In fact, code-blocks (that is code framed in large square brackets) have property of closure, i.e. variables from an enclosing lexical context can be used and be modified.

The Collections DSL along with samples probably will be available in the next MPS build.

Posted by Igor Alshannikov at September 30, 2006 04:35 AM

Comments

Although the Collections Language looks nice, I fail to see how it offers a greater improvement over writing the same methods in a class.

I mean the way you created a Fibonacci Sequence here is pretty much the same way one would create in C# or Java. Just that the code you put in those huge brackets would be neatly tucked in a method.

Posted by: PDeva at October 17, 2006 01:22 AM

That’s right, collections language doesn't offer much improvements over using of Java/C# collections.
Instead it makes base language not dependable on java.util or other class library so that we (potentially) will be able to generate BL code to either Java or C# or other target.

Posted by: Igor Alshannikov at October 20, 2006 08:10 PM

IMHO it's important to distinguish between finite and infinite sequences, since the semantics are different. For example, you can serialize elements of a finite sequence but not of an infinite sequence.

Infinite sequences are called streams in Scheme. Operations such as "where" can be defined on both sequences and streams and return a value of the corresponding type.

Posted by: Misha Dynin at October 29, 2006 06:31 AM

Note what pure sequence (in MPS) is not a collection - it doesn't store its elements. Instead a sequence can compute elements in turn.
Therefore sequences are not serializable (no matter finite or infinite ones) unless we can serialize the computation procedure itself.

I'm not sure there is need to distinguish between finite and infinite sequences.
In most practical cases we use 'list' (which is always finite) to pass data and have sequences as byproduct in local computation (where it is obvious whether it is finite or infinite).

Posted by: Igor Alshannikov at November 9, 2006 07:33 PM

Post a comment




Remember Me?