Lecture 2: Program Semantics - GitHub Pages

37
Lecture 2: Program Semantics Rohan Padhye and Jonathan Aldrich Program Analysis

Transcript of Lecture 2: Program Semantics - GitHub Pages

Page 1: Lecture 2: Program Semantics - GitHub Pages

Lecture 2: Program Semantics

Rohan Padhye and Jonathan Aldrich

Program Analysis

Page 2: Lecture 2: Program Semantics - GitHub Pages

Learning goals

• Use big- and small-step semantics to show how While programs evaluate

• Use small-step semantics to show how While3Addr programs evaluate

• Use structural induction to prove things about program semantics

(c) 2021 Le Goues, Aldrich, Padhye 2

Page 3: Lecture 2: Program Semantics - GitHub Pages

WHILE (concrete) syntax

• Categories:– S ∈ Stmt statements– a ∈ Aexp arithmetic expressions– x, y ∈ Var variables– n ∈ Num number literals– P ∈ BExp boolean predicates– l ∈ labels statement addresses (line numbers)

• Syntax:– S ::= x := a | skip | S1 ; S2

| if P then S1 else S2 | while P do S– a ::= x | n | a1 opa a2– opa ::= + | - | * | / | …– P ::= true | false | not P | P1 opb P2 | a1 opr a2– opb ::= and | or | …– opr ::= < | ≤ | = | > | ≥ | ...

3(c) 2021 Le Goues, Aldrich, Padhye

Page 4: Lecture 2: Program Semantics - GitHub Pages

Analysis of WHILE

• Questions to answer:

– What is the “meaning” of a given While

expression/statement ?

– How would we go about evaluating While

expressions and statements?

– How are the evaluator and the meaning related?

(c) 2021 Le Goues, Aldrich, Padhye 4

Page 5: Lecture 2: Program Semantics - GitHub Pages

Three Canonical Approaches

• Operational– How would I execute this?

• Axiomatic– What is true after I

execute this?– Symbolic Execution

• Denotational– What function is this trying

to compute?

(c) 2021 Le Goues, Aldrich, Padhye 5

Page 6: Lecture 2: Program Semantics - GitHub Pages

An Operational Semantics

• Specifies how expressions and statements should be evaluated

• Depending on the form of the expression– 0, 1, 2, . . . don’t evaluate any further.

• They are normal forms or values.– a1 + a2 is evaluated by first evaluating a1 to n1, then

evaluating a2 to n2. (post-order traversal)• The result of the evaluation is the literal representing n1 + n2.

– Similarly for a1 * a2

• Operational semantics abstracts the execution of a concrete interpreter

(c) 2021 Le Goues, Aldrich, Padhye 6

Page 7: Lecture 2: Program Semantics - GitHub Pages

Semantics of WHILE

• The meanings of WHILE expressions depend on the values of variables – What does “x+5” mean? It depends on “x”!

• The value of integer variables at a given moment is abstracted as a function from Varto Z (a state)– If x = 8 in our state, we expect “x+5” to mean 13

• We use E, a state, to denote a map from variables to values

(c) 2021 Le Goues, Aldrich, Padhye 7

Page 8: Lecture 2: Program Semantics - GitHub Pages

Notation: Judgment

• We write:

< E, a> n

• To mean that a evaluates to n in state E.• This is a judgment. It asserts a relation

between E, a, and n. • In this case we can view as a function

with two arguments (E and a).

(c) 2021 Le Goues, Aldrich, Padhye 8

Page 9: Lecture 2: Program Semantics - GitHub Pages

Operational Semantics

• This formulation is called big-step operational semantics– or natural operational semantics– the judgment relates the expression and

its “meaning”

• How should we define < E, a1 + a2> … ?

(c) 2021 Le Goues, Aldrich, Padhye 9

Page 10: Lecture 2: Program Semantics - GitHub Pages

Notation: Rules of Inference

• We express the evaluation rules as rules of inference for our judgment– called the derivation rules for the judgment– also called the evaluation rules (for

operational semantics)

• Typically, we have a rule per language construct:

<E, a1 + a2> n1 + n2

<E, a1> n1 <E, a2> n2 This is the onlyrule for a1 + a2

(c) 2021 Le Goues, Aldrich, Padhye 10

Page 11: Lecture 2: Program Semantics - GitHub Pages

Rules of Inference

Conclusion

Hypothesis1 … HypothesisN

<E, a1 + a2> n1 + n2

<E, a1> n1 <E, a2> n2

• For any given proof system, a finite number of rules of inference (or schema) are listed somewhere

• Rule instances should be easily checked

(c) 2021 Le Goues, Aldrich, Padhye 11

Page 12: Lecture 2: Program Semantics - GitHub Pages

Evaluation Rules (for Aexp)

<E, n> n <E, x> E(x)

<E, a1 + a2> n1 + n2

<E, a1> n1 <E, a2> n2

<E, a1 - a2> n1 - n2

<E, a1> n1 <E, a2> n2

<E, a1 - a2> n1 * n2

E, a1> n1 <E, a2> n2

• This is called structural operational semantics– rules defined based on the structure of the expression

• These rules do not impose an order of evaluation!

(c) 2021 Le Goues, Aldrich, Padhye 12

Page 13: Lecture 2: Program Semantics - GitHub Pages

Derivations

(c) 2021 Le Goues, Aldrich, Padhye 13

Page 14: Lecture 2: Program Semantics - GitHub Pages

Evaluation of Statements

• The evaluation of a Stmt may have side effects but has no direct result– What is the result of evaluating a statement?

• The “result” of a Stmt is a new state:

< E, S> E’– Note: the evaluation of S might not

terminate! We’ll come back to this issue

(c) 2021 Le Goues, Aldrich, Padhye 14

Page 15: Lecture 2: Program Semantics - GitHub Pages

Stmt Evaluation Rules 1

<E, skip> E <E, S1 ; S2> E’’

<E, S1> E’ <E’, S2> E’’

15(c) 2021 Le Goues, Aldrich, Padhye

How would you write rule(s) for if?

Page 16: Lecture 2: Program Semantics - GitHub Pages

Stmt Evaluation Rules 1

<E, skip> E <E, S1 ; S2> E’’

<E, S1> E’ <E’, S2> E’’

<if P then S1 else S2> E’

<E, P> true <E, S1> E’

<if b then S1 else S2> E’

<E, P> false <E, S2> E’

16(c) 2021 Le Goues, Aldrich, Padhye

Page 17: Lecture 2: Program Semantics - GitHub Pages

Stmt Evaluation Rules 2

Def: E[x n](x) = nE[x n](y) = E(y)<E, x := a> E[x n]

<E, a> n

• Practice: write the rule(s) for while

17(c) 2021 Le Goues, Aldrich, Padhye

Page 18: Lecture 2: Program Semantics - GitHub Pages

Stmt Evaluation Rules 3

<E, while P do S> E

<E, P> false

Def: E[x n](x) = nE[x n](y) = E(y)<E, x := a> E[x n]

<E, a> n

<E, while P do S> E’

< E, P> true <E, S; while P do S> E’

18(c) 2021 Le Goues, Aldrich, Padhye

Page 19: Lecture 2: Program Semantics - GitHub Pages

Big-Step Evaluation Issues

• The evaluation rules are not syntax-directed– See the rules for while– The evaluation might not terminate

• Recall: the evaluation rules suggest an interpreter

• Big-step semantics has two big disadvantages (continued …)

19(c) 2021 Le Goues, Aldrich, Padhye

Page 20: Lecture 2: Program Semantics - GitHub Pages

Disadvantages of Natural-Style Operational Semantics

• It is hard to talk about statements whose evaluation does not terminate– When there is no E’ such that <E, S> E’– But that is true also of ill-formed or

erroneous statements (in a richer language)!

• It does not give us a way to talk about intermediate states– Thus we cannot say that on a parallel

machine the execution of two commands is interleaved (= no modeling threads)

20(c) 2021 Le Goues, Aldrich, Padhye

Page 21: Lecture 2: Program Semantics - GitHub Pages

Semantics Solution

• Small-step semantics addresses these problems– Execution is modeled as a (possible infinite)

sequence of states– Each atomic execution step rewrites the

program

21(c) 2021 Le Goues, Aldrich, Padhye

Page 22: Lecture 2: Program Semantics - GitHub Pages

Small step semantics

• We will define a relation <E, S> <E’, S’>– S’ is obtained from S via a rewrite step– Evaluation terminates when the program has

been rewritten to a terminal program• one from which we cannot make further progress

– For While the terminal command is “skip”– As long as the command is not “skip” we can

make further progress• some commands never reduce to skip (e.g., “while

true do skip”)

22(c) 2021 Le Goues, Aldrich, Padhye

Page 23: Lecture 2: Program Semantics - GitHub Pages

Configurations

• A pair of state and statement: <E, S>.

• Big step judgment relates a configuration to a new state: <E, S> E’

• Small step relates a configuration to a new configuration: <E, S> <E’, S’>

• We have one such arrow per grammar production, except Aexp and Bexp do not produce new states (as before).

23(c) 2021 Le Goues, Aldrich, Padhye

Page 24: Lecture 2: Program Semantics - GitHub Pages

Executions

• Key idea: we repeatedly rewrite the program until we reach a final configuration.

• A small step execution is a sequence (or list) of rewrites:

<E,x+(7-3)> <E,x+(4)> <E,5+4> <E,9>

Ex)=5

24(c) 2021 Le Goues, Aldrich, Padhye

Page 25: Lecture 2: Program Semantics - GitHub Pages

While: Small-step examples

<E, if P then S1 else S2>

<E, P> b P’

<E, if P’ then S1 else S2>

<E, if true then S1 else S2> <E, S1>25(c) 2021 Le Goues, Aldrich, Padhye

Page 26: Lecture 2: Program Semantics - GitHub Pages

While: Small-step examples

26(c) 2021 Le Goues, Aldrich, Padhye

Page 27: Lecture 2: Program Semantics - GitHub Pages

Practice: write small-step rule(s) for a while loop

27(c) 2021 Le Goues, Aldrich, Padhye

Page 28: Lecture 2: Program Semantics - GitHub Pages

While3Addr: a different representation

• While3Addr programs are mappings between numbers and instructions.

• So, the configuration needs to include a program counter:

• And the judgment is slightly different:

28(c) 2021 Le Goues, Aldrich, Padhye

Page 29: Lecture 2: Program Semantics - GitHub Pages

(c) 2021 Le Goues, Aldrich, Padhye 29

Page 30: Lecture 2: Program Semantics - GitHub Pages

(c) 2021 Le Goues, Aldrich, Padhye 30

Page 31: Lecture 2: Program Semantics - GitHub Pages

Provability

• Given an opsem system, <E, a> n is provable if there exists a well-formed derivation with <E, a> n as its conclusion– “well-formed” = “every step in the derivation is

a valid instance of one of the rules of inference for this opsem system”

– “<E, a> n” = “it is provable that <E, a> n”

31(c) 2021 Le Goues, Aldrich, Padhye

Page 32: Lecture 2: Program Semantics - GitHub Pages

Proof by Mathematical Induction

Prove n.P(n) by induction on natural numbers

• Base case: show that P(0) holds

• Inductive case: show that P(m) implies P(m+1)

32(c) 2021 Le Goues, Aldrich, Padhye

Page 33: Lecture 2: Program Semantics - GitHub Pages

Structural Induction

• We can also do induction over syntax

a ::= x | n | a1 opa a2

(c) 2021 Le Goues, Aldrich, Padhye 33

Base casesBase casesInductive

case

Page 34: Lecture 2: Program Semantics - GitHub Pages

Example Proof by Structural Induction

34(c) 2021 Le Goues, Aldrich, Padhye

Page 35: Lecture 2: Program Semantics - GitHub Pages

Induction on Syntax

• Provable by induction on syntax

• Not provable by induction on syntax

35(c) 2021 Le Goues, Aldrich, Padhye

Rule for while doesn’t just depend on subexpressions

Page 36: Lecture 2: Program Semantics - GitHub Pages

Induction on Derivations

• Derivations have recursive structure

36(c) 2021 Le Goues, Aldrich, Padhye

Base casesBase casesBase cases

Inductive cases

Inductive cases

Page 37: Lecture 2: Program Semantics - GitHub Pages

Determinism of Statements

37(c) 2021 Le Goues, Aldrich, Padhye