Dynamics Semantics - Lecture Slides | ECEN 5023, Study notes of Electrical and Electronics Engineering

Dynamic Semantics Material Type: Notes; Class: SPECIAL TOPICS; Subject: Electrical & Computer Engineering; University: University of Colorado - Boulder; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-6zt
koofers-user-6zt 🇺🇸

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dynamic Semantics
The dynamic semantics of a language defines what happens
when you run a program.
There are many approaches to defining the dynamic semantics of a
language:
ISmall-step operational semantics
IBig-step operational semantics
IAbstract machines
ITranslation to another language
IDenotational semantics (translation to a mathematical
system)
IAxiomatic semantics
In this class we will primarily be concerned with the small-step
operational semantics, which is the most widely used approach.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Dynamics Semantics - Lecture Slides | ECEN 5023 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Dynamic Semantics

The dynamic semantics of a language defines what happens when you run a program. There are many approaches to defining the dynamic semantics of a language: I (^) Small-step operational semantics I (^) Big-step operational semantics I (^) Abstract machines I (^) Translation to another language I (^) Denotational semantics (translation to a mathematical system) I (^) Axiomatic semantics

In this class we will primarily be concerned with the small-step operational semantics, which is the most widely used approach.

Recall the language of Arithmetic Expressions

e ::= true | false | if e then e else e | 0 | succ e | pred e | iszero e

v ::= true | false | nv nv ::= 0 | succ nv

Definition of small-step semantics

We define a relation between two expressions that says when one expression evaluates to the other expression. We’ll call this the ReducesTo relation and define the relation inductively with the following rules. The first bunch of rules do some real computation

(1) (pred 0 , 0) ∈ ReducesTo (2) (pred succ nv , nv ) ∈ ReducesTo

(3) (iszero succ nv , false) ∈ ReducesTo

(4) (iszero 0 , true) ∈ ReducesTo

(5) (if true then e 2 else e 3 , e 2 ) ∈ ReducesTo

(6) (if false then e 2 else e 3 , e 3 ) ∈ ReducesTo

Some notation

Before defining the rest of the rules for ReducesTo, we introduce the following notation:

e 7 −→ e′^ ≡ (e, e′) ∈ ReducesTo

Inversion Lemmas

Suppose you know that for some e and e′, e 7 −→ e′. What further information can you glean from this based on the rules for ReducesTo? We can do case analysis on the rules. For example, suppose

if true then e 2 else e 3 7 −→ e 4

Then you know that e 2 = e 4 because the only rule that could have appeared as the final step in the derivation is rule (5). Why not rule (10)? Because then there would need to be a rule of the form true 7 −→ −, but there is no such rule.

A Proof by Rule Induction on ReducesTo

Theorem

If e 7 −→ e′^ and e 7 −→ e′′^ then e′^ = e′′. Proof by rule induction on e 7 −→ e′. Case (1) pred 0 7 −→ 0 : So e = pred 0 , e′^ = 0 and by assumption we have pred 0 7 −→ e′′. There are two rules that could have applied (1) and (8), but (8) can’t apply because there are no rules of the form 0 7 −→ −. Thus, e′′^ = 0 and we conclude that e′^ = e′′.

More Definitions

Definition

An expression e is in normal form if 6 ∃e′. e 7 −→ e′.

Definition

Multi-step reduction (written 7 −→∗) is the relation inductively defined by the following rules:

(1) e 7 −→∗^ e (2) e 1 7 −→ e 2 e 2 7 −→∗^ e 3 e 1 7 −→∗^ e 3

(This is an alternate but equivalent definition than the one in the textbook.)

The Untyped Lambda Calculus

Syntax: x Variables e ::= x | (λx.e) | (e e) Expressions v ::= λx.e Values

I (^) An expression of the form (λx. e) create an anonymous function. The x is the one parameter of the function and e is the function body. I (^) An expression of the form (e 1 e 2 ) is a function call. The expression e 1 should evaluate to a function, which is then called using the result of e 2 as the argument.

α-equivalence

The variable name associated with a λ doesn’t really matter. The meaning of the program remains unchanged if you change the variable name and consistently replace the occurrences of that variable in the λ.

λy .e = λz.[y 7 → z]e provided z is not in e

Substitution and Free Variables

We write [x 7 → e′]e to say that e′^ is substituted for the free occurrences of x in e. For example, in the following we substitute the expression (y z) for the variable x in the expression ((λx. x) x). [x 7 → (y z)]((λx. x) x) = ((λx. x) (y z)) A free occurrence of a variable x within an expression e is an occurrence of x in e that does not have a surrounding λ in e that binds x. The the following examples, free occurrences are enclosed in a box.

I (^) ( z x ) I (^) (λ y. y) I (^) (λ y. (y x )) I (^) ((λ x. x) x ) I (^) y

Capture-avoiding Substitution

I (^) We need to be careful when defining substitution ([x 7 → e′]e) to make sure that free variables in e′^ don’t get captured by λs in e. I (^) The following is an example of what happens if you don’t define substitution correctly:

(λy. (λx. λy. x) y ) −→ (λy. (λy. y ))

I (^) Here’s a correct definition of substitution:

[x 7 → e′]y = if x = y then e′^ else y [x 7 → e′](λy. e) = (λz.[x 7 → e′][y 7 → z]e) (where z is fresh) [x 7 → e′](e 1 e 2 ) = ([x 7 → e′]e 1 [x 7 → e′]e 2 )

Evaluation Contexts

Congruence rules are a verbose way to specify evaluation order. A more succinct way is to use evaluation contexts. Recall the small-step textual rewriting view of semantics: if iszero (pred (succ 0)) then 0 else succ 0 7 −→ if iszero 0 then 0 else succ 0 An evaluation context is everything not inside an evaluation box. We can use a grammar to define where the boxes are allowed and what the surrounding evaluation context looks like.

E ::= [] The evaluation box forms a hole in the context. (E e) (v E )

Small-step Semantics with Evaluation Contexts

We separate out the computational reduction rules into the relation −→. For the lambda calculus there is just one rule:

(β) ((λx.e 1 ) v 2 ) −→ [x 7 → v 2 ]e 1

We then define top-level reduction using evaluation contexts:

e −→ e′ E [e] 7 −→ E [e′]