\( \newcommand\D{\mathrm{d}} \newcommand\E{\mathrm{e}} \newcommand\I{\mathrm{i}} \newcommand\bigOh{\mathcal{O}} \newcommand{\cat}[1]{\mathbf{#1}} \newcommand\curl{\vec{\nabla}\times} \newcommand{\CC}{\mathbb{C}} \newcommand{\NN}{\mathbb{N}} \newcommand{\QQ}{\mathbb{Q}} \newcommand{\RR}{\mathbb{R}} \newcommand{\ZZ}{\mathbb{Z}} \)
UP | HOME

Definitions in Lisp

Table of Contents

1. Overview

Common Lisp — being a Lisp-2 — has two namespaces for definitions: one for functions, the other for values. How does this work? And how does, say, defgeneric and defmethod work?

2. Defun

Usually defun is a macro.

2.1. SBCL

As I understand it, SBCL's implementation is a macro which calls %defun. It amounts to (setf (fdefinition name) def) as one might expect.

2.2. ABCL

Armed Bear CL has a primitive %defun Java class.

3. Defgeneric

3.1. SBCL

SBCL uses a rather contrived macro roughly 90 lines long. It boils down to delegating the binding to ensure-generic-function. If we call defgeneric on an existing function, it attempts to redefine that function as a generic by clearing the existing definition and continuing on as if there were no definition; otherwise, it just calls ensure-generic-function-using-class (CLHS for ensure-generic-function). This then amounts to (setf (%funcallable-instance-fun name) ...).

3.2. ABCL

Armed Bear uses a macro. It delegates to %defgeneric (L1786-L1802). which is a wrapper around ensure-generic-function (CLHS, implementation).

3.3. CCL

CCL has a macro defining defgeneric. Like ABCL, it delegates to %defgeneric which is a wrapper around ensure-generic-function.

Last Updated 2021-06-01 Tue 10:00.