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

* Re: Applicable structs and vtables
  2021-08-13 20:33 Applicable structs and vtables Zelphir Kaltstahl
@ 2021-08-14  6:53 ` tomas
  2021-08-14  9:19   ` Zelphir Kaltstahl
  0 siblings, 1 reply; 4+ messages in thread
From: tomas @ 2021-08-14  6:53 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1007 bytes --]

On Fri, Aug 13, 2021 at 08:33:33PM +0000, Zelphir Kaltstahl wrote:
> 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)>

I don't know whether what you're after is about generalising what a
function can be ("callable objects" or similar), or if procedure
arity introspection at run time is your ultimate goal.

In the second case, there is `procedure-minimum-arity', which might
already cater to your wishes :-)

  (procedure-minimum-arity cons) ; takes two arguments, no "rest"
  => (2 0 #f)
  (procedure-minimum-arity +) ; takes at least 0, takes "rest"
  => (0 2 #t)
  (let ((foo (lambda (x y . z) ('some-result)))) ; DIY
    (procedure-minimum-arity foo))               ; at least 2, with "rest"
  => (2 0 #t)

I don't know (yet) what the middle number is for, though ;-)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: Applicable structs and vtables
  2021-08-14  6:53 ` tomas
@ 2021-08-14  9:19   ` Zelphir Kaltstahl
  2021-08-14 10:40     ` tomas
  0 siblings, 1 reply; 4+ messages in thread
From: Zelphir Kaltstahl @ 2021-08-14  9:19 UTC (permalink / raw)
  To: tomas; +Cc: Guile User

Hi Tomas!

Ultimately I need the minimum and maximum arity of functions (not sure if
anything in between is needed, but I think not). Minimum and knowing, that rest
arguments exist is not sufficient. The exact maximum number of arguments is what
I require, even if it turns out to be infinity or some implementation limit.

Trying to give an example:

It is a learning project, about function combinators and there are combinators,
which could for example return functions, which split their arguments into 2
groups of arguments, 1 group for each combined function. Lets say the combined
function h takes k, k+1 or k+2 arguments. Out of k arguments the first function
(f) of the combined functions could take n arguments leaving k-n arguments for
the second function (g). However, if k+1 arguments were provided to h or k+2
arguments, then then the second function (g) could take k-n+1 or k-n+2
arguments, simply because more arguments are remaining for the second function.

To implement checks for correct number of arguments at runtime (assertions), I
need, I think, the exact maximum number of arguments and not only the
information, that there can be rest arguments.

The reference manual has more about "program arities"
(https://www.gnu.org/software/guile/manual/guile.html#Compiled-Procedures
<https://www.gnu.org/software/guile/manual/guile.html#Compiled-Procedures>), but
that API is not working currently, as found out in a previous thread on this
mailing list:
https://lists.gnu.org/archive/html/guile-user/2021-05/msg00044.html
<https://lists.gnu.org/archive/html/guile-user/2021-05/msg00044.html>. I think
only the one function you mention is currently working.

While that API is not properly working, I think "callable objects" could be a
solution to my problem.

Thanks for your reply!
Zelphir

On 8/14/21 8:53 AM, tomas@tuxteam.de wrote:
> On Fri, Aug 13, 2021 at 08:33:33PM +0000, Zelphir Kaltstahl wrote:
>> 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)>
> I don't know whether what you're after is about generalising what a
> function can be ("callable objects" or similar), or if procedure
> arity introspection at run time is your ultimate goal.
>
> In the second case, there is `procedure-minimum-arity', which might
> already cater to your wishes :-)
>
>   (procedure-minimum-arity cons) ; takes two arguments, no "rest"
>   => (2 0 #f)
>   (procedure-minimum-arity +) ; takes at least 0, takes "rest"
>   => (0 2 #t)
>   (let ((foo (lambda (x y . z) ('some-result)))) ; DIY
>     (procedure-minimum-arity foo))               ; at least 2, with "rest"
>   => (2 0 #t)
>
> I don't know (yet) what the middle number is for, though ;-)
>
> Cheers
>  - t

-- 
repositories: https://notabug.org/ZelphirKaltstahl




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

* Re: Applicable structs and vtables
  2021-08-14  9:19   ` Zelphir Kaltstahl
@ 2021-08-14 10:40     ` tomas
  0 siblings, 0 replies; 4+ messages in thread
From: tomas @ 2021-08-14 10:40 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Guile User

[-- Attachment #1: Type: text/plain, Size: 403 bytes --]

On Sat, Aug 14, 2021 at 09:19:49AM +0000, Zelphir Kaltstahl wrote:
> Hi Tomas!
> 
> Ultimately I need the minimum and maximum arity of functions [...]

It seems that the second number in `procedure-minimum-arity's result
is the number of optional arguments (cf. `define*'). Since Guile 2.0,
optional args are in the core.

So that still might fit your bill. Or perhaps not ;-)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ 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 NNTP newsgroup(s).