help-gnu-emacs
[Top][All Lists]
Advanced

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

Procedure (closure) using a macro fails to work after byte-compilation


From: Alex Vong
Subject: Procedure (closure) using a macro fails to work after byte-compilation
Date: Thu, 04 Aug 2016 22:24:55 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Hi,

I am trying to simulate proper tail recursion in emacs, so I write a
custom macro that does the same as `letrec'. Following this textbook
implementation in Scheme:

  (define-syntax letrec
    (syntax-rules ()
      ((_ ((var init) ...) . body)
       (let ((var 'undefined) ...)
         (let ((var (let ((temp init)) (lambda () (set! var temp))))
               ...
               (bod (lambda () . body)))
           (var) ... (bod))))))


I wrote:

  ;; Note that we have to `(setq lexical-binding t)' before doing
  ;; anything. Also, the macro is a simplified version of the real one.
  (defmacro letrec2 (bindings form &rest forms)
    (declare (indent defun))
    (let* ((uninit-bindings ; Produce s-exp to produce uninitialized bindings.
            (cl-mapcar (lambda (binding)
                         (pcase-let ((`(,name ,_) binding))
                           `(,name (cl-gensym))))
                       bindings))

           ;; Produce s-exp to produce thunks for doing assignment.
           (assign-thunks 
            (cl-mapcar (lambda (binding)
                         (pcase-let ((temp (cl-gensym))
                                     (`(,var ,init) binding))
                           `(,var
                             (let ((,temp ,init))
                               (lambda ()
                                 (setq ,var ,temp))))))
                       bindings))

           (eval-thunks ; Produce s-exp to evaluate thunks.
            (cl-mapcar (lambda (binding)
                         (pcase-let ((`(,name ,_) binding))
                           `(funcall ,name)))
                       bindings))

           (body (cl-gensym)))

      `(let ,uninit-bindings
         (let (,@assign-thunks (,body (lambda ()
                                        ,form
                                        ,@forms)))
           ,@eval-thunks
           (funcall ,body)))))


Now the problem is that the macro works normally, but fails to work
after byte-compilation.

For example:

  (funcall (lambda ()
             (letrec2 ((sumtorial (lambda (n a)
                                    (if (zerop n)
                                        a
                                        (funcall sumtorial (- n 1)
                                                 (+ n a))))))
               (funcall sumtorial 100 0))))

=> 5050


But:

  (funcall
   (byte-compile
    '(lambda ()
       (letrec2 ((sumtorial (lambda (n a)
                              (if (zerop n)
                                  a
                                (funcall sumtorial (- n 1)
                                         (+ n a))))))
         (funcall sumtorial 100 0)))))

gives the message "Symbol's function definition is void: nil".


Is this a bug, or a limitation I am unaware of?


Cheers,
Alex



reply via email to

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