Re: Guile Introspection

2007-07-08 Thread Ludovic Courtès
Hi,

Mike Gran <[EMAIL PROTECTED]> 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+
  #
  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


multi_module

2007-07-08 Thread Frank J. R. Hanstick

Hello,
How do I change the default multi_module option to single_module?
Frank



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


Re: Guile Introspection

2007-07-08 Thread Marco Maggi
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