32

Type Systems

Embed Size (px)

Citation preview

Page 1: Type Systems
Page 2: Type Systems

Typing

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 2 / 30

Page 3: Type Systems

The Purpose of Typing

”Well-typed programs never go wrong.”

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 3 / 30

Page 4: Type Systems

What Does ”Go Wrong” Entail?

Execution errors.

Program crash.

Divergence.

. . .

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 4 / 30

Page 5: Type Systems

The Purpose of Typing (II)

As a side effect, typed programs are also more likely to actually do whatthey are supposed to do.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 5 / 30

Page 6: Type Systems

Two Kinds of Typing

Static typing.

Dynamic typing (more precisely: dynamic checking).

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 6 / 30

Page 7: Type Systems

Why Do They Both Exist?

It is provenly impossible to create a type system that rejects allincorrect programs (written in a real-world programming language)and accepts all correct ones.

Benefits vs. costs trade-off: static typing rejects at least someincorrect programs at the expense of not accepting certain correctones.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 7 / 30

Page 8: Type Systems

Why Do They Both Exist?

It is provenly impossible to create a type system that rejects allincorrect programs (written in a real-world programming language)and accepts all correct ones.

Benefits vs. costs trade-off: static typing rejects at least someincorrect programs at the expense of not accepting certain correctones.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 7 / 30

Page 9: Type Systems

Example

class Foo { public void urgh() { ...} }class Bar { public void urgh() { ...} }

void main() {Object var;

if ( something()) var = new Foo();

else var = new Bar();

var.urgh();

}

It is obvious that an object pointed at by var has method urgh, however,many type systems don’t provide any way of expressing it withoutmodifying (and recompiling) definitions of Foo and Bar.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 8 / 30

Page 10: Type Systems

Example

class Foo { public void urgh() { ...} }class Bar { public void urgh() { ...} }

void main() {Object var;

if ( something()) var = new Foo();

else var = new Bar();

var.urgh();

}

It is obvious that an object pointed at by var has method urgh, however,many type systems don’t provide any way of expressing it withoutmodifying (and recompiling) definitions of Foo and Bar.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 8 / 30

Page 11: Type Systems

Which Is Better?

Debating which is better may (and probably will) get you into aviolent flamewar.

The real question is whether there will ever be a type system that doesnot stand in programmers’ way and rejects all incorrect programs.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 9 / 30

Page 12: Type Systems

Relaxed Notions of Typing

Untrapped errors vs. trapped errors, forbidden errors.

Safe languages disallow untrapped errors (usually by a mixture ofstatic and runtime checks).

Strongly typed languages disallow forbidden errors (ditto).

Weakly typed languages allow even for untrapped errors.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 10 / 30

Page 13: Type Systems

Type Algebra

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 11 / 30

Page 14: Type Systems

Function Type

A→ B

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 12 / 30

Page 15: Type Systems

Product Type

A× B

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 13 / 30

Page 16: Type Systems

Intersection Type

A ∩ B

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 14 / 30

Page 17: Type Systems

Union Type

A ∪ B

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 15 / 30

Page 18: Type Systems

Type System

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 16 / 30

Page 19: Type Systems

Two Components of Type Control

Type system—a specification.

Type checking algorithm—an algorithm.

There may be zero or more than one distinct type checkingalgorithms for a particular type system.

Type systems described in a formal language have lowerprobability of ambiguities, unconsidered corner cases etc.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 17 / 30

Page 20: Type Systems

Example

begin

string s;

s = "abc";

string t;

t = s + "def";

end

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 18 / 30

Page 21: Type Systems

Grammar

P → begin CL end programCL→ C ; CL sequential composition

εC → T I declaration

I = E assignmentT → string types

intE → I identifier

N numeralS string literalE1 + E2 sum of two subexpressions

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 19 / 30

Page 22: Type Systems

Key Components of a Type System Specification

Judgements.

Type rules.

Environment.

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 20 / 30

Page 23: Type Systems

Type System

Empty Environment

∅ ` ◦

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 21 / 30

Page 24: Type Systems

Type System (II)

Variable Creation

I : string 6∈ Γ I : int 6∈ Γ Γ ` ◦Γ ∪ {I : string} ` ◦ Γ ∪ {I : int} ` ◦

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 22 / 30

Page 25: Type Systems

Type System (III)

Literals

Γ ` ◦Γ ` S : string

Γ ` ◦Γ ` N : int

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 23 / 30

Page 26: Type Systems

Type System (IV)

Variables in Environment

I : string ∈ Γ

Γ ` I : string

I : int ∈ Γ

Γ ` I : int

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 24 / 30

Page 27: Type Systems

Type System (V)

Sum

Γ ` E : string Γ ` F : string

Γ ` E + F : string

Γ ` E : int Γ ` F : int

Γ ` E + F : int

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 25 / 30

Page 28: Type Systems

Type System (VI)

Assignment

Γ ` I : string Γ ` E : string

Γ ` I = E : �Γ ` I : int Γ ` E : int

Γ ` I = E : �

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 26 / 30

Page 29: Type Systems

Type System (VII)

Sequential Composition

Γ ` ◦Γ ` ε : �

Γ ∪ {I : string} ` ◦ Γ ∪ {I : string} ` C : �Γ ` string I ;C : �

Γ ∪ {I : int} ` ◦ Γ ∪ {I : int} ` C : �Γ ` int I ;C : �

Γ ` C : � Γ ` CL : �Γ ` C ;CL : �

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 27 / 30

Page 30: Type Systems

Type System (VIII)

Program

∅ ` CL : �` begin CL end : �

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 28 / 30

Page 31: Type Systems

Other Uses of Type Information

Semantics.

Optimization.

Static analysis.

. . .

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 29 / 30

Page 32: Type Systems

See

Luca Cardelli. Type Systems. ACM Computing Surveys 28, 1 (March1996), 263-264. http://doi.acm.org/10.1145/234313.234418

Michal Pıse (CTU in Prague) Object Programming Lect. 1: Type Systems September 21, 2010 30 / 30