Type Theory 101

Type Theory For Absolute beginners

@hannelita - #mixit17

Hi! I'm Hanneli (@hannelita)

• Computer Engineer
• Programming
• Electronics
• Math <3 <3
• Physics
• Lego
• Meetups
• Animals
• Coffee
• Pokémon
• GIFs

#mixit  - @hannelita

Why 'Type Theory?'

• Frameworks and architecture are important topics
• But what are the boundaries of computer science?
• We need theory to improve our practical tools.

Why 'Type Theory?'

In mathematics, logic, and computer

science, a type theory is any of a class of

formal systems, some of which can serve

as alternatives to set theory as a

foundation for all mathematics. In type

theory, every "term" has a "type" and

operations are restricted to terms of a

certain type.

Type theory is closely related to (and in

some cases overlaps with) type systems.

`https://en.wikipedia.org/wiki/Type_theory`

Goals

Understand what type theory is 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
• Verbosity
• Memory management

Wait - what is a type system?

```"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"```

Wait - what is a type system?

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

Data

Operations

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?

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 - Example in Java:

extends        ==> extends ClassType

implements ==> implements InterfaceTypeList

throws          ==> throws ClassTypeList

#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)

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 - Example in Java

`http://www.jot.fm/issues/issue_2007_09/article3.pdf`
`( ::= is the definition symbol)`

#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 - 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(ζ)

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/mgs2011-tlc.pdf`

#6 Lambda Calculus

With Lambda Calculus we can define a Type itself using mathematical tools

"A type is a collection of objects having similar structure"

`http://www.nuprl.org/book/Introduction_Type_Theory.html`

How do I know what is a similar structure?

#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) { ... }``

x = x + 1

But that does not make any sense in Maths! How can we represent that?

#6 Lambda Calculus

Functions can transform data

``public Integer nextInt(Integer number) { ... }``

function: λx.x+1

That looks like mathematics!

x = x + 1

#6 Lambda Calculus

λx.x+1 is in (A → A)

Lamba Calculus help us to build these statements, highly connected to predicate logic

f(x) = x+1 (Integer →Integer)

#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:

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

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

Examples:

• Is everything immutable here? (prove it)
• Is everything an object in language X? (prove it!)
• Do I have co-variance? (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/ref-immutability-oopsla2004-abstract.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!

Type Theory

Most type theory studies are applied to functional languages.

But you can analyse languages that are not purely funcional as well.

References

• http://blogs.atlassian.com/2013/01/covariance-and-contravariance-in-scala/
• 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/hannelitavante-hannelita/type-theory-101-35#/

Session at Devoxx Belgium 2016

http://slides.com/hannelitavante-hannelita/devoxx-be-notes-type-theory#/

Special Thanks

• B.C., for the constant review and support
• Professor M. Coutinho (UNIFEI)
• MiXiT Team

Merci! :)

Questions?

hannelita@gmail.com

@hannelita