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 09:21:02 +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="29985"; 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 10:14:07 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 1nFufN-0007bU-Uh for guile-user@m.gmane-mx.org; Fri, 04 Feb 2022 10:14:06 +0100 Original-Received: from localhost ([::1]:56638 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nFufM-0003rm-NM for guile-user@m.gmane-mx.org; Fri, 04 Feb 2022 04:14:04 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:46700) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nFtqL-0005Nk-Lx for guile-user@gnu.org; Fri, 04 Feb 2022 03:21:21 -0500 Original-Received: from [2607:f8b0:4864:20::b32] (port=36755 helo=mail-yb1-xb32.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nFtqI-0005Ix-EO for guile-user@gnu.org; Fri, 04 Feb 2022 03:21:21 -0500 Original-Received: by mail-yb1-xb32.google.com with SMTP id c6so16540548ybk.3 for ; Fri, 04 Feb 2022 00:21:14 -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=u7DvtqmmixZ87pj4a5t79d4sGcTZHrlwS39TgBEZMxU=; b=PO05jSsFz2OE4U7agNctJwlv/el/n2eFtRxidE68HPWbTyMJq6MEG3nRI9setclU6M /Io1m8Hscm3mv6p1YJnbImb0lPJU0f3/NygLxKNXumkfc1ud2CUThHljv8QdxkoSYUDR PBckWrF0BQeQBRkocL8qReAiTcFNkKHli5w8OlX5HZEVUFUwsK/n1kxdgqllvhWe5Yx8 Ys5V/K3RX98NnZL2P45dBrh2gaWLC/Knynqz+PtgMx3v9IZqJFcS8M9Csk0iE1xv4ap5 +gBFHmQ/X81i9Xs5FHdWD8PLPJFw6Ouwm00uMpoFG3ceABntCKiValHbrVTvJVE07W2K bpCQ== 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=u7DvtqmmixZ87pj4a5t79d4sGcTZHrlwS39TgBEZMxU=; b=uQaOBMLoEUSXaRfbUiGecHofZIq24Yn+ZSugdB5oTn21v0oVDIFp6FXdMz8Sjrrmqy 3STB2NeAAkychdmoZirHGlPavVMOJN2KgHHOEdIVv3s0bzVf9G8/M0X4IWQDqYPP06Uu isPJFVP/HYVdx2ZNrINKE4+6G0WnrTIfmxWHP2NX22DCR7WQW2dJZx0gvS8kcgL17XvB 3/pGc+tpiiweWnCigDUl3QcXsWzryY6PeEY+BmiDO0KwsFdxKO0RfeJqsaSvTDfREeM9 e2DU11jvUeXyL5DMNmF1qL/b4t14Gs0LD1krvfkg8b37sLw558TQ/dQN+jc77nOZdzHF ViRw== X-Gm-Message-State: AOAM5339FIM4VCBu4ipq46EEYHlOZThx44XNcdUWCzOfkFHW8T4y1LMm U1BzyjpF/e6My0ltEShwM2mbgUvC7qk+EvyTzX6BD8OcRwI= X-Google-Smtp-Source: ABdhPJzOX4TjViXSntpRtwf5mBf+OC4masm3uHElft5BsoFE+gz2BSfy9jEy2Q7iX4uTdK7fxji0WdCSEG3EFB2xN/U= X-Received: by 2002:a81:4fd8:: with SMTP id d207mr1686488ywb.505.1643962873918; Fri, 04 Feb 2022 00:21:13 -0800 (PST) In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::b32 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::b32; envelope-from=damien.mattei@gmail.com; helo=mail-yb1-xb32.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:17994 Archived-At: 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 >> >