Type Systems

Preview:

Citation preview

Typing

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

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

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

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

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

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

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

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

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

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

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

Type Algebra

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

Function Type

A→ B

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

Product Type

A× B

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

Intersection Type

A ∩ B

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

Union Type

A ∪ B

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

Type System

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

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

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

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

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

Type System

Empty Environment

∅ ` ◦

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

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

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

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

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

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

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

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

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

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

Recommended