Naturality, but not as you know it

Similar documents
The Lifting Lemma. Ralf Hinze

Relational Algebra by Way of Adjunctions. Jeremy Gibbons (joint work with Fritz Henglein, Ralf Hinze, Nicolas Wu) DBPL, October 2015

Algebras for Combinatorial Search

Inverting the Burrows-Wheeler Transform

A categorical view of computational effects

F U N C T I O N A L P E A R L S Inverting the Burrows-Wheeler Transform

Functor Pulling. Lambert Meertens

DEFINITIONS: OPERADS, ALGEBRAS AND MODULES. Let S be a symmetric monoidal category with product and unit object κ.

Programming Languages in String Diagrams. [ four ] Local stores. Paul-André Melliès. Oregon Summer School in Programming Languages June 2011

On a Monadic Encoding of Continuous Behaviour

Boolean Algebras, Boolean Rings and Stone s Representation Theorem

Category Theory. Categories. Definition.

Direct Limits. Mathematics 683, Fall 2013

The Under-Performing Unfold

2-DIMENSIONAL TOPOLOGICAL QUANTUM FIELD THEORIES AND FROBENIUS ALGEBRAS. Contents 1. The main theorem 1

A Simple Implementation Technique for Priority Search Queues

Remarks on categorical equivalence of finite unary algebras

Categories and Functors (Lecture Notes for Midlands Graduate School, 2013) Uday S. Reddy The University of Birmingham

CATEGORICAL GROTHENDIECK RINGS AND PICARD GROUPS. Contents. 1. The ring K(R) and the group Pic(R)

Lecture 17: Invertible Topological Quantum Field Theories

About categorical semantics

Proofs Regarding Specification of Spark Aggregation

Parallel Prefix Sums In An Embedded Hardware Description Language

Computing autotopism groups of partial Latin rectangles: a pilot study

Cohomology and Base Change

Postulated colimits and left exactness of Kan-extensions

Universal Algebra for Termination of Higher-Order Rewriting. Makoto Hamana

MORITA EQUIVALENCE OF MANY-SORTED ALGEBRAIC THEORIES

Categories for Imperative Semantics

Enriched Categories. Stephen Fitz

Why walk when you can take the tube?

A categorical model for a quantum circuit description language

Overview. Systematicity, connectionism vs symbolic AI, and universal properties. What is systematicity? Are connectionist AI systems systematic?

INTRODUCTION TO CATEGORY THEORY FOR FUNCTIONAL PROGRAMMING

Notes on Monoids and Automata

Artin s map in stable homology. Ulrike Tillmann

Exercises on chapter 0

Teooriaseminar. TTÜ Küberneetika Instituut. May 10, Categorical Models. for Two Intuitionistic Modal Logics. Wolfgang Jeltsch.

D-MODULES: AN INTRODUCTION

(work to be done!) Ronnie Brown () Higher Structures Lisbon July 24-27, 2017 Instituto Superior Técnico, Lisbon Homotopical 1 / 19

1 The Hyland-Schalke functor G Rel. 2 Weakenings

Category theory for computer science. Overall idea

Semantics for algebraic operations

ACLT: Algebra, Categories, Logic in Topology - Grothendieck's generalized topological spaces (toposes)

Equational Reasoning with Applicative Functors. Institute of Information Security

Applications of 2-categorical algebra to the theory of operads. Mark Weber

1 Categorical Background

Lecture 15: Duality. Next we spell out the answer to Exercise It is part of the definition of a TQFT.

Categories and Functors (Lecture Notes for Midlands Graduate School, 2012) Uday S. Reddy The University of Birmingham

Partially ordered monads and powerset Kleene algebras

Categorical techniques for NC geometry and gravity

Towards a Flowchart Diagrammatic Language for Monad-based Semantics

Jesse Han. October 10, McMaster University. Strong conceptual completeness and internal. adjunctions in DefpT q. Jesse Han.

On the skeleton of a finite transformation semigroup

Lattices and Orders in Isabelle/HOL

Categorical quantum channels

An algebraic view of topological -machines

Inverting operations in operads

arxiv: v1 [math.ct] 28 Dec 2018

p I Hecke I ( 1) i Tr(T Λ n i V ) T i = 0.

Higher Order Containers

Monoids: Theme and Variations (Functional Pearl)

Affine Monads and Side-Effect-Freeness

The category of open simply-typed lambda terms

Cartesian Closed Topological Categories and Tensor Products

Construction of Physical Models from Category Theory. Master Thesis in Theoretical Physics. Marko Marjanovic

Math Homotopy Theory Spring 2013 Homework 13 Solutions

Monoids: Theme and Variations (Functional Pearl)

Relational Reasoning for Recursive Types and References

Binary Operations. Chapter Groupoids, Semigroups, Monoids

When is a function a fold or an unfold?

Conservation of Information

ALGEBRAIC GEOMETRY (NMAG401) Contents. 2. Polynomial and rational maps 9 3. Hilbert s Nullstellensatz and consequences 23 References 30

What s category theory, anyway? Dedicated to the memory of Dietmar Schumacher ( )

Representations of quivers

WHY POLYNOMIALS? PART 1

CATEGORY THEORY. Cats have been around for 70 years. Eilenberg + Mac Lane =. Cats are about building bridges between different parts of maths.

An algebraic formulation of dependent type theory

From syntax to semantics of Dependent Type Theories - Formalized

Algebraic Geometry Spring 2009

Permutation Groups and Transformation Semigroups Lecture 2: Semigroups

Categories and Natural Transformations

Counting Colorings Cleverly

106 CHAPTER 3. TOPOLOGY OF THE REAL LINE. 2. The set of limit points of a set S is denoted L (S)

Vectors Coordinate frames 2D implicit curves 2D parametric curves. Graphics 2008/2009, period 1. Lecture 2: vectors, curves, and surfaces

MA441: Algebraic Structures I. Lecture 14

FUNCTORS AND ADJUNCTIONS. 1. Functors

A Type Theory for Probabilistic and Bayesian Reasoning

Lecture 9 - Faithfully Flat Descent

Universal Algebra for Logics

Tsirelson s problem and linear system games

Congruence Boolean Lifting Property

Universal Properties of Impure Programming Languages

Boolean bunched logic: its semantics and completeness

Topological Groupoids and Exponentiability

arxiv:math/ v1 [math.gt] 14 Nov 2003

Reconsidering MacLane. Peter M. Hines

OMEGA-CATEGORIES AND CHAIN COMPLEXES. 1. Introduction. Homology, Homotopy and Applications, vol.6(1), 2004, pp RICHARD STEINER

OPERAD BIMODULE CHARACTERIZATION OF ENRICHMENT. V2

1 Differentiable manifolds and smooth maps

Transcription:

Naturality, but not as you know it Prologue WG2.8 #31, Aussois, October 2013 Naturality, but not as you know it Ralf Hinze Department of Computer Science, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@cs.ox.ac.uk http://www.cs.ox.ac.uk/ralf.hinze/ October 2013 Joint work with Fritz Henglein University of Oxford Ralf Hinze 1-22

Naturality, but not as you know it Prologue WG2.8 #31, Aussois, October 2013 0 Recap: Key-based sorting & searching Idea: employ the structure of sort keys directly. A hierarchy of operations: sort :: Order k [k v] [v] discr :: Order k [k v] [[v]] trie :: Order k [k v] Trie k [v] Keys and satellite data, ie values, are separated. An element of Order K represents an order over the type K: data Order :: where OUnit :: Order () OSum :: Order k 1 Order k 2 Order (k 1 + k 2 ) OProd :: Order k 1 Order k 2 Order (k 1 k 2 )... University of Oxford Ralf Hinze 2-22

Naturality, but not as you know it Prologue WG2.8 #31, Aussois, October 2013 0 Structure of correctness proofs Show that sort is correct. Relate sort and discr: concat discr o = sort o This is nontrivial as discr and sort have different algorithmic strategies: MSD versus LSD. Relate discr and trie: discr o = flatten trie o This is straightforward. University of Oxford Ralf Hinze 3-22

Naturality, but not as you know it Naturality WG2.8 #31, Aussois, October 2013 1 Recap: Naturality reverse :: [a] [a] is a natural transformation: map h reverse = reverse map h for all h :: A B. (Parametricity implies naturality.) (We work in Set.) University of Oxford Ralf Hinze 4-22

Naturality, but not as you know it Naturality WG2.8 #31, Aussois, October 2013 1 Recap: Naturality Given magic :: [a] [a] with map h magic = magic map h for all h :: A B. What do we know about magic? University of Oxford Ralf Hinze 5-22

Naturality, but not as you know it Naturality WG2.8 #31, Aussois, October 2013 1 Recap: Naturality magic :: [a] [a] is fully determined by its N instance. magic xs = { introduce ix : N A so that map ix [1.. n] = xs } magic (map ix [1.. n]) = { magic is natural } map ix (magic [1.. n]) University of Oxford Ralf Hinze 6-22

Naturality, but not as you know it Naturality WG2.8 #31, Aussois, October 2013 1 Recap: Naturality Say, we suspect that magic = reverse. It suffices to show that magic [1.. n] = reverse [1.. n]. magic xs = { see above } map ix (magic [1.. n]) = { proof obligation } map ix (reverse [1.. n]) = { reverse is natural } reverse (map ix [1.. n]) = { definition of ix } reverse xs University of Oxford Ralf Hinze 7-22

Naturality, but not as you know it Naturality WG2.8 #31, Aussois, October 2013 1 Recap: Naturality What about magic [ ] = [ ]? (Intuitively, magic :: [a] [a] can rearrange elements, delete elements, duplicate elements, but it cannot create elements.) University of Oxford Ralf Hinze 8-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Strong naturality reverse :: [a] [a] satisfies a stronger property: filter p reverse = reverse filter p for all p :: A Maybe B. (You may want to view p as a partial function.) filter combines mapping and filtering. filter :: (a Maybe b) ([a] [b]) filter p [ ] = [ ] filter p (x : xs) = case p x of Nothing filter p xs Just y y : filter p xs (Also called mapmaybe.) University of Oxford Ralf Hinze 9-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Strong naturality Given magic :: [a] [a] with filter p magic = magic filter p for all p :: A Maybe B. What do we know about magic? University of Oxford Ralf Hinze 10-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Strong naturality What about magic [ ] = [ ]? Let be the totally undefined function, a = Nothing, then magic [ ] = { property of filter } magic (filter [ ]) = { magic is strongly natural } filter (magic [ ]) = { property of filter } [ ] University of Oxford Ralf Hinze 11-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Permutation If magic :: [a] [a] additionally satisfies magic [x] = [x] then it permutes its input! (For simplicity, we only consider inputs with no repeated elements.) University of Oxford Ralf Hinze 12-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Permutation If magic :: [a] [a] additionally satisfies magic [x] = [x] then it permutes its input! (For simplicity, we only consider inputs with no repeated elements.) Let x be the partial function that maps x to x and is undefined otherwise, x a = if x a then Just x else Nothing. Definition: perm :: [a] [a] permutes its input if filter x perm = filter x for all x :: a. University of Oxford Ralf Hinze 12-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Permutation Here is the proof: filter i (magic [1.. n]) = { magic is strongly natural } magic (filter i [1.. n]) = { definition of filter } magic (if 1 i n then [i] else [ ]) = { conditionals } if 1 i n then magic [i] else magic [ ] = { magic [ ] = [ ] and assumption magic [i] = [i] } if 1 i n then [i] else [ ] = { definition of filter } filter i [1.. n] University of Oxford Ralf Hinze 13-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Reversal If magic :: [a] [a] additionally satisfies magic [x, y] = [y, x] then it reverses its input! University of Oxford Ralf Hinze 14-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Reversal If magic :: [a] [a] additionally satisfies magic [x, y] = [y, x] then it reverses its input! Let x, y be the partial function that maps x to x and y to y and is undefined otherwise. We have xs = ys x y. filter x, y xs = filter x, y ys University of Oxford Ralf Hinze 14-22

Naturality, but not as you know it Strong naturality WG2.8 #31, Aussois, October 2013 2 Reversal Here is the proof: magic [1.. n] = reverse [1.. n] { see above } i j. filter i, j (magic [1.. n]) = filter i, j (reverse [1.. n]) Assume 1 i, j n, then filter i, j (magic [1.. n]) = filter i, j (reverse [1.. n]) { definition of reverse and filter } filter i, j (magic [1.. n]) = [j, i] { magic is strongly natural } magic (filter i, j [1.. n]) = [j, i] { definition of filter } magic [i, j] = [j, i] The other cases are similar. University of Oxford Ralf Hinze 15-22

Naturality, but not as you know it Categorically speaking WG2.8 #31, Aussois, October 2013 3 Categorically speaking map is the arrow part of a functor List : Set Set. reverse is a natural transformation between List and List. What about filter? University of Oxford Ralf Hinze 16-22

Naturality, but not as you know it Categorically speaking WG2.8 #31, Aussois, October 2013 3 Categorically speaking map is the arrow part of a functor List : Set Set. reverse is a natural transformation between List and List. What about filter? filter is the arrow part of a functor Filter : Set Maybe Set. Set Maybe is the Kleisli category of the monad Maybe. (You may want to view Set Maybe as the category of partial functions.) The object part of Filter is just Filter A = [A]. reverse is a natural transformation between Filter and Filter. University of Oxford Ralf Hinze 16-22

Naturality, but not as you know it Categorically speaking WG2.8 #31, Aussois, October 2013 3 Properties of filter filter is a monoid homomorphism: filter p [ ] = [ ] filter p (xs + ys) = filter p xs + filter p ys filter preserves identity and composition: filter id = id filter (p q) = filter p filter q The arguments of filter live in the Kleisli category Set Maybe. University of Oxford Ralf Hinze 17-22

Naturality, but not as you know it Application: Sorting WG2.8 #31, Aussois, October 2013 4 Key-based sorting: correctness sort is correct: sort o is strongly natural: filter p sort o = sort o filter (id p) sort o produces a permutation of the input values: sort o [(k, v)] = [v] values are output in non-decreasing order of their keys: sort o [(a, i), (b, j)] = [i, j] leq o a b leq :: Order k (k k B) interprets an order representation. University of Oxford Ralf Hinze 18-22

Naturality, but not as you know it Application: Sorting WG2.8 #31, Aussois, October 2013 4 Key-based sorting: correctness Relate sort and discr: discr commutes with strong natural transformations: if perm is strongly natural, filter p perm = perm filter (id p), then map perm discr o = discr o perm map swap : [K (A V)] [[V]] This is straightforward then: concat discr o = sort o University of Oxford Ralf Hinze 19-22

Naturality, but not as you know it Epilogue WG2.8 #31, Aussois, October 2013 5 Summary Strong naturality: an amusing twist on naturality. Key-based sorting: strong naturality, coupled with preserving singletons and correct sorting of two-element lists, corresponds to Henglein s consistent permutativity, which characterizes stable sorting functions. For the details see: Henglein, F., Hinze, R.: Sorting and Searching by Distribution: From Generic Discrimination to Generic Tries. In Shan, C., ed.: Proc. 11th Asian Symposium on Programming Languages and Systems (APLAS), (December 2013). University of Oxford Ralf Hinze 20-22

Naturality, but not as you know it Appendix WG2.8 #31, Aussois, October 2013 6 Generic key-based sorting sort o takes a list of key-value pairs and returns the values in non-decreasing order of their associated keys. sort :: Order k [k v] [v] sort o [ ] = [ ] sort (OUnit) rel = map val rel sort (OSum o 1 o 2 ) rel = sort o 1 (filter froml rel) + sort o 2 (filter fromr rel) sort (OProd o 1 o 2 ) rel = sort o 1 (sort o 2 (map curryr rel)) LSD strategy: curryr :: (k 1 k 2 ) v k 2 (k 1 v) curryr ((k 1, k 2 ), v) = (k 2, (k 1, v)) University of Oxford Ralf Hinze 21-22

Naturality, but not as you know it Appendix WG2.8 #31, Aussois, October 2013 6 Generic key-based discrimination discr o returns a list of non-empty lists of values, where the inner lists group values whose keys are equivalent. discr :: Order k [k v] [[v]] discr o [ ] = [ ] discr o [(k, v)] = [[v]] discr OUnit rel = [map val rel] discr (OSum o 1 o 2 ) rel = discr o 1 (filter froml rel) + discr o 2 (filter fromr rel) discr (OProd o 1 o 2 ) rel = concat (map (discr o 2 ) (discr o 1 (map curryl rel))) MSD strategy: curryl :: (k 1 k 2 ) v k 1 (k 2 v) curryl ((k 1, k 2 ), v) = (k 1, (k 2, v)) University of Oxford Ralf Hinze 22-22