Website/slides/talks/2018-2-DBCategoryTheory/index.html

540 lines
18 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Category Theory for DB-ies</title>
<meta name="description" content="Mimir">
<meta name="author" content="Oliver Kennedy">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../reveal.js-3.5.0/css/reveal.css">
<link rel="stylesheet" href="ubodin.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../reveal.js-3.5.0/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? '../reveal.js-3.5.0/css/print/pdf.css' : '../reveal.js-3.5.0/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="../reveal.js-3.5.0/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="header">
<!-- Any Talk-Specific Header Content Goes Here -->
Category Theory for DB-ies
</div>
<div class="footer">
<!-- Any Talk-Specific Footer Content Goes Here -->
<div style="float: left; margin-top: 15px; ">
Exploring <u><b>O</b></u>nline <u><b>D</b></u>ata <u><b>In</b></u>teractions
</div>
<img src="graphics/FullText-white.png" height="40" style="float: right;"/>
</div>
<div class="slides">
<section>
<section>
<h2>Category Theory for DB-ies</h2>
<h4>(The Math of "Things" and "Stuff")<h4>
</section>
<section>
<img src="graphics/bake_pi.jpg" height="400px">
</section>
<section>
<p>People always say that SQL is "Declarative"</p>
</section>
<section>
<h3>Why is the following replacement ok?</h3>
<pre><code class="sql">
SELECT * FROM R WHERE A = 1 AND B = 2
</code></pre>
<pre><code class="sql">
SELECT * FROM (SELECT * FROM R WHERE A = 1) WHERE B = 2
</code></pre>
<p class="fragment">(and how is it the same as computing $x + 1 + 2$?)</p>
</section>
</section>
<section>
<section>
<h2>Addition is Cool</h2>
<p class="fragment" data-fragment-index="1">
<b class="fragment" data-fragment-index="4">Commutative</b>
$$a + b = b + a$$
</p>
<p class="fragment" data-fragment-index="2">
<b class="fragment" data-fragment-index="4">Associative</b>
$$(a + b) + c = a + (b + c)$$
</p>
<p class="fragment" data-fragment-index="3">
<b class="fragment" data-fragment-index="4">Neutral Element</b>
$$0 + a = a$$
</p>
</section>
<section>
<h2>Addition is Cool</h2>
<pre><code class="java">
public int add(int a, int b) { ... }
add(a, b) == add(b, a)
add(add(a, b), c) == add(a, add(b, c))
add(a, 0) == a
</code></pre>
</section>
<section>
<h2>Math is about Patterns</h2>
<table style="font-size: smaller;">
<tr><th>Commutativity</th><th>Associativity</th><th>Neutral Element</th></tr>
<tr>
<td>$a + b = b + a$</td>
<td>$(a + b) + c = a + (b + c)$</td>
<td>0</td>
</tr>
<tr class="fragment">
<td>$a \texttt{ AND } b = b \texttt{ AND } a$</td>
<td>$(a \texttt{ AND } b) \texttt{ AND } c = a \texttt{ AND } (b \texttt{ AND } c)$</td>
<td>TRUE</td>
</tr>
<tr class="fragment">
<td>$a \texttt{ OR } b = b \texttt{ OR } a$</td>
<td>$(a \texttt{ OR } b) \texttt{ OR } c = a \texttt{ OR } (b \texttt{ OR } c)$</td>
<td>FALSE</td>
</tr>
<tr class="fragment">
<td>$A \cup B = B \cup A$</td>
<td>$(A \cup B) \cup C = A \cup (B \cup C)$</td>
<td>$\emptyset$</td>
</tr>
<tr class="fragment" style="font-size: smaller">
<td>$min(A, B) = min(B, A)$</td>
<td>$min(min(A, B), C) = min(A, min(B, C))$</td>
<td>$\infty$</td>
</tr>
</table>
<p class="fragment">Mathematicians call this a <b>Group</b></p>
</section>
<section>
<pre><code class="java">
interface Group&lt;K&gt; {
public K getZero() { ... }
public K add(K a, K b) { ... }
}
</code></pre>
<br/>
<p class="fragment">
$$\left<\; K,\; +,\; 0\;\right>$$
</p>
</section>
<section>
$$\left<\; \mathbb R,\; +,\; 0\;\right>$$
$$\left<\; \mathbb N,\; +,\; 0\;\right>$$
$$\left<\; \mathbb B,\; \texttt{AND},\; \texttt{TRUE}\;\right>$$
$$\left<\; \mathbb B,\; \texttt{OR},\; \texttt{FALSE}\;\right>$$
$$\left<\; Set,\; \cup,\; \emptyset\;\right>$$
$$\left<\; Multiset,\; \uplus,\; \emptyset\;\right>$$
$$\left<\; \mathbb Z^{-\infty},\; max,\; -\infty;\right>$$
</section>
</section>
<section>
<section>
<h2>Fun With Groups</h2>
<p style="margin-top: 100px; margin-bottom: 200px;">
$$\left<\; Set,\; \cup,\; \emptyset\;\right>$$
$$\left<\; \mathbb N,\; max,\; -\infty\;\right>$$
</p>
</section>
<section>
<pre><code class="java">
class SetGroup implements Group&lt;Set&lt;int&gt;&gt; {
// Empty Set union with anything is a no-op
public Set&lt;int&gt; getZero() { return Collections.emptySet(); }
// Compute Set Union
public Set&lt;int&gt; add(Set&lt;int&gt; a, Set&lt;int&gt; b) {
Set&lt;int&gt; tmp = new HashSet&lt;int&gt;(a);
tmp.addAll(b);
return tmp;
}
}
</code></pre>
</section>
<section>
<pre><code class="java">
class IntMaxGroup implements Group&lt;int&gt; {
// Max(x, -infinity) = x
public int getZero() { return Integer.MIN_VALUE; }
// Compute Max of two elements
public int add(int a, int b) { return Integer.max(a, b) }
}
</code></pre>
</section>
<section>
<pre><code class="java">
//Compute the maxium value in a Set
public int SetMax(Set&lt;int&gt; set) {
int ret = Integer.MIN_VALUE
for(int i : set) {
ret = Integer.max(i, ret);
}
return ret;
}
</code></pre>
<p style="margin-top: 100px;" class="fragment">
$$\texttt{SetMax} : SetGroup \rightarrow IntGroup$$
</p>
</section>
<section>
<p>There's something really cool about <tt>SetMax</tt></p>
</section>
<section>
$$\texttt{SetMax}(A + B) \equiv \texttt{SetMax}(A) + \texttt{SetMax}(B)$$
<svg data-src="graphics/homomorphism.svg" class="fragment" style="margin-top:50px"/>
</section>
<section>
<p>Mathematicians call this a <b>homomorphism</b></p>
<dl class="fragment">
<dt>Homo</dt>
<dd>"Same"</dd>
<dt>Morphism</dt>
<dd>"Function"</dd>
</dl>
</section>
<section>
<h1>So what?</h1>
</section>
<section>
<pre><code class="sql">
SELECT MAX(x) FROM (SELECT * FROM R UNION S)
</code></pre>
<pre><code class="sql">
MAX( (SELECT MAX(x) FROM R), (SELECT MAX(x) FROM S) )
</code></pre>
</section>
<section>
<h2>Without Homomorphisms</h2>
<svg data-src="graphics/distributed-1.svg"/>
<imagecredits>(OpenClipArt.org)</imagecredits>
</section>
<section>
<h2>With Homomorphisms</h2>
<svg data-src="graphics/distributed-2.svg"/>
<imagecredits>(OpenClipArt.org)</imagecredits>
</section>
</section>
<section>
<section>
<p>Let's dig a little deeper</p>
</section>
<section>
<h2>Multiplication is also Cool!</h2>
<p class="fragment" data-fragment-index="1">
<b>Commutative</b>
$$a \times b = b \times a$$
</p>
<p class="fragment" data-fragment-index="2">
<b>Associative</b>
$$(a \times b) \times c = a \times (b \times c)$$
</p>
<p class="fragment" data-fragment-index="3">
<b>Neutral Element</b>
$$1 \times a = a$$
</p>
</section>
<section>
<h2>Combine for epic coolness!</h2>
<p class="fragment" data-fragment-index="1">
<b class="fragment" data-fragment-index="3">Distributive</b>
$$a \times (b + c) = a \times b + a \times c$$
</p>
<p class="fragment" data-fragment-index="2">
<b class="fragment" data-fragment-index="3">Sphere of Annihilation</b>
$$0 \times a = 0$$
</p>
</section>
<section>
<h2>More Patterns!</h2>
<table>
<!-- -->
<tr><th>$K$</th> <th>$+$</th> <th>$\times$</th> <th>$0$</th> <th>$1$</th>
<td></td></tr>
<!-- -->
<tr><td>$\mathbb N$</td> <td>$+$</td> <td>$\times$</td> <td>$0$</td> <td>$1$</td>
<td style="text-align: left">Natural Number Arithmetic</td></tr>
<!-- -->
<tr><td>$\mathbb B$</td> <td>$\vee$</td><td>$\wedge$</td> <td>F</td> <td>T</td>
<td style="text-align: left">Boolean Algebra</td></tr>
<!-- -->
<tr class="fragment highlight-current-blue" data-fragment-index="2">
<td>Tables</td> <td>$\cup$</td><td>$\bowtie$</td><td>$\emptyset$</td><td>$\left<\right>$</td>
<td style="text-align: left">SQL</td></tr>
</table>
<p class="fragment" data-fragment-index="1">Mathematicians call this a <b>Semiring</b></p>
</section>
<section>
<table><tr>
<td>
<table>
<tr><th>$Students$</th><th>|</th><th>S_ID</th><th>Name</th></tr>
<tr><td> </td><td>|</td><td>111 </td><td>Alice</td></tr>
<tr><td> </td><td>|</td><td>222 </td><td>Bob</td></tr>
<tr><td> </td><td>|</td><td>333 </td><td>Carol</td></tr>
</table>
</td>
<td>
<table>
<tr><th>$Courses$</th><th>|</th><th>S_ID</th><th>Course</th></tr>
<tr><td> </td><td>|</td><td>111 </td><td>CSE-562</td></tr>
<tr><td> </td><td>|</td><td>111 </td><td>CSE-521</td></tr>
<tr><td> </td><td>|</td><td>222 </td><td>CSE-562</td></tr>
</table>
</td>
</tr></table>
</section>
<section>
<p>How many courses is each student taking?</p>
<table class="fragment">
<tr><th>$\pi_{S\_ID}(Courses)$</th><th>|</th><th>S_ID</th></tr>
<tr><td> </td><td>|</td><td>111 </td></tr>
<tr><td> </td><td>|</td><td>111 </td></tr>
<tr><td> </td><td>|</td><td>222 </td></tr>
</table>
<pre class="fragment"><code class="sql">SELECT S_ID FROM COURSES</code></pre>
</section>
<section>
<p>How many courses is each student taking?</p>
<table>
<tr><th>$\pi_{S\_ID}(Courses)$</th><th>|</th><th>S_ID</th><th> </th><th>#</th></tr>
<tr><td> </td><td>|</td><td>111 </td><td>$\rightarrow$</td><td>2</td></tr>
<tr><td> </td><td>|</td><td>222 </td><td>$\rightarrow$</td><td>1</td></tr>
<tr class="fragment" style="color: grey"
><td> </td><td>|</td><td>* </td><td>$\rightarrow$</td><td>0</td></tr>
</table>
<pre><code class="sql">SELECT S_ID, COUNT(*) FROM COURSES GROUP BY S_ID</code></pre>
</section>
<section>
<p>How many courses is each student taking?</p>
<table>
<tr><th>$\pi_{Name}(Courses \bowtie Students)$</th><th>|</th><th>S_ID</th><th> </th><th>#</th></tr>
<tr><td> </td><td>|</td><td>111 </td><td>$\rightarrow$</td><td class="fragment highlight-current-blue" data-fragment-index=1>2</td></tr>
<tr><td> </td><td>|</td><td>222 </td><td>$\rightarrow$</td><td class="fragment highlight-current-blue" data-fragment-index=1>1</td></tr>
<tr style="color: grey"
><td> </td><td>|</td><td>* </td><td>$\rightarrow$</td><td class="fragment highlight-current-blue" data-fragment-index=1>0</td></tr>
</table>
<pre><code class="sql">SELECT Name, COUNT(*) FROM COURSES GROUP BY S_ID</code></pre>
</section>
<section>
<table>
<tr>
<td>$$\left<\mathbb B, \vee, \wedge, F, T\right>$$</td>
<td>Set Databases (SELECT DISTINCT)</td>
</tr>
<tr>
<td>$$\left<\mathbb N, +, \times, 0, 1\right>$$</td>
<td>Multiset Databases (Normal SQL)</td>
</tr>
</table>
<p class="fragment"><b>Other Applications</b>: Provenance, Permissions, Differential Privacy</p>
</section>
<section>
<table>
<tr>
<th>Database Operation</th>
<th>Semiring Operation</th>
</tr>
<tr>
<td>Union</td>
<td>$+$</td>
</tr>
<tr>
<td>Join</td>
<td>$\times$</td>
</tr>
<tr>
<td>Aggregation</td>
<td>$+$</td>
</tr>
</table>
</section>
</section>
<section>
<section>
<pre><code class="sql">
SELECT * FROM R WHERE A = 1
</code></pre>
<p class="fragment" style="margin-top: 100px">
$$\sigma_{A = 1}(t \rightarrow \#) = \begin{cases} \# & \text{if }t.A = 1 \\ 0 & \text{otherwise}\end{cases}$$
</p>
</section>
<section>
<pre><code class="sql">
SELECT * FROM (SELECT * FROM R WHERE A = 1) WHERE B = 2
</code></pre>
<p style="margin-top: 100px">
$$\sigma_{B = 1}(\sigma_{A = 1}(t \rightarrow \#)) = \begin{cases} \sigma_{A = 1}(t \rightarrow \#) & \text{if }t.B = 2 \\ 0 & \text{otherwise}\end{cases}$$
</p>
</section>
<section>
<pre><code class="sql">
SELECT * FROM (SELECT * FROM R WHERE A = 1) WHERE B = 2
</code></pre>
<p style="margin-top: 100px">
$$\sigma_{B = 1}(\sigma_{A = 1}(t \rightarrow \#)) = \begin{cases} \# & \text{if }t.A = 1\text{ and }t.B = 2 \\ 0 & \text{if }t.A = 1\text{ and }t.B \neq 2 \\ 0 & \text{otherwise}\end{cases}$$
</p>
</section>
<section>
<pre><code class="sql">
SELECT * FROM (SELECT * FROM R WHERE A = 1) WHERE B = 2
</code></pre>
<p style="margin-top: 100px">
$$\sigma_{B = 1}(\sigma_{A = 1}(t \rightarrow \#)) = \begin{cases} \# & \text{if }t.A = 1\text{ and }t.B = 2 \\ 0 & \text{otherwise}\end{cases}$$
</p>
</section>
<section>
<pre><code class="sql">
SELECT * FROM R WHERE A = 1 AND B = 2
</code></pre>
<p style="margin-top: 100px">
$$\sigma_{B = 1}(\sigma_{A = 1}(t \rightarrow \#)) = \begin{cases} \# & \text{if }t.A = 1\text{ and }t.B = 2 \\ 0 & \text{otherwise}\end{cases}$$
</p>
</section>
</section>
<section>
<h2>Other Resources</h2>
<dl>
<dt>How to Bake Pi</dt>
<dd><img src="graphics/bake_pi.jpg" height="100px">
<dt>Category Theory for Programmers</dt>
<dd><a href="https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/">https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/</a>
</dl>
</section>
</div></div>
<script src="../reveal.js-3.5.0/lib/js/head.min.js"></script>
<script src="../reveal.js-3.5.0/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/../reveal.js#configuration
Reveal.initialize({
controls: false,
progress: true,
history: true,
center: true,
slideNumber: true,
transition: 'fade', // none/fade/slide/convex/concave/zoom
// Optional ../reveal.js plugins
dependencies: [
{ src: '../reveal.js-3.5.0/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: '../reveal.js-3.5.0/plugin/math/math.js',
condition: function() { return true; },
mathjax: '../reveal.js-3.5.0/js/MathJax.js'
},
{ src: '../reveal.js-3.5.0/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../reveal.js-3.5.0/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../reveal.js-3.5.0/plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: '../reveal.js-3.5.0/plugin/zoom-js/zoom.js', async: true },
{ src: '../reveal.js-3.5.0/plugin/notes/notes.js', async: true },
{ src: '../reveal.js-3.5.0/plugin/svginline/es6-promise.auto.js', async: false },
{ src: '../reveal.js-3.5.0/plugin/svginline/data-src-svg.js', async: false }
]
});
</script>
</body>
</html>