Top Banner
Doaitse Swierstra Atze Dijkstra Type Systems
40

Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

Doaitse SwierstraAtze Dijkstra

Doaitse SwierstraAtze Dijkstra

Type SystemsType Systems

Page 2: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

2

lectures from the book “Types and Programming Languages”, Benjamin Piercestudy the type inferencer/checker of the Utrecht Haskell Compiler (UHC)projectsmall exercises

Components

Page 3: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

3

Grammars and ParsingImplementation of Programming Languagesfluency in Haskell programming

Prerequisites

Page 4: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

4

should be relatively easy to follow, but it will take time (500 pages)well written, with extensive set of exercisesboth theory and practiceuses ML as a demonstration and implementation languagewritten at an easy pace

Book

Page 5: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

5

“extension” of IPT compilerdata typesmultiparameter type classesforall and existential quantifierspolymorphic kinds

UHC

Page 6: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

6

1.Modules

2.Extendible Records

3.Functional Dependencies

4.Typed AG

5.XMLambda

6.Unifying Kind and Type Inference

7.Kind Inferencing in Helium

8.Haskell Type System

9.Uniqueness Types

10.Implicit Arguments

Projects

Page 7: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

7

import and export typeskeep safetyclass issuesparallel importstyped annotationsML functors

1 Modules

Page 8: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

8

allow cartesian products with labelled fieldssubtype relationuse class sytem to express constraintsmany different proposals exits

2 Extendible Records

Page 9: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

9

allow parameters in instance definitions to have a functional relationshipvery useful in multi-parameter type classesimplemented in Hugs and GHC

3 Functional Dep’cies

Page 10: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

10

add elements of Haskell to AG systemdelicate choice of features has to be madeuseful work, as you will all recognise

Typed AG

Page 11: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

11

yet another approach to extendible recordsmoves further away from Haskellhas nice applications

XMLambda

Page 12: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

12

type inference and kind inference are very similarmake the code look just as similarcan this be extended to even more levels?

Unifying Type and Kind Inference

Page 13: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

13

Helium: light weight Haskell for first year studentsexcellent error messagestranport kind inference to heliumcontraint based type inferencer

Kind Inference in Helium

Page 14: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

14

study the full haskell type systemdocument missing features from UHCadd some of the still missing elements

Haskell Type System

Page 15: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

15

the type system keep tracks of the number of references to a value (1 or >1)replacement for IO monadinvented and implemented in Cleanmakes efficient implementation possibleavoids dynamic garbage collection

Uniqueness Types

Page 16: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

16

generalise the class parametersextend haskell so you can make classes into first class citizens

Implicit Arguments

Page 17: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

17

if you want a specific partner then choose a partnerdecide on your preferences ranking 1-5 for your top 5 preferred projectsmail to [email protected] before tomorrow 17.00

What to do now?

Page 18: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

18

motivationmathematical preliminariesuntyped arithmetic expressionsthe untyped lambda calculusnameless representation of terms

Chapter 1-10

Page 19: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

19

ML implementationTyped arithmetic expressionsSimply typed lanbda calculusML implementation

1-10 continued

Page 20: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

20

nothing in these chapters should come as a surpsise

and you should just browse/read

ML is used:

somewhat baroque notation!!

strict evaluation!!

the theory treated at some points relies on this!, so keep this in mind

Observations

Page 21: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

21

originally: data organistation, layout of data in memory: PIC (99)overloading, nice notation: 3+5.0programs should not go wrong

elementary operations are applied to bit patterns that represent the kind of data they expect

programs do terminate

Why types?

Page 22: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

22

assist in organisation of program code

dependent types => total correctness

a type system constrains the number of legal programs is some useful way

Why Types?

Page 23: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

23

dynamic typing (LISP, Java coercions)static typing (as you know it)Higher Order Logic, Automath

integration with theorem proversthe type system can express every property of a program

Kind of Typing Systems

Page 24: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

operational semanticsbig step semanticssmall step semantics

denotational semanticsaxiomatic semantics

Proving program properties

Our preference here

Page 25: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

25

we can express every possible computation in the untyped lambda calculus

Lambda calculus

Page 26: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

26

Booleans t ::= true | false | if t then t else t

v ::= true | false

if true then t1 else t2 => t1if false then t1 else t2 => t2

Page 27: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

27

Booleans (strict evaluation)

t1 -> t1’ if t1 then t2 else t3 =>

if t1’ then t2 else t3

note that the condition is evaluated before the expressions themselves are

Page 28: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

28

evaluation is deterministic (i.e. always exactly one rule applies)evaluation always terminatesnormal forms are unique

Observe

Page 29: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

29

Arithmetic Expressions

t ::= 0 | succ t | pred t | iszero t

nv :: = 0 | succ nv

t1 => t2succ t1 => succ t2

Page 30: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

30

Booleans tru = \t.\f.tfls = \t.\f.fif = \c.\t.\e. c t e

if tru 2 3 =>(\c.\t.\e. c t e) tru 2 3 =>( \t.\e. tru t e) 2 3 =>( \e. tru 2 e) 3 => tru 2 3 => (\t.\f.t) 2 3 => ( \f.2) 3 => ( 2)

Page 31: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

31

Integersc0 = \s.\z. zc1 = \s.\z. s zc2 = \s.\z.s (s z)c3 = \s.\z.s (s (s z))

Page 32: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

32

Recursion!!omega = (\x. x x)(\x. x x) ??

fix = \f.(\x. f(x x)) (\x. f(x x))

fix g => (\x. g(x x))(\x. g(x x)) =>g((\x. g(x x))(\x. g(x x))) =>g (fix g)

Page 33: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

33

Recursion (cont.)

note that fix generates copies of g if necessaryand passes the generater on to this gso that g can generate new copies is necessary

Page 34: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

34

try to find a type for fixif you cannot find one, try to explain why you cannot

Exercise

Page 35: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

35

Untyped lambda calculus

t ::= x | \x.t | t t

v :: = \x.t

Page 36: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

36

De Bruijn Indices

represent lambda terms without using identifiersan identifier is replaced by a number indicating how far the argument is removed from the top of the stack

Page 37: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

37

Example

is represented by

Page 38: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

38

Terms

Page 39: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

39

Exercise

define a Haskell data type for the untyped lambda calculus with variablesdefine a Haskell data type for representing de Bruijn termsdefine a translation from the first to the secondwrite a small interpreter for the latter

Page 40: Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.

40

Chapter 6: de Bruijn numbersChapter 7: an interpreter in ML

Hint