j14i/cl-macros huggingface dataset:
Write a Common Lisp macro to handle this code pattern:
(match list ((list x) (if (evenp x) (fail) x)))

Problem: Macro call that should be transformed by match

Macro definition:
(defmacro match (arg &body clauses)
  "Matches ARG with CLAUSES. CLAUSES is a list of the form of (PATTERN
. BODY) where PATTERN is a pattern specifier and BODY is an implicit
progn. If ARG matches some PATTERN, `match` then evaluates
the corresponding BODY and returns the evaluated value.  If no pattern matches,
then returns NIL.

Evaluating a form (FAIL) in the clause body causes the latest pattern
matching to fail. For example,

    (match 1
      (x (if (eql x 1)
             (fail)
             x))
      (_ 'ok))

returns OK, because the form (FAIL) in the first clause is
evaluated.

If BODY starts with the symbols WHEN or UNLESS, then the next form
will be used to introduce (FAIL). That is,

    (match list ((list x) when (oddp x) x))
    (match list ((list x) unless (evenp x) x))

will be translated to

    (match list ((list x) (if (oddp x) x (fail))))
    (match list ((list x) (if (evenp x) (fail) x)))

Examples:

    (match 1 (1 1))
    => 1
    (match 1 (2 2))
    => 2
    (match 1 (x x))
    => 1
    (match (list 1 2 3)
      (list x y z) (+ x y z))
    => 6"
  (compile-match-1 arg clauses nil))

Macroexpansion:
(%or (%if (consp list) (let ((#:g146 (car list)) (#:g147 (cdr list))) (declare (ignorable #:g146 #:g147)) (%if (%equals #:g147 nil) (%or (let ((x #:g146)) (if (evenp x) (fail) x)) (fail)) (fail))) (fail)) nil)
You are a Common Lisp macro expert. Given this code pattern:
(destructuring-bind* nil
    nil)

Write a macro that addresses: Macro call that should be transformed by DESTRUCTURING-BIND*

Macro definition:
(defmacro DESTRUCTURING-BIND* ...)

Macroexpansion:
(let (#:in-body126)
  (flet ((#:handle127 (#:condition128)
           (unless #:in-body126
             (error
              (make-condition 'destructuring-error :lambda-list 'nil
                              :expression 'nil :reason #:condition128)))))
    (declare (ignorable (function #:handle127)))
    (let ((#:value129 nil))
      (handler-bind ((error #'#:handle127))
        (destructuring-bind () #:value129 (setf #:in-body126 t) (locally))))))
You are a Common Lisp macro expert. Given this code pattern:
(do-matches-as-strings (match regex target-string (nreverse result-list) :start start :end end :sharedp sharedp) (push match result-list))

Write a macro that addresses: Macro call that should be transformed by do-matches-as-strings

Macro definition:
(defmacro do-matches-as-strings ((match-var regex
                                            target-string
                                            &optional result-form
                                            &key start end sharedp)
                                 &body body)
  "Iterates over TARGET-STRING and tries to match REGEX as often as
possible evaluating BODY with MATCH-VAR bound to the substring of
TARGET-STRING corresponding to each match in turn.  After the last
match, returns RESULT-FORM if provided or NIL otherwise.  An implicit
block named NIL surrounds DO-MATCHES-AS-STRINGS; RETURN may be used to
terminate the loop immediately.  If REGEX matches an empty string the
scan is continued one position behind this match.  If SHAREDP is true,
the substrings may share structure with TARGET-STRING.  BODY may start
with declarations."
  (with-rebinding (target-string)
    (with-unique-names (match-start match-end substr-fn)
      `(let ((,substr-fn (if ,sharedp #'nsubseq #'subseq)))
        ;; simple use DO-MATCHES to extract the substrings
        (do-matches (,match-start ,match-end ,regex ,target-string
                     ,result-form :start ,start :end ,end)
          (let ((,match-var
                  (funcall ,substr-fn
                           ,target-string ,match-start ,match-end)))
            ,@body))))))

Macroexpansion:
(let ((#:target-string145 target-string)) (let ((#:substr-fn148 (if sharedp (function nsubseq) (function subseq)))) (do-matches (#:match-start146 #:match-end147 regex #:target-string145 (nreverse result-list) :start start :end end) (let ((match (funcall #:substr-fn148 #:target-string145 #:match-start146 #:match-end147))) (push match result-list)))))
Generate a defmacro form that transforms code like this:
(lret ((out (serapeum::make-sequence-like array len))) (replace* out array))

Pattern to recognize: Macro call that should be transformed by lret

Macro definition:
(defmacro lret ((&rest bindings) &body body)
  "Return the initial value of the last binding in BINDINGS. The idea
is to create something, initialize it, and then return it.

    (lret ((x 1)
           (y (make-array 1)))
      (setf (aref y 0) x))
    => #(1)

Note that the value returned is the value initially bound. Subsequent
assignments are ignored.

    (lret ((x 1))
      (setf x 2))
    => 1

Furthermore, on Lisps that support it, the variable may be made
read-only, making assignment a compiler-time error.

`lret' may seem trivial, but it fufills the highest purpose a macro
can: it eliminates a whole class of bugs (initializing an object, but
forgetting to return it).

Cf. `aprog1' in Anaphora."
  `(lret-aux let ,bindings
     ,@body))

Macroexpansion:
(lret-aux let ((out (serapeum::make-sequence-like array len))) (replace* out array))
Write a Common Lisp macro to handle this code pattern:
(with-took-usec took-usec (query (funcall (query-prepared query) params) :database (connection-handle conn) :store nil))

Problem: Macro call that should be transformed by with-took-usec

Macro definition:
(defmacro with-took-usec (var &body body)
  (let ((before (gensym "BEFORE")))
    `(let ((,before (get-internal-real-time)))
       (multiple-value-prog1 (progn ,@body)
         (setf ,var (ceiling
                      (* (- (get-internal-real-time) ,before)
                         #.(/ 1000000 internal-time-units-per-second))))))))

Macroexpansion:
(multiple-value-bind (#:before-unixtime163 #:before-usec164) (sb-ext:get-time-of-day) (multiple-value-prog1 (progn (query (funcall (query-prepared query) params) :database (connection-handle conn) :store nil)) (multiple-value-bind (#:after-unixtime165 #:after-usec166) (sb-ext:get-time-of-day) (setf took-usec (+ (* (- #:after-unixtime165 #:before-unixtime163) 1000000) (- #:after-usec166 #:before-usec164))))))
Define a macro that eliminates this repetitive pattern:
(defun-compile-time walk-lambda-list (map-function reduce-function screamer? partial? nested? lambda-list environment) (check-lambda-list lambda-list) (if reduce-function (funcall reduce-function (funcall map-function lambda-list 'lambda-list) (walk-lambda-list-reducing map-function reduce-function screamer? partial? nested? lambda-list environment)) (funcall map-function lambda-list 'lambda-list)))

Issue: Macro call that should be transformed by defun-compile-time

Macro definition:
(defmacro defun-compile-time (function-name lambda-list &body body)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (cl:defun ,function-name ,lambda-list ,@body)
     (eval-when (:compile-toplevel) (compile ',function-name))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (common-lisp:defun walk-lambda-list (map-function reduce-function screamer? partial? nested? lambda-list environment) (check-lambda-list lambda-list) (if reduce-function (funcall reduce-function (funcall map-function lambda-list (quote lambda-list)) (walk-lambda-list-reducing map-function reduce-function screamer? partial? nested? lambda-list environment)) (funcall map-function lambda-list (quote lambda-list)))) (eval-when (:compile-toplevel) (compile (quote walk-lambda-list))))
Define a macro that eliminates this repetitive pattern:
(lret ((x 1) (y 2)) x)

Issue: Macro call that should be transformed by lret

Macro definition:
(defmacro lret ((&rest bindings) &body body)
  "Return the initial value of the last binding in BINDINGS. The idea
is to create something, initialize it, and then return it.

    (lret ((x 1)
           (y (make-array 1)))
      (setf (aref y 0) x))
    => #(1)

Note that the value returned is the value initially bound. Subsequent
assignments are ignored.

    (lret ((x 1))
      (setf x 2))
    => 1

Furthermore, on Lisps that support it, the variable may be made
read-only, making assignment a compiler-time error.

`lret' may seem trivial, but it fufills the highest purpose a macro
can: it eliminates a whole class of bugs (initializing an object, but
forgetting to return it).

Cf. `aprog1' in Anaphora."
  `(lret-aux let ,bindings
     ,@body))

Macroexpansion:
(lret-aux let ((x 1) (y 2)) x)
Generate a defmacro form that transforms code like this:
(defconstant +spaces+ (make-string 2000 :initial-element #\Space :element-type 'base-char) "Used for indentation.")

Pattern to recognize: Macro call that should be transformed by defconstant

Macro definition:
(defmacro defconstant (name value &optional doc)
  "Make sure VALUE is evaluated only once \(to appease SBCL & clasp)."
  `(cl:defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
     ,@(when doc (list doc))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (sb-impl::%defconstant (quote +spaces+) (make-string 2000 :initial-element #\Space :element-type (quote base-char)) (sb-c:source-location) (quote "Used for indentation.")))
You are a Common Lisp macro expert. Given this code pattern:
(bind (((:labels doit (x)) "whatever" (declare (type fixnum x)) (setf x (* 2 x)) (setf x (+ x 3)) x)) (ensure-same (doit 1) 5) (ensure-same (doit 2) 7))

Write a macro that addresses: Macro call that should be transformed by bind

Macro definition:
(defmacro bind ((&rest bindings) &body body)
  "Bind is a replacement for let*, destructuring-bind, multiple-value-bind and more. 

An example is probably the best way to describe its syntax:

    \(bind \(\(a 2\)
           \(\(b &rest args &key \(c 2\) &allow-other-keys\) '\(:a :c 5 :d 10 :e 54\)\)
           \(\(:values d e\) \(truncate 4.5\)\)
           \(\(:structure xxx- slot1 slot2\) \(make-xxx\)\)
           \(\(:flet name \(arg1 arg2\)\) \(+ arg1 arg2\)\)\)
         \(list a b c d e args\)\)

Simple bindings are as in let*. Destructuring is done if the first item
in a binding is a list. Multiple value binding is done if the first item
in a binding is a list and the first item in the list is ':values'. Other
forms have their own syntax. For example, :structure first has the conc
name and then slot names whereas :flet has the function name and a list
of arguments and then the function body (in an implicit progn)."
  (let (declarations)
    (loop while (and (consp (car body)) (eq (caar body) 'declare)) do
          (push (first body) declarations)
          (setf body (rest body)))
    (if bindings
	(let ((*all-declarations* (bind-expand-declarations (nreverse declarations))))
	  (prog1
	      (bind-macro-helper bindings body)
	    (check-for-unused-variable-declarations *all-declarations*)))
        `(locally
             ,@declarations
           ,@body))))

Macroexpansion:
(labels ((doit (x) "whatever" (declare (type fixnum x)) (progn (setf x (* 2 x)) (setf x (+ x 3)) x))) (progn (ensure-same (doit 1) 5) (ensure-same (doit 2) 7)))
Write a Common Lisp macro to handle this code pattern:
(defsynonym step :step)

Problem: Macro call that should be transformed by defsynonym

Macro definition:
(defmacro defsynonym (symbol synonym)
  "Set SYNONYM for a SYMBOL. "
  `(eval-always
     (setf (get ',symbol 'synonym) ',synonym)))

Macroexpansion:
(eval-always (setf (get (quote step) (quote synonym)) (quote :step)))
You are a Common Lisp macro expert. Given this code pattern:
(defun/1 toupper (c) (char-upcase (code-char c)))

Write a macro that addresses: Macro call that should be transformed by defun/1

Macro definition:
(defmacro defun/1 (name arglist &body body)
  "Lisp-1 defun; makes function pointers work."
  `(progn
     (defun ,name ,arglist
       (declare (optimize (speed 0) (debug 3)
                          ;; FIXME: Why do minor warnings result in load errors?
                          #+sbcl (sb-ext:inhibit-warnings 3)))
        
       ,@body)
     (defparameter ,name (vacietis.c:mkptr& (symbol-function ',name)))))

Macroexpansion:
(progn (defun toupper (c) (declare (optimize (speed 0) (debug 3) (sb-ext:inhibit-warnings 3))) (char-upcase (code-char c))) (defparameter toupper (vacietis.c:mkptr& (symbol-function (quote toupper)))))
Write a Common Lisp macro to handle this code pattern:
(lambda+ nil
  (progn))

Problem: Macro call that should be transformed by LAMBDA+

Macro definition:
(defmacro LAMBDA+ ...)

Macroexpansion:
(lambda ()
  (let+ nil
    (progn)))
Generate a defmacro form that transforms code like this:
(fgatherlet nil
  (progn))

Pattern to recognize: Macro call that should be transformed by FGATHERLET

Macro definition:
(defmacro FGATHERLET ...)

Macroexpansion:
(flet ()
  (progn))
Create a Common Lisp macro for the following transformation. Before code:
(define-custom-var (:beginning-of-object *beginning-of-object-handler*))

What needs to change: Macro call that should be transformed by define-custom-var

Macro definition:
(defmacro define-custom-var ((key name) &rest other-args)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (progn (pushnew '(,name . ,key) *custom-vars* :test #'equal)
            (defvar ,name ,@other-args))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (progn (pushnew (quote (*beginning-of-object-handler* . :beginning-of-object)) *custom-vars* :test (function equal)) (defvar *beginning-of-object-handler*)))
Generate a defmacro form that transforms code like this:
(var nil :key nil)

Pattern to recognize: Macro call that should be transformed by VAR

Macro definition:
(defmacro VAR ...)

Macroexpansion:
(defparameter nil :key)
You are a Common Lisp macro expert. Given this code pattern:
(define-error clone-global-env-error () () "Cannot clone the global environment")

Write a macro that addresses: Macro call that should be transformed by define-error

Macro definition:
(defmacro define-error (name (&key (error-type 'varjo-error) (prefix "Varjo"))
                            (&rest args) error-string &body body)
  `(defcondition ,name (:error-type ,error-type :prefix ,prefix) ,args
       ,error-string ,@body))

Macroexpansion:
(defcondition clone-global-env-error (:error-type varjo-error :prefix "Varjo") nil "Cannot clone the global environment")
You are a Common Lisp macro expert. Given this code pattern:
(enable-quasi-quoted-list-to-list-emitting-form-syntax)

Write a macro that addresses: Macro call that should be transformed by ENABLE-QUASI-QUOTED-LIST-TO-LIST-EMITTING-FORM-SYNTAX

Macro definition:
(defmacro ENABLE-QUASI-QUOTED-LIST-TO-LIST-EMITTING-FORM-SYNTAX ...)

Macroexpansion:
(eval-when (:compile-toplevel :execute)
  (setf *readtable* (copy-readtable *readtable*))
  (set-quasi-quoted-list-to-list-emitting-form-syntax-in-readtable
   :start-character #\GRAVE_ACCENT :end-character nil :unquote-character
   #\COMMA :splice-character #\COMMERCIAL_AT :destructive-splice-character
   #\FULL_STOP :dispatched-quasi-quote-name nil)
  (values))
Create a Common Lisp macro for the following transformation. Before code:
(define-layered-class 'some-name)

What needs to change: Macro call that should be transformed by DEFINE-LAYERED-CLASS

Macro definition:
(defmacro DEFINE-LAYERED-CLASS ...)

Macroexpansion:
(defclass 'some-name nil nil (:metaclass layered-class) (:in-layer . t))
Create a Common Lisp macro for the following transformation. Before code:
(enable-quasi-quote-syntax nil (progn))

What needs to change: Macro call that should be transformed by ENABLE-QUASI-QUOTE-SYNTAX

Macro definition:
(defmacro ENABLE-QUASI-QUOTE-SYNTAX ...)

Macroexpansion:
(eval-when (:compile-toplevel :execute)
  (setf *readtable* (copy-readtable *readtable*))
  (set-quasi-quote-syntax-in-readtable nil (progn) :nested-quasi-quote-wrapper
                                       quasi-quote-wrapper :start-character nil
                                       :dispatch-character nil :end-character
                                       nil :unquote-character #\COMMA
                                       :splice-character #\COMMERCIAL_AT
                                       :destructive-splice-character
                                       #\FULL_STOP :dispatched-quasi-quote-name
                                       nil :toplevel-reader-wrapper #'identity
                                       :unquote-readtable-case nil :body-reader
                                       nil :readtable-case nil)
  (values))
Create a Common Lisp macro for the following transformation. Before code:
(defclause finding (var condition) "Same as FOR, but perhaps nicer to read when used in conjunction with macros like MAXIMIZING." `(for ,var ,condition))

What needs to change: Macro call that should be transformed by defclause

Macro definition:
(defmacro defclause (name arglist &body body)
  "Same as cl:defmacro, but additionally records <name> in *clauses* as a known clause macro."
  `(progn
     (defmacro ,name ,arglist ,@body)
     (pushnew ',name *clauses*)))

Macroexpansion:
(progn (defmacro finding (var condition) "Same as FOR, but perhaps nicer to read when used in conjunction with macros like MAXIMIZING." (sb-int:quasiquote (for #S(sb-impl::comma :expr var :kind 0) #S(sb-impl::comma :expr condition :kind 0)))) (pushnew (quote finding) *clauses*))
You are a Common Lisp macro expert. Given this code pattern:
(ematch0 clause ((list* patterns body) (let ((patterns (ensure-list patterns))) (list* (if (zerop max) nil (append patterns (make-list (- max (length patterns)) :initial-element '_))) body))))

Write a macro that addresses: Macro call that should be transformed by ematch0

Macro definition:
(defmacro ematch0 (what &body clauses)
  `(match0 ,what
     ,@clauses
     (_ (error "level0 match error!"))))

Macroexpansion:
(match0 clause ((list* patterns body) (let ((patterns (ensure-list patterns))) (list* (if (zerop max) nil (append patterns (make-list (- max (length patterns)) :initial-element (quote _)))) body))) (_ (error "level0 match error!")))
Generate a defmacro form that transforms code like this:
(defsynonym minimizing :min)

Pattern to recognize: Macro call that should be transformed by defsynonym

Macro definition:
(defmacro defsynonym (symbol synonym)
  "Set SYNONYM for a SYMBOL. "
  `(eval-always
     (setf (get ',symbol 'synonym) ',synonym)))

Macroexpansion:
(eval-always (setf (get (quote minimizing) (quote synonym)) (quote :min)))
Define a macro that eliminates this repetitive pattern:
(some-<>
  nil
  :key
  nil)

Issue: Macro call that should be transformed by SOME-<>

Macro definition:
(defmacro SOME-<> ...)

Macroexpansion:
(let* ((#:var180 nil)
       (#:var181
        (if #:var180
            (:key #:var180)
            nil))
       (#:var182
        (if #:var181
            (nil #:var181)
            nil)))
  #:var182)
Create a Common Lisp macro for the following transformation. Before code:
(setdocs (type environment "Container class for a current \"environment\". An instance of this class is intended to represent most of the runtime environment present at a particular point. It is useful for stashing away debug information for inspection at a later date. See CAPTURE-ENVIRONMENT See ENVIRONMENT-CONDITION See ENVIRONMENT-STACK See ENVIRONMENT-RESTARTS See ENVIRONMENT-THREAD") (function environment-condition "Returns the condition stored in the environment (if any). See CL:CONDITION See ENVIRONMENT") (function environment-stack "Returns a list of calls stored in the environment (if any). See CALL See ENVIRONMENT") (function environment-restarts "Returns a list of restarts stored in the environment (if any). See CL:RESTART See ENVIRONMENT") (function environment-thread "Returns the thread stored in the environment (if any). See SB-THREAD:THREAD See THREADS:THREAD See MP:PROCESS See MT:THREAD See CCL:PROCESS See PROCESS:PROCESS See THREAD:THREAD See ENVIRONMENT") (function capture-environment "Capture the current environment into an environment object. See ENVIRONMENT"))

What needs to change: Macro call that should be transformed by setdocs

Macro definition:
(defmacro setdocs (&body pairs)
  `(progn
     ,@(loop for (type var doc) in pairs
             collect `(setf (documentation ',var ',type) ,doc))))

Macroexpansion:
(progn (setf (documentation (quote environment) (quote type)) "Container class for a current \"environment\". An instance of this class is intended to represent most of the runtime environment present at a particular point. It is useful for stashing away debug information for inspection at a later date. See CAPTURE-ENVIRONMENT See ENVIRONMENT-CONDITION See ENVIRONMENT-STACK See ENVIRONMENT-RESTARTS See ENVIRONMENT-THREAD") (setf (documentation (quote environment-condition) (quote function)) "Returns the condition stored in the environment (if any). See CL:CONDITION See ENVIRONMENT") (setf (documentation (quote environment-stack) (quote function)) "Returns a list of calls stored in the environment (if any). See CALL See ENVIRONMENT") (setf (documentation (quote environment-restarts) (quote function)) "Returns a list of restarts stored in the environment (if any). See CL:RESTART See ENVIRONMENT") (setf (documentation (quote environment-thread) (quote function)) "Returns the thread stored in the environment (if any). See SB-THREAD:THREAD See THREADS:THREAD See MP:PROCESS See MT:THREAD See CCL:PROCESS See PROCESS:PROCESS See THREAD:THREAD See ENVIRONMENT") (setf (documentation (quote capture-environment) (quote function)) "Capture the current environment into an environment object. See ENVIRONMENT"))
You are a Common Lisp macro expert. Given this code pattern:
(with-destructured-lambda-form nil
    :key
  nil)

Write a macro that addresses: Macro call that should be transformed by WITH-DESTRUCTURED-LAMBDA-FORM

Macro definition:
(defmacro WITH-DESTRUCTURED-LAMBDA-FORM ...)

Macroexpansion:
(destructuring-bind
    (#:function135 #:name136 #:qualifiers137 #:lambda-list138 #:docstring139
     #:declarations140 #:forms141)
    (split-lambda-form :key)
  (declare
   (ignore #:function135 #:name136 #:qualifiers137 #:lambda-list138
    #:docstring139 #:declarations140 #:forms141))
  nil)
You are a Common Lisp macro expert. Given this code pattern:
(lambda-ignoring-_ (_ exp) `(,op ,exp))

Write a macro that addresses: Macro call that should be transformed by lambda-ignoring-_

Macro definition:
(defmacro lambda-ignoring-_ (lambda-list &body body)
  "Works like `cl:lambda' macro except automatically `declare's
 `ignore' for parameters beginning with '_' character."
  (let ((ignored-parameters
          (collect-_-symbol-in-lambda-list lambda-list)))
    (unless ignored-parameters
      (warn "No ignored parameters in: ~A" lambda-list))
    (multiple-value-bind (body decls doc)
        (parse-body body :documentation t)
      `(lambda ,lambda-list
         ,@ (if doc `(,doc))
         (declare (ignore ,@ignored-parameters))
         ,@decls
         ,@body))))

Macroexpansion:
(lambda (_ exp) (declare (ignore _)) (sb-int:quasiquote (#S(sb-impl::comma :expr op :kind 0) #S(sb-impl::comma :expr exp :kind 0))))