Schwannden 2020/9/16
Expression & Statement
Did you mean what you say?
Appetizer
Course Announcement
Course Announcement
• Gitter: https://gitter.im/PLLecture/community

• Announcement system

• Post slides

• Post videos

• Post streaming links

• Notion: https://www.notion.so/PL-Lecture-Plan-
MoBagel-446b3bb01cee45e4b41fc965a8d25145

• Syllabus

• Google Calendar

• Send me your email so I can add you to lecture event.
Syllabus
1. Expression
1. Definition
2. Some Basic Expressions
2. Side Effect

1. Definition

2. Referential Transparency

3. Argument Evaluation Order
Expression
Definition
an expression is a syntactic entity in a programming
language that may be evaluated to determine its valu
Expression
Variable expression
// simple expression
x // evaluate to the value in memory location represented by x
// arithmetic expression
x + 1 // the expression x first evaluate to a value, and plus 1
// function call expression
f(x) // value to the return value of f given x as input of f
// class construction expression
New ClassA(A) // evaluate to an instance of type ClassA by constructor
Expression
Variable expression
// assignment expression
x = 1 // usually evaluate to nothing (null, undefined, Unit, …etc)
// increment expression
x++ // evaluate to the r-value before increment
++x // evaluate to the l-value after increment
Note l-value and r-value are terms common in C/C++ literature:
1. L-value are values that can appear on the left-hand-side of
an assignment expression.
2. R-value is the value that can appear only appear on the
right hand side of an assignment expression.
Syllabus
1. Expression

1. Definition

2. Some Basic Expressions

2. Side Effect
1. Definition
2. Referential Transparency
3. Argument Evaluation Order
Statement
Definition
a statement is a syntactic unit of an imperative
programming language that expresses some action to
be carried out.
Statement
Definition
In C/C++ think of an statement as an
expression + ;
It is an expression who value is not wanted
// assignment statement
x = 1; // usually evaluate to nothing (null, undefined, Unit, …etc)
// increment statement
x++; // evaluate to the r-value before increment
++x; // evaluate to the l-value after increment
Statement
Definition
If we don’t want the value of the
expression, what do we want?
// assignment statement
x = 1; // side effect is changing the content of x to 1
// increment statement
x++; // side effect is updating the content of x by its value + 1
++x; // side effect is the same
Statement
Definition
If we don’t want the value of the
expression, what do we want? -> Side effect
// assignment statement
x = 1; // side effect is changing the content of x to 1
// increment statement
x++; // side effect is updating the content of x by its value + 1
++x; // side effect is the same
Statement
Definition
Assignment and IO expression yields side
effects.
// assignment statement
x = 1; // side effect is changing the content of x to 1
// increment statement
x++; // side effect is updating the content of x by its value + 1
++x; // side effect is the same
// print to stdout expression
print(“tenet”) // write character string to stdout file descriptor
Statement
Definition
let x = 1 // undefined, assignment expression yield no value
function f() {
x++;
}
f() // undefined, the function doesn’t return any value
x // 2, x is incremented by 1
The call f() yields a side effect, due to the assignment
statement x++. This is why we call it side effect.
Statement
Referential Transparency and Opaque
• Referential Transparency 

• Contexts do not affect the meanings of
expressions

• Referential Opaque 

• Contexts do affect the meanings of expressions
Statement
Referential Transparency and Opaque
Side effect makes a language opaque, for example:
let x = 1
function f() {
return x++;
}
f() // 1
f() // 2
f() // 3
In a referential opaque language, exp==exp may not be true:
f()==f() // false
Statement
Cons for side effect
• Hard to read

• Hard to Prove

• Prohibit Optimization 

• exp + exp cannot be optimized to 2*exp, if exp
yields side effects. 

• Make Parallel Processing Difficult

• Suppose exp1 reads from x and exp2 writes to x,
what will be the value of exp1 + exp2, if exp1 and
exp2 are evaluated in parallel?
Statement
Cons for side effect - Hard to Prove
Side effect makes a language opaque, for example:
let x = 1
function f() {
return x++;
}
f()
f()
// what is the value of this expression
(x++ * x) == x + x + x
Statement
Cons for side effect - Portability issue
Side effect makes a language opaque, for example:
let x = 1
function f1() { return x++; }
function f2() { return x--; }
function f3() { return ++x; }
f1 + f2() * f3() // ?????
All languages specify operator precedence, but might not specify
operant evaluation order.
Statement
Side effects and argument evaluation order
• Side effects and argument evaluation order 

• Without side effects

• Argument evaluation order is immaterial

• Purely functional languages won’t specify
argument evaluation order, e.g. Haskell.

• With side effects 

• Argument evaluation order is important 

• Some specify argument evaluation order, e.g. Java

• Most languages don’t, e.g. C, C++, Scheme
Statement
Argument Evaluation Order
• Pro

• Increase portability

• Con

• Increase efficiency – leave a room for the compiler
optimization

• e+e can be optimized to 2*e 

• exp1+exp2 can be evaluated in parallel
int n = 0;
n * (5 / n) // with required order, this must be evaluated
// without evaluation order, compiler can decide this is 0,
because 0 * anything is 0
Lesson:
Don’t write code that
depends on evaluation order

PL Lecture 04 - Expression and Statement

  • 1.
    Schwannden 2020/9/16 Expression &Statement Did you mean what you say?
  • 2.
  • 3.
  • 4.
    Course Announcement • Gitter:https://gitter.im/PLLecture/community • Announcement system • Post slides • Post videos • Post streaming links • Notion: https://www.notion.so/PL-Lecture-Plan- MoBagel-446b3bb01cee45e4b41fc965a8d25145 • Syllabus • Google Calendar • Send me your email so I can add you to lecture event.
  • 5.
    Syllabus 1. Expression 1. Definition 2.Some Basic Expressions 2. Side Effect 1. Definition 2. Referential Transparency 3. Argument Evaluation Order
  • 6.
    Expression Definition an expression isa syntactic entity in a programming language that may be evaluated to determine its valu
  • 7.
    Expression Variable expression // simpleexpression x // evaluate to the value in memory location represented by x // arithmetic expression x + 1 // the expression x first evaluate to a value, and plus 1 // function call expression f(x) // value to the return value of f given x as input of f // class construction expression New ClassA(A) // evaluate to an instance of type ClassA by constructor
  • 8.
    Expression Variable expression // assignmentexpression x = 1 // usually evaluate to nothing (null, undefined, Unit, …etc) // increment expression x++ // evaluate to the r-value before increment ++x // evaluate to the l-value after increment Note l-value and r-value are terms common in C/C++ literature: 1. L-value are values that can appear on the left-hand-side of an assignment expression. 2. R-value is the value that can appear only appear on the right hand side of an assignment expression.
  • 9.
    Syllabus 1. Expression 1. Definition 2.Some Basic Expressions 2. Side Effect 1. Definition 2. Referential Transparency 3. Argument Evaluation Order
  • 10.
    Statement Definition a statement isa syntactic unit of an imperative programming language that expresses some action to be carried out.
  • 11.
    Statement Definition In C/C++ thinkof an statement as an expression + ; It is an expression who value is not wanted // assignment statement x = 1; // usually evaluate to nothing (null, undefined, Unit, …etc) // increment statement x++; // evaluate to the r-value before increment ++x; // evaluate to the l-value after increment
  • 12.
    Statement Definition If we don’twant the value of the expression, what do we want? // assignment statement x = 1; // side effect is changing the content of x to 1 // increment statement x++; // side effect is updating the content of x by its value + 1 ++x; // side effect is the same
  • 13.
    Statement Definition If we don’twant the value of the expression, what do we want? -> Side effect // assignment statement x = 1; // side effect is changing the content of x to 1 // increment statement x++; // side effect is updating the content of x by its value + 1 ++x; // side effect is the same
  • 14.
    Statement Definition Assignment and IOexpression yields side effects. // assignment statement x = 1; // side effect is changing the content of x to 1 // increment statement x++; // side effect is updating the content of x by its value + 1 ++x; // side effect is the same // print to stdout expression print(“tenet”) // write character string to stdout file descriptor
  • 15.
    Statement Definition let x =1 // undefined, assignment expression yield no value function f() { x++; } f() // undefined, the function doesn’t return any value x // 2, x is incremented by 1 The call f() yields a side effect, due to the assignment statement x++. This is why we call it side effect.
  • 16.
    Statement Referential Transparency andOpaque • Referential Transparency • Contexts do not affect the meanings of expressions • Referential Opaque • Contexts do affect the meanings of expressions
  • 17.
    Statement Referential Transparency andOpaque Side effect makes a language opaque, for example: let x = 1 function f() { return x++; } f() // 1 f() // 2 f() // 3 In a referential opaque language, exp==exp may not be true: f()==f() // false
  • 18.
    Statement Cons for sideeffect • Hard to read • Hard to Prove • Prohibit Optimization • exp + exp cannot be optimized to 2*exp, if exp yields side effects. • Make Parallel Processing Difficult • Suppose exp1 reads from x and exp2 writes to x, what will be the value of exp1 + exp2, if exp1 and exp2 are evaluated in parallel?
  • 19.
    Statement Cons for sideeffect - Hard to Prove Side effect makes a language opaque, for example: let x = 1 function f() { return x++; } f() f() // what is the value of this expression (x++ * x) == x + x + x
  • 20.
    Statement Cons for sideeffect - Portability issue Side effect makes a language opaque, for example: let x = 1 function f1() { return x++; } function f2() { return x--; } function f3() { return ++x; } f1 + f2() * f3() // ????? All languages specify operator precedence, but might not specify operant evaluation order.
  • 21.
    Statement Side effects andargument evaluation order • Side effects and argument evaluation order • Without side effects • Argument evaluation order is immaterial • Purely functional languages won’t specify argument evaluation order, e.g. Haskell. • With side effects • Argument evaluation order is important • Some specify argument evaluation order, e.g. Java • Most languages don’t, e.g. C, C++, Scheme
  • 22.
    Statement Argument Evaluation Order •Pro • Increase portability • Con • Increase efficiency – leave a room for the compiler optimization • e+e can be optimized to 2*e • exp1+exp2 can be evaluated in parallel int n = 0; n * (5 / n) // with required order, this must be evaluated // without evaluation order, compiler can decide this is 0, because 0 * anything is 0
  • 23.
    Lesson: Don’t write codethat depends on evaluation order