unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Guile Introspection
@ 2007-07-07 15:40 Mike Gran
  2007-07-08  0:18 ` Issac Trotts
  2007-07-08 15:18 ` Ludovic Courtès
  0 siblings, 2 replies; 8+ messages in thread
From: Mike Gran @ 2007-07-07 15:40 UTC (permalink / raw)
  To: Gule User

Hi-

Is it possible to get introspection information out of Guile
(preferably without having to load GOOPS)?

For example, how can I write a function that prints its own name?

Or, what is the scheme version of the following C code

printf("%s %d\n", __FILE__, __LINE__);

Thanks in advance,

Mike Gran


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
  2007-07-07 15:40 Mike Gran
@ 2007-07-08  0:18 ` Issac Trotts
  2007-07-08 15:18 ` Ludovic Courtès
  1 sibling, 0 replies; 8+ messages in thread
From: Issac Trotts @ 2007-07-08  0:18 UTC (permalink / raw)
  To: Mike Gran; +Cc: Guile User


[-- Attachment #1.1: Type: text/plain, Size: 782 bytes --]

On 7/7/07, Mike Gran <spk121@yahoo.com> wrote:
>
> Hi-
>
> Is it possible to get introspection information out of Guile
> (preferably without having to load GOOPS)?
>
> For example, how can I write a function that prints its own name?


You can define a macro to make this happen:

(defmacro define-named (name-and-args . body)
  (let ((name (car name-and-args))
        (args (cdr name-and-args)))
    `(define (,name ,@args)
      (let ((--function-- ',name))
        ,@body))))

Here is an example of how to use it, analogous to using __FUNCTION__ in gcc:

(define-named (some-fun)
  (display --function--)
  (newline))

Or, what is the scheme version of the following C code
>
> printf("%s %d\n", __FILE__, __LINE__);


I don't know if there's a way to do this in Guile.

Issac

[-- Attachment #1.2: Type: text/html, Size: 1425 bytes --]

[-- Attachment #2: Type: text/plain, Size: 140 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user

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

* Re: Guile Introspection
  2007-07-07 15:40 Mike Gran
  2007-07-08  0:18 ` Issac Trotts
@ 2007-07-08 15:18 ` Ludovic Courtès
  2007-07-09 10:57   ` Andy Wingo
  1 sibling, 1 reply; 8+ messages in thread
From: Ludovic Courtès @ 2007-07-08 15:18 UTC (permalink / raw)
  To: guile-user

Hi,

Mike Gran <spk121@yahoo.com> writes:

> For example, how can I write a function that prints its own name?

In Scheme, functions are first-class objects that are not necessarily
bound to a top-level name.  For instance, a `lambda' is nameless:

  (lambda args
    ...)

Thus, there is no generic, portable way to find the symbol under which a
procedure is bound (_if_ it's bound).  But...

> Or, what is the scheme version of the following C code

In Guile, "primitive procedures" (i.e., procedures written in C) have
their "official" name recorded in them.  For instance:

  guile> 1+
  #<primitive-procedure 1+>
  guile> (procedure-name 1+)
  1+

Happily, it also works with regular procedures defined with `define':

  guile> (define (f x) x)  
  guile> (procedure-name f)
  f

... but doesn't work with lambdas:

  guile> (procedure-name (lambda args args))
  #f

As for the file name and line number, you can in theory get them
(provided Guile runs in "debug" mode) using `procedure-source' and
`source-properties', although the details escape me now.

Thanks,
Ludovic.



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
@ 2007-07-09  6:28 Marco Maggi
  0 siblings, 0 replies; 8+ messages in thread
From: Marco Maggi @ 2007-07-09  6:28 UTC (permalink / raw)
  To: guile-user

Ciao,

"Mike Gran" wrote:
>Or, what is the scheme version of the following C code
>
>printf("%s %d\n", __FILE__, __LINE__);

I like a solution that redefines DEFINE without introducing
an environment in the body of the function. To do it: you
have to take the body in question and treat it as a tree,
mapping selected symbols to the values you want.

If I am correct in assuming that you are a Scheme beginner
and the following code is no clear enough, just ask.

;; ----------------------------------------

(define (map-tree func tree)
  (map (lambda (v)
	 (if (list? v)
	     (map-tree func v)
	   (func v))) tree))

(define (subst-tree old new tree)
  (map-tree (lambda (v)
	      (if (eq? old v) new v)) tree))

(define saved-define define)

;; ----------------------------------------

(define *current-file-name* "proof.scm")

(define-macro (define name-and-args . body)
  (let* ((fname	(symbol->string (car name-and-args)))
	 (args	(cdr name-and-args))
	 (body1 (subst-tree
		 '*current-file-name* *current-file-name*
		 (subst-tree '*current-function-name* fname body))))
    `(saved-define ,name-and-args ,@body1)))

;; ----------------------------------------
;; tests

(define (my-func)
  (format #t "file: ~A, func: ~A~%"
	  *current-file-name*
	  *current-function-name*))

(define (other-func alpha beta)
  (format #t "file: ~A, func: ~A, a ~A, b ~A~%"
	  *current-file-name*
	  *current-function-name*
	  alpha beta))

(define (further-func . args)
  (apply format #t "file: ~A, func: ~A, a ~A, b ~A~%"
	 *current-file-name*
	 *current-function-name*
	 args))

(my-func)
(other-func 123 'abc)
(further-func 123 'abc)

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
  2007-07-08 15:18 ` Ludovic Courtès
@ 2007-07-09 10:57   ` Andy Wingo
  2007-07-09 12:35     ` Ludovic Courtès
  2007-07-12 13:09     ` Thien-Thi Nguyen
  0 siblings, 2 replies; 8+ messages in thread
From: Andy Wingo @ 2007-07-09 10:57 UTC (permalink / raw)
  To: guile-user

Hi,

On Sun, 2007-07-08 at 17:18 +0200, Ludovic Courtès wrote:
> As for the file name and line number, you can in theory get them
> (provided Guile runs in "debug" mode) using `procedure-source' and
> `source-properties', although the details escape me now.

I actually don't think this is true; procedure-source returns a copy,
and the source-properties weak hash is an eq? hash. AFAIK.

(I would really like to be able to get line numbers so that the
documentation that I autogenerate based on introspection can be ordered
as it appears in the source. I poked at this a couple hours and failed.)

Cheers,

Andy.
-- 
http://wingolog.org/


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
  2007-07-09 10:57   ` Andy Wingo
@ 2007-07-09 12:35     ` Ludovic Courtès
  2007-07-12 13:09     ` Thien-Thi Nguyen
  1 sibling, 0 replies; 8+ messages in thread
From: Ludovic Courtès @ 2007-07-09 12:35 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

Hi Andy,

Andy Wingo <wingo@pobox.com> writes:

> On Sun, 2007-07-08 at 17:18 +0200, Ludovic Courtès wrote:
>> As for the file name and line number, you can in theory get them
>> (provided Guile runs in "debug" mode) using `procedure-source' and
>> `source-properties', although the details escape me now.
>
> I actually don't think this is true; procedure-source returns a copy,
> and the source-properties weak hash is an eq? hash. AFAIK.

Yeah, that's exactly what I meant by "the details escape me now".  ;-)

`display-backtrace', for instance, knows how to do this, but it gets the
source from the frame (rather than from the procedure).

Apparently, we can't do this with `procedure-source' because it does
many things (including unmemoizing and returning a fresh expression if
needed) that remove source properties.  We might be able to change this
using some Smart Tricks that escape me currently.  ;-)

Thanks,
Ludovic.


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
@ 2007-07-10 12:51 Marco Maggi
  0 siblings, 0 replies; 8+ messages in thread
From: Marco Maggi @ 2007-07-10 12:51 UTC (permalink / raw)
  To: guile-user

"Mike Gran" wrote:
>what is the scheme version of the following C code
>
>printf("%s %d\n", __FILE__, __LINE__);

A better solution[1] than the other I posted. Still far
from perfect.

[1]
<http://community.schemewiki.org/?guile-function-and-file-name>

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

* Re: Guile Introspection
  2007-07-09 10:57   ` Andy Wingo
  2007-07-09 12:35     ` Ludovic Courtès
@ 2007-07-12 13:09     ` Thien-Thi Nguyen
  1 sibling, 0 replies; 8+ messages in thread
From: Thien-Thi Nguyen @ 2007-07-12 13:09 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

() Andy Wingo <wingo@pobox.com>
() Mon, 09 Jul 2007 12:57:41 +0200

   (I would really like to be able to get line numbers
   so that the documentation that I autogenerate based
   on introspection can be ordered as it appears in the
   source. I poked at this a couple hours and failed.)

below is some code you can try.  to play: save as x.scm
and issue the shell command: guile -s x.scm.

thi

________________________________________________________
(define (lc alist)
  (list (assq-ref alist 'line)
        (assq-ref alist 'column)))

(define (posn form)
  (display "posn: ")
  (write (cond ((not (pair? form)) "not a pair")
               ((source-properties form) => lc)
               (else #f)))
  (newline))

(define p (open-input-file "x.scm"))

(define (r)
  (read-enable 'positions)
  (let ((form (read p)))
    (or (eof-object? form)
        (begin
          (display "form: ")
          (write form)
          (newline)
          (posn form)
          (r)))))

42

(r)


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

end of thread, other threads:[~2007-07-12 13:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-10 12:51 Guile Introspection Marco Maggi
  -- strict thread matches above, loose matches on Subject: below --
2007-07-09  6:28 Marco Maggi
2007-07-07 15:40 Mike Gran
2007-07-08  0:18 ` Issac Trotts
2007-07-08 15:18 ` Ludovic Courtès
2007-07-09 10:57   ` Andy Wingo
2007-07-09 12:35     ` Ludovic Courtès
2007-07-12 13:09     ` Thien-Thi Nguyen

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