Monday, June 7, 2010

Should programming languages be extensible?


Introduction:

Modern object-oriented programming languages provide, at their most basic level abstraction (through interfaces), encapsulation (through private class members) , programming by difference (through inheritance/decoration), and composition (of data through containers and of logic through declaring sequencial operations) . These 4 concepts give us the power to divide a complex problem into more bite-sized chunks, providing knowledge barriers in order to hide information unrelated to the bite-sized piece the developer is working on at the time.

Modern languages also inherit some very important but non-object-oriented abstractions too. This is because some abstractions we would like to create do not naturally fit into the object-oriented model where data and logic are encapsulated together. Some examples are :
  • Purely functional abstractions (like mathematical operators)
  • Pure data abstractions (like DTO's)
  • Memory Management
  • Threading issues like deferred execution, locking, asynchronous calls
  • Aspect oriented programming / cross-cutting concerns
So we find that along with the pure object oriented syntax are many other language features that may (or may not) be useful depending on the domain. Language features like overloading, reflection, lambda expressions, checked exceptions, design by contract, dynamic types, memory management, and extension methods to name a few. No one language can incoorperate all these features because some of them are mutually exclusive, or specific to a narrow domain (like telephony, or web services for example). Some features will provide more constraints on how we are able to do things. Others add more syntax that makes the language less natural looking. Some features are focused on prototyping/productivity while others focus on reliability.

The more language features we include, the more complex the language becomes. This is why most popular languages only implement the features that increase productivity, and leave out the ones that create more reliable code. Besides, some of the abstractions can be massaged into an object oriented framework or library. The only problem with using the OO counterparts is:
1) The syntax does not have a natural language type flow
2) There is no compile time checking
3) They often rely heavily on reflection.. a feature which typically has costly performance overheads

This article explores the concept of giving the developer the ability to pick and choose language features, or even create new language features specific to their domain.

The way we do things currently

From the application developers point of view, the language he uses should be everything he needs to express the requirements in a non-ambiguous way. This is achieved by providing language layers, of which only the bottom layer is the actual programming language. Each layer can function as an extension of the layer below it, or it could hide the details of the layer below it. The former case is similar to inheritance, the later similar to encapsulation.

Here is an example of a typical web services application and it's layers
Application wiring (often represented with a diagram, or as a IOC-configuration file or web.config)
Features (The part that most closely represents the requirements specification)
Reuseable framework and library componentsDomain specific classes (encapsulate the business objects)
Compiler language

The way of the future?

I would like to suggest another layer just above the "Compiler language". I think the compiler language should be very simple and support very few features. Just above this layer should be a set of features that are part of the compilation process, providing language elements that are executed and verified at compile time.

In the same way we include namespaces in a class header file we should be able to import just the language features that are needed . This could be defined at the scope of the application, or more finely scoped to the namespace/class level. If we were able to do this, then the base language would be able to support any number of arbitrary language features, even ones that are mutually exclusive of each other.

One thing is for sure. If we had the power to create our own language features, we wouldn't be waiting for microsoft to bring out their next release of .Net before we start using generics(v2.0), link(v3.5), or dynamic types(v4.0)! It would also be very nice to not have to develop your modules in Java just because you want checked exceptions, or Eiffel just because you want design-by-contract.

So what are the roadblocks to providing this kind of extensibility? We have been creating applications that are reliable and extendable by using good OO techniques and test driven development for years. We can add modules in small units and verify the application works as expected. Is it too hard to apply the same principles to compiler design?

I'm looking for some feedback on that question.

The base-level-compiler would need to provide only a few language features, plus hookins for the feature extensions. The compiler could be re-compiled and retested with each new feature added, or it could provide a plugin type architecture.

Concrete examples of things you could do with an extensible compiler:

  • Language features to support state patterns, and then on top of that syntax like: until (some event) do {...}
  • Being able to extend the metadata of a class without modifying the class's definition.
  • Many frameworks and toolkits that use reflection , dynamic code generation, or code weaving can be re-written to do their work at compile time.

Roadblocks in C#

It seems that in C# many language features are hacks where the compiler needs explicit info on class definitions in order to do lots of "special case" compilation. For example [AttributeUsage] and [ThreadStatic] attributes. The compiler has specific knowledge of these attributes, which shows a dependency of the compiler on a compiled MSIL library??!! Other examples are :
  • foreach/yield -> depends on IEnumerable
  • using -> depends on IDisposable
  • try/catch -> depends on Exception
  • linq -> depends on IQueryable
  • dynamic -> depends on DynamicObject
I wonder if the reason the language is so hard to extend is because there are so many bad dependencies? It doesn't seem right that compiler code should be able to interpret the OO code in the layer above it! I guess it's similar to a domain object reading the web.config file to find it's configuration. If there is a using keyword, there should also be a dispose keyword, and you should be able to mark a method in a class as the dispose method. That way there is no dependency on the OO layer.

Writing code for processors with many cores

The code we write is not supposed to be a line by line count of what we want the cpu to do. It is merely a way of describing our business rules / logic in a way that is non-ambiguous.

How our code gets converted into machine code, (or an IL for that matter) is the job of the compiler / interpreter.

With multiple cpu's we may need to address an extra concern : distributing load across multiple processors. For any loop or repetitive action there may be several ways to achieve optimal performance depending on the data we may be working on.

People talk of using extra markup to define to the compiler which optimization scheme (if any) should be used. I say this is the wrong approach. It adds complexity to the piece of logic we are trying to describe, causing the programmer to hold too many concerns in their head at the same time.

The code we write should be only what is necessary to define the routine and no more. That is the best abstraction for code. No leaks please. Optimization should be a concern you address later in an orthogonal view, or using an external tool which extrapolates the routines that qualify for optimization, and then allows the user to choose which model to use.

Its a bit like one person can design the schema of the database, and another can add and remove indexes in order to address performance issues. (Its one separation of concerns that rdbms databases do well. Databases suck at providing other abstractions like inheritance and interfaces. Views are a great way to create a sub-set of functionality. I think the problem is that the database should be defined as the combination of all possible views rather than the views being a subset of the whole database. Its kind of like micro-kernel vs monolithic kernel. In the end the database administrator really needs to understand every table, rule, column and view if he is to
make any change to the schema of the database. Nasty. )

Better yet, (going back to the multi-processor optimization) you could have the application keep statistics and choose the best optimization at run time based on the data/conditions at the time. Anyway, at least we have the option to do all this if we separate the concern!

I suppose this approach can also be taken for locking on variables.
Identifying the possibility of race conditions,and dead locks... shouldn't all this be possible just by interogating the source? If a human can do it, a computer program could do it even easier, and with a lot more accuracy. Why then do we even bother to think about obtaining locks on sync objects? Why cant the compiler work out where its needed and let us FOCUS ON THE LOGIC/ BUSINESS RULES!

Whenever I hear "don't forget to do this.... don't forget to do that..." I think "We could be doing this much better!"