Jules Jacobs
Posts: 119
Nickname: jules2
Registered: Mar, 2006
|
|
Re: How and When to Develop Domain-Specific Languages?
|
Posted: Mar 10, 2006 9:02 AM
|
|
I have tried Rails, and I don't know if it is a DSL. DSL means domain-specific-language. So that's a language. If you're used to Java, C#, or another static language it probably looks like a DSL. If you're used to python, it will look more like a group of classes and functions, with some other weird things. If you're used to Ruby, you'll know that this is not a DSL, but just a library like any other. In TCL, Lisp, and many other languages you can create APIs that look even more like a separate language.
For example, the Ruby-Make "DSL" in a C#/Java Like language:
using task;
public class ATask : Task { public void execute() { doStuff(); } }
----------
using task;
class TaskApplication { public static void Main() { Task.addtask(new ATask()); Task.runAll(); } }
In Ruby:
task :a_task do do_stuff() end
In Lisp:
(task a-task (do-stuff))
The first example doesn't look like a DSL because you can clearly see this is not a new language. There are too many aspects (such as a class, a main function, etc) that are not domain specific. The most common way to handle this is to create an xml "dsl", or configuration. A static language tries to be a dynamic one ;-). The ruby version looks more like a domain specific language, but there are some things a Ruby programmer would recognize: the "do" and "end" keywords, and the syntax to create a symbol: ":a_task". In the Lisp code, there are no keywords. The domain specific language is truly domain specific. The only weird things are the (()). But unfortunately, the parens make it possible to create a DSL.
class Posts < ApplicationController def show code() end
def new code() end end
vs:
(controller posts (action show (code)) (action new (code)))
This is more domain specific, because it contains no language keywords (def, class, end).
Or maybe a model from django:
class Post(meta.Model): author = meta.ForeignKey(Author) title = meta.CharField() content = meta.TextField()
or:
(model post (belongs-to author) (attributes title string content text))
So what makes an api a DSL? This is not entirely clear, because most DSLs will have some non DS aspects, and you can't say: "if an api contains less than n non DS aspects, it is a DSL". I think most people will agree that my first Java/C# like examle is not a DSL. But is the Django model a DSL?
DSLs are about abstractions. You can create better DSLs in languages with better ways to abstract. Many languages support procedural abstrations (FORTRAN, Python, Ruby, Lisp). A lot support object oriented abstractions (Python, Ruby, Lisp). A few support functional abstractions (Ruby, Lisp). Even less languages support syntactical abstrations (Lisp).
(note that because these languages are turing equivalent, you could write a lisp interpreter in FORTRAN, and have all these abstractions).
So "How and When to Develop Domain-Specific Languages?"?
How:
By choosing a language that supports powerful abstractions.
When:
Just create an appropriate abstraction for difficult things.
If you notice that you want to generate a form, and you're doing it this way:
(write-html "<form> <input type="text" name="title" /> <textarea name="content"></textarea> <input type="submit" value="save" /> </form>")
Create an abstraction:
(form (text-field :title) (text-area :content) (submit-button "save"))
Or:
form { text_field :title text_area :content submit_button "save" }
Or (in Java/C#):
// form.xml <?xml version="1.0" ?> <form> <text-field name="title" /> <text-area name="content" /> <submit-button caption="save" /> </form>
and in your code:
parseFormFromXMLAndPrintResult('form.xml');
Or, offcourse:
// form.lisp (form (text-field :title) (text-area :content) (submit-button "save"))
and in your code:
parseFormFromLispAndPrintResult('form.lisp');
To make it easier next time.
|
|