35
Universal Universal Types Types Report by Report by Matthias Horbach Matthias Horbach

Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity

Embed Size (px)

Citation preview

UniversalUniversal TypesTypes

Report byReport by

Matthias HorbachMatthias Horbach

ContentsContents

Types of Polymorphism

System F

Basic PropertiesErasureImpredicativityParametricity

Types of PolymorphismTypes of Polymorphism

Types of PolymorphismTypes of Polymorphism

polymorphism

universal(true)

ad hoc(apparent)

parametric

inclusion (new)

overloading

coercion

according to Strachey (1967)and Cardelli/Wegner (1985)

and others and in more complex relations…

Ad Hoc PolymorphismAd Hoc Polymorphism

OverloadingOverloading

one name for different functions

just a convenient syntax abbreviation

example:+: int int z.B. 1 + 2+: real real z.B. 1.0 + 2.0

Coercion and CastsCoercion and Casts

convert argument to fulfill requirements

examples:((real) 1) + 1.0 or 1 + 1.0

Operators only seem to be polymorphic!

Universal PolymorphismUniversal Polymorphism

Inclusion PolymorphismInclusion Polymorphism

one “object” belongs to many “classes”used in object oriented languages

modeled by subtypes

example:Dog, Bird Animal

Universal PolymorphismUniversal Polymorphism

Parametric PolymorphismParametric Polymorphism

Uniformity of type structure achieved by type parameters

examples:length (1::2::3::nil) //parameter: intlength (true::true::false::nil) //parameter: boollength (“hello”::“, ”::“world”::nil) //parameter: string

let-polymorphism:- ML, no polymorphic arguments- automatic type reconstruction possible (see Sven)

We will look at a system with explicit type annotation.

System FSystem F

System F (Context)System F (Context)

Polymorphic -calculus

Second order -calculus

Idea:do lambda abstraction over type variables, define functions over types

Girard (1972), motivation: logicsReynolds (1974), motivation: programming

System F (What’s new?)System F (What’s new?)

Extension of the simply typed -calculus:Abstraction and application also for types:

t ::= …X.t (type abstraction)t [T] (type application)

A new value:v ::= …

X.t (type abstraction value)

Added types:T ::= …

X (type variable)X.T (universal type)

Adjusted contexts: ::= …

, X (type variable binding)

System F (Rules, 1)System F (Rules, 1)

New typing rules:type abstraction type application

New evaluation rules:type application (1) type

application (2)t t’

t [T] t’ [T] (X.t) [T] [T/X] t

,X t : T

X.t : X.T t : X.T’

t [T] : [T/X] T’

System F (Rules, 2)System F (Rules, 2)

Needed restriction: Types in these rules have to be closed, or free type variables have to be bound:

X X :

,X T : X.T :

T1 : T2 :

T1 T2 :

System F (Examples)System F (Examples)

The polymorphic identity function (System F and ML)

id = X. x:X. x val id = fn x => x> id: X. X X > ‘a id: ‘a ‘a

is applied as follows:id [Nat] 5 id 5

which is evaluated as (X. x:X. x) [Nat] 5 [Nat/X](x:X. x) 5 (x:Nat. x) 5 [5/x](x) 5

System F (Further Examples)System F (Further Examples)

Double application ( f(f(x)) ):

double = X. f.XX. x. f (f x)> double: X. (XX) X X

(ML: val double = fn f => fn x => f(f x)> ‘a double: (‘a ’a) ‘a ‘a )

doubleFun = double [NatNat]> doubleFun: ((NatNat) Nat Nat)

(NatNat) Nat Nat

doubleFun (x. x+1) 3> 5

System F (Further Examples)System F (Further Examples)

Self application:

In simply typed -calculus, you cannot type x. x x.Now:

selfApp = f. f fselfApp = f:X.XX. f [X.XX] f> selfApp: (X.XX)(X.XX)

evaluation: selfApp id (f:X.XX. f [X.XX] f) id (X. x:X. x) [X.XX] id (x:X.X X. x) id id

Basic PropertiesBasic Properties

Type Uniqueness, Type Preservation and Type Uniqueness, Type Preservation and ProgressProgress

Theorem [Uniqueness]:Every well-typed system F term has exactly one type.

Theorem [Preservation]: t : T and t t’ implies t’ : T.

Theorem [Progress]:If t is closed and well founded,then either t is a value or t t’ for some t’.

Proofs: straightforward structural induction

NormalizationNormalization

Theorem: Every well-typed System F term is normalizing, i.e. the evaluation of well-typed programs terminates.

Proof: very hard (Girard 1972, doctoral thesis)(simplified later on to about 5 pages)

Amazing: Normalization holds although we can code many things.

to sorting function

Normalization – Simple ApplicationNormalization – Simple Application

There are untypable terms!

Example:(x. x x) (x. x x)

cannot be typable, since this term has no normal form.

ErasureErasure

Erasure and Type ReconstructionErasure and Type Reconstruction

See System F as extension of untyped -calculus:erase(x) = xerase(x:T. t) = x.erase(t)erase(t t‘) = (erase(t))(erase(t‘))erase(X.t) = erase(t)erase(t[T]) = erase(t)

Theorem (Wells, 1994): Let m be a closed term. It is undecidable, whether there is a well typed System F term t such that m = erase(t).

Are there solutions for weaker erasure?

Erasure and EvaluationErasure and Evaluation

Erasure operational semantics: Throw away types.

Assume existence of divergence, side effects… Thenlet f = X. diverge in 0

diverges, butlet f = diverge in 0

does.

So another reasonable erasure is:erase(x) = xerase(x:T. t) = x.erase(t)erase(t t‘) = (erase(t)) (erase(t‘))erase(X.t) = _.erase(t)erase(t[T]) = erase(t) ()

Type reconstruction is still undecidable (Pfenning 1992).

ImpredicativityImpredicativity

ImpredicativityImpredicativity

System F is impredicative:

Polymorphic types are defined by universal quantification over the universe of all types.This includes polymorphic types themselves.

Polymorphic types are „1st class“ in the world of types.

example:(f:X.XX. f) id

universally quantifiedtype

ImpredicativityImpredicativity

ML-polymorphism is predicative:

Polymorphic types are 2nd class,arguments do not have polymorphic types!(„prenex polymorphism“)

example:(fn f => fn x => f x) id 3

only one type /

instanciated

ParametricityParametricity

ParametricityParametricity

Evaluation of polymorphic applications does not depend on the type that is supplied.

This is a strong invariant!

ParametricityParametricity

Examples of easy results from parametricity:

There is exactly one function of typeX.XX,

namely the identity function.There are exactly two functions of type

X.XXXbehaving differently, namely those denoted by

X. a:X. b:X. aX. a:X. b:X. b

These do not (and cannot) alter their behavior depending on X!

Church Encodings: BooleansChurch Encodings: Booleans

System F has hidden structure:

CBool = X. XXX

contains (as already seen)

tru = X. t:X. f:X. t (> tru: CBool)fls = X. t:X. f:X. f (> fls: CBool)

and other terms, but it is intuitively clear that theyall behave like either tru or fls.

One function on CBool is

not = b:CBool. (X. t:X. f:X. b [X] f t)

Church Encodings: NatChurch Encodings: Nat

Elements of Nat could be encoded as

The corresponding type is

CNat = X. (XX)XX

and a term encoding the successor function is

csucc = n:CNat. (X. s:XX. z:X. s (n [X] s z))

c0 = X. s:XX. z:X. zc1 = X. s:XX. z:X. s zc2 = X. s:XX. z:X. s (s z)

c0 = s. z. zc1 = s. z. s zc2 = s. z. s (s z)

SummarySummary

System F is highly expressive!

Still, it is strongly normalizing!!!

Types must not be omitted.

In practice: Trade-off between convenience (e.g. automated type checking) and expressivity.

ReferencesReferences

Barendregt: Lambda Calculi with TypesHandbook of Computer Science, Vol. 2, 1992

Cardelli, Wegner: On Understanding Types, Data Abstraction, and PolymorphismComputing Surveys, Vol. 17, No. 4, p. 471-522, 1985

MacQueen: Lecture NotesChicago, 2003

Pfenning: On the Undecidability of Partial Polymorphic Type ReconstructionFundamentae Informaticae, Vol 19, No. 1-2, p. 185-199, 1993

Pierce: Types and Programming Languages, Chapter 22MIT Press, 2002

Questions / The ENDQuestions / The END

A Sorting Function in System F (Reynolds A Sorting Function in System F (Reynolds 1985)1985)

List X = R. (X R R) R R

insert = X. leq:XXbool. l:List X. e:X.let res = l [List X * List X]

(hd:X. acc:List X * List X.let rest = acc.1 inlet newrest = hd::rest inlet restwithe = acc.2 inlet newrestwithe =

if leq e hdthen e::hd::restelse hd::restwithe in

(newrest, newrestwithe) )(nil[X], e::nil[X])

in res.2

sort = X. leq:XXbool. l:List X.l [List X] (hd:X. rest:List X. insert [X] leq rest hd)

(nil [X])

only pure calculus, w/o fix or recursion

back to normalization

System F (Polymorphic Lists)System F (Polymorphic Lists)

List X = R. (X R R) R R (Church encoding)

nil: X. List XX. (R. c:XRR. n:R. n)

cons: X. X -> List X -> List X

isnil: X. List X -> Bool

head: X. List X -> X

tail: X. List X -> List X

map: X. Y. (X -> Y) -> List X -> List Y