cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
ErraticAttack
Level VI

JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL

If I'm wrong please correct me, but it would seem that JMP has dynamic-scope with no scope persistence (except for the pseudo-namespaces Here, Window, Box).  This contrasts with many popular programming languages that have lexical-scope with full scope-persistence using reference-counters.

 

This caused a lot of head-smashing on my end until I gained the intuition needed to work with the weird scope system that JMP has, and I believe that it's a large point of confusion for many people.  Not many programming languages use Dynamic scope (LISP being an important exception, and JSL has some similarities to LISP).

 

I'd encourage the JSL documentation team to point out this distinction and provide some resources / links on how to better understand Dynamic scope as it certainly is not the standard way of doing name-resolution.

 

Here is an example -- the outer function's local scope is cleared up immediately after the return statement, and the inner functions name-resolution is dynamic (based on the current call-stack):

Names Default to Here( 1 );

x = 7;
outer function = Function( {},
	{x},
	x = 4;
	inner function = Function( {},
		Print( x )
	);
	Return( Name Expr( inner function ) );
);

func = outer function;
func()

/*** OUTPUT ***
7

If you do something functionally equivalent in a lexically-scoped language with scope-persistence (like Python), then the inner function's enclosing-scope is persistent (it's the local-scope for the outer function), and the name-resolution for the inner function's x-variable is the value 4 from the lexical reading of the system.  Since JMP doesn't use lexical-scope, but uses dynamic-scope, then when the inner function is finally executed at the end of the script, it uses dynamic name-resolution of Local -> Here -> Global (and possible column-names for the current table) and finds the value 7.

 

Understanding this type of behavior is critical for building robust programs in JSL, and I just want to point it out to anyone learning / struggling with JSL.

 

Let me know if you have any thoughts / unintuitive examples!

Jordan
2 REPLIES 2
KevW
Level II

Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL

This is true, and definitely threw me for a loop when learning the language.  The other wrinkle is that JSL is a Term Rewriting Language thanks to its expression objects, which means that while being LISP-ish with respect to scoping, is not LISP-ish with respect to how it handles macros.  

 

The only other term-rewriting language I'm aware of in popular use is Wolfram Language.  I'm not well versed enough in CS to say for sure, but I have heard some people allege that the term-rewriting paradigm is strictly more powerful than Lisp's macros.  On the other hand, I have heard some claim that Wolfram Language is simply an implementation of m-expressions, where Lisp decided s-expressions were good enough.

 

Regardless, JSL's expression system will be foreign to just about everyone, and the ergonomics of their functions for working with expressions don't do them any favors.  There is a reason that you can find so many examples in this forum of people using string manipulation and Parse() instead of the term-rewriting features.

ErraticAttack
Level VI

Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL

I'm not a CS person myself, but this stuff on term-rewriting is quite interesting!  Thanks for the reference, I'll be up all night on this rabbit-hole now...

Jordan