CS611 Lecture 5 IMP: Big-Step and Small-Step Semantics 8 September 2006Lecturer: Dexter Kozen1 The IMP LanguageToday we present a very simple imperative language, IMP, along with small-step and big-step rules forevaluation. We will give• the IMP language syntax;• a small-step semantics for IMP;• a big-step semantics for IMP;• some notes on why both can be useful.1.1 SyntaxThere are three types of statements in IMP:• arithmetic expressions AExp (elements are denoted a, a0, a1, . . .)• Boolean expressions BExp (elements are denoted b, b0, b1, . . .)• commands Com (elements are denoted c, c0, c1, . . .)A program in the IMP language is a command in Com.Let Var b e a countable set of variables. Elements of Var are denoted x, x0, x1. . . . Let n, n0, n1, . . . denoteintegers (elements of Z = {. . . , −2, −1, 0, 1, 2, . . .}). Let n be an integer constant symbol representing thenumber n. The BNF grammar for IMP isAExp ::= n | x | (a0⊕ a1)BExp ::= true | false | (a0 a1) | (b0 b1) | (¬b)Com ::= skip | x := a | (c0; c1) | (if b then c1else c2) | (while b do c )⊕ ::= + | ∗ | − ::= ≤ | = ::= ∨ | ∧1.2 Stores and ConfigurationsA store (also known as a state) is a function V ar → Z that assigns an integer to each variable. The set ofall stores is denoted Σ.A configuration is a pair hc, σi, where c ∈ Com is a command and σ is a store. Intuitively, the config-uration hc, σi represents an instantaneous snapshot of reality during a computation, in which σ representsthe current values of the variables and c represents the next command to be executed.2 Structural Operational Semantics (SOS): Small-Step SemanticsSmall-step semantics specifies the operation of a program one step at a time. There is a set of rulesthat we continue to apply to configurations until reaching a final configuration hskip, σi (if ever). Wewrite hc, σi → hc0, σ0i to indicate that the configuration hc, σi reduces to hc0, σ0i in one step, and we writehc, σi∗→ hc0, σ0i to indicate that hc, σi reduces to hc0, σ0i in zero or more steps. Thus hc, σi∗→ hc0, σ0i iff1there is a k ≥ 0 and configurations hc0, σ0i, . . . , hck, σki such that hc, σi = hc0, σ0i, hc0, σ0i = hck, σki, andhci, σii → hci+1, σi+1i for 0 ≤ i ≤ k − 1.To be completely proper, we will define auxiliary small-step operators →aand →bfor arithmetic andBoolean expressions, respectively, as well as → for commands1. The types of these op e rators are→ : (Com × Σ) → (Com × Σ)→a: (AExp × Σ) → Z→b: (BExp × Σ) → 2Here 2 represents the two-element Boolean algebra consisting of the two truth values {true, false} with theusual Boolean operations ∧, ∨, ¬. Intuitively, ha, σi∗→an if the expression a evaluates to the integer valuen in state σ.2.1 Arithmetic and Boolean Expressions• Constants:hn, σi →an• Variables:hx, σi →aσ(x)• Operations:ha0, σi →an0ha1, σi →an1ha0⊕ a1, σi →an0⊕ n1The rules for evaluating Boolean expressions and comparison operators are similar.One subtle point: in the rule for arithmetic operations ⊕, the ⊕ appearing in the expression a0⊕ a1represents the operation symbol in the IMP language, which is a syntactic object; whereas the ⊕ appearingin the expression n0⊕ n1represents the actual operation in Z, which is a semantic object. These are twodifferent things, just as n and n are two different things and true and true are two different things. In thiscase, at the risk of confusion, we have used the same metanotation ⊕ for both of them.2.2 CommandsLet σ[n/x] denote the store that is identical to σ except possibly for the value of x, which is n. That is,σ[n/x](y)4=σ(y), if y 6= x,n, if y = x.• Assignments:ha, σi →anhx := a, σi → hskip, σ[n/x]i• Sequences:hc0, σi → hc00, σ0ihc0; c1, σi → hc00; c1, σ0i hskip; c1, σi → hc1, σi• Conditionals:hb, σi →btruehif b then c0else c1, σi → hc0, σihb, σi →bfalsehif b then c0else c1, σi → hc1, σi• While statements:hwhile b do c, σi → hif b then (c; while b do c) else skip, σiThere is no rule for skip, since hskip, σi is a final configuration.1Winskel uses →1instead of → to emphasize that only a single step is performed.23 Structural Operational Semantics: Big-Step SemanticsAs an alternative to small-step operational semantics, which specifies the operation of the program one stepat a time, we now consider big-step operational semantics, in which we specify the entire transition froma configuration (an hexpression, statei pair) to a final value. This relation is denoted ⇓. For arithmeticexpressions, the final value is an integer; for Boolean expressions, it is a Boolean truth value true or false;and for commands, it is a final state. We writehc, σi ⇓ σ0(σ0is the store of the final configuration hskip, σ0i, starting in configuration hc, σi)ha, σi ⇓ n (n is the integer value of arithmetic expression a evaluated in state σ)hb, σi ⇓ t (t ∈ {true, false} is the truth value of Boolean expression b evaluated in state σ)The big-step rules for arithmetic and Boolean expressions are the sam e as the small-step rules. However,the rules for commands are different:• Skip:hskip, σi ⇓ σ• Assignments:ha, σi ⇓ nhx := a, σi ⇓ σ[n/x]• Sequences:hc0, σi ⇓ σ0hc1, σ0i ⇓ σ00hc0; c1, σi ⇓ σ00• Conditionals:hb, σi ⇓ true hc0, σi ⇓ σ0hif b then c0else c1, σi ⇓ σ0hb, σi ⇓ false hc1, σi ⇓ σ0hif b then c0else c1, σi ⇓ σ0• While statements:hb, σi ⇓ falsehwhile b do c, σi ⇓ σhb, σi ⇓ true hc, σi ⇓ σ0hwhile b do c, σ0i ⇓ σ00hwhile b do c, σi ⇓ σ004 Comparison of Big-Step vs. Small-Step SOS4.1 Small-Step• Small-step semantics can model more complex features, like programs that run forever and concurrency.• Although one-step-at-a-time evaluation is useful for proving certain properties, in many cas es it isunnecessary extra work.4.2 Big-Step• Big steps in reasoning make it easier to prove things.• Big-step semantics more closely models an actual recursive interpreter.• Because evaluation skips over intermediate steps, all programs without final configurations (infiniteloops, errors, stuck configurations) look the
View Full Document