unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* interrogation about introspecting function bindings
@ 2011-12-21 12:26 rixed
  2012-01-10 21:15 ` Andy Wingo
  0 siblings, 1 reply; 4+ messages in thread
From: rixed @ 2011-12-21 12:26 UTC (permalink / raw)
  To: guile-user

I'm trying to get a list of parameter names from a function, using this:

(use-modules (system vm program))
(define (val->string v)
  (if (string? v) v (object->string v)))
(define (fun-params fun)
  (let ((bindings (program-bindings fun)))
    (map (lambda (binding) (val->string (binding:name binding))) bindings)))

All seams well:

(define (f foo) 'bar)
(fun-params f)
-> ("foo")

But sometime I've got internal binding as well as parameter in the
bindings list. So I use (program-arities) to find out how many
parameters I actually have, and take only the firsts bindings :

(define (pop l n)
  (cond
    ((= 0 n)   '())
    ((null? l) '())
    (else      (cons (car l)
                     (pop (cdr l) (- n 1))))))
(define (fun-params fun)
  (let ((bindings (program-bindings fun))
        (arity    (arity:nreq (car (program-arities fun)))))
    (map (lambda (binding)
           (val->string (binding:name binding)))
         (pop bindings arity))))

This seams to work, but is it guaranteed that the first b bindings will
be the parameters ?




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

* Re: interrogation about introspecting function bindings
  2011-12-21 12:26 interrogation about introspecting function bindings rixed
@ 2012-01-10 21:15 ` Andy Wingo
  2012-01-11 16:13   ` rixed
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Wingo @ 2012-01-10 21:15 UTC (permalink / raw)
  To: rixed; +Cc: guile-user

Hi Cedric,

On Wed 21 Dec 2011 13:26, rixed@happyleptic.org writes:

> I'm trying to get a list of parameter names from a function, using
> this:
>
> (use-modules (system vm program))
> (define (val->string v)
>   (if (string? v) v (object->string v)))
> (define (fun-params fun)
>   (let ((bindings (program-bindings fun)))
>     (map (lambda (binding) (val->string (binding:name binding))) bindings)))

Why not use program-arguments-alist, or program-lambda-list?

> is it guaranteed that the first b bindings will be the parameters ?

No, because there are complications with case-lambda, keyword, rest, and
optional arguments.

Cheers,

Andy
-- 
http://wingolog.org/



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

* Re: interrogation about introspecting function bindings
  2012-01-10 21:15 ` Andy Wingo
@ 2012-01-11 16:13   ` rixed
  2013-01-07 21:04     ` Andy Wingo
  0 siblings, 1 reply; 4+ messages in thread
From: rixed @ 2012-01-11 16:13 UTC (permalink / raw)
  To: guile-user

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

-[ Tue, Jan 10, 2012 at 10:15:54PM +0100, Andy Wingo ]----
> Why not use program-arguments-alist, or program-lambda-list?

Because they were not in the procedure index, hence the attached patch.


[-- Attachment #2: doc-for-program-arguments-alist --]
[-- Type: text/plain, Size: 2002 bytes --]

From 6637ce41e5e8cbfefe4c14c47ac79c7a3f5a6cfe Mon Sep 17 00:00:00 2001
From: Cedric Cellier <cedric.cellier@securactive.net>
Date: Wed, 11 Jan 2012 17:12:48 +0100
Subject: [PATCH] document program-arguments-alist and program-lambda-list

---
 doc/ref/api-procedures.texi  |    9 +++++++++
 module/system/vm/program.scm |    2 ++
 2 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/doc/ref/api-procedures.texi b/doc/ref/api-procedures.texi
index 2b4a05e..5875ced 100644
--- a/doc/ref/api-procedures.texi
+++ b/doc/ref/api-procedures.texi
@@ -270,6 +270,15 @@ sense at certain points in the program, delimited by these
 @code{arity:start} and @code{arity:end} values.
 @end deffn
 
+@deffn {Scheme Procedure} program-arguments-alist program [ip]
+@deffnx {Scheme Procedure} program-lambda-list [ip]
+Accessors for a representation of the arguments of a program, with both
+names and types (ie. either required, optional or keywords)
+
+@code{program-arguments-alist} returns this information in the form of
+an association list while @code{program-lambda-list} returns the same
+information in a form similar to a lambda definition.
+@end deffn
 
 @node Optional Arguments
 @subsection Optional Arguments
diff --git a/module/system/vm/program.scm b/module/system/vm/program.scm
index 02d5ec4..d4de335 100644
--- a/module/system/vm/program.scm
+++ b/module/system/vm/program.scm
@@ -238,11 +238,13 @@
 
 ;; the name "program-arguments" is taken by features.c...
 (define* (program-arguments-alist prog #:optional ip)
+  "Returns the signature of the given procedure in the form of an association list."
   (let ((arity (program-arity prog ip)))
     (and arity
          (arity->arguments-alist prog arity))))
 
 (define* (program-lambda-list prog #:optional ip)
+  "Returns the signature of the given procedure in the form of an argument list."
   (and=> (program-arguments-alist prog ip) arguments-alist->lambda-list))
 
 (define (arguments-alist->lambda-list arguments-alist)
-- 
1.7.2.5


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

* Re: interrogation about introspecting function bindings
  2012-01-11 16:13   ` rixed
@ 2013-01-07 21:04     ` Andy Wingo
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Wingo @ 2013-01-07 21:04 UTC (permalink / raw)
  To: rixed; +Cc: guile-user

On Wed 11 Jan 2012 17:13, rixed@happyleptic.org writes:

> -[ Tue, Jan 10, 2012 at 10:15:54PM +0100, Andy Wingo ]----
>> Why not use program-arguments-alist, or program-lambda-list?
>
> Because they were not in the procedure index, hence the attached patch.

Finally committed :)  Cheers!

Andy
-- 
http://wingolog.org/



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

end of thread, other threads:[~2013-01-07 21:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-21 12:26 interrogation about introspecting function bindings rixed
2012-01-10 21:15 ` Andy Wingo
2012-01-11 16:13   ` rixed
2013-01-07 21:04     ` Andy Wingo

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).