j14i/cl-macros huggingface dataset:
Create a Common Lisp macro for the following transformation. Before code:
(mvlet* ((low high (sort-values #'< start stop)) (in-order? (eql low start)) (element-type (if in-order? `(integer ,low (,high)) `(integer (,low) ,high))) (len (ceiling (abs (- high low)) (abs step)))) (values len element-type))

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

Macro definition:
(defmacro mvlet* ((&rest bindings) &body body &environment env)
  "Expand a series of nested `multiple-value-bind' forms.

`mvlet*' is similar in intent to Scheme’s `let-values', but with a
different and less parenthesis-intensive syntax. Each binding is a
list of

    (var var*... expr)

A simple example should suffice to show both the implementation and
the motivation:

    (defun uptime (seconds)
      (mvlet* ((minutes seconds (truncate seconds 60))
               (hours minutes (truncate minutes 60))
               (days hours (truncate hours 24)))
        (declare ((integer 0 *) days hours minutes seconds))
        (fmt \"~d day~:p, ~d hour~:p, ~d minute~:p, ~d second~:p\"
             days hours minutes seconds)))

Note that declarations work just like `let*'."
  (cond ((null bindings)
         `(locally ,@body))
        ((every #'simple-binding-p bindings)
         `(let* ,bindings ,@body))
        (t (multiple-value-bind (body decls)
               (parse-body body)
             (let* ((bindings (canonicalize-bindings bindings))
                    (mvbinds (member-if-not #'simple-binding-p bindings))
                    (simple-binds (ldiff bindings mvbinds)))
               (if simple-binds
                   (multiple-value-bind (local other)
                       (partition-declarations (mapcar #'car simple-binds) decls env)
                     `(let* ,simple-binds
                        ,@local
                        (mvlet* ,mvbinds
                          ,@other
                          ,@body)))
                   (let* ((vars (butlast (car bindings)))
                          (expr (lastcar (car bindings))))
                     (multiple-value-bind (local other)
                         (partition-declarations vars decls env)
                       `(multiple-value-bind ,vars
                            ,expr
                          ,@local
                          (mvlet* ,(cdr bindings)
                            ,@other
                            ,@body))))))))))

Macroexpansion:
(multiple-value-bind (low high) (sort-values (function <) start stop) (mvlet* ((in-order? (eql low start)) (element-type (if in-order? (sb-int:quasiquote (integer #S(sb-impl::comma :expr low :kind 0) (#S(sb-impl::comma :expr high :kind 0)))) (sb-int:quasiquote (integer (#S(sb-impl::comma :expr low :kind 0)) #S(sb-impl::comma :expr high :kind 0))))) (len (ceiling (abs (- high low)) (abs step)))) (values len element-type)))
You are a Common Lisp macro expert. Given this code pattern:
(c? (if (value bypass-lookup?) 'no-lookup (bwhen-c-stored (v :bar store 'nothing) (value v))))

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

Macro definition:
(defmacro c? (&body body)
  `(make-c-dependent
    :code ',body
    :value-state :unevaluated
    :rule (c-lambda ,@body)))

Macroexpansion:
(make-c-dependent :code (quote ((if (value bypass-lookup?) (quote no-lookup) (bwhen-c-stored (v :bar store (quote nothing)) (value v))))) :value-state :unevaluated :rule (c-lambda (if (value bypass-lookup?) (quote no-lookup) (bwhen-c-stored (v :bar store (quote nothing)) (value v)))))
You are a Common Lisp macro expert. Given this code pattern:
(defgenerator repeat (item &optional n) "Returns a single item indefinitely, or up to n times." (if n (dotimes (i n) (yield item)) (loop do (yield item))))

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

Macro definition:
(defmacro defgenerator (name arguments &body body)
  (multiple-value-bind (remaining-forms declarations doc-string)
      (alexandria:parse-body body :documentation t)
    `(defun ,name ,arguments
       ,doc-string
       ,@declarations
       (with-yield
	 ,@remaining-forms))))

Macroexpansion:
(defun repeat (item &optional n) "Returns a single item indefinitely, or up to n times." (with-yield (if n (dotimes (i n) (yield item)) (loop do (yield item)))))
Generate a defmacro form that transforms code like this:
;; Common pattern: test a value, use result in then-branch
(let ((result (compute-something)))
  (if result
      (format t "Got: ~a~%" result)
      :nothing))

;; Want: implicit 'it' bound to test result

Pattern to recognize: Repeated pattern of binding test result then branching on it — want implicit 'it' anaphor

Macro definition:
(defmacro aif (test then &optional else)
  `(let ((it ,test))
     (if it ,then ,else)))

Macroexpansion:
;; (aif (compute-something)
;;      (format t "Got: ~a~%" it)
;;      :nothing)
;; Expands to:
;; (LET ((IT (COMPUTE-SOMETHING)))
;;   (IF IT
;;       (FORMAT T "Got: ~a~%" IT)
;;       :NOTHING))
Generate a defmacro form that transforms code like this:
(dynamic-let nil
  (progn))

Pattern to recognize: Macro call that should be transformed by DYNAMIC-LET

Macro definition:
(defmacro DYNAMIC-LET ...)

Macroexpansion:
(let ()
  (dynamic-wind
    :proceed
    #:g194
    (let ()
      (declare (special))
      (#:g194 (progn)))))
Define a macro that eliminates this repetitive pattern:
(def-easy-macro nil
    (progn))

Issue: Macro call that should be transformed by DEF-EASY-MACRO

Macro definition:
(defmacro DEF-EASY-MACRO ...)

Macroexpansion:
(progn
 (defun call-nil (#:|fn127| progn)
   (flet ((#:|fn127| (&rest args) (declare (inline)) (apply #:|fn127| args)))))
 (defmacro nil ((&rest fn-arg-values) &body macro-body)
   nil
   (build-funcall 'call-nil '(progn) fn-arg-values macro-body)))
Create a Common Lisp macro for the following transformation. Before code:
(define-simple-definition-resolver class designator-class-p)

What needs to change: Macro call that should be transformed by define-simple-definition-resolver

Macro definition:
(defmacro define-simple-definition-resolver (class lookup-function &body body)
  (let ((package (gensym "PACKAGE")))
    `(define-definition-resolver ,class (,class ,package)
       (when (ignore-errors ,(if body
                                 `(let ((,(first lookup-function) ,class))
                                    ,@body)
                                 `(,lookup-function ,class)))
         (list (make-instance ',class :designator ,class :package ,package))))))

Macroexpansion:
(define-definition-resolver class (class #:package137) (when (ignore-errors (designator-class-p class)) (list (make-instance (quote class) :designator class :package #:package137))))
Define a macro that eliminates this repetitive pattern:
(defpattern nil
    :key
  nil)

Issue: Macro call that should be transformed by DEFPATTERN

Macro definition:
(defmacro DEFPATTERN ...)

Macroexpansion:
(eval-always
  (setf (get 'nil 'cl-match-pattern)
          (lambda (#:g133)
            (lett :key
                #:g133
              nil))))
You are a Common Lisp macro expert. Given this code pattern:
(define-ps-symbol-macro nil nil)

Write a macro that addresses: Macro call that should be transformed by DEFINE-PS-SYMBOL-MACRO

Macro definition:
(defmacro DEFINE-PS-SYMBOL-MACRO ...)

Macroexpansion:
(progn
 nil
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (setf (gethash 'nil *symbol-macro-toplevel*)
           (lambda (form) (declare (ignore form)) 'nil))))
Define a macro that eliminates this repetitive pattern:
(define-modf-rewrite cddaar (expr) `(cdr (cdaar ,(second expr))))

Issue: Macro call that should be transformed by define-modf-rewrite

Macro definition:
(defmacro define-modf-rewrite (name (expr) &body body)
  "Define a new rewrite rule.  If a form starting with NAME is encountered, call
the defined function to return a form that we can deal with (i.e. one defined
via DEFINE-MODF-EXPANDER, DEFINE-MODF-FUNCTION, and DEFINE-MODF-METHOD)."
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (setf (gethash ',name *modf-rewrites*)
           (lambda (,expr)
             ,@body))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (setf (gethash (quote cddaar) *modf-rewrites*) (lambda (expr) (sb-int:quasiquote (cdr (cdaar #S(sb-impl::comma :expr (second expr) :kind 0)))))))
Write a Common Lisp macro to handle this code pattern:
(lambda+ nil)

Problem: Macro call that should be transformed by LAMBDA+

Macro definition:
(defmacro LAMBDA+ ...)

Macroexpansion:
(lambda () (let+ nil))
You are a Common Lisp macro expert. Given this code pattern:
(reduced-typecase nil nil)

Write a macro that addresses: Macro call that should be transformed by REDUCED-TYPECASE

Macro definition:
(defmacro REDUCED-TYPECASE ...)

Macroexpansion:
(typecase nil (nil))
Generate a defmacro form that transforms code like this:
(with-special-initargs nil
  nil)

Pattern to recognize: Macro call that should be transformed by WITH-SPECIAL-INITARGS

Macro definition:
(defmacro WITH-SPECIAL-INITARGS ...)

Macroexpansion:
(funcall-with-special-initargs (list) (lambda () nil))
Write a Common Lisp macro to handle this code pattern:
(list-of nil)

Problem: Macro call that should be transformed by LIST-OF

Macro definition:
(defmacro LIST-OF ...)

Macroexpansion:
(lc (nil) collect)
Generate a defmacro form that transforms code like this:
(with-yield-binds (with-output-to-string (s) (write-string (yield (column-definition-clause-column-name clause)) s) (when (column-definition-clause-type clause) (let ((*use-placeholder* nil)) (format s " ~A" (yield (column-definition-clause-type clause))))) (when (column-definition-clause-not-null clause) (write-string " NOT NULL" s)) (when (column-definition-clause-default clause) (format s " DEFAULT ~A" (yield (column-definition-clause-default clause)))) (when (column-definition-clause-auto-increment clause) (write-string " AUTO_INCREMENT" s)) (when (column-definition-clause-unique clause) (write-string " UNIQUE" s)) (when (column-definition-clause-primary-key clause) (write-string " PRIMARY KEY" s)) (when (and (column-definition-clause-autoincrement clause) (not (column-definition-clause-auto-increment clause))) (write-string " AUTOINCREMENT" s))))

Pattern to recognize: Macro call that should be transformed by with-yield-binds

Macro definition:
(defmacro with-yield-binds (&body body)
  `(let ((*bind-values* nil)
         (*use-global-bind-values* t))
     (values
      (progn ,@body)
      (loop for bind in (reverse *bind-values*)
            append bind))))

Macroexpansion:
(let ((*bind-values* nil) (*use-global-bind-values* t)) (values (progn (with-output-to-string (s) (write-string (yield (column-definition-clause-column-name clause)) s) (when (column-definition-clause-type clause) (let ((*use-placeholder* nil)) (format s " ~A" (yield (column-definition-clause-type clause))))) (when (column-definition-clause-not-null clause) (write-string " NOT NULL" s)) (when (column-definition-clause-default clause) (format s " DEFAULT ~A" (yield (column-definition-clause-default clause)))) (when (column-definition-clause-auto-increment clause) (write-string " AUTO_INCREMENT" s)) (when (column-definition-clause-unique clause) (write-string " UNIQUE" s)) (when (column-definition-clause-primary-key clause) (write-string " PRIMARY KEY" s)) (when (and (column-definition-clause-autoincrement clause) (not (column-definition-clause-auto-increment clause))) (write-string " AUTOINCREMENT" s)))) (loop for bind in (reverse *bind-values*) append bind)))
Generate a defmacro form that transforms code like this:
(-<>> 'some-name)

Pattern to recognize: Macro call that should be transformed by -<>>

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

Macroexpansion:
'some-name
Generate a defmacro form that transforms code like this:
(defmethod* nil nil)

Pattern to recognize: Macro call that should be transformed by DEFMETHOD*

Macro definition:
(defmacro DEFMETHOD* ...)

Macroexpansion:
(let* ((#:nil125 (ensure-generic*-function 'nil))
       (temp-method (defmethod #:nil124 nil))
       (actual-gf
        (ensure-generic*-function 'nil :lambda-list
                                  (sb-mop:generic-function-lambda-list
                                   (symbol-function '#:nil124))
                                  :special-variables 'nil))
       (actual-method
        (add-method* #:nil125
                     (make-instance 'method* :special-variables (list)
                                    :qualifiers 'nil :specializers
                                    (sb-mop:method-specializers temp-method)
                                    :lambda-list
                                    (sb-mop:method-lambda-list temp-method)
                                    :documentation
                                    (documentation temp-method t) :function
                                    (sb-mop:method-function temp-method)))))
  (declare (ignorable #:nil125)
           (ignore actual-gf))
  (setf (documentation actual-method t) (documentation temp-method t))
  actual-method)
Generate a defmacro form that transforms code like this:
(letrec nil
  :key
  nil)

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

Macro definition:
(defmacro LETREC ...)

Macroexpansion:
(let ()
  (flet ()
    nil
    (setf)
    :key
    nil))
Define a macro that eliminates this repetitive pattern:
(deferror "42P06" duplicate-schema syntax-error-or-access-violation)

Issue: Macro call that should be transformed by deferror

Macro definition:
(defmacro deferror (code typename &optional (superclass 'database-error))
  `(progn (define-condition ,typename (,superclass) ())
          (setf (gethash ,code *error-table*) ',typename)))

Macroexpansion:
(progn (define-condition duplicate-schema (syntax-error-or-access-violation) nil) (setf (gethash "42P06" *error-table*) (quote duplicate-schema)))
Define a macro that eliminates this repetitive pattern:
(with-prepared-query query (conn (format nil "SELECT currval(pg_get_serial_sequence('~A', '~A')) AS last_insert_id" table-name serial-key-name)) (or (first (dbi:fetch (dbi:execute query) :format :values)) 0))

Issue: Macro call that should be transformed by with-prepared-query

Macro definition:
(defmacro with-prepared-query (query (conn sql &key use-prepare-cached) &body body)
  `(call-with-prepared-query ,conn ,sql (lambda (,query) ,@body) :use-prepare-cached ,use-prepare-cached))

Macroexpansion:
(call-with-prepared-query conn (format nil "SELECT currval(pg_get_serial_sequence('~A', '~A')) AS last_insert_id" table-name serial-key-name) (lambda (query) (or (first (dbi.driver:fetch (dbi.driver:execute query) :format :values)) 0)) :use-prepare-cached nil)
Generate a defmacro form that transforms code like this:
(define-modf-function nth 2 (new-val nth obj) (replace-nth nth obj new-val))

Pattern to recognize: Macro call that should be transformed by define-modf-function

Macro definition:
(defmacro define-modf-function (name nth-arg (new-val &rest args) &body body)
  "Define a new modf function.  It inverts NAME forms by modifying the NTH-ARG
term of the arguments of the place form in the MODF macro."
  ;; Side effect in a macro, I know.  How can you do this via EVAL-WHEN if I
  ;; want it to run even if not a toplevel macro?
  (setf (gethash name *modf-nth-arg*)
        nth-arg)
  `(progn
     (eval-when (:compile-toplevel :load-toplevel :execute)
       (setf (gethash ',name *modf-nth-arg*)
             ,nth-arg))
     (defun ,(intern (modf-name name) :modf) (,new-val ,@args)
       ,@body)))

Macroexpansion:
(progn (eval-when (:compile-toplevel :load-toplevel :execute) (setf (gethash (quote nth) *modf-nth-arg*) 2)) (defun |BUILDER:COMMON-LISP:NTH| (new-val nth obj) (replace-nth nth obj new-val)))
You are a Common Lisp macro expert. Given this code pattern:
(when (eql (top-info) 'aref) (format stream ")"))

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

Macro definition:
(defmacro when (test &body forms)
  `(if ,test
	   ,(cl:if (cadr forms)
			   `(progn ,@forms)
			   (car forms))))

Macroexpansion:
(if (eql (top-info) (quote aref)) (format stream ")"))
You are a Common Lisp macro expert. Given this code pattern:
(some-<>>
  nil
  :key
  nil)

Write a macro that addresses: Macro call that should be transformed by SOME-<>>

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

Macroexpansion:
(let* ((#:var145 nil)
       (#:var146
        (if #:var145
            (:key #:var145)
            nil))
       (#:var147
        (if #:var146
            (nil #:var146)
            nil)))
  #:var147)
Create a Common Lisp macro for the following transformation. Before code:
(define-declaration nil
    (progn))

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

Macro definition:
(defmacro DEFINE-DECLARATION ...)

Macroexpansion:
(sb-cltl2:define-declaration nil
    (progn))
Create a Common Lisp macro for the following transformation. Before code:
(defun-compile-time get-function-record (function-name) (or (gethash function-name *function-record-table*) (setf (gethash function-name *function-record-table*) (make-function-record :function-name function-name))))

What needs to change: 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 get-function-record (function-name) (or (gethash function-name *function-record-table*) (setf (gethash function-name *function-record-table*) (make-function-record :function-name function-name)))) (eval-when (:compile-toplevel) (compile (quote get-function-record))))
Write a Common Lisp macro to handle this code pattern:
;; Macro with double-evaluation bug
(defmacro square (x)
  `(* ,x ,x))
;; (square (incf y)) evaluates (incf y) TWICE

;; Manual fix requires gensym + let boilerplate every time
(defmacro square (x)
  (let ((g (gensym)))
    `(let ((,g ,x))
       (* ,g ,g))))

Problem: Macro arguments evaluated multiple times (double evaluation) — need automatic once-only that also provides both original form and evaluated value

Macro definition:
(defun o!-symbol-p (s)
  (and (symbolp s)
       (> (length (symbol-name s)) 2)
       (string= (symbol-name s)
                "O!"
                :start1 0
                :end1 2)))

(defun o!-symbol-to-g!-symbol (s)
  (symb "G!"
        (subseq (symbol-name s) 2)))

(defmacro defmacro! (name args &rest body)
  (let* ((os (remove-if-not #'o!-symbol-p (flatten args)))
         (gs (mapcar #'o!-symbol-to-g!-symbol os)))
    (multiple-value-bind (body declarations docstring)
        (parse-body body :documentation t)
      `(defmacro/g! ,name ,args
         ,@(when docstring
            (list docstring))
         ,@declarations
         `(let ,(mapcar #'list (list ,@gs) (list ,@os))
            ,(progn ,@body))))))

Macroexpansion:
;; (defmacro! square (o!x)
;;   `(* ,g!x ,g!x))
;;
;; (macroexpand '(square (incf y)))
;; => (LET ((#:X1633 (INCF Y)))
;;      (* #:X1633 #:X1633))
;;
;; o!x = original form (still accessible via ',o!x)
;; g!x = evaluated value, bound once
Define a macro that eliminates this repetitive pattern:
(defgeneric* nil (progn))

Issue: Macro call that should be transformed by DEFGENERIC*

Macro definition:
(defmacro DEFGENERIC* ...)

Macroexpansion:
(ensure-generic*-function 'nil :name 'nil :lambda-list '(progn))
You are a Common Lisp macro expert. Given this code pattern:
(make-obsolete mustache-context make-context "0.10.0")

Write a macro that addresses: Macro call that should be transformed by make-obsolete

Macro definition:
(defmacro make-obsolete (obsolete-name current-name when)
  `(defun ,obsolete-name (&rest args)
     ,(documentation current-name 'function)
     (warn 'deprecation-warning
           :name ',obsolete-name :replacement ',current-name :since ,when)
     (apply ',current-name args)))

Macroexpansion:
(defun mustache-context (&rest args) #A((40) base-char . "Create mustache context from alist DATA.") (warn (quote deprecation-warning) :name (quote mustache-context) :replacement (quote make-context) :since "0.10.0") (apply (quote make-context) args))
Create a Common Lisp macro for the following transformation. Before code:
(do-generator (a b (with-yield (dolist (x data) (yield x (1+ x))))) a (return b))

What needs to change: Macro call that should be transformed by do-generator

Macro definition:
(defmacro do-generator ((var &rest vars-and-generator) &body body)
  "Steps through the specified generator, binding the value it returns to var, then executing the body for each step. If multiple variables are specified and the generator returns multiple values, the extra values will be bound to the extra variables."
  (let ((g (gensym)))
    `(let ((,g ,(car (last vars-and-generator))))
       (loop 
	  do (if-generator ((,var ,@(butlast vars-and-generator)) ,g)
			   (progn
			     ,@body)
			   (return))))))

Macroexpansion:
(let ((#:g171 (with-yield (dolist (x data) (yield x (1+ x)))))) (loop do (if-generator ((a b) #:g171) (progn a (return b)) (return))))
Create a Common Lisp macro for the following transformation. Before code:
(define-c-syntax-reader-for-each-case-mode c-reader-level-0)

What needs to change: Macro call that should be transformed by define-c-syntax-reader-for-each-case-mode

Macro definition:
(defmacro define-c-syntax-reader-for-each-case-mode (base-readtable-name)
  (check-type base-readtable-name symbol)
  (loop with case-modes = '(:upcase :downcase :preserve :invert)
        for mode in case-modes
        as name = (format-symbol :with-c-syntax.core "~A-~A" base-readtable-name mode)
        collect `(defreadtable ,name
                   (:merge ,base-readtable-name)
                   (:case ,mode))
          into bodies
        finally
           (return `(progn ,@bodies))))

Macroexpansion:
(progn (defreadtable c-reader-level-0-upcase (:merge c-reader-level-0) (:case :upcase)) (defreadtable c-reader-level-0-downcase (:merge c-reader-level-0) (:case :downcase)) (defreadtable c-reader-level-0-preserve (:merge c-reader-level-0) (:case :preserve)) (defreadtable c-reader-level-0-invert (:merge c-reader-level-0) (:case :invert)))
You are a Common Lisp macro expert. Given this code pattern:
(with-html-string (:ul (:li (:a "hai"))))

Write a macro that addresses: Macro call that should be transformed by with-html

Macro definition:
(defmacro with-html (&body body &environment env)
  "Interpret BODY as HTML. Consult README.txt for the syntax."
  `(let ((*html* (ensure-html-stream *html*)))
     ,(if (and (null (cdr body)) (atom (car body)))
          (car body)
          `(progn ,@(parse-html body env)))))

Macroexpansion:
(with-output-to-string (*html*) (with-html (:ul (:li (:a "hai")))))
You are a Common Lisp macro expert. Given this code pattern:
(aand (values nil t) it)

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

Macro definition:
(defmacro aand (first &rest rest)
  "Like AND, except binds the first argument to IT (via LET) for the
scope of the rest of the arguments."
  `(anaphoric and ,first ,@rest))

Macroexpansion:
(anaphoric and (values nil t) it)