unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* loading a module via an absolute path
@ 2002-10-04 22:41 Paul Jarc
  2002-10-06  1:51 ` Rob Browning
  0 siblings, 1 reply; 10+ messages in thread
From: Paul Jarc @ 2002-10-04 22:41 UTC (permalink / raw)


use-modules is good for avoiding name clashes.  However, I'd like to
avoid searching %load-path in some cases, and load a module (not
necessarily in the Guile sense, exactly - just a library) via a
literal path.  use-modules can also only be used at the top level,
which isn't always convenient.  I think I ought to be able to
duplicate what I like about use-modules with something like this:
(eval '(load "/path/to/foo.scm") new-environment)
But I have some questions:
- The manual says (load) will evaluate the file's contents in the
  top-level environment.  Is that still true inside eval, or will it
  load into the specified environment?
- What's the difference between load and primitive-load?
- Is there a way to load from an already-open port rather than a
  filename?  Could I just loop with read and eval?  Would that handle,
  e.g., read-hash-extend properly?  I don't see any reason it
  wouldn't.
- How do I create a new environment?  The documented procedures don't
  seem to exist:
guile> (null-environment 5)
<unnamed port>:29:1: In expression (null-environment 5):
<unnamed port>:29:1: Unbound variable: null-environment
ABORT: (unbound-variable)
guile> (scheme-report-environment 5)
<unnamed port>:30:1: In expression (scheme-report-environment 5):
<unnamed port>:30:1: Unbound variable: scheme-report-environment
ABORT: (unbound-variable)
guile> (assv-ref %guile-build-info 'guileversion)
"1.6.0"

(interaction-environment) does what it's supposed to do, but that
isn't what I need.


paul


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


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

* Re: loading a module via an absolute path
  2002-10-04 22:41 loading a module via an absolute path Paul Jarc
@ 2002-10-06  1:51 ` Rob Browning
  2002-10-07 17:09   ` Paul Jarc
                     ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Rob Browning @ 2002-10-06  1:51 UTC (permalink / raw)


prj@po.cwru.edu (Paul Jarc) writes:

> which isn't always convenient.  I think I ought to be able to
> duplicate what I like about use-modules with something like this:
> (eval '(load "/path/to/foo.scm") new-environment)

  (eval-in-module '(primitive-load "/path/to/foo.scm") some-module)

might be what you want.

> - The manual says (load) will evaluate the file's contents in the
>   top-level environment.  Is that still true inside eval, or will it
>   load into the specified environment?
> - What's the difference between load and primitive-load?

I think that's roughly the difference.  If I recall right,
primitive-load doesn't alter the current-environment and doesn't
restore it after the load, but Marius can probably clarify if I'm
mistaken.

> - Is there a way to load from an already-open port rather than a
> filename?  Could I just loop with read and eval?  Would that handle,
> e.g., read-hash-extend properly?  I don't see any reason it
> wouldn't.

I think that should work.

> - How do I create a new environment?  The documented procedures don't
>   seem to exist:

How about this:

  $ guile
  guile> (use-modules (ice-9 safe))
  guile> (define msm (make-safe-module))
  guile> msm
  #<module 8089b50>
  guile> (module-ref msm 'car)
  #<primitive-procedure car>
  guile> (eval 'car msm)
  #<primitive-procedure car>

  guile> (use-modules (ice-9 safe-r5rs))                     
  guile> (define msm (null-environment 5))                                  
  guile> msm
  #<interface 807ed30>
  guile> (eval 'if msm)
  #<primitive-macro! if>
  guile> (module-ref msm 'if)
  #<primitive-macro! if>
  guile> (eval 'unlink msm)  

  Backtrace:
  In current input:
     6: 0* [eval unlink #<interface 807ed30>]
     ?: 1* unlink

  <unnamed port>: In expression unlink:
  <unnamed port>: Unbound variable: unlink
  ABORT: (unbound-variable)
  guile> (module-define! msm 'some-num 5)                          
  #f
  guile> (module-ref msm 'some-num)
  5
  guile> (eval 'some-num msm)      
  5
  guile> some-num

  Backtrace:
  In unknown file:
     ?: 0* some-num

  <unnamed port>: In expression some-num:
  <unnamed port>: Unbound variable: some-num
  ABORT: (unbound-variable)
  guile> (module-use! (current-module) msm)
  #f
  guile> some-num                          
  5
  guile> (module-public-interface msm)
  #f
  guile> (define foo (resolve-module '(ice-9 safe)))
  guile> (module-use! (current-module) (module-public-interface foo))
  #f
  guile> (module-public-interface foo)
  #<interface (ice-9 safe) 808b200>
  guile> 

etc.

> guile> (null-environment 5)
> <unnamed port>:29:1: In expression (null-environment 5):
> <unnamed port>:29:1: Unbound variable: null-environment
> ABORT: (unbound-variable)
> guile> (scheme-report-environment 5)
> <unnamed port>:30:1: In expression (scheme-report-environment 5):
> <unnamed port>:30:1: Unbound variable: scheme-report-environment
> ABORT: (unbound-variable)
> guile> (assv-ref %guile-build-info 'guileversion)
> "1.6.0"

or perhaps just (version) :>

Hope this helps.

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG=1C58 8B2C FB5E 3F64 EA5C  64AE 78FE E5FE F0CB A0AD


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


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

* Re: loading a module via an absolute path
  2002-10-06  1:51 ` Rob Browning
@ 2002-10-07 17:09   ` Paul Jarc
  2002-10-07 20:05   ` Paul Jarc
  2002-10-18 18:21   ` Paul Jarc
  2 siblings, 0 replies; 10+ messages in thread
From: Paul Jarc @ 2002-10-07 17:09 UTC (permalink / raw)


Rob Browning <rlb@defaultvalue.org> wrote:
> prj@po.cwru.edu (Paul Jarc) writes:
>> - The manual says (load) will evaluate the file's contents in the
>>   top-level environment.  Is that still true inside eval, or will it
>>   load into the specified environment?
>> - What's the difference between load and primitive-load?
>
> I think that's roughly the difference.  If I recall right,
> primitive-load doesn't alter the current-environment and doesn't
> restore it after the load, but Marius can probably clarify if I'm
> mistaken.

If that's right, then the documentation needs to be fixed.  1.6.0:
 - Scheme Procedure: load filename
     Load FILENAME and evaluate its contents in the top-level
     environment.  The load paths are not searched.  If the variable
     `%load-hook' is defined, it should be bound to a procedure that
     will be called before any code is loaded.  See documentation for
     `%load-hook' later in this section.
...
 - Scheme Procedure: primitive-load filename
 - C Function: scm_primitive_load (filename)
     Load the file named FILENAME and evaluate its contents in the
     top-level environment. The load paths are not searched; FILENAME
     must either be a full pathname or be a pathname relative to the
     current directory.  If the  variable `%load-hook' is defined, it
     should be bound to a procedure that will be called before any code
     is loaded.  See the documentation for `%load-hook' later in this
     section.

This should also specify what load does with a relative path.  It's
easy enough to guess, but the fact that it's specified for
primitive-load and not for load introduces doubt.

>> - Is there a way to load from an already-open port rather than a
>> filename?  Could I just loop with read and eval?  Would that handle,
>> e.g., read-hash-extend properly?  I don't see any reason it
>> wouldn't.
>
> I think that should work.

Ok, I'll probably do that then.

>> - How do I create a new environment?  The documented procedures don't
>>   seem to exist:
>
> How about this:
>
>   $ guile
>   guile> (use-modules (ice-9 safe))
>   guile> (define msm (make-safe-module))
...
>   guile> (use-modules (ice-9 safe-r5rs))
>   guile> (define msm (null-environment 5))

Thanks, that seems to work.  The manual doesn't mention the need for
(ice-9 safe-r5rs) in the Environments node, and make-safe-module is
undocumented.

>> guile> (assv-ref %guile-build-info 'guileversion)
>> "1.6.0"
>
> or perhaps just (version) :>

Yeah, that too.


paul


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


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

* Re: loading a module via an absolute path
  2002-10-06  1:51 ` Rob Browning
  2002-10-07 17:09   ` Paul Jarc
@ 2002-10-07 20:05   ` Paul Jarc
  2002-10-18 18:21   ` Paul Jarc
  2 siblings, 0 replies; 10+ messages in thread
From: Paul Jarc @ 2002-10-07 20:05 UTC (permalink / raw)


Rob Browning <rlb@defaultvalue.org> wrote:
> prj@po.cwru.edu (Paul Jarc) writes:
>> which isn't always convenient.  I think I ought to be able to
>> duplicate what I like about use-modules with something like this:
>> (eval '(load "/path/to/foo.scm") new-environment)
>
>   (eval-in-module '(primitive-load "/path/to/foo.scm") some-module)

eval-in-module isn't documented; how does it differ from eval?  (It's
too bad built-in procedures don't have procedure-documentation; that's
something I miss from Python and Emacs.)

>   guile> (use-modules (ice-9 safe-r5rs))
>   guile> (define msm (null-environment 5))

This still doesn't define scheme-report-environment.  Is that in a
different module, or is the documentation ahead of the code here?


paul


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


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

* Re: loading a module via an absolute path
  2002-10-06  1:51 ` Rob Browning
  2002-10-07 17:09   ` Paul Jarc
  2002-10-07 20:05   ` Paul Jarc
@ 2002-10-18 18:21   ` Paul Jarc
  2002-10-18 22:26     ` Marius Vollmer
  2 siblings, 1 reply; 10+ messages in thread
From: Paul Jarc @ 2002-10-18 18:21 UTC (permalink / raw)


Ok, I got my modules-with-absolute-paths working, but there is
noticeable friction between it and use-modules.  Is there any chance
of extending use-modules (or providing a cooperating procedure) to
allow things like:
(use-modules ("/path/to/foo.scm" :select ...))
?

Rob Browning <rlb@defaultvalue.org> wrote:
>   guile> (use-modules (ice-9 safe))
>   guile> (define msm (make-safe-module))

The main problem I've had with this is that it's too safe.  How do I
create a module with the same bindings as the original "root" module,
after boot-9, etc., was loaded in it?  I.e., the kind use-modules
creates for the libraries it loads?  If there's a way to iterate over
the bindings in a module, I guess I could add them one by one with
module-define!.

>   guile> (module-use! (current-module) msm)
>   #f

Ah - would (module-use! msm (current-module)) do what I want?  Or
would that risk creating a harmful loop if I also import bindings the
other way?


paul


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


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

* Re: loading a module via an absolute path
  2002-10-18 18:21   ` Paul Jarc
@ 2002-10-18 22:26     ` Marius Vollmer
  2002-10-18 23:26       ` Paul Jarc
  0 siblings, 1 reply; 10+ messages in thread
From: Marius Vollmer @ 2002-10-18 22:26 UTC (permalink / raw)


prj@po.cwru.edu (Paul Jarc) writes:

> Is there any chance of extending use-modules (or providing a
> cooperating procedure) to allow things like:
>
> (use-modules ("/path/to/foo.scm" :select ...))

Could you use the this?

  (load "/path/to/foo.scm")
  (use-modules (NAME :select ...))

This will require you to know the name of the module that is defined
in foo.scm, but I find this to be actually cleaner, since files and
modules are not the same.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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


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

* Re: loading a module via an absolute path
  2002-10-18 22:26     ` Marius Vollmer
@ 2002-10-18 23:26       ` Paul Jarc
  2002-10-19 10:44         ` Marius Vollmer
  0 siblings, 1 reply; 10+ messages in thread
From: Paul Jarc @ 2002-10-18 23:26 UTC (permalink / raw)


Marius Vollmer <mvo@zagadka.ping.de> wrote:
> prj@po.cwru.edu (Paul Jarc) writes:
>> Is there any chance of extending use-modules (or providing a
>> cooperating procedure) to allow things like:
>>
>> (use-modules ("/path/to/foo.scm" :select ...))
>
> Could you use the this?
>
>   (load "/path/to/foo.scm")
>   (use-modules (NAME :select ...))

That's not quite what I'm looking for.  The way I did it, I don't use
define-module at all; the imported code doesn't know or care what name
the importing code uses to refer to it.  The loader procedure takes
care of creating a new (anonymous) module, and loads the code into it.
The importing code gets a reference to the module containing the
imported code, and binds that value to a normal variable, and uses
module-ref to fetch things it wants.  It's much like dlopen and dlsym
(not that I'm more than slightly familiar with them).

I'd be happy to keep using my loader, except that the modules it
creates (with make-safe-module) are too sparse - lacking useful things
like, say, defined?.  OTOH, a library loaded by use-modules has all
the normal bindings at its disposal.  How can I create a module like
that?


paul


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


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

* Re: loading a module via an absolute path
  2002-10-18 23:26       ` Paul Jarc
@ 2002-10-19 10:44         ` Marius Vollmer
  2002-10-19 20:42           ` Paul Jarc
  0 siblings, 1 reply; 10+ messages in thread
From: Marius Vollmer @ 2002-10-19 10:44 UTC (permalink / raw)


prj@po.cwru.edu (Paul Jarc) writes:

> That's not quite what I'm looking for.  The way I did it, I don't use
> define-module at all; the imported code doesn't know or care what name
> the importing code uses to refer to it.  The loader procedure takes
> care of creating a new (anonymous) module, and loads the code into it.

I see.  You shouldn't use 'use-modules' for it, then.  It is better to
give this new concept a new set of functions to work with it.

> I'd be happy to keep using my loader, except that the modules it
> creates (with make-safe-module) are too sparse - lacking useful
> things like, say, defined?.  OTOH, a library loaded by use-modules
> has all the normal bindings at its disposal.  How can I create a
> module like that?

You can use

  (make-module 1021 (list (resolve-interface '(guile))))

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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


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

* Re: loading a module via an absolute path
  2002-10-19 10:44         ` Marius Vollmer
@ 2002-10-19 20:42           ` Paul Jarc
  2002-10-20 16:19             ` Marius Vollmer
  0 siblings, 1 reply; 10+ messages in thread
From: Paul Jarc @ 2002-10-19 20:42 UTC (permalink / raw)


Marius Vollmer <mvo@zagadka.ping.de> wrote:
>   (make-module 1021 (list (resolve-interface '(guile))))

This seems to be exactly what I'm looking for, thanks.  And it seems
to me that such a module includes the 'app binding, so invocations of
use-modules from the main module and the new module will not duplicate
each others' work, right?  Good.

One last (heh) question, then: after I've loaded the file into a new
module like this, is there a way to mark the module as "read-only" so
that the importing code can't change bindings inside this module?  One
of my goals is to protect modules from each other.  It looks like it's
easy enough to avoid messing with someone else's bindings by accident,
but I'd like to make it impossible to do it even deliberately, if I
can.


paul


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


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

* Re: loading a module via an absolute path
  2002-10-19 20:42           ` Paul Jarc
@ 2002-10-20 16:19             ` Marius Vollmer
  0 siblings, 0 replies; 10+ messages in thread
From: Marius Vollmer @ 2002-10-20 16:19 UTC (permalink / raw)


prj@po.cwru.edu (Paul Jarc) writes:

> One last (heh) question, then: after I've loaded the file into a new
> module like this, is there a way to mark the module as "read-only" so
> that the importing code can't change bindings inside this module?

No, Guile currenly does not have read-only variables.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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


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

end of thread, other threads:[~2002-10-20 16:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-04 22:41 loading a module via an absolute path Paul Jarc
2002-10-06  1:51 ` Rob Browning
2002-10-07 17:09   ` Paul Jarc
2002-10-07 20:05   ` Paul Jarc
2002-10-18 18:21   ` Paul Jarc
2002-10-18 22:26     ` Marius Vollmer
2002-10-18 23:26       ` Paul Jarc
2002-10-19 10:44         ` Marius Vollmer
2002-10-19 20:42           ` Paul Jarc
2002-10-20 16:19             ` Marius Vollmer

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