all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Procedure (closure) using a macro fails to work after byte-compilation
@ 2016-08-04 14:24 Alex Vong
  2016-08-06 23:32 ` Michael Heerdegen
  0 siblings, 1 reply; 2+ messages in thread
From: Alex Vong @ 2016-08-04 14:24 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Procedure (closure) using a macro fails to work after byte-compilation
  2016-08-04 14:24 Procedure (closure) using a macro fails to work after byte-compilation Alex Vong
@ 2016-08-06 23:32 ` Michael Heerdegen
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Heerdegen @ 2016-08-06 23:32 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I've created a bug report about this issue with a smaller recipe:

https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-08/msg00168.html


Regards,

Michael.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-06 23:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-04 14:24 Procedure (closure) using a macro fails to work after byte-compilation Alex Vong
2016-08-06 23:32 ` Michael Heerdegen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.