Why dynamic scope?

Started by arquebus, May 02, 2012, 08:42:57 PM

Previous topic - Next topic

arquebus

I cant believe no one has asked this yet, but why does NewLisp have dynamic scope? I dont see any benefit to dynamic scope as all it does is create danger for name clashes between functions although I did notice that their is lexical scope for namespaces and modules. Is the reason for avoiding lexical scope is that you want to avoid incuring an overhead on speed in NewLisp? Is there any practical reason for having dynamic scope over lexical scope other than speed and efficiency of the running code?

Lutz

#1
The dangers of dynamic scope are vastly overrated and because of that, possibilities of dynamic scoping are almost unexplored.



Over the years, we have hardly ever seen dynamic scope to be a problem. Where the danger is somewhat present, is in 'define-maccro' fexprs and that danger is easily avoided using namespaces. There are very few reasons in newLISP style of programming to pass quoted symbols to 'define' functions, there are other safe ways to pass data by reference in newLISP, i.e. via namespace handles.



You can have lexical isolation using namespaces, which have very minimal overhead in newLISP - you can literally have millions of them. 'define-macro' style fexprs can be put in their own namespace context and then are completely safe. Generally, the best style of newLISP programming is, to put related data, functions and fexprs into a namespace context.



The absence of Scheme like lexical closures lets newLISP do a different kind of automatic memory management - not traditional garbage collection, but a synchronous type of memory management - much faster, without unexpected garbage collection pauses and much more efficient in using memory resources. There is no way to write a fully dynamic language that small and fast as newLISP using traditional garbage collection memory management.



see also:



http://www.newlisp.org/index.cgi?Closures">http://www.newlisp.org/index.cgi?Closures

http://www.newlisp.org/MemoryManagement.html">http://www.newlisp.org/MemoryManagement.html

http://www.newlisp.org/ExpressionEvaluation.html">http://www.newlisp.org/ExpressionEvaluation.html



these links all referenced from here:



http://www.newlisp.org/index.cgi?page=Differences_to_Other_LISPs">http://www.newlisp.org/index.cgi?page=D ... ther_LISPs">http://www.newlisp.org/index.cgi?page=Differences_to_Other_LISPs

cormullion

#2
There have been quite a few discussions about this topic here on the forum, over the years. Also, there are some interesting posts on other blogs, such as Kazimir's: http://kazimirmajorinc.blogspot.co.uk/2008/11/dont-fear-dynamic-scope.html">//http://kazimirmajorinc.blogspot.co.uk/2008/11/dont-fear-dynamic-scope.html.



You may find that dynamic scope is not a problem for your scripting needs, or proves advantageous. Or you may find it a huge stumbling block...

Lutz

#3
Just to point out the essential in Kazimir's post:



(define (sum f n)
    (set 'result 0)
    (for (i 1 n)
        (inc result (f i)))
    result)

; works because no clash of free j in f with i in sum
(for (j 1 5)
    (define (f x) (pow x j))
    (println j ": " (sum f 10)))

; does not work, free i in f clashes with i in sum
(for (i 1 5)
     (define (f x) (pow x i))
     (println i ": " (sum f 10)))

; works with previous expansion of free variable
(for (i 1 5)
     (letex (e i) (define (f x) (pow x e))) ; expansion of free variable
     (println i ": " (sum f 10)))


produces:



1: 55
2: 385
3: 3025
4: 25333
5: 220825
1: 1.040507132e+10
2: 1.040507132e+10
3: 1.040507132e+10
4: 1.040507132e+10
5: 1.040507132e+10
1: 55
2: 385
3: 3025
4: 25333
5: 220825


Free variables in dynamic scoping are not pre-bound during function definition. But just like a Scheme closure would bind free variables to its environment during definition of f, the same can be done using 'letex' or 'expand' binding the free variable during function definition.

arquebus

#4
Lutz and Cormullian- Thank you for your responses. Although I was hoping for a quick 2 sentence explanation, the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing. I have also discovered PicoLisp which is another light weight Lisp scripting language which is also dynamically scoped. Incidentally it was also designed by a german fellow. I also plan to learn emacs which means I will learn elisp, so I am now much more motivated to thoroughly understand elisp now that I have discovered that dynamic typing is used in NewLisp and PicoLisp.



I myself enjoy learning programming languages as a hobby so I have a general understanding of Lisp syntax as well as most other major functional languages. So hopefully I know enough to be able to understand the articles youve linked to.



I have applied to some universities and hopefully I will be accepted by one and start my CS degree this fall.

   thanks much

Ishpeck

#5
Quote from: "arquebus"I also plan to learn emacs which means I will learn elisp, so I am now much more motivated to thoroughly understand elisp now...


emacs is my very best friend.  



Be sure to get newlisp mode for emacs.

http://www.artfulcode.net/projects/">http://www.artfulcode.net/projects/



It's delightful.

abaddon1234

#6
the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing.





https://www.gclub88.com/">gclub online

rickyboy

#7
Quote from: "abaddon1234"the fact that the reason for using dynamic scope requires a deeper understanding of Lisp is of course a good thing.

Nice plagarism.  All you did was copy exactly what arquebus wrote two posts above yours.


Quote from: "abaddon1234"
https://www.gclub88.com/">gclub online

Again, you should be banned from this forum.
(λx. x x) (λx. x x)

clubZa

#8
I understand what you want



https://gclub.bbet88.com/gclub-download.html">gclub download

TedWalther

#9
We have a flood of spammers bumping topics.  But anyway... wow.  artfulcode is gone. :(
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

itistoday

Quote from: rickyboy on June 24, 2016, 10:55:52 AMAgain, you should be banned from this forum.

They've been banned, along with most/all of the other spammers. ;D
Get your Objective newLISP groove on.