Type Systems
Ken Q Pu, Faculty of Science, Ontario Tech University
Why types?
Let's first understand what a type system is.
Ken Q Pu, Faculty of Science, Ontario Tech University
Types
Types are descriptions of values.
Values can be:
Each value can be described by one or more types.
3
"Hello"
{:name "Einstein"
:IQ 160}
(defn add [x y]
(+ x y))
Integer
String
Record
:name String
:IQ Int
Function
(Num, Num) -> Num
Ken Q Pu, Faculty of Science, Ontario Tech University
Type Signature
"Hello"
{:name "Einstein"
:IQ 160}
(defn add [x y]
(+ x y))
String
Record
:name String
:IQ Int
Function
(Num, Num) -> Num
Expressions to be evaluated during runtime.
Type signature, or simply type of the expression, to be determined at compile time.
Ken Q Pu, Faculty of Science, Ontario Tech University
Type System
A type system is a formal sublanguage that can express the type signatures for all possible values supported by the programming language.
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4)))
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4)))
Float
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4)))
Float
Float
Float
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4 )))
Float
Float
Float
Int
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4 )))
Float
Float
Float
Int
Int -> Int
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4) ))
Float
Float
Float
Int
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
( + pi
(inc 4) ))
Float
Float
Float
Int
(Num, Num) -> Num
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
( + pi
(inc 4) ))
Float
Float
Float
Int
(Num, Num) -> Num
Num
Num
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4)) )
Float
Float
Float
Ken Q Pu, Faculty of Science, Ontario Tech University
Statically typed language
A programming language is statically typed if all expressions in the source code have a valid type signature. Furthermore, the expression types must be determined before the program executes.
(let [pi 3.14]
(+ pi
(inc 4)))
Float
Computing the type signature of all expressions is called type inference.
Type inference is only done for statically typed languages. Dynamic typed languages computes the type signatures based on data values at runtime.
Ken Q Pu, Faculty of Science, Ontario Tech University
Clojure is not statically type.
Clojure compiler does not determine the type information of expressions during compile time.
All type information are determined at runtime.
(let [x (get-value ...)]
x)
Clojure only knows the type of x after get-value is executed.
Ken Q Pu, Faculty of Science, Ontario Tech University
Why types?
Ken Q Pu, Faculty of Science, Ontario Tech University
Runtime errors
Certain computation cannot be carried out at runtime, and the operating system has no choice but to stop the program execution immediately.
(let [x [:a :b :c]]
(println (nth x 5)))
(+ 3 "four")
Illegal data access. (nth x 5) cannot be evaluated because x only has three elements.
Illegal function invocation. Parameters of + cannot be mixed between numbers and strings.
Ken Q Pu, Faculty of Science, Ontario Tech University
Runtime errors are dangerous
Runtime errors can cause the system to be stuck in undesirable states.
The program can terminate prematurely beyond the expectation of the programmer.
(defn move-file [source target]
(let [content (read-file source)]
(remove-file source)
(println (+ "Bytes to move: " (count content)))
(write-file target content)))
What is the runtime behaviour?
(move-file "important.txt"
"passwords.txt")
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
Type checking is the verification that all expressions have valid types.
More details will be discussed on the type checking algorithm.
( + "Bytes to move: " ( count content ) )
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Array ✔
Iterable ✔
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Iterable -> Int ✔
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Iterable -> Int ✔
Iterable ✔
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Iterable -> Int ✔
Iterable ✔
Int ✔
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Int ✔
String ✔
Int, Int -> Int
or
String, String -> String
Ken Q Pu, Faculty of Science, Ontario Tech University
Static Analysis: type checking
( + "Bytes to move: " ( count content ) )
Int ✔
String ✔
Int, Int -> Int
or
String, String -> String
ERROR ✖
Ken Q Pu, Faculty of Science, Ontario Tech University