8

Lisp is said to enable redefinitions of its core functions. I want to define an alias to the function cl:documentation function, such that

(doc 'write 'function) === (documentation 'write 'function)

How can this be done and made permanent in SBCL?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
sjas
  • 18,644
  • 14
  • 87
  • 92
  • possible duplicate of [redefining built-in function](http://stackoverflow.com/questions/15691868/redefining-built-in-function); please modify the question is you mean something else. – sds Jun 16 '14 at 21:36
  • Except that there is no information on how to make this permanent? Maybe I am just a noob, as I just started learning common lisp. – sjas Jun 16 '14 at 21:38
  • what is a 'REPL function'? – Rainer Joswig Jun 16 '14 at 21:56
  • Sorry, I meant the built-in `documentation` function such that in can be used in this shortened version, especially in the REPL. – sjas Jun 16 '14 at 22:00
  • 4
    `(setf (symbol-function 'doc) #'documentation)`. Set it or load it in the init-file for SBCL. – Rainer Joswig Jun 16 '14 at 22:23
  • This is all I actually wanted. Danke! – sjas Jun 18 '14 at 22:17

3 Answers3

14

Creating an Alias

You are not trying to redefine (i.e., change the definition of) the system function documentation, you want to define your own function with a shorter name which would do the same thing as the system function.

This can be done using fdefinition:

 (setf (fdefinition 'doc) #'documentation)

How to make your change "permanent" in common lisp

There is no standard way, different implementation may do it differently, but, generally speaking, there are two common ways.

Add code to an init file - for beginners and casual users

The code in question will be evaluated anew every time lisp starts.

Pro:

  • Easy to modify (just edit file)
  • Takes little disk space
  • Normal lisp invocation captures the change

Con:

  • Evaluated every time you start lisp (so, slows start up time if the code is slow)

Save image - for heavy-weight professionals

The modified lisp world is saved to disk.

Pro:

  • Start uptime is unaffected

Con:

  • Requires re-dumping the world on each change
  • Lisp image is usually a large file (>10MB)
  • Must specify the image at invocation time
sds
  • 58,617
  • 29
  • 161
  • 278
  • Note that init files and image saving *aren't* part of Common Lisp; those are implementation extensions. (Very common and useful, so the advice here is good, but they're not specified by the standard, so each implementation may, e.g., use different names for their init files, use different functions to save images, etc.) – Joshua Taylor Jun 17 '14 at 00:17
  • Thanks for improving your initial answer! :) – sjas Jun 18 '14 at 22:16
5

Even though @sds has already answered pretty thoroughly I just wanted to add that the utility library serapeum has defalias

PuercoPop
  • 6,707
  • 4
  • 30
  • 40
3

I use a simple macro for this:

(defmacro alias (to fn)
    `(setf (fdefinition ',to) #',fn))

e.g.

(alias neg -) => #<Compiled-function ... >

(neg 10) => -10

Other answers include detail about how to make this permanent.

Capstone
  • 2,254
  • 2
  • 20
  • 39