0% found this document useful (0 votes)
52 views

Types S

This document discusses types and type systems in programming languages. It covers: 1. Examples of types like int, string, and functions. Types determine permissible operations and how values are interpreted. 2. Type errors occur when execution does not match intended semantics, like adding a string and int. Languages use static or dynamic checking. 3. Type inference allows determining types without declarations by solving constraints. The ML type system is designed for inference. 4. Polymorphism allows types to contain parameters. ML supports parametric polymorphism while C++ uses templates.

Uploaded by

Marko Schuetz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Types S

This document discusses types and type systems in programming languages. It covers: 1. Examples of types like int, string, and functions. Types determine permissible operations and how values are interpreted. 2. Type errors occur when execution does not match intended semantics, like adding a string and int. Languages use static or dynamic checking. 3. Type inference allows determining types without declarations by solving constraints. The ML type system is designed for inference. 4. Polymorphism allows types to contain parameters. ML supports parametric polymorphism while C++ uses templates.

Uploaded by

Marko Schuetz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS316

Types
Type
· example · non--example
− int − 2
− string − "Bill"
− int -> string
− (int -> bool) -> string

what is permissible as type depends on programming lan-


guage
Functions of Types
· organize and document program

− type age = int;;

− type name = string;;

· give meaning to bit--patterns: how is the bit--pattern to


be interpreted, what are the permissible operations

· find/prevent programming errors: 3 + "Test"


Type Error
examples

· x() where x is not a function

· execute invalid instruction

· intAdd(3,4.5)

· interpret 4.5 as int

values must be valid for the operations performed


Type Error: General Definition
· A type error occurs when execution of a program is not
faithful to the intended semantics

· intended semantics of 4.5: float (interpretation, permis-


sible operations)

· used with integer addition: there interpreted as integer


Static and Dynamic Checking
· static: at compile--time

· dynamic: at run--time

· trade--off

− both prevent type--errors

− dynamic: overhead

− static: less flexible

? Lisp: list can have e.g. integer and function ele-


ments alternating

? ML: every list element has the same type


General Type--Checking
· Scheme function

(define (tx x)
(cond
((< x 10) x)
(else (car x))))
(if (very-complicated-boolean-expression)
(tx 2)
(tx 11))

· can we statically determine whether a run--time type--


error may occur?
Relative Type--Safety
· unsafe: BCPL family: e.g. C/C++

casts, pointer arithmetic

· safer: Algol family: Ada Pascal

explicit allocation and deallocation can lead to dangling


pointers, no language with explicit deallocation of mem-
ory is safe

· safe: Lisp Ml, Smalltalk, Java

− Lisp, Smalltalk: dynamically typed

− ML, Java: statically typed


Type--Checking and Type--Inference
· type--checking: use type annotations to check agreement
with uses

example:

int f(int x) { return x+1; }


int g(int x) { return f(y+1)*2; }

· type--inference: infer type from uses

example:

f(x) { return x+1; }


g(x) { return f(y+1)*2; }

· ML type--system designed for inference


ML Type--Inference
· example

# let f = function x -> 2*x;;


val f : int -> int = <fun>

· how? informally

− label nodes with type information =⇒ equations

− solve equations using unification

Rule for λ--expressions:

function :: α α = β→γ

arg :: β exp :: γ
Rule for applications:

@ :: α β = γ→α
exp1 :: β exp2 :: γ

Example:

function :: α δ = ε→γ
γ = η→β
x :: ζ @ :: β α = ζ→β
ζ = η
@ :: γ x :: η
ε = int
δ = int → int → int
(*) :: δ 2 :: ε
γ = int → int
η = int
β = int
let f = function x -> 2*x;;

· example

1. f has some type, say, α


2. binding to right--hand side makes f’s type equal to
expression’s type, β: thus α = β
3. function expression must have function type, say, β1 →
β2
4. multiplication expression now has type β2
int
n
5. * has type γ ∗ γ → γ with γ =
real
6. thus x has type γ
7. 2 has type γ, but also type int, so γ = int
8. whenever more precise information is available for a
type variable it is substituted
9. thus x now has type int this also makes β1 = int
Type Inference: Application
· f x

· the entire expression has a type: α

· f has a type β and β = β1 → β2 and α = β2

· x has a type γ and γ = β1

β = β1 → β2
α = β2
γ = β1

can substitute

β = γ→α
α = β2
γ = β1
Type Inference: Abstraction
· function x -> e

· function expression has type: α

· must be function type: α = α1 → α2

· x has a type: β, β = α1

· e has a type: γ, γ = α2

substituting we get

α = β→γ
β = α1
γ = α2
Type Inference: Polymorphism 1
# let f = function g -> g(2);;
val f : (int -> ’a) -> ’a = <fun>

· function expression has type α and α = α1 → α2

· g has type β and β = α1

· g is used as function so β = β1 → β2

· 2 has type int, g applied to 2 so β1 = int

· function body has type α2 and α2 = β2


α = α1 → α2
β = α1
β = β1 → β2
β1 = int
α2 = β2

unifying we get

α = (int → β2) → β2
β = int → β2
α1 = int → β2
β1 = int
α2 = β2
Applying Polymorphic Functions
# let f g = g 2;;
val f : (int -> ’a) -> ’a = <fun>

applications

# f(function x -> x+2);;


- : int = 4
# f(function x -> x=2);;
- : bool = true
type error

# let t = function x -> not x;;


val t : bool -> bool = <fun>
# f t;;
Characters 2-3:
f t;;
^
This expression has type bool -> bool but is here used with
> ’a
Using Inferred Type
· we programmed

let rec reverse = function


[] -> []
| x :: rest -> reverse rest;;
val reverse : ’a list -> ’b list = <fun>

· type of elements in input list could differ from that in


output list =⇒ mistake

· without inference a type--checker would agree had we giv-


en type

reverse : ’a list -> ’a list


Summary Types--Inference
· infers type of expressions

− can be used without type--declarations

− finds most general type by solving constraints

− naturally handles parametric polymorphism

· suitable for static checking

· good support for error detection

may help find programming errors even in the absence of


type--errors
Kinds of Polymorphism
· parametric: types may contain parameters to be filled
with types

· ad--hoc: overloading, same name for different implemen-


tations

· subtype: where a type is expected any subtype of the type


may be used
Parametric Polymorphism
· type parameters may be instantiated to any type

· f : ’t -> ’t =⇒

f : int -> int,

f : bool -> bool,

f : (int -> ’s) -> (int -> ’s)

· g : ’a -> ’b -> ’a * ’b =⇒

g : int -> bool -> int * bool

but not g : int -> bool -> int * int


Ad--Hoc Polymorphism
· single identifier names different implementations

· type context determines selection among implementations

· + can be overloaded implementing:

− integer addition,

− string concatenation,

− boolean or,

− anything even if it doesn’t make sense

· in ML:

− (+) : int * int -> int and

− (+) : real * real -> real only


Comparing Parametric Polymorphism
· ML polymorphic functions

− no type information in declaration

− type information is inferred

− inference substitutes type variables as needed

· C++ templates

− declaration carries type information

− template construct allows introduction of type vari-


ables
Polymorphism: Example
· ML

# let swap(x,y) =
let tmp = !x in x := !y; y := tmp;;
val swap : ’a ref * ’a ref -> unit = <fun>

· C++

template <typename T>


void myswap(T& x, T& y) {
T tmp = x; x = y; y = tmp;
}
Polymorphic Sort
· C++ polymorphic sort function

template <typename T>


void sort(int count, T* A[count]) {
for (int i = 0; i < count-1; i++)
for (int j = i+1; j < count-1; j++)
if (A[j] < A[i]) swap(A[i], A[j]);
}

· parts depending on type:

− indexing into array

− < will be overloaded

− swap’s type
ML Overloading
· very limited in ML

− + overloaded

− illegal to define function body with ambiguous type:


e.g. x+y

· absent from OCaml: overloading together with paramet-


ric polymorphism and object--orientation would make type
system too complicated

You might also like