unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Applicable structs and vtables
@ 2021-08-13 20:33 Zelphir Kaltstahl
  2021-08-14  6:53 ` tomas
  0 siblings, 1 reply; 4+ messages in thread
From: Zelphir Kaltstahl @ 2021-08-13 20:33 UTC (permalink / raw)
  To: Guile User

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:

<arity-function: min: 1, max: 3, function: (some lambda here)>

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
<arity-function>, 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
<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
<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
<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
<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
<http://gnu-guile.7481.n7.nabble.com/Does-anyone-have-an-applicable-struct-example-td13094.html>,
which has an example:

~~~~
scheme@(guile-user)> (define <mything-vtable> (make-struct <applicable-struct-vtable> 0 'pw))
scheme@(guile-user)> (make-struct <mything-vtable> 0)
$1 = #<struct:23ce1e0 pw 24a8ec0>
scheme@(guile-user)> (make-struct <mything-vtable> 0 (lambda () 10))
$2 = #<struct:23ce1e0 pw 24b7920 proc: #<procedure 24b7940 at <current input>: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




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-14 10:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 20:33 Applicable structs and vtables Zelphir Kaltstahl
2021-08-14  6:53 ` tomas
2021-08-14  9:19   ` Zelphir Kaltstahl
2021-08-14 10:40     ` tomas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).