# Type Theory 101

#oscon

@hannelita

## Hi!

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

# Why Type Theory??

# DISCLAIMER

# Introductory content

# GIFs and theory

# DISCLAIMER

# Discussion + Theory

# Few practical examples

# DISCLAIMER

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

# Logic

# How do we guarantee our program meets the spec and are not absurd statements?

# Most of the time is spent with "I think..."

# Let me try:

# Only formal proofs could guarantee something more precise

# When we write code

### Programming => Logic

### Types => Propositions

## A programming language can be seen as a logical system

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

## WHERE IS THE MATHEMATICS ON THAT?

# What is ~~love~~ ?

# Logic

# Science of the argument

# 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

### Functional Programming

WAT

## It's simple:

- Lambda calculus
- Lisp
- Haskell
- Scala
- Clojure
- Papers
- Academia
- Category Theory
- λ
- λ
- λ

## Functional Programming has some resources to write code:

- High Order Functions
- Monoids
- Monads
- functors

# Example 1 - with OSCON pants

amount = 20

f(change)

How can I get OSCON pants?

Attendee

# Or:

f(change)

Speaker

# Or:

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

- Person A, 2 badges
- Person B, 4 badges
- Person C, 1 badge

## Badges sum: 7

- Person A, 2 badges
- Person C, 1 badge
- Person B, 4 badges

## Badges sum: 7

- Person B, 4 badges
- Person C, 1 badge
- Person A, 2 badges

- Person C, 1 badge
- Person A, 2 badges
- Person B, 4 badges

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

# sumEverythong AndGiveTheResult([2,4,1])

## fold|reduce|other_names

# We can have someone with 0 badges.

## Team

- Person A, 2 badge
- Person C, 1 badge
- Person B, 4 badge
- Person D, 0 Badge

## Badge sum: 7

## Person does not interfere on the sum

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

## reduce

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

## identity element

## collection

# MONOIDS

# Attendees

## and badges

{A: 15},

{B: 5},

{C: 10},

{D: 5}

{Attendee, badges}

[

]

↓

{A: 15},

{B: 5},

{C: 10},

{D: 5}

Badges

donate_badge[{A: 15}]

[

]

[{A: 14}],

{B: 5},

{C: 10},

{D: 5}

[

]

[]

[ ]

[ ]

↓

↓

↓

{A: 15},

{B: 5},

{C: 10},

{D: 5}

Badges

[

]

{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

## MONADS

# Now we have programming structures to manipulate (monoids, functions)

# λ-calculus

# How do we express functional programming with Mathematics?

## The same way we apply algebra to numbers, we can apply a type of calculation in our Functional Programming structures

# 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

# Now we have abstract structures coming from our code that we applied some calculations.

## Mathematical Constructions

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

# Constructive mathematics is typed - the logical statements need a type to be distinct

# FINALLY! Type Theory

Types

Functions

Propositions

Proofs

Isomorfism

# Fast Cookbook

# 1. Choose a language that allows pure Functional Programming (ex. Haskell, Idris, OCaml)

# 2. By using FP elements, you will be able to map your code into logical statements

Abstraction

# 3. Apply Lamba Calculus rules

# 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)
- @_leticia ('Meownads')
- http://stackoverflow.com/questions/2704652/monad-in-plain-english-for-the-oop-programmer-with-no-fp-background

## Thank you! :)

Questions?

hannelita@gmail.com

@hannelita

#### Type Theory 101 OSCON

By Hanneli Tavante (hannelita)