naturality but not as you know it
play

Naturality, but not as you know it Ralf Hinze Department of - PowerPoint PPT Presentation

Naturality, but not as you know itPrologue 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


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

Recommend


More recommend