From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: problems with syntax-case and with-syntax Date: Sun, 27 Aug 2017 20:36:53 -0400 Message-ID: <87lgm4il3u.fsf@netris.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1503880673 25923 195.159.176.226 (28 Aug 2017 00:37:53 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 28 Aug 2017 00:37:53 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) Cc: Guile User To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Aug 28 02:37:48 2017 Return-path: Envelope-to: guile-user@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 1dm83g-00067o-M1 for guile-user@m.gmane.org; Mon, 28 Aug 2017 02:37:40 +0200 Original-Received: from localhost ([::1]:36277 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dm83n-0000cR-FF for guile-user@m.gmane.org; Sun, 27 Aug 2017 20:37:47 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58157) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dm83L-0000c9-0Z for guile-user@gnu.org; Sun, 27 Aug 2017 20:37:20 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dm83G-0005ZH-36 for guile-user@gnu.org; Sun, 27 Aug 2017 20:37:19 -0400 Original-Received: from world.peace.net ([50.252.239.5]:42626) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dm83F-0005Yz-Sy for guile-user@gnu.org; Sun, 27 Aug 2017 20:37:13 -0400 Original-Received: from pool-72-93-33-39.bstnma.east.verizon.net ([72.93.33.39] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dm83E-00061l-KZ; Sun, 27 Aug 2017 20:37:12 -0400 In-Reply-To: (Matt Wette's message of "Sun, 27 Aug 2017 13:18:20 -0700") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.252.239.5 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14068 Archived-At: Matt Wette writes: > Q1) The code below creates two macros. One called `define-foo' which generates a new identifier and > then defines that to #t. The other, `define-foo/p', generates the same identifier (lexical issue?) > and another identifier, then "calls" define-foo and then uses both identifiers in a `define'. When > executed I get this error: > > scheme@(guile-user)> (define-foo/p abc) > ;;; :2:0: warning: possibly unbound variable `wrap-abc' > :2:0: :2:0: In procedure module-lookup: Unbound variable: wrap-abc > > What am I doing wrong here? The problem is that in Guile 2.2, whenever (define ...) is found in the expanded code, where was introduced by a macro (i.e. not passed as an explicit argument to the macro), Guile will rewrite the into a new name based on the hash of the entire definition form. I don't know of any way to make this work without passing 'wrap-abc' explicitly as an argument to the 'define-foo' macro. FWIW, I've always been opposed to these non-standard semantics, but they were included in Guile 2.2 over my strenuous objections: https://lists.gnu.org/archive/html/guile-devel/2014-01/msg00061.html https://lists.gnu.org/archive/html/guile-devel/2011-11/msg00021.html https://lists.gnu.org/archive/html/guile-devel/2011-11/msg00042.html > Q2) Also with respect to the code below. Is there any way to pull the > definitions for stx->str and gen-id out of the define-syntax body to > make them general purpose? Yes, you can wrap the definitions of 'stx->str' within 'eval-when', like this: (eval-when (expand load eval) (define (stx->str stx) ...) (define (gen-id tmpl-id . args) ...)) above the macro definitions that use them. One more thing: > (define-syntax define-foo > (lambda (x) > (define (stx->str stx) > (symbol->string (syntax->datum stx))) > (define (gen-id tmpl-id . args) > (datum->syntax > tmpl-id > (string->symbol > (apply string-append > (map (lambda (ss) (if (string? ss) ss (stx->str ss))) args))))) > (syntax-case x () > ((_ name) > (with-syntax ((wrap (gen-id x "wrap-" #'name))) > #'(begin > (define wrap #t))))))) Here, the 'tmpl-id' that is being passed to 'datum-syntax' is not actually an identifier, but rather a compound syntax object. Although we do not currently raise an error in this case (we probably should), it is against the spec and likely to cause problems in the future, if not today. You should pass an actual identifier as 'tmpl-id'. Regards, Mark