Type Theory For Absolute beginners
@hannelita - #mixit17
#mixit - @hannelita
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
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
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"
Data
Operations
"Strong"
"Weak"
"Static"
"Dynamic"
* Definitions are not exact on literature
Java: static (why?)
Python: dynamic
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?
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
extends ==> extends ClassType
implements ==> implements InterfaceTypeList
throws ==> throws ClassTypeList
A ==> ClassType
B ==> InterfaceType(List)
C ==> ClassType(List)
ζ ==> ClassType
(people like letters from the greek alphabet)
http://www.jot.fm/issues/issue_2007_09/article3.pdf
( ::= is the definition symbol)
All mappings of a ClassType have a ClassDeclaration in Java (the same for interfaces). We will use the symbol
m
Keep expanding the definitions:
http://www.jot.fm/issues/issue_2007_09/article3.pdf
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!
Understanding lambda calculus (out of scope of this presentation) will help you come out with these relations.
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
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?
Functions can transform data
public Integer nextInt(Integer number) { ... }
A: Integer
A → A
Functions can transform data
public Integer nextInt(Integer number) { ... }
Think about Programming:
x = x + 1
But that does not make any sense in Maths! How can we represent that?
Functions can transform data
public Integer nextInt(Integer number) { ... }
function: λx.x+1
That looks like mathematics!
x = x + 1
λ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)
"In Java, every class type that you define will be a subclass of a class"
class => ζ
environment => Γ
class relation
(subclass or class itself) =>
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)
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
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
General subclass chain
A subclass or the class itself
Γ
⊢
ζ1
ζ2
Valid type!
validType(Γ, ζ)
Γ
⊢
ζ1
ζ2
True!
Java type system - proposed type representations:
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!
Most type theory studies are applied to functional languages.
But you can analyse languages that are not purely funcional as well.
http://slides.com/hannelitavante-hannelita/type-theory-101-35#/
http://slides.com/hannelitavante-hannelita/devoxx-be-notes-type-theory#/
Questions?
hannelita@gmail.com
@hannelita