From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Taylan Kammer Newsgroups: gmane.lisp.guile.user Subject: Re: Macros as 1st class objects Date: Tue, 12 Mar 2019 19:46:31 +0100 Message-ID: <87k1h4kk1k.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="209420"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: "guile-user@gnu.org" To: Tk Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Mar 12 16:47:19 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.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h3jcY-000sHH-KX for guile-user@m.gmane.org; Tue, 12 Mar 2019 16:47:14 +0100 Original-Received: from localhost ([127.0.0.1]:54607 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3jcX-0003XL-Hq for guile-user@m.gmane.org; Tue, 12 Mar 2019 11:47:13 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:58279) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3jby-0003W7-6x for guile-user@gnu.org; Tue, 12 Mar 2019 11:46:39 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3jbw-0002pi-TP for guile-user@gnu.org; Tue, 12 Mar 2019 11:46:38 -0400 Original-Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]:52358) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3jbw-0002pN-Jo for guile-user@gnu.org; Tue, 12 Mar 2019 11:46:36 -0400 Original-Received: by mail-wm1-x335.google.com with SMTP id f65so3203873wma.2 for ; Tue, 12 Mar 2019 08:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=XjA4LjbG609HnnXWS9IFmweiukzPvPnX3lz56Ql0H1g=; b=ZFDwywIULQQF1p368Lm7ldHb7UhxO87I99+X/Pig5QKjUslc7tSJobUyjGCfwqizbU FDp6W3x8HZ2FTXTUEaA+s9E1VUQEQmVeEahFiTXW08CevhSlfuuX8Gtwv304gyFoH4kN mfeKGom5pm7zRfhAJ75aisNYqVIo0iv+p8+04lNNCymHV8opAbr9dFxvQBmXwnLQo390 Dco/P33j/GXoXLQaHowW/hirgDCcgUBesln3zQpx5vx9/nBDp1wlcpUQoZBLrRKghTOJ 9LBuCHdDPEhtSckbYT67BIVjmxOlNsz4GHjzJNA8jJlLRj7RUlZBGraFF8bJ130htKnG o0mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=XjA4LjbG609HnnXWS9IFmweiukzPvPnX3lz56Ql0H1g=; b=t7jmkuH+H367xwSitkErvS4p759If618sYYRAcQkc1Cz4SADSchwIxvsc2qZde8bRD djFZy9Nti+nq5dQuz+aGoiqdlTknYWg5HE84oOVnYkeIMFA4e5Q3wgPuMX6ubks7S9pk o2g2BLm7GmtXh+wptPzPX89G5lfyR8Yn7S/Tpjs3LdUH9BxA0wepKpr3SmhOxm01xwGU lxuJ83s4aAAr1ETiAa2ienPFJ8FYRLvGCzgGMeHgPpTR4VyXy896y0zcoR0tojdLho2A QlLiDqYVDJ5/T/G1nTW8EJCEk49zm3xiWzHH1mduf2v3gw2zrJJEvNZv4LFDEd8fgmx7 lTow== X-Gm-Message-State: APjAAAU2ffQjK0bSuR+abVu6oZUaqfYRGRl668gFVNKfrz/hGoGQU688 rWg4/YKgsvQvyZUPhUwqrrxtYqAO2HA= X-Google-Smtp-Source: APXvYqx1yPM3Zek8eEpwUhX7RacZDKB/DMUyv1cYv+eDY3SuHNPsuVcyoXtZ0KBHO1wl2V3ihc0tjA== X-Received: by 2002:a1c:4009:: with SMTP id n9mr2857003wma.132.1552405594534; Tue, 12 Mar 2019 08:46:34 -0700 (PDT) Original-Received: from guixsd ([88.230.143.144]) by smtp.gmail.com with ESMTPSA id w18sm11009677wru.55.2019.03.12.08.46.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 12 Mar 2019 08:46:33 -0700 (PDT) In-Reply-To: (Tk's message of "Tue, 05 Mar 2019 08:12:33 +0000") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::335 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:15366 Archived-At: Tk writes: > How likely is that something likes this, > (define macro2 (module-ref (current-module) 'macro1)) > > breaks in the future versions of Guile? The docs say this is > low-level, so I have my suspicions. > > I have an application that defines abstract interface modules and > specialises the names of the functions defined in it in other modules > that get initialised based on user input. For normal procedures, this > works fine. Applying the same principle to macros could work with the > pattern above, but for that to make sense, i'd like to know that a) > mechanism is stable to medium-term Guile evolution, b) the practice is > not frowned upon. > > Tk > > Sent with [ProtonMail](https://protonmail.com) Secure Email. Let's see if I can help. If I understood your issue correctly, then the following should be what you need. (If I misunderstood, then sorry, it was just a fun exercise for me. :-) ) Here's a syntax-case macro that lets you alias a name to any other, where that 'other' is provided as a symbol object like in your code: (define-syntax alias (syntax-rules () ((_ ) (define-syntax (lambda (stx) (syntax-case stx () ((_ . args) (cons (datum->syntax stx ) #'args)))))))) Example usage: scheme@(guile-user)> (alias test 'display) scheme@(guile-user)> (test "foo\n") foo How does this work? It's a macro-generating macro. When you use the following code... (alias test 'display) ...the 'alias' macro (writtin in syntax-rules) firstly turns that into the following code: (define-syntax test (lambda (stx) (syntax-case stx () ((_ . args) (cons (datum->syntax stx 'display) #'args))))) And that code in turn defines 'test' as a macro (written in syntax-case) that takes any number of arguments, and creates the following code... ([['display' from the macro-call context]] arg1 arg2 ... argN) ...where I've used the pseudo-code [['display' from the macro call context]] to denote a syntax-object which encapsulates the symbol 'display' together with the syntax-context that came from the 'stx' object, which as you see I passed as an argument to 'datum->syntax'. If this still seems like black magic, you might want to look closer into how things like syntax-rules, syntax-case, or datum->syntax work, which ever ones you're not familiar with yet. Scheme macros are a fine art. :-) Lots of headache until you grasp them, but then it's blissful. Hope this was useful, - Taylan