From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "pelzflorian (Florian Pelz)" Newsgroups: gmane.lisp.guile.user Subject: Re: How to use-modules within macro? Date: Fri, 30 Aug 2019 08:47:34 +0200 Message-ID: <20190830064734.n5kmpqzyypmcxhkt@pelzflorian.localdomain> References: <20190829144144.lsstoykbj3xigjua@pelzflorian.localdomain> <875zmflgjh.fsf@netris.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="28552"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: NeoMutt/20180716 Cc: guile-user@gnu.org To: Mark H Weaver Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Aug 30 08:48:44 2019 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i3aiA-0007HG-Lw for guile-user@m.gmane.org; Fri, 30 Aug 2019 08:48:42 +0200 Original-Received: from localhost ([::1]:56658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i3ai8-0005AX-Va for guile-user@m.gmane.org; Fri, 30 Aug 2019 02:48:40 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54631) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i3ahD-0005AE-NA for guile-user@gnu.org; Fri, 30 Aug 2019 02:47:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i3ah7-0002o4-RV for guile-user@gnu.org; Fri, 30 Aug 2019 02:47:41 -0400 Original-Received: from pelzflorian.de ([5.45.111.108]:38956 helo=mail.pelzflorian.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i3ah6-0002jK-Jr for guile-user@gnu.org; Fri, 30 Aug 2019 02:47:37 -0400 Original-Received: from pelzflorian.localdomain (unknown [5.45.111.108]) by mail.pelzflorian.de (Postfix) with ESMTPSA id A4D683600E9; Fri, 30 Aug 2019 08:47:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=pelzflorian.de; s=mail; t=1567147654; bh=Dusugj5GMC72V4lCUeRiQgkI1D++HqeUdXeoVie/xvY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=M3gUOKe7u9iCjSoHrDMlnNu56GjVPUY4quPdRNbLiZAt3byHVP4hI8qYfNxWp4xsm uhqZmCGAIXbTh1i+jss6MTwCOazSwXQKBb1/2MROX8ZPG2ki6l3poR+z7O+Uh6DDbt o6qilIWCse0ws2ejIPXKN5JcFNfYlCXUheNTwnCY= Content-Disposition: inline In-Reply-To: <875zmflgjh.fsf@netris.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.45.111.108 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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:15689 Archived-At: On Thu, Aug 29, 2019 at 07:04:07PM -0400, Mark H Weaver wrote: > This approach is misguided and unnecessary. You don't need to include > 'use-modules' in your macro expansion, and it's best avoided. > :) I suspected I was doing something very wrong. > FYI, the way this works internally is that the macro expander operates > on "syntax objects" instead of plain S-expressions. The main difference > is that "syntax objects" keep additional information about the lexical > environments where the embedded identifiers were originally found. So > when a use of (O) expands into (local-eval 42 (the-environment)), the > identifiers 'local-eval' and 'the-environment' are looked up in the > proper environment. > I had read but not understood that syntax carries references, but of course it does because of hygiene. Thank you! > By the way, another consequence of hygiene, which you probably don't > want here, is that the (the-environment) above will capture the lexical > environment where 'O' was *defined*, instead of the environment where > (O) is used. In other words, in (let ((x 5)) (O)), the captured lexical > environment will not include 'x'. > > I should also mention that using (the-environment) will pretty much > disable most compiler optimizations that would otherwise occur with that > top-level form. That entire mechanism is best avoided if at all > possible. > > Can you tell me more broadly what you are trying to accomplish here? > I may be able to suggest an alternate approach. > > Best, > Mark What I am actually trying to do is rewriting S-expressions based on a translated gettext MO file for localization. I have a gettext POT file which contains: #: apps/packages/templates/components.scm:104$ msgid " issue" msgid_plural " issues" msgstr[0] "" msgstr[1] "" (define (sngettext x) "After choosing an identifier for behavior similar to ngettext:1,2, make it usable like (define-syntax N_ sngettext). sngettext takes into account that not all languages have only singular and plural forms." (syntax-case x () ((_ msgid1 msgid2 n) ;three sexps ;; sexp->msgid computes a gettext msgid for lookup with gettext (let* ((msgstr1 (sexp->msgid (syntax->datum #'msgid1))) (msgstr2 (sexp->msgid (syntax->datum #'msgid2)))) #`(begin (use-modules (ice-9 local-eval)) (local-eval (let ((applicable (if (= #'n 1) #'msgid1 #'msgid2))) ;; deconstruct builds a new sexp from either ;; msgid1 or msgid2 and the translation from ;; the gettext MO file, i.e. it deconstructs ;; the translation to an sexp. (deconstruct (syntax->datum applicable) (ngettext #,msgstr1 #,msgstr2 n))) (the-environment))))))) The first argument of sexp->msgid and deconstruct must be the msgid, which can be a complicated nested sexp. Macros must not have been expanded there. For ngettext, it is important that the n s-expression be evaluated by ngettext in the evaluation phase and not at expansion time. The corresponding sgettext works fine with deconstruct moved to macro expansion time, therefore there is no need for local-eval. Moving deconstruct to macro expansion time would mean I would need to deconstruct all singular, plural etc. forms, because I do not yet know the value of n. I could do this by evaluating the Plural-Forms line from the MO file, I just wanted to avoid this and let ngettext do the work. Thank you!! I now understand macros better. Regards, Florian