evaluation: state · cs101 special topics in computer science language-based security lecture 7:...
TRANSCRIPT
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
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
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
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
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
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