Q.E.D. Code Michael L Perry

 Technology

Explore the intersection between software and mathematics. Michael L Perry takes you on a journey through the history of computing, information theory, cryptography, distributed systems, and everywhere else that math has influenced technology. Not only will you dive into math papers, but you'll also learn valuable programming techniques that you can apply in any language.
Code Erat Demonstrandum

QED 19: Dependency Tracking
The number of degrees of freedom in a system is equal to its number of unknowns minus its number of equations. That is also equal to the number of independent variables in the system. Dependent variables do not contribute degrees of freedom. However, by taking advantage of determinism, we can compute at run time the set of independent variables upon which a dependent variable depends. This gives libraries like Update Controls, Assisticant, and Knockout the ability to figure out when to update the view with extreme efficiency.
Both C# and Java have enumerables. These are interfaces understood by the looping constructs of the language itself. Unfortunately, it is unsafe to modify a collection while using an iterator. But immutable data types hold the key, and should be the preferred way of managing collections.
Prime numbers are the building blocks of all natural numbers. Godel used them to prove his Incompleteness Theorem. There are many mindblowing facts that we know about prime numbers, such as the general distribution of them as we continue counting. However, there are equally mindblowing things that we don't know, such as how clumped together they are far out along the number line. 
Q.E.D. 18: Paxos
Leslie Lamport wrote a paper describing the Paxos algorithm in plain English. This is an algorithm that guarantees that a distributed system reaches consensus on the value of an attribute. Lamport claims that the algorithm can be derived from the problem statement, which would imply that it is the only algorithm that can achieve these results. Indeed, many of the distributed systems that we use today are based at least in part on Paxos.
Linq not only introduced the ability to query a database directly from C#. It also introduced a set of language features that were useful in their own right. Lambda expressions did not exist in C# prior to 3.0, but now they are preferred to their predecessor, anonymous delegates. Extension methods were added to the language specifically to make Linq work, but nevertheless provide an entirely new form of expressiveness. Linq works because its parts were designed to compose. This is not quite so true of language features that came later, like async and primary constructors.
Fermat was quite fond of writing little theorems and not providing their proofs. One such instance, known as "Fermat's Little Theorem", turns out to be fairly easy to prove, but provides the basis for much of cryptography. It states that a base raised to one less than a prime in the prime's modulus will always be equal to 1. This is useful in generating a good trap door function, where we can easily compute the discrete exponent of a number, but not the discrete logarithm. 
QED 17: Pythagoras
The Pythagoreans were a cult of Greek mathematicians that believed that all things were composed of large enough integers. Their leader, Pythagoras, is best known for the proof that the square of the hypotenuse of a right triangle is equal to the sum of the squares of the two sides. Unfortunately, this theorem leads to the conclusion that some numbers cannot be expressed as the ratio of large enough integers, and so proof destroyed their beliefs.
Monads are an extremely useful category of type. Whereas Applicatives let us build pipelines of single values, Monads support forking within that pipeline. They provide a "bind" method that joins a function returning another Monad to the end of the pipeline, and then flattens out the result. We can use this to build queries, to eliminate null reference exceptions, and even to execute asynchronous code without nested callbacks.
Event Sourcing, popularized by Greg Young, is the practice of storing the events that have occurred in a system, instead of the current state of the system. The benefits are auditability, replayability, and reconciliation. Mathematically speaking, an event sourced system looks like a left fold operation. Starting with the initial state of the system, we apply a function that computes the next state of the system after an event has occurred. This left fold operation computes the current state from the sequence of events. 
QED 16: Elements
Euclid's Elements takes a disciplined, formal approach to proving assertions based only on simple axiomatic statements. While most of these axioms are elegant, one of them is more complex and wordy. It seems as if it should be provable from the others. Several mathematicians have tried, but eventually they found a surprising result. Beware when proving your own assertions that you don't make the mistake of assuming something that seems obvious.
Category theory isn't as complex as you might be lead to believe. A category is nothing more than a contract that sets rules on how a type is to behave. Functors, despite having a weird name, are just types that can map functions into a different space. The List type is an example of a functor, because it can transform a function on its elements into a function on lists. And Applicatives are just wrapper types. They are especially useful for building pipelines.
CQRS, Command Query Responsibility Segregation, is a pattern that applies the Single Responsibility Principle to components such that they either change the system (commands), or observe it (queries). Segregation alone gives us the ability to prove some very useful things about a system: safety, determinism, and consistency for example. But it does not prescribe asynchrony or eventual consistency. Those are architectural decisions left to you. 
QED 15: The Y Combinator
The Lambda Calculus uses simple replacement to compute expressions. However, it does not define a way to replace a parameter of a function with the function itself. That would seem to make it impossible to write recursive functions. However, with a clever bit of selfapplication, we can define a function that makes any other function recursive. This is the YCombinator.
Claude Shannon told us that it was important to keep equivocation high in order to make a cypher difficult to crack. In the second half of his paper, he tells us how to do that. We need to diffuse the message, and we need to confuse the key. Doing these two things will destroy the statistical patterns of the message so that the only possible attack is brute force.
The Two Generals problem has no solution. But does that mean that we cannot guarantee that a distributed system will reach consensus? Not at all. By applying two simplifying assumptions, we reduce the problem to one that can be solved with a very simple protocol. This durable messaging protocol guarantees that a message is received once and only once. 
QED 14: Equivocation
Claude Shannon followed up one incredibly important paper with a second of even greater significance. In Communication Theory of Secrecy Systems, he analyzes cryptosystems based on the probabilities of certain plaintext messages given an intercepted cyphertext. Understanding this form of analysis will help us to design more effective systems.
The Lambda Calculus computes using nothing but symbol replacement. If we are going to run programs like a computer, we need to express conditional branches. We can represent the value "true" as a function λa.λb.a. In other words, the function that returns the first of two arguments. Similarly, the value "false" is represented by the function λa.λb.b. To create a conditional "ifelse" statement, capture two branches and then apply the third argument to select between them: λa.λb.λc.c a b.
Suppose that you needed to reach an agreement among several people by passing messages. Now suppose that some of those people could not be trusted. Under what conditions could you find a protocol to reach an agreement? Leslie Lamport, Robert Shostak, and Marshall Pease studied the Byzantine Generals problem to determine how to design algorithms for distributed systems.