From: Maxime Devos <maximedevos@telenet.be>
To: Sascha Ziemann <ceving@gmail.com>, guile-user@gnu.org
Subject: Re: Does declaration order matter in guile?
Date: Mon, 13 Feb 2023 18:07:10 +0100 [thread overview]
Message-ID: <f620cd29-d0ed-32f3-b422-caddb7263808@telenet.be> (raw)
In-Reply-To: <CAGUt3y6hGkNKpgM_26cPr6obOmLxmLjRUgBTJ1VWWhAhPNqMzA@mail.gmail.com>
[-- Attachment #1.1.1: Type: text/plain, Size: 2777 bytes --]
On 13-02-2023 09:05, Sascha Ziemann wrote:
> You also can not ask Scheme about macros, because macros are not
> first-class-citizens.
>
>
> This might be interesting:
>
https://matt.might.net/articles/metacircular-evaluation-and-first-class-run-time-macros/
You actually can ask (Guile)Scheme about macros, and they are
first-class (just not procedures, and almost never actually used as
first-class things):
;; first-class: macros are just a type of values
(let ((the-case-macro (module-ref (current-module) 'case)))
(pk the-case-macro) ; -> #<syntax-transformer case>
;; This type is disjoint from procedures:
(pk (procedure? the-case-macro)) ; -> #false
(pk (macro? the-case-macro)) ; -> #true
;; You can use macros to transform syntax to new syntax at runtime:
(pk (procedure? (macro-transformer the-case-macro))) ; -> #true
(pk ((macro-transformer the-case-macro)
#'(case number
((one) 1)
((two) 2)))) ; -> #<syntax: [lots of stuff]>
;; You can make macros at runtime (though usually you would just
;; pass syntax-transforming procedures instead of the macro wrapper
;; type):
(pk (make-syntax-transformer 'pick-a-name 'macro (lambda (s) #'0)))
(values))
While unconventional, in principle there is nothing stopping you
(besides cross-Scheme compatibility) from using a combination of
'let-syntax-syntax', 'syntax-case' and 'syntax-local-binding' to make
let macros accept macros as arguments. Example:
(use-modules (system syntax))
(define-syntax call-macro-for-each
(lambda (s)
(syntax-case s ()
((_ macro-identifier arg ...)
(call-with-values
(lambda ()
;; Note: syntax-local-binding implicitly calls macro-transformer
(syntax-local-binding #'macro-identifier))
(lambda (type value)
(unless (eq? type 'macro)
(error "first argument to call-macro-for-each must be an
identifier of a macro"))
(let loop ((arguments #'(arg ...)))
(syntax-case arguments ()
((last) (value #'last))
((stuff . more-stuff)
#`(begin #,(value #'stuff)
#,(loop #'more-stuff)))))))))))
(let-syntax ((pk+quote
(lambda (s)
#`(pk '#,s '-> #,s))))
(call-macro-for-each pk+quote (+ 1 1) (+ 1 2) (+ 1 3)))
;; Output:
;;; ((+ 1 1) -> 2)
;;; ((+ 1 2) -> 3)
;;; ((+ 1 3) -> 4)
$1 = 4
The only snag here is that each macro you want to pass to a
'higher-order macro', you need to give an identifier. As you can always
do that with 'let-syntax', that doesn't make it non-first class IMO.
Greetings,
Maxime.
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
next prev parent reply other threads:[~2023-02-13 17:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-12 18:46 Does declaration order matter in guile? wolf
2023-02-12 19:52 ` Taylan Kammer
2023-02-13 8:05 ` Sascha Ziemann
2023-02-13 10:44 ` Dr. Arne Babenhauserheide
2023-02-13 17:07 ` Maxime Devos [this message]
2023-02-14 20:00 ` wolf
2023-02-14 20:26 ` Dr. Arne Babenhauserheide
2023-02-13 17:10 ` Maxime Devos
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f620cd29-d0ed-32f3-b422-caddb7263808@telenet.be \
--to=maximedevos@telenet.be \
--cc=ceving@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).