# What's the difference between environment and memory in terms of programming languages?

I've been reading up on programming languages to better my understanding with the textbook Essentials of Programming Languages (3e) - Friedman & Wand.

In a chapter regarding the states of languages (i.e. when we account for the effects that a computation may have rather than the immediately evaluated value), I've noticed that the terms environment and memory are used frequently, but I'm not sure how to grasp the difference.

According to the text, "an environment associates a value with each element of a finite set of variables," and "memory is a finite map from locations to a set of values called stored values."

Does this mean that environments are a part of memory? The part that especially confuses me is when we "extend the environment."

To give a short example, if I had a procedure such that

let f = proc (x) (x + 1) in
f (1)


then the text states that we "extend our environment" so variable f points to a location (say, l1) and argument x points to l2.

l1 in turn points to the procedure tuple (x, (x + 1), empty_env), and finally l2 points to the actual argument 1.

We update the location contents according to the procedure, which means we update l2 with 2, and the program finally returns 2.

Thank you.

# Functions

The terms "associate" and "map" together with "finite" indicate that both definitions refer to finite mathematical functions.

A (mathematical) function is a relation that associates each element of a set, the domain of the function, to a single element of another set (possibly the same set), the codomain of the function [Wikipedia].

Another way to put this, for finite functions are: a finite set of ordered pairs.

Map is usually defined as a synonym to function.

Thus the definitions could be rewritten as follows:

## Environment

An environment is a finite function that associates each element in the finite set of variables (domain), to a single element in the finite set of values (codomain). The range would be the set of all possible values (possibly infinite).

## Memory

A memory is a finite function that associates each element in the finite set of locations (domain), to a single element in the finite set of stored values (codomain). The range would be the set of all possible values (possibly infinite).

# Sets

Does this mean that environments are a part of memory?

Since this is about sets, if you by part mean subset, then the answer would be no.

However, the codomains are definitely related, if a variable is associated with a value in the environment, a location must also be associated to a stored value in the memory, that corresponds to the variable value.

# Extending the Environment

The part that especially confuses me is when we "extend the environment."

It seems the aim of the text is to build an interpreter. You start out with no variables declared (including procedures), i.e., an empty environment and then you interpret/parse/analyze the code to make these associations, i.e., extending the environment.

# In Practice

It might be helpful to think of the memory as the values stored somewhere in the physical memory and the environment as the values that the interpreted process currently knows about.