## Monday, December 28, 2009

### Bridge-crossing with Clojure

Solving puzzles has become popular among colleagues at the end of this year. One of these was the popular Bridge and torch problem. To summarize: four men have to cross a bridge in the dark with a torch, which can only light the way for two persons. These guys can cross the bridge in 1, 2, 5 and 10 minutes, respectively. When two people cross the bridge, it takes the time needed for the slower one to pass.

I'm not going to explain the optimal solution, but I wanted to show how this is a graph-traversal problem, which is amenable to solving using Dijkstra's algorithm. I also noticed that some clever guy has solved the puzzle using Prolog. So an idea was formed: generate the tree of solutions using a language, which I'm not really comfortable with (so I can practice and learn it better). I settled on Clojure, because I wanted to learn it this year (and the year is almost over).

The solution has a lot to do with sets. The state of each of the banks (which I'll call left and right) is a set of people, which are identified by their crossing speed. If we generate the list of possible combinations between the persons for each next move and recurse into them, then we'll have the tree of all solutions. In order to avoid indefinitely long (and stupid) solutions, we assume that going forward always takes two men, and going back takes only one.

The necessary libraries are set, combinatorics and str-utils. That's where I start:

`(use '[clojure.contrib.combinatorics])(use '[clojure.set])(use '[clojure.contrib.str-utils])(use '[clojure.contrib.pprint])`

Here are the initial data structures: the two river banks are sets, the right one is empty:

`(def left #{1, 2, 5, 10})(def right #{})`

And here's a first version of the solution:

`(defn forward [left right steps minutes]        (map #(back                (difference left %)                (union right (set %))                (cons % steps)                (+ minutes (reduce max %)) )            (combinations left 2)) )(defn back [left right steps minutes]    (if (empty? left)        (list steps minutes)        (map #(forward                (union left (set %))                (difference right %)                (cons % steps)                (+ minutes (reduce max %)) )            (combinations right 1)) ) )(pprint (forward left right nil 0))`

This prints some semblance of a tree, constructed by nested lists (hey, that's what Lisp is good for, remember?). What I didn't like about this is that there is too much repetition. There are many parts of these functions, which look the same. In order to evolve the solution, I wanted to extract the common parts in a single function:

`(defn solve [from to steps minutes group]    (if (and (empty? to) (not group))        (list steps minutes)        (mapcat #(solve                (union to (set %))                (difference from %)                (cons % steps)                (+ minutes (reduce max %))                (not group) )            (combinations from (if group 2 1))) ) )(pprint (solve left right nil 0 true))`

The function behaves differently depending on whether the group of people crossing consists of two or just one person. Also, the tree is now flattened, so the optimal solution can be easily found like this:

`(print (reduce #(if (< (second %1) (second %2)) %1 %2) (apply hash-map (solve left right nil 0 true))))`

Next step, I want to be able to collect a lot more data about each state, so I need to stop messing around with lists of lists and use something more organized like a hashmap. There's a reason Clojure includes the function second, but not third or, heaven forbid, fourth. Naming each part of your ad-hoc data structure plays a major role in readability.

`(defn solve [from to steps minutes group]    (if (and (empty? to) (not group))        {:steps steps, :from from, :time minutes}        {:steps steps, :from from, :time minutes,            :solutions (map #(solve                    (union to (set %))                    (difference from %)                    (cons % steps)                    (+ minutes (reduce max %))                    (not group) )                (combinations from (if group 2 1)))} ) )`

So now the interesting part can begin- rendering the tree in a nice graphical form using GraphViz. Watch this:

`(defn printgraph [solution_tree]    (let [steps (solution_tree :steps)        nodename (if (nil? steps) "root" steps)        from (solution_tree :from)        label (if (= (count from) (count left)) (solution_tree :time) from)]        (str "\"" nodename "\" [label=\"" label "\"];\n"            (if (contains? :solutions solution_tree)                ""                (str-join ""                    (map #(str "\"" nodename "\" -> "                            "\"" (:steps %) "\""                            " [label=\"" (intersection from (:from %)) "\"];\n"                            (printgraph %))                        (solution_tree :solutions)) ) ) ) ) )`

And now you can output the recursive stuff wrapped with the header and footer like this:

`(println "digraph G {")(print (printgraph (solve left right nil 0 true)))(println "}")`

This graph shows each node as the state of alternating river banks, and the edges show the people crossing the bridge. The leaves display the number of minutes which it took for this particular combination of crossings.

I also wanted to generate a mind map for FreeMind, but since it doesn't support edge labels, I used the persons crossing for the nodes:

`(defn mindmap [solution_tree]    (str-join "\n"        (map #(str                "<node text=\"" (intersection (:from solution_tree) (:from %)) "\">"                (mindmap %)                "</node>")            (solution_tree :solutions)) ) )`

Now generating the complete file according to the FreeMind format:

`(println "<map><node text=\"root\">\n")(print (mindmap (solve left right nil 0 true)))(println "</node></map>\n")`

Finally, finding the optimal solution is just a wee bit more involved, because it requires traversing the tree recursively, but that's no problem for us anymore:

`(defn optimal [tree]    (if (contains? tree :solutions)        (reduce #(if (< (second %1) (second %2)) %1 %2) (map optimal (tree :solutions)))        (list (tree :steps) (tree :time)) ) )(print (optimal (solve left right nil 0 true)))`

In conclusion, I knew that one can write very compact code in Clojure (and any Lisp, in general), and this was certainly true here. I didn't use the Clojure-specific concurrency primitives or the lazy sequences, so my example would probably look similar in another Lisp. I did try to make it as functional as possible and Clojure does encourage that.

Some of the difficulties I had could be solved by static type checking, but I guess I'm spoiled by Scala. I've ran into small problems like renaming a variable and forgetting to rename (or misspelling) some instance of it in my code. I also tend to mix up which of the arguments to contains? comes first, and which second. This is probably because you can exchange a symbol as a key in a hashmap, and the hashmap without changing the meaning, e.g. (:from solution_tree) and (solution_tree :from). Of course, this is a problem with dynamic typing, not of Clojure in particular (and could probably be interpreted as a problem of using the language inappropriately).

Whatever the small hurdles, the problem was solved. Having fun? Check. Generating nice graphs and mind maps to impress colleagues? Check. Learning a bit of Lispy Clojure in the process? Check. Mission accomplished.

## Thursday, December 3, 2009

### String interpolation in Scala

When I tried Scala for the first time, one of the first missing language features I noticed was string interpolation (you guessed it, I was using Ruby at the time). Of course, this is a small convenience rather than a major language feature and usually String.format and java.util.Formatter are pretty good substitutes. Still, string interpolation comes now and then in Scala discussions and one has to wonder if Scala's powerful language extension facilities can emulate a feature like this.

It turns out you can get reasonably close:

`trait InterpolationContext {  class InterpolatedString(val s: String) {    def i = interpolate(s)  }  implicit def str2interp(s: String) = new InterpolatedString(s)  def interpolate(s: String) = {    val sb = new StringBuilder    var prev = 0    for (m <- "\\\$\\{.+?\\}".r.findAllIn(s).matchData) {      sb.append(s.substring(prev, m.start))      val matchString = m.matched      var identifier = matchString.substring(2, matchString.length - 1)      try {        val method = this.getClass.getMethod(identifier)        sb.append(method.invoke(this))      } catch {        case _: NoSuchMethodException =>        sb.append(matchString)      }      prev = m.end    }    sb.append(s.substring(prev, s.length))    sb.toString  }}object Test extends InterpolationContext {  val a = "3"  def main(args: Array[String]) {    println("expanded: \${a}; not expanded: \${b}; more text".i)  }}`

How does this work? If you call the i method on a String, it will force implicit conversion to a InterpolatedString, similar to how the r method works for converting to a Regex. The interpolate method uses reflection to find a method with the name equal to the identifier delimited by "\${" and "}". This works both for vals and defs due to Scala's adhering to the uniform access principle. If the delimited string is not a valid identifier or a parameterless method with this name doesn't exist, it is not substituted, but stays as it is.

How do you use it? Just extend or mix in the InterpolationContext trait in the class where you want this to work, and then call i on the Strings where you want interpolation to work. The InterpolationContext trait serves two purposes- first of all, it imports the implicit conversion to the interpolated string, and second it provides access to the methods of the current object via reflection.

The limitations of this method are that interpolation only works on member vals and defs of the current object only. I rather like this, because you know you can't accidentally construct a String from user input in an html form like "Name: \${System.exec(\"echo echo pwned >> .bashrc\")}". Also, interpolation doesn't work for private members as well as local variables. Finally, you have to both mix in or extend the trait and call a method on every String (even though it's a one-letter method). This is not too bad, because you can control the scope where interpolation works and therefore avoid and debug nasty surprises easier.

I don't see this as a pattern, which can be used in real-life projects, and I don't see the use of implicits here worthy of emulation. Nevertheless, trying to copy features from other languages is instructive about the limitations of your current language of choice and the tradeoffs you get for these features. Last but not least- even if it's a gimmick feature, implementing it was fun.