My last blog entry was on my mind quite a bit last night. I kept coming back to the same questions - when are narrative languages appropriate? And when are they overkill?

I started to think a bit about some of great APIs I've seen that live in a completely different world. One is an embedded DSL for music composition from Paul Hudak's book The Haskell School of Expression. Here's a little example from the book:

    funkGroove
        = let p1 = perc LowTom qn
              p2 = perc AcousticSnare en
          in Tempo 3 (Instr Percussion (cut 8 (repeatM
                  ((p1 :+: qnr :+: p2 :+: qnr :+: p2 :+:
                    p1 :+: p1 :+: qnr :+: p2 :+: enr)
                   :=: roll en (perc ClosedHiHat 2))
             )))

It is a little scary at first glance, but it becomes clearer once you know that en is eighth note, qnr is quarter note rest, and the symbols :+:and :=:are sequential and parallel composition; i.e., play after another phrase and play at the same time as another phrase, respectively.

The thing that is very cool about this DSL is that you can easily format your code to see phrases above or below phrases that will play at the same time.

The variable names p1 and p2aren't very informative, but I do think that they aren't quite as shocking as they would be in a conventional program. This DSL is completely about structure. It's about building up a piece of music and being able to tell, at a glance, whether it is what you want it to be. The abbreviations for notes and rests actually look like they work well within it. It pains me to say that, because I usually hate abbreviations with passion.

No, it's hard for me to imagine a narrative, fluent, or English-like API for this problem that I would like better than the symbolic approach. I guess it has something to do with the domain. People don't talk about how one note follows another as much as they listen for notes or see them on a page. For people who read music, it is a very spatial domain.

But, maybe it's more than that. Maybe the reason why the symbolic approach works so well here is because these programs are about structure. The order and arrangement of the notes has a very direct meaning. Other domains like time and money, are less structural and more semantic, so it seems that they might be more suited to the narrative approach.

Perhaps the choice between narrative and symbolic is more than just style?