Monthly Archives: January 2014

The less-familiar parts of Lisp for beginners — fill pointers

Continuing the series, I’ll just briefly touch on fill pointers.  These are basically just convenience features that can be attached to one-dimensional arrays.

If a one-dimensional array (vector) has a fill pointer, it informs the system that the vector’s length is equal in value to the fill pointer.  One might use this feature when incrementally filling a vector, maybe a cache.  To avoid the cost of reallocation on resizing, you could create a large initial vector, but set the fill pointer to zero.  Then, as elements are added to the vector, you increase the fill pointer.

The fill pointer does not forbid references to offsets of the vector that are above the limit of the fill pointer, it merely allows length to return something other than the actual length of the allocated vector.  When printing a vector, no elements past the fill pointer are displayed.  Other than those two cases, the fill pointer doesn’t have any other effect.  Here’s a series of operations:

CL-USER> (defparameter *fvec* (make-array 10 
                                          :fill-pointer 4 
                                          :initial-element 1))
CL-USER> *fvec*
#(1 1 1 1)
CL-USER> (setf (aref *fvec* 6) 15)
CL-USER> (aref *fvec* 6)
CL-USER> (array-dimension *fvec* 0)
CL-USER> (length *fvec*)
CL-USER> (setf (fill-pointer *fvec*) 10)
CL-USER> *fvec*
#(1 1 1 1 1 1 15 1 1 1)

You’ll notice that I can modify and retrieve elements past the end of the region the fill pointer declares as active.  You’ll also see that array-dimension and length return different values.

If you use the fill-pointer accessor on a vector that doesn’t have a fill pointer, an error is raised.  You can check whether or not a vector has a fill pointer with the function array-has-fill-pointer-p.

The less-familiar parts of Lisp for beginners — fdefinition

We move on now to fdefinition.  This is a fairly short topic, but before reviewing it, you might want to read the article on fboundp, if you have not yet read it.

The fdefinition accessor provides a means for setting or reading the function object associated with a particular symbol in the function symbol table.  As such, it is, like fboundp, also unaffected by lexical functions or macros created with flet, labels, or macrolet.

The uses of this accessor are fairly esoteric.  It can certainly be used as part of the implementation of the defun macro, or in the implementations of functions that are passed function symbols, like apply, funcall, and mapcar.  It can also be used to sniff the type of function that is bound, after fboundp returns non-nil.  There might be some rare contexts in which the programmer needs the code to determine whether a particular passed symbol points to a function or macro, or to a generic function, and fdefinition is part of the means for answering that question.  In everyday programming, though, you are unlikely to need to use this feature.

CL-USER> (defun my-adder (x)
           (+ 1 x))
CL-USER> (my-adder 2)
CL-USER> (setf (fdefinition 'my-adder) #'(lambda (x) (+ x 10)))
#<FUNCTION (LAMBDA (X)) {100673943B}>
CL-USER> (my-adder 2)

An example of examining the type of a symbol:
(defun what-symbol (sym)
    ((fboundp sym)
     (typecase (fdefinition sym)
        (format t "~A is a generic function~%" sym))
        (format t "~A is a (non-generic) function~%" sym))
        (format t "~A is not a function~%"))))
     (format t "~A is not bound in the function symbol table.~%" sym))))

Edit #1:  2014-02-02

Please do not attach too much to the vague and possibly misleading “function symbol table” language I’ve used above.  Instead, look over this later article that I hope provides some more precise details on what is really happening.  It’s not merely a semantic distinction, the material there is important to understand when programming in Lisp.

The less-familiar parts of Lisp for beginners — fboundp

Next, we have fboundp.  This is another function for querying the state of the Lisp environment, asking whether a particular name is bound to a function or macro.  This function does not determine whether the name is interned as variable name.  Recall that, unlike C++, the Lisp language syntax unambiguously distinguishes between variable names and function names.  Consequently, these two types of symbols are not at risk of namespace collisions.

Now, you might think that this makes fboundp very simple.  The temptation is to say that if fboundp on a symbol returns nil, then calling that symbol in a function context will always fail, but things are a bit more complicated than that.  The fact is that it is possible to create named functions and macros which are not inserted into the global/persistent Lisp function namespace, but instead have a limited scope, following which time the functions effectively disappear.  The features labels, flet, and macrolet do this.  If, in your reading, you’ve seen a reference to “lexical functions”, the labels and flet features are what create them, and fboundp does not see lexical functions or macros.

And from this starting point, we can gain some helpful insight into the way Lisp works…

When one of labels, flet, or macrolet is used, it does not insert a new entry into the function symbol table, but it does shadow any existing functions with that name in the same scope.  If labels did insert an entry into the function symbol table, that would interfere with the view seen by other threads of execution.

This distinction between names in the function symbol table and names constructed by labels, flet, or macrolet manifests itself in one of the less obvious syntax requirements.  There are many contexts where the programmer can pass a function by referring to its symbol.  If the programmer has decided to use labels to build a function that happens to collide with an existing name in the function symbol table, how can he tell the program which one to use?  When passing a name that is not in the function symbol table, then rather than using a single-quote to quote the name, one must use a number sign followed by the single quote.  This is demonstrated here:

CL-USER> (defun my-adder (x)
           (+ x 2))
CL-USER> (labels
             ((my-adder (x) (+ x 3)))
           (mapcar 'my-adder '(1 2 3)))
(3 4 5)
CL-USER> (labels
             ((my-adder (x) (+ x 3)))
           (mapcar #'my-adder '(1 2 3)))
(4 5 6)

I begin by creating a function called my-adder in the function symbol table, one that adds 2 to the numbers passed to it.  Then, I use labels to shadow my-adder with a new definition, one that adds 3 to the numbers passed.  Note, though, that when I use the symbol ‘my-adder in mapcar, the one that is used is the one in the defun, not the one that I supposedly used to shadow it.  In order to use my new definition of my-adder, I have to use the #’my-adder syntax.  It is important to understand the difference between these two cases, and why the different forms are necessary.

It helps to understand what these notations are doing.  What, exactly, is the difference between mapcar acting on ‘my-adder and acting on #’my-adder?  In the first case, the mapcar function is called with a symbol as a parameter.  Inside mapcar, the symbol is resolved in the function symbol table to obtain the function that is to be used for the operation.  That is, mapcar is told “use the function whose symbol is designated by ‘my-adder“.  The second case, #’my-adder, is entirely different.  The # prefix is a reader macro that converts the text at load time.  The sequence #’my-adder is replaced by the text sequence (function my-adder) before the Lisp instance even sees it.  The function special operator resolves the name in the current lexical environment (before the mapcar function call), and returns a function object, not a symbol.  The mapcar function, rather than receiving a symbol and being told to look it up, receives a bare function itself, and uses that, without further resolution.

Perhaps now, there is a realization dawning.  You know that familiar construct for passing anonymous functions, lambdaWe start with a quoted list, the car of which is the keyword lambda.  Then, by prefixing the #, we convert this list into a function in the current lexical environment, and that function is what is passed downwards.  In effect, when we pass a function as a parameter, we can either choose to pass it by name, by sending a symbol down, or by value, by resolving the symbol into an actual function object and passing that instead.  And that is the difference between these two syntaxes, and the # prefix is a read-time shorthand for the second case.  Edit #3: 2014-02-27.  This phrasing was awkward, and now that we’ve covered read-macros under gensym, and have a separate article for lambda, this struck-out text should be ignored.  Instead, please refer to the article about lambda for more details.  The final point is this: many Lisp features which accept functions as arguments can be passed either a symbol from which the function is to be retrieved, or simply the bare function itself.  For functions that are not bound to a symbol, like those created with labels, flet, or lambda, it it necessary that the function object itself be passed.  The function object is retrieved within the scope where it is visible with the function special operator, for which #’ is a read-macro shortcut.  Finally, Common Lisp defines a lambda macro that expands to include the invocation of function, so the #’ is technically optional on lambda forms.

Edit #1:  2014-02-01

Some criticism has been raised on other sites about the terms and descriptions above.  It has been noted that referring to something like the “function symbol table” may confuse more than illuminate, as that is not a term commonly used.  I apologize for this, and it’s a good point to bring up.  My tendency in this series has been to try to describe logical parallels directed at the C++ programmer.  Not an excuse, merely an explanation.  But I certainly appreciate that the language I use above is strange or off-putting to a veteran Lisp programmer, a category in which I emphatically do not include myself.

I sincerely hope that when I reach the intern function, that the description I provide then will be more familiar and provide a better view of what is really going on.

For now, if you’ve read the original text above, and seen me talking about a “function symbol table”, don’t attach too much to that.  The underlying concept, I hope, is helpful.  There exists an mechanism in the Lisp image that allows a function to be referenced by its symbol.  The labels form, while superficially similar to defun, is, in fact, notably different in that it does not influence this aforementioned mechanism, and so does not interfere with the resolution of that symbol in other forms.

I also invite others to post copies of their comments and criticisms here, if they so desire.  I don’t want confusing or misleading postings to sit uncorrected on this site, while helpful criticism sits on other web sites that the casual visitor might not have come across.  I’ll point out that my motivation for writing this series of postings is to improve my understanding of Lisp, and deliberately researching every feature that I haven’t had the opportunity to use.  These posts are primarily an educational tool for myself, but I hope they help others.  If I say something wrong or confusing, please let me know.

Edit #2:  2014-02-02

I’ve posted an out of sequence article about interned symbols and packages here.  I hope that this provides more clarity, and invite the reader to go over that material carefully in order to avoid being mislead by some of the less precise terms I’ve been using in this series of posts.  Once again, I invite comments if the more experienced readers feel I’m failing to give helpful explanations.

The less-familiar parts of Lisp for beginners — export

Next in our list of Lisp features not necessarily encountered in a brief introduction is export.  This is related to the package system of Lisp.  You may find it useful to review the earlier article on delete-package, to understand what a package is in Lisp, and how it differs from C++ classes and namespaces.

Once again, the use of this function ties back to a fundamental difference in the creation of Lisp programs, as distinct from C++ programs.  In C++, the author edits one or more disc files, compiles them into a single executable unit, and then runs that executable.  If changes are to be made, the programmer edits the disc files once more, recompiles, and then restarts the C++ binary.

Lisp programs, on the other hand, are assembled by inserting code into a Lisp image.  Rather than creating a new stand-alone binary, you should think of this in terms of adding things to a blank Lisp image.  Functions, classes, structure, packages, and more, are added, serially, to a Lisp image in order to achieve a desired program state.  Some of the things that C++ would do with keywords and compiler or linker directives are, in Lisp, done with functions that act at the time of invocation, not at the time of compilation.

So, we mentioned packages earlier, and how they are a bit like namespaces, but have the private/public symbols the C++ programmer might associated with private/public methods in classes.  A symbol in a Lisp package is not exported by default.  To the beginner Lisp programmer, this looks like a small difference, calling those symbols requires using a double-colon after the package name, rather than a single colon.  In the context of package inheritance, however, an exported symbol is visible in derived packages that inherit from it, and exported symbols raise the possibility of namespace collisions in that context.

So, what does export do?  Well, unsurprisingly, it causes a particular symbol to be exported from the package.  Normally, the programmer lists the exported symbols in the package definition, as, for instance, in this earlier article, with the :DL-LIST package.  The implementation of the defpackage macro, however, generally calls export or something of equivalent functionality.  The programmer may have reason to choose to export certain symbols at runtime, for instance if optional packages that would supply those symbols are not loaded.  In most circumstances, this function will be only rarely used.

The less-familiar parts of Lisp for beginners — eval-when

Next in our review of less-commonly seen Lisp features is eval-when.  This is a bit of a subtle one, that has to do with executing certain forms in certain circumstances.

There are three arguments you might use to eval-when:compile-toplevel, :load-toplevel, and :execute.  The first two apply only in situations that involve compiling Lisp code.  This does not include entering a defun at the command line, or even loading a .lisp file from disc.  It’s important to note that :load-toplevel applies only to the loading of compiled Lisp files, not to the loading of the source code in a .lisp disc file.

To understand things a bit, let’s talk about what happens when you load a .lisp file.  You may look at the file and think it looks like you’re telling it to load a bunch of functions and macros, but you should think of it more like running a shell script.  A set of commands that are immediately executed, in sequence.  Of course, when a form begins with defun or defmacro, it doesn’t do anything obvious, merely sets up a function or macro.  But if a toplevel form were something like a format output command, that would be executed, and the output appear on your screen as you load the .lisp file.  Loading a .lisp file looks like typing those commands into the Lisp prompt, in order.  So, as I said, think shell script, acting in a functioning Lisp environment.

Compiling a file, however, is different.  The compiler executes in a specially modified Lisp environment, scanning the input file and compiling functions, but deliberately not executing toplevel forms.  Once the file has been compiled, loading the resulting object file looks a lot like loading a .lisp file, toplevel forms are executed.  So, you can think of compile-file as compiling a script, and the following load operation as running the compiled script.

Sometimes, however, the programmer has some forms that he or she wants to have execute in the compilation environment, perhaps to modify the behaviour of the compiler.  This might be used for optimization settings, or maybe to record when a file was compiled.  Another interesting use is in modifying the language, and adjusting the compiler to handle it.

I’ve talked before about Lisp language features that I miss when I’m writing C++ code.  Well, there’s a feature of C/C++ that I miss in Lisp.  That is, preprocessor concatenation of string literals.  When I’m writing a long string literal, and don’t want the text to wrap, I can type in a sequence of string literals, enclosed in double-quotes and separated by blanks, knowing the the preprocessor will assemble them into a single long string.  This feature isn’t present in the Lisp reader.  Here, though, I’ll show how to implement it at compile time, so that the compiler can operate on files containing this modified Lisp syntax.

(format t "Hello there, this is a toplevel executing form.~%")

(eval-when (:compile-toplevel)

  (defparameter *rt-copy* (copy-readtable))

  (defun bar-reader (stream char)
    (declare (ignore char))
    (let ((stringlist (read stream t nil t)))
      (apply 'concatenate 'string stringlist)))

  (set-macro-character #\_ #'bar-reader)

  (multiple-value-bind (sec min hour day month year)
      (decode-universal-time (get-universal-time))
    (with-open-file (s "e-w-demo-compile-time.lisp"
                       :direction :output 
                       :if-exists :supersede)

      (format s "~S~%"
              `(unintern '+compile-time+))
      (format s "~S~%"
              `(defconstant +compile-time+
                (format nil 
                 "~2,'0D:~2,'0D:~2,'0D ~0,4D-~2,'0D-~2,'0D"
                 ,hour ,min ,sec ,year ,month ,day))))))

(eval-when (:compile-toplevel :load-toplevel)
  (load "e-w-demo-compile-time.lisp" :if-does-not-exist nil))

(defun demonstrate ()
    ((boundp '+compile-time+)
     (format t "File compiled at: ~A~%" +compile-time+))
     (format t "No compile time information available.~%")))
  (format t _("This is a long string that I don't "
              "want to see linewrap when formatting "
              "for the blog post~%")))

(eval-when (:compile-toplevel)
  (copy-readtable *rt-copy* *readtable*)
  (unintern '*rt-copy*))

The output here is:
CL-USER> (compile-file "eval-when")
; compiling file "/home/neufeld/programming/lisp/blogging/eval-when.lisp" (written 17 JAN 2014 19:59:43 AM):
; compiling (FORMAT T ...)
; compiling (LOAD "e-w-demo-compile-time.lisp" ...)
; compiling (DEFUN DEMONSTRATE ...)

; /home/neufeld/programming/lisp/blogging/eval-when.fasl written
; compilation finished in 0:00:00.018
CL-USER> (load "eval-when")
Hello there, this is a toplevel executing form.
CL-USER> (demonstrate)
File compiled at: 20:05:42 2014-01-17
This is a long string that I don't want to see linewrap when formatting for the blog post

Note that the format statement was not printed out when compiling the file, but was printed when loading it.  The compile-time modification to the reader that I made asks it, when presented with the ‘_’ (underline) character, to read the next object, which must be a list of strings, and return a string which is the concatenation of those in the list.  When this new syntax is used in the demonstrate function, it is recognized and understood.  Without the use of eval-when, this would not be possible, as the compiler deliberately avoids executing forms such as the one to modify the read syntax, unless specifically told otherwise.

What, exactly, is going on in this file?  Well, at compile time, a copy is made of the read table, so that we can restore it after we’ve finished using our modified read table.  That is to avoid polluting the Lisp image on which we’re performing the compilation, it’s bad form for the compile operation to modify the behaviour of the image after the compilation has completed.  Next, we set up our modified read table to concatenate strings, and we write out the current time to a file on disc.  This disc file allows us to record new forms generated at compile time but absent at load time.  The file is compiled, and then at the end of the compilation we return the read table to its former state.

I haven’t mentioned the final eval-when option.  The third option to eval-when, :execute, is the “everything else” category.  These forms are evaluated when neither in the compiler, nor loading a compiled file.