Anyway, that's besides the point. What I want to talk about today is a feature that I've seen in Prism's model description language (which looks a lot like Promela). Here is the second part of the Prism tutorial. It introduces a language feature that I loved instantly. It is called module renaming and it rocks.

Module renaming basically works in the following way: take this function, which does this. Then create another function, which is exactly the same, but replace this symbol by another.

I can imagine how this can work for a general-purpose programming language. Define a function which sums two numbers:

pseudocode: sum x y = x + y

Then define the product to be like the sum, except replace + by *.

pseudocode: product = sum where + = *

That's nice. So I thought I'd try and see if I can implement this using lisp macros. I don't have much experience with lisp, but here's how I sort-of did it:

First thing's first. Create a function that takes a list and replaces every "source" with "target".

(defun rename-symbol (list source target)

(if (eq list nil)

nil

(cons

(if (listp (first list))

(rename-symbol (first list) source target)

(progn

(if (eq (first list) source)

target

(first list))))

(rename-symbol (rest list) source target))))

Here's how it works:

CL-USER> (rename-symbol `(+ 1 2) '+ '*)

(* 1 2)

Well, I actually want to perform multiple renamings, so I replaced source and target with an association list (sort of like a hash table):

(defun association (item alist)

(rest (assoc item alist)))

(defun rename-symbols (list alist)

(if (eq list nil)

nil

(cons

(if (listp (first list))

(rename-symbols (first list) alist)

(if (not (eq (association (first list) alist) nil))

(association (first list) alist)

(first list)))

(rename-symbols (rest list) alist))))

Which does the same thing, except it takes an association list and can replace multiple symbols. Here's how it works:

CL-USER> (rename-symbols `(defun sum (a b) (+ a b)) '((+ . *) (sum . product)))

(DEFUN PRODUCT (A B) (* A B))

See where we're going? Now let's write a simple macro:

(defun rename (list alist)

(eval (rename-symbols list alist)))

And here's how Lisp's syntax can be extended to allow renaming:

(setq my-function `(defun sum (a b) (+ a b)))

(rename my-function nil)

(rename my-function '((+ . *) (sum . product)))

When evaluating the above, you'll end up with two functions, sum and product, which do what you expect.

Disclaimer: I am a Lisp beginner and I've probably made some mistakes.

I welcome any and all corrections/improvements/suggestions. I would be especially interested in creating a rename macro that works as follows:

(defun sum (a b) (+ a b))

(rename sum product '((+ . *)))

If there is any Lisp guru reading this, please let me know ;-)

Also, I am curios about how this could be implemented in languages such as Ocaml or Haskell. Some of the readers will be quick to point out that this feature is not really required, because you can get the same benefit by creating a function which is more generic that product and sum, which would look like this in Haskell:

combine op x y = op x y

sum = combine (+)

product = combine (*)

But I find the rename feature extremely geeky and cool. So let me have it :D.