Objectives You should be able to... In order to express the meaning - - PowerPoint PPT Presentation

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives You should be able to... In order to express the meaning - - PowerPoint PPT Presentation

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser Introduction Formal Systems A Simple Imperative Programming Language Church Rosser Objectives You should be able to... In order to express the meaning of a


slide-1
SLIDE 1

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Small Step Semantics

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Objectives

You should be able to...

In order to express the meaning of a program, we need a formal language to capture these meanings. Today’s semantics will use transitions to specify the value of an expression. By the end of lecture, you should know how to use transitional semantics.

◮ what the word “semantics” means. ◮ determine the value of an expression (i.e., be able to read) ◮ specify the meaning of a language (i.e., be able to write).

You should also know the Church-Rosser property and be able to give examples of languages that have it and languages that don’t have it.

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Parts of a Formal System

To create a formal system, you must specify the following:

◮ A set of symbols or an alphabet. ◮ A defjnition of a valid sentence. ◮ A set of transformation rules to make new valid sentences out of old

  • nes.

◮ A set of initial valid sentences.

You do NOT need:

◮ An interpretation of those symbols.

They are highly recommended, but the formal system can exist and do its work without one.

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example

Symbols S, (, ), Z, P, x, y. Defjnition of a furbitz

◮ Z is a furbitz. x and y are variables of type furbitz. ◮ if x is a furbitz, then S(x) is a furbitz. ◮ if x and y are furbitzi, then P(x, y) is a furbitz.

Defjnition of the gloppit relation

◮ Z has the gloppit relation with Z. ◮ If x and y have the gloppit relation, then S(x) and

S(y) have the gloppit relation.

◮ If α and β, then we can write αgβ.

True Sentences If αgβ, then also

◮ P(S(α), β)gS(P(α, β)),

and P(Z, β)gβ

slide-2
SLIDE 2

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example

Symbols S, (, ), Z, P, x, y. Defjnition of an integer

◮ 1 is an integer. x and y are variables of type integer. ◮ if x is an integer, then S(x) is an integer. ◮ if x and y are integers, then P(x, y) is an integer.

Defjnition of the equality relation

◮ 1 has the equality relation with 1. ◮ If x and y have the equality relation, then S(x) and

S(y) have the equality relation.

◮ If α and β, then we can write α = β.

True Sentences If α = β, then also

◮ P(S(α), β) = S(P(α, β)),

and P(1, β) = β

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Grammar for Simple Imperative Programming Language

The Language

S ::= skip | u := t | S1; S2 | if B then S1 else S2 fi | while B do S1 od

◮ Let u be a possibly subscripted variable. ◮ Let t be an expression of some sort. ◮ Let B be a boolean expression.

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Transistions

◮ There are many ways we can specify the meaning of an expression.

One way is to specify the steps that the computer will take during an evaluation.

◮ A transition has the following form:

< S1, σ >→< S2, τ > where S1 and S2 are statements, and σ and τ represent

  • environments. The statement could change the environment.

◮ Note well: → indicates exactly one step of evaluation. (Hence

“small step semantics”)

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Defjnition of →

Skip and Assignment

< skip , σ >→< E, σ > < u := t, σ >→< E, σ[u := σ(t)] >

◮ σ will have the form {u1 := t1, u2 := t2, . . . , un := tn} ◮ If σ = {x := 5}, then we can say σ(x) = 5 ◮ We can update σ.

σ[x := 20] = {x := 20} σ[y := 20] = {x := 5, y := 20}

slide-3
SLIDE 3

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Defjnition of →

Sequencing

< S1, σ >→< S2, τ > < S1; S, σ >→< S2; S, τ > E; S ≡ S

◮ Notice how we don’t talk about the second statement at all!

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Defjnition of →

If

< if B then S1 else S2 fi , σ >→< S1, σ > where σ | = B < if B then S1 else S2 fi , σ >→< S2, σ > where σ | = ¬B

◮ The notation σ |

= B means “B is true given variable assignments in σ.”

◮ {x := 20, y := 30} |

= x < y

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Defjnition of →

While

< while B do S1 od , σ >→< S1; while B do S1 od , σ > where σ | = B < while B do S1 od , σ >→< E, σ > where σ | = ¬B

◮ Notice how the body of the while loop is copied in front of the loop!

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} >

slide-4
SLIDE 4

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} >

slide-5
SLIDE 5

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 6, n := 2} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 6, n := 2} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 6, n := 1} >

slide-6
SLIDE 6

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example Evaluation

Evaluate: x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od < x:=1; n:=3; while n>1 do x:=x*n; n:=n-1 od, {} > → < n:=3; while n>1 do x:=x*n; n:=n-1 od, {x := 1} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 1, n := 3} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 3} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < x:=x*n;n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 3, n := 2} > → < n:=n-1;while n>1 do x:=x*n; n:=n-1 od, {x := 6, n := 2} > → < while n>1 do x:=x*n; n:=n-1 od, {x := 6, n := 1} > → < E, {x := 6, n := 1} >

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Other Notations

Notations

→0 ≡ The identity →1 ≡ → →n ≡ → · →n−1 →∗ ≡ ∞

i=0 →i

→+ ≡ ∞

i=1 →i

a ← b ≡ b → a ↔ ≡ → ∪ ← ↔∗ ≡ (→ ∪ ←)∗

Example

3 →∗ 3, and if 3 > 2 then 5 + 9 else 2 * 4 →∗ 14

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Be careful with ↔∗

a ↔∗ b ≡ a ←∗ b ∪ a →∗ b For example a ↔∗ b when a ← a1 → a2 → a3 ← b2 ← b1 → b

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Term Rewriting Systems

Transition semantics can be thought of as a term-rewriting system. Common questions:

◮ Does an expression always terminate? ◮ Can we tell if two expressions are equal?

Church-Rosser Property: If x ↔∗ y then x and y normalize to the same value. x z y ∗ ∗ ∗

slide-7
SLIDE 7

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Example

Confmuence

If x → y1 and x → y2 then y1 and y2 normalize to the same value. (Confmuence and the Church-Rosser Property coincide.) x = 2 + 3 + 5 x = 5 + 5 x = 2 + 8 x = 10 This is also known as the “diamond property”

Introduction Formal Systems A Simple Imperative Programming Language Church Rosser

Who has it?

◮ Alonzo Church and J. Barkley Rosser proved that the λ-calculus has

these properties in 1936.

◮ Very important for theorem provers. ◮ Most programming languages have this property... some of the

time...

◮ One Benefjt: you can check for equality of x and y by evaluating

them.