Expressions, statements, and values

What has a value, what does something, and what the program’s state is made of — in Pyret and in Java.

Three words, used from Class 13 on. DCIC 3.2.2 set up the first two for Pyret; Java needs all three.

Values

A value is what an expression comes to, and what a name in the directory holds:

  • a number: 60, 2.5;
  • true or false;
  • a text: "ab". A text is an object, but one that never changes — every method on it gives a new text back — so it can be treated as a value;
  • an address: 1007, which is what a name holds when the thing is a builder or a generator.

Expressions

An expression has a value. Working it out is evaluation, and the value takes the expression’s place:

Pyret Java its value
u * 6 u * 6 a number
km <= 2 km <= 2 true or false
string-append("ab", "c") "ab" + "c" a text
stripe-width(10) stripeWidth(10) whatever the function gives back
if km <= 2: 5 else: 11 end km <= 2 ? 5 : 11 one branch’s value
— new Random(42) an address
— roll(r) a number — and the generator at r moves on

The last row is the first expression in the course that both has a value and changes something, and it gets a name of its own.

Impure expressions

A method call is an expression, and most are like u * 6: "ab".repeat(3) has a value, and nothing else happens. That is a pure call. An impure call has a value and changes the state — the directory, the heap or the output, defined below:

its value what it changes
r.nextInt(1, 7), and so roll(r) a number the generator at r moves on
b.append("c"), b.reverse() b’s address: the builder gives itself back the builder’s text
IO.println(x) nothing — the method is void the output

new Random(42) is not a method call, but it is impure the same way: its value is a new address, and the heap has one more thing in it — which is why two news are never ==.

Two things follow:

  • The same impure expression twice is not the same value twice. roll(r) + roll(r) on seed 42 is 3 + 4, so rollTwo could not be 2 * roll(r), and each check in Class 12 made its own new Random(42). A pure expression can be replaced by its value wherever it stands, which is the substitution of How Pyret runs your program, and every expression in the Pyret weeks was pure. An impure one has to be run where it stands, in order.
  • Any method call with ; after it is a statement. Its value is dropped, and the call is there for what it changes — its side effect: roll(r); rolls and keeps nothing, and that is why b.reverse(); sits in the table below. A void method is nearly always used this way, IO.println(x);. A bare value cannot stand alone: 5; is not a statement.

Statements

A statement does something and has no value. Java says so when a value stands alone: 5; is error: not a statement.

Pyret Java what it does
x = 5 int x = 5; adds x to the directory
x := 6 x = 6; changes what x holds
fun f(u): … end int f(int u) { … } adds a function
print(x) IO.println(x); adds a line to the output
— b.reverse(); changes the builder at b’s address
— return x; hands the function’s value out
— if (…) { … } else { … } chooses which statements run

Pyret’s if is on the expressions side; Java’s is on this one. That is why each branch of a Java function needs a return, and why ? : exists.

The program’s state

What a statement or an impure expression changes is the state: the directory (the names, and what each holds), the heap (the things at addresses — builders, generators), and the output. An object that can change — a builder, a generator — is part of the state. One that cannot — a text — is a value.