• No results found

Programming Languages and Techniques (CIS120)

N/A
N/A
Protected

Academic year: 2021

Share "Programming Languages and Techniques (CIS120)"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Typechecking

How does OCaml* typecheck your code?

(3)

OCaml Typechecking Errors

(4)

Typechecking

How do you determine the type of an expression?

1. Recursively determine the types of all sub-expressions

Constants have “obvious” types

3 : int “foo” : string true : bool

Identifiers may have type annotations

• let and function arguments • Module signatures/interfaces

2. Expressions that construct structured values have compound

types built from the types of sub-expressions

(3, “foo”) : int * string (fun (x:int) -> x + 1) : int -> int

(5)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

(6)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

(7)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

int -> bool -> ??

(8)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

int -> bool -> ??

bool

(9)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

int -> bool -> bool

bool

(10)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

int -> bool -> bool int

??

(11)

Typechecking II

3. The type of a function-application expression is obtained as

the result from the function type:

Given a function f : T

1

-> T

2

and an argument e : T

1

of the input type

(f e) : T

2

((fun (x:int) (y:bool) -> y) 3) : ??

int -> bool -> bool int bool

bool -> bool

Here: T1 = int

(12)

Typechecking III

What about generics? i.e. what if

f:'a ->'a

?

For generic types we unify

Given a function f : T

1

-> T

2

and an argument e : U

1

of the input type

Can “match up” T

1

and U

1

to obtain information about

type parameters in T

1

and U

1

based on their usage

(13)

Example Typechecking Problem

empty : ('k, 'v) map

add : 'k -> 'v -> ('k, 'v) map -> ('k, 'v) map entries : ('k, 'v) map -> ('k * 'v) list

fun (x:'v) -> entries (add 3 x empty)

(14)

Example Typechecking Problem

empty : ('k, 'v) map

add : 'k -> 'v -> ('k, 'v) map -> ('k, 'v) map entries : ('k, 'v) map -> ('k * 'v) list

fun (x:'v) -> entries (add 3 x empty)

(15)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ?? empty : ('k, 'v) map

(16)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int

empty : ('k, 'v) map

(17)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v

empty : ('k, 'v) map

(18)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

‘v -> ??

int 'v ('k, 'v) map

empty : ('k, 'v) map

(19)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v ('k, 'v) map

empty : ('k, 'v) map

(20)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v ('k, 'v) map

?? empty : ('k, 'v) map

(21)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ?? int 'v ('k, 'v) map Application: T1 = 'k T2 = 'v -> ('k,'v) map -> ('k,'v) map Instantiate: 'k = int T2 empty : ('k, 'v) map

(22)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v (int, 'v) map

Another Application: T’1 = 'v

T’2 = (int,'v) map -> (int,'v) map

Instantiate: 'v = 'v

T2

T’2

empty : ('k, 'v) map

(23)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v (int, 'v) map

A third Application: T’’1 = (int,'v) map

T’’2 = (int,'v) map

Argument and argument type already agree

T2

T’2

T’’2= (int, 'v) map

empty : ('k, 'v) map

(24)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v (int, 'v) map

T2

T’2

T’’2= (int,’v) map

U1 -> U2

empty : ('k, 'v) map

(25)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

'v -> ??

int 'v (int, 'v) map

T2 T’2 T’’2= (int,’v) map U1 -> U2 Another Application: U1 = ('k,'v) map U2 = ('k * 'v) list Unify U1 with T’’2

('k,'v) map ~~ (int,'v) map

Instantiate 'k = int ?? empty : ('k, 'v) map

(26)

Example Typechecking Problem

fun (x:'v) -> entries (add 3 x empty)

‘v -> ??

int 'v (int, 'v) map

T2 T’2 T’’2= (int, 'v) map U1 -> U2 Another Application: U1 = (int,'v) map U2 = (int * 'v) list U2= (int * 'v) list empty : ('k, 'v) map

(27)

Example Typechecking Problem

fun (x:’v) -> entries (add 3 x empty)

'v -> (int * 'v) list

int 'v (int, 'v) map

T2 T’2 T’’2= (int, 'v) map U1 -> U2 Another Application: U1 = (int,'v) map U2 = (int * 'v) list U2= (int * 'v) list empty : ('k, 'v) map

(28)

Ill-typed Expressions?

An expression is ill-typed if, during this type checking process,

inconsistent constraints are encountered:

add 3 true (add “foo” false empty)

Error: found int but expected string empty : ('k, 'v) map

References

Related documents

The book’s first section offers a foundation of four simple but comprehensive Lean key performance indicators (KPIs): waste of the time of things (as in cycle time),

Phulner can then replace that function so that the taint is retained and a vulnerability has been injected, because user input which has not been sanitized is outputted on the page..

With the assumption that TOA is the integer multiples of chip duration, our decoupled multiuser ranging (DEMR) estimator employs integrate-and-dump filter (IDF) in chip sampling

Reference equality == on class values is a correct way to check for class equality (because there is only ever one object that represents each class)... Overriding equals, take two..

These test data, and those from many other researchers, highlight the benefits to be obtained in terms of reducing sulphate attack and chloride ion penetration from incorporating

The essence of the VB method relies on making simplifying assumptions about the poste- rior dependence of a problem. By definition, the general posterior dependence structure

Variable reference x Declared type of variable Object creation new Counter () Class of the object. Method call c.inc() Return type

OCaml directly supports the declaration of abstract types via signatures.. interface): defines operations on the type...