A blog about software – researching it, developing it, and contemplating its future.

Archive for the ‘metaprogramming’ Category

Intentional shipped!!!

leave a comment »

Intentional Software was founded in 2002 by Charles Simonyi, one of Microsoft’s first billionaires. They’ve been pretty vague ever since then, which led to the usual vaporware skepticism.

I’ve been following them for some time, because I had extremely positive results using domain-driven design at Nimblefish (my first serious enterprise software job). I’ve also blogged extensively here before about the appeal of extensible languages.

Well, last week they finally gave a real demo, and it turns out they’ve been very busy for these last seven years. The Intentional team has made some real leaps forward in the whole concept of language construction, multi-language projection, and bidirectional editing. Basically, their system lets you use any number of different languages to describe a problem domain; you can create your own languages, project them as Ruby/Java/C# code, tables, or diagrams, edit them in any format and have it translated directly to the others (to the extent possible), run the model directly while editing it, and just generally take metaprogramming to completely the next level.

Yes, I’m excited; it’s not every year (or even every decade) that you see a demo like this.

Here’s a breakdown of the contents, if you need to optimize your optical cycles:

  • 0:00 – 9:00: basic Powerpoint conceptual overview
  • 9:00 – 13:00: more technical Powerpoint about the structure of their system
  • 13:00 – 21:00: illustrating the “metamodel” for a state machine domain-specific language
  • 21:00 – 31:00: bidirectionally projecting and editing a state machine via Ruby, UML, etc.
  • 31:00 – 36:00: demonstrating an electronics DSL, including a live evaluation of electrical flow while editing the circuit graphically
  • 36:00 – 43:00: the Cap Gemini beta test product, implementing a pension system with live Intellisense on the actuarial math equations, and a temporal rule language for pension rules
  • 43:00 – 46:00: the multi-user versioning support that operates at the level of their fundamental tree-based data structure
  • 46:00 – end: various Q&A

If you only have 15 minutes, watch 31:00 – 46:00.

There are various systems that have done various subsets of all this before, but I’ve never seen it packaged in a unified way. Ever. It’s time to start watching Intentional very closely. It may also be time to check out the open-source JetBrains Meta Programming System. Software is all about raising levels of abstraction, and we might just have some new cranes coming online.

Edit: I just checked out the Meta Programming System’s tutorial, and yay! Looks like it’s a free version of many of these concepts that we can play with now! Time to tinker….

Written by robjellinghaus

2009/04/26 at 02:59

Not Dark, Just Busy

leave a comment »

It’s amazing what a difference not commuting can make. Up until our move to Seattle in April, I’d been commuting an hour each way every day into San Francisco. That time on BART — without net access — was where all my blogging got done. Well, now I’m driving 15 minutes each way to work (modulo time to take my daughter to her school), and still coming down off the stress of moving, so I’ve been putting my energy elsewhere in the evenings. In any case, consider this a summer vacation for your RSS feed 🙂

Not that I’ve personally been vacationing — not at all! I’m finally getting up to some kind of reasonable cruising speed at work. It’s been a colossal re-education in .NET, C#, low-level Windows debugging, SQL data stack design, and about 50 different interesting research projects which I can’t really discuss — at least not yet. It’s very educational being on the list for all the different talks that happen inside Microsoft Research; there are a lot of different pies that those folks have their fingers in. The internal Windows developer talks are also very intriguing.

Now, this isn’t to say that everything is on the hush-hush. Some of the better publicly visible tidbits I’ve run across lately involve LINQ, the techniques for basically doing data-oriented metaprogramming in the .NET framework. I mentioned LINQ in my last post, but suffice to say that it’s a combination of a set of data-oriented query operators (that form a composable, monadic algebra), some syntactic extensions to allow more idiomatic SQL-like query expressions, and an internal syntax-tree representation that can be transformed into native query languages (including but not limited to SQL) and partially evaluated into high-performance serializers. Overall it’s a very well-thought-out structure with a lot of room for growth. To wit:

  • DryadLINQ extends the LINQ operators to support MapReduce-like distributed cluster computation at scale.
  • PLINQ and the ParallelFX framework are building up the .NET infrastructure for fine-grained parallelism and efficient use of multi-core for data-parallel problems.
  • Matt Warren’s series on implementing a LINQ query provider is a great example of metaprogramming in practice — taking a C# abstract syntax tree describing a query, and transforming it into efficient SQL. This is the technique that is at the heart of LINQ. I’ve heard tell of F# projects that are using LINQ-like expressions to describe linear programming constraints declaratively — same metaprogramming paradigm, completely different domain.

All of this is exciting because you noble long-term readers (e.g. since December 2007) will know how interesting metaprogramming is to me. Microsoft really gets it on multiple levels, and has put it into production with LINQ. There’s a lot more on the horizon, too, and I’m eagerly waiting for each bit of it to reach RTM in some form so I can happily blog away about it!

Not only that, but I have a personal hacking project again. Matt Warren’s blogs (mentioned above) are a great example of how to implement metaprogramming with an imperative C# visitor-style API. But I find the code hard to read — it’s taking this lovely declarative representation and then transforming it with all of this intensely imperative, explicitly scheduled transformation code. It reminds me of the JastAdd project, which has evidently now reached public release. JastAdd creates a declarative framework for implementing language analyses and transformations. I want to hack on an F# implementation of the JastAdd paradigm, applied to LINQ to SQL transformations. It would be very interesting to see if it could lead to something significantly easier to maintain and reason about.

This is something that arguably is potentially relevant to work. So I am going to blog about it internally first, and then repost publicly a week or so later (on a per-post basis). If it gets to where it is interesting enough for Microsoft to want to privatize it, I’ll just have to find something else to blog publicly about! In any case, it’ll be fun to post about here for as long as it lasts 🙂

Written by robjellinghaus

2008/08/03 at 21:04

A Growable Language Manifesto

with one comment

Warning: this is by far the largest and most action-packed post I’ve ever made. Grab a cup of [insert favored beverage here], sit back, and enjoy. If you get headcramp from reading this in narrow-column blog format, there’s another full-screen version here — but please return to this post to leave your comments!

I’ve posted recently about the dynamic versus static flamewar and about recent research in extensible languages. The more I think about these ideas, the more compelling they get. It seems clear to me that these ideas are synergistic and point in the direction of a new kind of programming environment — one which could potentially offer the ease of dynamic languages, the safety of static languages, and an altogether new level of extensibility (both for enhancing the type system and for allowing safe metaprogramming.)

So I want to lay out a manifesto here for a growable programming language. Or perhaps it’s more like a toolbox for language construction. Or perhaps it’s a framework for experimenting with extensible syntax and extensible analysis. In any case, here is the vision. At the end, I include extensive references to the research that’s inspired these ideas.

  • A growable language should be opt-in. It must support a graceful spectrum of usage styles. A growable language will necessarily be multi-paradigmatic, since it will be extensible with features from various fields of programming. The language should implement a dynamically-typed core on which all static analysis is built, to allow programmers a straightforward and lightweight syntax for prototyping.
  • A growable language should be increasingly powerful. It should support a rich and expanding variety of static analyses and dynamic checking, to give maximal benefit to the programmer who wishes to leverage these features. over time, as more program analyses become possible, a growable language should gracefully integrate them. A growable language must continually increase the expressive power of the programmer, both in terms of what the programmer can say and in terms of what the environment can help the programmer prove about their program. A growable language should have a powerful analysis framework as a foundational component, such that other analyses can share that basic capability; wherever possible, additional analyses should require only incremental effort by leveraging infrastructure already created.
  • A growable language needs a declarative metalanguage. Current languages do not define inherent mechanisms for syntax extension and for type extension. The syntax and the type system are both baked into the language’s compiler. A growable language needs to lift the specification level of the language itself to be both declarative and modularly defined, so that syntax and type analytics can both be expressed and implemented as layered extensions.
  • A growable language needs partial typing. Certain analyses are necessarily incomplete or even undecidable. A growable language should permit such type systems, and should be able to fall back on dynamic typing whenever analysis is inconclusive. This lets programmers work freely without having to continually meet the demands of the type system, yet supports graceful type annotation to enhance static safety at the programmer’s discretion. (Without partial typing, the opt-in goal cannot be met.) The language should ideally provide clear feedback as to when its analyses are conclusive or inconclusive, and ideally should identify the sources of inconclusiveness so the programmer can either annotate them appropriately or deliberately ignore them.
  • A growable language needs layered types. A growable language should be able to extend itself with primitive types, dependent types (e.g. data-dependent subrange types), traits or other parametric or abstraction-based genericity mechanisms, and multiple flavors of type qualifiers. Integers should be as much of a language extension as non-null types, tainted types, or range types. A growable language requires an extensible subtype lattice.
  • A growable language needs inferrable types. To avoid drowning in explicit type declarations, the language should be able to infer types wherever possible, and the programming environment should support controllable visibility for the inferred type information. Without inferred types and environmental support for controlling analysis visibility, a growable language cannot scale for users; being able to selectively ignore some or all of the (many) analyses is critical.
  • A growable language needs explicit, optional type annotations. An extensible analysis framework will be able to infer or deduce a great deal about the actual behavior of the program. But the actual behavior of the program may or may not correspond to the programmer’s intent. The programmer’s explicit annotations express the programmer’s expectations. A programmer might look at the analyzed annotations and make some of them explicit — perhaps they reflect properties of the code that the programmer considers important after the fact, and that the programmer now wants to enforce. Or a programmer might add explicit annotations during development, such that the system can confirm they are valid and warn when they are violated. Explicit annotations at module boundaries — whether programmer-generated or system-generated — are likely to aid in separate compilation and in module documentation.
  • A growable language must be efficiently implementable. As more language features are added — more qualifiers, more types, more syntax — the language must still be efficient and usable. This applies both to programs written in the language (which should have performance competitive at least with Java or the CLR) and to programming environments for the language. The latter requires aggressive attention to analytical optimizations and to multi-threaded analysis frameworks. As the language’s analysis structure grows, the language’s programming environments must be able to leverage multicore architectures to ensure consistent responsiveness for users. Moreover, the language should be continuously analyzing in the background; incremental feedback as the user edits should be available for all language features and extensions.
  • A growable language must have a unified internal representation. Concrete syntax, abstract syntax, type and name bindings, type qualifier propagation, type parameterization, dependent range type constraints, and logical queries over the program’s alias structure should all leverage a single internal representation of the program. This maximizes the reusability of internal language implementation code and ensures consistency in analytical derivations. Where multiple representations are necessary, the derivation rules must be clear and consistent.
  • A growable language must promote static metaprogramming. Fully dynamic metaprogramming — runtime extension of classes and objects with arbitrary code, or even unrestricted dynamic reflective access to arbitrary methods — is almost impossible to analyze effectively. To give a growable language’s extended type systems maximum exposure to the actual behavior of the code, static metaprogramming techniques must be definable, extensible, and compatible with the rest of the language’s structure. One would hope that the very extensibility techniques that implement the language itself would be usable for static metaprogramming.
  • A growable language must support diverse analyses. Some analyses are most naturally expressed as systems of recursive equations over an abstract syntax tree. Others can best be expressed as logical queries over a program’s alias graph. Ideally these could both be expressed naturally in the metalanguage.
  • A growable language must be analytically composable. This is likely the single most technically ambitious goal. Traditional compiler development involves subtle and explicit scheduling tradeoffs, layering multiple analyses through manual interleavings that can be fragile or non-optimal. A growable language with a declarative metalanguage needs an analysis engine that can automatically schedule multiple, potentially interleaved analyses, leveraging parallelism where possible to optimize non-dependent analyses. Achieving this goal will be immensely difficult, but proportionately valuable. Interestingly, here is where the metalanguage itself will require many of the same extensibility properties as the language it describes; meta-level closure — implementing the metalanguage using the language itself — will be a holy grail for this language design.

Is the above language even possible? Is it too much of a stretch — particularly the “unified internal representation” and “analytically composable” goals? Maybe so. I’m definitely not an expert at programming language implementation; the only compiler I ever wrote was back in high school. So this may be ridiculously unrealistic in whole or in part. I welcome feedback on which areas of this manifesto are more or less plausible.

Overall, consider this my stab at answering Paul Graham’s challenge to ponder the hundred-year language. Given current trends in programming language development, it seems that languages of the future will transcend being “languages” as we know them and will become more like unified environments for language creation and extension. Arguably, this vision has a lot in common with intentional programming, which doesn’t bode well, since the intentional guys have been in stealth mode for almost fifteen years and nothing has publicly come of it. But that doesn’t mean the general direction isn’t interesting, any more than the slow progress of Chandler means that a unified and flexible personal information manager isn’t worth pursuing.

I promised references. Here they are:

  • opt-in — Gilad Bracha’s pluggable type systems paper is the origin of this goal. Bracha forcefully posits that static type systems are all necessarily incomplete and that dynamically typed languages have necessary flexibility. Meijer makes a similar point in his static where possible, dynamic where necessary paper. I’m not clear that Bracha’s extreme view — that all static analysis must be kept out of the language kernel — is the correct one, given the potential performance cost, but I suppose RPython provides an encouraging counterpoint.
  • increasingly powerful — There are increasingly many varieties of static analysis being developed primarily for Java. One recent paper on points out that its framework could straightforwardly be implemented on top of other analyses with similar intraprocedural resolution. The BDDBDDB framework has already been used for implementing taint analysis, and the JastAdd system for non-null Java type inference. In general it seems there is a lot of opportunity for shared infrastructure here. Also note that support for excellent error messages and great visibility into analysis results (and analysis failures) will be critical for usability. See Grimm’s paper titled Systems Need Languages Need Systems for some forceful advocacy here.
  • a defining metalanguage — Some good examples of metalanguages for syntactic language description are the OMeta pattern-matching language for executable grammars, Gilad’s executable grammars in NewSpeak, and the Rats! extensible parsing system. A good example of an extensible language for static analysis is the JastAdd extensible Java compiler with its support for defining rewritable circular reference-attributed grammars… they implemented Java 1.5 generics as a modular declarative compiler extension, which proves their point to me, anyway!
  • partial typing — The two best examples of this that I know of are the gradual typing work of Siek and Taha, and the hybrid typing for undecidable type systems work of Flanagan, Freund, et al. In both cases, a static type system is enhanced with a generalized type (named Dynamic or “?”), which is inferred to be a more specific static type where possible, and otherwise cast at runtime to preserve dynamic type safety.
  • layered types — The already-mentioned JastAdd system is perhaps the best example of a structure which permits layering additional analyses. The extensible Java compiler Polyglot is another.
  • inferrable types — My original thinking about this entire line of research originated in a blog post from a couple of months ago where I realized that some implementations of type qualifiers — for example, a “tainted” type qualifier in C++ — would ripple through the whole program due to mandatory explicit static typing everywhere. It’s noteworthy that many type qualifier analyses for Java are not based on explicit syntax. For example, the taint analysis based on the BDDBDDB framework does not require explicit propagation of tainted or untainted declarations, yet it derives such information throughout the program’s structure. An environment which made the results of that analysis visible — and traceable — at every program point would let the programmer see the flow of tainted values without having to explicitly declare them.
  • explicit, optional type annotations — Programmers must also be able to add explicit qualifiers, since the programmer’s intent may or may not match the analysis; the analysis may be incomplete and the programmer needs to provide more information, or the analysis may be consistent and the programmer wants to declare that they confirm it and want it to be enforced at that location (e.g. if the program changes such that the property is no longer true there, the language would signal an error). The programmer’s explicit intent and the analyser’s implicit understanding should be able to be flexibly cross-checked. I’m not aware of any inference systems that support this fully; they seem to be either purely inference-based (e.g. JQual) or purely annotation-based (e.g. a C++-based type qualifier system discussed in the “Extending Type Systems in a Library” paper from LCSD ’06).
  • efficiently implementable — This is obviously enormously difficult, insofar as analyses can in general be interdependent. There is a great tension between layering analyses (for separability) and weaving them (for mutual dependence and synergy). See the “analytically composable” goal below. In general, I wouldn’t be surprised if aggressive parallelization of a language analysis / compilation framework required software transactions to support optimistic and incremental analysis by multiple threads.
  • a unified internal representation — I mean something along the lines of Grimm’s declarative extensible syntax trees, a concept proven by his weaving of Java and C typechecking into a single system. The JastAdd framework is another example; there is no separate symbol table in JastAdd, since name bindings become reference links in the abstract syntax tree (which is really more of an abstract syntax graph). Note that JastAdd’s own declarative language for extending the syntax tree is fundamentally similar to open classes, in that subsequent extensions can directly modify the structure of already-defined syntax node classes. This seems to inhibit modular development of language extensions, but modular language extension development is really hard anyway.
  • promote static metaprogramming — This goal is about ensuring the entire program text remains analyzable, and about permitting domain-specific languages to be implemented in the same structure used for extending the base language. See OMeta’s SQL extensions or C# 2.0’s language-integrated queries which reify some expressions as syntax trees visible to the runtime system. The Google Web Toolkit’s support for extensible code generation is another example, as is the RapidMind system for creating parallel compute kernels from C++ code. Finally, there’s the RPython system which creates a “metaprogramming startup phase” for Python programs, followed by a static compilation phase yielding 100x speedups. Interestingly, this whole goal contradicts Paul Graham’s 2001-era view that “true macro systems aren’t compatible with static type systems.”
  • support diverse analyses — The best two already-cited examples here are the JastADD grammars formalism and the BDDBDDB Datalog-based analysis specification. These are radically different but potentially very synergistic. I’d be fascinated to see whether there’s some deeper commonality between them….
  • analytically composable — The JastADD framework seems the best example of a declarative structure that supports automatic weaving of multiple analyses. For evidence to this effect, consider that the JastADD folks claim that the Polyglot team is reimplementing their framework in JastADD to avoid the difficulties of scheduling dozens of analysis passes.

I plan to start experimenting with some prototypes of an Eclipse plugin for an extensible language framework along these lines, likely starting with something much like OMeta and extending it with a JastADD-like rewritable grammar formalism. This will be open source all the way. I would enthusiastically welcome all pointers to similar projects, all interest in helping with such a framework, and all critical comments on all or part of this manifesto!

(Disclaimer: our family will also be moving out of our house in the next three months, so progress may well be slow 🙂

Written by robjellinghaus

2007/12/04 at 19:11