09The Language imPL CS 4215: Programming Language Implementation - - PowerPoint PPT Presentation

09 the language impl
SMART_READER_LITE
LIVE PREVIEW

09The Language imPL CS 4215: Programming Language Implementation - - PowerPoint PPT Presentation

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL 09The Language imPL CS 4215: Programming Language Implementation Martin Henz March 16, 2012 Generated on Thursday 15


slide-1
SLIDE 1

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL

09—The Language imPL

CS 4215: Programming Language Implementation

Martin Henz

March 16, 2012

Generated on Thursday 15 March, 2012, 22:56 CS 4215: Programming Language Implementation 09—The Language imPL

slide-2
SLIDE 2

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL

Introduction

simPL, rePL: an identifier refers to a value

  • nce computed, the value does not change

pass-by-need exploits this fact referential transparency good for formal reasoning

CS 4215: Programming Language Implementation 09—The Language imPL

slide-3
SLIDE 3

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL

Motivation

many algorithms are more natural when presented using random-access memory assigment allows to change the value stored in the memory location associated with an identifier imPL0 allows assignment imPL1 adds assignment to record properties many interesting variants...

CS 4215: Programming Language Implementation 09—The Language imPL

slide-4
SLIDE 4

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

1 imPL0

Syntax Examples Denotational Semantics

2 imPL1 3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL

CS 4215: Programming Language Implementation 09—The Language imPL

slide-5
SLIDE 5

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Syntax of imPL0

Assignment E x := E Sequence E1 E2 E1 ; E2 Loop E1 E2 while E1 do E2 end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-6
SLIDE 6

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Example

let x = 0 in x := 1; x := x + 2; x := x + 3; x end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-7
SLIDE 7

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Another Example

fun x -> let i = 1 f = 1 in while \ i > x do f := f * i; i := i + 1 end; f end end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-8
SLIDE 8

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Yet Another Example

let gcd = fun a b -> while \(a = b) do if a > b then a := a - b else b := b - a end end; a end in (gcd 6 10) end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-9
SLIDE 9

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Denotational Semantics: Then How?

let x = 0 in x := 1; x := x + 2; x := x + 3; x end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-10
SLIDE 10

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Denotational Semantics: Idea

identifiers refer to locations a store maps locations to values the store is passed to and returned from the semantic function

CS 4215: Programming Language Implementation 09—The Language imPL

slide-11
SLIDE 11

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Semantic Domains

Domain name Definition EV Int + Bool + Fun + {error} SV Int + Bool + Fun DV Loc Fun DV ∗ · · · ∗ DV ∗ Store (EV, Store) Store Loc SV Env Id DV

CS 4215: Programming Language Implementation 09—The Language imPL

slide-12
SLIDE 12

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Example

Let us say we have a store with the value 1 at location l Σ = ∅Store[l ← 1] and an environment that carries location l at identifier x ∆ = ∅Env[x ← l] Then we can access the value of x in the store as follows: Σ(∆(x)) = 1

CS 4215: Programming Language Implementation 09—The Language imPL

slide-13
SLIDE 13

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

The Main Semantic Function

· ֌ · : imPL0 → EV ∅Store | ∅Env E ֌ (v, Σ) E ֌ v · | · · ֌ · : Store ∗ Env ∗ imPL0 → EV ∗ Store

CS 4215: Programming Language Implementation 09—The Language imPL

slide-14
SLIDE 14

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Let Expressions

Σ′[l1 ← v1] | ∆[x1 ← l1] E ֌ (v, Σ′′) Σ | ∆ let x1 = E1 in E end ֌ (v, Σ′′) if Σ | ∆ E1 ֌ (v1, Σ′), and l1 is a new location, which means Σ′(l1) is undefined

CS 4215: Programming Language Implementation 09—The Language imPL

slide-15
SLIDE 15

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Identifiers

Σ | ∆ x ֌ (Σ(∆(x)), Σ)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-16
SLIDE 16

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Assignment

Σ | ∆ E ֌ (v, Σ′) Σ | ∆ x := E ֌ (v, Σ′[∆(x) ← v])

CS 4215: Programming Language Implementation 09—The Language imPL

slide-17
SLIDE 17

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Example

∅Store[l ← 1] | ∅Env[a← l] a := 2 ֌ (2, ∅Store[l ← 1][l ← 2]) The resulting store ∅Store[l ← 1][l ← 2]) is of course the same as ∅Store[l ← 2]. The original binding of l to 1 is overwritten by the new value 2.

CS 4215: Programming Language Implementation 09—The Language imPL

slide-18
SLIDE 18

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Function Definition

Σ | ∆ fun x -> E end ֌ (f , Σ) where f (l, Σ′) = (v′, Σ′′), where Σ′ | ∆[x ← l] E ֌ (v′, Σ′′)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-19
SLIDE 19

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Function Application

Σ | ∆ E1 ֌ (f , Σ′) Σ′ | ∆ E2 ֌ (v2, Σ′′) Σ | ∆ (E1 E2) ֌ f (l, Σ′′[l ← v2]) where l is a new location in Σ′′.

CS 4215: Programming Language Implementation 09—The Language imPL

slide-20
SLIDE 20

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

Sequence

Σ | ∆ E1 ֌ (v1, Σ′) Σ′ | ∆ E2 ֌ (v2, Σ′′) Σ | ∆ E1;E2 ֌ (v2, Σ′′)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-21
SLIDE 21

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

While Loop

Σ | ∆ E1 ֌ (v1, Σ′) Σ | ∆ while E1 do E2 end ֌ (true, Σ′) if v1 = false

CS 4215: Programming Language Implementation 09—The Language imPL

slide-22
SLIDE 22

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Examples Denotational Semantics

While Loop

Σ | ∆ E1 ֌ (v1, Σ′) Σ | ∆ while E1 do E2 end ֌ (v, Σ′′′) if v1 = true, where Σ′ | ∆ E2 ֌ (v2, Σ′′) and Σ′′ | ∆ while E1 do E2 end ֌ (v, Σ′′′)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-23
SLIDE 23

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

1 imPL0 2 imPL1

Syntax Denotational Semantics Example

3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL

CS 4215: Programming Language Implementation 09—The Language imPL

slide-24
SLIDE 24

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Record Property Assignment

E1 E2 E1.q:=E2

CS 4215: Programming Language Implementation 09—The Language imPL

slide-25
SLIDE 25

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Semantic Domains

Domain name Definition EV Int + Bool + Fun + Rec + {⊥} SV Int + Bool + Fun + Rec DV Loc Rec Id Loc

CS 4215: Programming Language Implementation 09—The Language imPL

slide-26
SLIDE 26

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Record Construction

Σ(0) | ∆ E1 ֌ (v1, Σ(1)) Σ(n−1) | ∆ En ֌ (vn, Σ(n)) Σ(0) | ∆ [q1:E1, . . . ,qn:En] ֌ (f , Σ′) where l1, . . . , ln are new locations in Σ(n), Σ′ = Σ(n)[l1 ← v1] · · · [ln ← vn], and f (qi) = li for 1 ≤ i ≤ n.

CS 4215: Programming Language Implementation 09—The Language imPL

slide-27
SLIDE 27

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Property Access

Σ | ∆ E ֌ (f , Σ′) Σ | ∆ E.q ֌ (Σ′(f (q)), Σ′)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-28
SLIDE 28

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Property Assignment

Σ | ∆ E1 ֌ (f , Σ′) Σ′ | ∆ E2 ֌ (v, Σ′′) Σ | ∆ E1.q:=E2 ֌ (v, Σ′′[f (q) ← v])

CS 4215: Programming Language Implementation 09—The Language imPL

slide-29
SLIDE 29

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

Java: Passing Objects to Methods

void f(MyClass myobject) { myobject.myfield = 1; myobject = new MyClass(); myobject.myfield = 2; }

CS 4215: Programming Language Implementation 09—The Language imPL

slide-30
SLIDE 30

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Syntax Denotational Semantics Example

imPL: Passing Records to Functions

let a = [Myfield: 0] in (fun b -> b.Myfield := 1; b := [Myfield: 2]; b.Myfield := 3 end a); a.Myfield end

CS 4215: Programming Language Implementation 09—The Language imPL

slide-31
SLIDE 31

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Pass-by-reference Pass-by-copy

1 imPL0 2 imPL1 3 Pass-by-reference, Pass-by-copy

Pass-by-reference Pass-by-copy

4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL

CS 4215: Programming Language Implementation 09—The Language imPL

slide-32
SLIDE 32

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Pass-by-reference Pass-by-copy

Pass-by-Reference

Σ | ∆ E1 ֌ (f , Σ′) Σ | ∆ (E1 x) ֌ f (∆(x), Σ′)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-33
SLIDE 33

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Pass-by-reference Pass-by-copy

Pass-by-Copy

Σ | ∆ E1 ֌ (f , Σ′) Σ′ | ∆ E2 ֌ (v2, Σ′′) Σ | ∆ (E1 E2) ֌ f (l, Σ′′′) if v2 ∈ Rec, where l, l′

1, . . . , l′ n are new locations in Σ′′,

{q1, . . . , qn} = dom(v2), Σ′′′ = Σ′′[l ← {(q1, l′

1), . . . , (qn, l′ n)}]

[l′

1 ← Σ′′(v2(q1))] . . . [l′ n ← Σ′′(v2(qn))]

CS 4215: Programming Language Implementation 09—The Language imPL

slide-34
SLIDE 34

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Standard Semantics Alternative Semantics

1 imPL0 2 imPL1 3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling

Standard Semantics Alternative Semantics

5 A Virtual Machine for imPL

CS 4215: Programming Language Implementation 09—The Language imPL

slide-35
SLIDE 35

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Standard Semantics Alternative Semantics

Division by Zero

Σ | ∆ E1 ֌ (v1, Σ′) Σ′ | ∆ E2 ֌ (0, Σ′′) Σ | ∆ E1/E2 ֌ (e, Σ′′) if v1 ∈ Exc and where e = [DivisionByZero:true], and e ∈ Exc

CS 4215: Programming Language Implementation 09—The Language imPL

slide-36
SLIDE 36

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Standard Semantics Alternative Semantics

Execution of try-expressions

Σ | ∆ E1 ֌ (v, Σ′) Σ | ∆ try E1 catch x with E2 end ֌ (v, Σ′) if v ∈ Exc Σ | ∆ E1 ֌ (v1, Σ′) Σ′[l ← v1] | ∆[x ← l] E2 ֌ (v2, Σ′′) Σ | ∆ try E1 catch x with E2 end ֌ (v2, Σ′′) if v1 ∈ Exc and l new location

CS 4215: Programming Language Implementation 09—The Language imPL

slide-37
SLIDE 37

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Standard Semantics Alternative Semantics

Rolling back the State

Σ | ∆ E1 ֌ (v1, Σ′) Σ[l ← v1] | ∆[x ← l] E2 ֌ (v2, Σ′′) Σ | ∆ try E1 catch x with E2 end ֌ (v2, Σ′′) if v1 ∈ Exc and l new location

CS 4215: Programming Language Implementation 09—The Language imPL

slide-38
SLIDE 38

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

1 imPL0 2 imPL1 3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL

Idea Assignment Sequences Loops

CS 4215: Programming Language Implementation 09—The Language imPL

slide-39
SLIDE 39

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Idea

Reuse heap for implementing imperative constructs

CS 4215: Programming Language Implementation 09—The Language imPL

slide-40
SLIDE 40

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Translation of Assignment

E ֒ → s x := E ֒ → s.ASSIGNS x

CS 4215: Programming Language Implementation 09—The Language imPL

slide-41
SLIDE 41

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Execution of Assignment

s(pc) = ASSIGNS x (os, pc, e, rs, h) ⇉s (os, pc + 1, e, rs, update(e, x, v, h′)) where (v, h′) = pop(os, h)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-42
SLIDE 42

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Translation of Sequences

E1 ֒ → s1 E2 ֒ → s2 E1;E2 ֒ → s1.POP.s2

CS 4215: Programming Language Implementation 09—The Language imPL

slide-43
SLIDE 43

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Implementation of Sequences

s(pc) = POP (os, pc, e, rs, h) ⇉s (os, pc + 1, e, rs, h′) where (v, h′) = pop(os, h)

CS 4215: Programming Language Implementation 09—The Language imPL

slide-44
SLIDE 44

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Translation of Loops

E1 ֒ → s1 E2 ֒ → s2 while E1 do E2 ֒ → s1.(JOFR |s2 + 3|).s2.POP. (GOTOR − (|s1| + 2 + |s2|)).LDCB true

CS 4215: Programming Language Implementation 09—The Language imPL

slide-45
SLIDE 45

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Idea Assignment Sequences Loops

Overview of Next Two Lectures

Object-oriented programming languages

defining an object system runtime support typing

Concurrent languages

CS 4215: Programming Language Implementation 09—The Language imPL