unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Alex Vong <alexvong1995@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Procedure (closure) using a macro fails to work after byte-compilation
Date: Thu, 04 Aug 2016 22:24:55 +0800	[thread overview]
Message-ID: <87vazgwr0o.fsf@gmail.com> (raw)

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	other threads:[~2016-08-04 14:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-04 14:24 Alex Vong [this message]
2016-08-06 23:32 ` Procedure (closure) using a macro fails to work after byte-compilation Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vazgwr0o.fsf@gmail.com \
    --to=alexvong1995@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).