peaky-binders

2026-11-29

Functions that generate the code to bind lambda lists

Upstream URL

github.com/HectareaGalbis/peaky-binders

Author

Héctor Galbis Sanchis

License

MIT
README

Peaky Binders

Welcome to Peaky Binders!

This library defines functions that return the code needed to bind the variables of a lambda list. They are meant to write macros like destructuring-bind, but they support three kinds of lambda lists:

  • Ordinary lambda lists.
  • Destructuring lambda lists.
  • Macro lambda lists, where you can choose the macro name used by &whole.

Every symbol is exported from the peaky-binders package, although you can use the nickname pbinders.

Installation

Clone the repository into a directory where ASDF can find it:

cd ~/common-lisp
git clone https://github.com/HectareaGalbis/peaky-binders.git

And load it:

(asdf:load-system "peaky-binders")

Binding macros

The library defines two macros, ordinary-bind and macro-bind, and re-exports cl:destructuring-bind, so the three kinds of lambda lists can be used from the same package.

ordinary-bind

(ordinary-bind lambda-list arguments &body body)

Evaluates arguments, which must produce a list, and binds the variables of the ordinary lambda-list as if the list were the arguments of a function call. Then, body is evaluated.

(ordinary-bind (a &optional (b (* a 10)) &rest r &key c) (list 1 2 :c 3)
  (list a b r c))
;; Returns
(1 2 (:C 3) 3)
(ordinary-bind (a &optional (b (* a 10) bp)) (list 1)
  (list a b bp))
;; Returns
(1 10 NIL)

Declarations at the beginning of body affect the variables of the lambda list:

(ordinary-bind (a b) (list 1 2)
  (declare (fixnum a b))
  (+ a b))
;; Returns
3

destructuring-bind

It is the same symbol as cl:destructuring-bind.

(eq 'peaky-binders:destructuring-bind 'cl:destructuring-bind)
;; Returns
T

macro-bind

(macro-bind (lambda-list &key name environment) arguments &body body)

Evaluates arguments, which must produce the list of arguments of a macro call (without the operator), binds the variables of the macro lambda-list and evaluates body.

  • The &whole variable is bound to a list whose car is the value of name and whose cdr is the list of arguments.
  • The &environment variable is bound to the value of environment.

Both name and environment are evaluated. They default to nil.

(macro-bind ((&whole w (a b) &body body) :name 'my-macro) '((1 2) x y)
  (list w a b body))
;; Returns
((MY-MACRO (1 2) X Y) 1 2 (X Y))
(macro-bind ((&environment env a) :environment :some-environment) '(1)
  (list env a))
;; Returns
(:SOME-ENVIRONMENT 1)

Writing your own macros

The macros above are built with the following functions. They receive a lambda list, a form and a list of body forms, and return a form that evaluates the given form, binds the variables of the lambda list and evaluates the body forms.

(ordinary-bind-form lambda-list arguments body)
(destructuring-bind-form lambda-list expression body)
(macro-bind-form lambda-list arguments body &key name environment)

In macro-bind-form, name and environment are forms that are inserted into the generated code.

(destructuring-bind-form '(a (b c)) 'expression '((list a b c)))
;; Returns
(LET* ((#:LIST187
        (PEAKY-BINDERS::CHECK-ARGUMENTS EXPRESSION '(A (B C)) 2 0 NIL NIL 'NIL
                                        NIL))
       (A (CAR #:LIST187))
       (#:TAIL188 (CDR #:LIST187))
       (#:LIST189
        (PEAKY-BINDERS::CHECK-ARGUMENTS (CAR #:TAIL188) '(B C) 2 0 NIL NIL 'NIL
                                        NIL))
       (B (CAR #:LIST189))
       (#:TAIL190 (CDR #:LIST189))
       (C (CAR #:TAIL190))
       (#:TAIL191 (CDR #:TAIL190))
       (#:TAIL192 (CDR #:TAIL188)))
  (DECLARE
   (IGNORABLE #:LIST187 #:TAIL188 #:LIST189 #:TAIL190 #:TAIL191 #:TAIL192))
  (LIST A B C))

For example, this is a version of defmacro that uses macro-bind-form:

(defmacro my-defmacro (name lambda-list &body body)
  (let ((form (gensym "FORM"))
        (env (gensym "ENV"))
        (ignored (gensym "IGNORED")))
    `(defmacro ,name (&whole ,form &environment ,env &rest ,ignored)
       (declare (ignore ,ignored) (ignorable ,env))
       ,(macro-bind-form lambda-list `(cdr ,form) body
                         :name `',name
                         :environment env))))

(my-defmacro show (&whole whole &environment env form)
  `'(,whole ,(macroexpand form env)))

(symbol-macrolet ((thing 42))
  (show thing))
;; Returns
((SHOW THING) 42)

Lambda lists

The following table shows which elements are accepted by each kind of lambda list:

ElementOrdinaryDestructuringMacro
&optional, &rest, &key, &allow-other-keys, &auxYesYesYes
&wholeNoYesYes
&bodyNoYesYes
&environmentNoNoYes (top level only)
Nested patternsNoYesYes
Dotted tail (a . rest)NoYesYes

Nested patterns can be used for required parameters, &optional and &key variables, and &rest and &body variables.

Errors

lambda-list-error

Signaled when the code is generated, if the lambda list is malformed. It is a subtype of simple-error and program-error. The lambda list can be retrieved with lambda-list-error-lambda-list.

(handler-case (ordinary-bind-form '(a &rest) 'arguments '())
  (lambda-list-error (e)
    (format t "~a" e)))
;; Output
Invalid lambda list (A &REST):
  Missing variable after &REST.
;; Returns
NIL

binding-error

Signaled when the generated code is executed, if the values do not match the lambda list. It is a subtype of simple-error and program-error. The lambda list and the values can be retrieved with binding-error-lambda-list and binding-error-arguments. The values of each list are checked before binding any of its variables.

(handler-case (ordinary-bind (a b) (list 1)
                (list a b))
  (binding-error (e)
    (format t "~a" e)))
;; Output
Too few arguments: expected at least 2.
  Lambda list: (A B)
  Arguments: (1)
;; Returns
NIL
(handler-case (eval (destructuring-bind-form '(&key a) '(list :b 1) '(a)))
  (binding-error (e)
    (format t "~a" e)))
;; Output
Unknown keyword argument :B. Valid keywords: :A.
  Lambda list: (&KEY A)
  Arguments: (:B 1)
;; Returns
NIL

Reference

Macro: pbinders:ordinary-bind (lambda-list arguments &body body)

Evaluate ARGUMENTS, which must produce a list, bind the variables of the
ordinary LAMBDA-LIST as if the list were the arguments of a function call and
evaluate BODY. Declarations at the beginning of BODY affect the variables of
LAMBDA-LIST.

A LAMBDA-LIST-ERROR is signaled at macroexpansion time if LAMBDA-LIST is
malformed. A BINDING-ERROR is signaled at run time if the list does not match
LAMBDA-LIST.

Macro: pbinders:macro-bind ((lambda-list &key name environment) arguments &body body)

Evaluate ARGUMENTS, which must produce the list of arguments of a macro
call (without the operator), bind the variables of the macro LAMBDA-LIST and
evaluate BODY. Declarations at the beginning of BODY affect the variables of
LAMBDA-LIST.

NAME and ENVIRONMENT are evaluated and default to NIL. The &whole variable is
bound to (NAME . arguments) and the &environment variable is bound to the
value of ENVIRONMENT.

A LAMBDA-LIST-ERROR is signaled at macroexpansion time if LAMBDA-LIST is
malformed. A BINDING-ERROR is signaled at run time if the arguments do not
match LAMBDA-LIST.

Function: pbinders:ordinary-bind-form (lambda-list arguments body)

Return a form that evaluates the form ARGUMENTS, which must produce a list,
binds the variables of the ordinary LAMBDA-LIST as if the list were the
arguments of a function call and evaluates the forms in the list BODY.
Declarations at the beginning of BODY affect the variables of LAMBDA-LIST.

A LAMBDA-LIST-ERROR is signaled if LAMBDA-LIST is malformed. The returned form
signals a BINDING-ERROR if the list does not match LAMBDA-LIST.

Function: pbinders:destructuring-bind-form (lambda-list expression body)

Return a form that evaluates the form EXPRESSION, destructures its value
with the destructuring LAMBDA-LIST and evaluates the forms in the list BODY.
Declarations at the beginning of BODY affect the variables of LAMBDA-LIST.

A LAMBDA-LIST-ERROR is signaled if LAMBDA-LIST is malformed. The returned form
signals a BINDING-ERROR if the value does not match LAMBDA-LIST.

Function: pbinders:macro-bind-form (lambda-list arguments body &key name environment)

Return a form that evaluates the form ARGUMENTS, which must produce the
list of arguments of a macro call (without the operator), binds the variables
of the macro LAMBDA-LIST and evaluates the forms in the list BODY.
Declarations at the beginning of BODY affect the variables of LAMBDA-LIST.

NAME and ENVIRONMENT are forms inserted into the returned form. The &whole
variable is bound to (NAME . arguments) and the &environment variable is bound
to the value of ENVIRONMENT. Both default to NIL.

A LAMBDA-LIST-ERROR is signaled if LAMBDA-LIST is malformed. The returned form
signals a BINDING-ERROR if the arguments do not match LAMBDA-LIST.

Condition: pbinders:lambda-list-error

Signaled when the code is generated if a lambda list is malformed.
  • Direct slots:

    • pbinders::lambda-list
      • Allocation: :instance
      • Readers: pbinders:lambda-list-error-lambda-list
  • Metaclass: sb-pcl::condition-class

  • Precedence list: pbinders:lambda-list-error, simple-error, simple-condition, program-error, error, serious-condition, condition, sb-pcl::slot-object, t

  • Direct superclasses: simple-error, program-error

Condition: pbinders:binding-error

Signaled when the generated code is executed if the values do not match the lambda list.
  • Direct slots:

    • pbinders::lambda-list
      • Allocation: :instance
      • Readers: pbinders:binding-error-lambda-list
    • pbinders::arguments
      • Allocation: :instance
      • Readers: pbinders:binding-error-arguments
  • Metaclass: sb-pcl::condition-class

  • Precedence list: pbinders:binding-error, simple-error, simple-condition, program-error, error, serious-condition, condition, sb-pcl::slot-object, t

  • Direct superclasses: simple-error, program-error

The symbol destructuring-bind is re-exported from the common-lisp package. See destructuring-bind.

Dependencies (0)

    Dependents (1)

    • expanders
    • GitHub
    • Quicklisp