From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Macro for replacing a placeholder in an expression Date: Wed, 27 Jul 2022 23:57:43 +0000 Message-ID: <80083c86-19b0-7537-be70-f763bd5b390f@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20952"; mail-complaints-to="usenet@ciao.gmane.io" To: Guile User Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Jul 28 01:58:05 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 1oGqui-0005Gg-To for guile-user@m.gmane-mx.org; Thu, 28 Jul 2022 01:58:04 +0200 Original-Received: from localhost ([::1]:59098 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oGquh-0000r7-Iy for guile-user@m.gmane-mx.org; Wed, 27 Jul 2022 19:58:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45546) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGquV-0000qx-EV for guile-user@gnu.org; Wed, 27 Jul 2022 19:57:51 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:37347) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGquS-0001Jp-Q1 for guile-user@gnu.org; Wed, 27 Jul 2022 19:57:51 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 3BBFB240107 for ; Thu, 28 Jul 2022 01:57:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1658966265; bh=R04YRMoN5w8iSGTYHfTMvJ0S242H1gVRGWNShlEFmtI=; h=Date:To:From:Subject:From; b=MxgDmfUczOfSprq7nojnE9xpIZ+AfX8q2M+QEgMC+gefE/yLmbugCmkfgXNds97bl VmkhFvKij0p4GJEupewlycj26UYwrL3G1A2zwSQJELY18BIlkaG3axxfUkhyNrSH27 mynW5mjQlTdoxMX7Ivr8It75A65jA28a5aLM84nw9UBvK1F5zwMlYPu1r3kvQYIew0 jIpYSl/Lp93InW9qPFU2G2oWOBUeunxwotimSrJKT2aTvADs2SjWHFaWUcUZjl0uE4 KZsNFqOX9qvo+lB0IpAuINm9OrptIHGv9xQB5/i5ZBzVsUj16k5FFYat6Bc4BvmCfo FMW6eFJFpowbg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4LtW1N355Bz6tmQ for ; Thu, 28 Jul 2022 01:57:44 +0200 (CEST) Content-Language: en-US Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:18453 Archived-At: Hello Guile Users, I am trying to write a macro, which replaces all placeholders (in this case ) with an identifier in an arbitrarily structured expression (arbitrary nesting of expressions). I have the following code now: ~~~~ (define-syntax replace-result-placeholder (syntax-rules ( replace-result-placeholder) "Iterate through the parts of an expression, search for the placeholder and replace the placeholder with the result-identifier." ;; Transform trivial cases, base cases. [(_ result-identifier ) result-identifier] [(_ result-identifier ()) (result-identifier)] [(_ result-identifier (op)) (op)] ;; If there already is such a list of transformed args ;; and there are still arguments not transformed. [(_ res-id-outer (op arg args* ... (list ;; Must match a compound expression here, to ;; avoid matching of other lists, like lists of ;; arguments in a lambda expression or ;; similar. Here we must only match a list of ;; arguments, which are yet to be transformed. (replace-result-placeholder res-id-inner arg-to-transform) other-args* ...))) (replace-result-placeholder res-id-outer (op args* ... (list (replace-result-placeholder res-id-outer arg-to-transform) other-args* ... (replace-result-placeholder res-id-inner arg))))] ;; If there already is such a list of transformed args ;; and there are no arguments not yet transformed. [(_ res-id-outer (op (list (replace-result-placeholder res-id-inner arg-to-transform) other-args* ...))) ((replace-result-placeholder res-id-outer op) (replace-result-placeholder res-id-inner arg-to-transform) other-args* ...)] ;; Create list of transformed args, if it does not yet ;; exist. [(_ result-identifier (op arg args* ...)) (replace-result-placeholder result-identifier (op args* ... (list (replace-result-placeholder result-identifier arg))))] ;; Must place this trivial case last, to avoid ;; accidental matching of compound expressions. [(_ result-identifier op) op] ;; Catch all. [(_ other* ...) (syntax-error "unrecognized form in macro call:" (quote (replace-result-placeholder other* ...)))] )) ~~~~ This already seems to work mostly: ~~~~ scheme@(guile-user)> (define res 3) scheme@(guile-user)> (replace-result-placeholder res ) $18 = 3 scheme@(guile-user)> (replace-result-placeholder res (+ 1 )) $19 = 4 scheme@(guile-user)> (replace-result-placeholder res (+ 1 (- 5 ))) $20 = 3 scheme@(guile-user)> (replace-result-placeholder res (+ 1 (* 2) (- 5 ))) $21 = 9 scheme@(guile-user)> ~~~~ I was already happy, because everything seemed to work. However, when it comes to replacing things inside lambda expressions, things seem to not work correctly: ~~~~ scheme@(guile-user)> (replace-result-placeholder res (lambda (a) (+ a ))) While compiling expression: Syntax error: unknown file:965:0: lambda: invalid argument list in subform ((a)) of (replace-result-placeholder res (a)) ~~~~ I think (but not 100% sure), that the ((a)) comes from the case: ~~~~ ... [(_ result-identifier (op)) (op)] ... ~~~~ (In this case it is not an "operation", but nevermind the name in that pattern.) I do not understand, why it outputs something which is wrapped twice in parentheses. I do not understand where that second pair of parentheses comes from. I tried creating a simpler macro, for the specific case of a lambda expression, to maybe find a solution this way. But it has got the same problem. At least it now serves to reproduce the problem in a simpler example: ~~~~ scheme@(guile-user)> (define-syntax test (syntax-rules (lambda) [(_ (op args body* ...)) ((test op) (test args) (test body* ...))] [(_ thing1 thing2 things* ...) ((test thing1) (test thing2 things* ...))] [(_ (thing)) (thing)] [(_ thing) thing])) scheme@(guile-user)> (test (lambda (a) (+ a 1))) While compiling expression: Syntax error: unknown file:798:0: lambda: invalid argument list in subform ((a)) of (test (a)) ~~~~ There seems to be something about a template like (one-thing) that I do not understand or something completely different is going on. What am I doing wrong? Best regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl