Notes on Type Theory
#Devoxx Belgium  2016
For Absolute beginners
Hi! I'm Hanneli (@hannelita)
 Computer Engineer
 Programming
 Electronics
 Math <3 <3
 Physics
 Lego
 Meetups
 Animals
 Coffee
 Pokémon
 GIFs
#Devoxx  @hannelita
Disclaimer
GIFs :)
Quick session
Lots of theory
And mathematics
No advanced Type Theory
#Devoxx  @hannelita
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
#Devoxx  @hannelita
Agenda
#Devoxx  @hannelita
 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?
#Devoxx  @hannelita
 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
#Devoxx  @hannelita
How often do you consider the following items when choosing a language?
 Type System
 Immutability
 Avoidance to runtime errors
 Paradigm
 Verbosity
 Memory management
#Devoxx  @hannelita
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"
#Devoxx  @hannelita
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
#Devoxx  @hannelita
Wait  what is a type system?
#Devoxx  @hannelita
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.
#Devoxx  @hannelita
If you use incompatible pieces of data for an operation, you will have a representation error
#Devoxx  @hannelita
Programming languages use a type system to look at a program and determine if a representation error will happen or not
#Devoxx  @hannelita
What are the possible strategies that a type system can use to handle representation errors?
#Devoxx  @hannelita
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
#Devoxx  @hannelita
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?)
... and somewhat strong
#Devoxx  @hannelita
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?
#Devoxx  @hannelita
We need some Mathematics
#Devoxx  @hannelita
The steps to Type Theory
#Devoxx  @hannelita
#Devoxx  @hannelita
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
#Devoxx  @hannelita
#1:
Given a language, collect all the keywords and analyse the grammar for each of these works individually.
#Devoxx  @hannelita
#1  Example in Java:
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
#Devoxx  @hannelita
#2
Make it look like Mathematics  replace text with variables :)
#Devoxx  @hannelita
#2  Example in Java:
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
A ==> ClassType
B ==> InterfaceType(List)
C ==> ClassType(List)
#Devoxx  @hannelita
#2  Example in Java:
ζ ==> ClassType
#Devoxx  @hannelita
(people like letters from the greek alphabet)
#3
Group these results in sets and remove duplicates. These sets will reveal the types.
#Devoxx  @hannelita
A very difficult task in science is grouping topics appropriately.
#Devoxx  @hannelita
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
#Devoxx  @hannelita
#4
Use symbolic logic to simplify your system
#Devoxx  @hannelita
#4  Example in Java
http://www.jot.fm/issues/issue_2007_09/article3.pdf
(Of course, you can come up with a different grouping)
#Devoxx  @hannelita
#4
Every program has its set of Classes and Variables. We call it Environment (Γ):
#Devoxx  @hannelita
#4
All mappings of a ClassType have a ClassDeclaration in Java (the same for interfaces). We will use the symbol
m
#Devoxx  @hannelita
#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
#Devoxx  @hannelita
#5
Use predicate logic to analyse your system. Start with true statements ('wellformed'):
#Devoxx  @hannelita
#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(ζ)
#Devoxx  @hannelita
#6  Breathe
Free GIF!
#Devoxx  @hannelita
#6  Lambda Calculus
Understanding lambda calculus (out of scope of this presentation) will help you come out with these relations.
#Devoxx  @hannelita
#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
#Devoxx  @hannelita
#7  Write some statements that you can prove:
"In Java, every class type that you define will be a subclass of a class"
#Devoxx  @hannelita
#7  Sketch a mathematical expression:
class => ζ
environment => Γ
class relation
(subclass or class itself) =>
#Devoxx  @hannelita
#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)
#Devoxx  @hannelita
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
#Devoxx  @hannelita
#8  We can almost read this:
#Devoxx  @hannelita
#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
#Devoxx  @hannelita
#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
#Devoxx  @hannelita
#8:
General subclass chain
A subclass or the class itself
Γ
⊢
ζ1
ζ2
#Devoxx  @hannelita
#8:
Valid type!
validType(Γ, ζ)
Γ
⊢
ζ1
ζ2
True!
#Devoxx  @hannelita
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
#Devoxx  @hannelita
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
#Devoxx  @hannelita
Bonus #1  Clojure type system
"Clojure was not designed to support a static type system" (https://s3.amazonaws.com/github/downloads/frenchy64/papers/ambrosehonours.pdf)
But you can treat it as Typed.
#Devoxx  @hannelita
Bonus #1  Notes on Clojure type system
 DataType (similar to ClassType in Java)
 ProtocolType (similar to InterfaceType in Java)
 SingletonType
 HeterogeneousMaps
 NullableType (no equivalence in Java)
#Devoxx  @hannelita
Bonus #2  Notes on Scala type system
 AnyType
 NothingType
 ParametrisedTypes
 BoundedType
 Static and Strong
http://www.slideshare.net/dgalichet/demystifyingscalatypesystem
Sometimes it is difficult to find an equivalent type across different languages
#Devoxx  @hannelita
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
#Devoxx  @hannelita
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
#Devoxx  @hannelita
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:
#Devoxx  @hannelita
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#/
#Devoxx  @hannelita
Special Thanks
 B.C., for the constant review and support
 Professor M. Coutinho (UNIFEI)
 Devoxx Team
#Devoxx  @hannelita
Thank you :)
Questions?
hannelita@gmail.com
@hannelita
#Devoxx  @hannelita
Notes on Type Theory  Devoxx 2016
By Hanneli Tavante (hannelita)