[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: error Wrong type to apply: #<syntax-transformer
From: |
Maxime Devos |
Subject: |
Re: error Wrong type to apply: #<syntax-transformer |
Date: |
Wed, 9 Aug 2023 11:42:29 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 |
Op 09-08-2023 om 01:00 schreef Jean Abou Samra:
Le mardi 08 août 2023 à 21:38 +0200, Maxime Devos a écrit :
As such, this not working on the top-level seems a bug to me -- after
all, a module definition is conceptually just a big let:
<enable extra reader syntax> (if applicable)
(let ()
<magic to make imports work>
(define ...)
(define-syntax-rule ...) ...
;; use a new macro using syntax-local-binding
;; to extract the syntax transformer (*).
<insert stuff in hash tables>)
(*) not sure if that precise approach actually works in this context
This is very tempting to believe, and I wish it were true, but it's not true.
At least in Guile, the <insert stuff in hash tables> part doesn't happen
at the end of evaluating the module. Each module variable is created and
inserted while evaluating the define form. Otherwise this would give
an error:
(define a 5)
(define b (module-ref (current-module) 'a))
(display b)
I don't see a problem here, only a little backwards-incompatibility (I
mean, you could just ... not do that, and do (define b a) instead).
And furthermore that incompatibility could be resolved by doing the
<insert stuff> after the corresponding (define ...) instead of at the end:
(define modules (make-hash-table))
(let ((this-module (make-hash-table)))
(hash-set! modules '(this module) this-module)
(define (foo)
(bar 'quux))
(hash-set! this-module 'foo foo)
(define foo-via-module-ref (hash-ref this-module 'foo))
(hash-set! this-module 'foo-via-module-ref foo-via-module-ref)
(define bar-procedure
(syntax-rules ()
((x) x)))
(define-syntax-rule (bar x) x)
;;(hash-set! this-module 'bar <make a syntax transformer>)
;; output: quux
(display (foo))
(newline))
;; output: quuxquux
(display ((hash-ref (hash-ref modules '(this module)) 'foo)))
(display ((hash-ref (hash-ref modules '(this module)) 'foo-via-module-ref)))
(newline)
The consequences are not innocent.
A variable is associated to a binding which is a "place" in the terminology
of some other languages, i.e., something you can set!. For toplevel variables,
because you can use module-set!, a module variable is used as the place.
And because module variables are bound to names which are just symbols,
duplicate definitions stomp on each other. Consider this:
(define a 5)
(define (b) a)
(define a 6)
(display (b))
> [...]
That problem can be avoided by disallowing duplicate definitions and
requiring using 'set!' for such things instead.
This is valid in Guile and prints 6. The second definition for `a` reuses
the same variable as the first one, effectively acting like a set!.
Contrast this with
(let ()
(define a 5)
(define (b) a)
(define a 6)
(display (b)))
which raises an error due to the duplicate binding.
As https://okmij.org/ftp/Scheme/macros.html#syntax-rule-dark-corner
puts it, "the top level of Scheme is indeed under-specified and treacherous".
It can be made a bit more specified and less treacherous.
Best,
Jean
OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key
OpenPGP_signature
Description: OpenPGP digital signature
- error Wrong type to apply: #<syntax-transformer, Damien Mattei, 2023/08/03
- Re: error Wrong type to apply: #<syntax-transformer, Maxime Devos, 2023/08/08
- Re: error Wrong type to apply: #<syntax-transformer, Damien Mattei, 2023/08/08
- Re: error Wrong type to apply: #<syntax-transformer, Jean Abou Samra, 2023/08/08
- Re: error Wrong type to apply: #<syntax-transformer,
Maxime Devos <=
- Re: error Wrong type to apply: #<syntax-transformer, Maxime Devos, 2023/08/09
- Re: error Wrong type to apply: #<syntax-transformer, Jean Abou Samra, 2023/08/09