Common Lisp: getting an error using readtable-case - lisp

When I enter this at the REPL prompt:
(setf (readtable-case *readtable*) :invert)
I get this error message:
Error in SETF [or a callee]: Cannot expand the SETF form (READTABLE-CASE
*READTABLE*).
Why do I get this error?

(format nil "~A ~A" (lisp-implementation-type) (lisp-implementation-version)) says "Kyoto Common Lisp GCL 2.6.2". The result of (eq 'readtable-case 'cl:readtable-case) is T. What does this all mean please?
The second expression means you're using the correct symbol. The first indicates which Lisp implementation you're using: That should be GNU Common Lisp 2.6.2
After some search I found this message on the gcl-devel list saying ...
The problem appears to be this line:
(setf (readtable-case *readtable*) readcase)
in randomly-check-readability. I'd recommend this as the next
ansi issue to resolve, since it's blocking the tests.
... with a subject line "ansi-tests in 2.7.0". The message is from 2004.
Bottom line: I guess you need a more recent or even* a different Lisp implementation.
(* as mentioned by Rainer Joswig the issue also affects the current 2.6.12 release)
I guess all major Lisp implementations support this. CLISP 2.49 does, SBCL and CCL probably do, as far as I know ECL does also.

Related

Clozure Common Lisp - file-exists-p is undefined

I get a "Undefined function FILE-EXISTS-P called with arguments ..." error when calling (file-exists-p "somepath") in Clozure Common Lisp but everywhere I look it appears that this function should be available. I even see it when using M-x apropos.
I'm using LispBox for Windows.
Does anyone have an idea of what might be wrong or maybe suggest a process by which I can try to figure it out?
FILE-EXISTS-P is not a standard Common Lisp function or a Clozure Common Lisp specific function.
Instead, you can use the standard PROBE-FILE function (see the manual) to check if a file exists:
CL-USER> (probe-file "not-existant-file.lisp")
NIL
CL-USER> (probe-file "/Users/myname/temp.lisp")
#P"/Users/myname/temp.lisp"
Note that the in the standard is undefined the result of applying the function to a directory, while the CCL implementation (at least on some systems) checks correctly also if a directory exists:
CL-USER> (probe-file "/Users/myname/")
#P"/Users/myname/"

STYLE-WARNING: in SLIME 2.12 SBCL 1.2.7 Gnu Emacs 25

I get this message when starting SLIME:
; loading #P"d:/lisp/slime-2.12/swank-loader.lisp"
STYLE-WARNING: redefining EMACS-INSPECT (#<SB-PCL:SYSTEM-CLASS T>) in DEFMETHOD
The REPL works normal.
I am totally new to EMACS and SLIME and I'd like to know what does this mean and how to fix it.
I use Windows 8 (64-bit), GNU Emacs 25.0.50.1 (x86_64-w64-mingw32), SLIME 2.12, and SBCL 1.2.7
I addressed folks in GitHub and jackcarrozzo replied
with this post:
Good question - it took a bit of spelunking to find a full answer. The short answer is that emacs-inspect stores handlers for inspecting objects; when a particular object is inspected, the relevant generic function (based on type) will match and be evaluated. From http://lisp-book.org/contents/chslime.pdf page 16, you can run this to see the currently-attached generics:
M-. swank-backend:emacs-inspect
That pdf also describes creating your own inspector as well as additional features that look pretty cool.
Regarding your second point: lots of stuff gets printed to the slime-events buffer; unless you're having a slime-specific issue, you probably don't need to even have it open in a window. Warnings and so forth relevant to your code and interactions will come out either directly in the REPL, in the inferior-lisp buffer, or in one of a few other buffers that emacs/slime will auto-open for you when needed.
CL-USER> (use-package :elk)
; Evaluation aborted on #<SB-KERNEL:SIMPLE-PACKAGE-ERROR "The name ~S does not designate any package." {1002C9D683}>.
CL-USER> (defun moose (a) (+ a 7))
MOOSE
CL-USER> (defun moose (a) (+ a 8))
STYLE-WARNING: redefining COMMON-LISP-USER::MOOSE in DEFUN
MOOSE
CL-USER>
So, in summary: don't worry about it. Slime makes interfacing Common Lisp in emacs simple, and it does a great job of staying out of the way. Slime does admittedly have a ton of features but it doesn't force you to use them. Note to self: I should really get around to learning them some day...

Why am I getting an unbound error for "atom?"

I'm trying to go through "The Little Lisper" and already running into snags in the first chapter. I'm relatively new to Emacs (which has fueled my interest in learning Lisp and clojure). I downloaded the Mit-scheme app, and am working the exercises on Edwin.
I'm trying:
(atom? (cons a l))
where a is an atom and l is a list already defined. I get the following error:
;Unbound variable: atom?
Why? I have no problems using the "null?" function. I thought "atom?" is an internal function checking to see if the value returned is an atom.
Any explanation would be much appreciated. I still haven't set up my emacs to run scheme, and the minor differences between all the lisp dialects is testing my patience.
In "The Little Schemer" ("The Little Lisper"'s updated version) the atom? procedure is defined as follows (because atom? doesn't exist in Scheme):
(define (atom? x)
(and (not (null? x))
(not (pair? x))))
If you're following an old version of the book, I advise you to either look for a newer version or use the same programming language used in the book: Common Lisp for The Little Lisper, Scheme for The Little Schemer - and Racket is a great Scheme IDE to work with! take a look at this answer for some tips when going through The Little Schemer using Racket.
I'm trying to go through "The Little Lisper"...
I downloaded the Mit-scheme
Common Lisp and Scheme are very different languages.
You have to either use a different book (e.g., SICP) to match your language implementation or a different language implementation (e.g., clisp or sbcl) to match your book.
Take a look at the Scheme R5RS specification; it includes a list of functions and syntactic keywords. Although not part of the Scheme standard, mit-scheme has a function apropos that will find functions (other stuff) with a given name. try:
(apropos "atom")
(but it won't show anything, :-).
An atom is something that is not a 'cons cell' (generally, if I remember my CommonLisp). In Scheme you could implement one as:
(define (atom? thing) (not (pair? thing)))
Note: this definition of atom? is consistent with CommonLisp atom.

Unix signal handling in (common) lisp

I've done a bit of research on this subject and am turning up blanks. There seem to be implementation-dependent ways of doing Unix signal handling in Common Lisp, but is there a package that gives a cross-implementation way of doing signal handling?
I would mainly like to listen for SIGINT and do a graceful shutdown in my app. I'm using Clozure CL 1.7 on linux...like mentioned, it would be great for a package for this, but if I have to resort to implementation-specific code, that's fine.
I'm also not completely married to using SIGINT (although it's ideal). I can use another signal if needed.
If this is going to be messy, does anyone have any other suggestions for gracefully shutting down a lisp app from outside the app? One idea I had is to create a file the app monitors for, and if it detects the file, it shuts down...kind of hacky, though.
Thanks!
Although out of ignorance I was originally skeptical of Daimrod's comment (first comment under the question) about using CFFI, I looked around a bit more and found http://clozure.com/pipermail/openmcl-devel/2010-July/011675.html. I adapted it to use CFFI and have confirmed this works on SBCL/CCL/clisp (probably others) on linux pretty damn well:
(defmacro set-signal-handler (signo &body body)
(let ((handler (gensym "HANDLER")))
`(progn
(cffi:defcallback ,handler :void ((signo :int))
(declare (ignore signo))
,#body)
(cffi:foreign-funcall "signal" :int ,signo :pointer (cffi:callback ,handler)))))
(set-signal-handler 2
(format t "Quitting lol!!!11~%")
;; fictional function that lets the app know to quit cleanly (don't quit from callback)
(signal-app-to-quit))
Note that from what I understand, whatever is in the body of the callback must be short and sweet! No lengthy processing. In the linked article, the macro actually creates a separate thread just for handling the signal, which is overkill for my purposes, since I'm just setting a global variable from nil to t and returning.
Anyway, hopefully this is helpful to others!
I can't find a general library for signal handling either. However, Slime implements "create a custom SIGINT handler" for most Lisp implementations. By looking at the CCL case of that code, I found ccl:*break-hook*. ccl:*break-hook* is not in the documentation, but the commit it was introduced in is located here.
This trivial example code works on my system (CCL 1.8, linux x86):
(setf ccl:*break-hook*
(lambda (cond hook)
(declare (ignore cond hook))
(format t "Cleaning up ...")
(ccl:quit)))
After this code is entered into a non-Slime REPL, sending SIGINT will cause the program to print "Cleaning up ..." and exit.
This is a late answer, but for anybody else searching for this, have a look at trivial-signal, available on Quicklisp. This is based on CFFI.
Example
(signal-handler-bind ((:int (lambda (signo)
(declare (ignorable signo))
...handler...)))
...body...)
If you use SBCL, you cannot change the signal mask without causing SBCL to crash. Ask nyef about his tips on how to fix SBCL...
So there is trivial-signal as mentioned. Here's how I catch a C-c in my code:
(handler-case
(my-app-main-function)
;; AFAIK trivial-signal is supposed to handle the implementation differences.
(#+sbcl sb-sys:interactive-interrupt
#+ccl ccl:interrupt-signal-condition
#+clisp system::simple-interrupt-condition
#+ecl ext:interactive-interrupt
#+allegro excl:interrupt-signal
() (progn
(format *error-output* "Aborting.~&")
(exit)))
(error (c) (format t "Woops, an unknown error occured:~&~a~&" c)))

Which dialect of LISP is 'Paradigms of Artificial Intelligence Programming' written in?

What version/dialect/implementation of LISP is this meant to run on?
(I do understand that the book is written in Common LISP (as specified in the introduction) and that it predates the 1994 CL standard).
Split question into two to make it clearer.
Those aren't functions. Those are variable bindings. Not everything that appears as the first thing in a form is the name of a function; the enclosing form may introduce special meaning to internal forms. That's the case with e.g. let:
(let ((action 42)
(result 51))
(+ action result))
Neither action nor result names a function in that example.
If we look at auxfns.lisp found on Peter Norvig's web page for the book, there's this bit
(eval-when (eval compile load)
;; Make it ok to place a function definition on a built-in LISP symbol.
#+(or Allegro EXCL)
(dolist (pkg '(excl common-lisp common-lisp-user))
(setf (excl:package-definition-lock (find-package pkg)) nil))
;; Don't warn if a function is defined in multiple files --
;; this happens often since we refine several programs.
#+Lispworks
(setq *PACKAGES-FOR-WARN-ON-REDEFINITION* nil)
#+LCL
(compiler-options :warnings nil)
)
which suggests its supposed to work in Franz Allegro, Lucid Lisp, or Lispworks
The dialect of Lisp is called 'Common Lisp'. The book is written in relatively portable Common Lisp.