Programming languages are about people

Programming languages are more about people and less about machines.

Programming languages are about staying inside the limitations of people’s minds and their ability to keep track of and work with abstractions. If people had no such limitations, they could code in assembly language all the time.

Programming languages and supporting tools and environments are the interface between people and the raw instruction set of a computer (or a bigger entity, like a network of computers). When we design programming languages, we must take into account not only what the machine environment will do and how it changes, but also how people create the software, how they modify it, how they think about it. Maybe programming languages should even be designed with business processes in mind, in some cases.

But this is also a question of what kind of programmer we want to cultivate. The language shapes the programmer, too.

Basic research in the UK

The Guardian reports that a new government panel will henceforth judge what research is worthy of funding in the UK. Universities will have to make the case for their research projects in order to receive money. Reuters UK, perhaps keen to draw attention, blurt out that “‘Mickey Mouse’ degrees face [a] funding battle”.

Examples cited by Reuters UK include surf science, golf management and winemaking. I agree that these are probably vocational qualifications rather than fields meriting university study. But for blue skies projects in the natural sciences or the humanities, the payoff and effects on society are very hard to judge in advance. After all, we very often conduct the research precisely to evaluate these benefits.

The incentive situation with basic research is different today from what it was during the cold war era. When basic research was a national affair, not to be shared freely in the scientific community, it was probably possible to gain a national advantage by investing more in basic research. Today it’s all too easy to make the argument that other countries will reap the benefits, so why pay for the investment? Essentially a reverse prisoner’s dilemma: out of selfishness, you are tempted not to invest, but everybody benefits more if everybody invests. But surely this is too simple a view of the situation.

Where will countries that cut down on basic research be in the league tables of the future?

Bibliography tools (2) – Mendeley

Following a comment on my previous foray into bibliography management systems, I had a look at the product known as Mendeley.

mendeley

In order to evaluate Mendeley, let’s ask ourselves what we want from a bibliography management system in the modern research environment. At a bare minimum, we want an easy way to catalogue and search PDF documents, and of course compile the all-important reference list at the end of the laborious writing process. Mendeley does this, as well as bring a social networking aspect into the picture. It tries to recommend papers that are relevant to your work, as well as give you an easy way of sharing interesting papers with colleagues.

In contrast to Aigaion, which I wrote about previously, Mendeley is not a web based system but a desktop application. This definitely has benefits as the interface is quite slick. I can set the application to watch my “papers folder”, and any PDFs I save to that folder, or its subfolders, will automatically be scanned and entered into Mendeley. Metadata, such as author, title and references, is automatically extracted from the document in most cases, though I found I had to manually revise it sometimes. There’s a built in command that searches for the metadata by paper title on Google Scholar, which comes in very handy in such cases.

Mendeley is built around an internal PDF viewer where the user can highlight text, add little stickies with notes, and so on. This works quite smoothly, but on the Mac platform, it’s definitely not as polished as the Mac’s built in Preview PDF viewer. Mendeley is using its own PDF rendering layer, and it shows in the slower loading times when you scroll the documents. Some additional work could be done here. This is my only major complaint so far, though.

Much like the Evernote application, Mendeley has the option of storing all the papers on a central server, so that I can easily access them (and any annotations I might have made) from a different computer by signing in with my user name and password and then syncing the files. This means I don’t have to give up the benefits I get from a centralized server. It might be nice, however, to have the option of running my own Mendeley server, so I’m not dependent on the Mendeley company’s server somewhere – but then I would forgo the social networking benefits of course.

This application has similarities to how last.fm is used for music, in that people build a profile based on what they consume. Indeed, Mendeley is describing itself as a last.fm for research (video presentation). Let’s compare research and music as forms of media.

  • Most music listeners probably don’t make their own music – most people who read research papers probably write their own papers.
  • Songs sample other songs (the remix culture), but it’s relatively recent – researchers have always done this in order to establish basic credibility.
  • The atomic unit of music is the song. The atomic unit of research is the research paper (the PDF in today’s internet based world, at least in my discipline) – but could this change in the future? Do we have to constrain ourselves to the article format?

In summary, Mendeley is probably the most useful, workflow friendly bibliography system I’ve tried so far. If you’re in research, I’d recommend you give it a try. If I get time, I plan to also investigate a more Mac-centric tool called Sente in the future.

The Savage Minds blog recommends that you don’t use Mendeley as your main tool yet due to its relative immaturity, but I have seen no showstopper bugs so far.

Scala and actors

Programming with actors was a new concept to me until I tried it out in Scala. It’s appears to be one of Scala’s most celebrated features, judging by the official blurb. Actors was a daunting word at first but it really ends up being a very simple concept.

Actors are a programming model for concurrent programming. With conventional mutex/monitor based programming in Java, say, programmers hold and release locks (the synchronized keyword) to achieve safe concurrency. Condition variables are used for thread communication (the wait and notify family of functions on java.lang.Object). Communication is synchronous: a typical case would be that you change some condition, invoke notifyAll to wake up threads waiting on that condition, and then they can take over the relevant lock and proceed to do some processing.

An actor is a unit of execution with an asynchronous message queue. Actors can receive messages from other actors or send messages to other actors at any time, however, the messages wait in the receiving actor’s “mailbox” until the actor has time to receive it.

As a simple example, let’s develop a program that converts text files to upper case using actors. The program will have an “Input” actor, an “Output” actor, and a number of “UpperCase” actors that do the processing. First the Input actor:

import scala.actors._
import java.io._
 
class Input(in: BufferedReader) extends Actor {
	def act() {
	  while(true) {
	    receive {
	      case Next => { sender ! Line(in.readLine()) }
	    }
	  }
	}
}

It’s worth noting that the Actor system is implemented completely in the libraries, outside of the core language. Actors are not first class constructs, but sometimes look as if they were. The act method is where actors begin their execution. The receive method causes them to block and wait for a message, which we may pattern match on. The sender variable corresponds to whoever sent the last message received, and the ‘!’ operator sends a message. So whenever this actor receives the Next message, it will respond with the next line from a buffered reader.

Then, the UpperCase actor:

import scala.actors._
 
case class Next
case class Line(x: String)
 
class UpperCase(input: Actor, out: Actor) extends Actor {
	def act() {
		while(true)
		{
			input ! Next
			receive {
			case Line(x:String) => { out ! x.toUpperCase() }
			}
		}
	}
}

This actor is created with in- and output actors as its constructor parameters. It continually asks the input actor for a new line, converts it to upper case, and sends it to the output actor. Also note the case classes here, which are for pattern matching only. They are a bit like algebraic data types in Haskell.

Finally, the Output actor:

import scala.actors._
 
class Output extends Actor {
	def act() {
		while(true)
		{
			receive {
			case x:String => { println(x) }
			}
		}
	}
}

And then we have to tie it all together:

import java.io._
 
object Demonstration {
 
  val reader = new BufferedReader(new InputStreamReader(System.in))
 
  def main(args: Array[String]) {
 
    val in = new Input(reader)
    in.start
 
    val out = new Output()
    out.start
 
    1.to(5).foreach(x => {
      val tr = new UpperCase(in, out)
      tr.start
    })
  }
}

Here I abuse the foreach notation slightly to create 5 parallel text processors. Each actor runs on its own thread (though there are ways to prevent this if one wants very large numbers of actors). Now of course, the lines will probably be output in the wrong order. Another obvious shortcoming is that there is no clean shutdown protocol that terminates all the actors when the input stream is fully read. Solving these problems is outside of the scope of this article.

Some other interesting resources on actors: the official tutorial, the papers (slightly more academic but accessible to the monomorphic reader, I imagine). Debasish highlights how actors can be used to get threadless concurrency, Erlang-style.

A couple of quick ideas

I’m currently in Sweden, enjoying the Scandinavian nature, catching up with family and a few old friends.

This time, some quick notes on a few ideas that have been brewing.

Orthopraxy is when people do things the same way: “correct” action/praxis. On Artima developer spotlight, there was a lively discussion on this in the Ruby community: is Ruby a language that is less patronizing to the programmer than many other languages? That is, does it enforce orthopraxy to a lesser extent than other languages? In very established languages such as Java, orthopraxy does not just come from language design though; it comes from the culture surrounding the language. Today it is so mature that there is a very small number of accepted styles and accepted ways of coding. This does make people more productive by easing communication, but I wonder if we could have both ease of communication and stylistic freedom…?

Garbage collection in society is the collection of discarded resources: entropy has gone so far that we banish used up objects to a heap of rubbish. Some of these may be immediately recyclable, most of them will take a long time to disintegrate fully. In software, garbage collection is about reclaiming the space used by lost objects that can no longer be used by the program. As such it’s more about recycling – all the memory is reused pretty much instantaneously. The trick here is finding those lost objects and putting the memory to use in a good way. Finally, in life, when plants or animals die, they become part of new plants and animals in a normal ecosystem. Isn’t this garbage collection on a molecular/atomic level? Maybe even as high as on the protein level.

Actor programming in Scala is something I casually started experimenting with for a text processing tool, and it turned out to be a very pleasant way of doing parallel computation. The asynchronous message queues were a much nicer way of doing things than the conventional monitor/mutex methods. I recommend trying to use it for something. In Scala they can equally easily be made threadless (usually each runs on its own thread), making support for a huge number of actors trivial.