Monday, June 6, 2011
Quicklisp
Excluding my personal packages, Quicklisp already holds most (if not all) of the libraries that I use on a daily basis. This means that my method of maintaining packages, manual downloads, updates, and versioning, is basically obsolete (I had long ago ditched ASDF-Install because it was doing things I didn't want it to). To really sweeten the pot, Quicklisp builds several large and/or complicated to build systems that are hard to keep working.
Quicklisp easily integrates with your own packages. It acts as a last ditch solution to loading a system. This means that if you have a package in your ASDF path, it will use that first. Only if ASDF cannot find the requested system, does it check the Quicklisp systems (either on disk or on-line).
But perhaps the thing that I find most satisfying about Quicklisp is that it forces libraries into a more standard format, and that it has generated momentum to get a large number of Lisp libraries canned and dead simple to install on any operating system.
Several people have noted that Quicklisp isn't the first search result for Lisp libraries, so I definitely putting a link here and strongly recommend people use it (for to manage their libraries and perhaps to publish their libraries).
Sunday, June 5, 2011
Bitcoin is for drug lords and terrorists?
For those who don't know, bitcoin is something new and old at the same time. It is digital cash, or as the creators might put it, digital gold. Instead of talking about the mathematics and technology behind it, which is very interesting, let's just discuss the properties. Like cash, bitcoins can be traded between individuals as payment for goods. Unlike cash, bitcoins can be traded over the Internet. Like cash, no third party needs to know who gave what currency to whom; it's anonymous. Unlike cash, no third party controls the circulation, printing, and distribution of currency at all (actually the entire community is the third party). Like cash, bitcoins cannot be easily counterfeit. Unlike cash, which can and has been copied, counterfeit bitcoins are largely accepted to be a practical impossibility (although shy of a mathematical proof, it is based on the same math as strong encryption). Comparing to credit cards and PayPal, the main forms of payment over the Internet, bitcoins have much to offer.
After describing what a bitcoin is, the first response I hear is (as verbatim as I can remember it),
Hostess: "Wow, it seems like that would be good for drug lords."
Me: (Shocked) "Yeah, I guess... but I would say it's useful anytime you want to send money to people without any third party involved. Like if you wanted to make contributions to someone your government didn't particularly like..."
Hostess: "Right, people could use it to send funds to al-Qaeda or the Taliban..."
Me: (Bewildered) "I was thinking more like sending donations to groups rebelling in authoritarian regimes, or... do you remember that whole Wikileaks thing?"
Hostess: "Yeah...?"
Me: "Well there was a movement to try to get it declared as a terrorist organization back when all that hit the fan, which would of made it illegal to donate funds to them. Bitcoins would have made it possible to do so more safely, if you believed in what they were doing and wanted to give them money..."
Hostess: "oh... something, something, PayPal."
Me: "But that's part of my point. Because a third party is involved, you open yourself to all kind of possible bad things. Worst of all PayPal has a pretty notorious past when it comes to messing with their clients' transactions, like freezing or closing accounts at their discretion just because they don't like what you are doing."
Conversation continues about PayPal and eBayNo kidding. I don't know what, if anything, I did to put bitcoins in the light of, useful for drug lords, criminals, and terrorists. I guess it is just a case of if you aren't doing anything wrong, you should care if you have privacy or not. Saying bitcoins would be good for drug lords is like saying cars would be good for drug lords because you could use them to smuggle drugs. It's true, but kind of missing the point.
Thursday, May 26, 2011
Modf ready for the public
Edit: I went ahead with the FUNDS support, but it isn't really much. Unlike FSet, FUNDS doesn't intend to be a replacement to the fundamental Lisp types. Most FUNDS data structures do not include the ability to edit arbitrary elements. FUNDS has trees, and Modf functions are defined for those, but the queue, stack, and heap data structures don't really make sense with Modf. You access them in a different place than you add elements.
This library is small, but kind of reaching completion. Any suggestions or patches are welcome.
Friday, May 20, 2011
Introducting Modf: Setf for functional programming
Setf is such a wonderful macro. Sometimes we forget. It is one of the first things I find missing when I have to resort to an imperative language, which is exactly the place where you want to use it the most. But there seems to be a shortcoming in Common Lisp when it comes to functional programming. What I mean is, it is incredibly easy to mutate state with setf, but when we want to use data in a functional way, it becomes much more cumbersome to do what is needed. For example…
;; Let's try something in a non-fuctional way (let ((list '(1 2 3 4 5 6 7))) ... ;; Change the 5th element in the list to t (setf (fifth list) t) ... ) ;; Let's try it functionally (let ((list '(1 2 3 4 5 6 7))) ... ;; Change the 5th element in the list to t (labels ((replace-nth (nth list new-val) (if (> nth 0) (cons (car list) (replace-nth (- nth 1) (cdr list) new-val)) (cons new-val (cdr list)) ))) (setf list (replace-nth 4 list t)) ) ... )
The cleanest way I could think to do this (barring what will follow) is to define a function that would do this for me. There is nothing wrong with that, and many will state that this general function should be placed in a toolbox so it can be used in the future saving me such effort from now on.
I find, however, that such functions are defined and often never used again, not because I don't need them but because I forget the work I've done in the past. (Not to mention that the function above isn't nearly general enough. What happens if you want to replace the cdr of the nth element, or the cadar?).
If you've read Graham's On Lisp, there are probably a couple of functions (ttrav, trec) floating around in your head that might serve our purposes here. I believe that Graham would state that making functions like the one above easier to write alleviates the problem. I find that using such "one off" methods still uses a bit too much mental overhead when coding.
Now, look at what setf does for us. It hides functions like rplaca and rplacd which no sane individual should spend grey matter on. Setf knows how to invert (car x) and (cdr x) into the proper mutating "function". Wouldn't it be nice if we had a functionality like setf that keeps track of and inserts the proper functional manipulation code. To this end I have created modf.
Modf is a macro that behaves like setf, except it returns a new object with the requested properties rather than makes the changes in place. It changes functional example from above into…
;; Let's try it functionally (let ((list '(1 2 3 4 5 6 7))) ... ;; Change the 5th element in the list to t (modf (nth 4 list) t) ... )
Right, that's all well and good, but that isn't nearly the most use you can get out of this. Let's say we were using the FSet seq data structure to hold a Sudoku board. We might use a seq of seq structures to represent our board. If we want to set a number on the board, we would do this…
(let ((board (make-board))) ;; Set element (4,3) to a 2 (modf (fset:@ (fset:@ board 4) 3) 2) )
…instead of like this…
(let ((board (make-board))) ;; Set element (4,3) to a 2 (fset:with board 4 (fset:with (fset:@ board 4) 3 2)) )
…which is considerably more convoluted. But to really nail this home, consider we have a mess like crazy-datastructure which is a mixture of classes, arrays, lists, and strings…
(defclass test-class () ((slot1 :initform 1 :initarg :slot1) (slot2 :initform 2 :initarg :slot2) (array :initform #() :initarg :array) )) (defparameter crazy-datastructure (list 1 (make-instance 'test-class :array (vector 'a 'b "hello") ) 3 4 )) (defmethod print-object ((obj test-class) str) (with-slots (slot1 slot2 array) obj (format str "#<TEST-CLASS: SLOT1: ~S, SLOT2: ~S, ARRAY: ~S>" slot1 slot2 array )))
CL-USER> (subseq (aref (slot-value (second crazy-datastructure) 'array) 2) 1 3) "el"
Let's say we want to modify that substring to "EL" instead.
CL-USER> (modf (subseq (aref (slot-value (second crazy-datastructure) 'array) 2) 1 3) "EL") (1 #<TEST-CLASS: SLOT1: 1, SLOT2: 2, ARRAY: #(A B "hELlo")> 3 4) CL-USER> ;; And the original CRAZY-DATASTRUCTURE remains unchanged crazy-datastructure (1 #<TEST-CLASS: SLOT1: 1, SLOT2: 2, ARRAY: #(A B "hello")> 3 4) CL-USER> ;; If we compare to the SETF form (setf (subseq (aref (slot-value (second crazy-datastructure) 'array) 2) 1 3) "EL") "EL" CL-USER> crazy-datastructure (1 #<TEST-CLASS: SLOT1: 1, SLOT2: 2, ARRAY: #(A B "hELlo")> 3 4)
If we consider how we might perform this functional modification without modf…
CL-USER>
(cons (car crazy-datastructure)
(cons
(make-instance 'test-class
:slot1 (slot-value (second crazy-datastructure) 'slot1)
:slot2 (slot-value (second crazy-datastructure) 'slot2)
:array (vector
(aref (slot-value (second crazy-datastructure)
'array) 0)
(aref (slot-value (second crazy-datastructure)
'array) 1)
(concatenate 'string
(subseq
(aref (slot-value
(second crazy-datastructure)
'array) 2)
0 1)
"EL"
(subseq
(aref (slot-value
(second crazy-datastructure)
'array) 2)
3))))
(cddr crazy-datastructure) ))
Here we made it simple to functionally modify parts of a data structure that includes a string nested in an array nested in a class nested in a list. Modf acts as a shorthand for complicated functional manipulations. If you examine the macro expansion of the modf form, you will see something very similar to the hand written code to change the deeply nested substring.
How it works
Really, when it comes down to it, modf is a simpler functionality than setf because any modifier can be represented as a function. This isn't true with setf. Consider…
(let ((x 5))
(setf x 5) )
…there is no function you can call with arguments x and 5 which has the effect of setting the lexical variable x to 5 in this scope. This isn't true of functional changes as we are returning the modified value. That is not to say that this didn't turn out to be a tricky macro to write. This is due to the fact that the construction mechanism has to be in the reverse order of the access forms encountered during the expansion.
You can define macro like "rewrites" with define-modf-rewrite that translate access code into other access code that modf knows how to deal with (e.g. (cadr x) -> (car (cdr x))).
You define expansion functions similar (defun (setf func) ...) and (defmethod (setf func) ...) with define-modf-function and define-modf-method, respectively.
You define expansions based on the lexical structure of the code via define-modf-expander (this is analogous in some sense to define-setf-expander). This allows you to invert forms like (car x) to the builder code (cons new-value (cdr x)). These functions return new code that will replace the old code that was passed as an argument to the function.
There is a big difference between these functions and the setf equivalents. You need to specify which argument in the form contains the object that is being modified. This is taken as an extra argument right after the name of the expander.
In principle there is no need to have define-modf-expander, since any modifier can be expressed as a function. It might be beneficial to "open code" certain modf expansions as it will give the compiler a crack at optimizing the resultant code.
There is one special form in the "modf syntax," modf-eval. Modf-eval marks sections of code that modf shouldn't try to invert, and should just leave for the Lisp system to evaluate or compile as it will (the same way modf treats any atom it encounters). This is important if so you can have code like this…
(modf (second (modf-eval '(1 2 3 4 5))) 5)
Without modf-eval, modf would try to invert the form (quote (1 2 3 4 5)), rather than modify the list (1 2 3 4 5). You can even go so far as…
(modf (second (modf-eval
(modf (third (modf-eval '(1 2 3 4 5))) 10) )) 5)
Which allows you to chain modf statements. This can get a little clunky, so to ease the reuse of previously calculated results, you can use extra modf arguments to reuse previous results.
(modf (third (modf-eval '(1 2 3 4 5))) 10
last-result
(second last-result) 5 )
== (let ((last-result (modf (third (modf-eval '(1 2 3 4 5))))))
(modf (second last-result) 5) )
You can even use previous results in non-trivial ways…
(let ((lst '(1 2 3 4 5)))
(modf (third lst) 10
result-a
(second lst) 5
result-b
(fourth lst) (list result-a result-b) ))
==> (1 2 3 ((1 2 10 4 5) (1 5 3 4 5)) 5)
An Example
As an example of how to use this, here is how you might set up modf to work with an affine matrix data structure based on FSet seqs.
;;; First we define how our data structure like we always would. (defclass fset-matrix () ((dims :initarg :dims :accessor mat-dimensions :accessor dims-of) (seq :initarg :seq :accessor seq :accessor seq-of) (a :initarg :a :initform #(1 0 0 1) :type (array integer (4)) :accessor a-of) (b :initarg :b :initform #(0 0) :type (array integer (2)) :accessor b-of) )) (defun make-fset-matrix (dims &key (initial-element 0)) (let ((arr (make-instance 'fset-matrix :seq (fset:with (fset:empty-seq initial-element) (apply #'* dims) initial-element ) :dims dims ))) arr )) (defun fref (mat &rest idx) (destructuring-bind (i j) idx (aif2 (fset:@ (seq-of mat) (+ (* (car (dims-of mat)) (+ (* (aref (a-of mat) 0) i) (* (aref (a-of mat) 1) j) (aref (b-of mat) 0) )) (+ (* (aref (a-of mat) 2) i) (* (aref (a-of mat) 3) j) (aref (b-of mat) 1) ))) it (error "Indicies ~A out of bounds ~A." idx (dims-of mat)) ))) (defun (setf fref) (val mat &rest idx) (setf (seq-of mat) (destructuring-bind (i j) idx (fset:with (seq-of mat) (+ (* (car (dims-of mat)) (+ (* (aref (a-of mat) 0) i) (* (aref (a-of mat) 1) j) (aref (b-of mat) 0) )) (+ (* (aref (a-of mat) 2) i) (* (aref (a-of mat) 3) j) (aref (b-of mat) 1) )) val ))) val ) ;; Then we define a modf function that will inform modf how to invert ;; access function. (define-modf-function fref (val mat &rest idx) (destructuring-bind (i j) idx (modf (fset:@ (slot-value mat 'seq) (+ (* (car (dims-of mat)) (+ (* (aref (a-of mat) 0) i) (* (aref (a-of mat) 1) j) (aref (b-of mat) 0) )) (+ (* (aref (a-of mat) 2) i) (* (aref (a-of mat) 3) j) (aref (b-of mat) 1) ))) val )))
The Code
I am putting up a clone of my repository on Github. I am not sure that the code is ready for public consumption, yet. I will try, in the somewhat near future, to strip out some of the dependencies and make sure it builds on Lisp images other than mine. I would like to see the removal of the dependency on my toolbox library and implementing facilities for FUNDS.
Monday, May 16, 2011
Backtracking with the Common Lisp Condition System
The Common Lisp condition system is a pretty nice facility. I use it extensively in my work. One of its strongest benefits from my point of view is the smooth transition between programmatic error handling and interactive error handling.
The Common Lisp condition system is capable of much more than handling errors, though. However, I have yet to find time to really learn its "ins and outs". I decided try out a project using the condition system in a way that most wouldn't. Here I will show you how to use the CL condition system to implement a backtracking framework.
Update (June 2015): You can read some Reddit discussion on this. Nikodemus Siivola provided a superior roll-your-own backtracking method, pointing out that using the condition system is wasteful for this purpose. He is right, of course.
The Basic Idea
A failure of an assertion is a condition. For example, if we co-opt the error condition for our purposes…
(let ((options '(2 4 6 7 2))) ; These are our options (labels ((fail (&rest args) ; If we fail, for what ever reason (error "Failed: ~A" args) ) (success (&rest args) ; If we find a successful condition, call this args ) ;; This is our re-entry point (retry-with (x) (handler-case ;; This is the actual body of our function (if (oddp x) (success x) (fail x) ) ;; If we find an error... (error (cond) (warn "Failed: ~A" cond) (if options ;; ...we recall the function (retry-with (pop options)) (error "No solution found") ))))) (retry-with (pop options)) ))
All we need is to write a macro that translates the following into that.
(let ((x '(2 4 6 7 2))) (if (oddp x) (success x) (fail x) ))
Of course, using the error condition is a bit of a faux pas. Instead, let's define our own condition type, failure. Finding a solution to the problem could also be a condition, if we choose, and we will this time. While we're at it, we'll define two functions, fail and success, that are pretty self explanatory.
;; Define a parent type for flexibility... (define-condition success (backtracking-condition) ()) ;; NOTE can hold some data about how it failed (define-condition failure (backtracking-condition) ((note :initarg :note))) ;; VALUE holds the solution (define-condition success (backtracking-condition) ((value :initarg :value))) (defun fail (&rest args) "For whatever reason, this has failed. Backtrack." (signal 'failure :note args) ) (defun success (&rest args) "We found a solution. Either return it, or add it onto the list of solutions depending on the value of *MODE* \(as set by WITH-BACKTRACKING)." (cond ((eql *mode* 'find-one) (signal 'success :value args) ) ((eql *mode* 'find-all) (push args *solutions*) (signal 'failure :value args) )))
These really don't do anything by themselves. We need to define a special way to bind nondeterministic variables, or variables that will change their values during the search. I've done this with the bt-let* macro, which is the real meat and potatoes of this. It behaves like let* except that it recognizes two special forms, one-of and one-in (one for objects, one for a list of objects), that perform these nondeterministic bindings. It's not perfect, these forms have to be in simple locations, but it works. We have to have a macro that sets up an environment to do things like handle success conditions, control the way the search is performed, and set up variables that save solutions that are found. This environment is set up with with-backtracking.
(defmacro bt-let* (bindings &body body) "Like LET*, but if you find a special nondeterministic choice form like ONE-OF or ONE-IN, treat it specially by setting up the framework for nondeterministic search." (let (bt-var (option-list (gensym)) rest-bindings ) `(let* ,(iter (for (binding . rest) on bindings) (until bt-var) (cond ((and (consp binding) (consp (second binding)) (eql 'one-of (first (second binding))) ) (setf bt-var (first binding) rest-bindings rest ) (collect (list option-list (cons 'list (rest (second binding))) ))) ((and (consp binding) (consp (second binding)) (eql 'one-in (first (second binding))) ) (setf bt-var (first binding) rest-bindings rest ) (collect (list option-list (second (second binding)) ))) (t (collect binding)) )) ,(if bt-var `(labels ((try-with (,bt-var) (handler-case (bt-let* ,rest-bindings ,@body) (failure () (if ,option-list (try-with (pop ,option-list)) (fail) ))))) (try-with (pop ,option-list)) ) `(progn ,@body) )))) (defmacro with-backtracking ((mode) &body body) "Set up the environment where backtracking can be performed. MODE can be set as one of FIND-ONE or FIND-ALL in order to specify where just the first or all possible solutions should be returned." `(let ((*mode* ',mode) *solutions* ) (handler-case (progn ,@body) (failure () (cond ((eql 'find-one *mode*) (error "No solutions found.") ) ((eql 'find-all *mode*) *solutions* ))) (success (cond) (slot-value cond 'value) ))))
Pythagorean Triples
Pythagorean Triples are three integers, $a$, $b$, and $c$, that satisfy the equation, $a^{2} + b^{2} = c^{2}$. In other words, $a$, $b$, and $c$ are the lengths of the sides of a right triangle.
(defun pyth-triples (n) (with-backtracking (bt-let* ((a (one-in (iter (for i from 1 below n) (collect i)))) (b (one-in (iter (for i from 1 below n) (collect i)))) (c (one-in (iter (for i from 1 below n) (collect i)))) ) (if (= (+ (* a a) (* b b)) (* c c)) (success (list a b c)) (fail) ))))
Performance
Common Lisp already has a pretty good backtracking library, Screamer. We can compare the performance of our library with Screamer. Here's our version…
CL-USER> (time (pyth-triples 100)) Evaluation took: 0.892 seconds of real time 0.880000 seconds of total run time (0.840000 user, 0.040000 system) [ Run times consist of 0.030 seconds GC time, and 0.850 seconds non-GC time. ] 98.65% CPU 2,135,319,408 processor cycles 345,196,992 bytes consed
…and the Screamer version?
CL-USER> (time (screams::pythagorean-triples 100)) Evaluation took: 0.060 seconds of real time 0.060000 seconds of total run time (0.060000 user, 0.000000 system) 100.00% CPU 143,821,854 processor cycles 490,032 bytes consed
So, looks like around a factor of ten. Not so good, but maybe it's not so bad that people can't use it. Now, it might seem like exhaustive search is the time, if there ever is one, where we should be the most concerned about performance. That has some truth, but we should also remember that if we ever really rely on the exhaustive search to do the work, we probably have already lost the battle. Being ten times faster will allow you to handle an input size one larger than the slow version. I like to use nondeterminism to write an algorithm that's guaranteed correct, then improve from there. So, perhaps the slow version isn't unusable after all.
Conclusions
Well, this method is a pretty simple method of implementing backtracking in Common Lisp. It is less performant and versatile than Screamer, but Screamer is a pretty gigantic library.
Monday, April 18, 2011
The difference between Open Source and Free Software
To quickly paraphrase the FSF's position, Open Source describes a method of developing, distributing, and supporting software, whereas Free Software deals with the rights people should be granted when receiving software. These are two very different things. Looking at this, I would say that what people are coming to realize is that Android is certainly Free Software, but it is not Open Source. It is Free Software because it is released under a Free Software license; your rights are protected, period. However, Android is not developed in the same way most Open Source software is. It is not so much open to community patches and development. It is developed behind closed doors by a team that may choose to listen to outside opinion but is in no way obligated to.
RMS remarks that by and large Open Source projects are Free Software projects. He poses it as an extension of Open Source, a step further, a statement of commitment to protecting user and developer rights. If we all remember correctly, the term Open Source was first coined to act as a synonym for Free Software that didn't have the same anti-corporate stigma and later evolved into something with its own foundation. People introduced the term FOSS or FLOSS (Free (Libre) Open Source Software) to lump the two together. I find it interesting to see how we really shouldn't lump them together at all; they truly are independent. Projects can be Open Source, projects can be Free Software, and they can be both.
Monday, February 7, 2011
Using Git even when collaborating with those that don't
I did, however, just move a bunch of snapshots of my supervisors simulation code into a git repo. This had the effect of organizing all of my files nicely and in one spot, provided a natural place to put weird side versions (as abandoned branches), as well as cutting about 1 GiB of files into around 20 MiB since versioned snapshots are basically 90% or more the same. In addition, I am hoping that the merge tools will allow me to reduce the number of times I will have a directory labeled v1.36, one labeled v1.36_matt, and one labeled v1.36_robert.
Next time a colleague sends me a new version, I will just create a temporary branch, merge it into my current version, and tag it with the new version number. So, even though I doubt that my supervisor will ever be able to spend the time to learn how to use Git, I think I will be able to use it to organize things on my end, which makes me a bit happier.