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

# 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

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

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

## fold|reduce|other_names

# 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

## Floor sum: 7

## Building D does not interfere on the sum

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

## reduce

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

## identity element

## collection

# MONOIDS

# Attendees

## and badges

## at a Conference

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

By Hanneli Tavante (hannelita)