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: Applicable structs and vtables Date: Fri, 13 Aug 2021 20:33:33 +0000 Message-ID: <598960ee-3baf-dca7-ed88-e128d5eb2fe9@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13672"; 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 Aug 13 22:34:00 2021 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 1mEdsO-0003M1-2c for guile-user@m.gmane-mx.org; Fri, 13 Aug 2021 22:34:00 +0200 Original-Received: from localhost ([::1]:33740 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mEdsM-0007QR-54 for guile-user@m.gmane-mx.org; Fri, 13 Aug 2021 16:33:58 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60114) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mEds7-0007Q8-QT for guile-user@gnu.org; Fri, 13 Aug 2021 16:33:44 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:49001) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mEds4-0008Ub-UT for guile-user@gnu.org; Fri, 13 Aug 2021 16:33:43 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id B8668240101 for ; Fri, 13 Aug 2021 22:33:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628886814; bh=txjDAONtXjTKV9PMW4zuMuHBiE5ydeV2K1JwQKzR7xk=; h=To:From:Subject:Date:From; b=DpWksuzmSefku5fsuUB/PGsOe7QLXCGP6dY6h3VS48VeKnk2/ID1AAJZuxLSmqfsW Sy9fuqBAUBMYlMgOBWbnLjwQLx2I1iqdAzLBDGrO3TLgU8o5DXU1ejpwyyryfZJnHM Z/d4qJoCHKz/kk2Pcc7KaQ0iFQNDVUdJTCYD8Hro1x1Nu0ICGqsZnC9bYaUbX7d/Yv ExHJbYkIBcfIYzqHFdPY/6YKtLr5yyJYgAHbuo6Yt6l0jMOck3DINEetD+r80kag2B hF3uso+G1v7r0y20/KIhflXVGiIF0I5XdpSNsOJ4kPddGVpV8cVKhyfklIDUinwipQ pK/6Z61hTMkjQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GmZyQ22NGz6tmM for ; Fri, 13 Aug 2021 22:33:34 +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 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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:17682 Archived-At: Hi Guile users, I have some code, in which I wrap procedures in a record, which contains members, which express the arity of the procedures. For example one struct be: Something like that. A while ago someone mentioned applicable structs and now I am curious, whether perhaps Guile itself already provides something, that I can use to store arity information in, while having something that is simply applicable by being the first thing in an S-expression, so that it all looks and works like normal procedure application. The downside to my current approach of defining a normal record is, that I cannot use (apply ...) on it. I have to use a special custom (arity-function-apply ...) to access and actually apply the function stored in the record. And this is infectious. All code, which wants to make use of any , needs to use (arity-function-apply ...) or whatever its name is, unless I access the record's member directly, which would also be bad, because of overstepping abstraction barriers. I could go an alternative way of storing information about the functions in some global state, some hash-table probably, but I actually wanted to avoid using global state. The book I am reading does it that way already, but I would like to try to get the same functionality without using global state. (My code is here: https://notabug.org/ZelphirKaltstahl/function-combinators/src/master/notes.org ) The upside of being able to use applicable structs, I imagine, would be, that I could store arity information in them and and then use them like normal procedures, using (apply …) or simply (my-arity-func-here …). So I looked it up and found: 1. my bookmark of the previously mentioned code, which makes apparently use of applicable structs: https://github.com/lloda/guile-newra/blob/ee633920bd779463e43510ed07db17ce296eb85d/mod/newra/base.scm#L179 , which led me to 2. https://www.gnu.org/software/guile/manual/html_node/Structure-Basics.html , which led me to 3. https://www.gnu.org/software/guile/manual/html_node/Vtable-Contents.html But I have no idea what the pwpwpw stuff is all about or what p and w stand for or why they are important. I have a hunch, that they somehow give information about how to arrange stuff in memory, so that the compiler knows what size the struct is, or something similar. Perhaps I am completely wrong though. There is also the question, why such a struct is applicable. What makes it special? Is my idea of using such a struct actually a valid idea? The docs say: > A vtable is itself a structure. It has a specific set of fields describing various aspects of its instances: the structures created from a vtable. Some of the fields are internal to Guile, some of them are part of the public interface, and there may be additional fields added on by the user. "additional fields added on by the user" sounds exactly like what I am looking for. I also found: http://gnu-guile.7481.n7.nabble.com/Does-anyone-have-an-applicable-struct-example-td13094.html , which has an example: ~~~~ scheme@(guile-user)> (define (make-struct 0 'pw)) scheme@(guile-user)> (make-struct 0) $1 = # scheme@(guile-user)> (make-struct 0 (lambda () 10)) $2 = #:4:32 ()>> scheme@(guile-user)> ($2) $3 = 10 ~~~~ Looks like it really produces something applicable and I could stuff my actual function in that struct and somehow add additional information about arity. If this is a valid approach for having functions which have information about their arity, then the next question is: What other downsides, if any, are there, if I use these applicable structs? Best regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl