Tim Bray presented his 11 Theses on Clojure. Thesis number 2 is “Being a Lisp Is a Handicap”. This argument has actually 3 parts: the Lisp-parenthesis syntax, which he finds bad. Homoiconicity (he finds good) and macros (again good).

I would like to discuss a bit the first (syntax) and the last (macros). The macro part discussed somewhere in the future. For now I would like to concentrate on syntax.

Lisp syntax is strange for most humans, we simply do not write (+ (* 2 3) (/ 4 2)). Lisp proposers are probably aware that this will not be very popular, as people are used to something completely different. It is not only different but is also very uneconomical to both read and write: 2*3 + 4/2 is arguably easier to read. One can even make some intuitive declarative things. Notice that I’ve written 2*3 (no space around *) but 2*3 + 4*2 (spaces around +). This allows to give queues to readers about the precedence of operators. You can say that these issues are just simple and ridiculous syntactic sugar. Well, it seems that Homo sapiens has evolved to appreciate it. And I don’t really see a reason not to accommodate this requirement.

Yes, I read you already: infix syntax forces hard-coding of operators and precedences (e.g. + precedes *) . Makes parsing much more complex (there goes the beautifully simple Lisp parser out of the window) and in fact forcing a certain structure with hard-coded operators.

Well… no.

Take a look at Prolog op built-in. :- op allows you to define operator precedence and association rules. So the parser is really not much complex: it starts with a simple, uber-flexible, blank slate which you can fill in.

So, for (- 4 3 2) you can say – is an infix operator with first argument with precedence over the second (i.e. (4 – 3) – 2) and, voila, you can write 4 – 3 – 2. No need for hard-coded operators, all dynamically defined. You could, maybe for sadistic reasons, change the argument precedence so 4 – 3 – 2 would become 4 – (3 – 2). The flexibility is there.

You can also state that + and – have higher precedence than * and /. So that 2*3 + 2 is interpreted as (+ (* 2 3) 2). Again, you can change the precedence rules.

Ah… and you can have operators that are atoms (alphanumeric symbols). In Prolog you can say “X is 3 +2″. “is” is an operator. You can redefine operator rules, define new operators.

Operators are nothing more than a set of syntactic sugar rules to convert a more intuitive representation to another. But they really make things much more pleasant and humane.

I would imagine that adding this to Clojure would be DEAD EASY, by the way.

Regarding the issues about macros, I will leave it for another post.

Social network sharing
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • LinkedIn
  • connotea
  • FriendFeed
  • Twitter
  • Yahoo! Bookmarks

4 Comments

  1. E[X] says:

    Actually, it isn’t easy to do in clojure because it lacks reader macros.
    However infix syntax on lisp has been attempted many times and it never became mainstream, probably because the code that really benefits from it is ends up being much less common than you intuitively think it is and the abuntant number of parenthesis are there because of the deeply nested style lisp programmers (myself included) tend to favor.

  2. tiago says:

    Thanks for the comment. Note the following, though: The Prolog type of solution DOES NOT IMPOSE infix notation. It can accommodate both types of syntax.

    +(2,3,X) – equivalent to (+ 2 3)
    2 + 3.

    Note full flexibility and freedom to choose your style with the :- op built-in . And at very little cost in parser complexity. No hard-coded operators at all.

  3. jneira says:

    The op flexibility (op infix == function prefix) reminds me haskell, i suppose haskell borrow it from prolog (directly or not). But it seems that the compiler has to be necessarily more complicated and you have to keep in mind the precedence rules (even if your have created or changed the rules!). I am sure that (for other features of Clojure) Hickey built clojure with Haskell-Ocaml in mind and chose a unique way for good reason. Anyway reader macros are discouraged but it is possible to use them (trough java or pure clojure code)
    See:
    http://fulldisclojure.blogspot.com/2009/12/how-to-write-clojure-reader-macro.html
    http://briancarper.net/blog/clojure-reader-macros

  4. Ross Judson says:

    I like the prolog operators, and how they can make things quite readable. Looking at the clojure code, it doesn’t look like it would be very difficult to insert an “operator transformation” immediately after macroexpand, in Compiler.

Leave a Reply