(?) Unexpected Symbols in My Context

Started by m i c h a e l, May 05, 2006, 08:12:01 AM

Previous topic - Next topic

m i c h a e l

This is odd:


(context 'graph)
graph
graph> (define (foo x y z) {...})
(lambda (x y z) "...")
graph> (set 'var 123)
123
graph> (symbols)
(foo var x y z)
graph> ; What are 'x, 'y, & 'z doing in there?

   

You may recognize this as the manual's example from the context section. It says I should only be seeing foo and var. Has the behavior of contexts changed?



Defining functions within MAIN caused the symbols I used for the parameters to be added to MAIN, as well. The "locals" from the likes of dolist, for, and let are also emerging from their hiding places, joining their brothers and sisters in their defining context.



Actually, I've been playing with ways of hiding these symbols (using contexts variously called private, local, and hidden) and have learned some interesting things along the way. Like realizing that context default functions enable a programming language dream of mine to come true. I hope to write up a show&tell about it as soon as I know whether the manual or the command line is telling me the truth ;-)





m i c h a e l



P.S. I recently installed 8.8.6, and for this test I commented out my init.lsp file. I'm not sure if this was happening in 8.8.5.

Lutz

#1
The definition of 'foo' is done while the current context is 'graph', so they are correctly added to the 'graph' context. Inside the 'graph' context 'x','y' and 'z' are treated as dynamically scoped variables but in their own lexical space isolated from other contexts like MAIN



Ther are also local in their behaviour as function parameters of 'foo':

> (context 'graph)
graph
graph> (define (foo x y z) (+ x y z))
(lambda (x y z) (+ x y z))
graph> (set 'x 1 'y 2 'z 3)
3
graph> (foo 10 20 30)
60
graph> x y z
1
2
3
graph>
(context MAIN)
> x y z
nil
nil
nil


After 'foo' was used 'x','y' and 'z' still have theire original values of 1 2 3, but duing the scope of 'foo' they where 10 20 30.



Lutz



ps: where did you see in the manual the contrary?

Lutz

#2
... more on symbols:



There is a MAIN symbol tree

MAIN:
    abs
    acos
    ...
    graph:
       foo
       x
       y
       z
    write-line
    x
    y
    z
    xml-parse
    xml-type-tags


and their are subtrees hanging of context symbols, part of MAIN (like graph). So there are actually to sets of 'x','y' and 'z' one in MAIN and another one in 'graph:'.



symbols are created in their current context as soon as they are 'seen' during loading of a program or as soon as you type them. And they are created in the current context. In their own context they behave according the rules of dynamic scoping. Their contents changes 'dynamically' during funtion execution if function parameters. But each context is a lexical unit by itself and protected from symbols of same name in other contexts/namespaces.



The default function 'foo' from our previous example is just syntactic sugar for 'foo:foo' to make it feel as in the current space, so (foo ...) behaves as if statically scoped, because its really (foo:foo foo:x foo:y foo:z) with it's parameter variables isolated from 'MAIN' and all name spaces not 'foo'



Lutz

m i c h a e l

#3
Hi Lutz. Thank you for taking the time to explain this to me. I'm still reading through your responses for comprehension ;-)


Quote from: "Lutz"ps: where did you see in the manual the contrary?


I found this as http://newlisp.org/downloads/newlisp_manual.html#context">context's example. (symbols) is shown returning: (foo-draw var) instead of (foo-draw var x y z).





m i c h a e l

Lutz

#4
Thanks, found it, I was looking in the Users Manual part in the Contexts section, will be corrected, as you mentioned it should be:



(foo-draw var x y z)



Lutz