109
10/9/2018 1 Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/CS421A Based on slides by Elsa Gunter , which were inspired by earlier slides by Mattox Beckman, Vikram Adve, and Gul Agha

Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 1

Programming Languages and Compilers

(CS 421)

Sasa Misailovic

4110 SC, UIUChttps://courses.engr.illinois.edu/cs421/fa2017/CS421A

Based on slides by Elsa Gunter, which were inspired by

earlier slides by Mattox Beckman, Vikram Adve, and Gul Agha

Page 2: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 2

Terminology

Type: A type t defines a set of possible data

values

E.g. short in C is {x| 215 - 1 x -215}

A value in this set is said to have type t

Type system: rules of a language assigning

types to expressions

Page 3: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 3

Why Data Types?

Data types play a key role in:

Data abstraction in the design of programs

Type checking in the analysis of programs

Compile-time code generation in the

translation and execution of programs

Data layout (how many words; which are data and

which are pointers) dictated by type

Page 4: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 4

Types as Specifications

Types describe properties

Different type systems describe different properties:

Data is read-write versus read-only

Operation has authority to access data

Data came from “right” source

Operation might or could not raise an exception

Common type systems focus on types describing same

data layout and access methods

Page 5: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 5

Sound Type System

Type: A type t defines a set of possible data values

E.g. short in C is {x| 215 - 1 x -215}

A value in this set is said to have type t

Type system: rules of a language assigning types to expressions

If an expression is assigned type t, and it evaluates to a

value v, then v is in the set of values defined by t

SML, OCAML, Scheme and Ada have sound type

systems

Most implementations of C and C++ do not

Page 6: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Sound Type System

But Java and Scala are also unsound

10/9/2018 6

For details, see this paper:

Java and Scala’s Type

Systems are Unsound ∗The Existential Crisis of

Null Pointers.

Amin and Tate

(OOPSLA 2016)

Page 7: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 7

Strongly Typed Language

When no application of an operator to

arguments can lead to a run-time type

error, language is strongly typed

Eg: 1 + 2.3;;

Depends on definition of “type error”

Page 8: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 8

Strongly Typed Language

C++ claimed to be “strongly typed”, but Union types allow creating a value at one type

and using it at another Type coercions may cause unexpected

(undesirable) effects No array bounds check (in fact, no runtime

checks at all)

SML, OCAML “strongly typed” but still must do dynamic array bounds checks, runtime type case analysis, and other checks

Page 9: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 9

Static vs Dynamic Types

• Static type: type assigned to an expression at compile time

• Dynamic type: type assigned to a storage location at run time

• Statically typed language: static type assigned to every expression at compile time

• Dynamically typed language: type of an expression determined at run time

Page 10: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 10

Type Checking

When is op(arg1,…,argn) allowed?

Type checking assures that operations are applied to the right number of arguments of the right types

Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied

Used to resolve overloaded operations

Page 11: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 11

Type Checking

Type checking may be done statically at compile time or dynamically at run time

Dynamically typed (aka untyped) languages (eg LISP, Prolog, JavaScript) do only dynamic type checking

Statically typed languages can do most type checking statically

Page 12: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 12

Dynamic Type Checking

Performed at run-time before each

operation is applied

Types of variables and operations left

unspecified until run-time

Same variable may be used at different types

Page 13: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 13

Dynamic Type Checking

Data object must contain type information

Errors aren’t detected until violating

application is executed (maybe years after

the code was written)

Page 14: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 14

Static Type Checking

Performed after parsing, before code

generation

Type of every variable and signature of

every operator must be known at compile

time

Page 15: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 15

Static Type Checking

Can eliminate need to store type information in data object if no dynamic type checking is needed

Catches many programming errors at earliest point

Can’t check types that depend on dynamically computed values

Eg: array bounds

Page 16: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 16

Static Type Checking

Typically places restrictions on languages

Garbage collection

References instead of pointers

All variables initialized when created

Variable only used at one type

Union types allow for work-arounds, but effectively introduce dynamic type checks

Page 17: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 17

Type Declarations

Type declarations: explicit assignment of

types to variables (signatures to functions)

in the code of a program

Must be checked in a strongly typed language

Often not necessary for strong typing or even

static typing (depends on the type system)

Page 18: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 18

Type Inference

Type inference: A program analysis to

assign a type to an expression from the

program context of the expression

Fully static type inference first introduced by

Robin Miller in ML

Haskel, OCAML, SML all use type inference

Records are a problem for type inference

Page 19: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 19

Format of Type Judgments

A type judgement has the form

|- exp :

is a typing environment

Supplies the types of variables (and function names when function names are not variables)

is a set of the form { x : , . . . }

For any x at most one such that (x : )

exp is a program expression

is a type to be assigned to exp

|- pronounced “turnstyle”, or “entails” (or “satisfies” or, informally, “shows”)

Page 20: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 20

Axioms - Constants

|- n : int (assuming n is an integer constant)

|- true : bool |- false : bool

These rules are true with any typing environment

, n are meta-variables

Page 21: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 21

Axioms – Variables (Monomorphic Rule)

Notation: Let (x) = if x :

Note: if such exits, its unique

Variable axiom:

|- x : if (x) =

Page 22: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 22

Simple Rules - Arithmetic

Primitive operators ( { +, -, *, …}):

|- e1:1 |- e2:2 ():1 2 3

|- e1 e2 : 3

Relations ( ˜ { < , > , =, <=, >= }):

|- e1 : |- e2 :

|- e1 ˜ e2 :bool

For the moment, think is int

Page 23: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Example: {x:int} |- x + 2 = 3 :bool

{x:int} |- x:int {x:int} |- 2:int

{x : int} |- x + 2 : bool {x:int} |- 3 :int

{x:int} |- x + 2 = 3 : bool

10/9/2018 23

What do we need to show first?

Page 24: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Example: {x:int} |- x + 2 = 3 :bool

{x:int} |- x:int {x:int} |- 2:int

{x : int} |- x + 2 : int {x:int} |- 3 :int

{x:int} |- x + 2 = 3 : bool

10/9/2018 24

Rel

What do we need for the left side?

Page 25: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Example: {x:int} |- x + 2 = 3 :bool

{x:int} |- x:int {x:int} |- 2:int

{x : int} |- x + 2 : int {x:int} |- 3 :int

{x:int} |- x + 2 = 3 : bool

10/9/2018 25

Rel

AO

How to finish?

Page 26: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Example: {x:int} |- x + 2 = 3 :bool

{x:int} |- x:int {x:int} |- 2:int

{x : int} |- x + 2 : int {x:int} |- 3 :int

{x:int} |- x + 2 = 3 : bool

10/9/2018 26

Rel

AOConst

ConstVar

Complete Proof (type derivation)

Variable axiom:

|- x : if (x) =

Page 27: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 27

Simple Rules - Booleans

Connectives

|- e1 : bool |- e2 : bool

|- e1 && e2 : bool

|- e1 : bool |- e2 : bool

|- e1 || e2 : bool

Page 28: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 28

Type Variables in Rules

If_then_else rule:

|- e1 : bool |- e2 : |- e3 :

|- (if e1 then e2 else e3) :

is a type variable (meta-variable)

Can take any type at all

All instances in a rule application must get same type

Then branch, else branch and if_then_else must all have same type

Page 29: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 29

Function Application

Application rule:

|- e1 : 1 2 |- e2 : 1

|- (e1 e2) : 2

If you have a function expression e1 of

type 1 2 applied to an argument e2 of

type 1, the resulting expression e1e2 has

type 2

Page 30: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 30

Fun Rule

Rules describe types, but also how the

environment may change

Can only do what rule allows!

fun rule:

{x : 1 } + |- e : 2

|- fun x -> e : 1 2

Page 31: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 31

Fun Examples

{y : int } + |- y + 3 : int

|- fun y -> y + 3 : int int

{f : int bool} + |- f 2 :: [true] : bool list

|- (fun f -> f 2 :: [true])

: (int bool) bool list

Page 32: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 32

(Monomorphic) Let and Let Rec

let rule:

|- e1 : 1 {x : 1} + |- e2 : 2

|- (let x = e1 in e2 ) : 2

let rec rule:

{x: 1} + |- e1:1 {x: 1} + |- e2:2

|- (let rec x = e1 in e2 ) : 2

Page 33: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 33

Example

Which rule do we apply?

?

|- (let rec one = 1 :: one in

let x = 2 in

fun y -> (x :: y :: one) ) : int int list

Page 34: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 34

Example

Let rec rule: 2 {one : int list} |-

1 (let x = 2 in

{one : int list} |- fun y -> (x :: y :: one))

(1 :: one) : int list : int int list

|- (let rec one = 1 :: one in

let x = 2 in

fun y -> (x :: y :: one) ) : int int list

Page 35: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 35

Proof of 1

Which rule?

{one : int list} |- (1 :: one) : int list

Page 36: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 36

Proof of 1

Application

3 4

{one : int list} |- {one : int list} |-

((::) 1): int list int list one : int list

{one : int list} |- (1 :: one) : int list

Page 37: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 37

Proof of 3

Constants Rule Constants Rule

{one : int list} |- {one : int list} |-

(::) : int int list int list 1 : int

{one : int list} |- ((::) 1) : int list int list

Page 38: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 38

Proof of 1

Application

3 4

{one : int list} |- {one : int list} |-

((::) 1): int list int list one : int list

{one : int list} |- (1 :: one) : int list

Page 39: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 39

Proof of 4

Rule for variables

{one : int list} |- one : int list

Page 40: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 40

Example

Let rec rule: 2 {one : int list} |-

1 (let x = 2 in

{one : int list} |- fun y -> (x :: y :: one))

(1 :: one) : int list : int int list

|- (let rec one = 1 :: one in

let x = 2 in

fun y -> (x :: y :: one) ) : int int list

Page 41: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 41

Proof of 2

5 {x:int; one : int list} |-

Constant fun y ->

(x :: y :: one))

{one : int list} |- 2 : int : int int list

{one : int list} |- (let x = 2 in

fun y -> (x :: y :: one)) : int int list

Page 42: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 42

Proof of 5

?

{x:int; one : int list} |- fun y -> (x :: y :: one))

: int int list

Page 43: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 43

Proof of 5

?

{y:int; x:int; one : int list} |- (x :: y :: one) : int list

{x:int; one : int list} |- fun y -> (x :: y :: one))

: int int list

Page 44: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 44

Proof of 5

6 7

{y:int; x:int; one:int list} {y:int; x:int; one:int list}

|- ((::) x):int list int list |- (y :: one) : int list

{y:int; x:int; one : int list} |- (x :: y :: one) : int list

{x:int; one : int list} |- fun y -> (x :: y :: one))

: int int list

Page 45: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 45

Proof of 6

Constant Variable

{…} |- (::)

: int int list int list {…; x:int;…} |- x:int

{y:int; x:int; one : int list} |- ((::) x)

: int list int list

Page 46: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 46

Proof of 7

Like Pf of 6 [replace x w/ y] Variable

{y:int; …} |- ((::) y) {…; one: int list} |-

:int list int list one: int list

{y:int; x:int; one : int list} |- (y :: one) : int list

Page 47: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 47

Curry - Howard Isomorphism

Type Systems are logics; logics are type systems

Types are propositions; propositions are types

Terms are proofs; proofs are terms

Function space arrow corresponds to implication; application corresponds to modus ponens

Page 48: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 48

Curry - Howard Isomorphism

Modus Ponens

A B A

B

• Application

|- e1 : |- e2 :

|- (e1 e2) :

Page 49: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 49

Mea Culpa

The above system can’t handle polymorphism as in OCAML

No type variables in type language (only meta-variable in the logic)

Would need:

Object level type variables and some kind of type quantification

let and let rec rules to introduce polymorphism

Explicit rule to eliminate (instantiate) polymorphism

Page 50: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Types

Polymorphism is the ability of a type term to

simultaneously admit several distinct types (Reynolds, 1974)

id : ‘a -> ‘a ;

Can be instatiated to e.g.:

int -> int

bool list -> bool list

(‘b -> ‘b) -> (‘b -> b’)

length : ‘a list -> int

int list -> int

(float->bool) list -> int

10/10/2018 50

Page 51: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Types

let swap (x,y) = (y,x) ;;

val swap : 'a * 'b -> 'b * 'a = <fun>

Replaces all these spurious variants:

val swapInt : int * int -> int * int = <fun>

val swapFloat : float * float -> float * float = <fun>

val swapIntFloat : int * float -> float * int = <fun>

val swapFloatInt : float * int -> int * float = <fun>

….

The road to type abstraction: function swap knows

nothing about the variables x and y except to treat

them as generic “black boxes” (or generic objects) 51

Page 52: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Support for Polymorphic Types

Monomorpic Types ():

Basic Types: int, bool, float, string, unit, …

Type Variables: , , g, d, e

Compound Types: , int * string, bool list, …

Polymorphic Types:

Monomorphic types

Generic types: universally quantified monomorphic t

1, … , n .

The variables 1, … , n are distinguished as being generic

Can think of as same as .

10/10/2018 52

A

A

Page 53: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Support for Polymorphic Types

We extend typing Environment to supply

polymorphic types for variables

Free variables of monomorphic type just type variables

that occur in it

Write FreeVars()

They are not bound by the quantifier

Free variables of polymorphic type remove variables

that are universally quantified

FreeVars( 1, … , n . ) = FreeVars() – {1, … , n }

FreeVars() = all FreeVars of types in range of

10/9/2018 53

A

Page 54: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

From Monomorphic to Polymorphic

Given:

type environment

monomorphic type

shares type variables with

Want most polymorphic type for that doesn't

break sharing type variables with

Gen(, ) = 1, … , n . where

{1, … , n} = freeVars() – freeVars()

10/9/2018 54

Page 55: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Typing Rules

A type judgement has the form

|- exp :

uses polymorphic types

still monomorphic

Most rules stay same (except use more general typing

environments). Rules that change:

Variables

Let and Let Rec

Allow polymorphic constants

Worth noting functions again55

Page 56: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Variables (Identifiers)

Variable axiom:

|- x : j() if (x) = 1, … , n .

Where j replaces all occurrences of

1, … , n by monotypes 1, … , n

Examples:

{ x : . } |- x : int

{ f : , . -> } |- f : int -> float56

A

Page 57: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Variables (Identifiers)

Variable axiom:

|- x : j() if (x) = 1, … , n .

Where j replaces all occurrences of

1, … , n by monotypes 1, … , n

Examples:

{ g : , . -> } |- g : -> int

{ h : . int -> } |- h : float -> int

A

Page 58: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Variables (Identifiers)

Variable axiom:

|- x : j() if (x) = 1, … , n .

Where j replaces all occurrences of

1, … , n by monotypes 1, … , n

Note: Monomorphic rule special case:

|- x : if (x) =

Constants treated same way

10/10/2018 58

A

Page 59: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/9/2018 59

Fun Rule Stays the Same

fun rule:

{x : 1} + |- e : 2

|- fun x -> e : 1 2

Types 1, 2 monomorphic

Function argument must always be used at

same type in function body

Page 60: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example

Assume additional built-in constants:

hd : . list ->

tl: . list -> list

is_empty : . list -> bool

:: : . -> list -> list

[] : . list

10/9/2018 60

Page 61: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (1)

Show:

?

{length: list -> int} |-

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

: list -> int

10/11/2018 61

Page 62: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: Fun Rule

Show: (2)

{length: list -> int, lst: list } |-

if is_empty lst then 0

else length (hd l) + length (tl lst) : int

{length: list -> int} |-

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

: list -> int

10/11/2018 62

Page 63: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (2)

Let = {length: list -> int, lst: list }

Show

?

|- if is_empty lst then 0

else 1 + length (tl lst) : int

10/11/2018 63

Page 64: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (3):IfThenElse

Let = {length: list -> int, lst: list }

Show

(4) (5) (6)

|- is_empty lst |- 0:int |- 1 + length (tl lst)

: bool : int

|- if is_empty lst then 0

else 1 + length (tl lst) : int

10/11/2018 64

Page 65: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (4)

Let ={length: list -> int, lst: list }

Recall: is_empty : . list -> bool

Show

?

|- is_empty lst : bool

10/11/2018 65

Page 66: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (4):Application

Let ={length: list -> int, lst: list }

Recall: is_empty : . list -> bool

? ?

|- is_empty : list -> bool |- lst : list

|- is_empty lst : bool

10/11/2018 66

Page 67: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (4)

Let ={length: list -> int, lst: list }

Recall: is_empty : . list -> bool

By Const since list -> bool is

instance of . list -> bool ?

|- is_empty : list -> bool |- lst : list

|- is_empty lst : bool

10/11/2018 67

A

Page 68: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (4)

Let ={length: list -> int, l: list }

Show

By Const since list -> bool is By Variable

instance of . list -> bool (lst) = list

|- is_empty : list -> bool |- lst : list

|- is_empty lst : bool

This finishes (4)

10/11/2018 68

A

Page 69: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (3):IfThenElse (Repeat)

Let ={length: list -> int, lst: list }

Show

(4) (5) (6)

|- is_empty lst |- 0:int |- 1 + length (tl lst)

: bool : int

|- if is_empty lst then 0

else 1 + length (tl lst) : int

10/11/2018 69

Page 70: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (5):Const

Let ={length: list -> int, lst: list }

Show

By Const Rule

|- 0:int

10/11/2018 70

Page 71: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (6):Arith Op

Let ={length: list -> int, lst: list }

Show

By Variable (7)

|- length |- (tl lst)

By Const : list -> int : list

|- l : int |- length (tl lst) : int

|- 1 + length (tl lst) : int

10/11/2018 71

Page 72: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (7):App Rule

Let ={length: list -> int, lst: list }

Recall const tl: . list -> list

By Const By Variable

|- (tl lst) : list -> list |- lst : list

|- (tl lst) : list

By Const since list -> list is instance of

. list -> list

10/11/2018 72

A

Page 73: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example (3):IfThenElse (Repeat)

Let ={length: list -> int, lst: list }

Show

(4) (5) (6)

|- is_empty lst : bool |- 0:int |- 1 + length (tl lst) : int

|- if is_empty lst then 0

else 1 + length (tl lst) : int

Proved by deriving the proof tree in the previous slides

10/11/2018 73

Page 74: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: Fun Rule (Repeat-Done)

Show: (3)

{length: list -> int, lst: list } |-

if is_empty lst then 0

else length (hd l) + length (tl lst) : int

{length: list -> int} |-

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

: list -> int

Proved by deriving the proof tree in the previous slides

10/11/2018 74

Page 75: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 75

(Monomorphic) Let and Let Rec [Reminder]

let rule:

|- e1 : 1 {x : 1} + |- e2 : 2

|- (let x = e1 in e2 ) : 2

let rec rule:

{x: 1} + |- e1:1 {x: 1} + |- e2:2

|- (let rec x = e1 in e2 ) : 2

Page 76: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/10/2018 76

Polymorphic Let

and Let Rec

let rule:

|- e1 : 1 {x : Gen(1,)} + |- e2 : 2

|- (let x = e1 in e2 ) : 2

let rec rule:

{x : 1 } + |- e1:1 {x : Gen(1,)} + |- e2:2

|- (let rec x = e1 in e2 ) : 2

Page 77: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example

Show:

?

{} |- let rec length =

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

in

length ((::) 2 []) + length((::) true []) : int10/9/2018 77

Page 78: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: Let Rec Rule

Show: (1) (2)

{length: list -> int} {length: . list -> int}

|- fun lst -> … |- length ((::) 2 []) +

: list -> int length((::) true []) : int

{} |- let rec length =

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

in

length ((::) 2 []) + length((::) true []) : int10/11/2018 78

Our previous function example

Page 79: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (2) by ArithOp

Let ’ = {length: . list -> int}

Show:

(8) (9)

’ |- length ((::) 2 []) :int ’ |- length((::) true []) : int

{length: . list -> int}

|- length ((::) 2 []) + length((::) true []) : int

10/11/2018 79

A

Page 80: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (8) AppRule

Let ’ = {length: . list -> int}

Show:

’ |- length : int list ->int ’ |- ((::)2 []) : int list

’ |- length ((::) 2 []) : int

10/9/2018 80

A

Page 81: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (8)AppRule

Let ’ = {length: . list -> int}

Show:

By Var since int list -> int is instance of

. list -> int

(10)

’ |- length : int list ->int ’ |- ((::)2 []):int list

’ |- length ((::) 2 []) : int

10/9/2018 81

A

A

Page 82: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (10)AppRule

Let ’ = {length: . list -> int}

Show:

By Const since list is instance of . list

(11)

’|-((::) 2) : int list -> int list ’ |- [] : int list

’ |- ((::) 2 []) : int list

10/9/2018 82

A

A

Page 83: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (11)AppRule

Let ’ = {length: . list -> int}

Show:

By Const since list is instance of . list

By Const

’ |- (::) : int -> int list -> int list ’ |- 2 : int

’ |- ((::) 2) : int list -> int list

10/9/2018 83

A

A

Page 84: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (9)AppRule

Let ’ = {length: . list -> int}

Show:

’ |- ’ |-

length:bool list ->int ((::) true []):bool list

’ |- length ((::) true []) :int

10/9/2018 84

A

Page 85: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (9)AppRule

Let ’ = {length: . list -> int}

Show:

By Var since bool list -> int is instance of

. list -> int

(12)

’ |- length : bool list ->int ’ |- ((::) true []) :bool list

’ |- length ((::) true []) :int

10/11/2018 85

A

A

Page 86: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (12)AppRule

Let ’ = {length: . list -> int}

Show:

By Const since list is instance of

. list

(13)

’|-((::)true):bool list ->bool list ’|- []:bool list

’ |- ((::) true []) :bool list

10/9/2018 86

A

A

Page 87: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: (13)AppRule

Let ’ = {length: . list -> int}

Show:

By Const since bool list is instance of . list

By Const

’ |- (::):bool ->bool list ->bool list ’ |- true : bool

’ |- ((::) true) : bool list -> bool list

10/11/2018 87

A

A

Page 88: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

Polymorphic Example: Let Rec Rule – Done!

Show: (1) (2)

{length: list -> int} {length: . list -> int}

|- fun lst -> … |- length ((::) 2 []) +

: list -> int length((::) true []) : int

{} |- let rec length =

fun lst -> if is_empty lst then 0

else 1 + length (tl lst)

in

length ((::) 2 []) + length((::) true []) : int10/11/2018 88

Page 89: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 89

Two Problems

Type checking

Question: Does exp. e have type in env ?

Answer: Yes / No

Method: Type derivation

Typability

Question Does exp. e have some type in env. ? If so, what is it?

Answer: Type / error

Method: Type inference

Page 90: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

90

Type Inference - Outline

Begin by assigning a type variable as the type of the whole expression

Decompose the expression into component expressions

Use typing rules to generate constraints on components and whole

Recursively find substitution that solves typing judgment of first subcomponent

Apply substitution to next subcomponent and find substitution solving it; compose with first, etc.

Apply composition of all substitutions to original type variable to get answer

Page 91: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 91

Type Inference - Example

What type can we give to

(fun x -> fun f -> f (f x))

Start with a type variable and then look at the

way the term is constructed

Page 92: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 92

Type Inference - Example

First approximate: Give type to full expr

{ }|- (fun x -> fun f -> f (f x)) :

Second approximate: use fun rule

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

Remember constraint ( g)

Page 93: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 93

Type Inference - Example

Third approximate: use fun rule

{f : d ; x : } |- f (f x) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f(f x)) :

( g); g (d e)

Page 94: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 94

Type Inference - Example

Fourth approximate: use app rule

{f:d; x:}|- f : j e {f:d; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 95: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 95

Type Inference - Example

Fifth approximate: use var rule, get

constraint dj e, Solve with same

Apply to next sub-proof

{f:d; x:}|- f : j e {f:d; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 96: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 96

Type Inference - Example

Current subst: {dj e}

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 97: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 97

Type Inference - Example

Current subst: {dj e} Use App Rule

{f:je; x:}|- f:zj {f:je; x:}|- x:z

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 98: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 98

Type Inference - Example

Current subst: {dj e}

Var rule: Solve zj je Unification

{f:je; x:}|- f:zj {f:je; x:}|- x:z

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 99: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 99

Type Inference - Example

Current subst: {ze, je} o {dj e}

Var rule: Solve zj je Unification

{f:je; x:}|- f:zj {f:je; x:}|- x:z

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 100: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 100

Type Inference - Example

Current subst: {ze, je, dee}

Apply to next sub-proof

(done)… {f:je; x:}|- x:z

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 101: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 101

Type Inference - Example

Current subst: {ze, je, dee}

Apply to next sub-proof

(done)… {f:ee; x:}|- x:e

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 102: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 102

Type Inference - Example

Current subst: {ze, je, dee}

Var rule: e

… {f:ee; x:}|- x:e

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 103: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 103

Type Inference - Example

Current subst: {e}o{ze, je, dee}

Solves subproof; return one layer

… {f:ee; x:}|- x:e

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 104: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 104

Type Inference - Example

Current subst: {e, z, j, d}

Solves this subproof; return one layer

… {f:j e; x:}|- f x : j

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 105: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 105

Type Inference - Example

Current subst: {e, z, j, d}

Need to satisfy constraint g (d e),

given subst, becomes: g (() )

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 106: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 106

Type Inference - Example

Current subst:

{g (() ),e, z, j, d}

Solves subproof; return one layer

{f : d ; x : } |- (f (f x)) : e

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g); g (d e)

Page 107: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 107

Type Inference - Example

Current subst:

{g (() ),e, z, j, d}

Need to satisfy constraint ( g)

given subst: ( (() ))

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

( g);

Page 108: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 108

Type Inference - Example

Current subst:

{ ( (() )),

g (() ),e, z, j, d}

Solves subproof; return on layer

{x : } |- (fun f -> f (f x)) : g

{ } |- (fun x -> fun f -> f (f x)) :

Page 109: Programming Languages and Compilers (CS 421)...10/9/2018 2 Terminology Type: A type tdefines a set of possible data values E.g. short in C is {x| 215 - 1 x -215} A value in this set

10/11/2018 109

Type Inference - Example

Current subst:

{ ( (() )),

g (() ),e, z, j, d}

Done: ( (() ))

{ } |- (fun x -> fun f -> f (f x)) :