leah blogs

April 2005

28apr2005 · Fortress

Fortress is a fairly new language developed at Sun Microsystems. They claim it is a

general-purpose, statically checked, nominally typed, component-based programming language designed for producing robust high-performance software with high programmer productivity.

And they also state its domain very clearly:

The name “Fortress” is derived from the intent to produce a “secure Fortran”, i.e., a language for high-performance computation that provides abstraction and type safety on par with modern programming language principles. Despite this etymology, the language is a new language with little relation to Fortran other than its intended domain of application.

Recently they published a preliminary Fortress Language Specification (which contains above quotes, by the way) and had an a bit closer look at it.

My general impression is that the language totally rules in a way such a thing never was there before. Think about mixing the domain of Fortran with the flexibility of Common Lisp and the type system and syntax of ML and Haskell and you have a language very close to Fortress.

Should Fortress gain a wide acceptance (which, I hope, is likely; but remember Self), it will be one of the most advanced languages out there, I don’t think there is any language outside academics that would be on par (feel free to prove me wrong).

But now, after this hype, let’s have a look at some features.

The first thing you will notice by glazing at some Fortress code is that it uses Unicode syntax. For example, you write instead of >= and also can use greek letters like α, β or γ. You can write Fortress in plain 7-bit ASCII too, but that will force you to possibly use ugly constructs, so Unicode really is the way to go. I wonder how this will influence Fortress IDEs, they maybe will have special keyboard macros for this kind of stuff.

Very soon in the manual, Objects and Traits are introduced. Fortress includes a very nice, statically typed object system. Traits are a bit like classes in other languages (Modules in Ruby), but can inherit (or, mix-in) other traits. Traits implement methods. If I think about it, the object system is very similar to the one of Self. You create new Objects by specifying a main trait (parent* in Self) and mixing in other traits (Modules in Self, if I recall correctly). The object system supports overloading and multiple dispatch.

Also, Fortress has top-level functions (wrapped in a package system of course, inspired by Java’s; it is rather complex but also addresses issues like building programs or installing 3rd-party libraries. A bit as if Ant was included, or defsystem) that are first-class and can be passed around. Anonymous functions can be created, but only with limitations. I could not find any mention of closures.

The Fortress language is very clear and easy to read. The default (more on that later) Syntax uses indentation in a Haskell-way to save usage of ;, and another special (but IMO obscure) syntax for arrays. For example, you can simply write

accelerate(θ) =
  spin := spin + θ

Another interesting (IMO not a very good idea) thing is that multiplication is implicit, that is, you can write:

foo := bar quux dong

A nifty feature, probably taken from Eiffel, are function contracts. They can check the values (“require n to be greater than zero”) or do assertions (“if the input list was sorted, the return value will be too”).

Fortress provides a generic for loop that uses generators internally and can easily parallelize (important!) the calculation.

There is a case statement which works for all kind of types and any predicate (took a long time to get that into languages…) and dispatch, which can dispatch on type and is type safe.

Arrays, sets, maps and matrices are in the core language and have easy syntaxes to make them convenient to enter. Also, you can calculate sets and arrays using Haskell-style list comprehensions.

Arbitrary-precision integers and rational numbers are provided, too. One very nice thing I really like is that you can define dimensions that provide type-safety to calulations. For example:

dim Length
unit m : Length
k = 1000

circumference = 40075 k m

Now, you can create functions that only operate on Lengths. Additionally, and this is the nifty part, dimensions can be connected to compound types, for example:

type Area = Length²

I can imagine this to be very useful to scientific calculations, as wrong calculations/formulae can be found easily.

Definitions and declarations have modifiers, some of which are well-known like abstract or private. However, there are much more powerful and cool modifiers, for example io which needs to be given to perform IO directly or indirectly (kind of like the IO monad of Haskell). Or pure which makes methods ensure they have have no side effects and will probably help a lot with optimization. Then, there is a static modifier that can make methods run at compile-time, for example for static checkers or parsers. You can use value to make your objects immutable and wrapped to forward undefined methods. Finally, there is a test modifier to define unit tests.

Fortress also provides futures, values that will be calculated in “background” and can synchronize when they are enforced. A very nifty thing are atomic expressions that will make all read/writes to variables happen in a single step, for example:

atomic do sum:=sum+a(i) end

Arrays can be optimized heavily due to their abstraction. You can choose between several ways to distribute them in memory and how to apply parallel calculations.

Now we come to one of the biggest and most powerful features, the extensible syntax. It can be used to write domain-specific languages inside Fortress and works a bit like Caml4p, IIRC. DSLs can access inner Fortress code using syntax escapers. E.g. you write:

syntax sql exp end = parseSQL(exp)
  SELECT spectral_class FROM stars

and it can generate code as if you had written

SqlQuery(Select("spectral_class"), From("stars"))

It would be even possible to support C-ish for loops like

jfor (int i = 0; i < 10; i++) {

As you probably can tell, I’m rather excited about Fortress. I think it is a very powerful and flexbile language, but it has lots of features (not to say, it is over-engineered). Maybe it’s a bit too big and they would rather stick some features into external libaries instead of providing them in the core.

I hope Fortress will be a successful language (you could say that solely depends on the marketing, but they need a damn good optimizing compiler too for their domain; also, an open-source implementation to get support from “the community”), but lastly it will be the programmers’ thing to actually make use of all the features provided in a sensible way, which can be too much for people that only use Fortran all the day for now. However, I think they can be introduced into Fortress rather easily.

Fortress is probably the best and most properly designed language for high-performance computing to my knowledge.

NP: Brazilian Girls—Lazy Lover

Copyright © 2004–2022