Lecture 6: Small Step Operational Semantics

Small step operational semantics specifies how programs execute 1 step at a time.

SSOS makes it easier to reason about complex features are we have a much more detailed picture of the execution.

Big Step Operational Semantics hides non-termination, SSOS does not.

Once again with the λ\lambda-Calculus our states are λ\lambda-terms and transitions are provided by inference rules. Our values are λ\lambda abstractions of the form λx.M)\lambda x.M) and are denoted as VV.

Rules:

β\beta rule:

(λx.M)VvM[x\V]β\overline{( \lambda x.M)V \rarr_v M[x \backslash V]}^\beta

Essentially applies a β\beta reduction.

Note the difference in arrow with     \implies used for big step operational semantics and \rarr used for SSOS

Context Rules:

MvPMNvPNAPP1\frac{M \rarr_v P}{M N \rarr_v P N} \texttt{APP}_1

MvPVMvVPAPP2\frac{M \rarr_v P}{V M \rarr_v V P}\texttt{APP}_2

We use v\rarr^*_v as the reflexive and transitive closure of v\rarr_v

Evaluation Strategy

As in Big step operational semantics, we can employ different evaluation strategies. Above, we have used call by value or eager evaluation. Below are the rules for call by name or lazy evaluation.

The β\beta Rule:

(λx.M)NnM[x\N]β\overline{(\lambda x.M)N \rarr_n M[x \backslash N]}^\beta

Context Rule:

MnPMNnPNAPP\frac{M \rarr_n P}{M N \rarr_n P N}\texttt{APP}

Again, we define n\rarr^*_n as the reflexive and transitive closure of n\rarr_n
MnN    (M=NP.MnPPnN)M \rarr^*_n N \iff (M = N \cup \exists P. M \rarr_n P \cap P \rarr^*_n N)

Properties

Values:

If MvVM \rarr^*_v V and VV is a value then M    vVM \implies_v V
If MnVM \rarr^*_n V and VV is a value then M    nVM \implies_n V

Deterministic:

If MvNM \rarr_v N and MvPM \rarr_v P then N=PN = P
If MnNM \rarr_n N and MnPM \rarr_n P then N=PN = P

β\beta-equality

If MvNM \rarr_v N then M=βNM =_\beta N
If MnNM \rarr_n N then M=βNM =_\beta N

Evaluation Contexts

The untyped pure λ\lambda-Calculus Small step semantics has only:

However, other languages can, and do, have many more.

Evaluation context provides a way to compactly express context rules.

An evaluation context is a term with a special variable \bullet called a hole

If C is an evaluation context, then C[M]C[M] represents CC with MM substituted for \bullet. For example:

((λx.x))[M]=(λx.x)M((\lambda x.x)\bullet)[M] = (\lambda x.x)M

Every evaluation context CC represents a context rule.

MvNC[M]vC[N]\frac{M \rarr_v N}{C[M] \rarr_v C[N]}

expressing that MM can be reduced to NN in the context CC where CC is defined in the form:

C::=___C ::= \_ | \_ | \_ | \ldots

Evaluation Contexts for the λ\lambda-Calculus

Values (λ\lambda abtractions of the form λx.M\lambda x.M) are denoted by VV

Call-by-value evaluation contexts:

C::=CMVCC ::= \bullet | C M | V C

Call-by-name evaluation contexts:

C::=CMC ::= \bullet | C M

Using these evaluation contexts, we can redefine the small-step semantics of the untyped pure λ\lambda-Calculus as:

Call-by-value

(λx.M)VvM[x\V]β\overline{(\lambda x.M)V \rarr_v M[x\backslash V]}^\beta

MvNC[M]vC[N]APPC\frac{M \rarr_v N}{C[M] \rarr_v C[N]}\texttt{APP}_C

Call-by-name

(λx.M)NnM[x\N]β\overline{(\lambda x.M)N \rarr_n M[x\backslash N]}^\beta

MnNC[M]nC[N]APPC\frac{M \rarr_n N}{C[M] \rarr_n C[N]}\texttt{APP}_C

Note: although these look the same, the definition of CC for each is different. resulting in different rules

Normal Forms

A normal form, with respect to a transition system, is a form that cannot be reduced further.

A term that has a normal form is said to converge otherwise they are said to diverge.

A normal form that is not a value is said to be stuck e.g. xx or xxx x or x(λx.x)x(\lambda x.x) as values are defined as λx.M\lambda x.M

Theorem:

If a term converges using call-by-value then it converges using call-by-name, although not necessarily to the same normal form.

Normal Order Reduction

Theorem:

If MM is β\beta-equal to a β\beta-normal form NN, then the normal order reduction reduces MM to NN

Theorem

Call-by-name converges whenever normal order converges

Normal order reduction is defined as:

x    nox\underline{x \implies_{no} x }
M    noNλx.M    noλx.N\frac{M \implies_{no} N}{\lambda x.M \implies_{no} \lambda x.N} \\
M    nλx.P      P[x\N]    noQMN    noQ\frac{M \implies_{n} \lambda x.P \;\;\; P[x\backslash N] \implies_{no} Q}{M N \implies_{no} Q}
M    nPλx.T      M    noQ      N    noRMN    noQR\frac{ M \implies_{n} P \neq \lambda x.T \;\;\; M \implies_{no} Q \;\;\; N \implies_{no} R }{ M N \implies_{no} Q R }