unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Module reflection and the list of bound symbols
@ 2012-09-28 17:37 Panicz Maciej Godek
  2012-09-29  5:28 ` Thien-Thi Nguyen
  2012-09-30 13:21 ` Ian Price
  0 siblings, 2 replies; 4+ messages in thread
From: Panicz Maciej Godek @ 2012-09-28 17:37 UTC (permalink / raw)
  To: guile-user

Hello,
In guile there seem to be many ways of querying whether
a certain symbol is bound in a given module/environment,
like `defined?', `module-symbol-binding', `module-variable'
etc.

On the other hand, there is no way (or I haven't found one)
to list the bound symbols explicitly. It seems tempting to
have a reflection function `module-symbols' or `environment-symbols'
that would return the names of bound symbols,
or to have functions like environment->list or module->list that
would return assoc lists mapping symbols to their values.

I've been wondering whether the lack of such functions
is deliberate, or if there's a chance that they could be added
some time in the future.
(Or maybe I just overlooked something, and they are already
available?)

Yours,
Maciek



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

* Re: Module reflection and the list of bound symbols
  2012-09-28 17:37 Module reflection and the list of bound symbols Panicz Maciej Godek
@ 2012-09-29  5:28 ` Thien-Thi Nguyen
  2012-09-30 13:21 ` Ian Price
  1 sibling, 0 replies; 4+ messages in thread
From: Thien-Thi Nguyen @ 2012-09-29  5:28 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: guile-user

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

() Panicz Maciej Godek <godek.maciek@gmail.com>
() Fri, 28 Sep 2012 19:37:17 +0200

   (Or maybe I just overlooked something, and they are already
   available?)

Check out ‘(ice-9 session) apropos’ to see what it does.  In Guile 1.4,
one of its subroutines:

(define apropos-fold-exported
  (make-fold-modules root-modules submodules
                     (lambda (fob)
                       (fob-info fob #:public-interface))))

uses ‘fob-info ... #:public-interface’, which was the result of long
past (but still memorably pleasant) "separation of concerns" hacking
(see <http://www.gnuvola.org/software/guile/doc/Module-Monkey.html>).
Anyway, the important bit is that "public-interface" derives from the
procedure ‘module-public-interface’, present in other Guile versions.

Probably you want to explore the ancestor of ‘fob-info ... #:obarray’,
instead, to access the full set of bindings (not just public), but i
didn't mention that because the code that uses it in Guile 1.4 ‘(ice-9
session)’ is long and the Guile module system is somewhat of a rats nest
[insert maniacal laughter, here :-D]...  Same thinking applies, however.

-- 
Thien-Thi Nguyen ..................................... GPG key: 4C807502
.                  NB: ttn at glug dot org is not me                   .
.                 (and has not been since 2007 or so)                  .
.                        ACCEPT NO SUBSTITUTES                         .
........... please send technical questions to mailing lists ...........


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

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

* Re: Module reflection and the list of bound symbols
  2012-09-28 17:37 Module reflection and the list of bound symbols Panicz Maciej Godek
  2012-09-29  5:28 ` Thien-Thi Nguyen
@ 2012-09-30 13:21 ` Ian Price
  2012-09-30 16:21   ` David Pirotte
  1 sibling, 1 reply; 4+ messages in thread
From: Ian Price @ 2012-09-30 13:21 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: guile-user

Panicz Maciej Godek <godek.maciek@gmail.com> writes:

> On the other hand, there is no way (or I haven't found one)
> to list the bound symbols explicitly. It seems tempting to
> have a reflection function `module-symbols' or `environment-symbols'
> that would return the names of bound symbols,
> or to have functions like environment->list or module->list that
> would return assoc lists mapping symbols to their values.
Hmm, no, but you are able to get the "obarray" for the module which is a
hash of symbols -> variables.

(define (hash-keys hash)
  (hash-fold (lambda (key val prev)
               (cons key prev))
             '()
             hash))

(define (module-exports module)
  (hash-keys (module-obarray module)))

scheme@(guile−user)> (module-exports (resolve-module '(pfds queues)))
$20 = (queue−>list queue−empty−condition? enqueue &queue−empty
queue−length make−queue rotate queue−l∧ dummy queue−l
make−queue−empty−condition %module−public−interface list−>queue
dequeue queue−empty? queue? queue %make−queue queue−r makeq)

This lists _all_ the bindings, even the non-public ones.

I'm sure there is a way to get only the public (exported) ones using the
interfaces, but that is beyond my knowledge at the moment.

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



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

* Re: Module reflection and the list of bound symbols
  2012-09-30 13:21 ` Ian Price
@ 2012-09-30 16:21   ` David Pirotte
  0 siblings, 0 replies; 4+ messages in thread
From: David Pirotte @ 2012-09-30 16:21 UTC (permalink / raw)
  To: Ian Price; +Cc: guile-user

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

Hello,

> ... to get only the public (exported) ones using the
> interfaces, but that is beyond my knowledge at the moment.

Here is what i do, see the attached code [which i copied from
guile-gnome], then you can use it this way, as an example:

1. you define a module b and export some ...

2.

   ;;; file a.scm STARTS here
   (define-module (a)
     :use-module (reexport)  
     :use-module (b))
     :export ... ...

   (eval-when (compile load eval)
     (re-export-public-interface (b)))

   ;; your code here...
   ;; ...
   ;;; file a.scm ENDS here

3.

   finally:

   guile
   scheme@(guile-user)> (use-modules (a))
   ;; then you can use both public interface from a and b
      

Of course this is true for any module which uses the module a [it can
refers to the public interface of b too...]

Hope this helps,
Cheers,
David

ps:	i have always tought guile itself should provide this feature as built-in

[-- Attachment #2: reexport.scm --]
[-- Type: text/x-scheme, Size: 1417 bytes --]

;; -*- mode: scheme; coding: utf-8 -*-

;;;; Copyright (C) 2011, 2012
;;;; Free Software Foundation, Inc.

;;;; This library is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License as
;;;; published by the Free Software Foundation, either version 3 of
;;;; the License, or (at your option) any later version.

;;;; This library is distributed in the hope that it will be useful, but
;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; General Public License for more details.

;;;; You should have received a copy of the GNU General Public License
;;;; along with this library.  If not, see <http://www.gnu.org/licenses/>.
;;;;

;;; Commentary:

;; this code is the same as the one use by guile-gnome, see
;; /usr/local/share/guile-gnome-2/gnome/gw/support/modules.scm

;;; Code:

(define-module (macros reexport)
  :export (re-export-public-interface))


(define-macro (re-export-public-interface . args)
  "Re-export the public interface of a module or modules. Invoked as
@code{(re-export-modules (mod1) (mod2)...)}."
  (if (null? args)
      '(if #f #f)
      `(begin
	 ,@(map (lambda (mod)
		  (or (list? mod)
		      (error "Invalid module specification" mod))
		  `(module-use! (module-public-interface (current-module))
				(resolve-interface ',mod)))
		args))))

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

end of thread, other threads:[~2012-09-30 16:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-28 17:37 Module reflection and the list of bound symbols Panicz Maciej Godek
2012-09-29  5:28 ` Thien-Thi Nguyen
2012-09-30 13:21 ` Ian Price
2012-09-30 16:21   ` David Pirotte

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