From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alex Vong Newsgroups: gmane.emacs.help Subject: Procedure (closure) using a macro fails to work after byte-compilation Date: Thu, 04 Aug 2016 22:24:55 +0800 Message-ID: <87vazgwr0o.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1470324554 16951 195.159.176.226 (4 Aug 2016 15:29:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 4 Aug 2016 15:29:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Aug 04 17:29:10 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bVKZu-0002l6-S5 for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Aug 2016 17:28:59 +0200 Original-Received: from localhost ([::1]:40423 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bVKZr-0001hh-Ef for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Aug 2016 11:28:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48236) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bVJaC-0004Gg-MA for help-gnu-emacs@gnu.org; Thu, 04 Aug 2016 10:25:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bVJa7-0003Qh-FI for help-gnu-emacs@gnu.org; Thu, 04 Aug 2016 10:25:11 -0400 Original-Received: from mail-pa0-x241.google.com ([2607:f8b0:400e:c03::241]:36698) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bVJa7-0003Q0-6v for help-gnu-emacs@gnu.org; Thu, 04 Aug 2016 10:25:07 -0400 Original-Received: by mail-pa0-x241.google.com with SMTP id ez1so17495960pab.3 for ; Thu, 04 Aug 2016 07:25:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version; bh=eYrX2sVKJdCNcr9AL3CCVuPHa9ukvOq17bHqMsm7LWM=; b=U+454YoQ0T/oyokEFCNpN9/rWtEEBOJrSu0rvla8eGMlM5dXJ2a68yzQNl7OICilSk yu0hz0PCQwK9o30E2XqzFldsRYIvsguUW8gWinjCPoRpnUJo5SuFdp58py4B/HiZiyr5 QxgzHuePMuSlpCnpw89fjrxMQSNQjc6pEMtT/2K5XraxNrRO1eqCiFoduvJOIVSNZji/ /dTZ+1CQdNfFbMUbHfKmQaqnOhDvxRCnnTbTQZrfeFrO0L9zh/n30FPsoUvCtsAe+Aa7 giRFgdxWQBcU458Q9exaHrZfVM+J5xrT0Cy+QarxLYO8mN312zg1bIYtkn/7hwRL92MI Ng6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=eYrX2sVKJdCNcr9AL3CCVuPHa9ukvOq17bHqMsm7LWM=; b=MMhvsJlce+g/bPK1CSrodwV8dxkmv7WxhQFjML5urgYFmGiV8yxvpGuqcKVdRj3dmU sbFx3zk9zESjhLF0/FdLpqaOwcvM+RdKz3XHyJtBQHW9xd//byHNjyjMo7rltc0jCeay Rrrll7GUx84xkU34UgzWAci+9FVa416KgdtofPFr+i9OH0ZRQooJbpj13hN4NbtUd7Sm iV1OK10hDLtTbPscX1BItXwcUnwFUba3DKhmiwM7dagu9z9oXUgxU0y0LeKtgjZkoZv9 bER+yNx+XUzWX177OCZ0iQs88BjverqdBe9Gi3M9jMUdk4z8+gL3juJ6hboY7cPo3Nq6 bkJg== X-Gm-Message-State: AEkoouuKTH5vFk9viW0POTNxe7DY3oyfq5kH1mR2D/bFFZscDMTdcrLsT5KR3AaBp7ouQA== X-Received: by 10.66.2.135 with SMTP id 7mr18253893pau.136.1470320706152; Thu, 04 Aug 2016 07:25:06 -0700 (PDT) Original-Received: from debian (n058152178019.netvigator.com. [58.152.178.19]) by smtp.gmail.com with ESMTPSA id ty6sm20988544pac.18.2016.08.04.07.25.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Aug 2016 07:25:05 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c03::241 X-Mailman-Approved-At: Thu, 04 Aug 2016 11:28:18 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:111068 Archived-At: 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