emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Code for cond*


From: Stefan Monnier
Subject: Re: Code for cond*
Date: Mon, 12 Feb 2024 21:27:48 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> The main use-case I can think of for `cdr-ignore` is when matching
> against things like Lisp code, where a pattern like
>
>     `(lambda ,args . ,body)
>
> should also match (lambda) because (lambda) is a (broken) lambda
> expression rather than being something *else* than a lambda expression.
>
> So I'm not sure the behavior you chose for `cdr-ignore` is the one
> we want.

BTW, here's what I used at one place to address this problem:

    (defmacro byte-optimize--pcase (exp &rest cases)
      ;; When we do
      ;;
      ;;     (pcase EXP
      ;;       (`(if ,exp ,then ,else) (DO-TEST))
      ;;       (`(plus ,e2 ,e2)        (DO-ADD))
      ;;       (`(times ,e2 ,e2)       (DO-MULT))
      ;;       ...)
      ;;
      ;; we usually don't want to fall back to the default case if
      ;; the value of EXP is of a form like `(if E1 E2)' or `(plus E1)'
      ;; or `(times E1 E2 E3)', instead we either want to signal an error
      ;; that EXP has an unexpected shape, or we want to carry on as if
      ;; it had the right shape (ignore the extra data and pretend the missing
      ;; data is nil) because it should simply never happen.
      ;;
      ;; The macro below implements the second option by rewriting patterns
      ;; like `(if ,exp ,then ,else)'
      ;; to   `(if . (or `(,exp ,then ,else) pcase--dontcare))'.
      ;;
      ;; The resulting macroexpansion is also significantly 
cleaner/smaller/faster.
      (declare (indent 1) (debug pcase))
      `(pcase ,exp
         . ,(mapcar (lambda (case)
                      `(,(pcase (car case)
                           ((and `(,'\` (,_ . (,'\, ,_))) pat) pat)
                           (`(,'\` (,head . ,tail))
                            (list '\`
                                  (cons head
                                        (list '\, `(or ,(list '\` tail) 
pcase--dontcare)))))
                           (pat pat))
                        . ,(cdr case)))
                    cases)))

- Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]