NIL is undefined?

Discussion of Common Lisp
Post Reply
subhuman
Posts: 4
Joined: Tue Oct 20, 2009 1:44 am

NIL is undefined?

Post by subhuman » Tue Dec 08, 2009 1:25 am

hi @ll,

being still a bloody noob, i'm experimenting, trying this and that, just in order to get a feeling for the language. i use every means i can find on the web, but today i've been running into a trap of which no escape seems available. consider this simple script:

Code: Select all

(push "/root/clbuild/systems/" asdf:*central-registry*)
(asdf:operate 'asdf:load-op :bordeaux-threads)

(defun in-thread (a)
  (let ((b 0)
  (lock (bordeaux-threads:make-lock)))
  (loop while (= 1 1) do
           (setq b (+ b 1))
           (setq a (+ a 1))
           (bordeaux-threads:acquire-lock lock t)
           (print a)
           (bordeaux-threads:release-lock lock)
          (if (= b 10) (return 0))
  )))

(bordeaux-threads:make-thread (in-thread 0) :name 't-1)
(bordeaux-threads:make-thread (in-thread 10) :name 't-2)
(bordeaux-threads:make-thread (in-thread 100) :name 't-3)
it compiles well, but when i try running it, via 'sbcl --script', the debugger keeps on telling me that the function NIL is undefined. apparently the error gets thrown from within the if condition, but from where ever: nil is certainly not undefined. can anyone please shed some light on this?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: NIL is undefined?

Post by ramarren » Tue Dec 08, 2009 1:53 am

subhuman wrote:

Code: Select all

(bordeaux-threads:make-thread (in-thread 0) :name 't-1)
The function NIL is most certainly undefined, since it is not by default and most implementations will stop you from defining one. There is a constant named NIL, but it is not a function. In SBCL the error from that is in any case "The value 0 is not of type (OR FUNCTION SYMBOL).", nothing about NIL.

The mistake here is that MAKE-THREAD takes a thunk. A thunk is a function of no arguments. In this case it is what is run in the new thread. Your IN-THREAD function does not return a thunk, it returns a number, which is not a function, and so it cannot be called. What I guess you want is to run IN-THREAD in a thread, which can be achieved by:

Code: Select all

(bordeaux-threads:make-thread #'(lambda () (in-thread 0)) :name 't-1)
or modifying IN-THREAD to return a thunk. For example, and fixing a number of other weird things, although I am not sure what this functions is supposed to achieve anyway:

Code: Select all

(defun in-thread (a)
  (lambda ()
    (loop repeat 10
          with lock = (bordeaux-threads:make-lock)
          do (incf a)
             (bordeaux-threads:with-lock-held (lock)
               (print a)))
    0))

Post Reply