Why dynamic scope?

Pondering the philosophy behind the language
Locked
arquebus
Posts: 2
Joined: Thu May 03, 2012 3:14 am

Why dynamic scope?

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Why dynamic scope?

Post by Lutz »

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Why dynamic scope?

Post by cormullion »

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/2 ... 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Why dynamic scope?

Post by Lutz »

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.

arquebus
Posts: 2
Joined: Thu May 03, 2012 3:14 am

Re: Why dynamic scope?

Post by arquebus »

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
Posts: 14
Joined: Thu Jun 09, 2011 3:53 am

Re: Why dynamic scope?

Post by Ishpeck »

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.

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

Re: Why dynamic scope?

Post by abaddon1234 »

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


gclub online

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Why dynamic scope?

Post by rickyboy »

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)

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

Re: Why dynamic scope?

Post by clubZa »

I understand what you want

gclub download

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Why dynamic scope?

Post by TedWalther »

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.

Locked