evaluation: state · cs101 special topics in computer science language-based security lecture 7:...

6
CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 1 IMP— a Simple Imperative Language Numbers (N): m, n Locations (Loc): X , Y Arithmetical expressions (Aexp): a ::= n | X | a 1 + a 2 | a 1 - a 2 | a 1 * a 2 Boolean expressions (Bexp): b ::= true | false | a 1 = a 2 | a 1 a 2 | ¬b | b 1 b 2 | b 1 b 2 Commands (Com): c ::= skip | X := a | c 1 ; c 2 | if b then c 1 else c 2 | while b do c CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 2 Evaluation: State A state σ maps locations to numbers. Σ= Loc N — set all all states. σ(X ) — contents of location X in state σ. For σ Σ and a Aexp we will define a relation hσ, ai -→ n: CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3 Evaluation: State A state σ maps locations to numbers. Σ= Loc N — set all all states. σ(X ) — contents of location X in state σ. For σ Σ and a Aexp we will define a relation hσ, ai -→ n: hσ, ni -→ hσ, X i -→ hσ, a 1 + a 2 i -→ hσ, a 1 - a 2 i -→ etc CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Upload: others

Post on 21-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

CS101

Special Topics in Computer Science

Language-Based Security

Lecture 7: IMP — a Simple Imerative Language

Aleksey Nogin

October 26, 2005

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 1

IMP— a Simple ImperativeLanguage

Numbers (N): m, n

Locations (Loc): X , Y

Arithmetical expressions (Aexp):a ::= n | X | a1 + a2 | a1 − a2 | a1 ∗ a2

Boolean expressions (Bexp):b ::= true | false | a1 = a2 | a1 ≤ a2 |

¬b | b1 ∨ b2 | b1 ∧ b2

Commands (Com):c ::= skip | X := a | c1; c2 | if b then c1 else c2 |

while b do c

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 2

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→n

〈σ, X〉−→σ(X)

〈σ, a1 + a2〉−→〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→

n

〈σ, X〉−→

σ(X)

〈σ, a1 + a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Page 2: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→n

〈σ, X〉−→

σ(X)

〈σ, a1 + a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→n

〈σ, X〉−→σ(X)

〈σ, a1 + a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→n

〈σ, X〉−→σ(X)

〈σ, a1 + a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Evaluation: StateA state σ maps locations to numbers. Σ = Loc → N — set allall states. σ(X) — contents of location X in state σ.For σ ∈ Σ and a ∈ Aexp we will define a relation 〈σ, a〉−→n:

〈σ, n〉−→n

〈σ, X〉−→σ(X)

〈σ, a1 + a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 + a2〉−→n, where n = n1 + n2.

〈σ, a1 − a2〉−→

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 − a2〉−→n, where n = n1 − n2.

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 3

Page 3: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

Evaluation of Booleans

〈σ, true〉−→ true and 〈σ, false〉−→ false

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 = a2〉−→ true, if n1 and n2 are equal;

〈σ, a1〉−→n1 〈σ, a2〉−→n2

〈σ, a1 = a2〉−→ false, if n1 and n2 are unequal;

〈σ, b〉−→ true

〈σ,¬b〉−→ falseand 〈σ, b〉−→ false

〈σ,¬b〉−→ true

etc

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 4

Evaluation of Commands

〈σ, c〉−→ ???

σ′

〈σ, skip〉−→ ???σ

〈σ, X := a〉−→???〈σ, a〉−→n

〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???〈σ, c1〉−→σ′

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→ ???σ

〈σ, X := a〉−→???〈σ, a〉−→n

〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???〈σ, c1〉−→σ′

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→ ???

σ

〈σ, X := a〉−→???〈σ, a〉−→n

〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???〈σ, c1〉−→σ′

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Page 4: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???〈σ, c1〉−→σ′

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Page 5: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, c〉−→

???

σ′

〈σ, skip〉−→

???

σ

〈σ, X := a〉−→???

〈σ, a〉−→n〈σ, X := a〉−→σ[n/X]

〈σ, c1; c2〉−→???

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, if b then c1 else c2〉−→???

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, while b do c〉−→???

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 5

Evaluation of Commands

〈σ, skip〉−→σ〈σ, a〉−→n

〈σ, X := a〉−→ σ[n/X]

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false

〈σ, while b do c〉−→σ

〈σ, b〉−→ true

〈σ, c〉−→σ′⟨

σ′, while b do c⟩

−→σ′′

〈σ, while b do c〉−→

σ′′

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 6

Evaluation of Commands

〈σ, skip〉−→σ〈σ, a〉−→n

〈σ, X := a〉−→ σ[n/X]

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false

〈σ, while b do c〉−→σ

〈σ, b〉−→ true 〈σ, c〉−→ σ′

σ′, while b do c⟩

−→ σ′′

〈σ, while b do c〉−→

σ′′

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 6

Page 6: Evaluation: State · CS101 Special Topics in Computer Science Language-Based Security Lecture 7: IMP — a Simple Imerative Language Aleksey Nogin October 26, 2005 CS101 Lecture 7

Evaluation of Commands

〈σ, skip〉−→σ〈σ, a〉−→n

〈σ, X := a〉−→ σ[n/X]

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false

〈σ, while b do c〉−→σ

〈σ, b〉−→ true 〈σ, c〉−→ σ′⟨

σ′, while b do c⟩

−→σ′′

〈σ, while b do c〉−→

σ′′

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 6

Evaluation of Commands

〈σ, skip〉−→σ〈σ, a〉−→n

〈σ, X := a〉−→ σ[n/X]

〈σ, c1〉−→σ′⟨

σ′, c2

−→ σ′′

〈σ, c1; c2〉−→σ′′

〈σ, b〉−→ true 〈σ, c1〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false 〈σ, c2〉−→σ′

〈σ, if b then c1 else c2〉−→σ′

〈σ, b〉−→ false

〈σ, while b do c〉−→σ

〈σ, b〉−→ true 〈σ, c〉−→ σ′⟨

σ′, while b do c⟩

−→σ′′

〈σ, while b do c〉−→σ′′

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 6

Small-Step Semantics

〈σ, skip; c〉 →1 〈σ, c〉

〈σ, c1〉 →1 〈σ′, c′1〉

〈σ, c1; c2〉 →1 〈σ′, c′1; c2〉

〈σ, a〉−→n

〈σ, X := a〉 →1 〈σ[n/X], skip〉

〈σ, b〉−→ true

〈σ, if b then c1 else c2〉 →1 〈σ, c1〉

〈σ, while b do c〉 →1

〈σ, if b then (c; while b do c) else skip〉

CS101 Lecture 7 IMP — a Simple Imerative Language October 26, 2005 – p. 7