Type Theory 101
Type Theory For Absolute beginners
Hi! I'm Hanneli (@hannelita)
 Computer Engineer
 Programming
 Electronics
 Math <3 <3
 Physics
 Lego
 Meetups
 Animals
 Coffee
 Pokémon
 GIFs
#confoo  @hannelita
Disclaimer
GIFs :)
Quick session
Lots of theory
And mathematics
No advanced Type Theory
Goals
Understand what is type theory about
Understand how can we jump from language analysis to mathematics (it is not magic)
Understand some benefits of this analysis
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
How do you choose a programming language?
 By company
 By popularity
 By team
 By deadline to deliver a project
 By project goal
 By available tools
 That was the only language I learned at school
 Somebody told me I should use that
 I really don't know
How often do you consider the following items when choosing a language?
 Type System
 Immutability
 Avoidance to runtime errors
 Paradigm
 Verbosity
 Memory management
Wait  what is a type system?
Let's ask Wikipedia:
"In programming languages, a type system is a collection of rules that assign a property called type to various constructs a computer program consists of, such as variables, expressions, functions or modules"
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
Wait  what is a type system?
In all languages, even in Assembly, we have at least two components:
Data
Operations
Not all of the available operations make sense to all kinds of data.
If you use incompatible pieces of data for an operation, you will have a representation error
Programming languages use a type system to look at a program and determine if a representation error will happen or not
What are the possible strategies that a type system can use to handle representation errors?
Strategies
 Generate a compile error
 Perform a type check before run the code
 Well defined error set
 Unpredictable runtime errors
 Try implicit conversion
 A compiler tags pieces of code and tries to infer if the behaviour will be valid or not (before the program runs)
 A compiler / interpreter generates code to keep track of the data
Strategies
 Generate a compile error
 Perform a type check before run the code
 Well defined error set
 Unpredictable runtime errors
 Try implicit conversion
 A compiler tags pieces of code and tries to infer if the behaviour will be valid or not (before the program runs)
 A compiler / interpreter generates code to keep track of the data
"Strong"
"Weak"
"Static"
"Dynamic"
* Definitions are not exact on literature
You don't have to choose only one alternative
Java: static (why?)
Python: dynamic
But how can we perform the 'type check' mentioned before?
Have you ever heard someone saying "Language X has a terrible type system, it is a total mess!" Why? What does it even mean? How can we prove that?
We need some Mathematics
The steps to Type Theory
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
#1:
Given a language, collect all the keywords and analyse the grammar for each of these works individually.
#1  Example in Java:
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
#2
Make it look like Mathematics  replace text with variables :)
#2  Example in Java:
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
A ==> ClassType
B ==> InterfaceType(List)
C ==> ClassType(List)
#2  Example in Java:
ζ ==> ClassType
(people like letters from the greek alphabet)
#3
Group these results in sets and remove duplicates. These sets will reveal the types.
A very difficult task in science is grouping topics appropriately.
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
#4
Use symbolic logic to simplify your system
#4  Example in Java
http://www.jot.fm/issues/issue_2007_09/article3.pdf
( ::= is the definition symbol)
(Of course, you can come up with a different grouping)
#4
Every program (in Java) has its set of Classes and Variables. We call it Environment (Γ):
#4
All mappings of a ClassType have a ClassDeclaration in Java (the same for interfaces). We will use the symbol
m
#4
Keep expanding the definitions:
http://www.jot.fm/issues/issue_2007_09/article3.pdf
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
#5
Use predicate logic to analyse your system. Start with true statements ('wellformed'):
#5  Example in Java
validType = true
validType (primitive) = true
validType (π) = true
validType (environment, primitive) = true
validType (Γ, π) = true
validClass(Type) = ClassMap of the environment for that type
validClass(τ) = Γ classMap(τ)
validType(Class) =
validClass(Type)
validType(ζ) = validClass(ζ)
#6  Breathe
Free GIF!
#6  Lambda Calculus
Understanding lambda calculus (out of scope of this presentation) will help you come out with these relations.
#6  Bonus  Lambda Calculus
Lambda Calculus is about formal function theory. We can apply them to functional programming. We can also apply the ideas to general functions in programming.

http://www.cs.le.ac.uk/people/amurawski/mgs2011tlc.pdf
#6 Lambda Calculus
With Lambda Calculus we can define a Type itself
"A type is a collection of objects having similar structure"
http://www.nuprl.org/book/Introduction_Type_Theory.html
#6 Lambda Calculus
Functions can transform data
public Integer nextInt(Integer number) { ... }
A: Integer
A → A
#6 Lambda Calculus
Functions can transform data
public Integer nextInt(Integer number) { ... }
function: λx.x+1
That looks like mathematics!
#6 Lambda Calculus
λx.x+1 is in (A → A)
Lamba Calculus help us to build these statements, highly connected to predicate logic
#7  Write some statements that you can prove:
"In Java, every class type that you define will be a subclass of a class"
#7  Sketch a mathematical expression:
class => ζ
environment => Γ
class relation
(subclass or class itself) =>
#7  Sketch a mathematical expression:
In an environment Γ,
Γ
we can prove
⊢
that a class of a
certain type
ζ1
ζ2
is a subclass of another type
or the other type itself (Object)
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
#8  We can almost read this:
#8:
A valid class. A class type that is a subclass of another type
The super class of a type. But the super class also is a subclass of another type
#8:
A valid class. A class type that is a subclass of another type
The super class of a type. But the super class also is a subclass of another type
#8:
General subclass chain
A subclass or the class itself
Γ
⊢
ζ1
ζ2
#8:
Valid type!
validType(Γ, ζ)
Γ
⊢
ζ1
ζ2
True!
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
Why is this so important?
 Reduce runtime errors by checking the types
 IDEs can perform a better analysis of your code based on logical statements
 Different languages have different type systems
 You have a solid point to choose a language
Sometimes it is difficult to find an equivalent type across different languages
Collect the characteristics that are important for you and compare them across the languages using the ideas of type theory
Examples:
 Is everything immutable here? (prove it)
 Is everything an object in language X? (prove it!)
 Do I have covariance? (related to subtyping)
Agenda
 Choosing a programming language
 Quick intro about type systems
 Sketching the possible types
 Symbolic Logic analysis
 Predicate logic
 Getting there!
 Why is this important?
 Challenges
Challenges
 There is no single way to describe a type system
 It is hard to find equivalences between languages
 It is a lot of mathematics!
 We have lots of theory and very few time to study them
Challenges
 http://www.jot.fm/issues/issue_2007_09/article3.pdf
 http://www.dsi.unive.it/myths/GC2004/Slides/Zucca_slides.pdf
 http://groups.csail.mit.edu/pag/pubs/refimmutabilityoopsla2004abstract.html
 http://pubs.doc.ic.ac.uk/JavaProbablySound/JavaProbablySound.pdf
Java type system  proposed type representations:
Final notes
Don't be scared of mathematics  the concepts, itself, are not so difficult!
There are several active researched focusing on Type Theory!
Even if you don't have a PhD, you can learn and use type theory concepts!
References
 http://blogs.atlassian.com/2013/01/covarianceandcontravarianceinscala/
 http://cseweb.ucsd.edu/~atl017/papers/pldi11.pdf
 STEPANOV, A. Elements of Programming.
 PIERCE, B. Types and Programming Languages
 THOMPSON, S. Type Theory and Functional Programming (free ebook!)
 MICHAELSON, G. An Introduction to Functional Programming Through Lambda Calculus.
Session at Open Source Bridge 2016
http://slides.com/hannelitavantehannelita/typetheory10135#/
Session at Devoxx Belgium 2016
http://slides.com/hannelitavantehannelita/devoxxbenotestypetheory#/
Special Thanks
 B.C., for the constant review and support
 Professor M. Coutinho (UNIFEI)
 Devoxx Team
Thank you :)
Questions?
hannelita@gmail.com
@hannelita
Type Theory 101  Confoo Vancouver 2016
By Hanneli Tavante (hannelita)
Type Theory 101  Confoo Vancouver 2016
 789