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: Re: Understanding `symbol??` macro from okmij.org Date: Thu, 14 Mar 2024 13:46:31 +0000 Message-ID: <6b132982-1585-4fc3-b2b4-98ef84b6af42@posteo.de> References: <9142ff20-0722-4fb7-b35a-7434e68d1777@posteo.de> <20240314020307.yR322B00F319MNK01R36Mg@andre.telenet-ops.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22022"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Maxime Devos , Jean Abou Samra Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Mar 14 14:47:23 2024 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 1rklQY-0005RW-Tl for guile-user@m.gmane-mx.org; Thu, 14 Mar 2024 14:47:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rklPv-0005yz-MN; Thu, 14 Mar 2024 09:46:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rklPu-0005yd-0n for guile-user@gnu.org; Thu, 14 Mar 2024 09:46:42 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rklPp-0002NQ-Aw for guile-user@gnu.org; Thu, 14 Mar 2024 09:46:41 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id EC2BA240104 for ; Thu, 14 Mar 2024 14:46:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1710423994; bh=LhLkhtJSRVpGoopVOd+0gjzdt/XNWeG0VU71oQfYnXY=; h=Content-Type:Message-ID:Date:MIME-Version:Subject:To:From:Cc: From; b=YP2Vh1N8M/V2VRkY5b3eiEFk8jDPaOW1NMzDMBOnvJOyLcsMVndOrd4lf6gaMXu20 SOHezv5CBqnR14i/le2BYSNofLB3lqmjExjzJ2Jc4kY5aT81vEQ5SeaxJIbaPp7aIx nqv4Lh3FdUllrJ89iK0wXm73wO/Zy5nCqmIKfyFSFa+mZrwYQoyccOacBJwgXCROTC Duh+fUFkrU7kfupzNciHNfZXquGGn55VQC6JSfqGyqGBmwLqaw6hDwBSxBOv4IXjZy QO2SELf00g8L1aopKiGVB/EtgaWanIAfzsMxSNue/cc0elbdrboBZsGL76uF10Vt4R 2is1YHghgrfTA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4TwTF46hmFz6twG; Thu, 14 Mar 2024 14:46:32 +0100 (CET) Content-Language: en-US In-Reply-To: <20240314020307.yR322B00F319MNK01R36Mg@andre.telenet-ops.be> 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, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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-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-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19462 Archived-At: On 3/14/24 02:01, Jean Abou Samra wrote: > Hi! > > The explanations are on > > https://okmij.org/ftp/Scheme/macros.html#macro-symbol-p > > Maybe this version will be easier to understand (we don't really > need continuation-passing style here): > > (define-syntax symbol?? > (syntax-rules () > ((symbol?? maybe-symbol) > (let-syntax > ((test > (syntax-rules () > ((test maybe-symbol) #t) > ((test _) #f)))) > (test abracadabra))))) > > (symbol?? foo) ⇒ #t > (symbol?? (a . b)) ⇒ #f > (symbol?? 5) ⇒ #f > (symbol?? "a") ⇒ #f > (symbol?? #(1 a)) ⇒ #f > > > Basically: the macro call > > (symbol?? ) > > expands to a macro definition of test as > > (syntax-rules () >   ((test ) #t) > ((test _) #f)) > > and a call (test abracadabra). Now, observe that if > is a symbol, then it's a catch-all pattern when inserted > in the syntax-rules definition of `test`, so it will match > abracadabra (because it matches anything). On the other hand, > if it's not a symbol, then it won't match abracadabra, by case > analysis: if it's a number it will only match that number; booleans, > strings and characters likewise; if it's a pair it can only match > pairs; if it's a vector it can only match vectors; etc. > > I'm not exactly sure why Oleg Kiselyov included special cases for a pair > and a vector, but my guess is that not all Scheme implementations > support vectors in syntax-rules patterns (and the pair check is > necessary because the car or cdr could contain a vector). The Scheme > standards certainly have their opinion on this, and I knew that > stuff by heart at some point (when I implemented a syntax-rules/syntax-case > expander for a university project), but I don't remember, and it's > too late for scouring the standards... > > Best, > Jean On 3/14/24 02:03, Maxime Devos wrote: > > (define-syntax symbol?? > >    (syntax-rules () > >      ;; The check is done by first pattern matching against some other > >      ;; forms, that are not a symbol. > >      ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol > >      ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol > > The first two cases don’t need any explanation I think. > >      ;; After those things are excluded, the thing might be a symbol. > >      ((symbol?? maybe-symbol kt kf) > >       (let-syntax ((test > >                     (syntax-rules () > >                       ((test maybe-symbol t f) t) > >                       ((test x t f) f)))) > >         (test abracadabra kt kf))))) > > If maybe-symbol is a symbol, then (test abracadabra kt kf) matches (test > maybe-symbol t f) (let t=kt, f=kf, maybe-symbol=abracadabra). > > If maybe-symbol is not a symbol, for example it is a string “hello”  (please > ignore wrong quoting), then > > (test maybe-symbol t f) becomes (test “hello” t f).  The string “hello” cannot > act as an identifier (because it is a string, not a symbol), so (test > abracadabra kt kf) does not match the first case (test maybe-symbol t f). Only > the second case (test x t f) remains, and (test abracadabra kt kf) matches > this (set x to abracadabra, t to kt, f to kf). > > I think the first two cases are superfluous, but perhaps there is a > performance advantage. > > (I’m wondering if this still works in the case (symbol? ...), because ... is > special in syntax-rules) > > Best regards, > > Maxime Devos. > Thank you both! Mind-blowing stuff. But I think I understand it now. How clever. Best regards, Zelphir -- repositories:https://notabug.org/ZelphirKaltstahl