# Type Theory 101

#oscon

@hannelita

## Hi!

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

# Mathematics + Programming + Too much information

## Agenda

• Logic
• Functional Programming
• Intro to Lambda Calculus and Mathematical Formalism
• 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

# 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 OSCON pants

amount = 20

f(change)

How can I get OSCON pants?

Attendee

f(change)

Speaker

f(change)

Speaker

+

amount = 20

amount = 20

Attendee

f(change)

# High Order Function

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

# Example 2 - Imagine a team of people collecting O'Reilly's badges here at OSCON

## Team

`Credits: @bitemyapp and @argumatronic - http://haskellbook.com/`

# Person with zero badge works as an identity element to the level.

## reduce

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

# Attendees

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