unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* reserved-keyword in macro
@ 2022-02-02 10:13 Damien Mattei
  2022-02-02 10:33 ` Maxime Devos
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-02 10:13 UTC (permalink / raw)
  To: guile-user

hello,
what is the best way to use a reserved keyword in a macro (a keyword for me
not already used in scheme):
if i have multiple patterns in a macro,for example:
(define-syntax macro
  (syntax-rules ()
((_ arg1 arg2) code1)
((_ reserved-keyword arg) code2)))

now if i call the macro with (macro reserved-keyword 777) code1 is expansed
but not code2

i can merge code1 and code2 and do some inner test to check
reserved-keyword like this:

  (if (equal? (quote arg1) (quote reserved-keyword)) ;; test
which had worked in past but is there a better way like :
(define-syntax macro
  (syntax-rules (reserved-keyword)
but i can not fix it to work.

regards,
damien


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-02 10:13 reserved-keyword in macro Damien Mattei
@ 2022-02-02 10:33 ` Maxime Devos
       [not found]   ` <CADEOaddzjFbSYvVGEOkrSrjHSZvUkqC9beiRw29jbH3XHeRkZw@mail.gmail.com>
  0 siblings, 1 reply; 10+ messages in thread
From: Maxime Devos @ 2022-02-02 10:33 UTC (permalink / raw)
  To: Damien Mattei, guile-user

[-- Attachment #1: Type: text/plain, Size: 427 bytes --]

Damien Mattei schreef op wo 02-02-2022 om 11:13 [+0100]:
> (define-syntax macro
>   (syntax-rules ()
> ((_ arg1 arg2) code1)
> ((_ reserved-keyword arg) code2)))

The rules are matched in-order, so when the first rule matches, the
second rule is ignored.  I suggest:

(define-syntax macro
  (syntax-rules (reserved-keyword)
    ((_ reserved-keyword arg) code2)
    ((_ arg1 arg2) code1)))

Greetings,
Maxime


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Fwd: reserved-keyword in macro
       [not found]   ` <CADEOaddzjFbSYvVGEOkrSrjHSZvUkqC9beiRw29jbH3XHeRkZw@mail.gmail.com>
@ 2022-02-02 13:02     ` Damien Mattei
  2022-02-02 21:23       ` Damien Mattei
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-02 13:02 UTC (permalink / raw)
  To: guile-user

sorry i miss copy to mailing list...

---------- Forwarded message ---------
From: Damien Mattei <damien.mattei@gmail.com>
Date: Wed, Feb 2, 2022 at 12:09 PM
Subject: Re: reserved-keyword in macro
To: Maxime Devos <maximedevos@telenet.be>


thanks maxim ,it works so my problem come from elsewhere

scheme@(guile-user)> (define-syntax macro
  (syntax-rules (reserved-keyword)
    ((_ reserved-keyword arg) 'first)
... ((_ arg1 arg2) 'second)))
scheme@(guile-user)> (macro 4 5)
second
scheme@(guile-user)> (macro reserved-keyword 5)
first

this was a simplified example my real case is this:
(define-syntax <-

  (syntax-rules ($bracket-apply$)

    ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T 4))

    ;; one dimension array, example: {a[4] <- 7}
    ;; $bracket-apply$ of SRFI 105
    ((_ ($bracket-apply$ 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]}
       ;;(begin
       ;;(display "<- : vector or array set! or hash-table set!") (newline)
       (cond ((vector? container) (vector-set! container index value))
    ((hash-table? container) (hash-table-set! container index value))
    (else (array-set! container index value)));)

       ;; rare case  (to prevent any error)
       ;; (let ((var (funct-or-macro container index))) ;; MUST be in a
variable , otherwise:
       ;; While compiling expression:
       ;;  Syntax error:
       ;;  unknown location: quote: bad syntax in form quote
       ;; <- : variable set! after creation
       ;;  (set! var value)))

       value))


    ;; multi dimensions array :  {a[2 4] <- 7}
    ;; $bracket-apply$ of SRFI 105
    ((_ ($bracket-apply$ array index1 index2 ...) 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
       ;;(begin
       ;;(display "<- : multidimensional vector or array set!") (newline)
       (if (vector? array)
  (array-n-dim-set! array value index1 index2 ...)
  (array-set! array index1 index2 ... value));)

;; rare case (to prevent any error)
;; (let ((var (funct-or-macro array index ...))) ;; MUST be in a variable
;;   (display "<- : variable set! after creation (multidimensional)")
(newline)
;;   (set! var value)))
value))

     ;; not sure this case will be usefull
    ;;  (define (foo) (values 1 2 3))
    ;; (call-with-values foo list)
    ;;'(1 2 3)
    ;;  (define (foo) (display "inside foo") (newline) (values 1 2 3))
;; > (declare x y z)
;; > (<- (x y z) (foo))
    ;; ((_ (funct-or-macro arg ...) expr)
    ;;  (let ((var (funct-or-macro arg ...))
    ;;   (value expr)) ;; to avoid compute it twice
    ;;    (set! var value)
    ;;    var))

    ((_ (var ...) expr)
     (begin
     (display expr) (newline)
     (let ((expr-list (call-with-values (lambda () expr) list)))

       (assign-var (var ...) expr-list)
       expr-list)))


    ;;(<- x 5)
    ((_ var expr)

     (begin
       ;;(display "<- : variable set!") (newline)
       (set! var expr)
       var))


    ;; (declare x y z t)
    ;; {x <- y <- z <- t <- 7}
    ;; 7
    ;; (list x y z t)
    ;; (7 7 7 7)

    ;; (declare I)
    ;; {I <- (make-array 0 4 4)}
    ;; #2((0 0 0 0)
    ;;    (0 0 0 0)
    ;;    (0 0 0 0)
    ;;    (0 0 0 0))
    ;;
    ;; {I[0 0] <- I[1 1] <- I[2 2] <- I[3 3] <- 1}
    ;; 1
    ;;
    ;; I
    ;; #2((1 0 0 0)
    ;;    (0 1 0 0)
    ;;    (0 0 1 0)
    ;;    (0 0 0 1))

    ((_ var var1 var2 ...) ;; there is an expression in the last part of
ellipsis!
     (<- var (<- var1 var2 ...)))

    ))


;; > (declare x y z)
;; > (assign-var (x y z) (1 2 3))
;; > x
;; 1
;; > y
;; 2
;; > z
;; 3
(define-syntax assign-var
  (syntax-rules ()

    ((_ (var ...) (exp ...)) (begin (set! var exp) ...))))

i'm cross developping in Guile and Racket and i'm sure Guile will do as
Racket and here is the result:

Bienvenue dans DrRacket, version 7.7 [3m].
Langage: reader "../SRFI/SRFI-105.rkt", avec débogage; limite mémoire : 128
MB.
> (declare x y z)
> (define (foo) (display "inside foo") (newline) (values 1 2 3))
> (<- (x y z) (foo))
inside foo
. .
../../../../../../../../usr/share/racket/collects/racket/private/kw.rkt:1201:25:
result arity mismatch;
 expected number of values not received
  expected: 1
  received: 3
  values...:
> (define T (make-vector 5))
> {T[2] <- 1}
1
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var ($bracket-apply$ T 2) expr-list)
> (<- T[2] 1)
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var (2) expr-list)
> (<- ($bracket-apply$ T 2) 1)
1
. ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
(assign-var ($bracket-apply$ T 2) expr-list)
>

the problem is that anyway i do it when using reserved keyword in
define-syntax the macro use the third case:
(_ (var ...) expr)
even when i have :

(syntax-rules ($bracket-apply$)

    ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T 4))

    ;; one dimension array, example: {a[4] <- 7}
    ;; $bracket-apply$ of SRFI 105
    ((_ ($bracket-apply$ container index) expr)


with (<- ($bracket-apply$ T 2) 1) entered at toplevel i should not be in
the third case but the first of the macro <-

that is strange... i think i could not get help from mailing list on a so
complex example that rely on my Scheme+ development for multiple values
return (aka let-values replacment with <- ),
sorry for the inconvenience
Damien


On Wed, Feb 2, 2022 at 11:33 AM Maxime Devos <maximedevos@telenet.be> wrote:

> Damien Mattei schreef op wo 02-02-2022 om 11:13 [+0100]:
> > (define-syntax macro
> >   (syntax-rules ()
> > ((_ arg1 arg2) code1)
> > ((_ reserved-keyword arg) code2)))
>
> The rules are matched in-order, so when the first rule matches, the
> second rule is ignored.  I suggest:
>
> (define-syntax macro
>   (syntax-rules (reserved-keyword)
>     ((_ reserved-keyword arg) code2)
>     ((_ arg1 arg2) code1)))
>
> Greetings,
> Maxime
>
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-02 13:02     ` Fwd: " Damien Mattei
@ 2022-02-02 21:23       ` Damien Mattei
  2022-02-02 21:38         ` Damien Mattei
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-02 21:23 UTC (permalink / raw)
  To: guile-user

really,nothing have an idea? seems $bracket-apply$ is bind to the variable
var of the third case in the macro instead of ignored in the first case...
but why? (it the same thing if i change the patterns order)

On Wed, Feb 2, 2022 at 2:02 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> sorry i miss copy to mailing list...
>
> ---------- Forwarded message ---------
> From: Damien Mattei <damien.mattei@gmail.com>
> Date: Wed, Feb 2, 2022 at 12:09 PM
> Subject: Re: reserved-keyword in macro
> To: Maxime Devos <maximedevos@telenet.be>
>
>
> thanks maxim ,it works so my problem come from elsewhere
>
> scheme@(guile-user)> (define-syntax macro
>   (syntax-rules (reserved-keyword)
>     ((_ reserved-keyword arg) 'first)
> ... ((_ arg1 arg2) 'second)))
> scheme@(guile-user)> (macro 4 5)
> second
> scheme@(guile-user)> (macro reserved-keyword 5)
> first
>
> this was a simplified example my real case is this:
> (define-syntax <-
>
>   (syntax-rules ($bracket-apply$)
>
>     ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T
> 4))
>
>     ;; one dimension array, example: {a[4] <- 7}
>     ;; $bracket-apply$ of SRFI 105
>     ((_ ($bracket-apply$ 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]}
>        ;;(begin
>        ;;(display "<- : vector or array set! or hash-table set!") (newline)
>        (cond ((vector? container) (vector-set! container index value))
>     ((hash-table? container) (hash-table-set! container index value))
>     (else (array-set! container index value)));)
>
>        ;; rare case  (to prevent any error)
>        ;; (let ((var (funct-or-macro container index))) ;; MUST be in a
> variable , otherwise:
>        ;; While compiling expression:
>        ;;  Syntax error:
>        ;;  unknown location: quote: bad syntax in form quote
>        ;; <- : variable set! after creation
>        ;;  (set! var value)))
>
>        value))
>
>
>     ;; multi dimensions array :  {a[2 4] <- 7}
>     ;; $bracket-apply$ of SRFI 105
>     ((_ ($bracket-apply$ array index1 index2 ...) 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
>        ;;(begin
>        ;;(display "<- : multidimensional vector or array set!") (newline)
>        (if (vector? array)
>   (array-n-dim-set! array value index1 index2 ...)
>   (array-set! array index1 index2 ... value));)
>
> ;; rare case (to prevent any error)
> ;; (let ((var (funct-or-macro array index ...))) ;; MUST be in a variable
> ;;   (display "<- : variable set! after creation (multidimensional)")
> (newline)
> ;;   (set! var value)))
> value))
>
>      ;; not sure this case will be usefull
>     ;;  (define (foo) (values 1 2 3))
>     ;; (call-with-values foo list)
>     ;;'(1 2 3)
>     ;;  (define (foo) (display "inside foo") (newline) (values 1 2 3))
> ;; > (declare x y z)
> ;; > (<- (x y z) (foo))
>     ;; ((_ (funct-or-macro arg ...) expr)
>     ;;  (let ((var (funct-or-macro arg ...))
>     ;;   (value expr)) ;; to avoid compute it twice
>     ;;    (set! var value)
>     ;;    var))
>
>     ((_ (var ...) expr)
>      (begin
>      (display expr) (newline)
>      (let ((expr-list (call-with-values (lambda () expr) list)))
>
>        (assign-var (var ...) expr-list)
>        expr-list)))
>
>
>     ;;(<- x 5)
>     ((_ var expr)
>
>      (begin
>        ;;(display "<- : variable set!") (newline)
>        (set! var expr)
>        var))
>
>
>     ;; (declare x y z t)
>     ;; {x <- y <- z <- t <- 7}
>     ;; 7
>     ;; (list x y z t)
>     ;; (7 7 7 7)
>
>     ;; (declare I)
>     ;; {I <- (make-array 0 4 4)}
>     ;; #2((0 0 0 0)
>     ;;    (0 0 0 0)
>     ;;    (0 0 0 0)
>     ;;    (0 0 0 0))
>     ;;
>     ;; {I[0 0] <- I[1 1] <- I[2 2] <- I[3 3] <- 1}
>     ;; 1
>     ;;
>     ;; I
>     ;; #2((1 0 0 0)
>     ;;    (0 1 0 0)
>     ;;    (0 0 1 0)
>     ;;    (0 0 0 1))
>
>     ((_ var var1 var2 ...) ;; there is an expression in the last part of
> ellipsis!
>      (<- var (<- var1 var2 ...)))
>
>     ))
>
>
> ;; > (declare x y z)
> ;; > (assign-var (x y z) (1 2 3))
> ;; > x
> ;; 1
> ;; > y
> ;; 2
> ;; > z
> ;; 3
> (define-syntax assign-var
>   (syntax-rules ()
>
>     ((_ (var ...) (exp ...)) (begin (set! var exp) ...))))
>
> i'm cross developping in Guile and Racket and i'm sure Guile will do as
> Racket and here is the result:
>
> Bienvenue dans DrRacket, version 7.7 [3m].
> Langage: reader "../SRFI/SRFI-105.rkt", avec débogage; limite mémoire :
> 128 MB.
> > (declare x y z)
> > (define (foo) (display "inside foo") (newline) (values 1 2 3))
> > (<- (x y z) (foo))
> inside foo
> . .
> ../../../../../../../../usr/share/racket/collects/racket/private/kw.rkt:1201:25:
> result arity mismatch;
>  expected number of values not received
>   expected: 1
>   received: 3
>   values...:
> > (define T (make-vector 5))
> > {T[2] <- 1}
> 1
> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
> (assign-var ($bracket-apply$ T 2) expr-list)
> > (<- T[2] 1)
> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
> (assign-var (2) expr-list)
> > (<- ($bracket-apply$ T 2) 1)
> 1
> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
> (assign-var ($bracket-apply$ T 2) expr-list)
> >
>
> the problem is that anyway i do it when using reserved keyword in
> define-syntax the macro use the third case:
> (_ (var ...) expr)
> even when i have :
>
> (syntax-rules ($bracket-apply$)
>
>     ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T
> 4))
>
>     ;; one dimension array, example: {a[4] <- 7}
>     ;; $bracket-apply$ of SRFI 105
>     ((_ ($bracket-apply$ container index) expr)
>
>
> with (<- ($bracket-apply$ T 2) 1) entered at toplevel i should not be in
> the third case but the first of the macro <-
>
> that is strange... i think i could not get help from mailing list on a so
> complex example that rely on my Scheme+ development for multiple values
> return (aka let-values replacment with <- ),
> sorry for the inconvenience
> Damien
>
>
> On Wed, Feb 2, 2022 at 11:33 AM Maxime Devos <maximedevos@telenet.be>
> wrote:
>
>> Damien Mattei schreef op wo 02-02-2022 om 11:13 [+0100]:
>> > (define-syntax macro
>> >   (syntax-rules ()
>> > ((_ arg1 arg2) code1)
>> > ((_ reserved-keyword arg) code2)))
>>
>> The rules are matched in-order, so when the first rule matches, the
>> second rule is ignored.  I suggest:
>>
>> (define-syntax macro
>>   (syntax-rules (reserved-keyword)
>>     ((_ reserved-keyword arg) code2)
>>     ((_ arg1 arg2) code1)))
>>
>> Greetings,
>> Maxime
>>
>>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-02 21:23       ` Damien Mattei
@ 2022-02-02 21:38         ` Damien Mattei
  2022-02-03  0:52           ` Vijay Marupudi
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-02 21:38 UTC (permalink / raw)
  To: guile-user

i suppose it has something to see with this:
"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:
https://www.gnu.org/software/guile/manual/html_node/Syntax-Rules.html


On Wed, Feb 2, 2022 at 10:23 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> really,nothing have an idea? seems $bracket-apply$ is bind to the variable
> var of the third case in the macro instead of ignored in the first case...
> but why? (it the same thing if i change the patterns order)
>
> On Wed, Feb 2, 2022 at 2:02 PM Damien Mattei <damien.mattei@gmail.com>
> wrote:
>
>> sorry i miss copy to mailing list...
>>
>> ---------- Forwarded message ---------
>> From: Damien Mattei <damien.mattei@gmail.com>
>> Date: Wed, Feb 2, 2022 at 12:09 PM
>> Subject: Re: reserved-keyword in macro
>> To: Maxime Devos <maximedevos@telenet.be>
>>
>>
>> thanks maxim ,it works so my problem come from elsewhere
>>
>> scheme@(guile-user)> (define-syntax macro
>>   (syntax-rules (reserved-keyword)
>>     ((_ reserved-keyword arg) 'first)
>> ... ((_ arg1 arg2) 'second)))
>> scheme@(guile-user)> (macro 4 5)
>> second
>> scheme@(guile-user)> (macro reserved-keyword 5)
>> first
>>
>> this was a simplified example my real case is this:
>> (define-syntax <-
>>
>>   (syntax-rules ($bracket-apply$)
>>
>>     ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T
>> 4))
>>
>>     ;; one dimension array, example: {a[4] <- 7}
>>     ;; $bracket-apply$ of SRFI 105
>>     ((_ ($bracket-apply$ 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]}
>>        ;;(begin
>>        ;;(display "<- : vector or array set! or hash-table set!")
>> (newline)
>>        (cond ((vector? container) (vector-set! container index value))
>>     ((hash-table? container) (hash-table-set! container index value))
>>     (else (array-set! container index value)));)
>>
>>        ;; rare case  (to prevent any error)
>>        ;; (let ((var (funct-or-macro container index))) ;; MUST be in a
>> variable , otherwise:
>>        ;; While compiling expression:
>>        ;;  Syntax error:
>>        ;;  unknown location: quote: bad syntax in form quote
>>        ;; <- : variable set! after creation
>>        ;;  (set! var value)))
>>
>>        value))
>>
>>
>>     ;; multi dimensions array :  {a[2 4] <- 7}
>>     ;; $bracket-apply$ of SRFI 105
>>     ((_ ($bracket-apply$ array index1 index2 ...) 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
>>        ;;(begin
>>        ;;(display "<- : multidimensional vector or array set!") (newline)
>>        (if (vector? array)
>>   (array-n-dim-set! array value index1 index2 ...)
>>   (array-set! array index1 index2 ... value));)
>>
>> ;; rare case (to prevent any error)
>> ;; (let ((var (funct-or-macro array index ...))) ;; MUST be in a variable
>> ;;   (display "<- : variable set! after creation (multidimensional)")
>> (newline)
>> ;;   (set! var value)))
>> value))
>>
>>      ;; not sure this case will be usefull
>>     ;;  (define (foo) (values 1 2 3))
>>     ;; (call-with-values foo list)
>>     ;;'(1 2 3)
>>     ;;  (define (foo) (display "inside foo") (newline) (values 1 2 3))
>> ;; > (declare x y z)
>> ;; > (<- (x y z) (foo))
>>     ;; ((_ (funct-or-macro arg ...) expr)
>>     ;;  (let ((var (funct-or-macro arg ...))
>>     ;;   (value expr)) ;; to avoid compute it twice
>>     ;;    (set! var value)
>>     ;;    var))
>>
>>     ((_ (var ...) expr)
>>      (begin
>>      (display expr) (newline)
>>      (let ((expr-list (call-with-values (lambda () expr) list)))
>>
>>        (assign-var (var ...) expr-list)
>>        expr-list)))
>>
>>
>>     ;;(<- x 5)
>>     ((_ var expr)
>>
>>      (begin
>>        ;;(display "<- : variable set!") (newline)
>>        (set! var expr)
>>        var))
>>
>>
>>     ;; (declare x y z t)
>>     ;; {x <- y <- z <- t <- 7}
>>     ;; 7
>>     ;; (list x y z t)
>>     ;; (7 7 7 7)
>>
>>     ;; (declare I)
>>     ;; {I <- (make-array 0 4 4)}
>>     ;; #2((0 0 0 0)
>>     ;;    (0 0 0 0)
>>     ;;    (0 0 0 0)
>>     ;;    (0 0 0 0))
>>     ;;
>>     ;; {I[0 0] <- I[1 1] <- I[2 2] <- I[3 3] <- 1}
>>     ;; 1
>>     ;;
>>     ;; I
>>     ;; #2((1 0 0 0)
>>     ;;    (0 1 0 0)
>>     ;;    (0 0 1 0)
>>     ;;    (0 0 0 1))
>>
>>     ((_ var var1 var2 ...) ;; there is an expression in the last part of
>> ellipsis!
>>      (<- var (<- var1 var2 ...)))
>>
>>     ))
>>
>>
>> ;; > (declare x y z)
>> ;; > (assign-var (x y z) (1 2 3))
>> ;; > x
>> ;; 1
>> ;; > y
>> ;; 2
>> ;; > z
>> ;; 3
>> (define-syntax assign-var
>>   (syntax-rules ()
>>
>>     ((_ (var ...) (exp ...)) (begin (set! var exp) ...))))
>>
>> i'm cross developping in Guile and Racket and i'm sure Guile will do as
>> Racket and here is the result:
>>
>> Bienvenue dans DrRacket, version 7.7 [3m].
>> Langage: reader "../SRFI/SRFI-105.rkt", avec débogage; limite mémoire :
>> 128 MB.
>> > (declare x y z)
>> > (define (foo) (display "inside foo") (newline) (values 1 2 3))
>> > (<- (x y z) (foo))
>> inside foo
>> . .
>> ../../../../../../../../usr/share/racket/collects/racket/private/kw.rkt:1201:25:
>> result arity mismatch;
>>  expected number of values not received
>>   expected: 1
>>   received: 3
>>   values...:
>> > (define T (make-vector 5))
>> > {T[2] <- 1}
>> 1
>> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
>> (assign-var ($bracket-apply$ T 2) expr-list)
>> > (<- T[2] 1)
>> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
>> (assign-var (2) expr-list)
>> > (<- ($bracket-apply$ T 2) 1)
>> 1
>> . ../required-files/assignment.rkt:140:7: assign-var: bad syntax in:
>> (assign-var ($bracket-apply$ T 2) expr-list)
>> >
>>
>> the problem is that anyway i do it when using reserved keyword in
>> define-syntax the macro use the third case:
>> (_ (var ...) expr)
>> even when i have :
>>
>> (syntax-rules ($bracket-apply$)
>>
>>     ;;  special form like : (<- ($bracket-apply$ T 3) ($bracket-apply$ T
>> 4))
>>
>>     ;; one dimension array, example: {a[4] <- 7}
>>     ;; $bracket-apply$ of SRFI 105
>>     ((_ ($bracket-apply$ container index) expr)
>>
>>
>> with (<- ($bracket-apply$ T 2) 1) entered at toplevel i should not be in
>> the third case but the first of the macro <-
>>
>> that is strange... i think i could not get help from mailing list on a so
>> complex example that rely on my Scheme+ development for multiple values
>> return (aka let-values replacment with <- ),
>> sorry for the inconvenience
>> Damien
>>
>>
>> On Wed, Feb 2, 2022 at 11:33 AM Maxime Devos <maximedevos@telenet.be>
>> wrote:
>>
>>> Damien Mattei schreef op wo 02-02-2022 om 11:13 [+0100]:
>>> > (define-syntax macro
>>> >   (syntax-rules ()
>>> > ((_ arg1 arg2) code1)
>>> > ((_ reserved-keyword arg) code2)))
>>>
>>> The rules are matched in-order, so when the first rule matches, the
>>> second rule is ignored.  I suggest:
>>>
>>> (define-syntax macro
>>>   (syntax-rules (reserved-keyword)
>>>     ((_ reserved-keyword arg) code2)
>>>     ((_ arg1 arg2) code1)))
>>>
>>> Greetings,
>>> Maxime
>>>
>>>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-02 21:38         ` Damien Mattei
@ 2022-02-03  0:52           ` Vijay Marupudi
  2022-02-03 10:09             ` Damien Mattei
  0 siblings, 1 reply; 10+ messages in thread
From: Vijay Marupudi @ 2022-02-03  0:52 UTC (permalink / raw)
  To: Damien Mattei, guile-user

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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-03  0:52           ` Vijay Marupudi
@ 2022-02-03 10:09             ` Damien Mattei
  2022-02-04  8:21               ` Damien Mattei
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-03 10:09 UTC (permalink / raw)
  To: Vijay Marupudi; +Cc: guile-user

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
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-03 10:09             ` Damien Mattei
@ 2022-02-04  8:21               ` Damien Mattei
  2022-02-04 16:24                 ` Damien Mattei
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-04  8:21 UTC (permalink / raw)
  To: guile-user

definitely i do not think it is possible to have a macro doing this:
{(x y z) <- (foo)} ;; foo is a function that return 3 values (ex: (values 1
2 3))
or this :
{(x y z ...) <- (bar)} ;; bar return an arbitrary (known in advance )
number of values
and also have a compatibility with:
{T[2] <- T[1]} that deal with arrays

$bracket-apply$ and syntax-rules is not enough i think i will have to use
$nfx$ overloading from SRFI-105 for doing almost the same:
{x y z <- (foo)} which i admit is more Haskell style than Scheme

On Thu, Feb 3, 2022 at 11:09 AM Damien Mattei <damien.mattei@gmail.com>
wrote:

> 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
>>
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-04  8:21               ` Damien Mattei
@ 2022-02-04 16:24                 ` Damien Mattei
  2022-02-04 16:44                   ` Maxime Devos
  0 siblings, 1 reply; 10+ messages in thread
From: Damien Mattei @ 2022-02-04 16:24 UTC (permalink / raw)
  To: guile-user

even with $nfx$ it does not seem to be possible without set!-values:
https://docs.racket-lang.org/reference/set_.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._set%21-values%29%29

does someone know how to implement set!-values in Scheme for Guile?

with set!-values things are really easy:
(define-syntax <+
  (syntax-rules ()

    ((_ (var1 ...) expr) (begin
  (define var1 '())
  ...
  (display "<+ multiple") (newline)
  (set!-values (var1 ...) expr)))
    ((_ var expr) (begin
   (display "<+ single") (newline)
   (define var expr)
   (display var) (newline)))))

Welcome to DrRacket, version 8.2 [cs].
Language: reader "../SRFI/SRFI-105.rkt", with debugging; memory limit: 128
MB.
> (define (foo) (values 1 2 3))
> {(x y z) <+ (foo)}
<+ multiple
> x
1
> y
2

(define-syntax </

  (syntax-rules ()

    ((</ (x ...) expr) (set!-values (x ...) expr))))

> (declare x y z)
> {(x y z) </ (foo)}
> x
1
> y
2
> z
3


On Fri, Feb 4, 2022 at 9:21 AM Damien Mattei <damien.mattei@gmail.com>
wrote:

> definitely i do not think it is possible to have a macro doing this:
> {(x y z) <- (foo)} ;; foo is a function that return 3 values (ex: (values
> 1 2 3))
> or this :
> {(x y z ...) <- (bar)} ;; bar return an arbitrary (known in advance )
> number of values
> and also have a compatibility with:
> {T[2] <- T[1]} that deal with arrays
>
> $bracket-apply$ and syntax-rules is not enough i think i will have to use
> $nfx$ overloading from SRFI-105 for doing almost the same:
> {x y z <- (foo)} which i admit is more Haskell style than Scheme
>
> On Thu, Feb 3, 2022 at 11:09 AM Damien Mattei <damien.mattei@gmail.com>
> wrote:
>
>> 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
>>>
>>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: reserved-keyword in macro
  2022-02-04 16:24                 ` Damien Mattei
@ 2022-02-04 16:44                   ` Maxime Devos
  0 siblings, 0 replies; 10+ messages in thread
From: Maxime Devos @ 2022-02-04 16:44 UTC (permalink / raw)
  To: Damien Mattei, guile-user


[-- Attachment #1.1: Type: text/plain, Size: 953 bytes --]

Damien Mattei schreef op vr 04-02-2022 om 17:24 [+0100]:
> even with $nfx$ it does not seem to be possible without set!-values:
> https://docs.racket-lang.org/reference/set_.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._set%21-values%29%29
> 
> does someone know how to implement set!-values in Scheme for Guile?

This is not Guile-specific, it works in any Scheme with syntax-rules,
by using a form of recursion, call-with-values, set!, lambda and
values, see attachement (LGPL3.0+ licensed).

Also, Racket is free software, so you could look at the source code of
Racket, maybe Racket's implementation can be ported to Guile Scheme and
maybe it's already valid Guile Scheme.

> On Fri, Feb 4, 2022 at 9:21 AM Damien Mattei <damien.mattei@gmail.com>
> wrote:
> [ a huge block of text ]

This block of text does not appear to have anything to do with your
question, what is it included for?

Greetings,
Maxime.

[-- Attachment #1.2: Type: text/plain, Size: 1149 bytes --]

;;; Copyright © 2022 Maxime Devos
;;;
;;; This library is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-syntax set!-values
  (syntax-rules ()
    ((_ (var var* ...) exp)
     (call-with-values
       (lambda () exp)
       (lambda (value . rest)
         (set! var value)
         (set!-values (var* ...) (apply values rest)))))
    ((_ () exp)
     (call-with-values
       (lambda () exp)
       (lambda () (values)))))) ; nothing to do!

(define x)
(define y)
(define z)
(set!-values (x y z) (values 0 1 2))
(pk x y z)

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-02-04 16:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2022-02-04  8:21               ` Damien Mattei
2022-02-04 16:24                 ` Damien Mattei
2022-02-04 16:44                   ` Maxime Devos

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).