Why dynamic scope?

Pondering the philosophy behind the language

Why dynamic scope?

Postby arquebus » Thu May 03, 2012 3:42 am

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?
Last edited by arquebus on Thu May 03, 2012 6:31 am, edited 1 time in total.
arquebus
 
Posts: 2
Joined: Thu May 03, 2012 3:14 am

Re: Why dynamic scope?

Postby Lutz » Thu May 03, 2012 6:15 am

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/MemoryManagement.html
http://www.newlisp.org/ExpressionEvaluation.html

these links all referenced from here:

http://www.newlisp.org/index.cgi?page=D ... ther_LISPs
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Why dynamic scope?

Postby cormullion » Thu May 03, 2012 8:00 am

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.

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...
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: Why dynamic scope?

Postby Lutz » Thu May 03, 2012 3:04 pm

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

Code: Select all
(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:

Code: Select all
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.
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Why dynamic scope?

Postby arquebus » Thu May 03, 2012 9:10 pm

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
arquebus
 
Posts: 2
Joined: Thu May 03, 2012 3:14 am

Re: Why dynamic scope?

Postby Ishpeck » Sat Jun 02, 2012 2:39 pm

arquebus wrote: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/

It's delightful.
Ishpeck
 
Posts: 14
Joined: Thu Jun 09, 2011 3:53 am

Re: Why dynamic scope?

Postby abaddon1234 » Thu Jun 09, 2016 2:50 am

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


gclub online
abaddon1234
 
Posts: 21
Joined: Mon Sep 14, 2015 3:09 am

Re: Why dynamic scope?

Postby rickyboy » Fri Jun 24, 2016 5:55 pm

abaddon1234 wrote: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.

abaddon1234 wrote:gclub online

Again, you should be banned from this forum.
(λx. x x) (λx. x x)
rickyboy
 
Posts: 582
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Why dynamic scope?

Postby clubZa » Fri Dec 02, 2016 10:13 am

I understand what you want

gclub download
clubZa
 
Posts: 1
Joined: Thu Nov 10, 2016 9:00 am

Re: Why dynamic scope?

Postby TedWalther » Fri Dec 02, 2016 5:49 pm

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.
TedWalther
 
Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC


Return to Whither newLISP?

Who is online

Users browsing this forum: No registered users and 2 guests