04Typing and Denotational Semantics of simPL CS4215: Programming - - PowerPoint PPT Presentation

04 typing and denotational semantics of simpl
SMART_READER_LITE
LIVE PREVIEW

04Typing and Denotational Semantics of simPL CS4215: Programming - - PowerPoint PPT Presentation

Typing of simPL Denotational Semantics of simPL 04Typing and Denotational Semantics of simPL CS4215: Programming Language Implementation Martin Henz February 3, 2012 Generated on Thursday 2 February, 2012, 18:17 CS4215: Programming


slide-1
SLIDE 1

Typing of simPL Denotational Semantics of simPL

04—Typing and Denotational Semantics of simPL

CS4215: Programming Language Implementation

Martin Henz

February 3, 2012

Generated on Thursday 2 February, 2012, 18:17 CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-2
SLIDE 2

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

1 Typing of simPL

Type Environments Typing Relation for simPL Type Safety of simPL

2 Denotational Semantics of simPL

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-3
SLIDE 3

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Example

Is x + 3 well-typed?

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-4
SLIDE 4

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Type Environments

A Type environment, denoted by Γ, keeps track of the type of indentifiers appearing in the expression. Γ(x) returns the type that is known by environment Γ for the identifier x.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-5
SLIDE 5

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Environment Extension

If Γ[x ← t]Γ′, then Γ′ behaves like Γ, except that the type of x is t.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-6
SLIDE 6

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Example

Let Γ = ∅. ∅[AboutPi ← int]Γ′ Γ′(AboutPi) = int Γ′[Square ← int -> int]Γ′′ dom(Γ′′) = {AboutPi, Square}

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-7
SLIDE 7

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Typing Relation

The set of well-typed expressions is defined by the ternary typing relation, written Γ ⊢ E : t, where Γ is a type environment such that E ⊲ ⊳ X and X ⊆ dom(Γ). “The expression E has type t, under the assumption that its free identifiers have the types given by Γ.”

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-8
SLIDE 8

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Examples

Γ′ ⊢ AboutPi ∗ 2 : int Γ′′ ⊢ fun{int -> int} x -> AboutPi * (Square 2) end : int -> int

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-9
SLIDE 9

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Examples

Γ′ ⊢ fun {int -> int} x -> AboutPi * (Square 2) end : int -> int does not hold, because Square occurs free in the expression, but the type environment Γ′ to the left of the ⊢ symbol is not defined for Square.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-10
SLIDE 10

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation

Γ ⊢ x : Γ(x) [VarT] If Γ(x) is not defined, then this rule is not applicable. In this case, we say that there is no type for x derivable from the assumptions Γ.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-11
SLIDE 11

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

Γ ⊢ n : int [NumT] Γ ⊢ true : bool [TrueT] Γ ⊢ false : bool [FalseT]

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-12
SLIDE 12

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

For each primitive operation p that takes n arguments of types t1, ..., tn and returns a value of type t, we have exactly one rule of the following form. Γ ⊢ E1 : t1 · · · Γ ⊢ En : tn Γ ⊢ p[E1, . . . , En] : t [PrimT]

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-13
SLIDE 13

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

p t1 t2 t + int int int

  • int

int int * int int int / int int int & bool bool bool | bool bool bool \ bool bool = int int bool < int int bool > int int bool

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-14
SLIDE 14

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

Γ ⊢ E : bool Γ ⊢ E1 : t Γ ⊢ E2 : t Γ ⊢ if E then E1 else E2 end : t

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-15
SLIDE 15

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

Γ1[x1 ← t1]Γ2 · · · Γn[xn ← tn]Γn+1 Γn+1 ⊢ E : t Γ1 ⊢ fun {t1 ∗ · · · ∗ tn -> t} x1 . . . xn -> E end : t1 ∗ · · · ∗ tn -> t

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-16
SLIDE 16

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

Γ[f ← t1 ∗ · · · ∗ tn -> t]Γ1 Γ1[x1 ← t1]Γ2 · · · Γn[xn ← tn]Γn+1 Γn+1 ⊢ E : t Γ ⊢ recfun f {t1 ∗ · · · ∗ tn -> t} x1 . . . xn -> E end : t1 ∗ · · · tn -> t

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-17
SLIDE 17

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Typing Relation (cont’d)

Γ ⊢ E : t1 ∗ · · · ∗ tn -> t Γ ⊢ E1 : t1 · · · Γ ⊢ En : tn Γ ⊢ (E E1 · · · En) : t

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-18
SLIDE 18

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Well-Typedness

An expression E is well-typed, if there is a type t such that E : t.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-19
SLIDE 19

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Example Proof

∅ ⊢ 2 : int ∅ ⊢ 3 : int ∅ ⊢ 2*3 : int ∅ ⊢ 7 : int ∅ ⊢ 2*3>7 : bool

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-20
SLIDE 20

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Example Proof

∅[x ← int]Γ Γ ⊢ x : int Γ ⊢ 1 : int Γ ⊢ x+1 : int ∅ ⊢ fun {int -> int} x -> x+1 end : int -> int ∅ ⊢ 2 : int ∅ ⊢ (fun {int -> int} x -> x+1 end 2) : int

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-21
SLIDE 21

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Unique Type

Lemma For every expression E and every type assignment Γ, there exists at most one type t such that Γ ⊢ E : t.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-22
SLIDE 22

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

More Properties of Typing Relation

Lemma Typing is not affected by “junk” in the type assignment. If Γ ⊢ E : t, and Γ ⊂ Γ′, then Γ′ ⊢ E : t. Lemma Substituting an identifier by an expression of the same type does not affect typing. If Γ[x ← t′]Γ′, Γ′ ⊢ E : t, and Γ ⊢ E ′ : t′, then Γ ⊢ E ′′ : t, where E[x ← E ′]E ′′.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-23
SLIDE 23

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Type Safety

Type safety is a property of a given language with a given static and dynamic semantics. It says that if a program of the languge is well-typed, certain problems are guaranteed not to occur at runtime. What do we consider as “problems”?

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-24
SLIDE 24

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Components of Type Safety

  • Progress. Well-typed expressions are values or can be further

evaluated.

  • Preservation. Well-typed expressions do not change their type

during evaluation.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-25
SLIDE 25

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Definition of Type Safety

A programming language with a given typing relation · · · ⊢ · · · : · · · and one-step evaluation → is called type-safe, if the following two conditions hold:

1 Preservation. If E is a well-typed program with respect to

· · · ⊢ · · · : · · · and E → E ′, then E ′ is also a well-typed program with respect to ⊢.

2 Progress. If E is a well-typed program, then either E is a

value or there exists a program E ′ such that E → E ′.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-26
SLIDE 26

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Preservation in simPL

If for a simPL expression E and some type t holds E : t and if E →simPL E ′, then E ′ : t.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-27
SLIDE 27

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Progress in simPL

Let simPL’ be simPL without division. If for a simPL’ expression E holds E : t for some type t, then either E is a value, or there exists an expression E ′ such that E →simPL’ E ′.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-28
SLIDE 28

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Is perfect typing possible?

The type safety of simPL’ ensures that evaluation of a well-typed simPL’ expression does not get stuck. Can we say the reverse by claiming that any expression for which the dynamic semantics produces a value is well-typed?

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-29
SLIDE 29

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Is perfect typing possible?

Unfortunately, some expressions produce a value although they are not well-typed. Example: if true then 1 else false end Interesting to note: Perfect typing is not possible for languages such as simPL’.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-30
SLIDE 30

Typing of simPL Denotational Semantics of simPL Type Environments Typing Relation for simPL Type Safety of simPL

Stepping back

Summary so far Typing allows us to focus on well-typed programs Well-typed programs “behave well” (progress, preservation) Outlook We will focus on well-typed programs and develop a semantics that eliminates many of the efficiency and engineering issues encountered with dynamic semantics.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-31
SLIDE 31

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

1 Typing of simPL 2 Denotational Semantics of simPL

Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-32
SLIDE 32

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

A Critique of Current Approach

Contraction relies substitution; mathematically rather complex. Primitive operations that are not total functions, such as division, make the evaluation process get stuck. We want a more explicit way of handling runtime errors. simPL contains many “unreasonable” programs, which complicates the definition of a dynamic semantics Dynamic semantics cannot be extended easily to other language paradigms such as imperative programming.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-33
SLIDE 33

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

A Glimpse of Denotational Semantics

E1 ֌ v1 E2 ֌ v2 +[E1, E2] ֌ v1 + v2 syntactic domains semantic domains semantic functions

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-34
SLIDE 34

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Sublanguages of simPL

simPL0; integer and boolean expressions simPL1; add let and if simPL2; add division simPL3; add functions simPL4; add recursive functions

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-35
SLIDE 35

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Syntactic Domain of simPL0

n true false E1 E2 p[E1, E2] p ∈ {|,&,+, -,*,=,>,<} E p[E] p ∈ {\}

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-36
SLIDE 36

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Domains

Domain name Definition Explanation Bool {true, false} ring of booleans Int {. . . , −2, −1, 0, 1, 2, . . .} ring of integers EV Bool + Int expressible values Expressible values EV are values that are the result of evaluating an expression.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-37
SLIDE 37

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Function

The semantic function · ֌ · : simPL0 → EV expresses the meaning of elements of simPL0, by defining the value of each element.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-38
SLIDE 38

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Rules

true ֌ true false ֌ false n ֌N i n ֌ i The function ֌N transforms the simPL0 integer syntax into an element of Int.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-39
SLIDE 39

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Rules (cont’d)

E1 ֌ v1 E2 ֌ v2 E1+E2 ֌ v1 + v2 E1 ֌ v1 E2 ֌ v2 E1-E2 ֌ v1 − v2 E1 ֌ v1 E2 ֌ v2 E1*E2 ֌ v1 · v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-40
SLIDE 40

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Rules (cont’d)

E1 ֌ v1 E2 ֌ v2 E1&E2 ֌ v1 ∧ v2 E1 ֌ v1 E2 ֌ v2 E1|E2 ֌ v1 ∨ v2 E ֌ v \E ֌ ¬v E1 ֌ v1 E2 ֌ v2 E1=E2 ֌ v1 ≡ v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-41
SLIDE 41

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Rules (cont’d)

E1 ֌ v1 E2 ֌ v2 E1>E2 ֌ v1 > v2 E1 ֌ v1 E2 ֌ v2 E1<E2 ֌ v1 < v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-42
SLIDE 42

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Example

1 + 2 > 3 ֌ false holds because 1 + 2 ֌ 3 and 3 > 3 is false.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-43
SLIDE 43

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

simPL1

Add the following to simPL0: conditionals, identifiers, let

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-44
SLIDE 44

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Syntactic Domain of simPL1

E E1 E2 if E then E1 else E2 end x E E1 . . . En let x1 = E1 · · · xn = En in E end

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-45
SLIDE 45

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Motivation: Semantic Domains of simPL1

We need to introduce environments that allow us to keep track of the binding of identifiers. These environments map identifiers to denotable values.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-46
SLIDE 46

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Domains of simPL1

Semantic domain Definition Explanation Bool {true, false} ring of booleans Int {. . . , −2, −1, 0, 1, 2, . . .} ring of integers EV Bool + Int expressible values DV Bool + Int denotable values Id alphanumeric string identifiers Env Id DV environments

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-47
SLIDE 47

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Operations on Environments

We introduce an operation ∆[x ← v], which denotes an environment that works like ∆, except that ∆[x ← v](x) = v

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-48
SLIDE 48

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Functions for simPL1

Define · ֌ · using auxiliary semantic function · · ֌ · that gets an environment as additional argument. · ֌ · : simPL1 → EV ∅ E ֌ v E ֌ v

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-49
SLIDE 49

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1

· · ֌ · : Env ∗ simPL1 → EV

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-50
SLIDE 50

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆ true ֌ true ∆ false ֌ false n ֌N i ∆ n ֌ i ∆ x ֌ ∆(x)

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-51
SLIDE 51

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1+E2 ֌ v1 + v2 ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1-E2 ֌ v1 − v2 ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1*E2 ֌ v1 · v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-52
SLIDE 52

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1&E2 ֌ v1 ∧ v2 ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1|E2 ֌ v1 ∨ v2 ∆ E ֌ v ∆ \E ֌ ¬v ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1=E2 ֌ v1 ≡ v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-53
SLIDE 53

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1>E2 ֌ v1 > v2 ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1<E2 ֌ v1 < v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-54
SLIDE 54

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆ E ֌ true ∆ E1 ֌ v1 ∆ if E then E1 else E2 end ֌ v1 ∆ E ֌ false ∆ E2 ֌ v2 ∆ if E then E1 else E2 end ֌ v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-55
SLIDE 55

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function for simPL1 (cont’d)

∆[x1 ← v1] · · · [xn ← vn] E ֌ v ∆ E1 ֌ v1 · · · ∆ En ֌ vn ∆ let x1 = E1 · · · xn = En in E end ֌ v

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-56
SLIDE 56

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Example

· · · ∅[AboutPi ← 3] AboutPi + 2 ֌ 5 ∅ let AboutPi = 3 in AboutPi + 2 end ֌ 5 let AboutPi = 3 in AboutPi + 2 end ֌ 5

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-57
SLIDE 57

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Syntactic Domain of simPL2

The language simPL2 adds division to simPL1. E1 E2 E1/E2 The difficulty lies in the fact that division on integers is a partial function, not being defined for 0 as second argument.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-58
SLIDE 58

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Domains of simPL2

Domain name Definition Explanation Bool {true, false} ring of booleans Int {. . . , −2, −1, 0, 1, 2, . . .} ring of integers EV Bool + Int + {⊥} expressible values DV Bool + Int denotable values Id alphanumeric string identifiers Env Id DV environments

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-59
SLIDE 59

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Modify Auxiliary Semantic Function

The semantic function · · ֌ · is modified to take the occurrence

  • f the error value ⊥ into account.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-60
SLIDE 60

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function (cont’d)

∆ E1 ֌ ⊥ ∆ E1+E2 ֌ ⊥ ∆ E2 ֌ ⊥ ∆ E1+E2 ֌ ⊥ ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1+E2 ֌ v1 + v2 if v1, v2 = ⊥

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-61
SLIDE 61

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function (cont’d)

∆ E1 ֌ ⊥ ∆ E1/E2 ֌ ⊥ ∆ E2 ֌ ⊥ ∆ E1/E2 ֌ ⊥ ∆ E2 ֌ 0 ∆ E1/E2 ֌ ⊥ ∆ E1 ֌ v1 ∆ E2 ֌ v2 ∆ E1/E2 ֌ v1/v2 if v1, v2 = ⊥ and v2 = 0

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-62
SLIDE 62

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function (cont’d)

∆ E ֌ ⊥ ∆ if E then E1 else E2 end ֌ ⊥ ∆ E ֌ true ∆ E1 ֌ v1 ∆ if E then E1 else E2 end ֌ v1 ∆ E ֌ false ∆ E2 ֌ v2 ∆ if E then E1 else E2 end ֌ v2

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-63
SLIDE 63

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function (cont’d)

∆ Ei ֌ ⊥ ∆ let x1 = E1 · · · xn = En in E end ֌ ⊥ for i, 1 ≤ i ≤ n ∆[x1 ← v1] · · · [xn ← vn] E ֌ v ∆ E1 ֌ v1 · · · ∆ En ֌ vn ∆ let x1 = E1 · · · xn = En in E end ֌ v

  • tw’

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-64
SLIDE 64

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Example

For any environment ∆, ∆ 5+(3/0) ֌ ⊥, since ∆ 3/0 ֌ ⊥.

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-65
SLIDE 65

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Syntactic Domain of simPL3

Add (non-recursive) function definition and application. E fun { t } x1 . . . xn -> E end E E1 · · · En (E E1 . . . En)

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-66
SLIDE 66

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Semantic Domains of simPL3

Domain name Definition Explanation Bool {true, false} ring of booleans Int {. . . , −2, −1, 0, 1, 2, . . .} ring of integers EV Bool + Int + {⊥} + Fun expressible values DV Bool + Int + Fun denotable values Id alphanumeric string identifiers Env Id DV environments Fun DV ∗ · · · ∗ DV EV function values

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-67
SLIDE 67

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Auxiliary Semantic Function (cont’d)

∆ fun { t } x1 . . . xn -> E end ֌ f f is function such that f (y1, . . . , yn) = v, where ∆[x1 ← y1] · · · [xn ← yn] E ֌ v

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-68
SLIDE 68

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Evaluation of Function Application

∆ E ֌ f ∆ E1 ֌ v1 · · · ∆ En ֌ vn ∆ (E E1 . . . En) ֌ f (v1, . . . , vn)

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-69
SLIDE 69

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Syntactic Domain of simPL4

E recfun f { t } x1 . . . xn -> E end

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-70
SLIDE 70

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Discussion simPL4

We would like to add the following rule to our definition of ֌. ∆ recfun g { t } x1 . . . xn -> E end ֌ f f is function such that f (y1, . . . , yn) = v, where ∆[x1 ← y1] · · · [xn ← yn] [g ← f ] E ֌ v

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-71
SLIDE 71

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Problem

The symbol f occurs on the right hand side of the definition of f . How do we know that such a function f exists? Some expressions have unique solutions for f , others have multiple solutions. Example: (recfun f {int -> int} x -> (f x) end 0) Theory of fixpoints . . .

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

slide-72
SLIDE 72

Typing of simPL Denotational Semantics of simPL Denotational Semantics of simPL0 Denotational Semantics of simPL1 Denotational Semantics of simPL2 Denotational Semantics of simPL3 simPL4

Overview of Next Lecture

Friday 10/2: A Virtual Machine for simPL

CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL