macro that renames a function and records in a global list each non-local exit

Barton Willis via Maxima-discuss <[email protected]> Wed, 3 Jun 2026 12:18:24 +0000
Newsgroups gmane.comp.mathematics.maxima.general
Message-ID <SN6PR07MB7952F1BE1AC12BFDAC681CFAB6132@SN6PR07MB7952.namprd07.prod.outlook.com>


Here is an AI‑assisted macro (with some push‑back from me along the way) that renames a Common Lisp function and records information about each non‑local exit in a global list:

;; AI written
(defmacro define-renamed-with-nlexit (old-name new-name log-var)
  "Rename OLD-NAME to NEW-NAME and wrap OLD-NAME so that any non-local
exit is recorded in LOG-VAR, which must be a place suitable for PUSH."
  (let ((args (gensym "ARGS")))
    `(progn
       ;; Save the old definition under NEW-NAME
       (setf (symbol-function ',new-name)
             (symbol-function ',old-name))

       ;; Define the wrapper under OLD-NAME
       (setf (symbol-function ',old-name)
             (lambda (&rest ,args)
               (handler-case
                   (apply #',new-name ,args)

                 (condition (c)
                   (push (ftake 'mlist :function  ',old-name
                               :renamed   ',new-name
                               ;; Maxima list of args: (mlist arg1 arg2 ...)
                               :args      (cons (list 'mlist) ,args)
                               :condition c
                               :timestamp (get-universal-time))
                         ,log-var)
                   (signal c)))))

       ',old-name)))

Running the testsuite with

(defvar *nonlocal-exit-limit* nil)
(define-renamed-with-nlexit $limit old-limit *nonlocal-exit-limit*)

Results in

(%i5) :lisp(msetq $xxx  (fapply 'mlist *nonlocal-exit-limit*))

 < stuff deleted>

(%i5) map(sixth,xxx);

(%o5) [[inf/ind],[1/(zerob+zeroa)],[x,x,box(inf)]]

Maybe somebody could refine this macro further.

--Barton

_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss