From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: reserved-keyword in macro Date: Fri, 4 Feb 2022 17:24:54 +0100 Message-ID: References: <6f5e23754b7523f30e31d9006b0a5a1127d503cb.camel@telenet.be> <87bkzou5yi.fsf@vijaymarupudi.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16695"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Feb 04 17:26:45 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nG1Q4-00048T-JT for guile-user@m.gmane-mx.org; Fri, 04 Feb 2022 17:26:44 +0100 Original-Received: from localhost ([::1]:60298 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nG1Q3-0007kQ-Jr for guile-user@m.gmane-mx.org; Fri, 04 Feb 2022 11:26:43 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:46932) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nG1Oa-0006nm-TI for guile-user@gnu.org; Fri, 04 Feb 2022 11:25:15 -0500 Original-Received: from [2607:f8b0:4864:20::b2e] (port=39723 helo=mail-yb1-xb2e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nG1OW-0006N7-Vp for guile-user@gnu.org; Fri, 04 Feb 2022 11:25:12 -0500 Original-Received: by mail-yb1-xb2e.google.com with SMTP id 124so19652158ybw.6 for ; Fri, 04 Feb 2022 08:25:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=7wVkF0J3mAPqg60XV59V7vd/6KeNnoIRDAwFmTAdyC4=; b=NGAKsBKxA29DAhEiSzpVrZHE+ZSQjmHPf+zmlxqtlGIP9w0i7nFD/84s3mDj+RkzwJ 5uGpfdGSqMQBNWWmmS2vD2rPpUZV2g4S2poD7HImasIRlz1+Ru3+hi5S1h8Zok0A83qa Xzz1z97Oa4/koGpfDRlhviP46F0wIDRe0YPQUjqn3/kKoOXlhDyIcxMRPdZOzA977pF+ 3lF5rlIMf0b4BGd16IX60s0KQ0RzWyczRxrFcQ9ZWXT+BBIdKFSQ7SMBIxFeOnZQtWiv CsevtBUIVNpHeq2rp2t6bwGeTojp4fKPS9/bVh3eeX8os9ZUput6EMb/rood7KzxkQsp DFLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=7wVkF0J3mAPqg60XV59V7vd/6KeNnoIRDAwFmTAdyC4=; b=jQ2efvKUvdsJ/bm4hp7SggdfIi9RpChGy6ybroF6eo8H2T43pSkpcjSKC4UBc6pxRc FIpyJzKqQbCBeFNyro6WRYM5eU/t7s6oOCogvsN2pAU38y/XIoWC4aWJVx5LVcD7Dryl FtcsULD87WKliYe9/j2OG541lQBWZmsxnA3PbeUN/GGaqePUEq+zQpToIirQPOGOBoKn uH1ESmsxHeZQR2KtZkYa6VTVaFCPxFHF8iVWXWnUpfKMAwpmSKeUxdrk3eZmn5s8uMfO fB4XJ0kOkEh8k2LyReCai8I1zwtJ4yfjPktcHjISjZZifCSZSUEycYHdwW4MTUGvXTAu QDLA== X-Gm-Message-State: AOAM533k/u9XQ9tAo4tLbuX4vEBiqaJD6bZgJy8cbpZ621u7KWFa+/zd +Iijka6i61IYR3c80YHsNWtjwr+BZJ+T/RzLqG1rmHFpubA= X-Google-Smtp-Source: ABdhPJz0HR+H6wNJfACHF9cPLIV4XWOWoYyPxDkIqEzmkYFgnxqHzSgVAsJvclA54x5q9TZLsB+ghs6Mpke8L73sK6c= X-Received: by 2002:a25:7e81:: with SMTP id z123mr435537ybc.466.1643991906014; Fri, 04 Feb 2022 08:25:06 -0800 (PST) In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::b2e (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::b2e; envelope-from=damien.mattei@gmail.com; helo=mail-yb1-xb2e.google.com X-Spam_score_int: 7 X-Spam_score: 0.7 X-Spam_bar: / X-Spam_report: (0.7 / 5.0 requ) AC_HTML_NONSENSE_TAGS=1.997, BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17995 Archived-At: 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 (declare x y z) > {(x y z) x 1 > y 2 > z 3 On Fri, Feb 4, 2022 at 9:21 AM Damien Mattei 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 > 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 >> 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 >>> >>> #(# # 1 # #) >>> >>> 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 >>> >>