of 59

• date post

20-Aug-2018
• Category

## Documents

• view

214

0

Embed Size (px)

### Transcript of Introductory Course on Logic and Automata Theory iosif/LogicAutomata07/type-systems-  ·...

• Introductory Courseon Logic and Automata Theory

Introduction to type systems

Polyvios.Pratikakis@imag.fr

Based on slides by Jeff Foster, UMD

Introduction to type systems p. 1/59

• The need for typesConsider the lambda calculus terms:

false = x.y.x

0 = x.y.x (Scott encoding)

Everything is encoded using functionsOne can easily misuse combinators

false 0, or if 0 then . . ., etc. . .Its no better than assembly language!

Introduction to type systems p. 2/59

• Type systemA type system is some mechanism for distinguishinggood programs from bad

Good programs are well typedBad programs are ill typed or not typeable

Examples:0 + 1 is well typedfalse 0 is ill typed: booleans cannot be applied tonumbers1 + (if true then 0 else false) is ill typed: cannot add aboolean to an integer

Introduction to type systems p. 3/59

• A definition

A type system is a tractable syntactic method forproving the absence of certain program behaviorsby classifying phrases according to the kinds ofvalues they compute.

Benjamin Pierce, Types and Programming Languages

Introduction to type systems p. 4/59

• Simply-typed lambda calculuse ::= n | x | x : .e | e e

Functions include the type of their argumentWe dont need this yet, but it will be useful later

::= int |

1 2 is a the type of a function that, given anargument of type 1, returns a result of type 2We say 1 is the domain, and 2 is the range

Introduction to type systems p. 5/59

• Typing judgementsThe type system will prove judgments of the form

e :

In type environment , expression e has type

Introduction to type systems p. 6/59

• Type environmentsA type environment is a map from variables to types(similar to a symbol table) is the empty type environment

A closed term e is well-typed if e : for some Also written as e :

, x : is just like except x has type The type of x in is For z 6= x, the type of z in , x : is the type of z in (we look up variables from the end)

When we see a variable in a program, we look in thetype environment to find its type

Introduction to type systems p. 7/59

• Type rules

n : int

x dom()

x : (x)

, x : e :

x : .e :

e1 :

e2 :

e1 e2 :

Introduction to type systems p. 8/59

• ExampleAssume = ( : int int)

dom()

: int int 3 : int 3 : int

Introduction to type systems p. 9/59

• An algorithm for type checkingThe above type rules are deterministic

For each syntactic form of a term e, only one rule ispossible

They define a natural type checking algorithm

TypeCheck : (type_env expression) type

TypeCheck(, n) = int

TypeCheck(, x) = if x dom() then (x) else fail

TypeCheck(, x : .e) = TypeCheck((, x : ), e)

TypeCheck(, e1 e2) =

let 1 = TypeCheck(, e1) in

let 2 = TypeCheck(, e2) in

if dom(1) = 2 then range(1) else fail

Introduction to type systems p. 10/59

• Reminder: semanticsSmall-step, call-by-value semantics

If an expression is not a value, and cannot evaluateany more, we say it is stuck, e.g. 0 1

(x : .e1) v2 e1[v2/x]

e1 e

1

e1 e2 e

1 e2

e2 e

2

v1 e2 v1 e

2

wheree ::= v | x | e e

v ::= n | x : .e

Introduction to type systems p. 11/59

• Progress theoremIf e : then either e is a value, or there exists e suchthat e e

Proof by induction on eBase cases (values): n, x : .e, trivially trueCase x: impossible, untypeable in emptyenvironmentInductive case: e1 e2

If e1 is not a value, apply the theorem inductivelyand then second semantic rule.If e1 is a value but e2 is not, similar (using the thirdsemantic rule)If e1 and e2 are values, then e1 has function type,and the only value with a function type is a lambdaterm, so we apply the first semantic rule

Introduction to type systems p. 12/59

• Preservation theoremIf e : and e e then e :

Proof by induction on e e

In all cases (one for each rule), e has the forme = e1 e2Inversion: from e1 e2 : we get e1 : and e2 :

Three semantic rules:Second or third rule: apply induction on e1 or e2respectively, and type-rule for applicationRemaining case: (x : .e) v e[v/x]

Introduction to type systems p. 13/59

• Preservation continuedCase (x : .e) v e[v/x]From hypothesis:

x : e : x : .e :

To finish preservation proof, we must prove e[v/x] : .

Susbstitution lemma

Introduction to type systems p. 14/59

• Substitution lemmaIf v : and , x : e : , then e[v/x] :

Proof by induction on e (not shown)

For lazy (call by name) semantics, substitution lemma isIf e1 : and , x : e : , then e[e1/x] :

Introduction to type systems p. 15/59

• SoundnessSo far:

Progress: If e : , then either e is a value, or thereexists e such that e e

Preservation: If e : and e e then e :

Putting these together, we get soundnessIf e : then either there exists a value v such thate v or e doesnt terminate

What does this mean?Well-typed programs dont go wrongEvaluation never gets stuck

Introduction to type systems p. 16/59

• Product types (tuples)

e ::= . . . | fst e | snd e

::= . . . |

e1 : e2 :

(e1, e2) :

e :

fst e : e :

snd e :

Alternatively, using function signaturespair :

fst :

snd :

Introduction to type systems p. 17/59

• Sum types

e ::= . . . | inL2 e | inR1 e | (case e of x1 : 1 e1 | x2 : 2 e2)

::= . . . | +

e : 1 inL2 e : 1 + 2

e : 2 inR1 e : 1 + 2

e : 1 + 2, x1 : 1 e1 : , x2 : 2 e2 :

(case e of x1 : 1 e1 | x2 : 2 e2) :

Introduction to type systems p. 18/59

• Self application and typesSelf application is not typeable in this system

, x :? x :

, x :? x :

, x :? x x : . . .

x :?.x x : . . .

We need a type such that =

The simply-typed lambda calculus is stronglynormalizing

Every program has a normal formOr, every program halts!

Introduction to type systems p. 19/59

• Recursive typesWe can type self application if we have a type torepresent the solution to equations like =

We define the type . to be the solution to the(recursive) equation = Example: .int

Introduction to type systems p. 20/59

• Folding/unfolding recursive typesInferred: We can check type equivalence with theprevious definition

Standard unification, omit occurs checks (explainedlater)

Alternative method: explicit fold/unfoldThe programmer inserts explicit fold and unfoldoperations to expand/contract a level of the type

unfold . = [./]fold [./] = .

Introduction to type systems p. 21/59

• Fold-based recursive types

e ::= . . . | fold e | unfold e

::= . . . | .

e : [./]

fold e : .

e : .

unfold e : [./]

Introduction to type systems p. 22/59

• ML DatatypesCombines fold/unfold-style recursive and sum types

Each occurence of a type constructor whenproducing a value corresponds to inR/inL and (ifrecursive,) also foldEach occurence of a type constructor in a patternmatch corresponds to a case and (if recursive,) atleast one unfold

Introduction to type systems p. 23/59

• ML Datatypes examples

type intlist =

Int of int

| Cons of int intlist

is equivalent to .int + (int )

( Int 3)

is equivalent to fold (inLint.int+(int)3)

Introduction to type systems p. 24/59

• More ML Datatype examples

(Cons (42, ( Int 3)))

is equivalent to fold (inRint(2, fold (inLint.int+(int)3)))

match e with

Int x e1| Cons x e2

is equivalent to

case (unfold e) of

x : int e1| x : int (.int + (int )) e2

Introduction to type systems p. 25/59

• DiscussionIn the pure lambda calculus, every term is typeable withrecursive types

Pure means only including functions and variables(the calculus from the last class)

Most languages have some kind of recursive typeUsed to encode data structures like e.g. lists, trees,. . .

However, usually two recursive types are differentiatedby name, even when they define the same structure

For example,struct foo { int x; struct foo *next; }

is different fromstruct bar { int x; struct bar *next; }

Introduction to type systems p. 26/59

• Intermission

Next: Curry-Howard

correspondence

Introduction to type systems p. 27/59

• Classical propositional logicFormulas of the form

::= p | | | |

Where p P is an atomic proposition, e.g. Socrates isa man

Convenient abbreviations: means means ( ) ( )

Introduction to type systems p. 28/59

• Semantics of classical logicInterpretation m : P {true, false}

JpKm = m(p)

JKm = false

J Km = JKmJKm

J Km = JKmJKm

J Km = JKmJKm

Where , , are the standard boolean operations on{true, false}

Introduction to type systems p. 29/59

• TerminologyA formula is valid if JKm = true for all m

A formula is unsatisfiable if JKm = false for all m

Law of excluded middle:

Formula is valid for any

A proof system attempts to determine the validity of aformula

Introduction to type systems p. 30/59

• Proof theory