Video of my geek night “Creating a language on the JVM” presentation


The day before QCon 10 days ago, I gave a presentation at the ThoughtWorks geek night, about creating languages for the JVM. It ended up being two hours long, and is now available from Skillsmatter, here: http://skillsmatter.com/podcast/java-jee/language-on-jvm.



IKanServe – an Ioke web framework


The last few days I’ve cobbled together IKanServe, which could be called a web framework, if you’re really generous about the meaning of the words. It doesn’t really do that much right now. But it does allow you to serve dynamic web pages using Ioke.

Much of the hard parts in the Ioke servlet was totally cribbed from Nick Sieger’s JRuby-rack, which is a very well-engineered project.

IKanServe is basically three different parts. First and most importantly, it’s an IokeServlet, that takes care of starting runtimes and pooling them. The second part is the Ioke parts of dispatching, that will turn headers and parameters into a dict, and then find matching actions and dispatch them. The third part is a very small builder library that lets you create HTML with Ioke code. In fact, the code for the html builder could be quite interesting, so let’s start with that. It’s very small, promise!

use("blank_slate")
IKanServe HtmlBuilder = BlankSlate create(fn(bs,
    bs pass = method(+args, +:attrs,
      args "<%s%:[ %s=\"%s\"%]>%[%s%]</%s>\n" format(
        currentMessage name, attrs, args, currentMessage name))))

As you can see, we use a blank_slate library, that is part of the standard library of Ioke. If you’re familiar with Ruby, you know what a blank slate is. Ioke’s blank slate is a bit more blank than the Ruby equivalent, though. To create a blank slate, we call the create method, and then send in a lexical block where we can set up anything we want to have on the blank slate. After create is finished, it will be impossible to add new stuff to the blank slate, so this is the only place we can do that. What we do here is to add a “pass” method. Pass is mostly the same as method_missing in Ruby. That method will create a tag from the method name, add all keyword arguments as attributes, then add all the content inside the tag and finally close the tag. The funky looking syntax in the Text literal is mostly the same as the % or sprintf methods in Ruby.

There is some subtle things going on in this method, but to go into them would derail this whole post. Before abandoning the HTML stuff, you might want to know how to use it. This is how:

h = IKanServe HtmlBuilder
title = "I kan serve HTML!"
h html(
  h head(h title(title)),
  h body(
    h h1(
      style: "font-size: 1.5em",
      "Isn't this cool?"
    )
  )
)

HtmlBuilder doesn’t have any state, so you just use it immediately. I alias it as “h” to make it easier on the eyes. That means that every call to the “h” object will return html. That’s how I can differentiate between creating the tag “title” and referring to the variable “title”. This is very easy stuff, but it seems to work well for smaller cases.

OK. Let’s get back to the rest of IKanServe. What do you need to use it? In your WEB-INF, you need to have three files in the “lib” directory – namely “ioke.jar”, “ioke-lib.jar” and “ikanserve.jar”. Since Ioke is inbetween releases you will have to build these yourselves, but it’s pretty simple. You can build the first two using the target “jar-lib” in the Ioke source. The third can be created by checking out ikanserve from Github: http://github.com/olabini/ikanserve, and then building it with ant.

Once you have those in your WEB-INF, you also need a web.xml file. It should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <context-param>
    <param-name>ioke.max.runtimes</param-name>
    <param-value>2</param-value>
  </context-param>

  <context-param>
    <param-name>ioke.min.runtimes</param-name>
    <param-value>1</param-value>
  </context-param>

  <servlet>
    <servlet-name>IokeServlet</servlet-name>
    <servlet-class>ioke.lang.ext.ikanserve.IokeServlet</servlet-class>
    <load-on-startup/>
  </servlet>

  <servlet-mapping>
    <servlet-name>IokeServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <listener>
    <listener-class>ioke.lang.ext.ikanserve.IokeServletContextListener</listener-class>
  </listener>
</web-app>

Nothing spectacular, just set up the number of runtimes, the servlet and a servlet context listener.

And that’s it. Now you can build a war file and it will be ready to serve. Of course, if you deploy it and try to go to any URL, you will get a message that says: “Couldn’t find any action corresponding to your request. Sorry!”. That’s the default 404 action.

To actually create some code that does something, you need to create a file called iks_application.ik. This file should be places in WEB-INF/classes. Any other Ioke files you want to use should be on the classpath, and you can just use “use” to load those files. What can you do in IKanServe then? Well, basically you can bind actions to urls. Right now there is only one way to bind urls, and that is with regular expressions. A typical small action can be bound like this:

IKanServe actionForPath(#r[^/bar],
  method("you requested this: #{request pathInfo}"))

Here we bind any URL that starts with /bar to the method sent in. At the moment you have access to a request object, and the return value of the method will be rendered as the result. And that’s basically it. As mentioned above, it’s not so much a framework as the initial skeleton for one – but it still works quite well.

If you want to try this out in the easiest way possible, you can do “ant example-app” in the ikanserve source tree, then do “cd jetty” and finally do “java -jar start.jar”. This will build and deploy an example application and then start a Jetty instance to serve it. Go to localhost:8080 and try /foo, /bar or any other URL, and see it in action. You can see the code for this small app in web/WEB-INF/classes/iks_application.ik.

And that’s all for this time. Have fun.



Bottom Types in Dynamic Languages


On the Friday of QCon, Sir Tony Hoare held a presentation about the null reference. This was interesting stuff, and it tied into something I’ve been thinking about for a while. Namely, the similarities and differences in the role of null/nil in static and dynamic languages.

At the surface, a null reference in a statically typed language seems to be a value of a bottom type. Since null is still a value, that cannot be true – since according to the formal definition, a bottom type can have no real value.

To take a typical example of where a bottom type is needed, take a look at what happens in Scala when you have a method that always throws an exception. Of course, that method can never return – so what is the type of the return value? That type is Nothing, the bottom type in Scala. There are no values of type Nothing in Scala, and can never be. The Nothing type is really useful in other circumstances too. Lists in Scala correspond to functional linked lists, where each cons cell is immutable. Every cons cell has a type – this type is the most generic type of the value the cons cell points to, and the type of cons cell it points to. Recursively, this means that the full list will have a generic type that is the least generic type that can encompass every element of the list. But how does this really work for the end element? Every list needs to have a final element that stops the list. This end element generally doesn’t contain a value. So what is the type of that end element? List[Nothing]. The reason being that Nothing can be viewed as the subtype of every other type in the system, which means it is always more specific than any other type. But when you create a new cons cell that points to this end value, the full list will always get the type of the new cons cell.

That explains bottom types, but we are no closer to understanding null references. In a statically typed language, a null reference acts like a value of a bottom type. The reason is that you can assign null to any other type – and this is generally only true for types that are subtypes of the place where it is being assigned to. What is interesting about null in C-like languages, is that there is no type that actually corresponds to null. In languages with null references like this, it seems that the domain of every other type in the system is extended to include null as a value. I haven’t found any reference to how this works from a type checking perspective, but it does look like the bottom type – except that there is no type associated to it. So from now on I will just call null a bottom value. One of the interesting aspects of including a bottom value in your language is that it opens up for runtime errors. If you don’t check every place that uses a reference for the bottom value before using it, you have the potential to get a runtime error.

The reason I started thinking about this was actually to contrast the way null works in a statically typed language, and how it works in a dynamically typed language. But the points above make it more and more obvious that null is actually a runtime feature even in static languages. The type system bypass nulls to allow more dynamism at runtime. But take a look at a dynamically typed language. Of course you won’t have any bottom types, since you don’t have a type system that need to check types. Another feature of most dynamically typed languages is that everything is an expression. In these languages, nil is usually used as a sentinel value to mark that the operation didn’t actually result in any real value. The semantics of how nil is interpreted can depend on both the language and the specific API of the operation in question.

The crucial difference in how nil is handled in a dynamically typed language, compared with how a null reference in a statically typed language works, is one of high level approach. In particular, in a dynamically typed language you will not know what kind of value you get back from an operation. In Ruby you might get back something that is of an expected class, but you can also get something that just looks like that class. Or something totally different, like a recorder object for example. All of these things make nils in dynamically typed languages much less of a problem, since the way you develop in these languages tend to be quite different. In contrast, a null reference in Java is a loop hole, that basically means that anything can return either null or something of the type specified. It’s not exactly a lie, but it is an implicit effect that generally comes as a surprise in a statically typed language.

There is a proposal to add a @NotNull annotation to Java, so you can mark references that shouldn’t allow null. This is not good enough, and will probably not help much at this stage. The reason is that you will be penalized for avoiding null – not the other way around. It also makes code that tries to be correct _more_ verbose, instead of the other way around. Because of backwards compatibility, there is really nothing to do about this though. But I recommend language designers to think carefully before adding a Java-style reference.

Some people asks what the alternative is. What stronger type systems generally allow you to do is have an Option (or Maybe) type, that is genericized on the expected output value.

Say you define a method in Java called get() that will either return a String or a null. If you instead had an Option class in Java, you could define the method as “Option<String> get()”. What is neat about Option is that it’s abstract and it has got two concrete subtypes. The first one is Some, the second is None. This makes it explicit when you can expect a return value to be not be there, and at the same time forces the user of the method to actively handle this case. The default case is references that will always be valid. This a good way to handle this problem in a type system.



The ThoughtWorks Geek Night


Last Tuesday ThoughtWorks hosted a Geek Night, where I was the headline. My presentation was about Creating languages for the JVM, which as it turns out is a quite broad topic. The London office do these kind of geek nights now and again, and this time we ended up being about full capacity for what the office could hold, which is about 50-60 people. It was really crowded, actually. That is a good problem to have, though. I am glad so many turned up to see me do a two hour rambling presentation that ended up being like a non-stop fire hose of information.

I’m not going to try to summarize the actual presentation. There were way to much information in it for that. Luckily, SkillsMatter had a film camera there, and I’ve been told the video should be up anytime. Once that happens I will post about it here. In the meantime, the slides can be found here: http://olabini.com/presentations/CreatingALanguageForTheJVM.pdf.

It was an interesting subject to present about though. I think this material could easily be a one-day tutorial. I wonder if anyone would be interesting in having such a tutorial at their conference? And whether anyone would show up for such a subject…

It was fun, though. I had a great time, and I hope the people who showed up didn’t end up being disappointed with the material.



QCon London – Summary


All in all, QCon London this year was amazing. I find it interesting that from the first time I attended QCon I thought they were exceptionally good. And every time they keep getting better. Of course, it is fantastic to be able to meet all these great people at the conference, and you get lots of chances to hang out with them, ask questions and have discussions. But if you take a look at the presentations offered, they all feel very fresh and the quality is consistently of a very high level.

I think the system of having track hosts that put together their own track is a fantastic idea, and I think it might be one of the reasons that there are ALWAYS more than one presentation going that you want to see.

The fact that the QCon crew from InfoQ and Trifork are all lovely people is not a negative either.

If you haven’t visited QCon, I really think you should. It is really very good, and I’m privileged to have been asked to present there more than once. I always have a great time and I hope I will be able to continue to come back there.



QCon London – Friday


The last day of any conference can sometimes get a weak schedule, since many organizers feel that people have a tendency to be wiped out after all the previous days of conference, and won’t be able to focus as much on the last day. Luckily, QCon and JAOO never does this – the last day is generally at least as strong as any other day. This time around I felt it was a very good day indeed. I saw Stefan Tilkov do a very deep presentation on his thoughts on the balance between generic and specific in architecture and development. Stefan shared his long experience in a funny and thoughtful way that contained lots of interesting insights. After that I managed to catch the last 10-15 minutes of Joe Armstrongs talk about Erlang. This talk was focused on systems that never stop, and Joe’s presentation was based on this, which changed the focus a bit. Sir Tony Hoare was in the audience during this talk, and one amusing moment was when he asked Joe a question about the tradeoff between timeouts and failing fast. Great stuff. This only happens at QCon and JAOO. (As an aside, the evening before I got the chance to sit down and have beers with Steve Vinoski, Joe Armstrong and Rich Hickey. This never happens at other conferences either.)

After the presentation, Hoare asked me if I was interested in being part of a panel during his after-lunch presentation. The presentation was about null references, which Hoare calls his billion dollar mistake. To be fair, I think it was one of those inevitable things, that would have happened sooner or later anyway. Ulf Wiger became part of the discussion, and presented a much better defense than my point about inevitability.

In the DSL track, Amanda Laucher gave a good overview about what Oslo is and why you might want to use it. What I was looking for here was just enough of a look at the tech to see if this is something worth investing more time in. My conclusion was that at this point in time, it doesn’t seem to give me much new that would help me. Especially the Mgrammar stuff seemed to be a not-invented-here copycat of Antlr. Martin corrected me about that, though, by pointing out that Mgrammar actually handles GLR grammars. My intuition is that ANTLRs LL(*) would be able to handle most of the same parses, but that’s not something I would bet money on. Mgrammar also doesn’t need semantic predicates to avoid left recursion, which can make the grammars more readable.

The last time slot of the day I spent doing some hacking, and peeking in at Glenn Vanderburgs presentation on DSLs in Ruby, which gave a good overview of the available techniques.

The day ended with the totally hilarious Bullseye panel, where my totally insane colleagues Dan North and Jim Webber hosted a gameshow combining darts and zany questions to a panel of Michael T Nygard, Ian Robinson, Martin Fowler and Steve Vinoski. To many great moments to even start to describe, but if I had to choose one, it would probably be Ian Robinsons answer to the question about what tech has the worst man boobs. Ian’s response basically said that the problem wasn’t the man boobs, but that he couldn’t actually get close enough to touch them without lots of clothes getting in the way. Lovely metaphors there.



QCon London – Thursday


I started the second day quite late. There were a few sessions I wanted to see there but not enough to get out of bed early enough. After lunch I saw parts of David Pollaks talk on Lift. Lift is definitely impressive – you can achieve very powerful things very quickly with it.

After that, I saw Rich Hickey talk about Persistent Data Structures. This was without doubt the best presentation at QCon this year. Rich spent lots of time talking about identity, state and value. It was very good – you should go to http://qconlondon.com/london-2009/schedule/thursday.jsp and take a look at the slides for it. It is pretty deep stuff. I heard many other people share my opinion of the quality of this talk. QCon and JAOO could do much worse than getting Rich back for the next time.

Overall, I spent most of the Thursday having chats with interesting people instead of watching presentations. It was a good day in many ways, especially due to Rich’s talk.



QCon London – Wednesday (Emerging Languages)


The first day of the proper QCon conference started out with Sir Tony Hoare doing a keynote about the difference and overlap between the science and engineering of computing. Fairly interesting, but the questions and answers were much more interesting stuff. One of the more interesting points made by Hoare was that in his view, a full specification is a generalization of testing. After the keynote I started out my track called Emerging Languages in the Enterprise. I introduced this track, doing 15 minutes of talking about my views on programming languages. The slides for my piece can be found here: http://olabini.com/presentations/ELITE.pdf. My talk was made much more interesting by Tony Hoare being in the front row. That made the whole thing a bit more daunting, obviously… =)

I then spent the rest of the day in my track – which was very good. I am very happy with all the presentations, and felt the track was a great success. First of was Michael Foord, talking about IronPython, and how Resolver uses IronPython to create a great product. Some interesting lessons and information there.

After lunch Jonas Bonér talked about Real-world Scala. The presentation gave a good grounding in Scala without looking at all small details – instead Jonas talked about more high level concerns and styles.

After that, Rich Hickey did a great presentation about Clojure. Rich did a great presentation, talking about Clojure from the ground up. It was very well received.

Martin Fowler did a fantastic presentation on ThoughtWorks experience with Ruby. The room was packed for this.

The final presentation in my track was Attila Szegedi talking about JavaScript in the Enterprise. This was also a great presentation, and gave me some new insight into what you could achieve with Rhino.

All in all, the full track was excellent, and all the presentations achieved pretty much what I hoped from them. I learned a lot from all of them.

After the final session of my track, Martin Fowler and Zach Exley did the evening keynote, talking about how technology helped the Obama compaign. Very interesting stuff too. At the end of the day, a very good day at QCon.



QCon London – Tutorials


The first two days of QCon London were tutorials. I didn’t actually spend all my time there, since I had to get back to our office at some times too. On the Monday, I attended Jim Webbers tutorial called GET connected, which talked about different ways of using the web for distributing and separation functionality, where REST style SOA is part of it, but also other things like POX. It was a very information rich session, and in the end Jim had to skip several things that I would personally have been interested to find out more about, such as different ways to handle security, and so on. At the end of the day, it was a very good tutorial, but the material was really too much for one half day.

I spent the rest of the day in the office, reconnecting with lots of colleagues. Lovely.

Tuesday I spent running between several tutorials. The morning was mostly in the Advanced Ruby tutorial by Sam Aaron, which was overall very good content. I think I might have ruined it for Sam a bit by sitting there and being annoying. The only thing that could have improved the tutorial would have been some more advanced material in the pieces on functional programming – not that the material Sam was using was bad, it was just a bit more basic than the rest, which made it feel a bit uneven.

In the afternoon, I spent some time in Dan Norths tutorial on BDD. His material is riveting and very useful. In fact, it was so riveting it was hard to find a place to sit, so I ended up not staying the whole time. I then sat down for a while in Joe Armstrongs tutorial on Erlang. Also good stuff, but it was material I already knew. So at the end of the day I went back to Sam Aarons talk on Ruby Aesthetics, which ended up sparkling several really interesting discussions.



QCon London and Ioke Geek night


In one and a half week, QCon London is coming up. That will be a week of great fun, and I definitely recommend it to anyone who has the possibility to be there. I have the highest respect for Trifork and InfoQ who are organizing the QCon conferences, and I’m definitely looking forward to it.

This year I am actually not speaking at the conference itself. Instead I’m hosting a track where other people speak. My track is called “Emerging Languages in the Enterprise” and I’ve managed to convince some amazing people to show up and talk there. To be specific, my speakers are: Jonas Boner, Michael Foord, Attila Szegedi, Rich Hickey and Martin Fowler. So, if you are interested in languages, be there; it will be a blast.

ThoughtWorks UK have a tradition to organize geek nights around QCon, and this time is no exception. On the evening March 10th (that is a Tuesday) I will be talking about how to create a JVM language. I will use Ioke for most examples, but will also contrast with details from JRuby. The presentation will be quite audience controlled, so exactly what will be presented is not determined. But it should give people a good grounding in Ioke, at least. More information here. Hope to see you there!