relational algebra by way of adjunctions 2 · grading (indexing, parametrizing) a monad by a...

18

Upload: others

Post on 07-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid
Page 2: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 2

1. Overview

• relational databases in terms of certain monads (sets, bags, lists)

• monads support comprehensions, providing a query notation:

[ (customer.name, invoice.amount)| customer customers,

invoice invoices, invoice.due 6 today,customer.cid == invoice.customer ]

• monads have nice mathematical foundations via adjunctions

• monad structure explains aggregation, selection, projection

• less obvious how to explain join

Page 3: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 3

2. Galois connections

Relating monotonic functions between two ordered sets:

(A,6)

g

99? (B,v)

f

yymeans f b 6 a () b v g a

For example,

(R,R)

floor

99? (Z,Z)

inj

yy(Z,6)

÷k

99? (Z,6)

⇥k

yy

“Change of coordinates” can sometimes simplify reasoning.Eg rhs gives n⇥ k 6 m() n 6 m ÷ k, and multiplication is easier to reason aboutthan rounding division.

Page 4: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 4

3. Category theory from ordered sets

A category C consists of

• a set⇤ |C| of objects,

• a set⇤ C(X , Y ) of arrows X ! Y for each X , Y : |C|,• identity arrows id

X

: X ! X for each X

• composition f · g : X ! Z of compatible arrows g : X ! Y and f : Y ! Z,

• such that composition is associative, with identities as units.

Think of a directed graph, with vertices as objects and paths as arrows.

An ordered set (A,6) is a degenerate category, with objects A and a unique arrowa ! b iff a 6 b.

· · · ** �2** �1

((0

((1

((2

))· · ·Many categorical concepts are generalisations from ordered sets.

⇤proviso. . .

Page 5: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 5

4. Concrete categories

Ordered sets are a concrete category: roughly,

• the objects are sets with additional structure

• the arrows are structure-preserving mappings

For example, category PoSet has preordered sets (A,6) as objects, and monotonicfunctions h : (A,6)! (B,v) as arrows:

a 6 a

0 =) h(a) v h(a

0)

For another example, category CMon has commutative monoids (M ,⌦, ✏) asobjects, and homomorphisms h : (M ,⌦, ✏)! (M

0,�, ✏0) as arrows:

h (m⌦ n) = h m� h n

h ✏ = ✏0

Trivially, category Set has sets (no additional structure) as objects, and totalfunctions as arrows.

Page 6: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 6

5. Functors

Categories are themselves structured objects. . .

A functor F : C! D is an operation on both objects and arrows, preserving thestructure: F f : F X ! F Y when f : X ! Y , and

F id

X

= id

F X

F (f · g) = F f · F g

For example, forgetful functor U : CMon! Set:

U (M ,⌦, ✏) = M

U (h : (M ,⌦, ✏)! (M

0,�, ✏0)) = h : M ! M

0

Conversely, Free : Set! CMon generates the free commutative monoid (ie bags)on a set of elements:

Free A = (Bag A,],;)Free (f : A! B) = map f : Bag A! Bag B

Page 7: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 7

6. Adjunctions

Adjunctions are the categorical generalisation of Galois connections.

Given categories C, D, and functors L : D! C and R : C! D, adjunction

C

R

??? D

L

~~means⇤ b-c : C(L X , Y ) ' D(X , R Y ) : d-e

The functional programmer’s favourite example is given by currying:

Set

(-)P

<<? Set

-⇥P

||with curry : Set(X ⇥ P, Y ) ' Set(X , Y

P) : curry

hence definitions and properties of apply = uncurry id

Y

P : Y

P ⇥ P ! Y .

Page 8: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 8

7. Products and coproducts

Set

<<? Set

2

+||

<<? Set

—zz

with

fork : Set

2(—A, (B, C)) ' Set(A, B ⇥ C) : fork

junc

� : Set(A + B, C) ' Set

2((A, B), —C) : junc

hence

dup = fork id

A,A : Set(A, A⇥A)(fst, snd) = fork

�id

B⇥C

: Set

2(—(B, C), (B, C))

give tupling and projection. Dually for sums and injections.And more generally for any arity—even zero.

Page 9: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 9

8. Free commutative monoids

Free/forgetful adjunction:

CMon

U

;;? Set

Free

{{with b-c : CMon(Free A, (M ,⌦, ✏))

' Set(A, U (M ,⌦, ✏)) : d-e

Unit and counit:

single A = bidFree A

c : A! U (Free A)LMM = did

M

e : Free (U M)! M -- for M = (M ,⌦, ✏)

whence, for h : Free A! M and f : A! U M = M ,

h = LMM · Free f () U h · single A = f

ie 1-to-1 correspondence between (i) homomorphisms from thefree commutative monoid (bags) and (ii) their behaviour on singletons.

Page 10: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 10

9. Aggregation

Aggregations are bag homomorphisms:

aggregation monoid action on singletons

count (N, 0, +) *a+ , 1sum (R, 0, +) *a+ , a

max (Z[ {�1},�1, max) *a+ , a

all (B, True,^) *a+ , a

Projection ⇡i

= Bag i is a homomorphism—just functorial action.Selection �

p

is also a homomorphism, to bags, with action

guard : (A! B)! Bag A! Bag A

guard p a = if p a then *a+ else;

Projection and selection laws follow from homomorphism laws(and from laws of coproducts, since B = 1 + 1).

Page 11: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 11

10. Monads

Finite bags form a monad (Bag, union, single) with

Bag = U · Free

union : Bag (Bag A)! Bag A

single : A! Bag A

which justifies the use of comprehension notation

*f a b _ a x, b g a+

and its equational properties.

In fact, any adjunction L a R yields a monad (T, µ, ⌘) on D, where

C

R

??? D

L

~~

T = R · L

µ A = R didA

e L : T (T A)! T A

⌘ A = bidA

c : A! T A

Page 12: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 12

11. Maps

Database indexes are essentially maps Map K V = V

K . Maps (-)K from K form amonad (the Reader monad in Haskell), so arise from an adjunction.

The laws of exponents follow from this adjunction, and from those for productsand coproducts:

Map 0 V ' 1Map 1 V ' V

Map (K1 + K2) V ' Map K1 V ⇥Map K2 V

Map (K1 ⇥ K2) V ' Map K1 (Map K2 V )Map K 1 ' 1Map K (V1 ⇥ V2) ' Map K V1 ⇥Map K V2 : merge

—ie merge is right-to-left half of the latter iso:

merge : Map K V1 ⇥Map K V2 ! Map K (V1 ⇥ V2)

Page 13: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 13

12. Indexing

Relations are in 1-to-1 correspondence with set-valued functions:

Rel

E

<<? Set

J

||where J embeds, and E R : A! Set B for R : A ⇠ B.

Moreover, the correspondence remains valid for bags:

index : Bag (K ⇥ V ) ' Map K (Bag V )

Together, index and merge give efficient relational joins:

x

f

ˆg

y = flatten (Map K cp (merge (groupBy f x, groupBy g y)))

groupBy : Eq K ) (V ! K)! Bag V ! Map K (Bag V )flatten : Map K (Bag V )! Bag V

expressible also via comprehensive comprehensions

Page 14: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 14

13. Finiteness

A catch:

• being finite is important, for aggregations

• begin a monad is important, for comprehensions

• finite bags form a monad (as above)

• maps form a monad, but finite maps do not: the unit

⌘ a = (�k ! a) : A! Map K A

generally yields an infinite map.

How to reconcile finiteness of maps with being a monad?

Page 15: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 15

14. Graded monads

Grading (indexing, parametrizing) a monad by a monoid:an indexed family of endofunctors that collectively behave like a monad.

For monoid M = (M ,⌦, ✏), the M-graded monad (T, µ, ⌘) isa family T

m

of endofunctors indexed by m : M , with

µ X : T

m

(T

n

X )! T

m⌦n

X

⌘ X : X ! T✏ X

satisfying the usual laws. These too arise from adjunctions(even though T itself is not an endofunctor!).

For example, think of finite vectors, indexed by length.

We use the monoid (K⇤, ++, h i) of finite sequences of finite key types K.

Page 16: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 16

15. Query transformations

These can now all be shown by equational reasoning:

⇡i

· ⇡j

= ⇡i

-- when i · j = i

�p

· ⇡i

= ⇡i

· �p

-- when p · i = p

LMM · Bag f · ⇡i

= LMM · Bag (f · i)LMM · Bag f · �

p

= LMM · Bag (�a ! if p a then f a else ✏)x

f

ˆg

y = Bag swap (y

g

ˆf

x)(x

f

ˆg

y) (g·snd)ˆh

z = Bag assoc (x

f

ˆ(g·fst) (y

g

ˆh

z))⇡

i⇥j

(x

f

ˆg

y) = ⇡i

x

f

0ˆg

0 ⇡j

y -- when f a = g b () f

0 (i a) = g

0 (j b)�

p

(x

f

ˆg

y) = �q

x

f

ˆg

�r

y -- when p (a, b) = q a ^ r b

for monoid M = (M ,⌦, ✏).

Page 17: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

Relational Algebra by Way of Adjunctions 17

16. Summary

• monad comprehensions for database queries

• structure arising from adjunctions

• equivalences from universal properties

• fitting in relational joins, via indexing and graded monads

• calculating query transformations

Paper to appear at ICFP 2018.

Thanks to EPSRC Unifying Theories of Generic Programming for funding.

Page 18: Relational Algebra by Way of Adjunctions 2 · Grading (indexing, parametrizing) a monad by a monoid: an indexed family of endofunctors that collectively behave like a monad. For monoid

transformations in relational algebra come from adjunctions