background preloader

Dsl

Facebook Twitter

Dsltools

Ometa who what when where why. What if programming language implementations were object-oriented? What if you wanted to change one itty-bitty part of your favorite language? Say you wanted to add an exponentiation operator (^^). How hard would it be? Wouldn't it be nice if you could "subclass" a language like C# and then add 5 lines of code to make it work and then use it?

What if you could add Ruby-like method_missing support in 20 lines? What if you could conceive of a new language and start experimenting with it in production code in an hour? What if it leveraged your knowledge of existing frameworks like .NET? Let's imagine that it were possible. Even without knowing the syntax, you can probably figure out how it works since it's written very close to the standard way of describing a language.

But you could make it work by writing this object-oriented-like "subclass" of the language: Now, giving this language "2^(3*4)" happily gives you 4096. This worked using simple inheritance. Who? Pattern matching. When? P.S. Architecture as Language: A story. Abstract Architecture is typically either a very non-tangible, conceptual aspect of a software system that can primarily be found in Word documents, or it is entirely driven by technology ("we use an XML architecture"). Both are bad: the former makes it hard to work with, and the latter hides architectural concepts behind technology hype. What can be done? As you develop the architecture, evolve a language that allows you to describe systems based on this architecture.

Based on my experience in a number of real-world projects, this makes the architecture tangible and provides an unambiguous description of the architectural building blocks as well as the concrete system while still staying away from technology decisions (which then can be made consciously in a separate step). The first part of this paper illustrates the idea using a real-world story. A story System Background So I was with a customer for one of my regular consulting gigs. The starting point Background: What is a language?

Concept Programming. DSLs aren't (necessarily) natural language. Throwing a DSL over the wall. Here are a few thoughts about the idea that given a DSL, you can just turn your entire business related stuff to the business users, and be done with it. This kind of thinking is very common, from the dreams of the secretary building applicaitons by dragging boxes to the business analyst modifying the workflows. I disagree with this quite strongly. Giving a user this type of tool is an irresponsible step at best. The users are not developers, and they will make mistakes. It will be your fault when the server is on 100% CPU because the user has created endless loop. It will be your fault Handing a user a tool like that is wonderful, it is a tool that can enable better communication and rapid response betweenthe IT and the business.

It does not mean that you can just throw it over the wall and walk away. Professional developers are having problems with handling that, giving it to unexperienced users is a mistake. Consider the departmental application and their usual bar of quality. Requirements of a DSL engine. DSL Styles - Imperative vs. Declarative. I posted yesterday about building a rule engine using a DSL. After I wrote that, I tried to think about what style it was. That made me realize that I had no clear distinction between imperative and declarative DSL.

At least not a coherent, internally consistent one. I asked the Alt.net mailing list, and a lively discussion enthused. Contrary to my opinion, a lot of people disagreed with my opinion that this is a declarative style DSL: when User.IsPreferred and Order.TotalCost > 1000: addDiscountPrecentage 5 applyFreeShippingwhen not User.IsPreferred and Order.TotalCost > 1000:suggestUpgradeToPreferred applyFreeShippingwhen User.IsNotPreferred and Order.TotalCost > 500: applyFreeShipping It looks too much like code apperantely. From Chad: about User when Preferred and OrderTotal > 1000: Discount is 0.05 And Shipping is FREE; when not Preferred and OrderTotal > 1000: Upgrade Suggested And Shipping is FREE; when not Preferred and OrderTotal > 500: Shipping is FREE; Based on Berry's ideas: Building an External DSL. Over in the Alt.net mailing list, Glenn said: If I was really being verbose in the way a business user would expect to define this I would say When customer is preferred and the order exceeds 1000 then apply a .05 discount and apply free shipping.

Now do that in a DSL…. Now, I am sucker for this kind of things, and I can probably use the practice. So I decided to go ahead with this. Just to be clear, this is supposedly a natural language processing, only we are going to cheat and get around that. First, let us analyze the structure of the statement that we need to handle: When customer is preferred and the order exceeds 1000 then apply a .05 discount and apply free shipping. I put strike through all the fluff stuff, the things that doesn't really means anything for the language. We have the following keywords: When And Then And the following operators: Is Exceeds Discount Shipping Everything else is a variable. This language follows the following syntax: Now we need to look at the DslExecuter. Language explorations. I blogged about looking at languages a while back.

At that point I didn't know what my next language to explore would be. I got lots of excellent suggestions. In the end I decided to try OCaML, but gave that up quickly when I found out that half of the type system exists to cover up deficiencies in the other half of it. So I went back and decided to learn Scala. So let's get back to the motivation here? I know many people have said this before, but it bears repeating. The first layer is what I called the stable layer.

The second layer is the dynamic layer. The third layer is the domain layer. I think I realized a long time ago that Java is not a good enough language to implement applications. But I need to make one thing clear - I don't believe there will be a winner at any of these layers. I got accused of being "religious" about languages yesterday.