















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of types in programming languages, including the classification of values, basic and compound types, type checking and inference, and the importance of types in modern programming. It covers topics such as type declarations, type errors, and the relative type-safety of different languages.
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Represent concepts from problem domain Accounts, banks, employees, students Represent different implementation of values Integers, strings, floating points, lists, records, tuples …
Support organization of concepts Separate types for separate concepts from problem domain Identify and prevent errors Compile-time and run-time type checking Prevent meaningless computation 3 + true - “Bill” Support efficient translation (by compilers) Short integers require fewer bits Access record component by a known offset Use integer units for integer operations
have different layouts have different operations
int arr[100] arr: array(int,100) (3, 4, “abc”) : int * int * string int *x x : pointer(int) int f(int x) { return x + 5} f : int int
When a value is misinterpreted or misused with unintended semantics, a type error occurs
Not safe: casts, pointer arithmetic, …
Almost safe Dangling pointers: Pointers to locations that have been deallocated No language with explicit de-allocation of memory is fully type- safe
Lisp, ML, Smalltalk, Java Dynamically typed: Lisp, Smalltalk Statically typed: ML, JAVA
Compile-time vs Run-time Type Checking
Value x must have type float
Lisp list: elements can have different types ML list: all elements must have the same type
Example: Java (array bound check at runtime)
Find most general type by solving constraints Lead to polymorphism
Find Type Errors Without Running the Program
It can hold only values of this type
It maps input values to a return value It can return only values of this type
These rules specify the proper usage of each operator
(define (Add exp num) (cond ((null? exp) exp) ((cons? exp) (cons (Add (car exp) num) (Add (cdr exp) num))) ( (number? exp) (+ exp num)) (else exp)))
(null? exp) false (cons? exp) false (number? exp) true (+ exp num) ‘int
(lambda (a b c) (+ a b c)) ((a int) (b int) (c int)) (a+b+c) : int ((a float) (b int) (c int)) (a + b + c) : float ((a list) (b int) (c int)) (a + b + c): type_error
Each variable has a single type each expression has a single type
Each variable may have different types depending on the running context each expression may have different types
f (a, b, c) { return a + b + c; } +: intintint a: int; b: int; c: int; a + b + c : int +: floatfloatfloat a : float; b : float; c : float; … type error
(lambda (a b c) (+ (+ a b) c)) +: number*numbernumber a: number; b : number; c : number; (+ a b) : number
When each operator has a single type definition, we can automatically infer types of variables and expressions When each operator has multiple type definitions (evaluation rules), type inference can easily fail An operator is overloaded if it has multiple type definitions
+ is overloaded because it has two types. Most operators in a static type system have a single type In many cases, unique type may be polymorphic.