j14i/cl-macros huggingface dataset:
Generate a defmacro form that transforms code like this:
(with-decoder-simple-list-semantics (flet ((safe-symbols-parsing (name) (or (find-symbol name *json-symbols-package*) (error "unknown symbols not allowed")))) (let ((good-symbols "{\"car\":1,\"cdr\":2}") (bad-symbols "{\"could-be\":1,\"a-denial-of-service-attack\":2}") (*json-symbols-package* (find-package :cl)) (*identifier-name-to-key* #'safe-symbols-parsing)) (is (equal '((car . 1) (cdr . 2)) (decode-json-from-string good-symbols))) (signals error (decode-json-from-string bad-symbols)))))

Pattern to recognize: Macro call that should be transformed by with-decoder-simple-list-semantics

Macro definition:
(defmacro with-decoder-simple-list-semantics (&body body)
  "Execute BODY in a dynamic environement where the decoder semantics
is such as set by SET-DECODER-SIMPLE-LIST-SEMANTICS."
  `(with-shadowed-custom-vars
     (set-decoder-simple-list-semantics)
     ,@body))

Macroexpansion:
(with-shadowed-custom-vars (set-decoder-simple-list-semantics) (flet ((safe-symbols-parsing (name) (or (find-symbol name *json-symbols-package*) (error "unknown symbols not allowed")))) (let ((good-symbols "{\"car\":1,\"cdr\":2}") (bad-symbols "{\"could-be\":1,\"a-denial-of-service-attack\":2}") (*json-symbols-package* (find-package :cl)) (*identifier-name-to-key* (function safe-symbols-parsing))) (is (equal (quote ((car . 1) (cdr . 2))) (decode-json-from-string good-symbols))) (signals error (decode-json-from-string bad-symbols)))))
Define a macro that eliminates this repetitive pattern:
(define-simple-documentation-lookup type cl:type)

Issue: Macro call that should be transformed by define-simple-documentation-lookup

Macro definition:
(defmacro define-simple-documentation-lookup (class documentation-type)
  `(defmethod documentation ((,class ,class))
     ,(if (eql documentation-type T)
          `(cl:documentation (object ,class) 'T)
          `(cl:documentation (designator ,class) ',documentation-type))))

Macroexpansion:
(defmethod documentation ((type type)) (common-lisp:documentation (designator type) (quote common-lisp:type)))
You are a Common Lisp macro expert. Given this code pattern:
(define-accumulation-binding reducing ((var *unbound*) form &key (by NIL by-p)) (unless by-p (error ":BY argument required for REDUCING binding.")) `(cond ((eq ,var *unbound*) (setf ,var ,form)) (T (setf ,var (funcall ,by ,var ,form)))))

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

Macro definition:
(defmacro define-accumulation-binding (name (var &rest args) &body body)
  `(define-form-binding ,name (,var ,@args)
     ,@(form-fiddle:lambda-declarations `(lambda () ,@body))
     (values
      (progn ,@(form-fiddle:lambda-forms `(lambda () ,@body)))
      (delist var))))

Macroexpansion:
(define-form-binding reducing ((var *unbound*) form &key (by nil by-p)) (values (progn (unless by-p (error ":BY argument required for REDUCING binding.")) (sb-int:quasiquote (cond ((eq #S(sb-impl::comma :expr var :kind 0) *unbound*) (setf #S(sb-impl::comma :expr var :kind 0) #S(sb-impl::comma :expr form :kind 0))) (t (setf #S(sb-impl::comma :expr var :kind 0) (funcall #S(sb-impl::comma :expr by :kind 0) #S(sb-impl::comma :expr var :kind 0) #S(sb-impl::comma :expr form :kind 0))))))) (delist var)))
Generate a defmacro form that transforms code like this:
(with-html (:table (dotimes (i 1000) (:tr (dotimes (i 10) (:td (1+ i)))))))

Pattern to recognize: 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:
(let ((*html* (ensure-html-stream *html*))) (progn (with-tag (:table) (dotimes (i 1000) (with-tag (:tr) (dotimes (i 10) (with-tag (:td) (1+ i))))))))
Generate a defmacro form that transforms code like this:
(define-case-aware-token-p-function pp-stdc-pragma-p find-pragma-name with-c-syntax.pragma-name:|STDC|)

Pattern to recognize: Macro call that should be transformed by define-case-aware-token-p-function

Macro definition:
(defmacro define-case-aware-token-p-function (function-name find-symbol-function symbol)
  `(defun ,function-name (token &optional (readtable-case (readtable-case *readtable*)))
     (and (symbolp token)
          (eq (,find-symbol-function (string token) readtable-case)
              ',symbol))))

Macroexpansion:
(defun pp-stdc-pragma-p (token &optional (readtable-case (readtable-case *readtable*))) (and (symbolp token) (eq (find-pragma-name (string token) readtable-case) (quote with-c-syntax.pragma-name:stdc))))
You are a Common Lisp macro expert. Given this code pattern:
(define-error could-not-find-function (:error-type varjo-critical-error) (name) "No function called '~s' was found in this environment" name)

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 could-not-find-function (:error-type varjo-critical-error :prefix "Varjo") (name) "No function called '~s' was found in this environment" name)
Define a macro that eliminates this repetitive pattern:
(drelet* nil)

Issue: Macro call that should be transformed by DRELET*

Macro definition:
(defmacro DRELET* ...)

Macroexpansion:
(dynamic-relet* nil)
Write a Common Lisp macro to handle this code pattern:
(with-standard-readtable
  nil
  nil)

Problem: Macro call that should be transformed by WITH-STANDARD-READTABLE

Macro definition:
(defmacro WITH-STANDARD-READTABLE ...)

Macroexpansion:
(bind ((*readtable* (with-standard-io-syntax (copy-readtable *readtable*))))
  nil
  nil)
Define a macro that eliminates this repetitive pattern:
(action (:after node-application node) (multiple-value-bind (new-node new-changed?) (maybe-sink-application-into-rator node) (when new-changed? (setf changed? t) new-node)))

Issue: Macro call that should be transformed by action

Macro definition:
(defmacro action ((when type &rest args) &body body)
  "Construct an action block, turning `body` into a lambda function
accepting `args` if `args` are provided. If there are no `args`, then
`body` is assumed to be a function object and is used directly with
`when` and `type` to create an `action` instance. Otherwise, the first
argument in `args` is declared to be a node of type `type`, and the
`args` and `body` are used to generate a lambda function."
  (check-type when keyword)
  (check-type type symbol)
  (assert (subtypep type 'node))
  (assert (or (not (endp args)) (= 1 (length body))))
  (if (endp args)
      `(make-action ',when ',type ,@body)
      `(make-action ',when ',type
                    (lambda (,@args)
                      (declare (type ,type ,(first args)))
                      ,@body))))

Macroexpansion:
(make-action (quote :after) (quote node-application) (lambda (node) (declare (type node-application node)) (multiple-value-bind (new-node new-changed?) (maybe-sink-application-into-rator node) (when new-changed? (setf changed? t) new-node))))
Create a Common Lisp macro for the following transformation. Before code:
($op* op l)

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

Macro definition:
(defmacro $ (&rest args)
  `(kv ,@args))

Macroexpansion:
(multiple-value-bind (#:g329 #:g332) (op-split (unwrap (list ($exp+ l)))) (let ((#:g331 (kv (car #:g329) (kv op #:g332)))) (dolist (#:g330 (cdr #:g329)) (setf #:g331 (kv #:g331 (kv #:g330 (kv op #:g332))))) #:g331))
Generate a defmacro form that transforms code like this:
(ensure-getf options :ignorable ignorable)

Pattern to recognize: Macro call that should be transformed by ensure-getf

Macro definition:
(defmacro ensure-getf (place key &optional default)
  (once-only (key default)
    `(setf (getf ,place ,key) (getf ,place ,key ,default))))

Macroexpansion:
(let ((#:key173 :ignorable) (#:default174 ignorable)) (setf (getf options #:key173) (getf options #:key173 #:default174)))
Generate a defmacro form that transforms code like this:
(action (:after node-direct-application) #'apply-specialization)

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

Macro definition:
(defmacro action ((when type &rest args) &body body)
  "Construct an action block, turning `body` into a lambda function
accepting `args` if `args` are provided. If there are no `args`, then
`body` is assumed to be a function object and is used directly with
`when` and `type` to create an `action` instance. Otherwise, the first
argument in `args` is declared to be a node of type `type`, and the
`args` and `body` are used to generate a lambda function."
  (check-type when keyword)
  (check-type type symbol)
  (assert (subtypep type 'node))
  (assert (or (not (endp args)) (= 1 (length body))))
  (if (endp args)
      `(make-action ',when ',type ,@body)
      `(make-action ',when ',type
                    (lambda (,@args)
                      (declare (type ,type ,(first args)))
                      ,@body))))

Macroexpansion:
(make-action (quote :after) (quote node-direct-application) (function apply-specialization))
You are a Common Lisp macro expert. Given this code pattern:
(rem-bops nil)

Write a macro that addresses: Macro call that should be transformed by REM-BOPS

Macro definition:
(defmacro REM-BOPS ...)

Macroexpansion:
(eval-when (:load-toplevel :compile-toplevel :execute)
  (progn (save-binfix) (mapc 'rmbop '(nil)) (assign-properties)))
Write a Common Lisp macro to handle this code pattern:
(match nil)

Problem: Macro call that should be transformed by MATCH

Macro definition:
(defmacro MATCH ...)

Macroexpansion:
(block #:match123)
Write a Common Lisp macro to handle this code pattern:
(sctypecase (car x) (string (setf it :ok)))

Problem: Macro call that should be transformed by sctypecase

Macro definition:
(defmacro sctypecase (keyform &body cases)
  "Like CTYPECASE, except binds the keyform to IT (via SYMBOL-MACROLET) for
the scope of the cases. IT can be set with SETF."
  `(symbolic ctypecase ,keyform ,@cases))

Macroexpansion:
(symbolic ctypecase (car x) (string (setf it :ok)))
Write a Common Lisp macro to handle this code pattern:
(with-env [ht-equalp (copy-list '(4 5 6))])

Problem: Macro call that should be transformed by with-env

Macro definition:
(defmacro with-env (&body body)
  (let ((body-value (gensym)))
    `(let (,body-value)
       (defclass foo () ((a :initform 3)))
       (defmethod get-val ((object foo) &rest slot/s) (slot-value object (car slot/s)))
       (defmethod (setf get-val) (new-value (object foo) &rest slot/s)
         (setf (slot-value object (car slot/s)) new-value))
       (defstruct bar a)
       (setq ,body-value
             (let ((str (alexandria:copy-array "abcde"))
                   (vec (alexandria:copy-array #(a b c d e)))
                   (arr (alexandria:copy-array #2A((1 2 3)
                                                   (4 5 6))))
                   (list      (copy-list '(a b c d e)))
                   (ht        (eval (read-from-string "{'a 'b 'c 'd}")))
                   (ht-eq     (eval (read-from-string "{eq \"a\" 1
                                                    \"b\" 2}")))
                   (ht-equalp (eval (read-from-string "{equalp '(1 2 3) \"a\"
                                                               '(4 5 6) \"b\"}")))
                   (assoc-list (copy-tree '((a . 1) (b . 2))))
                   (plist      (copy-list '(:a 4 :c 5)))
                   (clos-object (make-instance 'foo))
                   (struct      (make-bar :a 3)))
               (declare (ignorable str vec arr list ht ht-eq ht-equalp
                                   assoc-list plist clos-object struct))
               ,@body))
       (setf (find-class 'foo nil) nil)
       (setf (find-class 'bar nil) nil)
       ,body-value))

Macroexpansion:
(let (#:g126) (defclass foo nil ((a :initform 3))) (defmethod get-val ((object foo) &rest slot/s) (slot-value object (car slot/s))) (defmethod (setf get-val) (new-value (object foo) &rest slot/s) (setf (slot-value object (car slot/s)) new-value)) (defstruct bar a) (setq #:g126 (let ((str (alexandria:copy-array "abcde")) (vec (alexandria:copy-array #(a b c d e))) (arr (alexandria:copy-array #2A((1 2 3) (4 5 6)))) (list (copy-list (quote (a b c d e)))) (ht (eval (read-from-string "{'a 'b 'c 'd}"))) (ht-eq (eval (read-from-string "{eq \"a\" 1
                                                    \"b\" 2}"))) (ht-equalp (eval (read-from-string "{equalp '(1 2 3) \"a\"
                                                               '(4 5 6) \"b\"}"))) (assoc-list (copy-tree (quote ((a . 1) (b . 2))))) (plist (copy-list (quote (:a 4 :c 5)))) (clos-object (make-instance (quote foo))) (struct (make-bar :a 3))) (declare (ignorable str vec arr list ht ht-eq ht-equalp assoc-list plist clos-object struct)) [ht-equalp (copy-list (quote (4 5 6))) ])) (setf (find-class (quote foo) nil) nil) (setf (find-class (quote bar) nil) nil) #:g126)
You are a Common Lisp macro expert. Given this code pattern:
(defmessage nil nil)

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

Macro definition:
(defmacro DEFMESSAGE ...)

Macroexpansion:
(progn
 (eval-when (:compile-toplevel) (proclaim `(ftype function ,'nil)))
 (aprog1 (ensure-message 'nil :lambda-list 'nil)))
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* ((#:g128 nil)
       (#:g128
        (if (null #:g128)
            nil
            (->> #:g128 :key))))
  (if (null #:g128)
      nil
      (->> #:g128 nil)))
You are a Common Lisp macro expert. Given this code pattern:
(defun/1 fflush (fd) (unless (eql fd NULL) (finish-output (fd-stream fd))) 0)

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 fflush (fd) (declare (optimize (speed 0) (debug 3) (sb-ext:inhibit-warnings 3))) (unless (eql fd null) (finish-output (fd-stream fd))) 0) (defparameter fflush (vacietis.c:mkptr& (symbol-function (quote fflush)))))
You are a Common Lisp macro expert. Given this code pattern:
(def-slot-annotation required () (when (plist-member slot-options :initform) (error "Required slot ~A must not have :initform" slot-name)) (unless (plist-member slot-options :initarg) (setf (getf slot-options :initarg) (make-keyword slot-name))) (setf (getf slot-options :initform) `(required-argument ,(getf slot-options :initarg))))

Write a macro that addresses: Macro call that should be transformed by def-slot-annotation

Macro definition:
(defmacro def-slot-annotation (name args &body body)
  (with-gensyms (slot-specifier)
    `(defannotation ,name ,(append args (list slot-specifier))
         (:inline t :arity ,(1+ (length args)))
       (destructuring-bind (slot-name . slot-options)
           (if (consp ,slot-specifier)
               ,slot-specifier
               (list ,slot-specifier))
         ,@body
         (cons slot-name slot-options)))))

Macroexpansion:
(defannotation required (#:slot-specifier123) (:inline t :arity 1) (destructuring-bind (slot-name . slot-options) (if (consp #:slot-specifier123) #:slot-specifier123 (list #:slot-specifier123)) (when (plist-member slot-options :initform) (error "Required slot ~A must not have :initform" slot-name)) (unless (plist-member slot-options :initarg) (setf (getf slot-options :initarg) (make-keyword slot-name))) (setf (getf slot-options :initform) (sb-int:quasiquote (required-argument #S(sb-impl::comma :expr (getf slot-options :initarg) :kind 0)))) (cons slot-name slot-options)))
Write a Common Lisp macro to handle this code pattern:
(defun* nil
    (progn))

Problem: Macro call that should be transformed by DEFUN*

Macro definition:
(defmacro DEFUN* ...)

Macroexpansion:
(defun nil (progn) (with-yield))
You are a Common Lisp macro expert. Given this code pattern:
(define-language nil nil)

Write a macro that addresses: Macro call that should be transformed by DEFINE-LANGUAGE

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

Macroexpansion:
(progn
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (defclass nil (nil) nil))
 (define-language-macro nil))
Create a Common Lisp macro for the following transformation. Before code:
(-<>
  nil
  (progn))

What needs to change: Macro call that should be transformed by -<>

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

Macroexpansion:
(let* ((#:var206 nil) (#:var207 (progn #:var206)))
  #:var207)
Define a macro that eliminates this repetitive pattern:
(fmap (lambda (chars) (destructuring-bind (a b c d) chars (code-char (+ (* 4096 (digit-char-p a 16)) (* 256 (digit-char-p b 16)) (* 16 (digit-char-p c 16)) (digit-char-p d 16))))) (funcall (*> (char #\\) (alt (char #\u) (char #\U)) (count 4 #'hex)) offset))

Issue: Macro call that should be transformed by fmap

Macro definition:
(defmacro fmap (f thing)
  "Apply a pure function to the inner contents of some parsing operation, if the
parsing itself was successful."
  `(multiple-value-bind (res next) ,thing
     (if (failure? res)
         (fail next)
         (values (funcall ,f res) next))))

Macroexpansion:
(multiple-value-bind (res next) (funcall (*> (char #\REVERSE_SOLIDUS) (alt (char #\LATIN_SMALL_LETTER_U) (char #\LATIN_CAPITAL_LETTER_U)) (count 4 (function hex))) offset) (if (failure? res) (fail next) (values (funcall (lambda (chars) (destructuring-bind (a b c d) chars (code-char (+ (* 4096 (digit-char-p a 16)) (* 256 (digit-char-p b 16)) (* 16 (digit-char-p c 16)) (digit-char-p d 16))))) res) next)))
Write a Common Lisp macro to handle this code pattern:
(define-find-c-readtable-name-methods c-reader-level-1 1)

Problem: Macro call that should be transformed by define-find-c-readtable-name-methods

Macro definition:
(defmacro define-find-c-readtable-name-methods (base-readtable-name level)
  (check-type base-readtable-name symbol)
  (check-type level integer)
  (loop with case-modes = '(nil :upcase :downcase :preserve :invert)
        for mode in case-modes
        as name = (format-symbol :with-c-syntax.core "~A~@[-~A~]"
                                 base-readtable-name mode)
        collect `(defmethod find-c-readtable-name
                     ((level (eql ,level)) (readtable-case (eql ,mode)))
                   ',name)
          into bodies
        finally
           (return `(progn ,@bodies))))

Macroexpansion:
(progn (defmethod find-c-readtable-name ((level (eql 1)) (readtable-case (eql nil))) (quote c-reader-level-1)) (defmethod find-c-readtable-name ((level (eql 1)) (readtable-case (eql :upcase))) (quote c-reader-level-1-upcase)) (defmethod find-c-readtable-name ((level (eql 1)) (readtable-case (eql :downcase))) (quote c-reader-level-1-downcase)) (defmethod find-c-readtable-name ((level (eql 1)) (readtable-case (eql :preserve))) (quote c-reader-level-1-preserve)) (defmethod find-c-readtable-name ((level (eql 1)) (readtable-case (eql :invert))) (quote c-reader-level-1-invert)))
Define a macro that eliminates this repetitive pattern:
;; Explicit continuation-passing is verbose
(defun fact-cps (n cont)
  (if (zerop n)
      (funcall cont 1)
      (fact-cps (- n 1)
                (lambda (x) (funcall cont (* n x))))))

;; Every function must take an explicit continuation parameter.
;; Every call must thread the continuation through.
;; Simple sequential code becomes deeply nested.

Issue: Manual continuation-passing style requires explicit continuation parameters and nested lambdas — want syntactic sugar for CPS

Macro definition:
(defmacro =lambda (parms &body body)
  `#'(lambda (*cont* ,@parms) ,@body))

(defmacro =defun (name parms &body body)
  (let ((f (intern (concatenate 'string
                                "=" (symbol-name name)))))
    `(progn
       (defmacro ,name ,parms
         `(,',f *cont* ,,@parms))
       (defun ,f (*cont* ,@parms) ,@body))))

(defmacro =bind (parms expr &body body)
  `(let ((*cont* #'(lambda ,parms ,@body))) ,expr))

(defmacro =values (&rest retvals)
  `(funcall *cont* ,@retvals))

Macroexpansion:
;; (=defun fact (n)
;;   (if (zerop n)
;;       (=values 1)
;;       (=bind (x) (fact (- n 1))
;;         (=values (* n x)))))
;;
;; =lambda: creates a CPS function (takes *cont* as first arg)
;; =defun: defines a CPS function + a macro for clean call syntax
;; =bind: captures *cont* as the rest of the computation
;; =values: sends values to the current continuation
;;
;; The code looks sequential but compiles to CPS.
;; =bind marks where a continuation is captured.
Define a macro that eliminates this repetitive pattern:
(define-shell-component less :input-type "less" :output-type "css" :shell-command "lessc")

Issue: Macro call that should be transformed by define-shell-component

Macro definition:
(defmacro define-shell-component (name &key input-type output-type shell-command)
  "Define an ASDF component that's compiled by running a shell command."
  `(define-component ,name
     :input-type ,input-type
     :output-type ,output-type
     :compile-function (lambda (input-pathname output-pathname)
                         (inferior-shell:run (list ,@(ensure-list shell-command)
                                                    input-pathname
                                                    output-pathname)
                                             :show t))))

Macroexpansion:
(define-component less :input-type "less" :output-type "css" :compile-function (lambda (input-pathname output-pathname) (inferior-shell:run (list "lessc" input-pathname output-pathname) :show t)))
Write a Common Lisp macro to handle this code pattern:
(lret ((vect (prepare-int-range start stop step))) (fill-int-range! vect start stop step))

Problem: 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 ((vect (prepare-int-range start stop step))) (fill-int-range! vect start stop step))
You are a Common Lisp macro expert. Given this code pattern:
(-> :p (v) (setf result v) (mt:open-latch latch))

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

Macro definition:
(defmacro -> (invariant &body args)
  "See flow:atomically"
  `(atomically ,invariant ,@args))

Macroexpansion:
(atomically :p (v) (setf result v) (cl-muth:open-latch latch))