help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: obarray


From: jack-mac
Subject: Re: obarray
Date: Tue, 17 Dec 2013 06:38:42 -0800 (PST)
User-agent: G2/1.0

Concerning uninterned symbols, you might try the following:

M-x ielm RET
*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq foo1 'foo)
foo
ELISP> (set foo1 "one")
"one"
ELISP> foo1
foo
ELISP> (symbol-value foo1)
"one"
ELISP> foo
"one"
ELISP> (unintern foo1)
t

ELISP> (setq foo2 'foo)
foo
ELISP> (set foo2 "two")
"two"
ELISP> foo
"two"

ELISP> (symbol-value foo1)
"one"
ELISP> (symbol-value foo2)
"two"

; And now, for the fun:

ELISP> foo1
foo
ELISP> foo2
foo
ELISP> (eq foo1 foo2)
nil

We now have two symbols almost identical called "foo"...

To understand what happened, you must refer to the 'read' function.
When I type the 16 characters contained in "(setq foo2 'foo)", 
the reader has a lot of intense thinking activity.

I'll try to be brief (and will skip some elementary steps), 
which leads to something more or less like this:

Reader: Hey! I just read a character "(". 
I recognize it since I've already seen that before.
It's what human beings (my creators/gods) call a left parenthesis.
Let me check the syntax table. Hum... it seems it's the beginning of a list.
So I should prepare to build such a list. Hence, I need a first single cons 
cell.
Hey, Allocator, could you please give me just one cons cell?
Allocator: Let me check. Yes, I've plenty of them (or, at least, one available).
So, I can give it to you for free without calling my friend, Garbage collector.
I just need to remove it from my free cells list and here it is.
Reader: Ok. Thank you for this fresh new cons cell.

Now, I should prepare to read what to put in this pretty cons cell.
Let me see. The character "s".  
Hum. Let me check the syntax table. That might be the beginning of a symbol.
Let me put it at the beginning of a string buffer.
Let's look forward. Oh! an "e"! That confirms it could be a symbol.
I'd rather collect it in the string buffer with the previous "s" I just found.
And now a "t" and a "q"! I knew it! It's definitely the beginning of a symbol.
I already have 4 characters in my string buffer.
Let's continue. Oh! What a pity! a character " ", a space, as THEY call it!
That breaks my quest for a symbol, since spaces are known to be separators 
(at least in this context).
So, now, let's look back. I got these 4 characters "s" "e" "t" "q" in my string 
buffer.
I know it must be a symbol. But which one?
I already know it's a symbol named "setq", but I need to find its identity, 
its address, otherwise I won't be able to ask it to do its job.
Hmmm. Where is the symbol named with these 4 letters "s" "e" "t" "q".
I'm pretty sure it's stored somewhere... 
Oh! I remember! It's probably stored in the obarray.

Hey, Obarray, could you please tell me if you know of a symbol called "setq"?
Obarray: Hum... Wait a minute, I need to do some searching.
Whoa! What a mess here... This array is really huge!
If I must parse all my atoms and ask politely each of them 
whether its name is exactly this string "setq",
it will last for (almost) an eternity.  
Maybe I should hash this string.
Hey, Hasher, could you please tell me the hash code for this string "setq"?
Hasher: Sure! its hash code is "Z0RG1U8".
Obarray: Thanks a lot! 
Now, let's have a look at the right bucket with this hash code and this string.
You are lucky, caller! I got a (really old) symbol in there just called "setq" 
and it's address is " 'setq ".
Reader: Oh! Thank you! 
Obarray: but, be careful, some pretend it's a special form...
Reader: Oh! Thanks for the warning! I don't really know yet what to do with it,
probably some sort of evaluation. I'll see that later.

Now I should store this address.
Hey, Pretty cons cell, could you please store this address inside the contents 
of your address register?
Pretty cons cell: What?! You speak like a dinosaur! 
Do you want me to store it inside my 'car' stuff?
Reader: Oh! Sorry. Yes, please.
Pretty cons cell: Ok! I won't forget it and you can rely on me to remind it to 
you
whenever you need it until I'm asked to replace it by something else, of course!
Reader: Thank you!

Let's erase my input string buffer and continue. 
The next character is an "f". 
Hum. It's not a dot. So I will probably need another cons cell.
Hey, Allocator, could you please give me another cons cell?
Allocator: What? Again? But what have you done with the last one I just gave 
you?
Reader: Oh! I keep it for the moment, but I need a new one.
Allocator: Ok. I see. Here is the address of another one, but don't loose it!
Reader: Oh! I won't! Thank you!

Hey, First cons cell, could you please insert this address into the contents of 
your decrement... errr... I mean, into your "cdr"?
First cons cell: you want me to replace this brave old "nil" in my "cdr" by 
this address?
Reader: Yes, please.
First cons cell: Ok. Done.
Reader: Thanks a lot.

Now let me store this "f" inside my input string. 
And a "o", and another "o", and now a "2".
Yes, it's still valid for a symbol's name (since they all have this symbol 
syntax)
and now a "space" separator, according to the syntax table.

Hey, Obarray, could you please tell me if you know of a symbol called "foo2"?
Obarray: Wait a minute, I need to do some searching.
[snip]
Nope! I have a "foo1", but no "foo2". 
Reader: Oh! What am I going to do with this symbol name belonging to no symbol?
Obarray: Do you want me to create one for you?
Reader: Oh yes, please.
Obarray: Ok. Hey, Allocator, could you please create a symbol named "foo2"?
Allocator: Sure. I just need a few contiguous cells and here it is!
Obarray: Thank you for this address! 
Hey, Caller, here is your fresh new symbol.
Will you want to have the ability to find it again later?
Reader: Oh yes, please.
Obarray: So, I'd rather store it inside my array and tag it with its hash code. 
Ok. It's now stamped as "interned".
Reader: Thank you so much.

Ok. The next character is a "'". Let me have a look at the syntax table.
Oh! Oh! This one is a macro-character! That is quite special...

Author's note: Though I must admit how fascinating is this story,
we'll just skip some small steps and have a look a little further...

[snip]

This ")" character is a separator. It ends up the string buffer.
Hey, Obarray, could you please tell me if you know of a symbol called "foo"?
Obarray: Wait a minute, I need to do some searching.
Hum... How strange! I thought I had the vague feeling I created such one 
earlier.
Let me check again. 
No, sorry! I have a "foo1" and a "foo2", but no "foo" interned.
Reader: Never mind! Could you create and intern one for me, please?
Obarray: No problem. Here it is!
Reader: Thank you so much!
Hey, Second second cons cell, could you please store this address inside your 
"car"?
Second second cons cell: Ok. Done.

Reader: Now, let me return back to this separator character ")".
Oh! It means that the second list I started to build earlier is finished.
It contains 2 linked cons cells, with pointers to symbols respectively
named "quote" and "foo".
Now, let me return back to my previous list. 
Hey, Third cons, could you please store inside your "car" the address 
of this second list?
Third cons cell: Ok. Done.
So, this first list now contains 3 linked cons cells, with a pointer 
to the symbols respectively named "setq" and "foo2", and a pointer 
to a cons whose 'car' points to the symbol named "quote" 
and whose 'cdr' points to a cons whose 'car' points to a symbol named "foo"
and whose 'cdr' points to a special location, a symbol named "nil".
This could be printed (for THEM) as "(setq foo2 (quote foo))".
Ok. Now I can return a pointer to this fresh list (I mean, 
to the first cons cell of this list) and give it back to my caller.
Yeah!

Evaluator: Well, thank you, Reader, for this beautiful list.
Let me have a look at its first element.
Hey, First cons cell, could you tell me what is the contents 
of your address register?
First cons cell: Oh! No! You too?! Anyway, my "car" is a pointer 
to the symbol named "setq".
Evaluator: Thank you!

Hey, Symbol named "setq", could you please give me the contents
of your 'symbol-function'?
Symbol named "setq": It's (a pointer to) a subr named "#<subr setq>".
Evaluator: Hey, Subr "setq", could you please tell me if your 'subr-arity' 
ends up with the symbol 'unevalled ?
Subr "setq": Yes! How did you know that?
Evaluator: I suspected it. So you are a special form! 
Thus I won't evaluate your arguments.
Subr "setq": which are?
Evaluator: Hey, First cons cell, what is your second element?
First cons cell: You mean my 'cadr'? It's (a pointer to) the symbol named 
"foo2".
Evaluator: Thank you. And your third element?
First cons cell: It's (a pointer to) a cons.
Evaluator: Ok. Hey, Subr "setq", could you please compute with the symbol "foo2"
and this cons?
Subr "setq": No problem. 

I first need to evaluate this second argument.
Hey, Evaluator, could you please evaluate this list "(quote foo)"?
[snip]
Evaluator: I got your result: it's a pointer to the symbol named "foo".
Subr "setq": Ok. Thank you. Let me check the first argument.
Hey, Symbol named "foo2", are you a variable?
Symbol named "foo2": Sure! Why? 
Evaluator: Could you please store inside your 'symbol-value' the symbol named 
"foo"?
Symbol named "foo2": Ok. Done.
Evaluator: Well. It seems I just finished my job. And the result is a pointer
to the symbol named "foo"!

And now, just for the fun:

(progn
  (setq foo1 'foo)
  (set foo1 "one")
  (unintern foo1)
  (setq foo2 'foo)
  (set foo2 "two")
  (eq foo1 foo2))
=> t


reply via email to

[Prev in Thread] Current Thread [Next in Thread]