Just A Summary : Tag pidgin, everything about pidgin http://www.bofh.org.uk/articles/tag/pidgin.rss en-us 40 Piers Cawley Practices Punditry Martin Fowler's big mouthful <p>Martin Fowler is writing a book about <a href="http://martinfowler.com/dslwip/">Domain Specific Languages</a> and, because you could never accuse Martin of a lack of ambition, he&#8217;s trying to write it in a reasonably (implementation) language agnostic fashion.</p> <p>It&#8217;s fairly easy to write an implementation language agnostic book about old school DSLs, what used to be called little languages &#8211; there&#8217;s a fairly well established literature and theory to do with lexing, parsing and interpreting. These are all about algorithms, and algorithms are implementation language neutral by their very nature.</p> <p>Where Martin has his work cut out for him is trying to talk about what he calls &#8216;internal DSLs&#8217; and what I&#8217;ve been calling &#8216;pidgins&#8217;. These are the sorts of languages where you don&#8217;t write a lexer or parser but instead build a family of objects, methods, functions or whatever other bits and pieces your host language provides in order to create a part of your program that, while it is directly interpreted by the host language, feels like it&#8217;s written in some new dialect.</p> <p>The Lisp family of languages can be said to be all about this. A good &#8216;bottom up&#8217; lisp programmer will shape a language to fit the problem space, essentially building a new lisp which makes it easy to solve the problem at hand. Lisp&#8217;s minimal syntax, powerful macros and the way it blurs the boundary between code and data really support this style.</p> <p>Once you move from Lisp to more &#8216;syntaxy&#8217; languages, things get hairier. As Martin himself <a href="http://martinfowler.com/bliki/BookCode.html">says</a></p> <blockquote> <p>Another issue with book code is to beware of using obscure features of the language, where obscure means for my general reader rather than even someone fluent in the language I&#8217;m using. [...] this is much harder for a <span class="caps">DSL</span> book. Internal DSLs tend to rely on abusing the native syntax in order to get readability. Much of this abuse involves quirky corners of the language. Again I have to balance showing readable <span class="caps">DSL</span> code against wallowing in quirk.</p> </blockquote> <p>He&#8217;s dead right. When I&#8217;m thinking about writing a pidgin in Ruby for instance, my first thought is usually to start with some kind of tabula rasa object which I can use to <code>instance_eval</code> a block. That lets me start to shape my language by lexically scoping the change:</p> <pre><code> in_pidgin do ... end </code></pre> <p>But, though it&#8217;s easy to illustrate what I&#8217;d <em>do</em> with my tabula rasa, the implementation is somewhat tricky, and the tricks needed are unique to Ruby.</p> <p>That sort of construct&#8217;s not really available to someone trying to write a pidgin in Java or Perl. In Perl, there are other odd corners of the language that can be abused to good effect. Dynamic scoping can let you &#8216;inject&#8217; methods into a block even though there&#8217;s no Perl equivalent to <code>instance_eval</code>, or you can do some quite staggering things with the otherwise really annoying Perl function prototypes. For instance, here&#8217;s part of a <a href="http://jifty.org/">Jifty</a> definition of a persistent object:</p> <pre><code> column title =&gt; type is 'text', label is 'Title', default is 'Untitled post'; column body =&gt; type is 'text', label is 'Content', render_as 'Textarea'; </code></pre> <p>Doesn&#8217;t look much like Perl does it? But it&#8217;s parsed and executed by perl with no source filters or <code>eval STRING</code> in sight. And there&#8217;s no unsightly <code>:symbols</code> scattered about the place either come to that.</p> <p>These things all work by making the language do something unexpected, and generally, the way to do that is by knowing your host language inside out and playing with it. One of Damian Conway&#8217;s more inspired moments in recent years was <a href="http://search.cpan.org/~dconway/List-Maker-v0.0.3/lib/List/Maker.pm">List::Maker</a>, in which the good doctor managed to find a corner of Perl where he could wedge a proper old school, complete with full on parser to build the <span class="caps">AST</span>, Little Language right in the heart of Perl without it <em>looking</em> like he was taking a plain old string and interpreting it. So, having found this odd little corner, he proceeded to implement a remarkably neat tool for building complex lists that are beyond the capabilities of Perl&#8217;s <code>..</code> operator.</p> <pre><code> @odds = &lt;1..100 : N % 2 != 0 &gt;; @primes = &lt;3,5..99&gt; : is_prime(N) &gt;; @available = &lt;1..$max : !allocated{N} &gt; </code></pre> <p>You may not think that&#8217;s all that sexy, but, and trust me on this, it&#8217;s just <em>gorgeous</em>. Yet more proof that Damian Conway is an (evil) genius.</p> <p>Frankly, once you&#8217;ve seen the best of the pidgins available in Perl, some of highly praised &#8216;DSLs&#8217; in Ruby start to look a bit ordinary. Ruby makes a great deal of stuff that a pidgin breeder needs to do really easy. In Perl it&#8217;s often rather hard with a huge amount of hoopage to deal with. But some of the things that are hard in Perl are impossible in Ruby.</p> <p>Anyhoo&#8230; coming back to my point. I do find myself wondering if Martin&#8217;s bitten off more than he can chew in attempting to write a book that covers implementing pidgins without getting bogged down in the nitty gritty of individual languages. The problem he&#8217;s facing is that different languages don&#8217;t just have different quirks, they have different idioms too. What reads naturally in the context of a Ruby program will read very weirdly in, say Java or a lisp. Any patterns of implementation beyond broad (but important) strokes like &#8220;Play to your host language&#8217;s strengths&#8221; will surely end up as language specific patterns. Designing and implementing a good pidgin is <em>hard</em>. Doing it effectively means getting down and dirty with your host language and its runtime structures. And that&#8217;s not the sort of thing you can cover effectively in a language agnostic book.</p> <p>Martin, if you&#8217;re reading this, good luck. I think you&#8217;re going to need it. I look forward to being proved wrong.</p> Fri, 18 Jan 2008 17:19:00 -0600 urn:uuid:85fbce77-14bf-44e5-ac34-b61f925d54b0 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2008/01/18/martin-fowlers-big-mouthful#comments Musings The Practice of Programming pidgin http://www.bofh.org.uk/articles/2008/01/18/martin-fowlers-big-mouthful