Nп/п : 52 из 93
 От   : Tom Russ                            2:5075/128        31 авг 23 11:18:56
 К    : none) (albert                                         31 авг 23 21:20:03
 Тема : Re: What does (gensym) generate?
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<75e51292-e706-4e7e-8aa2-8c8327c71f4fn@googlegroups.com> df690fa7
@REPLY:
9920c478
@REPLYADDR Tom Russ <taruss@google.com>
@REPLYTO 2:5075/128 Tom Russ
@CHRS: CP866 2
@RFC: 1 0
@RFC-References:

@RFC-Message-ID:
<75e51292-e706-4e7e-8aa2-8c8327c71f4fn@googlegroups.com>
@TZUTC: -0700
@PID: G2/1.0
@TID: FIDOGATE-5.12-ge4e8b94
On Thursday, August 31, 2023 at 2:11:21 AM UTC-7, none albert wrote:
> The explanations about the usage of gensym are clear, 
> how your are supposed to used them. 
> The theoretical background however is far from it. 

The theoretical background is essentially this:
* For regular symbols, when the reader encounters a symbol, it creates a
   symbol object and "interns" it. Interning means creating a mapping from
   the symbol name to the symbol object.
* GENSYM will generate an "uninterned" symbol. It creates and returns a
   symbol object, but does *not* create a mapping from the (generated)
   name to the symbol object. That means that even if you type in the same
   as gets internally chosen for this uninterned symbol, it will not be the
   same object.

This is useful if you want a symbol for some purpose, in modern lisp usually
only for creating bindings inside macros, but don`t want that symbol to have
any possibility of interfering with any other symbols, you can use one of these
generated symbols.

https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node114.html#SECTION0015300000000
00000000


> (let* (aaa (gensym)) .. aaa .. ) 

> In the context `` .. aaa .. `` `aaa is obviously a symbol. 
> The interesting question is what value is aaa coupled to due to 
> the environment (aaa (gensym))? 
> Apparently aaa must be evaluated before it is any use. 
> However subsequently you see 
> (let* (aaa (rest whatever)) ... ) 
> What is disturbing to me is that let* only allows symbols into the 
> position of aaa and normally the coupling of aaa hides previous usage. 
> I have the following lisp type of objects: 
> symbols lists numbers strings arrays hashes normal-function 
> special-functions booleans 
> and more MAL oddballs: 
> nil key atom 

> Nothing seems an appropriate type for `aaa. 

> Example studied: 

> (defmacro! or 
> (fn* (& xs) 
> (if (empty? xs) 
> nil 
> (if (= 1 (count xs)) 
> (first xs) 
> (let* (condvar (gensym)) 
> `(let* (~condvar ~(first xs)) 
> (if ~condvar ~condvar (or ~@(rest xs))))))))) 

 One of the general uses of GENSYM is in macros. By introducing a
new, uninterned symbol one
 does not have to worry that the new symbol can inadvertently
capture (rebind) an external symbol
when the macro is expanded.

These are a bit silly, since there isn`t 
(defmacro bad-example (value &body body)
  `(let ((x ,value))
       (print x)
       ,@body))
(defmacro good-example (value &body body)
  (let ((v (gensym)))
    `(let ((,v ,value))
        (print ,v)
        ,@body))))

Now consider these two cases:
(let ((x 5))
   (bad-example 20 (print x)))
(let ((x 5))
   (good-example 20 (print x)))
The bad example will print 20 20 and the good one will properly print 20 5.

That is because if you expand the macros you would end up with:
(let ((x 5))
  (let ((x 20))
     (print x)
     (print x)))
vs
(let ((x 5))
  (let ((#1=#:GENSYM100 20)) ; The actual printed value may differ
and isn`t important.
      (print #1#)
      (print x)))
 This version uses the printer`s alias ability to make sure that the
same symbol is used
 in the LET and the PRINT, since there is no mapping from name to
symbol object when
the symbol is not interned.
--- G2/1.0
 * Origin: usenet.network (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх