Pokémons, FP e Type Theory
Hi!
- Computer Engineer
- Programming
- Electronics
- Math <3 <3
- Physics
- Lego
- Meetups
- Animals
- Coffee
- GIFs

Why Type Theory??



DISCLAIMER
Introductory content
Pokémon, GIFs and theory
DISCLAIMER
Discussion + Theory
Few practical examples
DISCLAIMER
Mathematics + Programming
Agenda
- Logic
- Functional Programming with Pokémon
- Intro to Lambda Calculus and Mathematical Formalism
- Type Theory

Logic
How do we guarantee our program meets the spec?
Most of the time is spent with "I think..."
Let me try:

Only formal proofs could guarantee something more precise

When we write code
Formal proofs => Logic?
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?
Can you think about strong typed languages and the opposite?
p:P
- This is not an emoji
- "The value p has the type P"
- And so what? Then we analyse the 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!
(WTF ARE YOU TALKING ABOUT????)

What is love ?
Logic

Science of the argument
What is correct?
- Write arguments that can be checked mechanically
- Study properties
- Elaborate semantic meanings
Functional Programming

WAT


It's simple:
- Lambda calculus
- Lisp
- Haskell
- Scala
- Clojure
- Papers
- Academia
- Category Theory
- λ
- λ
- λ


Pokémon
We give instructions to Pokémon in a battle
Programming
We also give instructions to code

i = i+1;
Pokémon
We must know how some things work:
Programming
We must know how to manage information in our software
- Data types
- Evolution
- How to group them
- Important attributes
- Business rules
- Pokémon types
- Evolution
- Team strategy
- EV/IV
- Moves
- Items
- Trades




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)
In some competitions, there is a maximum value for the sum of the levels of the Pokémon on the team
Team
- Charizard, lvl 100
- Lapras, lvl 100
- Dragonite, lvl 100
- Rhydon, lvl 100
- Pidgeotto, lvl 100
- Gengar, lvl 100
Level sum: 600
- Gengar, lvl 100
- Charizard, lvl 100
- Lapras, lvl 100
- Rhydon, lvl 100
- Pidgeotto, lvl 100
- Dragonite, lvl 100
Level sum: 600
- Lapras, lvl 100
- Gengar, lvl 100
- Charizard, lvl 100
- Rhydon, lvl 100
- Pidgeotto, lvl 100
- Dragonite, lvl 100
- Pidgeotto, lvl 100
- Lapras, lvl 100
- Gengar, lvl 100
- Dragonite, lvl 100
- Charizard, lvl 100
- Rhydon, lvl 100
Credits: @bitemyapp and @argumatronic - http://haskellbook.com/

sumEverythong AndGiveTheResult([100,100, 100,100,100,100])
fold|reduce|other_names
(reduce + [100 100 100 100 100 100]) ;;=> 600
What else can you have on your party rather than battle Pokémon?
Eggs

Team
- Charizard, lvl 100
- Lapras, lvl 100
- Dragonite, lvl 100
- Egg
- Pidgeotto, lvl 100
- Gengar, lvl 100
Level sum: 500
Egg does not interfere on the sum
Egg level = 0
Does not change the value
Egg works as an identity element to the level.
- Gengar, lvl 100
- Charizard, lvl 100
- Lapras, lvl 100
- Egg
- Pidgeotto, lvl 100
- Dragonite, lvl 100
Level sum: 500
- Lapras, lvl 100
- Gengar, lvl 100
- Charizard, lvl 100
- Egg
- Pidgeotto, lvl 100
- Dragonite, lvl 100
- Pidgeotto, lvl 100
- Lapras, lvl 100
- Gengar, lvl 100
- Dragonite, lvl 100
- Charizard, lvl 100
- Egg
reduce
(reduce + 0[100 100 100 100 100]) ;;=> 500
identity element
collection
MONOIDS

Game items

Game items





[
]
Collection
Game Items




[
]
Collection
(map upgrade )
;;=>
[
]




Game Items

[0]
[1]
Optional
Game Items

Optional
(map change )
;;=>
[
]
[
]

Game Items

[
]
In some part of the game you will have the badges
Future
Game Items

[
]
(map other_cont )
;;=>
[
]

Future
[ Pokéballs]
[ Bike ]
[ Badges ]
=>
=>
=>
[ Pokéballs]
[ Bike ]
[ Badges ]
[ Pokéballs]
[ Bike ]
[ Badges ]
=>
=>
=>
[ Pokéballs]
[ Bike ]
[ Badges ]
FUNCTORS

Move set
and moves

{Surf: 15},
{Fissure: 5},
{Solarbeam: 10},
{Eruption: 5}
Moves
[
]
↓
{Surf: 15},
{Fissure: 5},
{Solarbeam: 10},
{Eruption: 5}
Moves
decrease_pp_used_move[{Surf: 15}]
[
]
[{Surf: 14}],
{Fissure: 5},
{Solarbeam: 10},
{Eruption: 5}
[
]
[]
[ ]
[ ]

↓
↓
↓

{Surf: 15},
{Fissure: 5},
{Solarbeam: 10},
{Eruption: 5}
Moves
[
]
{Surf: 14},
{Fissure: 5},
{Solarbeam: 10},
{Eruption: 5}
(flatten decrease_pp [{Surf:15}])
;;=> [{Surf: 14}, {Fissure: 5}, {Solarbeam: 10}, {Eruption: 5}]
Collection
↓
↓

Item
[Scope Lens]
boost_move
{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
↓
↓
↓
↓
{Fissure:5},
[{Surf:15:boosted}],
{Solarbeam: 10},
{Eruption: 5}
[
]
↓
Optional

Item
[Scope Lens]
{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
↓
↓
↓
↓
↓
Optional
(flatten boost_move [{Surf:15}]
;;=> [{Fissure:5}, {Surf: 15:boosted}, {Solarbeam: 10}, {Eruption: 5}]

{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
↓
↓
↓
↓
Future
{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
Moves
forget
{Fissure:5},
[],
{Solarbeam: 10},
{Eruption: 5}
[
]

{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
↓
↓
↓
↓
Future
(flatten forget [{Surf:15}]
;;=> [{Fissure:5}, {Solarbeam: 10}, {Eruption: 5]}
{Fissure:5},
{Surf:15},
{Solarbeam: 10},
{Eruption: 5}
[
]
Moves
No matter which move set you have (if it exists, if you have items, if you haven't learned the move yet):
- Get the element
- Apply a function if convenient
- flatten
- Allow call chain
MONADS

"Meownads!" (credits with @_leticia)
λ-calculus
How do we express functional programming with Mathematics?
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
- Untyped or Typed
- http://www.cs.le.ac.uk/people/amurawski/mgs2011-tlc.pdf
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)."
It asserts the existence of an object without giving any indication of what the object is
Constructive mathematics is typed
FINALLY! Type Theory

Types
Functions
Propositions
Proofs

Isomorfismo

Topics

How can we turn this into Mathematics?

Simplifying

We can apply all this formalism for
- Naming
- Generalisations
- Derivability
An then we can analyse programming statements with Mathematics.
Credits and references:
- @brunoskonrad and Codeminer 42
- GIRARD, Jean-Yves. Proofs and Types.
- @lafp, @romulostotel, @pedrofelipee: GIFs
- @klauss_weyl, for showing me the importance of logic and Type Theory
- Henrique
- 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

pokemons_fp_type_theory_en
By Hanneli Tavante (hannelita)