Re-evaluating expression

Whatever is on your mind, whether Lisp related or not.

Re-evaluating expression

Postby bitozoid » Mon Mar 19, 2018 5:12 pm

At http://yacas.readthedocs.io/en/latest/programming_in_yacas/:
One major design flaw in Yacas (one that other functional languages like LISP also have) is that when some expression is re-evaluated in another environment, the local variables contained in the expression to be evaluated might have a different meaning.

Do you agree? Anyone can provide an example in lisp?
bitozoid
 
Posts: 2
Joined: Mon Mar 19, 2018 5:03 pm

Re: Re-evaluating expression

Postby nuntius » Wed Mar 21, 2018 8:02 pm

I had to read the full "Yacas evaluation scheme" on the linked page to understand the quoted statement.

Back in the "good old days" (so I'm told), things like scoping rules were not taken for granted, and there were many ideas for how a variable name is mapped to a memory location.

Today, most languages have some notion of local variables, function parameters, and global variables that are checked in that order.
These are all scoped by static lexical rules.
Common Lisp uses similar rules, but it has dynamic variables (see DEFVAR) in place of global variables, where scope is determined by the function call stack.
By default, variables in Common Lisp are lexically scoped, similar to LocalSymbols and TemplateFunction in Yacas.

The Yacas design allows functions to be defined before the variables in the function body are defined.
Such undefined variables keep their name, and that name is looked up in the environment where the value is needed.
If the environment changes, then the value can also change.
In my book, this is more a feature of interpreted and some dynamic languages than it is a feature of functional languages or Lisp in particular.

So the undefined variables in Yacas have lookup similar to dynamic variables and calls to EVAL or PROGV in Common Lisp.
The design flaw mentioned in Yacas is most similar to the controversy surrounding Common Lisp's "unhygienic" macros.
The Scheme family of Lisp languages generally feature "hygienic" macro systems that avoid naming issues in macro definitions.

Other functional languages like Haskell use monads to provide similar "hygienic" features.

Common Lisp example:
Code: Select all
* (defvar *a* 1)
*A*

* *a*
1

* (let ((*a* 2)) *a*)
2

* (let ((*a* 3)) (eval '(list *a*)))
(3)
User avatar
nuntius
 
Posts: 534
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Re-evaluating expression

Postby bitozoid » Thu Mar 22, 2018 4:31 pm

Could you please explain more about this?
The Yacas design allows functions to be defined before the variables in the function body are defined.
Such undefined variables keep their name, and that name is looked up in the environment where the value is needed.


In LISP, afaik, eval uses null lexical environment and current dynamic environment, so a function argument (parameters are lexical) cannot be re-evaluated inside a function. Am I right?
bitozoid
 
Posts: 2
Joined: Mon Mar 19, 2018 5:03 pm

Re: Re-evaluating expression

Postby nuntius » Thu Mar 22, 2018 8:07 pm

bitozoid wrote:In LISP, afaik, eval uses null lexical environment and current dynamic environment


Agreed.

bitozoid wrote:so a function argument (parameters are lexical) cannot be re-evaluated inside a function.


I think I follow your question.
Common Lisp evaluates function arguments in the calling context, before the function is called, not in the context of the function body's definition.
Thus your statement holds for functions.
Macros are different, but that's not your question.

Some Lisp dialects have another type of function called "F expressions", where the arguments are evaluated within the function body (like CL macros).
Yacas seems more in that tradition.

https://en.wikipedia.org/wiki/Fexpr

http://www.nhplace.com/kent/Papers/Special-Forms.html
User avatar
nuntius
 
Posts: 534
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA


Return to The Lounge

Who is online

Users browsing this forum: No registered users and 1 guest