Archive for the ‘Programming’ Category
Core Wars is Related to “The Planiverse”
Just ran across (via Proggit) an interesting blog post about Core War, a game I played a bit with on Linux a few years back in which programs compete for dominance inside a virtual machine. Very neat idea – you program an agent that will go and fight for you in a virtual world. Cool.
What I didn’t know was that A.K. Dewdney wrote an article about Core War in Scientific American in 1984. This is the same A.K. Dewdney that authored one of my very favorite science fiction/philosophy books ever, The Planiverse. It turns out it is sort of a small world.
Using refs in Clojure to Scope State
I mentioned a couple of posts ago about a technique for scoping state I used when I programmed in Scheme more often than I do now. As I’ve been picking up Clojure as my hacking language of preference, I was surprised to find that it doesn’t support the same idioms. In Clojure, any variables referenced when a function is defined in a local scope are unbound when the function is returned to an outer scope. This means the code I wrote in Scheme doesn’t have a direct analogue in Clojure. This was a deliberate decision made by Rich Hickey, Clojure’s designer/author. As he says:
If locals were variables, i.e. mutable, then closures could close over mutable state, and, given that closures can escape (without some extra prohibition on same), the result would be thread-unsafe. And people would certainly do so, e.g. closure-based pseudo-objects. The result would be a huge hole in Clojure’s approach.
The end result is that when you first pick up Clojure, if you try to close a function definition over a variable, you’ll find it is no longer bound when you leave the enclosing scope, and if you try to close over let-bound value (local), you’ll find that is immutable, and can therefore not be used to store state. So how do we accomplish the same result as we did in Scheme?
You use Clojure’s shared transactional memory and create a mutable ref that is modified in a transaction to preserve thread-safety:
(let [z (ref 0)] (defn add [] (dosync (ref-set z (inc @z)))))
We still make use of local binding, but we bind z to a ref, which we can then modify inside of a dosync call. Naturally, an arbitrary number of functions could be defined inside of the let that all shared a reference to z. Once outside of the let, z is no longer accessible to the casual programmer, but the functions still have full access to it, creating a sort of data hiding/state-preserving closure that protects the “private” variable z.
I don’t use this idiom often, but when I want it, it’s exactly what I want, and avoids the need for more complex constructs to simply have a pair of functions share a variable that isn’t modifiable in other parts of the program.
Fine Grain Scoping in Scheme With Let
In Java, there are four scopes: public, package friendly, protected and private. These are useful, but rather coarse. It would be nice to be able to specify a variable that was only visible to a select set of methods. For example, suppose you wanted to develop a counter that could not be modified without being accessed through methods that modified it. In Java, this would be accomplished with an inner class that had two methods. It’s interface would have to be declared publicly, though. In R5RS Scheme, this would look like:
(let ((z 0)) (list (lambda () (set! z (+ z 1))) (lambda () (set! z (- z 1)))))
This defines two functions that each close over the variable z such that you must call the first to increment z and you must call the second to decrement z. Because z is not defined at the top level, there is no way to access it and modify it directly — you must go through the functions. This allows the functions to be a bottleneck to the modification of z. So, if, for example, you wanted to log every time z was changed, you could add a logging command to each of the functions. This approach allows arbitrarily fine scoping to be defined. If you want a variable to be available to only one or two functions, it is trivial to do so using the “let” construct.
I’ll take a look at how you can accomplish similar results in JVM-based functional languages like Scala and Clojure in my next post.
Creating Executable Jars With Clojure
Clojure is a superb new-ish lisp for the JVM that is quickly becoming my preferred hacking playground. As with any new language, as soon as you write anything worthwhile, you want to figure out how to distribute it. In Clojure’s case, even though Clojure is functional and is dynamically compiled, the process of creatng an executable jar isn’t too bad. I outline it here for reference:
- Create the application directory structure. For this example, I’m using org.etherplex.example.JarExample. I’m going to make a project directory called simply “project”. I’m assuming Linux/OS X here, Windows users need to adjust this or install Cygwin.
mkdir -p project/org/etherplex/example
- Create your application in Clojure, creating a main method that is named properly. For this example, I used a file called JarExample.clj. It resides in the project/org/etherplex/example folder.
(in-ns 'org.etherplex.example)
(clojure/refer 'clojure)
(def JarExample-main (fn [& args] (println "This is an example application!")))
If we want to have our “main” class be called JarExample, then in clojure, we need to define a function called JarExample-main. In general, Clojure will look for methods of a class in the same package as that class, and will use the form TheClass-methodName to locate the proper class.
- Now we need to create a stubbed .class file that will dispatch method calls to our Clojure file. The easiest way to do this is to use the REPL. I start the REPL in the directory that is the parent of my project directory. In this case, that’s my home directory.
cd ~
java -jar path/to/clojure.jar
Now that we are in a Clojure REPL, we can create our class stub:
(gen-and-save-class "project" "org.etherplex.example.JarExample" :main true)
First, we specify the path that contains our files (“project”) and then the fully qualified class we want to create. We also specify a flag that indicates that we want Clojure to create a main method that dispatches out to the Clojure file we created in Step 2.
- Unpack clojure.jar so you can include its contents in your jar file. We need to make sure our jar has all the dependencies in it so it is easy to distribute.
cd ~
unzip path/to/clojure.jar
mv clojure project
- Now we need to create a manifest file for the jar so Java know to execute JarExample.class. This is the contents of a file called manifest.txt that I put in my project directory.
Main-Class: org.etherplex.example.JarExample
Class-Path: .
It is vital that this file have a trailing newline!
- Finally, we can create our jar file:
cd ~/project
jar cmf manifest.txt myjar.jar clojure org
And execute it with:
java -jar myjar.jar
This will create a jar file that contains clojure and your code, and will interpret/compile your .clj file on the fly and run it. Good luck!
Trying to figure out if there is anything in the canonical definition of streams (lazy lists) that says that you need to keep the initial elements of the stream after use (if, for example, you’re recursing down the list and the head elements are getting garbage collected). Can streams be used as iterators when traversed recursively?
Emacs Uptime
Since I tend to work in Emacs no matter what computer I’m using, I got curious after reading Steve Yegge’s latest post about XEmacs instability what exactly my uptimes are on all my Emacs sessions (I use GNU Emacs, but I was still curious). This is some elisp I threw together to answer that question.
(require 'cl)
(defun duration (time)
"Takes in a time-value and returns the number of seconds since
the epoch that value represents."
(+ (* 65536 (car time)) (cadr time)))
(defun uptime ()
"Prints the current uptime of Emacs as recorded on startup in
the value 'start-time'"
(interactive)
(let* ((total (duration (time-subtract (current-time) start-time)))
(days (floor (/ total 86400)))
(hours (progn (decf total (* days 86400)) (floor (/ total 3600))))
(mins (progn (decf total (* hours 3600)) (floor (/ total 60))))
(secs (progn (decf total (* mins 60)) (floor total))))
(message "%d days, %d hours, %d minutes, %d seconds" days hours mins secs)))
; Put this in your .emacs
(setq start-time (current-time))
Treehouse, et. al.; How the Internet is Fundamentally Misused
Tad Williams had vision. He wrote an epic saga whose events unfolded in a hyper-connected world. In this Otherland of virtual realities existed an undernet, a virtual reality operated by those who populated it – it was an elusive place, a metaverse called Treehouse whose entrance was almost unlocatable – but it was owned by those that lived within. This made it completely unlike the Internet we use every day. We are the citizens of the internet, but we do not own it.
This was not the original vision. ARPANET was essentially peer-to-peer (they called it host-to-host), and when email was concieved in 1971, it was designed to be essentially peer-to-peer. Email has no central authority – it is a communication protocol between peers. But, we find ourselves surfing today in a hierarchical network of those who provide and those who consume. Once more, we are cast as the consumers.
With the penetration of broadband around the wold, we can finally realize a truly peer-to-peer web of connections between people, not corporations. We have seen a rise in the “social web” – a social gathering mediated by and on he property of a corporation.
This is convenient, until it isn’t. Until the service goes down, or mishandles your data and loses it, or worse, sells it, you might not mind. But even email, originally meant to belong to the users of the network is now “webmail”, and unsurprisingly is hosted on central servers that the users don’t own.
But it would be simple to change all that. We just have to figure out how. And I think I have a simple idea of how to start.