From: Damien Mattei <damien.mattei@gmail.com>
To: Vijay Marupudi <vijaymarupudi@gatech.edu>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: reserved-keyword in macro
Date: Thu, 3 Feb 2022 11:09:58 +0100 [thread overview]
Message-ID: <CADEOadfar_xA-GwrKsci6FbUoYFpU8TLA0xkM_G1BzO+B1+beg@mail.gmail.com> (raw)
In-Reply-To: <87bkzou5yi.fsf@vijaymarupudi.com>
oh :-O yes it is the behavior expected : assign the 2nd element of an array
T with value 1 :
prefix notation of : (<- ($bracket-apply$ T 2) 1) is equivalent in Curly
Infix syntax to : {T[2] <- 1}
as expected when $bracket-apply$ is not bound we have the good result.
Thanks for this result.
Of course in this case {T[2] <- 1} it is not a problem to have not
$bracket-apply$
bound,
things became harder when doing : {T[2] <- T[4]} for example because then
i want to assign the result value of T[4] to T[2] and so i need to
evaluate T[4]
before which is exactly
($bracket-apply$ T 4) and at this point $bracket-apply$ is bind to:
(define-syntax $bracket-apply$
(syntax-rules ()
((_ container index)
;(begin ;;(display "$bracket-apply$") (newline)
(cond ((vector? container) (vector-ref container index))
((hash-table? container) (hash-table-ref container index))
(else (array-ref container index))));)
((_ array index1 index2 ...)
;(begin ;;(display "$bracket-apply$") (newline)
(if (vector? array)
(array-n-dim-ref array index1 index2 ...)
(array-ref array index1 index2 ...)))));)
and is used in the RHS (right-hand side) of:
{T[2] <- T[4]} which expand in Curly Infix SRFI-105 to:
(<- ($bracket-apply$ T 2) ($bracket-apply$ T 4))
and then is expanded in Scheme+ in two phases:
first the RHS expr is evaluate in :
((_ ($bracket-apply$ container index) expr)
(let ((value expr)) ;; to avoid compute it twice
in : (array-ref T 4)
but the LHS (Left hand side) is not evaluated with $bracket-apply$ but with
the macro <-
in the body of let :
(cond ((vector? container) (vector-set! container index value))
which give :
(vector-set! T 2 value) where value is the value of expr, previously
expanded and evaluated.
And we get the good result.
But for this we need to have sometimes $bracket-apply$ as a bound macro (or
procedure) and sometimes not, being a reserved keyword NOT bound.
This for me obscure WHY the keyword in syntax-rules MUST not be bound to
behave correctly but this is like that in Scheme standarts and we have to
deal with.
I already faced this problem earlier and the solution is in the previously
commented code:
;; (if (equal? (quote $bracket-apply$) (quote funct-or-macro)) ;; test
funct-or-macro equal $bracket-apply$
which can only be understood knowing that the macro was in the past
declared like this and commented code does not match present code,here is
the previous definition of <- :
(syntax-rules ()
;; special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T 4))
;; one dimension array, example: {a[4] <- 7}
;; $bracket-apply$ of SRFI 105
((_ (funct-or-macro container index) expr)
(let ((value expr)) ;; to avoid compute it twice
;; (if (equal? (quote $bracket-apply$) (quote funct-or-macro)) ;;
test funct-or-macro equal $bracket-apply$
;; normal case
;; {T[2] <- 4}
;; {T[3] <- T[2]}
(cond ((vector? container) (vector-set! container index value))
((hash-table? container) (hash-table-set! container index
value))
(else (array-set! container index value)))
value))
so the solution will be to remove $bracket-apply$ as literal in:
(define-syntax <-
(syntax-rules ($bracket-apply$)
and some check manually in the macro with:
(if (equal? (quote $bracket-apply$) (quote funct-or-macro)) ;; test
funct-or-macro equal $bracket-apply$
to branch instead of using pattern matching.
i will code this later but this was of great help,thanks again all.
Damien
On Thu, Feb 3, 2022 at 1:52 AM Vijay Marupudi <vijaymarupudi@gatech.edu>
wrote:
> Hi Damien,
>
> I tried to run the code you provided. I ran
>
> -----------------------------------------------------------------
>
> (define-syntax <-
> (syntax-rules ($bracket-apply$)
> ((_ ($bracket-apply$ container index) expr)
> (let ((value expr)) ;; to avoid compute it twice
> (cond ((vector? container) (vector-set! container index value))
> ((hash-table? container) (hash-table-set! container index
> value))
> (else (array-set! container index value)));)
> value))
> ((_ ($bracket-apply$ array index1 index2 ...) expr)
> (let ((value expr))
> (if (vector? array)
> (array-n-dim-set! array value index1 index2 ...)
> (array-set! array index1 index2 ... value));)
> (newline)
> value))
> ((_ (var ...) expr)
> (begin
> (display expr) (newline)
> (let ((expr-list (call-with-values (lambda () expr) list)))
> (assign-var (var ...) expr-list)
> expr-list)))
> ((_ var expr)
> (begin
> (set! var expr)
> var))
> ((_ var var1 var2 ...)
> (<- var (<- var1 var2 ...)))))
>
> (define T (make-vector 5))
> (<- ($bracket-apply$ T 2) 1)
>
> -----------------------------------------------------------------
>
> After I ran that, T was
>
> #(#<unspecified> #<unspecified> 1 #<unspecified> #<unspecified>)
>
> Is that was you are looking for?
>
> > "A literal matches an input expression if the input expression is an
> > identifier with the same name as the literal, and both are unbound13
> > <
> https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html#FOOT13
> >.
> > " as $bracket-apply$ is already bind to a definition the pattern will
> > not be matched:
>
> It's possible, as in my case, I did not have it bound, and it seems to
> have worked the way you expected?
>
> ~ Vijay
>
next prev parent reply other threads:[~2022-02-03 10:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-02 10:13 reserved-keyword in macro Damien Mattei
2022-02-02 10:33 ` Maxime Devos
[not found] ` <CADEOaddzjFbSYvVGEOkrSrjHSZvUkqC9beiRw29jbH3XHeRkZw@mail.gmail.com>
2022-02-02 13:02 ` Fwd: " Damien Mattei
2022-02-02 21:23 ` Damien Mattei
2022-02-02 21:38 ` Damien Mattei
2022-02-03 0:52 ` Vijay Marupudi
2022-02-03 10:09 ` Damien Mattei [this message]
2022-02-04 8:21 ` Damien Mattei
2022-02-04 16:24 ` Damien Mattei
2022-02-04 16:44 ` 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=CADEOadfar_xA-GwrKsci6FbUoYFpU8TLA0xkM_G1BzO+B1+beg@mail.gmail.com \
--to=damien.mattei@gmail.com \
--cc=guile-user@gnu.org \
--cc=vijaymarupudi@gatech.edu \
/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).