Chapter 4Standard MLslides.en.pdf:200

Expressions, Values, Environments

An expression evaluates to a value in an environment

Def 1.3Declarations bind variables
Concept

The fundamental loop of SML execution is: expression → environment → value.

An expression is anything that can be evaluated: a literal (42), a variable name (x), an arithmetic combination (3 + 4), a function application (f 5), or an if-expression.

An environment is the table of currently bound names (variables → values). When the interpreter sees a name, it looks it up in the environment. Def 1.3 (slides p.200) says declarations bind variables — every val or fun statement adds a new row to the environment.

Evaluation proceeds by replacing each subexpression with its value until only a value remains. A value is a fully evaluated expression: a literal, a tuple of values, or a function (which is itself a value — SML evaluates functions to closures).

Examples:
- 3 + 4 — evaluate 3 to 3, 4 to 4, then apply +7.
- (1, 2 + 3) — evaluate 2 + 3 first → 5, then build the pair (1, 5).

Evaluation is call-by-value: function arguments are evaluated before the function body runs. This makes evaluation deterministic and predictable.

Animation — sml_eval
Transcript — click a line to jump9 cues
  1. 0.0sSML: Parsing and Evaluating 1 + 2 * 3
  2. 1.0sSource expression 1 + 2 * 3 at the top.
  3. 1.8sAn arrow labelled parse takes us to the AST.
  4. 2.4sAST: + has children 1 and *; * has children 2 and 3.
  5. 4.4sHighlight the * subtree: it binds tightest (precedence).
  6. 4.8sEvaluate 2 * 3 = 6, then collapse that subtree to a leaf.
  7. 6.0sThe tree now has + with leaves 1 and 6.
  8. 7.7sHighlight the + node and compute 1 + 6 = 7.
  9. 9.0sResult is 7. Parse + evaluate together produce the answer.
Practice — score 100% to advance
Multiple choice
Q1
What is an SML environment?
Q2
Per Def 1.3, declarations bind…
Q3
SML evaluation is…
Q4
What does 3 + 4 evaluate to in SML?
Q5
Is a function in SML a value?
Fill in the blank
Q1
The three pieces of every SML evaluation are: expression, ___, and value.
Q2
SML evaluates function arguments before the body runs. This is called ___-by-value.
Q3
In (1, 2 + 3), the subexpression 2 + 3 is evaluated ___ before the pair is built.
Loading…