unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* intern a top level variable
@ 2023-08-16 12:41 Mortimer Cladwell
  2023-08-16 14:08 ` tomas
  0 siblings, 1 reply; 9+ messages in thread
From: Mortimer Cladwell @ 2023-08-16 12:41 UTC (permalink / raw)
  To: guile-user

Hi,
How do I intern a top level variable? looks like intern-symbol is
deprecated? Is there a substitute? gensym does not seem to be it:

scheme@(guile-user)> (gensym "abc")
$11 = abc2652
scheme@(guile-user)> abc2652
;;; <unknown-location>: warning: possibly unbound variable `abc2652'
ERROR: In procedure module-lookup: Unbound variable: abc2652

Thanks
Mortimer


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

* Re: intern a top level variable
  2023-08-16 12:41 Mortimer Cladwell
@ 2023-08-16 14:08 ` tomas
  0 siblings, 0 replies; 9+ messages in thread
From: tomas @ 2023-08-16 14:08 UTC (permalink / raw)
  To: Mortimer Cladwell; +Cc: guile-user

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

On Wed, Aug 16, 2023 at 08:41:18AM -0400, Mortimer Cladwell wrote:
> Hi,
> How do I intern a top level variable?

You don't intern variables, you intern symbols (now I guess you
know that, but then: what are you trying to do?)

> looks like intern-symbol is
> deprecated? Is there a substitute? gensym does not seem to be it:

gensym evaluates to an already interned symbol:

  (symbol-interned? (gensym "foo"))
  => #t

> 
> scheme@(guile-user)> (gensym "abc")
> $11 = abc2652

... abc2652 would be an interned symbol here.

> scheme@(guile-user)> abc2652
> ;;; <unknown-location>: warning: possibly unbound variable `abc2652'
> ERROR: In procedure module-lookup: Unbound variable: abc2652

...but it is unbound (i.e. there is no value associated with it). For
this you would have to

  (define abc2652 "foo")

or something similar.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Re: intern a top level variable
@ 2023-08-16 14:55 Mortimer Cladwell
  2023-08-16 16:13 ` Jean Abou Samra
  0 siblings, 1 reply; 9+ messages in thread
From: Mortimer Cladwell @ 2023-08-16 14:55 UTC (permalink / raw)
  To: guile-user, tomas

I would like to intern and assign a value within a method:

(define (test-intern)
(let* ((name "abc")
       (data "def")
       (name-symbol (gensym name))
       )
  (pretty-print (string-append "symbol: " (symbol->string name-symbol)))
  (set! name-symbol data)))

scheme@(guile-user)> (test-intern)
"symbol: abc3301"
scheme@(guile-user)> abc3301
;;; <unknown-location>: warning: possibly unbound variable `abc3301'
ERROR: In procedure module-lookup: Unbound variable: abc3301

Thanks


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

* Re: Re: intern a top level variable
  2023-08-16 14:55 Re: intern a top level variable Mortimer Cladwell
@ 2023-08-16 16:13 ` Jean Abou Samra
  2023-08-16 17:58   ` tomas
  2023-08-16 19:17   ` Mortimer Cladwell
  0 siblings, 2 replies; 9+ messages in thread
From: Jean Abou Samra @ 2023-08-16 16:13 UTC (permalink / raw)
  To: Mortimer Cladwell, guile-user, tomas

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

Le mercredi 16 août 2023 à 10:55 -0400, Mortimer Cladwell a écrit :
> I would like to intern and assign a value within a method:
> 
> (define (test-intern)
> (let* ((name "abc")
>        (data "def")
>        (name-symbol (gensym name))
>        )
>   (pretty-print (string-append "symbol: " (symbol->string name-symbol)))
>   (set! name-symbol data)))
> 
> scheme@(guile-user)> (test-intern)
> "symbol: abc3301"
> scheme@(guile-user)> abc3301
> ;;; <unknown-location>: warning: possibly unbound variable `abc3301'
> ERROR: In procedure module-lookup: Unbound variable: abc3301


Sorry, but it's not clear to me what you mean by "intern and assign a value",
and I don't think it will be clear to someone else.

Can you be more precise please?

I don't understand why you expect "abc3301" to be bound after running (test-
intern). That (test-intern) call just creates a symbol, which is interned as a
symbol, meaning that creating another (interned) symbol with the same name will
reuse the same symbol value. But there is no reason why it shoud be bound to a
variable. Symbol interning just applies to symbols as values, it has nothing to
do with variables.



[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: Re: intern a top level variable
  2023-08-16 16:13 ` Jean Abou Samra
@ 2023-08-16 17:58   ` tomas
  2023-08-16 19:17   ` Mortimer Cladwell
  1 sibling, 0 replies; 9+ messages in thread
From: tomas @ 2023-08-16 17:58 UTC (permalink / raw)
  To: Jean Abou Samra; +Cc: Mortimer Cladwell, guile-user

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

On Wed, Aug 16, 2023 at 06:13:05PM +0200, Jean Abou Samra wrote:
> Le mercredi 16 août 2023 à 10:55 -0400, Mortimer Cladwell a écrit :
> > I would like to intern and assign a value within a method:
> > 
> > (define (test-intern)
> > (let* ((name "abc")
> >        (data "def")
> >        (name-symbol (gensym name))
> >        )
> >   (pretty-print (string-append "symbol: " (symbol->string name-symbol)))
> >   (set! name-symbol data)))
> > 
> > scheme@(guile-user)> (test-intern)
> > "symbol: abc3301"
> > scheme@(guile-user)> abc3301
> > ;;; <unknown-location>: warning: possibly unbound variable `abc3301'
> > ERROR: In procedure module-lookup: Unbound variable: abc3301
> 
> 
> Sorry, but it's not clear to me what you mean by "intern and assign a value",
> and I don't think it will be clear to someone else.
> 
> Can you be more precise please?

I think what the OP wants is to make a binding for
an existing symbol. Something, perhaps like

  scheme@(guile-user)> (define foovar (gensym "foo"))
  scheme@(guile-user)> foovar
  $1 = foo170
  scheme@(guile-user)> (module-add! (current-module) foovar (make-variable 42))
  scheme@(guile-user)> foo170
  $2 = 42

(note that the "top level" is Just Another Module).

Mortimer: is that what you were looking for? Note that
"you usually don't do this" (whatever that means), so
it'd make sense to take a step back and think about what
you are trying to achieve :-)

> I don't understand why you expect "abc3301" to be bound after running (test-
> intern). That (test-intern) call just creates a symbol, which is interned as a
> symbol, meaning that creating another (interned) symbol with the same name will
> reuse the same symbol value. But there is no reason why it shoud be bound to a
> variable. Symbol interning just applies to symbols as values, it has nothing to
> do with variables.

In any case it would make sense to revisit the concepts
of "symbol", "variable" and "binding". It's subtle, but
in Lispy lands it is indispensable.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Re: intern a top level variable
  2023-08-16 16:13 ` Jean Abou Samra
  2023-08-16 17:58   ` tomas
@ 2023-08-16 19:17   ` Mortimer Cladwell
  2023-08-16 20:35     ` Taylan Kammer
  2023-08-20 13:56     ` Jean Abou Samra
  1 sibling, 2 replies; 9+ messages in thread
From: Mortimer Cladwell @ 2023-08-16 19:17 UTC (permalink / raw)
  To: Jean Abou Samra; +Cc: guile-user, tomas

I would like to define a variable within a method but make it available to
the entire module - globally.
Take a string "abc" convert to a variable (symbol??) abc and set to the
string value "def".
The values of name and data are unknown - they are variable.
In this example the variable (symbol??) abc should evaluate to "def".

Thanks tomas you set me on the correct path. The following works:

(define (test-intern)
(let* ((name "abc")
       (data "def")
       )
   (module-define! (current-module)  (string->symbol name) data))
  )

scheme@(guile-user)> (test-intern)
scheme@(guile-user)> abc
$14 = "def"

So yes I will need to read more about interning.
Thanks
Mortimer



On Wed, Aug 16, 2023 at 12:13 PM Jean Abou Samra <jean@abou-samra.fr> wrote:

> Le mercredi 16 août 2023 à 10:55 -0400, Mortimer Cladwell a écrit :
>
> I would like to intern and assign a value within a method:
>
> (define (test-intern)
> (let* ((name "abc")
>        (data "def")
>        (name-symbol (gensym name))
>        )
>   (pretty-print (string-append "symbol: " (symbol->string name-symbol)))
>   (set! name-symbol data)))
>
> scheme@(guile-user)> (test-intern)
> "symbol: abc3301"
> scheme@(guile-user)> abc3301
> ;;; <unknown-location>: warning: possibly unbound variable `abc3301'
> ERROR: In procedure module-lookup: Unbound variable: abc3301
>
>
>
> Sorry, but it's not clear to me what you mean by "intern and assign a
> value", and I don't think it will be clear to someone else.
>
> Can you be more precise please?
>
> I don't understand why you expect "abc3301" to be bound after running
> (test-intern). That (test-intern) call just creates a symbol, which is
> interned as a symbol, meaning that creating another (interned) symbol with
> the same name will reuse the same symbol value. But there is no reason why
> it shoud be bound to a variable. Symbol interning just applies to symbols
> as values, it has nothing to do with variables.
>
>
>


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

* Re: intern a top level variable
  2023-08-16 19:17   ` Mortimer Cladwell
@ 2023-08-16 20:35     ` Taylan Kammer
  2023-08-17  6:07       ` Mortimer Cladwell
  2023-08-20 13:56     ` Jean Abou Samra
  1 sibling, 1 reply; 9+ messages in thread
From: Taylan Kammer @ 2023-08-16 20:35 UTC (permalink / raw)
  To: Mortimer Cladwell, Jean Abou Samra; +Cc: guile-user, tomas

On 16.08.2023 21:17, Mortimer Cladwell wrote:
> I would like to define a variable within a method but make it available to
> the entire module - globally.
> Take a string "abc" convert to a variable (symbol??) abc and set to the
> string value "def".
> The values of name and data are unknown - they are variable.
> In this example the variable (symbol??) abc should evaluate to "def".
> 
> Thanks tomas you set me on the correct path. The following works:
> 
> (define (test-intern)
> (let* ((name "abc")
>        (data "def")
>        )
>    (module-define! (current-module)  (string->symbol name) data))
>   )
> 

The "normal" way to do something like this would be with a macro that takes the
name of the variable to be bound, like so:

  (define-syntax defvar-example
    (syntax-rules ()
      ((_ <name>)
       (define <name> "def"))))

  (defvar-example abc)

  ;; variable abc has now been set to "def"

Is there any particular reason the name has to come from a string in your case?

For example, is the name of the variable only going to be known when the program
has already begun running, and doesn't appear in the initial source code?  That
would be one case where you really have to use 'module-define!' to do it.


(Note: I use pattern variables like <name> out of personal preference; there's no
special meaning to the <> around the name.)

-- 
Taylan




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

* Re: intern a top level variable
  2023-08-16 20:35     ` Taylan Kammer
@ 2023-08-17  6:07       ` Mortimer Cladwell
  0 siblings, 0 replies; 9+ messages in thread
From: Mortimer Cladwell @ 2023-08-17  6:07 UTC (permalink / raw)
  To: Taylan Kammer, guile-user

> is the name of the variable only going to be known when the program
has already begun running

yes

the values of name and data will be substringed from an argument passed to
the method at runtime
Thanks
Mortimer

On Wed, Aug 16, 2023 at 4:35 PM Taylan Kammer <taylan.kammer@gmail.com>
wrote:

> On 16.08.2023 21:17, Mortimer Cladwell wrote:
> > I would like to define a variable within a method but make it available
> to
> > the entire module - globally.
> > Take a string "abc" convert to a variable (symbol??) abc and set to the
> > string value "def".
> > The values of name and data are unknown - they are variable.
> > In this example the variable (symbol??) abc should evaluate to "def".
> >
> > Thanks tomas you set me on the correct path. The following works:
> >
> > (define (test-intern)
> > (let* ((name "abc")
> >        (data "def")
> >        )
> >    (module-define! (current-module)  (string->symbol name) data))
> >   )
> >
>
> The "normal" way to do something like this would be with a macro that
> takes the
> name of the variable to be bound, like so:
>
>   (define-syntax defvar-example
>     (syntax-rules ()
>       ((_ <name>)
>        (define <name> "def"))))
>
>   (defvar-example abc)
>
>   ;; variable abc has now been set to "def"
>
> Is there any particular reason the name has to come from a string in your
> case?
>
> For example, is the name of the variable only going to be known when the
> program
> has already begun running, and doesn't appear in the initial source code?
> That
> would be one case where you really have to use 'module-define!' to do it.
>
>
> (Note: I use pattern variables like <name> out of personal preference;
> there's no
> special meaning to the <> around the name.)
>
> --
> Taylan
>
>


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

* Re: Re: intern a top level variable
  2023-08-16 19:17   ` Mortimer Cladwell
  2023-08-16 20:35     ` Taylan Kammer
@ 2023-08-20 13:56     ` Jean Abou Samra
  1 sibling, 0 replies; 9+ messages in thread
From: Jean Abou Samra @ 2023-08-20 13:56 UTC (permalink / raw)
  To: Mortimer Cladwell; +Cc: guile-user, tomas

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

Le mercredi 16 août 2023 à 15:17 -0400, Mortimer Cladwell a écrit :
> I would like to define a variable within a method but make it available to the
> entire module - globally. [...]
> So yes I will need to read more about interning.


OK, so the reason we were all confused is that "interning" is an unrelated
concept that has absolutely nothing to do with what you were looking for here.

Symbol interning is what makes this true:

(eq? 'foobar 'foobar)

Namely, it's the fact that whenever a symbol is created, if there is already
another symbol with the same name, that symbol object is reused.

It's not the same as defining variables. A symbol is not a variable, just the
name for a variable. Interning applies to symbols, which also happen to be the
objects used to name variables, but creating a symbol does not create a
variable, just like when I say "John is an English name", it doesn't make
someone named John appear in the room :)


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

end of thread, other threads:[~2023-08-20 13:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-16 14:55 Re: intern a top level variable Mortimer Cladwell
2023-08-16 16:13 ` Jean Abou Samra
2023-08-16 17:58   ` tomas
2023-08-16 19:17   ` Mortimer Cladwell
2023-08-16 20:35     ` Taylan Kammer
2023-08-17  6:07       ` Mortimer Cladwell
2023-08-20 13:56     ` Jean Abou Samra
  -- strict thread matches above, loose matches on Subject: below --
2023-08-16 12:41 Mortimer Cladwell
2023-08-16 14:08 ` tomas

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