# Type Theory 101

@hannelita

## Hi!

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

# Why Type Theory??

## Goals

• Understand what type theory is about
• Understand its main base areas
• Type theory general idea is too difficult to be understood in 40minutes
• You can't explain it with simple examples

# Mathematics + Programming + Too much information

## Agenda

• Logic
• Functional Programming
• Intro to Lambda Calculus and Mathematical Formalism
• Type Theory

# Would it be possible to have a way to "pre-prove" our code in terms of pure logic?

## p:P

• This is not an emoji
• "The value p has the type P"
• And so what? Formalism to analyse functions, implications and mathematical constructions.
• Look carefully and think about functions (form Algebra)
• D(f):{2,9} or
• Value and Types could express domains and ranges in functions!

# What is correct?

• Write arguments that can be checked mechanically
• Study properties
• Elaborate semantic meanings
• Propositional and Predicate Logic

# How do I evaluate code under Logic?

Sometimes the imperative mode of writing code won't help at all

WAT

## It's simple:

• Lambda calculus
• Lisp
• Scala
• Clojure
• Papers
• Category Theory
• λ
• λ
• λ

## Functional Programming has some resources to write code:

• High Order Functions
• Monoids
• functors

# Example 1 - with Pokémon

level >=16

f(evol)

output level is the same as the input

How can I get a Charizard from a Charmander?

level >=16

f(evol)

output level is the same as the input

output level is the same as the input

f(up)

level = 36

level=36

f(evol)

level = 36

# Or:

level >=36

f(evol)

output level is the same as the input

f(evol)

output level is the same as the input

# High Order Function

f(x) => g(x) => h(x)

# Example 2 - Buildings and floors

## Team

• Building A, 2 floors
• Building B, 4 floors
• Building C, 1 floor

## Floors sum: 7

• Building A, 2 floors
• Building C, 1 floors
• Building B, 4 floors

## Floor sum: 7

• Building B, 4 floors
• Building C, 1 floor
• Building A, 2 floors
• Building C, 1 floor
• Building A, 2 floors
• Building B, 4 floors
`Credits: @bitemyapp and @argumatronic - http://haskellbook.com/`

# We can have a space with 0 floors.

## Team

• Building A, 2 floors
• Building C, 1 floor
• Building B, 4 floors
• Building D, 0 floor

# Building with zero floors works as an identity element to the floor count.

## reduce

`(reduce + 0[100 100 100 100 100])  ;;=> 500`

# Attendees

## at a Conference

{A: 15},

{B: 5},

{C: 10},

{D: 5}

[

]

{A: 15},

{B: 5},

{C: 10},

{D: 5}

[

]

[{A: 14}],

{B: 5},

{C: 10},

{D: 5}

[

]

[]

[            ]

[                     ]

{A: 15},

{B: 5},

{C: 10},

{D: 5}

[

]

{A: 14},

{B: 5},

{C: 10},

{D: 5}

```(flatten donate_badge [{A:15}])

```
```;;=> [{A: 14}, {B: 5}, {C: 10},
{D: 5}]```

# Collection

### No matter which badge set you have, you can:

• Get the element
• Apply a function if convenient
• flatten and guarantee same level type on output
• Allow chained calls

# Objects => Functions

• Individual Variables => x, y, z
• Applications => (e1, e2) [apply expression e1 to e2]
• Abstractions => (λx, e) [f(x) = e]

# λ-calculus: Topics

• Substitution
• Evaluation
• http://www.cs.le.ac.uk/people/amurawski/mgs2011-tlc.pdf

# Constructive vs classical Mathematics

## "If it's contradictory for no object x has the property P(x), then there's an object x with the property P(x)."

### Too confusing, did not read: Which mathematical structures can you construct given that something exists?

Ref: http://plato.stanford.edu/entries/mathematics-constructive/

Types

Functions

Propositions

Proofs

Isomorfism

Abstraction

# 4. Validate by constructive mathematics

## We can apply all this formalism for

• Naming
• Generalisations
• Derivability

And then we can analyse programming statements with Mathematics.

## Applications

• IDE Inference
• Tests
• Pattern matching
• Language optimisations
• Set Theory and Group theory for analysing types

# If you would like to learn it with Pokémon, check:

http://slides.com/hannelitavante-hannelita/pkmn_fp_tt_en

## Credits and references:

• Codeminer 42
• GIRARD, Jean-Yves. Proofs and Types.
• @lafp, @romulostotel, @pedrofelipee: GIFs
• B.C.
• http://www.cs.le.ac.uk/people/amurawski/mgs2011-tlc.pdf
• http://www.andres-loeh.de/LambdaPi/LambdaPi.pdf

## More credits and references:

• @bitemyapp, @argumatronic and @FunctionalWorks
• @txustice - http://blog.txus.io/
• http://www.amazon.com/Functional-Programming-Practice-Bruce-Maclennan/dp/0201137445/ref=sr_1_9?ie=UTF8&qid=1445967378&sr=8-9&keywords=functional+programming
• http://media.eurucamp.org/ (@eurucamp)

## Thank you! :)

Questions?

hannelita@gmail.com

@hannelita

#### Type Theory 101 - OSBridge

By Hanneli Tavante (hannelita)

• 1,029