The Hidden Mathematics of the Modern Developer

WARNING

Code examples will be in JavaScript

The Plan

  • Copy/paste from Wikipedia
  • Show hieroglyphs - call them "math"
  • Spout aphorisms like "math is all around us"
  • Pin code to refrigerator and reflect on its beauty
  • Make broad generalizations with dramatic tone
  • Run out of things to say

math · e · mat · ics

/maTH(ə)ˈmadiks/

noun

the abstract science of number, quantity, and space. Mathematics may be studied in its own right (pure mathematics), or as it is applied to other disciplines such as physics and engineering (applied mathematics)

"Object oriented programs are offered as alternatives to correct ones…"

Edsger Dijkstra, Pioneer of computer science

"Simplicity is a prerequisite for reliability."

Edsger Dijkstra, Pioneer of computer science

“[mathematics is a] paragon of reliability and truth...”

David Hilbert (1925), Super famous mathematician

“[mathematics] constitutes the most colossal metaphor imaginable, and must be judged, aesthetically as well as intellectually, in terms of the success of this metaphor”

Norbert Wiener, Originator of Cybernetics

Math Symbols

Conjunction
\( \wedge \)
"and"

Disjunction
\( \vee \)
"or"

Negation
\( \neg \)
"not"

Implication
\( \implies \)
"if...then"

Equivalence
\( \Longleftrightarrow \)
"iff"

Quantification
\( \forall \)
"for all"

Universal

Quantification
\( \exists \)
"there exists"

Existential

Turnstile
\( \vdash \)
"provable"

Union
\( \cup \)
"or"

Set Theory

Intersection
\( \cap \ \)
"and"

Set Theory

Element of
\( \in \)
"element of"

Set Theory

Natural #'s
\( \N \)
"0, 1, 2, 3, ..."

Number Theory

Integers
\( \Z \)
"...-1, 0, 1, ..."

Number Theory

Rational #'s
Q
"integer ratio"

Number Theory

“Java is the most distressing thing to happen to computing since MS-DOS.”

Alan Kay, Inventor of "OOP"

“[programming is] an activity by which a digital computer is made to do man’s will, by expressing this will suitably on punched tapes”

Remember when you memorized your multiplicative monoid of \( \N \) action table in 3rd grade?

#memories

\( 5 - 2 = 3 \)

Can you name each part of this equation?

minuend

subtrahend

difference

Math helps you:

  • name stuff
  • simplify stuff
  • understand stuff

“[Logic] is the only science that has made no progress at all since antiquity.”

Attributed to Kant (not true anymore)

Modus Ponens

const p = true;

const q = p && true;

q === true // true
\cfrac{P, P \to Q}{Q}

if this

then

that

Absoprtion Law

const p = true;

const q = false;

p && (p || q) === p; // true
P \wedge (P \vee Q) = P

\( (P\vee Q) \)

\( P \wedge (P\vee Q) \)

\( Q \)

\( P \)

\( true \)

\( true \)

\( false \)

\( true \)

\( true \)

\( true \)

\( true \)

\( true \)

\( true \)

\( true \)

\( false \)

\( true \)

\( false \)

\( false \)

\( false \)

\( false \)

\( false \)

De Morgan's Laws

\( \neg (P \vee Q)\Longleftrightarrow(\neg P)\wedge(\neg Q) \)

\( \neg (P \wedge Q)\Longleftrightarrow(\neg P)\vee(\neg Q) \)

const p = true;
const q = false;

!p && !q === !(p || q) // true

Double Complement Law

const p = true;
const q = false;

!!p === p; // true
!!q === q; // true

\( \neg (\neg P) \Longleftrightarrow P \)

Law of the Excluded Middle

const p = true;
const q = false;

p || q === true // true
q || p === true // true

\( \forall P \vdash (P \vee \neg P) \)

Automate with ESLint

// Bad
var someValue = foo ? foo : bar;

// Good
var someValue = foo || bar;
// .eslintrc.js
module.exports = {
    rules: {
        'no-unneeded-ternary': 'error'
    }
};

ESLint rule works because of Math!!!

(\neg foo \vee foo) \wedge (\neg(\neg foo) \vee bar)

Substitute \( P \to Q \) with \( \neg P \vee Q \):

(\neg foo \vee foo) \wedge (foo \vee bar)

Apply "double complement law":

true \wedge (foo \vee bar)

Simplify left side of conjunction using basic identity:

foo \vee bar

Remove left side of conjunction (\( true \) is identity for \( \wedge \)):

(foo \to foo) \wedge (\neg foo \to bar)

Write ternary using propositional logic:

Functions

\( f(x, y) = z \)

\( x, y, z \in A \)

\( f: A \times A \to A \)

name

codomain

domain

Injection ("one-to-one")

\( a' \)

\( b' \)

\( c' \)

\( a \)

\( b \)

\( f: X \to Y \)

\( X \)

\( Y \)

// X = [1, 2]
// Y = [1, 2, 3, 4]

const double = x => 2 * x;

double(1) === 2 // true

double(2) === 4 // true

Injective Function Example

\( 1 \)

\( 2 \)

\( X \)

\( Y \)

\( double: X \to Y \)

\( 1 \)

\( 2 \)

\( 3 \)

\( 4 \)

Injective Function Example

Surjection ("onto")

\( a' \)

\( b' \)

\( a \)

\( b \)

\( c \)

\( X \)

\( Y \)

\( f: X \to Y \)

// X = [1, 2, 3]
// Y = [true, false]

const isEven = x => x % 2 === 0;

isEven(1) === false // true

isEven(2) === true // true

isEven(3) === false // true

Surjective Function Example

\( true \)

\( false \)

\( 1 \)

\( 2 \)

\( 3 \)

\( X \)

\( Y \)

\( isEven: X \to Y \)

Surjective Function Example

Bijection

\( a' \)

\( b' \)

\( a \)

\( b \)

\( c \)

\( X \)

\( Y \)

\( f: X \to Y \)

\( c' \)

Bijection

injective and surjective

// X = [1, 2]
// Y = [2, 4]

const double = x => 2 * x;

double(1) === 2 // in Y

double(2) === 4 // in Y

Bijective Function Example

\( 2 \)

\( 4 \)

\( 1 \)

\( 2 \)

\( X \)

\( Y \)

\( double: X \to Y \)

Bijective Function Example

Groups

Binary Operation
\( \oplus: S \times S \to S\)

Group Theory

examples

addition, multiplication

Is subtraction a binary operation on \( \N \)?

Proof by Contradiction

\( 42 - 50 = -8 \)

\( 42, 50 \in \N, -8 \notin \N \implies  \)

Subtraction is NOT a binary operation on \( \N \)

Closure

Group Theory

example

\( a,b \in G \implies (a \oplus b) \in G\)

addition on \( \N \)

Identity
\( a \oplus i = i \oplus a = a\)

Group Theory

examples

\( 0 \) for addition

\( 1 \) for multiplication

Inverse
\( a \oplus a^{-1} = a^{-1} \oplus a = i\)

 

Group Theory

example

\( a \in \Z, +: \Z \times \Z \to \Z \)

\( 9001 + a^{-1} = 0 \implies a^{-1} = -9001 \)

Associativity
\( a, b, c \in G \)

\( (a \oplus b) \oplus c = a \oplus (b \oplus c)\)

Group Theory

example

\( (1 + 2) + 3 = 1 + (2 + 3) = 4 \)

\( 1,2,3,4 \in \N, +: \N \times \N \to \N \)

Group

A set of elements and a binary operation with the following properties:

  1. Closure

  2. Associativity

  3. Identity

  4. Inverse

\( (\Z, 0, +) \)

Group of Integers over Addition

elements

identity

operation

Group of Natural Numbers over Multiplication

\( (\N, 1, \times) \)

Groups have another definition...

monoids with inverse property

Monoid

Category with one element

example

\( (\N, 0, +) \)

THEORY

CATEGORY

What are bijections called in category theory?

Isomorphism

mapping between two structures of the same type that can be reversed by an inverse mapping.

// X = [1, 2]
// Y = [2, 4]

const double = x => 2 * x;
const inverse = y => y / 2;
const identity = x => inverse(double(x));

double(1) === 2 // true
double(2) === 4 // true

inverse(2) === 1 // true
inverse(4) === 2 // true

identity(1) === 1 // true
identity(2) === 2 // true

this is part of why everyone is talking about Category Theory and functional programming.

YES,

import {negate} from 'lodash';

const numbers = [1, 2, 3, 4, 5];

const lessThanTen = x => x < 10;

!(numbers.every(lessThanTen)) === !numbers.some(negate(lessThanTen)) // true
\neg (\forall x \in X, f(x)) \iff \exists x \in X, \neg f(x)
f: X \to Y
X = \{1, 2, 3, 4, 5\},
Y = \{true, false\},

The Opposite of "all" is "some"

const numbers = [1, 2, 3];

numbers.reduce((sum, num) => sum + num, 0); // 6
// (Number, 0, +)

numbers.reduce((sum, num) => sum + num, ''); // '123'
// (String, '', +)

numbers.reduce((sum, num) => [...sum, num], []); // [1, 2, 3]
// (Array, [], Array.prototype.concat)

Groups help you understand reduce

import Promise from 'bluebird';

const createTask = val => Promise
    .resolve(val)
    .delay(val * 100)
    .then(console.log);

const numbers = [20, 5, 3, 1, 4];

numbers.reduce((sum, num) => sum.then(() => createTask(num)), Promise.resolve());
(Promise, Promise.resolve(), Promise.prototype.then)

What is the Promise Identity?

Identity

and then categories, currying, functors, monads, Church numerals, Peano axioms, lambda calculus, Hindley-Milner type inference, free variables, function composition, point-free, graph theory, cyclomatic complexity...

The Plan

  • Copy/paste from Wikipedia
  • Show hieroglyphs - call them "math"
  • Spout aphorisms like "math is all around us"
  • Pin code to refrigerator and reflect on its beauty
  • Make broad generalizations with dramatic tone
  • Run out of things to say time

Type Inference

with

Hindley-Milner

Languages that do type inference

  • ReasonML (OCaml)
  • F#
  • Haskell
  • Elm (read this)
  • Rust

References

The Hidden Mathematics of the Modern Developer

By Jason Wohlgemuth

The Hidden Mathematics of the Modern Developer

Whether or not you are explicitly aware, you probably use math on a daily basis. In web development, there are are some mathematical principles more common than the rest. In this talk, we will elucidate the hidden mathematics supporting a web developer's daily life and with knowledge comes power. This talk will help beginners add tools to their toolbox and serve as an approachable refresher for denizens of the mathematical deeps. Come one, come all...together we shall see how computers are "...made to do man's will..." using the "...most colossal metaphor imaginable...". This talk’s goal is to help you simplify, name, understand and appreciate all the parts of whatever language you choose to use - although this talk chooses EcmaScript (JS)

  • 78