Lambda and the Environment Model - scheme

I need help drawing the relevant portions of the environment model diagram when evaluating this code:
Scheme>(define x 10)
Scheme> ((lambda (x y) (+ (y 3) x)) 6 (lambda (w) (* x 9)))
I need to make sure and write each lambda body next to the environment in which it is being evaluated.
Okay I know that there is only one define so most of the work will be done by “anonymous” or “nameless” functions and these will still show up in various ways in the environment model diagram

In addition to the answers already given, the 6.001 course at MIT has two very comprehensive lectures on the environment model, the reasons for its existence, as well as some very helpful and fine-grained step-by-step examples:
Lecture 1
Lecture 2
Hope this helps,
Jason

If I remember correctly, whenever you execute a lambda, a new environment is created where the arguments' values are bound to their names. This environment inherits from whichever environment the lambda was originally declared in.
The first environment in all cases is the global environment--this is where the (define x 10) resides. Then, as I said before, add a new environment whenever you execute a lambda (as in the second line). This environment inherits from whichever environment the lambda was executed in.
The first thing you did (starting with the second line) is call the first lambda. To do this, you have to evaluate the arguments. Since you evaluate the arguments before actually entering the first lambda, the second lambda is declared in the global environment.
Next, an environment is created for the first lambda's call (inheriting from the global environment). Here x is bound to 6 and y is bound to the second lambda. Then, to do the +, the second lambda is called. Since it was declared in the global environment, its new environment inherits from this rather than from the first lambda's environment. This means that, for the second one, x is bound to 10 rather than 6.
I hope this explains everything understandably.
To clarify: there are going to be three environments--the global environment and one environment per function invocation. Both of the function invocations' environments will inherit from the global environment. The first lambda's code will run in its own environment, while the second lambda's code will run the the second lambda's.
Additionally, check out envdraw, which can be found here: http://inst.eecs.berkeley.edu/~cs3s/stk/site-scheme/envdraw/
If you read the ANNOUNCE file, it will tell you how to get it. You'll need to use STk, a particular Scheme interpreter.
envdraw draws environment diagrams for Scheme automatically.
Disclaimer: I never bothered with envdraw when taking the class that used Scheme, but it was endorsed by my professor (apparently one of his students wrote it back in the day) and other people seemed to do fine using it.

Related

Clojure: capture runtime value of function arg, to use in REPL

Problem
My web front-end calls back-end queries with complex arguments. During development, to avoid time-consuming manual replication of those arguments, I want to capture their values in Vars, which can be used in REPL.
Research
This article shows that inline def is a suitable solution, but I couldn't make it work. After a call from the front-end happened, the Var remained unbound.
I launched the backend with REPL through VS Code + Calva, with the following code:
(defn get-analytics-by-category [params]
(def params params)
...)
And here's the evaluation of the Var in the REPL:
#object[clojure.lang.Var$Unbound 0x1298f89e "Unbound: #'urbest.db.queries/params"]
Question
Why the code above didn't bound value of the argument to the Var? Is there another solution?
The best way that I found is to use scope-capture library. It captures all the local variables with addition of 1 line in a function, and then with another 1-liner you can define all those variables as global, which allows you to evaluate in REPL any sub-expression in the function using runtime values.
If you ever spent a lot of time reproducing complex runtime values, I strongly recommend watching their 8-min demo.
My issue with inline-def was likely caused by reloading namespace after the Var was bound to a value. After restarting VS Code and carefully doing everything again, the issue went away.
Another way to look at the runtime is to use a debugger.
It is more flexible than scope-capture, but requires a bit more work to make variables available outside an execution.
VS Code's Calva extension comes with one, there's also Emacs packages.

Remove variable from namespace [duplicate]

How to undefine a variable in Scheme? Is this possible?
You're touching a nerve here. Scheme doesn't have a very clear standard notion of how top-level environments work. Why? Because the Scheme standards represent a compromise between two sets of people with very different ideas of how Scheme should work:
The interpretive crowd, who sees the top-level environment as you describe above: a runtime hash-table where bindings are progressively added as program interpretation proceeds.
Then there's the compilation crowd, who sees the top-level environment as something that must be fully computable at compilation time (i.e., a compiler must be able to conclusively identify all of the names that will be bound in the top-level environment).
Your "how do I undefine a variable" question only makes sense in the first model.
Note that the interpretive model, where a program's top-level bindings depend on what code paths get taken, makes efficient compilation of Scheme code much harder for many reasons. For example, how can a Scheme compiler inline a procedure invocation if the name of the procedure is a top-level binding that may not just change during runtime, but even disappear into nothingness?
I'm firmly in the compilation camp here, so what I would recommend to you is to avoid writing code that relies on the ability to add or remove top-level bindings at runtime, or even that requires the use of top-level variables (though those are often unavoidable). Some Scheme systems (e.g., Racket) are able to produce reasonably good compiled code, but if you make those assumptions you'll trip them up in that regard.
In Scheme, variables are defined with either lambda, or one of the various lets. If you want one of them to be 'undefined' then all you need to do is leave the scope that they're in. Of course, that's not really undefining them, it's just that the variable is no longer bound to its previous definition.
If you're making top level definitions, using (define), then technically you're defining a function. Since Scheme is functional, functions never really go away. I suppose that technically, it's stored in some sort of environment function somewhere, so if you were intimately familiar with your implementation (and it's not safeguarded somehow) you could probably overwrite it with your own definition of the globabl environment. Barring that, I'd say that your best bet would be to redefine the function to return the null list- that's really as empty as you get.
Scheme (R7RS) has no standard compliant way to remove a top-level binding.
If you evaluate a non existing variable, you get an error:
(eval 'a)
; => ERROR: undefined variable: a
If you define it, the variable gets added to the top-level environment.
(define a 1)
(eval 'a)
; => 1
As from now no matter what you do, you will not get an error, if you access the variable.
If you set it to false, you will get false:
(set! a #f)
(eval 'a)
; => #f
Even if you set it to something unspecified, it is unlikely that you get an error:
(set! a (if #f #t))
(eval 'a)
; =>
But Schemes may have a non-standard way to remove a top-level binding. MIT Scheme provides the function unbind-variable.
As stated in the other answers there is no standard way of manipulating the namespace in Scheme. For a specific implementation there might be a solution.
In Racket the top-level variables are stored in a namespace. You can remove a variable using namespace-undefined-variable.
There is no way of removing a local variable.
http://docs.racket-lang.org/reference/Namespaces.html?q=namespace#%28def.%28%28quote.~23~25kernel%29._namespace-undefine-variable%21%29%29
(set! no-longer-needed #f)
Does this achieve the effect you want? You can also use define at the top level.
guile> (define nigel "lead guitar")
guile> nigel
"lead guitar"
guile> (define nigel #f)
guile> nigel
#f
guile>
You could then re-define the variable. This all depends on the scope of the variables, of course: see Greg's answer.
You cannot unbind a variable in standard Scheme. You could set! the variable to 'undefined, I guess, or you could write a metainterpreter which reifies environments, allowing you to introduce your own notion of undefining variables.
I think, if your point is to do the equivalent of "free" or de-allocate, then no you're pretty much out of luck. you can't de-allocate a variable. you CAN re-define it to something small, like #f, but once you've done (define foo 'bar) the variable foo will exist in some form until you end the program.
On the other hand, if you use let, or letrec, of course, the name only exists until the relevant close paren...
I think your question is not stupid. In AutoLISP has unexisting (undefined) variable apriori supposted value "nil" (even if the variable does not exist in memory - it means - if it is not in a table of variables - then the value is "nil" - "false"). It means also false. And it is also empty list. If you program some kind of list processing function, it is enough to make initial test only by:
(if input-list ....)
When you want to explicitly undefine any variable, you may do this:
(setq old-var nil); or: (setq old-var ())
I like it. The keyword "setq" means "define". What is better on bounding and unbounding variables in other dialects? You must test if they exist, if they are lists, you need garbage-collector, you may not undefine variable to explicitly free memory. Following command can not be written if variable "my-list" is not defined:
(define my-list (cons 2 my-list))
So I think the AutoLISP way is for programming much better. Possibilities, that I written, you may use there. Unfortunately the AutoLISP works in some CAD engineering graphical systems only.

SBCL error: "No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME."

I am new to Lisp, using SBCL 1.2.11 from the terminal.
Could any one help me figure out where I should start looking to get rid of the above error? I think it is causing me the following error:
(setf x (list 'a 'b 'c))
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
; (SETF X (LIST 'A 'B 'C)) ; ==> ; (SETQ X (LIST 'A 'B 'C))
; ; caught WARNING: ; undefined variable: X ; ; compilation unit finished
; Undefined variable: ; X ; caught 1 WARNING condition (A B C)
I should not be seeing the comments, is that right?
Thank you so much!
[I've added this answer as there seem to be no others and in the hope that it may help.]
There are two problems here:
you're doing something which is not legal Common Lisp (however commonly it is done);
SBCL is warning about this in a slightly uninformative way.
There's an important bit of terminology which is common in Lisp but I think less common in other languages. That terminology is binding: a binding is, loosely, an association between a name of some kind and a value. Associated with a binding are a scope -- where it is visible -- and an extent, when it is visible. (I am not going to talk about scope and extent because it's a big subject and this answer is already too long.) Almost all programming languages have these three notions but they often call them different things in confusing ways.
The thing often called a variable is a name which is associated with a value: it is, in fact, a binding. And of course the term 'binding' originates from 'variable binding'. But not all bindings are variables -- the term is now more general and more precise (although I'm not giving anything like a precise definition here).
There are two families of constructs which deal with bindings:
constructs which establish bindings;
constructs which modify bindings.
These two things are different in CL. In common with many programming languages there are special constructs which create bindings, and other constructs which modify (mutate) them. Constructs which modify bindings require the bindings to exist so they can be modified.
Constructs which establish bindings
In CL These are things like let: (let ((x 1) y) ...) establishes local bindings for x and y visible in its lexical scope (usually). defvar and friends establish global bindings. defun and friends establish global bindings in a different namespace (the function namespace, as opposed to the variable namespace) in CL, and flet / labels establish local function bindings. There are other constructs, and in particular the set of constructs is effectively user-extensible in the usual Lisp way.
Constructs which modify bindings
The traditional construct to modify a variable binding is setq: setf is a macro which allows you to modify bindings (and other things it calls 'places' such as elements of arrays) in a more general, and user-extensible way. So (setf x 2) modifies the binding of x.
The mistake
The mistake you are making is that you can't just say (setf a ...) unless there is an existing binding of a. This code is illegal CL:
(defun foo ()
(setf a 1)
...)
Instead you need to establish a binding for a:
(defun foo ()
(let ((a ...))
...
(setf a 1)
...))
Well, the same thing is true at the top-level: you can't just say:
> (setf x (list 'a 'b 'c))
because *you're trying to modify a binding of x which does not exist.
And this is what SBCL is telling you, in a rather uninformative (I think) way: it's telling you that there is no existing binding of x.
Solutions and nonsolutions
Unfortunately CL doesn't really offer a very good solution to this problem. One way to 'solve' it is to do this:
> (defvar x ...)
[...]
> (setf x (list 'a 'b 'c))
But this is an undesirable solution: (defvar x ...) turns x into a globally special variable -- a variable which is dynamically scoped. This changes the semantics of any binding of x, for instance in a later function, in ways which can be unexpected. This is undesirable. If you want to do it, at least make sure your special variables follow the *star* convention so it's obvious which they are.
CL, out-of-the-box doesn't offer what you might want, which is 'top-level lexical variables' -- variable bindings you can declare at the top-level which don't do this unfortunate globally-special thing. However Lisp is so flexible that you can actually add this to it if you want.
But still this is kind of a clunky solution: the whole point of having a conversational language is that you don't need to spend your life painfully declaring everything when you talk to the implementation: you want just to be able to say (setf x 1) and have that work.
And in many implementations it does work: the top-level interactive environment lets you just say that, and just does the right thing, while when you, for instance, compile a file, you will get a compile-time warning and a run-time error (possibly warning) if you do the same thing. However this relaxed behaviour in the interactive environment is quite clearly outside the standard.
SBCL doesn't do that because, I think, it doesn't really have a top-level interactive interpreter but rather compiles everything. So you get these warnings. An SBCL person might want to correct me, but I think it is reasonably safe to ignore them when you're typing at the system (but not when you are compiling files) and treat SBCL like other implementations.
A way not to fix the problem
One way that might seem sensible to fix this problem is just not to have special constructs to create bindings: a binding is created by the first assignment. This is what Python does, for instance. Superficially this seems like a clever trick: there's less typing and fuss.
But what is the scope of these implicitly-created bindings meant to be? (Python says 'the whole function, including bits of it which get run before the first assignment', which is, well, interesting.) And, worse, how do you distinguish between something which is an assignment to a variable defined in an outer scope and the identical construct which is creating a binding in an inner scope: well you do that with a special 'global' construct ... which doesn't really work: Python now has a 'nonlocal' construct as well. And how do you tell whether a variable is bound or not at compile time (or even, really, at run-time) to give good warnings?
The trick which seemed like a good idea actually makes things more complicated in many cases: it's reasonable for a quick-and-dirty scripting language, but less reasonable for a large-scale-systems language, I think.

sicp 2.4.3 data directed programming and additivity, scheme

Could some provide clarification to the example of complex arithmetic decribed in the chapter. I can not understand one point. I would appreciate any help.
The problem is the following:
There are two packages with similiar naming of procedures.
The first one is "(install-rectangular-package)". The second one is "(install-polar-package)". In addition, a procedure is defined:
(define (make-from-real-imag x y)
(get 'make-from-real-imag 'rectangular) x y))
i type in scheme interperter
(install-rectangular-package)
(install-polar-package)
(make-from-real-imag 3 5)
and it works. what i do not understand how "get" inside "make-from-real" finds proper function in the proper package. when the string "(get 'make-from-real-imag 'rectangular)" is executed ,it replaces by "(lambda (x y) (tag (make-from-real-imag x y))))" but how it knows that it has to call function inside "(install-rectangular-package)" but not in "(install-polar-package)".
The chapter includes some sentences that say you are simply supposed to assume that the procedures put and get exist:
To implement this plan, assume that we have two procedures, put and get, for manipulating the operation-and-type table:
(put <op> <type> <item>)
installs the <item> in the table, indexed by the <op> and the <type>.
(get <op> <type>)
looks up the <op>, <type> entry in the table and returns the item found there. If no item is found, get returns false.
For now, we can assume that put and get are included in our language.
So, now we must ask for a clarification of your question:
Are you asking "how can implement a procedure like install-rectangular-package so that after (install-rectangular-package) is evaluated, the get procedure can lookup the desired operations?"
Or are you asking "how does get itself work?"
Or are you asking: "Even if we assume such a table for supporting put and get exists, how can the presented code work, where it installs multiple distinct functions with names like real-part (and imag-part, etc) even though the one real-part comes from the rectangular package, and another real-part comes from the polar package?"
If you are asking the first question, the answer is: install-rectangular-package simply calls put with the appropriate arguments to extend the lookup table that get will access.
If you are asking the second question, then you will need to see how put and get are implemented, which is discussed in Chapter 3. But the quick answer is: You could use a data structure that stores a record of every {<op>, <type>, <item>} triple inserted by put. The book describes one way to do this, where you just build up a list of entries.
(The main interesting thing that any implementation of put and get needs to do is imperatively modify some hidden state. The book uses the set-cdr! operation to do this. The requirement to use some form of imperative operation is probably the reason why they waited until Chapter 3 to describe the implementation of put and get.)
If you are asking the third question, the answer is "by the magic of lexical scoping"
The definition of install-rectangular-package has a collection of internal definitions, and install-polar-package has another collection of internal definitions. Even though there is overlap between the names chosen in the two definitions, installing the polar package does not overwrite the functions previously defined by the rectangular package.
(It is important to distinguish here between the name used in a function definition versus the function value/object (which you might think of as the (lambda (x y) ...)) itself. Even though install-rectangular-package and install-polar-package reuse the same names, they are creating distinct function values, and those distinct values are then being put into the put/get table, without any significance attached to the name used to originally define them.)
Even though the picture of the put/get table in the book looks like:
the entries in the table are not names. They are instead function objects. Other local definitions of real-part or imag-part will not affect the entries that were installed by install-rectangular-package nor install-polar-package; the only way to affect those entries is to call put itself with the matching <op> and <type> arguments to overwrite the previous cell in the table.
For more discussion of lexical scope and ways to think about local function definitions, I recommend this part of HtDP ("HtDP" stands for "How to Design Programs", which, like SICP, is an intro to programming, but written in a fashion that spells things out a bit more than SICP does; see also this paper comparing SICP and HtDP.)

when to free a closure's memory in a lisp interpreter

I'm writing a simple lisp interpreter from scratch. I have a global environment that top level variables are bound in during evaluation of all the forms in a file. When all the forms in the file have been evaluated, the top level env and all of the key value data structs inside of it are freed.
When the evaluator encounters a lambda form, it creates a PROC object that contains 3 things: a list of arguments to be bound in a local frame when the procedure is applied, the body of the function, and a pointer to the environment it was created in. For example:
(lambda (x) x)
would produce something internally like:
PROC- args: x,
body: x,
env: pointer to top level env
When the PROC is applied, a new environment is created for the frame and the local bindings are staged there to allow the body to be evaluated with the appropriate bindings. This frame environment contains a pointer to its closure to allow variable lookup inside of THAT. In this case, that would be the global environment. After the PROC body is evaluated, I can free all the cells associated with it including its frame environment, and exit with no memory leaks.
My problem is with higher order functions. Consider this:
(define conser
(lambda (x)
(lambda (y) (cons x y))))
A function that takes one argument and produces another function that will cons that argument to something you pass into it. So,
(define aconser (conser '(1)))
Would yield a function that cons'es '(1) to whatever is passed into it. ex:
(aconser '(2)) ; ((1) 2)
My problem here is that aconser must retain a pointer to the environment it was created in, namely that of conser when is was produced via the invocation (conser '(1)). When aconser the PROC is applied, its frame must point to the frame of conser that existed when aconser was defined, so I can't free the frame of conser after applying it. I don't know how/the best way to both free the memory associated with a lambda frame when it is applied and also support this kind of persistent higher order function.
I can think of some solutions:
some type of ARC
copying the enclosing environment into the frame of the evaluated PROC when it is produced
This seems to be what is being implied here. So, instead of saving a pointer in the PROC object to its closure, I would... copy the closure environment and store a pointer to that directly in the cell? Would this not just be kicking the can one level deeper and result in the same problem?
recursively substituting the labels at read time inside of the body of the higher order function
I am worried I might be missing something very simple here, and also I am curious as to how this procedure is supported in other implementations of lisp and other languages with closures in general. I have not had much luck searching for answers because the question is very specific, perhaps even to this implementation (that I am admittedly just pulling out of my hat as a learning project) and much of what I am able to find simply explains the particulars of closures from the language being implemented's perspective, not from the language that the language is being implemented in's.
Here is a link to the relevant line in my source, if it is helpful, and I am happy to elaborate if this question is not detailed enough to describe the problem thoroughly. Thanks!
The way this is handled usually in naive interpreters is to use a garbage-collector (GC) and allocate your activation frames in the GC'd heap. So you never explicitly free those frames, you let the GC free them when applicable.
In more sophisticated implementations, you can use a slightly different approach:
when a closure is created, don't store a pointer to the current environment. Instead, copy the value of those variables which are used by the closure (it's called the free variables of the lambda).
and change the closure's body to use those copies rather than look in the environment for those variables. It's called closure conversion.
Now you can treat your environment as a normal stack, and free activation frames as soon as you exit a scope.
You still need a GC to decide when closures can be freed.
this in turn requires an "assignment conversion": copying the value of variables implies a change of semantics if those variables get modified. So to recover the original semantics, you need to look for those variables which are "copied into a closure" as well as "modified", and turn them into "reference cells" (e.g. a cons cell where you keep the value in the car), so that the copy doesn't copy the value any more, but just copies a reference to the actual place where the value is kept. [ Side note: such an implementation obviously implies that avoiding setq and using a more functional style may end up being more efficient. ]
The more sophisticated implementation also has the advantage that it can provide a safe for space semantics: a closure will only hold on to data to which it actually refers, contrary to the naive approach where closures end up referring to the whole surrounding environment and hence can prevent the GC from collecting data that is not actually referenced but just happened to be in the environment at the time it was captured by the closure.

Resources