How to write particular function to file from SBCL interpreter?
Asked Answered
G

2

3

Say I've played a bit with SBCL with no SLIME, no whatsoever, plain interpreter. Now I want to save couple of functions in a file. Not an core image, just a bits of code in a text form. How should I do that?

Gautea answered 6/9, 2013 at 6:5 Comment(2)
Note that SBCL is not an interpreter but a compiler. So, when you write your (defun ...) it is directly compiled into machine code. The original Lisp code is not stored anywhere by the compiler.Committeewoman
@jlahd: note that SBCL now has both an interpreter and compiler. See sbcl.org/manual/#InterpreterSensate
I
2

If you are using sb-readline or rlwrap you press up until you hit when it got defined and copy and paste itto a file. You might have it in the termial window history too.

If none of these works only compiled definitions are available then the only way to save them is by dumping core image.

For next time, you could make a macro that stores every definition source in a special variable so that you can easily retreive them.

Ioannina answered 6/9, 2013 at 7:39 Comment(1)
I'm not sure about other terminals, but I would imagine this to be a common feature. Konsole in KDE will search the old input after you typing C-r. If you used Emacs shell, then it would be M-r or M-s. Might save you some key-presses :)Spirogyra
S
7

There are two ways to do that: use DRIBBLE and/or FUNCTION-LAMBDA-EXPRESSION

The first is to always use the Common Lisp function DRIBBLE before experimenting:

rjmba:tmp joswig$ sbcl
This is SBCL 1.1.9, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.

Dribble takes a pathname for a text file. Once called, the interactive IO will be written to that file.

* (dribble "/Lisp/tmp/2013-09-06-01.text")

* (defun foo (a) (1+ a))

FOO
* (foo 10)

11
* (quit)

See the file:

rjmba:tmp joswig$ cat 2013-09-06-01.text 

* (defun foo (a) (1+ a))

FOO
* (foo 10)

11
* (quit)

From above you should be able to see if you have any interesting functions entered... You could also set your SBCL (for example using the init file) to set up dribble always at start. Calling (dribble) without arguments ends a dribble.

Next: FUNCTION-LAMBDA-EXPRESSION:

* (defun foo (b) (1- b))

FOO

Now you can call FUNCTION-LAMBDA-EXPRESSION to get the definition back. It might be slightly altered, but it should do the job to recover valuable ideas written as code:

* (function-lambda-expression #'foo)

(SB-INT:NAMED-LAMBDA FOO
    (B)
  (BLOCK FOO (1- B)))
NIL
FOO
Sensate answered 6/9, 2013 at 7:52 Comment(2)
Did you do anything in advance to get SBCL to remember the lambda expression for function-lambda-expression? I've got SBCL 1.0.49, and after evaluating your defun in the REPL, (function-lambda-expression #'foo) just returns three values: NIL, T, FOO.Kalevala
I realized that I have a copy of 1.1.1 around as well. In there, I get the same results that you posted. I guess the point to make is that from function-lambda-expression "[any] implementation may legitimately return nil as the lambda-expression of any function," and that past versions of SBCL seem to have done this. (This came up in a past question.)Kalevala
I
2

If you are using sb-readline or rlwrap you press up until you hit when it got defined and copy and paste itto a file. You might have it in the termial window history too.

If none of these works only compiled definitions are available then the only way to save them is by dumping core image.

For next time, you could make a macro that stores every definition source in a special variable so that you can easily retreive them.

Ioannina answered 6/9, 2013 at 7:39 Comment(1)
I'm not sure about other terminals, but I would imagine this to be a common feature. Konsole in KDE will search the old input after you typing C-r. If you used Emacs shell, then it would be M-r or M-s. Might save you some key-presses :)Spirogyra

© 2022 - 2024 — McMap. All rights reserved.