Quicklisp using sbcl: getting UNDEFINED-FUNCTION error when loading various libraries - lisp

I'm trying to use quicklisp with SBCL 1.0.55.0.debian on xubuntu. So far, it's been slow going. About half of the time, I can load a library with, e.g., (ql:quickload "cl-ppcre").
But the other half of the time, I'm getting this error instead (while trying to load hunchentoot-cgi, libxml2, and a few others):
The function :NICKNAMES is undefined.
[Condition of type UNDEFINED-FUNCTION]
I've tried all of the restarts except for 2 (accept). This happens both in emacs/SLIME and at the REPL. I don't know exactly where the source file for, say, 'sb-kernel' is, but I'm not sure I'd even know how to fix it if I did. Any suggestions?
(UPDATE: The line (:use :cl) was directly below the (:nicknames :alexandria) line in package.lisp; swapping their places fixed the problem, but I'm curious as to how it got that way in the first place.)
The full trace (for hunchentoot-cgi) is:
The function :NICKNAMES is undefined.
[Condition of type UNDEFINED-FUNCTION]
Restarts:
0: [TRY-RECOMPILING] Recompile package and try loading it again
1: [RETRY] Retry loading FASL for #<CL-SOURCE-FILE "alexandria" "package">.
2: [ACCEPT] Continue, treating loading FASL for #<CL-SOURCE-FILE "alexandria" "package"> as having been successful.
3: [ABORT] Give up on "hunchentoot-cgi"
4: [RETRY] Retry SLIME REPL evaluation request.
5: [*ABORT] Return to SLIME's top level.
--more--
Backtrace:
0: (SB-KERNEL:%COERCE-CALLABLE-TO-FUN :NICKNAMES)
1: (SB-FASL::FOP-FUNCALL)
2: (SB-FASL::LOAD-FASL-GROUP ..)
3: ((FLET SB-THREAD::WITH-RECURSIVE-LOCK-THUNK :IN SB-FASL::LOAD-AS-FASL))
4: ((FLET #:WITHOUT-INTERRUPTS-BODY-88923 :IN SB-THREAD::CALL-WITH-RECURSIVE-LOCK))
5: (SB-THREAD::CALL-WITH-RECURSIVE-LOCK ..)
6: (SB-FASL::LOAD-AS-FASL ..)
7: ((FLET SB-FASL::LOAD-STREAM :IN LOAD) ..)
8: (LOAD ..)
9: (SB-IMPL::%MAP-FOR-EFFECT-ARITY-1 ..)
10: ((SB-PCL::FAST-METHOD ASDF:PERFORM (ASDF:LOAD-OP ASDF:CL-SOURCE-FILE)) ..)
11: ((SB-PCL::EMF ASDF:PERFORM) #<unavailable argument> #<unavailable argument> #<ASDF:LOAD-OP (:VERBOSE NIL) {10ABFD01}> #<ASDF:CL-SOURCE-FILE "alexandria" "package">)
12: ((SB-PCL::FAST-METHOD ASDF::PERFORM-WITH-RESTARTS (ASDF:LOAD-OP ASDF:CL-SOURCE-FILE)) ..)
13: ((SB-PCL::FAST-METHOD ASDF::PERFORM-WITH-RESTARTS :AROUND (T T)) ..)
14: ((LAMBDA () :IN ASDF::PERFORM-PLAN))
15: ((FLET SB-THREAD::WITH-RECURSIVE-LOCK-THUNK :IN SB-C::%WITH-COMPILATION-UNIT))
16: ((FLET #:WITHOUT-INTERRUPTS-BODY-88923 :IN SB-THREAD::CALL-WITH-RECURSIVE-LOCK))
17: (SB-THREAD::CALL-WITH-RECURSIVE-LOCK ..)
18: ((FLET SB-C::WITH-IT :IN SB-C::%WITH-COMPILATION-UNIT))
19: ((SB-PCL::FAST-METHOD ASDF::PERFORM-PLAN (LIST)) ..)
20: ((LAMBDA () :IN ASDF:OPERATE))
21: (ASDF::CALL-WITH-SYSTEM-DEFINITIONS #<CLOSURE (LAMBDA # :IN ASDF:OPERATE) {108EF07D}>)
22: ((SB-PCL::FAST-METHOD ASDF:OPERATE (T T)) #<unused argument> #<unused argument> ASDF:LOAD-OP "hunchentoot-cgi" :VERBOSE NIL)
23: ((SB-PCL::EMF ASDF:OPERATE) #<unused argument> #<unused argument> ASDF:LOAD-OP "hunchentoot-cgi" :VERBOSE NIL)
24: (QUICKLISP-CLIENT::CALL-WITH-MACROEXPAND-PROGRESS #<CLOSURE (LAMBDA # :IN QUICKLISP-CLIENT::APPLY-LOAD-STRATEGY) {108EEE4D}>)
25: (QUICKLISP-CLIENT::AUTOLOAD-SYSTEM-AND-DEPENDENCIES "hunchentoot-cgi" :PROMPT NIL)
26: ((SB-PCL::FAST-METHOD QL-IMPL-UTIL::%CALL-WITH-QUIET-COMPILATION (T T)) ..)
27: ((SB-PCL::FAST-METHOD QL-IMPL-UTIL::%CALL-WITH-QUIET-COMPILATION :AROUND (QL-IMPL:SBCL T)) ..)
28: ((SB-PCL::FAST-METHOD QUICKLISP-CLIENT:QUICKLOAD (T)) #<unused argument> #<unused argument> #<unavailable argument> :PROMPT NIL :VERBOSE NIL)
29: (QL-DIST::CALL-WITH-CONSISTENT-DISTS #<CLOSURE (LAMBDA # :IN QUICKLISP-CLIENT:QUICKLOAD) {108CD455}>)
30: (SB-INT:SIMPLE-EVAL-IN-LEXENV (QUICKLISP-CLIENT:QUICKLOAD "hunchentoot-cgi") #<NULL-LEXENV>)
31: (EVAL (QUICKLISP-CLIENT:QUICKLOAD "hunchentoot-cgi"))
32: (SWANK::EVAL-REGION "(ql:quickload \"hunchentoot-cgi\")\n")
33: ((LAMBDA () :IN SWANK::REPL-EVAL))
34: (SWANK::TRACK-PACKAGE #<CLOSURE (LAMBDA # :IN SWANK::REPL-EVAL) {108CD12D}>)
35: (SWANK::CALL-WITH-RETRY-RESTART "Retry SLIME REPL evaluation request." #<CLOSURE (LAMBDA # :IN SWANK::REPL-EVAL) {108CD0BD}>)
36: (SWANK::CALL-WITH-BUFFER-SYNTAX NIL #<CLOSURE (LAMBDA # :IN SWANK::REPL-EVAL) {108CD0A5}>)
37: (SWANK::REPL-EVAL "(ql:quickload \"hunchentoot-cgi\")\n")
38: (SB-INT:SIMPLE-EVAL-IN-LEXENV (SWANK:LISTENER-EVAL "(ql:quickload \"hunchentoot-cgi\")\n") #<NULL-LEXENV>)
39: (EVAL (SWANK:LISTENER-EVAL "(ql:quickload \"hunchentoot-cgi\")\n"))
40: (SWANK:EVAL-FOR-EMACS (SWANK:LISTENER-EVAL "(ql:quickload \"hunchentoot-cgi\")\n") "COMMON-LISP-USER" 144)
41: (SWANK::PROCESS-REQUESTS NIL)
42: ((LAMBDA () :IN SWANK::HANDLE-REQUESTS))
43: ((LAMBDA () :IN SWANK::HANDLE-REQUESTS))
44: (SWANK-BACKEND::CALL-WITH-BREAK-HOOK #<FUNCTION SWANK:SWANK-DEBUGGER-HOOK> #<CLOSURE (LAMBDA # :IN SWANK::HANDLE-REQUESTS) {D2420C5}>)
45: ((FLET SWANK-BACKEND:CALL-WITH-DEBUGGER-HOOK :IN "/home/amperry/.emacs.d/slime-2.7/swank-sbcl.lisp") #<FUNCTION SWANK:SWANK-DEBUGGER-HOOK> #<CLOSURE (LAMBDA # :IN SWANK::HANDLE-REQUESTS) {D2420C5}>)
46: (SWANK::CALL-WITH-BINDINGS ..)
47: (SWANK::HANDLE-REQUESTS #<SWANK::MULTITHREADED-CONNECTION {CBAE9B1}> NIL)
48: ((FLET #:WITHOUT-INTERRUPTS-BODY-223578 :IN SB-THREAD:MAKE-THREAD))
49: ((FLET SB-THREAD::WITH-MUTEX-THUNK :IN SB-THREAD:MAKE-THREAD))
50: ((FLET #:WITHOUT-INTERRUPTS-BODY-88894 :IN SB-THREAD::CALL-WITH-MUTEX))
51: (SB-THREAD::CALL-WITH-MUTEX ..)
52: (SB-THREAD::INITIAL-THREAD-FUNCTION)
53: ("foreign function: call_into_lisp")
54: ("foreign function: funcall0")
55: ("foreign function: new_thread_trampoline")
56: ("foreign function: #xB7FA6D4C")

As Xach pointed out, the active package did not (:use :cl) by the time (:nicknames :alexandria) was called. Swapping the order of those two lines fixed the issue, although I have no idea why they were in that sequence to begin with.

Related

My recursive function to search for an element always returns nil

I want to implement a recursive function that search for an element into a list.
I have made the following code:
(defun encontrar (element lista)
(if (atom lista)
(if (eq lista element)
t
nil
)
(progn
(loop for element_lista in lista
do(if (eq (encontrar element element_lista) t)
t
)
)
nil
)
)
)
I always get nil as return. I have tryed the following example that should return t:
(encontrar #\x '(#\P #\y (#\f \x) #\A))
Let's try to find the error.
Since it is a recursive function, trace-ing can show us all intermediate steps easily:
(trace encontrar)
Now, run the same test:
(encontrar #\x '(#\P #\y (#\f \x) #\A))
0: (ENCONTRAR #\x (#\P #\y (#\f |x|) #\A))
1: (ENCONTRAR #\x #\P)
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x #\y)
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x (#\f |x|))
2: (ENCONTRAR #\x #\f)
2: ENCONTRAR returned NIL
2: (ENCONTRAR #\x |x|)
2: ENCONTRAR returned NIL
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x #\A)
1: ENCONTRAR returned NIL
0: ENCONTRAR returned NIL
If you look at the trace, something should be surprising, namely |x|. This is a notation for writing symbols literally, without case conversion (which depends on the current readtable; usually symbols are upcased when read), and with characters that should be quoted (like spaces). Here |x| is the symbols whose name is the string "x" (notice the lowercase). It is not a character, and it is present in your list due to a typo (probably), you wanted to write #\x but wrote \x, which is another way of quoting symbols.
Let's fix the test:
(encontrar #\x '(#\P #\y (#\f #\x) #\A))
0: (ENCONTRAR #\x (#\P #\y (#\f #\x) #\A))
1: (ENCONTRAR #\x #\P)
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x #\y)
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x (#\f #\x))
2: (ENCONTRAR #\x #\f)
2: ENCONTRAR returned NIL
2: (ENCONTRAR #\x #\x)
2: ENCONTRAR returned T
1: ENCONTRAR returned NIL
1: (ENCONTRAR #\x #\A)
1: ENCONTRAR returned NIL
0: ENCONTRAR returned NIL
The result is still NIL, but notice that the intermediate (ENCONTRAR #\x #\x) returns T. Somehow that result is not propagated back up.
Your else case, where the recursive invocation is made, is as follows:
(progn
(loop for element_lista in lista
do(if (eq (encontrar element element_lista) t)
t
)
)
nil
)
First of all, the formatting is not idiomatic, let's rewrite it:
(progn
(loop
for element_lista in lista
do (if (eq (encontrar element element_lista) t)
t))
nil)
Many things here:
the expression (progn e1 .. en) has for value the one of en (this is what the n in progn means). So here, you have (progn (loop ...) nil), so the first expression's value is discarded and the return value is nil (and the loop does not perform jumps)
(if test t) is the same as (if test t nil), which is equivalent to just writing test (the returned value might not be literally t, but it is non-nil, so in a boolean context it will be interpreted as true.
likewise, testing (if (eq test t) t) is redundant, because EQ already returns either T or NIL (thanks #Kaz for pointing out mistakes).
in (eq lista element) (base case), eq should not be used to compare characters in portable programs, as an implementation might return NIL for values that are char=. If you want to allow other kind of values than characters, use eql, or maybe equalp if you don't mind the test to walk the values recursively; but in any case, do not use eq because it checks whether two objects are identical, but characters (and numbers) that have the same representation might be different objects (e.g. bignums).
the return value of your loop is nil, in all cases. What you compute in do is discarded because do is used for side-effects; if you want to loop until the test is true, use (loop for x in list thereis (test x)), or equivalently (some #'test list).
This should help you debug your code.

How to define (defmacro) a macro inside a let binding in sbcl common lisp?

I am using SBCL Common Lisp. I am not an expert, but I like to think I understand it well enough to muddle along. However, I have recently encountered a strange problem with defmacro.
Why does the following code not compile, and how do I change it to make it compile?
(let ((a nil))
(defmacro testmacro ())
(testmacro))
The error is:
Unhandled UNDEFINED-FUNCTION in thread #<SB-THREAD:THREAD "main thread" RUNNING
{100399C9A3}>:
The function COMMON-LISP-USER::TESTMACRO is undefined.
Backtrace for: #<SB-THREAD:THREAD "main thread" RUNNING {100399C9A3}>
0: ((LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX))
1: (SB-IMPL::CALL-WITH-SANE-IO-SYNTAX #<CLOSURE (LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX) {1003A0EA8B}>)
2: (SB-IMPL::%WITH-STANDARD-IO-SYNTAX #<CLOSURE (LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX) {1003A0EA5B}>)
3: (PRINT-BACKTRACE :STREAM #<SB-SYS:FD-STREAM for "standard error" {10039A22B3}> :START 0 :FROM :INTERRUPTED-FRAME :COUNT NIL :PRINT-THREAD T :PRINT-FRAME-SOURCE NIL :METHOD-FRAME-STYLE NIL)
4: (SB-DEBUG::DEBUGGER-DISABLED-HOOK #<UNDEFINED-FUNCTION TESTMACRO {1003A0C193}> #<unavailable argument>)
5: (SB-DEBUG::RUN-HOOK *INVOKE-DEBUGGER-HOOK* #<UNDEFINED-FUNCTION TESTMACRO {1003A0C193}>)
6: (INVOKE-DEBUGGER #<UNDEFINED-FUNCTION TESTMACRO {1003A0C193}>)
7: (ERROR UNDEFINED-FUNCTION :NAME TESTMACRO)
8: ((LAMBDA (&REST SB-C::ARGS) :IN SB-C::INSTALL-GUARD-FUNCTION))
9: (SB-INT:SIMPLE-EVAL-IN-LEXENV (LET ((A NIL)) (DEFMACRO TESTMACRO NIL) (TESTMACRO)) #<NULL-LEXENV>)
10: (EVAL-TLF (LET ((A NIL)) (DEFMACRO TESTMACRO NIL) (TESTMACRO)) 0 NIL)
11: ((LABELS SB-FASL::EVAL-FORM :IN SB-INT:LOAD-AS-SOURCE) (LET ((A NIL)) (DEFMACRO TESTMACRO NIL) (TESTMACRO)) 0)
12: ((LAMBDA (SB-KERNEL:FORM &KEY :CURRENT-INDEX &ALLOW-OTHER-KEYS) :IN SB-INT:LOAD-AS-SOURCE) (LET ((A NIL)) (DEFMACRO TESTMACRO NIL) (TESTMACRO)) :CURRENT-INDEX 0)
13: (SB-C::%DO-FORMS-FROM-INFO #<CLOSURE (LAMBDA (SB-KERNEL:FORM &KEY :CURRENT-INDEX &ALLOW-OTHER-KEYS) :IN SB-INT:LOAD-AS-SOURCE) {10039B19FB}> #<SB-C::SOURCE-INFO {10039B19B3}> SB-C::INPUT-ERROR-IN-LOAD)
14: (SB-INT:LOAD-AS-SOURCE #<SB-SYS:FD-STREAM for "file /mnt/nas-data/Documents/Projects/SHARED PROJECTS - EVAN/pkcmd-lx/temp.lisp" {10039A5103}> :VERBOSE NIL :PRINT NIL :CONTEXT "loading")
15: ((FLET SB-FASL::LOAD-STREAM :IN LOAD) #<SB-SYS:FD-STREAM for "file /mnt/nas-data/Documents/Projects/SHARED PROJECTS - EVAN/pkcmd-lx/temp.lisp" {10039A5103}> NIL)
16: (LOAD #<SB-SYS:FD-STREAM for "file /mnt/nas-data/Documents/Projects/SHARED PROJECTS - EVAN/pkcmd-lx/temp.lisp" {10039A5103}> :VERBOSE NIL :PRINT NIL :IF-DOES-NOT-EXIST T :EXTERNAL-FORMAT :DEFAULT)
17: ((FLET SB-IMPL::LOAD-SCRIPT :IN SB-IMPL::PROCESS-SCRIPT) #<SB-SYS:FD-STREAM for "file /mnt/nas-data/Documents/Projects/SHARED PROJECTS - EVAN/pkcmd-lx/temp.lisp" {10039A5103}>)
18: ((FLET #:WITHOUT-INTERRUPTS-BODY-146 :IN SB-IMPL::PROCESS-SCRIPT))
19: (SB-IMPL::PROCESS-SCRIPT "temp.lisp")
20: (SB-IMPL::TOPLEVEL-INIT)
21: ((FLET #:WITHOUT-INTERRUPTS-BODY-82 :IN SAVE-LISP-AND-DIE))
22: ((LABELS SB-IMPL::RESTART-LISP :IN SAVE-LISP-AND-DIE))
The obvious answer is, of course, to put the defmacro outside of the let binding. But, for technical reasons, this would be very inconvenient for my project. Besides, I know of no valid reason why defining a macro under a let binding should fail.
Is 'let over macro' specifically prohibited in Common Lisp? Or am I missing something?
EDIT: The crux of my requirement is that the macro shares the same level as the function it calls, and subsequent code is not nested within it.
This is because I am attempting to write a macro that generates a function and a macro at the same time. The generated macro would call the function. So we end up with something like the following.
I write a line like:
(generate-stuff function-name)
And this resolves into:
(defun function-name-1 () ...)
(defmacro function-name (&rest args)
`(function-name-1 ,#args)
Therefore the macro and the function it calls must be at the same lexical level, adjacent to each other, and cannot create a new lexical environment (macrolet) for subsequent code to nest within.
This would all work fine, except that I happen to be within a let binding at the time; because the function in question has to refer to variables within this binding.
Note that the macro is accessible from outside the binding:
(let ...)
(defmacro my-macro ...)
(my-macro)
It seems absurd to me that a macro defined inside a let binding should only be accessible after the binding has ended. Nothing else in lisp behaves this way.
Local macros can be defined with MACROLET.
Besides, I know of no valid reason why defining a macro under a let binding should fail.
It doesn't fail. It's just not available during compile time, if you just compile the file or compile the expression. The compiler does not make defmacro definitions available in the compile-time environment, if the definition is not at top-level. Inside a progn it would be at top-level, but not inside a let.
Remember: SBCL compiles Lisp source code to machine code using a compiler. It executes machine code.
Let's look at your example:
(let ((a nil))
(defmacro testmacro ())
(testmacro))
Generally putting a global macro into a LET is bad practice and it's hard to understand what it actually should do. What should the influence of the binding of a be? Remember, a compiler compiles code before it executes.
Let's assume we have SBCL and SBCL loads a file with the above form:
(load "foo.lisp")
SBCL does now: READ the whole form, COMPILE the whole form, EXECUTE the whole form. In that order.
SBCL reads the first form. Which is the equivalent of:
CL-USER 155 > (read-from-string "(let ((a nil))
(defmacro testmacro ())
(testmacro))")
(LET ((A NIL)) (DEFMACRO TESTMACRO NIL) (TESTMACRO))
Thus we have data.
The next step is that SBCL compiles that code.
it sees a LET expression. It creates code for the LET bindings. It does not execute the LET. We are just compiling it.
it then compiles the body of the LET
it sees a DEFMACRO form: the macro gets expanded and it gets compiled to machine code. It does not execute the DEFMACRO for. We are just expanding and compiling it.
it sees a TESTMACRO form: It has no idea what it is. It compiles it as a function call to an undefined function and warns about an undefined function
The next step is that SBCL executes the compiled code
it runs the compiled LET form and creates the binding for a
it then executes the body of the LET
it executes the compiled DEFMACRO form: a global macro named TESTMACRO gets defined
it the executes the compiled TESTMACRO form: the function gets called. It is not defined. An error is signalled.
That's all logical and in no way absurd. SBCL first reads the LET, then compiles the LET to machine code and then runs the machine code.
These are three independent steps: read-time, compile-time, run-time.
Let's try it in the REPL:
First we are READing the form
* (read-from-string "(let ((a nil))
(defmacro testmacro ())
(testmacro))")
(LET ((A NIL))
(DEFMACRO TESTMACRO ())
(TESTMACRO))
129
Then we are compiling the form:
* (compile nil `(lambda () ,*))
; in: LAMBDA ()
; (LET ((A NIL))
; (DEFMACRO TESTMACRO ())
; (TESTMACRO))
;
; caught STYLE-WARNING:
; The variable A is defined but never used.
; in: LAMBDA ()
; (TESTMACRO)
;
; caught STYLE-WARNING:
; undefined function: COMMON-LISP-USER::TESTMACRO
;
; compilation unit finished
; Undefined function:
; TESTMACRO
; caught 2 STYLE-WARNING conditions
#<FUNCTION (LAMBDA ()) {226B025B}>
T
NIL
You can see that SBCL tells us about some problems. TESTMACRO is undefined.
Now we run the code:
* (funcall *)
STYLE-WARNING:
TESTMACRO is being redefined as a macro when it was previously assumed to be a function.
debugger invoked on a UNDEFINED-FUNCTION in thread
#<THREAD "main thread" RUNNING {10004F84C3}>:
The function COMMON-LISP-USER::TESTMACRO is undefined.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE ] Retry calling TESTMACRO.
1: [USE-VALUE ] Call specified function.
2: [RETURN-VALUE ] Return specified values.
3: [RETURN-NOTHING] Return zero values.
4: [ABORT ] Exit debugger, returning to top level.
("undefined function")
0]
As expected - the compiler did warn us: the function TESTMACRO is undefined.
If you want SBCL to compile a macro form, you have to make sure that the macro form is known at compile-time.

Lisp - "Trying to bind a non symbol" error

i'm a newbie in Lisp and i'm doing a project for college. The project is a simulation of the LMC (Little Man Computer). I have a state that is a list of things (state:ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag), "acc" "pc" and "flag" are values, "mem" "in" and "out" are lists.
The one-instruction takes state as parameter and should return a new state, with different values because depending on the result of (nth pc mem) i've to do certain operation. Here for exemple, if the result of (nth pc mem) is between 100 and 199 i call the funcion do-add that should give me a new value for acc for the new state (with some controls that are not implemented yet).
(defun one-instruction '(state:ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag)
((setf (nth pc mem) istruzione)
(cond ( (0 < istruzione < 99) (do-halt '(state :ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag))))
( (100 < istruzione < 199) (do-add '(state :ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag)))))))
...
(defun do-add '(state :ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag)))
((setf (nth pc mem) value)
((setf (nth (- value 100) mem) addendo)
(setf (+ acc addendo) newacc))))
When i compile and load the following errors appear:
**++++Error in One-Instruction
Trying to bind a non symbol, (state:ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag) and same happens for the "do-add".
So it's wrong the way i pass state as parameter in the two functions? Or maybe i can't use "pc" and "mem" without a getf for example?
Last question, how i can return the whole new state in one-instruction and do-add?
Thanks a lot! (And sorry for bad english, i'm italian :) )
The syntax for DEFUN expects an ordinary lambda-list, which is in its most basic form an unevaluated list of variable names. Your code starts as follows:
(defun one-instruction '(state:ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag)
...)
You have two main errors:
you quoted the list
your lambda-list is malformed
Trying to bind a non symbol, (state:ACC acc :PC pc :MEM mem :IN in :OUT out :FLAG flag)
The error is a little bit weird, but remember that '(a b c) stands for (quote (a b c)), which in the context of a defun lambda-list is parsed as a two-element list, quote and the (a b c) list. The second list is not a symbol, which is how the malformed lambda-list is detected in your case.
The :pc pc syntax is used to pass keyword arguments, not to bind them in functions. If you want to define your function properly, with one mandatory state variables and keyword arguments, you should write:
(defun one-instruction (state &key acc pc mem in out flag)
...)
And you would call it as follows:
(one-instruction my-state :acc 0 :pc 0 :mem big-table ...)
Also, you have:
((setf (nth pc mem) istruzione) ...)
This is not a valid expression, the expression looks like (x ...), with x being the setf expression; but (x ...) in a normal evaluation context means function call, and (setf ...) is no function.
In: (setf (nth pc mem) value) i wanted to bind the result of the (nth pc mem), that i'f i'm correct it should give me the value in the list "mem" at the position "pc", to the variable "value"
You do not introduce variables with setf, which only modifies existing bindings (and more generally, places). Instead, you have to use LET:
(let ((instruction (nth n mem)))
...)
Inside ..., instruction is bound to the value obtained by evaluating (nth n mem).
Note also that your test expressions in cond expression are malformed too:
(0 < istruzione < 99)
The above reads as: call the function 0 with arguments <, istruzione, < and 99; there is no function named 0, and < is unbound as a variable. What you wanted instead is:
(< 0 istruzione 99)
The above is a call to < with multiple arguments, which is true when all the values are sorted according to <.
Note also that in the next test, you have 100 < istruzione, which means both 99 and 100 are corner cases that are not handled by your cond (except if it is on purpose, in which case its fine).

SBCL -Common Lisp Error

I am trying to run the repository from the following link:
https://github.com/JafferWilson/test-program
I am getting this error:
sbcl --script paraphrasing.lisp
Unhandled SB-KERNEL:SIMPLE-PACKAGE-ERROR in thread #<SB-THREAD:THREAD
"main thread" RUNNING
{100399C6D3}>:
The name "USER" does not designate any package.
Backtrace for: #<SB-THREAD:THREAD "main thread" RUNNING {100399C6D3}>
0: ((LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX))
1: (SB-IMPL::CALL-WITH-SANE-IO-SYNTAX #<CLOSURE (LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX) {10039E3C1B}>)
2: (SB-IMPL::%WITH-STANDARD-IO-SYNTAX #<CLOSURE (LAMBDA NIL :IN SB-DEBUG::FUNCALL-WITH-DEBUG-IO-SYNTAX) {10039E3BEB}>)
3: (PRINT-BACKTRACE :STREAM #<SB-SYS:FD-STREAM for "standard error" {10039A22B3}> :START 0 :FROM :INTERRUPTED-FRAME :COUNT NIL :PRINT-THREAD T :PRINT-FRAME-SOURCE NIL :METHOD-FRAME-STYLE NIL)
4: (SB-DEBUG::DEBUGGER-DISABLED-HOOK #<SB-KERNEL:SIMPLE-PACKAGE-ERROR "The name ~S does not designate any package." {10039E1643}> #<unavailable argument>)
5: (SB-DEBUG::RUN-HOOK *INVOKE-DEBUGGER-HOOK* #<SB-KERNEL:SIMPLE-PACKAGE-ERROR "The name ~S does not designate any package." {10039E1643}>)
6: (INVOKE-DEBUGGER #<SB-KERNEL:SIMPLE-PACKAGE-ERROR "The name ~S does not designate any package." {10039E1643}>)
7: (ERROR SB-KERNEL:SIMPLE-PACKAGE-ERROR :PACKAGE "USER" :FORMAT-CONTROL "The name ~S does not designate any package." :FORMAT-ARGUMENTS ("USER"))
8: (SB-INT:%FIND-PACKAGE-OR-LOSE "USER")
9: (SB-INT:FIND-UNDELETED-PACKAGE-OR-LOSE "USER")
10: ((LAMBDA NIL :IN "/home/aims/paraphrasing/paraphrasing.lisp"))
11: (SB-INT:SIMPLE-EVAL-IN-LEXENV (SETQ *PACKAGE* (SB-INT:FIND-UNDELETED-PACKAGE-OR-LOSE "USER")) #<NULL-LEXENV>)
12: (SB-INT:SIMPLE-EVAL-IN-LEXENV (IN-PACKAGE "USER") #<NULL-LEXENV>)
13: (EVAL-TLF (IN-PACKAGE "USER") 0 NIL)
14: ((LABELS SB-FASL::EVAL-FORM :IN SB-INT:LOAD-AS-SOURCE) (IN-PACKAGE "USER") 0)
15: ((LAMBDA (SB-KERNEL:FORM &KEY :CURRENT-INDEX &ALLOW-OTHER-KEYS) :IN SB-INT:LOAD-AS-SOURCE) (IN-PACKAGE "USER") :CURRENT-INDEX 0)
16: (SB-C::%DO-FORMS-FROM-INFO #<CLOSURE (LAMBDA (SB-KERNEL:FORM &KEY :CURRENT-INDEX &ALLOW-OTHER-KEYS) :IN SB-INT:LOAD-AS-SOURCE) {10039A7ECB}> #<SB-C::SOURCE-INFO {10039A7E83}> SB-C::INPUT-ERROR-IN-LOAD)
17: (SB-INT:LOAD-AS-SOURCE #<SB-SYS:FD-STREAM for "file /home/aims/paraphrasing/paraphrasing.lisp" {10039A4703}> :VERBOSE NIL :PRINT NIL :CONTEXT "loading")
18: ((FLET SB-FASL::LOAD-STREAM :IN LOAD) #<SB-SYS:FD-STREAM for "file /home/aims/paraphrasing/paraphrasing.lisp" {10039A4703}> NIL)
19: (LOAD #<SB-SYS:FD-STREAM for "file /home/aims/paraphrasing/paraphrasing.lisp" {10039A4703}> :VERBOSE NIL :PRINT NIL :IF-DOES-NOT-EXIST T :EXTERNAL-FORMAT :DEFAULT)
20: ((FLET SB-IMPL::LOAD-SCRIPT :IN SB-IMPL::PROCESS-SCRIPT) #<SB-SYS:FD-STREAM for "file /home/aims/paraphrasing/paraphrasing.lisp" {10039A4703}>)
21: ((FLET #:WITHOUT-INTERRUPTS-BODY-146 :IN SB-IMPL::PROCESS-SCRIPT))
22: (SB-IMPL::PROCESS-SCRIPT "paraphrasing.lisp")
23: (SB-IMPL::TOPLEVEL-INIT)
24: ((FLET #:WITHOUT-INTERRUPTS-BODY-82 :IN SAVE-LISP-AND-DIE))
25: ((LABELS SB-IMPL::RESTART-LISP :IN SAVE-LISP-AND-DIE))
unhandled condition in --disable-debugger mode, quitting
Kindly, help me to resolve this so the program runs effectively.
For SBCL:
Create a package and use only the package COMMON-LISP:
(defpackage "PARAPHRASING"
(:use "COMMON-LISP"))
(in-package "PARAPHRASING")
Replace all DEFCONSTANT defining lists with DEFPARAMETER
(defparameter +bad-single-pos-paraphrasings+
(mapcar #'intern '("TO" "DT" "CC" "IN" "WDT" "POS" "PRP")))
(defparameter +input-paren-types+
(list (list "[[" (intern "NN")) (list "((" (intern "VB"))))
;; Stuff with no alphabetic chars is ignored in any case
(defparameter +auxiliaries+ (mapcar #'intern '("DT" "IN" "TO")))
;; Lengths taken for rules
(defparameter +context-lens+ '(1))
Make sure the return value of NREVERSE is used
(defun extract-paraphrases1 (pair rule)
(labels (;; Find all positions where prfx is in seq.
(find-prefix-positions (prfx seq)
(if (null (first prfx))
;; Must be at the beginning.
(and (pos-match-prefix (rest prfx) seq) (list -1))
(pos-sub-positions prfx seq)))
;; Find all positions where sffx is in seq - after some point.
(find-suffix-positions (skip sffx seq)
(let ((lst (nthcdr skip seq)))
(if (null (first sffx))
;; a trick - nreverse both lists, not expensive
(let* ((slen (length (rest sffx)))
(sffx (nreverse (rest sffx)))
(lst (nreverse lst))
(m? (pos-match-prefix sffx lst)))
;; restore things
(setf sffx (nreverse sffx)) ; <- use the result
(setf lst (nreverse lst)) ; <- use the result
...
INTERN
All the calls to INTERN should use the package name "PARAPHRASING" as a second parameter. This way the symbols are always created in the correct package.
Use
* (paraphrasing::co-train "20000leagues-tagged-pairs-roots.txt")
Reading input...
Done, 6275 sentences read.
========== Iteration #1/10 ==========
Learning context rules...
...6250 left
...6000 left
...

Dynamically built functions in LISP

I have a question concerning dynamically built functions (or something comparable). In Java I can write some Source programmatically into a String, compile this string and execute it like a function several times.
Imagine I have some genetic algorithm to create the best code for taking n input parameters, calculate them according the genome and returning m output parameters. So I wonder if it's possible (and I'm quite sure it is), to create a list of lists of lists .... containing the function, and then call this function some thousand times with different input parameters to calculate the error rate.
What I do need now is an example, how to create such a list programmatically and how to use it. Currently I'm totally stuck.
Any references or examples warmly welcomed.
Lisp code is data: lists, symbols, numbers, ...
(defun foo () 42)
Lisp has functions like list and +. You can use it:
(list 'defun
'foo
'()
(+ 25 17))
How do you compile a function? Eval the code and compile it.
(compile (eval my-function-definition))
So put together:
CL-USER 10 > (compile (eval (list 'defun 'foo '() (+ 25 17))))
FOO
NIL
NIL
CL-USER 11 > (foo)
42
Is it really compiled?
CL-USER 11 > (disassemble 'foo)
424000EA3C:
0: 49396275 cmpq [r10+75], rsp
4: 7720 ja L1
6: 4883F900 cmpq rcx, 0
10: 751A jne L1
12: 4157 push r15
14: 55 push rbp
15: 4889E5 moveq rbp, rsp
18: 4989DF moveq r15, rbx
21: BF50010000 move edi, 150
26: B901000000 move ecx, 1
31: 4889EC moveq rsp, rbp
34: 5D pop rbp
35: 415F pop r15
37: C3 ret
L1: 38: 41FFA6E7020000 jmp [r14+2E7] ; SYSTEM::*%WRONG-NUMBER-OF-ARGUMENTS-STUB
45: 90 nop
46: 90 nop
47: 90 nop
48: 90 nop
49: 90 nop
50: 90 nop
51: 90 nop
Seems so...
As Rainer Joswig said, you should read some Lisp book (particularily the chapters about list manipulation and macros), but here's a very simple example using compile:
(defun test (op number)
(let ((func (compile nil `(lambda (y)
(,op y y)))))
(format t "The function is ~:[not compiled~;compiled~].~%"
(compiled-function-p func))
(funcall func number)))
(test '+ 5) ; => 10
(test '* 5) ; => 25
This constructs the lambda expression using backquote syntax, but you could just build it with regular list manipulation operations (push, cons and such) as well.