From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: Macro shadows everything in lexical environment Date: Sun, 29 Sep 2024 11:12:40 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24040"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user@gnu.org To: Adam Vagapov Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 29 11:13:32 2024 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1supzf-000667-OT for guile-user@m.gmane-mx.org; Sun, 29 Sep 2024 11:13:31 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1supz9-0006xY-DD; Sun, 29 Sep 2024 05:12:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1supz6-0006x8-Jx for guile-user@gnu.org; Sun, 29 Sep 2024 05:12:56 -0400 Original-Received: from mail-qk1-x731.google.com ([2607:f8b0:4864:20::731]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1supz3-00025l-P8 for guile-user@gnu.org; Sun, 29 Sep 2024 05:12:55 -0400 Original-Received: by mail-qk1-x731.google.com with SMTP id af79cd13be357-7a9b049251eso277201385a.2 for ; Sun, 29 Sep 2024 02:12:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727601172; x=1728205972; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=klLLweA79zdyv+lGF9rWTy3bRxOqcSz06ZNRAP09rhI=; b=k7AgBqiGMhBBaPkFYB+crwUsVobJax14hK9rIOzefbyZBGLQs/PEfxUA+R6NGiZiZ/ qKWcBXbPHji2ncTm2VL19/+regpQ6VU1lbJLObwoeWpw4pFss+vBqjyN2TAU6nH7VKlX 9FfPtj4mRUkwIIbblbCJvZuVq9mJ98GFfTCXDRtsw33s98o+9duoaWx/WqKy7F4uDfcc tanj4CpZ7EyYu7ZFatQz//6l6x4Uswfs60lALKCsbyM9fUxpyat76gg7coUZk1Bfm+aH ou9Ot+alvm9HgdcUew54hQKglOzx85ugzDmOzI+QKXVLZ6MsHc2gzNDNP1rm08NPdtzp V79w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727601172; x=1728205972; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=klLLweA79zdyv+lGF9rWTy3bRxOqcSz06ZNRAP09rhI=; b=cYcx9HJPHqxv4Xth0jKCb7/KVTepRTuNgd3lCYAF8N2+gsmxWdzJBWh8cKd6nyExUO O5LkK+p2snBUTNrXg0hdvwLZAqHdMjfSjLFz9LLWVCzZYJmQragKLpQDaXlfTQKfMZB2 XrFFJw9YzC8C3dwUU6k57wrC/3/HgL98cm7Pkejw2Zt37J+ueXpgv2U4gh/IuZhAeb7T 6sY8Zdv+IRJG5JXkNwM2syz7bvvYGkoEoccaBkTn4EYxLshT5/A5fPXQxQ9gRW1iAwye wJh2QGIadtvtvj8Bjx8UqqCGe87ozwGSWyl790W8OGLYJO5mUd1e1k0xgHeoMDoB+kGV DBFw== X-Gm-Message-State: AOJu0YxQZ2paxHZQVszGWa6vU5ihem7t9HXK11y/2u+QiU7fmVvY+X/s Ut9/4/Kx/vsSYYERrWjyqzw4fwuIDb5JMguLKMZzcMpDCjfcdmYGFyEP38gvCurbjl7SLSqkP+z /2hdCAFX/csW4+/0TOR9YqdNUt+Y= X-Google-Smtp-Source: AGHT+IEBxocGkZUVuN4FK9pnYHCq8OeoP17wUHM2InZKLuZHT4RiCoF0oktVvRZ6+Cbc5+OXXfHkXEU/BYDeCnfYwvM= X-Received: by 2002:a05:620a:1aaa:b0:79f:af4:66f1 with SMTP id af79cd13be357-7ae378c2cbfmr1418954085a.50.1727601172223; Sun, 29 Sep 2024 02:12:52 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::731; envelope-from=damien.mattei@gmail.com; helo=mail-qk1-x731.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19838 Archived-At: it will be hard to make it work, perhaps with syntax features... i find this variant that works at REPL and only : scheme@(guile-user)> (define-syntax foo-macro (syntax-rules () ((_ id) (define id (create-id-procedure id))))) (define (create-id-procedure id) (define temp id) (define (new-funct) (display "in procedure, temp=3D") (display temp) (newline)) new-funct) ; return new-funct scheme@(guile-user)> (define x 123) scheme@(guile-user)> (foo-macro x) scheme@(guile-user)> (x) in procedure, temp=3D123 indeed, always at REPL, note that your solution works in Racket: #lang racket (define-syntax-rule (foo-macro id) (begin (define temp id) ;; <-- evaluate expression [1] (define-syntax-rule (id) ;; <-- create macro [2] (begin (display "in macro, temp=3D") (display temp) (newline))))) > (define x 123) > (foo-macro x) > (x) in macro, temp=3D123 but only at toplevel too... this restrict the use of both solutions. But i really do not see the use you can do with replacing an identifier by itself as a macro, perheaps thinking of refactoring your code differently would be a good idea. If you can provide an example of your code someone could help you. regards, Damien On Sat, Sep 28, 2024 at 9:39=E2=80=AFPM Adam Vagapov wrote: > Hi everyone! > > I am currently in the process of writing macros for shortening some > common idioms I use in my Guile code. > > One thing I am struggling to do is to take an identifier that already > exists in the current lexical environment, and augment it by generating > a new macro in the lexical environment with the same identifier. > The newly created macro shadows the old identifier, but needs access to > the value that was once referred by the old identifier. > > See this code for example: > > (define-syntax-rule (foo-macro id) > (begin > (define temp id) ;; <-- evaluate expression [1] > > (define-syntax-rule (id) ;; <-- create macro [2] > (begin > (display "in macro, temp=3D") > (display temp) > (newline))))) > > > (define (bar) > (let ((x 123)) > (foo-macro x) ;; <-- store x and make 'x' a macro. > > ;; =3D=3D=3D 'x' should be a macro now =3D=3D=3D > > (x) ;; <-- should print "in macro, temp=3D123" > > #f)) > > Here, [1] first requires the identifier as-is in the lexical environment > to store its value in 'temp'. Only after storing it do I want to > introduce the new macro. > > However, what happens is that Guile tries to expand 'id' at 'temp' [1] > with the newly created macro at [2]. That means I get this error in 'bar'= : > > Syntax error: source expression failed to match any pattern in > form x > > Because the new macro doesn't handle the case where 'x' is used as just > an identifier. But I'd like the macro to only enter the lexical > environment after 'temp' is set to 'x'. > > A short test also reveals that every identifier before '(foo-macro x)' > in the caller's site also gets shadowed by the new macro. > > Is there some sort of method to remedy this? > > Regards, > Adam > >