Toward a categorical foundation of functional reactive programming David I. Spivak

advertisement
Toward a categorical foundation of functional reactive
programming
David I. Spivak
dspivak@math.mit.edu
Mathematics Department
Massachusetts Institute of Technology
Presented on 2014/01/23
at Carnegie Mellon University
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
1 / 60
Introduction
My goal: a visual, formal language for processes
My goal: a visual, formal language for processes
I want to be able to draw pictures like this:
Y
X2
X1
X3
such that, if one fills in each box Xi with a machine, it results in a new
machine for Y .
And I want it all to work as expected.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
2 / 60
Introduction
My goal: a visual, formal language for processes
What does all this mean?
But what is a picture like this?
Y
X2
X1
X3
And what kind of machines have this fill-me-in property?
And what expectations should we have about all this?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
3 / 60
Introduction
My goal: a visual, formal language for processes
Plan of this talk
I will show that wiring diagrams (WDs)
Y
X
form a symmetric monoidal category (or SMC), denoted W.
I will show that there is an algebra P : W → Set of propagators.
I will explain SMCs and their algebras as we go along.
Time permitting, I’ll talk about adding special symbols to the
language.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
4 / 60
Introduction
Relation to Functional reactive programming
Relation to Functional reactive programming
I began this work to understand the semantics of wiring diagrams.
Much of this talk reflects joint work with Dylan Rupel,
as well as some preliminary work with Nat Stapleton.
Disclaimers:
I’m new to FRP, Incremental Computation, and Haskell arrows.
I see them only through the lens of the above category theory, not PL.
I hired a CS grad student to implement these ideas.
The student is David Darais, working under Greg Morrisett.
Darais told me about the relation to FRP, IC, and Haskell arrows.
He had this to offer the audience of this talk:
Haskell arrows are less understood foundationally than our
favorite object of study: lambda calculus. Wiring diagrams are
perhaps the foundation for arrows/FRP that we have all been
waiting for.
You can see for yourself what you think!
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
5 / 60
Introduction
A bit about the notation A → B
A bit about the notation A → B
What does the notation A → B mean in this talk?
Type theorists write this to denote what I’d call Hom(A , B ) or B A .
When I write A → B, I’m indicating an inhabitant of Hom(A , B ).
I might denote this inhabitant f : A → B, like a type theorist,
f
I might denote it like this: A →
− B,
or if the inhabitant is clear from context I can write it A → B.
What does A → B → C mean in this talk?
Type theorists write this to denote what I’d call Hom(A , Hom(B , C )).
However, when I write A → B → C, I mean that:
I’ve produced an inhabitant of Hom(A , B ),
I’ve produced an inhabitant of Hom(B , C ), and
I’m showing you their composite as an inhabitant of Hom(A , C ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
6 / 60
Introduction
First example: a running total
First example: a running total
Consider the machine
+
which takes two integers and reports their sum.
Installing it into the following wiring diagram
+
constructs a new machine for the outer box.
The constructed machine reports a running total of its inputs.
It carries the previous sum on the internal wire as state.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
7 / 60
Introduction
Box-manipulation language vs. box-content language
Box-manipulation language vs. box-content language
We want to distinguish between the architecture and the contents.
Analogy 1: In databases, we have a schema and an instance.
Analogy 2: In a dependent type Σx :X Q (x ), think of X as architecture
and Q : X → Type as the contents.
Today’s wording: syntax and semantics.
A monoidal category W will define the syntax of box-manipulation.
A monoidal functor P : W → Set will model box-contents.
Part of the beauty: there are many monoidal functors W → Set.
In this talk we’ll focus on P, discrete state propagators.
One can ask whether continuous propagators also model this W
syntax.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
8 / 60
The monoidal category W of wiring diagrams
The picture of W
ψ
φ1
φ2
ψ ◦ (φ1 , φ2 )
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
9 / 60
The monoidal category W of wiring diagrams
Wires, boxes, wiring diagrams
Wires and boxes
Wires carry a defined set of values.
A wire w ∈ Set∗ is a pointed set w = (T , t0 ), where t0 ∈ T .
A finite set of wires is a pair (I, τ), where I = {i1 , . . . , in } is a finite set,
and τ : I → Set∗ is a function.
We write TFS (“typed finite sets”) to denote the collection of (I, τ)’s.
Boxes have input wires and output wires.
A box X consists of a pair X := (inp(X ), out(X ))
inp(X ) ∈ TFS is called the set of input wires to X , and
out(X ) ∈ TFS is called the set of output wires to X .
Another term for box might be interface.
Example: Box X = {a : Z, b : N}, {u : T1 , v : Bool , w : T2 }
Z
:a
X
u:
T1
v : Bool
N
David I. Spivak (MIT)
:b
w:
A categorical foundation of FRP
T2
Presented on 2014/01/23
10 / 60
The monoidal category W of wiring diagrams
Wires, boxes, wiring diagrams
Tensor product of boxes
Given boxes X = (inp(X ), out(X )) and Y = (inp(Y ), out(Y )),
x1
x2
X
x3
y1
x4
Y
y2
y3
we can stack them on top of each other and call that a box
x1
x2
y1
X ⊕Y
x3
x4
y2
y3
Define the tensor product of X and Y , denoted X ⊕ Y , by
X ⊕ Y := (inp(X ) + inp(Y ), out(X ) + out(Y )).
We define the inert box to be := (∅, ∅). It is a ⊕-unit:
X ⊕ X ⊕ X.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
11 / 60
The monoidal category W of wiring diagrams
Operad vs. monoidal
Wiring diagrams, operad flavor: Many boxes inside
Operads are many-inside, one-outside.
More precisely, morphisms in an operad have many domain objects.
For example φ : (X1 , X2 , . . . , Xn ) −→ Y .
These make for nicer, more intuitive pictures.
If desired, one can restrict to the sub-operad of loop-free WDs.
Loop-free being a smaller syntax, it is more easily modeled.
For example, spreadsheets (incremental computation?).
φ : (X1 , X2 ) −→ Y
Y
X2
X1
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
12 / 60
The monoidal category W of wiring diagrams
Operad vs. monoidal
Wiring diagrams, monoidal flavor: One box inside
Monoidal categories are more like regular old categories.
Morphisms in a monoidal category have one domain object.
But there’s a tensor operation that serves an operad-like purpose.
We can have φ : X1 ⊕ X2 ⊕ · · · ⊕ Xn → Y .
Advantages to using monoidal categories:
The mathematics works out cleaner for wiring diagrams.
More people know about monoidal categories.
Disadvantage: the pictures can be ugly and unintuitive.
Here’s the monoidal version of the picture from the previous slide.
φ : X1 ⊕ X2 −→ Y
Y
X1 ⊕ X2
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
13 / 60
The monoidal category W of wiring diagrams
Operad vs. monoidal
Today’s compromise: monoidal maths, operadic pictures
In our case (with loops allowed), these two notions are equivalent.
So we’ll go with the pretty option in both cases:
Pretty math: symmetric monoidal categories (SMCs)
Pretty pictures: operads.
We’ll write φ : X1 ⊕ X2 → Y and allow ourselves to draw the diagram
below.
φ : X1 ⊕ X2 −→ Y
Y
X2
X1
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
14 / 60
The monoidal category W of wiring diagrams
Morphisms in W
Where are we now?
We’re on our way to defining a symmetric monoidal category W.
I’ll tell you the definition of SMC’s soon.
For now just bear with me.
An object X ∈ Ob(W) is called a box.
Recall a box is a pair X = (inp(X ), out(X )) of typed finite sets.
The coincidence of the term “object” with OOP is not bad.
We are trying to formalize encapsulation.
Boxes can be tensored together by stacking them.
X ⊕ Y = inp(X ) + inp(Y ), out(X ) + out(Y )
Morphisms in W are wiring diagrams.
I showed pictures of the monoidal version and the operadic version.
Hopefully these pictures make intuitive sense.
But I haven’t told you what WDs are mathematically.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
15 / 60
The monoidal category W of wiring diagrams
Morphisms in W
Thinking about wiring diagrams
Let X = (inp(X ), out(X )) and Y = (inp(Y ), out(Y )) be boxes.
What is a wiring diagram?
φ: X → Y
Y
X
Think of φ as an economy, in which every demand needs a supply.
The inputs of X are supplied either by inputs of Y or by internal wires.
Both the internal wires and the outputs of Y are sourced by X -outputs.
A wiring diagram expresses these relationships in terms of functions.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
16 / 60
The monoidal category W of wiring diagrams
Morphisms in W
Mathematical formulation of wiring diagrams
Definition
Let X = (inp(X ), out(X )) and Y = (inp(Y ), out(Y )) be boxes. A wiring
diagram φ : X → Y consists of:
a typed finite set int(φ), called the set of internal wires,
a typed function φin : inp(X ) −→ int(φ) + inp(Y ), and
a typed function φout : int(φ) + out(Y ) −→ out(X ).
φ: X → Y
Y
X
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
17 / 60
The monoidal category W of wiring diagrams
Morphisms in W
Safe wiring diagrams, Wsafe ⊆ W
Let X = (inp(X ), out(X )) and Y = (inp(Y ), out(Y )) be boxes.
To make the math fit the pictures, we’ll restrict our attention to certain
wiring diagrams.
Say that a wiring diagram φ = (int(φ), φin , φout ) is safe if
the internal wires are inputs of X , i.e. int(φ) ⊆ inp(X ),
the restriction of φin to int(φ), i.e. the composite
φin
int(φ) ⊆ inp(X ) −−→ int(φ) + inp(Y ),
is the coproduct inclusion, and
the functions φin and φout are surjective.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
18 / 60
The monoidal category W of wiring diagrams
Morphisms in W
Unsafe wiring diagrams
Idea: a wiring diagram is unsafe if it has exposed wires.
This occurs when φin or φout is not surjective.
Recall φin : inp(X ) → int(φ) + inp(Y ), and
φout : int(φ) + out(Y ) → out(X ).
What is the problem with exposed wires?
Exposed wires are unsafe in the context of electrical wiring.
They’re also unsafe for ones intuition about WDs and propagators.
I’ll explain a bit more later.
Unsafe: exposed wires!
Y
X
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
19 / 60
Example of a (safe) wiring diagram (int(φ), φin , φout )
The monoidal category W of wiring diagrams
Example of a (safe) wiring diagram (int(φ), φin , φout )
Let X be the box with inp(X ) = {a , b } and out(X ) = {c , d }.
Let Y be the box with inp(Y ) = {u} and out(Y ) = {v , w }.
Here’s a WD with internal wires int(φ) = {a 0 }:
φ: X → Y
Y
v
a0
u
a
X
b
c
w
d
Here’s the (surjective) function φin : inp(X ) −→ inp(Y ) + int(φ):
b 7→ u
and
a 7→ a 0 .
Here’s the (surjective) function φout : int(φ) + out(Y ) −→ out(X ):
a 0 7→ c ,
David I. Spivak (MIT)
v 7→ c ,
and
A categorical foundation of FRP
w 7→ d .
Presented on 2014/01/23
20 / 60
The monoidal category W of wiring diagrams
Tensor product of wiring diagrams
Tensor product of wiring diagrams
Suppose given two wiring diagrams, φ1 : X1 → Y1 and φ2 : X2 → Y2 .
out
out
in
Say φ1 = (int(φ1 ), φin
1 , φ1 ) and φ2 = (int(φ2 ), φ2 , φ2 )
To tensor morphisms, we stack them.
φ1
φ1 ⊕ φ2
φ2
As with boxes, tensor is achieved by summation across the board:
int(φ1 ⊕ φ2 ) = int(φ1 ) + int(φ2 ),
in
(φ1 ⊕ φ2 )in = φin
1 + φ2 ,
out
(φ1 ⊕ φ2 )out = φout
1 + φ2 ,
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
21 / 60
The monoidal category W of wiring diagrams
Composing wiring diagrams
Composing wiring diagrams
We want to be able to plug wiring diagrams into wiring diagrams.
ψ
ψ ◦ (φ1 ⊕ φ2 )
φ1
φ2
Quiz: what are the internal wires of ψ ◦ (φ1 ⊕ φ2 )?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
22 / 60
The monoidal category W of wiring diagrams
Composing wiring diagrams
φ
ψ
Composing wiring diagrams, X →
− Y−
→Z
Recall that each wiring diagram, say φ, consists of
a typed finite set of internal wires int(φ),
a typed function φin : inp(X ) → int(φ) + inp(Y ), and
a typed function φout : int(φ) + out(Y ) → out(X ).
The internal wires of ψ ◦ φ are int(ψ ◦ φ) := int(φ) + int(ψ).
The function (ψ ◦ φ)in : inp(X ) → int(ψ ◦ φ) + inp(Z ) is given by
φin
int(φ)+ψin
inp(X ) −−→ int(φ) + inp(Y ) −−−−−−−−−→ int(φ) + int(ψ) + inp(Z ).
The function (ψ ◦ φ)out : int(ψ ◦ φ) + out(Z ) → out(X ) is given by
int(φ)+ψout
φout
int(φ) + int(ψ) + out(Z ) −−−−−−−−−−→ int(φ) + out(Y ) −−−→ out(X ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
23 / 60
The monoidal category W of wiring diagrams
W is a symmetric monoidal category
W is a symmetric monoidal category
Let’s recap what we know about W.
First of all, W is a category:
We defined an object of W to be a box (a pair of typed finite sets).
We defined a morphism φ : X → Y in W to be a wiring diagram,
(int(φ), φin , φout ).
On the last slide we showed the composition formula for ψ ◦ φ.
The identity (having int(idX ) = ∅) is straightforward.
Proving the associativity law is straightforward too.
So we indeed have a category.
Add a tensor product to that, and we have an SMC.
The tensor product needs to satisfy some laws:
For example, we need X ⊕ Y Y ⊕ X .
Another example: ⊕ X X X ⊕ .
But these are all straightforward, because we’re just working with finite
sets and their sums.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
24 / 60
The monoidal category W of wiring diagrams
What is a symmetric monoidal category
What is a symmetric monoidal category
A symmetric monoidal category consists of
a category M,
a functor ⊗ : M × M → M, called the tensor,
an object I ∈ Ob(M) called the unit,
as well as various coherence isomorphisms and commutative
diagrams that ensure that everything works as expected, e.g.
X ⊗ I X IX ,
(X ⊗ Y ) ⊗ Z X ⊗ (Y ⊗ Z ), etc.
Your favorite: Type with Cartesian ×, and unit type 1.
Another: Set with disjoint union +, and unit set ∅.
Another: VectR with tensor product ⊗, and unit vector space R.
Another: W with stacking tensor ⊕, and inert box .
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
25 / 60
The monoidal category W of wiring diagrams
What is a symmetric monoidal category
Quick aside: how is ⊗ different than ×?
Some people want to know how ⊗ is different than Cartesian product.
Note that (Set, ×, 1) is an SMC, so we must be saying SMCs are
more general, i.e. that × is more constrained than arbitrary ⊗.
The additional constraint on × is that you can project,
A ←− A × B −→ B .
Note that (Set, +, 0) is an SMC, but there is no canonical map
A + B → B.
Objection: “But in this case it’s flipped. Are monoidal categories just...?”
Consider the SMC category Wsafe of boxes and wiring diagrams.
We neither have maps X ⊕ Y → X nor X → X ⊕ Y in general.
Example X = and Y = ({y : N}, ∅).
None possible
David I. Spivak (MIT)
None possible
A categorical foundation of FRP
Presented on 2014/01/23
26 / 60
The W-algebra of propagators
The basic idea behind W-algebras
So... what to plug into these boxes?
We have this syntax of boxes; what are we going to do with it?
“Let’s put stuff into them!”
“What can we put into them?”
“Whatever we want, as long as we understand stacking and wiring.”
A W-algebra is a lax monoidal functor
F : W → Set.
To choose a W-algebra F is to choose semantics for the box syntax.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
27 / 60
The W-algebra of propagators
The basic idea behind W-algebras
What is a lax monoidal functor F : W → Set?
Suppose we want to choose semantics F for this box syntax.
We get to choose what we allow ourselves to put into the boxes.
For a box X ∈ Ob(W) we get to choose a set F (X ).
Once we’ve done so, we’ll call f ∈ F (X ) an F-fill for box X .
We get to say how to stack F-fills.
Given boxes X , Y and F-fills f ∈ F (X ) and g ∈ F (Y ),
we need to give an F-fill for their tensor, σ(f , g ) ∈ F (X ⊕ Y ).
We get to say how a wiring diagram φ : X → Y sends fills for X to fills
for Y .
Once we do that, we will have specified:
a function Ob(F ) : Ob(W) → Ob(Set),
a function σX ,Y : F (X ) × F (Y ) → F (X ⊕ Y ), and
a function HomF : HomW (X , Y ) → HomSet (F (X ), F (Y )).
For our choices to constitute a W-algebra, various laws must hold.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
28 / 60
The W-algebra of propagators
The basic idea behind W-algebras
Some stupid W-algebras
Let M = (M , ?, e ) be any commutative monoid.
For example the natural numbers, with addition, (N, +, 0),
or the integers, with multiplication, (Z, ∗, 1),
or the subsets of some set, with union, (P({0, 1, . . . , 9}), ∪, ∅).
Then there is an algebra F : W → Set that assigns
F (X ) := M,
σ := ? : M × M → M, and
HomF (φ) := idM .
For example, with M = (N, +, 0), we have
15
6
5
4
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
29 / 60
The W-algebra of propagators
The basic idea behind W-algebras
More interesting algebras W → Set
The previous algebras didn’t take advantage of the wiring structure.
We will focus on propagators, taking input-streams to output-streams.
Variations include:
asking the propagators to be continuous or differentiable.
continuous-time machines, etc.
In each case, just say what to put into boxes and how stacking and
wiring are to work.
Resulting fill?
fill2
fill1
fill3
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
30 / 60
The W-algebra of propagators
The basic idea behind W-algebras
A questionable algebra
One idea might be to put into each box the set of functions of the
specified type.
That is, suppose X is the box below.
Define F (X ) = Hom(Z × N, T 1 × Bool × T 2), the set of functions.
Z
N
X
Bool
But then how do wiring diagrams operate on functions?
Recall the running total.
It is made out of a pure function, but the result is not functional.
The same input in two successive moments returns different outputs.
1, 1, 1, 1
David I. Spivak (MIT)
+
A categorical foundation of FRP
1, 2, 3, 4
Presented on 2014/01/23
31 / 60
The W-algebra of propagators
State propagators
State propagators
Definition
Let A and B be sets. An (A , B )-propagator consists of
1. a set S, called the state-set,
2. a function f : S × A → S × B, called the propagation function.
An (A , B )-propagator is called initialized if we have chosen
3. an element s0 ∈ S, called the initial state.
We call a propagator (S , f ) simple if its state-set has one element, |S | = 1.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
32 / 60
The W-algebra of propagators
State propagators
Aside: Initialized propagators act on lists
Let (S , s0 , f ) be an initialized (A , B )-propagator, where s0 ∈ S.
For convenience, swap the propagation function’s outputs:
f : S × A −→ B × S .
For n ∈ N, we define fn : A n → B n × S, as follows:
define f0 = s0 , the initial state, and
define fn+1 : A n+1 −→ B n+1 × S to be the composite
f n ×A
B n ×f
An × A −
−−−→ B n × S × A −−−−→ B n × B × S
Project each fn : A n → B n × S and then sum the results to obtain
LP(S , s0 , f ) : List(A ) −→ List(B ),
called the list propagator associated to (S , s0 , f ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
33 / 60
The W-algebra of propagators
State propagators
Fill box X with the set of X -propagators
Quick aside on dependent products: notation and contravariance.
Given a typed finite set (I, τ) we denote the dependent product by
Y
(I, τ) :=
τ(i ).
i ∈I
This is contravariant: given a typed function p : (I, τ) → (I0 , τ0 ) we get
p : (I0 , τ0 ) → (I, τ).
Recall that a box X = (inp(X ), out(X )) is a pair of typed finite sets.
For example, if inp(X ) = {a : Z, b : Bool }, then inp(X ) = Z × Bool.
Define X := (inp(X ), out(X )).
So an X -propagator includes a state-set S and a propagation function
f : S × inp(X ) −→ S × out(X ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
34 / 60
The W-algebra of propagators
P : W → Set on objects
P : W → Set on objects
On boxes X ∈ Ob(W), define P(X ) to be the set of X -propagators,
For example, let X = ({a : N, b : N}, {u : N, v : Bool , w : N}),
N
:a
u:
N
v : Bool
N
:b
w:
N
Choosing an initialized X -propagator means:
choosing a state set S, an initial state s0 ∈ S, and a function,
f : S × (N × N) → S × (N × Bool × N).
For example, let’s choose S = N × N, with s0 = (0, 0), and
?
f ((s1 , s2 ), a , b ) = ((s1 + a , s2 + b ), (s1 , s1 = s2 , s2 )).
This returns running totals of a and b, as well as whether they’re equal.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
35 / 60
The W-algebra of propagators
Stacking propagators
Stacking propagators
Recall an X -propagator consists of a set S and a function
f : S × inp(X ) → S × out(X ).
For any two boxes X , Y ∈ Ob(W), we need a stacking function
σX ,Y : P(X ) × P(Y ) → P(X ⊕ Y ).
Given an X -propagator (S , f ) and a Y -propagator (T , g ), we need a
X ⊕ Y -propagator.
We use σX ,Y ((S , f ), (T , g )) := (S × T , f × g ).
(S , f )
σX , Y
(S × T , f × g )
(T , g )
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
36 / 60
The W-algebra of propagators
Wiring propagators
Wiring propagators
We’ve decided how P : W → Set works on boxes X ∈ Ob(W);
We’ve decided how P works with stacking.
Now we need to decide how P works with wiring diagrams.
Resulting fill?
fill2
fill1
fill3
Afterwards we need to check that the composition formula holds.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
37 / 60
The W-algebra of propagators
Wiring propagators
P(φ) : P(X ) −→ P(Y )
We begin with boxes X and Y , and a wiring diagram φ : X → Y .
Recall that each wiring diagram, say φ, consists of
a typed finite set of internal wires int(φ),
a typed function φin : inp(X ) → int(φ) + inp(Y ), and
a typed function φout : int(φ) + out(Y ) → out(X ).
Recall the contravariance of dependent products, e.g.
φin : int(φ) × inp(Y ) −→ inp(X ).
Suppose given an X -propagator (S , f ) ∈ P(X ), where
f : S × inp(X ) −→ S × out(X ).
We need to define a Y -propagator (T , g ) = P(φ)(S , f ) ∈ P(Y ).
For the new state-set, use the product, T := S × int(φ).
For the new propagation function, use the composite,
S ×φin
f
S ×φout
S × int(φ) × inp(Y ) −−−−→ S × inp(X ) −−−→ S × out(X ) −−−−−→ S × int(φ) × out(Y ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
38 / 60
The W-algebra of propagators
Wiring propagators
Example wiring diagram φ : X → Y
Let all wires carry the pointed type (N, 0).
Note that there is one internal wire, so int(φ) = N.
φ: X → Y
Y
X
Consider the X -propagator ({∗}, +), where + : N × N → N is sum.
Then P(φ)({∗}, +) = (N, f ) has propagation function given by
f (s , y ) = (s + y , s + y )
As a list propagator, it reports the running total as advertised,
P
(y1 , . . . , yn ) 7→ ni=1 yi .
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
39 / 60
The W-algebra of propagators
Quick aside about safe WDs
Quick aside about safe WDs
We’ve only been drawing safe WDs.
the internal wires are inputs of X , i.e. int(φ) ⊆ inp(X ),
the restriction of φin to int(φ), i.e. the composite
φin
int(φ) ⊆ inp(X ) −−→ int(φ) + inp(Y ),
is the coproduct inclusion, and
the functions φin and φout are surjective.
Safe WDs form a symmetric monoidal subcategory of all WDs.
Wsafe ⊆ W.
When WDs are not safe, it becomes harder to interpret int(φ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
40 / 60
The W-algebra of propagators
Quick aside about safe WDs
An example of non-safe WDs
The issue: there are multiple ways to interpret this wiring diagram:
φ: X → Y
Since φout : int(φ) + out(Y ) → out(X ) need not be surjective,
the size of int(φ) could be 0 or 1, or anything for that matter!
Our choice influences the state-set of new propagators.
Weird things happen when considering compositions such as:
φ
ψ
ψ◦φ
X−
→Y −
→Z
David I. Spivak (MIT)
X −−−→ Z
A categorical foundation of FRP
Presented on 2014/01/23
41 / 60
The W-algebra of propagators
Quick aside about safe WDs
Aside: another example of scary stuff we prevent
Early on, we thought a wiring diagram φ : X → Y should be:
an assignment of demand wires to supply wires. That is,
a typed function φ : inp(X ) + out(Y ) −→ inp(Y ) + out(X ).
In particular, a wire could connect out(Y ) directly to inp(Y ).
But this has problems, especially if we want to allow loops.
?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
42 / 60
φ
ψ
Checking P on the composition X −
→ Y −→ Z
The W-algebra of propagators
φ
ψ
Checking P on the composition X →
− Y−
→Z
We have defined P : W → Set on objects, morphisms, and stacking.
We must check that it works well with composition.
The computation is very straightforward:
S ×int(φ)×ψin
S × int(φ) × int(ψ) × inp(Z )
/
S ×φin
S × int(φ) × inp(Y )
P(ψ◦φ)(f )
/ S × inp(X )
P(φ)(f )
S × int(φ) × int(ψ) × out(Z )
o
S ×int(φ)×ψout
S × int(φ) × out(Y )
o
f
S ×φout
S × out(X )
I show you this not because it’s hard, but because it’s easy.
We worked hard to make this as simple as possible.
Our goal was to have something people would want to use!
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
43 / 60
The W-algebra of propagators
φ
ψ
Checking P on the composition X −
→ Y −→ Z
The subalgebra generated by NANDs?
Each transistor on a chip acts as a NAND gate, a simple propagator.
NOT
NAND
NAND
From here we can get NOT gates, then AND gates, and all logic gates.
Then n-bit adders, multiplication circuits, etc.
Consider the box T := ({a , b : Bool }, {c : Bool }) ∈ Ob(W).
Begin with the free algebra on T , denoted Fr (T ) : W → Set.
P
It is the algebra that sends X to n∈N HomW (T ⊕n , X ).
Now, there’s a unique map Fr (T ) → P, sending T 7→ NAND.
Its image defines the algebra of propagators generated by NAND.
Question: How does it compare to the computable functions?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
44 / 60
Changing the state-set
Morphisms of propagators
Morphisms of propagators
Let A and B be pointed sets.
Suppose we have two (A , B )-propagators, (S , f ) and (T , g ).
A morphism of propagators from (S , f ) to (T , g ) consists of:
a function ρ : S → T ,
such that the following diagram commutes:
S ×A
ρ×A
f
T ×A
/S ×B
g
ρ×B
/T ×B
If we’re working with initialized propagators, we require ρ(s0 ) = t0 .
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
45 / 60
Changing the state-set
Morphisms of propagators
Connected propagators act the same on lists
Let (S , s0 , f ) be an initialized (A , B )-propagator.
Recall: for each n ∈ N, it induces a function A n → B n , and
their sum is a function LP(S , s0 , f ) : List(A ) → List(B ).
Suppose given a morphism ρ : (S , s0 , f ) → (T , t0 , g ) of propagators.
In this case it is easy to show that LP(S , s0 , f ) = LP(T , t0 , g ).
So if two propagators are connected, they act the same on lists.
We write (S , s0 , f ) ∼ (T , t0 , g ) if they are connected by a zigzag.
(Aside: zigzags are chains like this, P0 ← P1 → P2 ← · · · → Pn .)
The relation ∼ is an equivalence relation on X -propagators.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
46 / 60
Changing the state-set
Morphisms of propagators
List(A ) can always serve as state-set
Let (S , s0 , f ) be an initialized (A , B )-propagator.
For each n ∈ N, it induces a function fn : A n → S × B n .
For convenience, we give names to its first and last projections,
σn : A n → S
and
ωn+1 : A n+1 → B .
We’ll find an equivalent propagator with state-set List(A ).
Let T = List(A ) and let t0 = [ ] be the empty list.
We need a propagation function b
f : T × A −→ T × B.
n
b
It’s sufficient to provide fn : A × A −→ A n+1 × B for every n ∈ N.
Use the top row in the diagram below.
An × A
(A n+1 ,ωn+1 )
A n+1
σn+1 ×B
σn ×A
S ×A
/ A n +1 × B
f
/ S ×B
The rest of the diagram shows the morphism (T , t0 ,b
f ) → (S , s0 , f ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
47 / 60
Changing the state-set
State reduction
State reduction
For any (A , B )-propagator (S , s0 , f ) we found a morphism
ρ : (List(A ), [ ],b
f ) −→ (S , s0 , f ).
In fact ρ is unique.
The image of ρ is some (S 0 , s0 , f ) having a subset of states S 0 ⊆ S.
S 0 is the set of reachable states, those that obtain on some list of input.
We can also quotient by an equivalence relation on states.
Declare two states equivalent if they act the same on any input list.
We have LP(S , −, f ) : S −→ List(B )List(A ) .
e be its image, so we have q : S S
e ⊆ List(B )List(A ) .
Let S
e
So S is the quotient of S by the equivalence relation.
e is the state-set for an equivalent propagator.
It is easy to show that S
S ×A
q ×A
f
/S ×B
q ×B
/S
e×B
e×A
S
e
f
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
48 / 60
Changing the state-set
Algorithmic state reduction
Algorithmic state reduction
Given an (A , B )-propagator, we want the smallest equivalent one.
e, s0 ,e
If (S , s0 , f ) is such that every state is reachable, use (S
f ).
In this case, and if A and S are finite, Hopcroft’s algorithm finds the
e, s0 ,e
smallest equivalent propagator (S
f ) in O (|S ||A |log |S |) time.
]
If some states are not reachable, use List
(A ), [ ],b
f .
e
Call this the minimal reduction of (S , s0 , f ).
It is a normal form for propagators.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
49 / 60
Changing the state-set
State reduction and wiring diagrams
State reduction and wiring diagrams
Back to the main theme, we had P : W → Set.
But in fact it can be extended to a monoidal functor P : W → Cat.
For each X ∈ Ob(W) we now have a category P(X ) of propagators.
For stacking boxes, there’s a functor P(X ) × P(Y ) → P(X ⊕ Y ).
For each WD φ : X → Y there’s a functor P(φ) : P(X ) → P(Y ).
And these all work together as required.
This means that reducing commutes with wiring.
Given a morphism φ : X → Y and a propagator P ∈ P(X ),
you can reduce P P 0 then apply P(φ),
and the result is a reduction, P(φ)(P ) P(φ)(P 0 ).
Note that this does not say much about minimal reduction.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
50 / 60
Changing the state-set
State reduction and wiring diagrams
Minimal reduction for wiring diagrams?
Suppose given a wiring diagram φ : X1 ⊕ X2 −→ Y .
φ : X1 ⊕ X2 → Y
Y
X2
X1
And suppose given propagators P1 ∈ P(X1 ) and P2 ∈ P(X2 ).
You can find minimal reductions of each, but it’s a waste of time.
If P1 only spits out one value, it effectively reduces P2 ’s input set.
Question: find an efficient algorithm to minimize P(φ)(P1 , P2 )?
Can you take advantage of the knowledge that it has this form?
Perhaps use machine learning to “atrophy” unused expressivity in the
wires?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
51 / 60
Customizing the language
How’s the time?
Shall we change gears a bit, or skip to the end?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
52 / 60
Customizing the language
Wiring diagrams as a visual language
Wiring diagrams as a visual language
One major feature of wiring diagrams is to engage the human visual
system.
Operadic pictures are a visual language for building instructions.
The category W purely syntactic.
We can build predefined functions into W.
For example, delay propagators might be denoted by nodes −
−
•.
Y
X
Or propagators that bundle four wires into a bus might be denoted by
4
.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
53 / 60
Customizing the language
Wiring diagrams as a visual language
Aside: timing in a wiring diagram
The formulas are written above; here we interpret them in terms of
timing.
Wires move data instantaneously.
Each propagator takes one “clock-cycle” to process data.
Consider a box X with one input wire and one output wire,
inp(X ) = {T } = out(X ) of the same type, T .
T
X
T
We define the delay propagator of type T to be the simple propagator
with propagation function idT : T → T .
delayed +
+
id
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
54 / 60
Customizing the language
Baking in special propagators
Baking in special propagators
What does it mean to bake the delay node −•−, etc., into W?
We want the following to count as a wiring diagram φ : X → Y .
Y
X
That is, we name special boxes for which we have chosen
interpretations.
What’s the math?
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
55 / 60
Customizing the language
Baking in special propagators
The math for baking in special symbols, part 1
We need to choose special symbols in W and propagators for them.
Fix a SMC, S, objects of which are called special symbols.
Fix a strict monoidal functor ι : S → W.
For each symbol s ∈ Ob(S) choose an element ms ∈ P(ι(s )).
ι /
S
W
!
•
mt
{1}
P
/ Set
Now define a new SMC, denoted W[S] as follows:
It has the same objects as W, but morphisms are defined as:
HomW[S] (X , Y ) =
X
HomW (X ⊕ ι(s ), Y ).
s ∈Ob(S)
Given X ⊕ ι(s ) → Y and Y ⊕ ι(t ) → Z,
we can compose to X ⊕ ι(s ⊕ t ) → Z, because ι is strict.
Stacking ⊕ in W[S] is also achieved by the strictness of ι.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
56 / 60
Customizing the language
Baking in special propagators
The math for baking in special symbols, part 2
We have constructed a SMC denoted W[S] out of our setup,
S
!
•
ι
mt
{1}
/W
P
/ Set
Note we haven’t used m yet, we’ve only used ι up to now.
We need an algebra P[S] : W[S] → Set.
Have it act the same on boxes as P does: P[S](X ) := P(X ).
A morphism φ : X → Y in W[S] is a morphism φ : X ⊕ ι(s ) → Y in W.
We need to assign a function P[S](φ) : P(X ) → P(Y ).
Use the following composite:
P(X )×m
P(φ)
P(X ) P(X ) × {1} −−−−−−→ P(X ) × P(ι(s )) −
→ P(X ⊕ ι(s )) −−−−−−→ P(Y ).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
57 / 60
Conclusion
Work to be done (please feel free to help!)
Work to be done (please feel free to help!)
I’d like wiring diagrams to be implemented.
As mentioned in the introduction, David Darais plans to do so.
But I encourage others to do so as well!
I’d like an open source WD-programming language, based in NANDs.
Does the operadic enclosure operation adequately handle abstraction?
I’d like an operadic model of reinforcement learning.
Each box could represent a population of learning agents.
Suppose given an operadic wiring diagram φ : (X1 , . . . , Xn ) → Y .
Can φ distribute global rewards among the Xi in such a way that:
if each Xi optimizes its behavior according to this sub-reward,
the result is that Y has optimized according to the global reward?
Similar vein: operadic verification and validation?
Y
X2
X1
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
58 / 60
Conclusion
Summary
Summary
We can draw pictures like this:
Y
X2
X1
X3
Such a picture represents a morphism φ : X1 ⊕ X2 ⊕ X3 −→ Y in a
symmetric monoidal category called W.
We can fill each interior box of φ with a machine, and thus derive a
machine for the exterior box.
We can abstract away the details of any part by enclosing it.
The requisite formulas are straightforward and written out here in full.
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
59 / 60
Conclusion
Summary
Thanks!
Thanks for inviting me!
Fibonacci
1, 1, 2, 3, 5, 8, . . .
+
. . . , 8, 5, 3, 2, 1, 1
All wires carry (N, 1).
David I. Spivak (MIT)
A categorical foundation of FRP
Presented on 2014/01/23
60 / 60
Download