the virtue of vicious circles
play

The Virtue of Vicious Circles Baltasar Trancn y Widemann TU Ilmenau - PowerPoint PPT Presentation

Introduction Technique Applications Conclusion The Virtue of Vicious Circles Baltasar Trancn y Widemann TU Ilmenau 2016-10-25 Trancn y Widemann The Virtue of Vicious Circles 1 / 34 Introduction Technique Applications Conclusion


  1. Introduction Technique Applications Conclusion Pointers Cycles Details Pointer Coalgebra Memory states are coalgebras Pointers are pointed coalgebras Deep equality is bisimilarity Hierarchy of models initial finite noncircular data – too small rational finite circular data – about right final infinite data – too large Trancón y Widemann The Virtue of Vicious Circles 12 / 34

  2. Introduction Technique Applications Conclusion Pointers Cycles Details Iteration & Coiteration in a Nutshell Consider simpler functor P ( X ) = X + 1 Iteration uses initial model N , [ succ , zero ] Coiteration uses final model N ∪ { ∞ } , pred Trancón y Widemann The Virtue of Vicious Circles 13 / 34

  3. Introduction Technique Applications Conclusion Pointers Cycles Details Iteration & Coiteration in a Nutshell Consider simpler functor P ( X ) = X + 1 Iteration uses initial model N , [ succ , zero ] Coiteration uses final model N ∪ { ∞ } , pred Trancón y Widemann The Virtue of Vicious Circles 13 / 34

  4. Introduction Technique Applications Conclusion Pointers Cycles Details Iteration & Coiteration in a Nutshell Consider simpler functor P ( X ) = X + 1 Iteration uses initial model N , [ succ , zero ] Coiteration uses final model N ∪ { ∞ } , pred Trancón y Widemann The Virtue of Vicious Circles 13 / 34

  5. Introduction Technique Applications Conclusion Pointers Cycles Details Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 13 / 34

  6. Introduction Technique Applications Conclusion Pointers Cycles Details Motto Those who cannot remember the past are condemned to repeat it. (Ruiz de Santayana 1906) Trancón y Widemann The Virtue of Vicious Circles 14 / 34

  7. Introduction Technique Applications Conclusion Pointers Cycles Details Call Stack Standard implementation of recursive functions Incarnation = code + (top) stack frame Cycle = identical stacked frames Suffices for cycle detection Handling? Trancón y Widemann The Virtue of Vicious Circles 15 / 34

  8. Introduction Technique Applications Conclusion Pointers Cycles Details Call Stack Standard implementation of recursive functions Incarnation = code + (top) stack frame Cycle = identical stacked frames Suffices for cycle detection Handling? Trancón y Widemann The Virtue of Vicious Circles 15 / 34

  9. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  10. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  11. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  12. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  13. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  14. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 6 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  15. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 4 6 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  16. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 3 4 6 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  17. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations I 3 5 1 2 2 3 4 6 Trancón y Widemann The Virtue of Vicious Circles 16 / 34

  18. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  19. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  20. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  21. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 3 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  22. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 3 4 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  23. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 3 4 6 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  24. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 3 4 6 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  25. Introduction Technique Applications Conclusion Pointers Cycles Details Order of Operations II 1 2 3 5 2 3 4 6 Destination-passing style (Minamide 1998) Tail recursion modulo cons(tructor) (Warren 1980) can be used to eliminate tail calls, or alternatively to handle duckling loops! Lazy evaluation similar; consumer- not producer-driven Trancón y Widemann The Virtue of Vicious Circles 17 / 34

  26. Introduction Technique Applications Conclusion Pointers Cycles Details Handler #1 Retain stack frames Fix output before recursive call Cycle detected = ⇒ copy outputs Generic algorithm (calling convention) applied to recursive function definition implements coiteration of body as coalgebra Proof by bisimulation construction & graph coloring Trancón y Widemann The Virtue of Vicious Circles 18 / 34

  27. Introduction Technique Applications Conclusion Pointers Cycles Details Handler #1 Retain stack frames Fix output before recursive call Cycle detected = ⇒ copy outputs Generic algorithm (calling convention) applied to recursive function definition implements coiteration of body as coalgebra Proof by bisimulation construction & graph coloring Trancón y Widemann The Virtue of Vicious Circles 18 / 34

  28. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  29. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  30. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 2 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  31. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 2 3 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  32. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 2 3 4 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  33. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 2 3 4 6 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  34. Introduction Technique Applications Conclusion Pointers Cycles Details Illustration 1 2 3 YOINK ! 5 2 3 4 YOINK ! 6 Trancón y Widemann The Virtue of Vicious Circles 19 / 34

  35. Introduction Technique Applications Conclusion Pointers Cycles Details Handler #2 Boolean function encoded as positive Hilbert calculus Semantics depend on data no cycles unique fixpoint (induction) cycles complete lattice of fixpoints (Tarski) Cycle detected = ⇒ abort with constant value Generic algorithm constant false/true select least/greatest fixpoint, resp. change of strategy safe at stratification boundaries Possibly for other (finite) base lattices? Trancón y Widemann The Virtue of Vicious Circles 20 / 34

  36. Introduction Technique Applications Conclusion Pointers Cycles Details Handler #2 Boolean function encoded as positive Hilbert calculus Semantics depend on data no cycles unique fixpoint (induction) cycles complete lattice of fixpoints (Tarski) Cycle detected = ⇒ abort with constant value Generic algorithm constant false/true select least/greatest fixpoint, resp. change of strategy safe at stratification boundaries Possibly for other (finite) base lattices? Trancón y Widemann The Virtue of Vicious Circles 20 / 34

  37. Introduction Technique Applications Conclusion Pointers Cycles Details More Handlers? Similar approach by Kozen’s group (Jeannin, Kozen, and Silva 2013) Pluggable cycle handlers fixpoints numerical solvers . . . Trancón y Widemann The Virtue of Vicious Circles 21 / 34

  38. Introduction Technique Applications Conclusion Pointers Cycles Details Cycle Handling vs. Laziness Handler #1 for free in lazy evaluation but Final semantics conflate cycles & true infinity Handler #2 impossible in lazy evaluation Combination yields many interesting algorithms General(?) recipe: take ordinary recursive algorithm 1 add cycle handler 2 obtain cycle-aware algorithm – or fail 3 Trancón y Widemann The Virtue of Vicious Circles 22 / 34

  39. Introduction Technique Applications Conclusion Pointers Cycles Details Cycle Handling vs. Laziness Handler #1 for free in lazy evaluation but Final semantics conflate cycles & true infinity Handler #2 impossible in lazy evaluation Combination yields many interesting algorithms General(?) recipe: take ordinary recursive algorithm 1 add cycle handler 2 obtain cycle-aware algorithm – or fail 3 Trancón y Widemann The Virtue of Vicious Circles 22 / 34

  40. Introduction Technique Applications Conclusion Pointers Cycles Details Cycle Handling vs. Laziness Handler #1 for free in lazy evaluation but Final semantics conflate cycles & true infinity Handler #2 impossible in lazy evaluation Combination yields many interesting algorithms General(?) recipe: take ordinary recursive algorithm 1 add cycle handler 2 obtain cycle-aware algorithm – or fail 3 Trancón y Widemann The Virtue of Vicious Circles 22 / 34

  41. Introduction Technique Applications Conclusion Pointers Cycles Details Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 22 / 34

  42. Introduction Technique Applications Conclusion Pointers Cycles Details Efficiency Cycle detection by stack inspection is costly Save work by invariant every cycle contains a marked pointer Trivial in pure functional programming mark the YOINK ! pointers For regular functions (homomorphisms) check only upon mark Also for reference-counting garbage collection Trancón y Widemann The Virtue of Vicious Circles 23 / 34

  43. Introduction Technique Applications Conclusion Pointers Cycles Details Efficiency Cycle detection by stack inspection is costly Save work by invariant every cycle contains a marked pointer Trivial in pure functional programming mark the YOINK ! pointers For regular functions (homomorphisms) check only upon mark Also for reference-counting garbage collection Trancón y Widemann The Virtue of Vicious Circles 23 / 34

  44. Introduction Technique Applications Conclusion Pointers Cycles Details Efficiency Cycle detection by stack inspection is costly Save work by invariant every cycle contains a marked pointer Trivial in pure functional programming mark the YOINK ! pointers For regular functions (homomorphisms) check only upon mark Also for reference-counting garbage collection Trancón y Widemann The Virtue of Vicious Circles 23 / 34

  45. Introduction Technique Applications Conclusion Pointers Cycles Details Implementation Requires read/write access to call stack not supported by usual backends (C, JVM, .NET, . . . ) Possible solution: stackless transform add code to load/unload stack frames to heap objects portable implementation of continuations Trancón y Widemann The Virtue of Vicious Circles 24 / 34

  46. Introduction Technique Applications Conclusion Pointers Cycles Details Implementation Requires read/write access to call stack not supported by usual backends (C, JVM, .NET, . . . ) Possible solution: stackless transform add code to load/unload stack frames to heap objects portable implementation of continuations Trancón y Widemann The Virtue of Vicious Circles 24 / 34

  47. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 24 / 34

  48. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 24 / 34

  49. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Addition Rational numbers as sequences of digits Addition is not naturally coiterative depends on carry from the infinite right Consider half-addition instead sum/carry digitwise (cf. map) shift & repeat Converges each digit can overflow at most once Trancón y Widemann The Virtue of Vicious Circles 25 / 34

  50. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Addition Rational numbers as sequences of digits Addition is not naturally coiterative depends on carry from the infinite right Consider half-addition instead sum/carry digitwise (cf. map) shift & repeat Converges each digit can overflow at most once Trancón y Widemann The Virtue of Vicious Circles 25 / 34

  51. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Addition Rational numbers as sequences of digits Addition is not naturally coiterative depends on carry from the infinite right Consider half-addition instead sum/carry digitwise (cf. map) shift & repeat Converges each digit can overflow at most once Trancón y Widemann The Virtue of Vicious Circles 25 / 34

  52. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Division Long division compute most significant digit by trial subtraction repeat with remainder Reverse loop structure inner iteration outer coiteration Problem: cycle modulo bisimilarity Trancón y Widemann The Virtue of Vicious Circles 26 / 34

  53. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Division Long division compute most significant digit by trial subtraction repeat with remainder Reverse loop structure inner iteration outer coiteration Problem: cycle modulo bisimilarity Trancón y Widemann The Virtue of Vicious Circles 26 / 34

  54. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Division Long division compute most significant digit by trial subtraction repeat with remainder Reverse loop structure inner iteration outer coiteration Problem: cycle modulo bisimilarity Trancón y Widemann The Virtue of Vicious Circles 26 / 34

  55. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 26 / 34

  56. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Usual suspects on lists Usual list functions generalize by adding handler #1 append insert/delete (periodically) Trancón y Widemann The Virtue of Vicious Circles 27 / 34

  57. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Filter Filter is not naturally coinductive can delete infinitely many elements straight away Two-phase approach: mark & sweep cross out unwanted elements 1 eight all crossed out or iteratively filterable 2 Trancón y Widemann The Virtue of Vicious Circles 28 / 34

  58. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Filter Filter is not naturally coinductive can delete infinitely many elements straight away Two-phase approach: mark & sweep cross out unwanted elements 1 eight all crossed out or iteratively filterable 2 Trancón y Widemann The Virtue of Vicious Circles 28 / 34

  59. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  60. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) I G O A L W A Y S (O N) � G A A I G O A L W A Y S (O N) � I I G O A L W A Y S (O N) � O L W Y S (O N) Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  61. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) I G O A L W A Y S (O N) � G A A G A A � A A G A A � G G A A � I G O A L W A Y S (O N) � I I G O A L W A Y S (O N) � O L W Y S (O N) Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  62. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) I G O A L W A Y S (O N) � G A A G A A � A A G A A � G G A A � I G O A L W A Y S (O N) � I I G O A L W A Y S (O N) � O L W Y S (O N) O L W Y S (O N) � L (N) O L W Y S (O N) � O (O) O L W Y S (O N) � W Y S Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  63. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) I G O A L W A Y S (O N) � G A A G A A � A A G A A � G G A A � I G O A L W A Y S (O N) � I I G O A L W A Y S (O N) � O L W Y S (O N) O L W Y S (O N) � L (N) O L W Y S (O N) � O (O) O L W Y S (O N) � W Y S W Y S � S W Y S � W W Y S � Y Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  64. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Quicksort I G O A L W A Y S (O N) I G O A L W A Y S (O N) � G A A G A A � A A G A A � G G A A � I G O A L W A Y S (O N) � I I G O A L W A Y S (O N) � O L W Y S (O N) O L W Y S (O N) � L (N) O L W Y S (O N) � O (O) O L W Y S (O N) � W Y S W Y S � S W Y S � W W Y S � Y A A G I L (N) Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  65. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Introduction 1 Technique 2 Pointer Coalgebra Cycle Detection & Handling Technical Details Applications 3 Rational Arithmetics Circular Lists Structural Polynomial Subtyping Conclusion 4 Trancón y Widemann The Virtue of Vicious Circles 29 / 34

  66. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Polynomial Datatypes Sums, Products & Recursion Structural subtyping sum with less variants product with more components transitively closed lifted to functions Dynamic conversion by access operation mapping table format recursive types = ⇒ circular table references Type conversion tables form category cycle-aware algorithms for subtype check & composition constant-time conversion of structured values Trancón y Widemann The Virtue of Vicious Circles 30 / 34

  67. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Polynomial Datatypes Sums, Products & Recursion Structural subtyping sum with less variants product with more components transitively closed lifted to functions Dynamic conversion by access operation mapping table format recursive types = ⇒ circular table references Type conversion tables form category cycle-aware algorithms for subtype check & composition constant-time conversion of structured values Trancón y Widemann The Virtue of Vicious Circles 30 / 34

  68. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Polynomial Datatypes Sums, Products & Recursion Structural subtyping sum with less variants product with more components transitively closed lifted to functions Dynamic conversion by access operation mapping table format recursive types = ⇒ circular table references Type conversion tables form category cycle-aware algorithms for subtype check & composition constant-time conversion of structured values Trancón y Widemann The Virtue of Vicious Circles 30 / 34

  69. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Example � � BinTree [ α ] ::= Branch 2 BinTree [ α ] , BinTree [ α ] 1 | Leaf ( α ) 2 � � 23Tree [ β ] ::= Branch 3 23Tree [ β ] , 23Tree [ β ] , 23Tree [ β ] 1 � � | Branch 2 23Tree [ β ] , 23Tree [ β ] 2 | Leaf ( β ) 3 � � c : ( α < : β ) → BinTree [ α ] < : 23Tree [ β ] � � 2 � � 1 → c ( d ) , c ( d ) c ( d ) = 3 ( d ) 2 → Trancón y Widemann The Virtue of Vicious Circles 31 / 34

  70. Introduction Technique Applications Conclusion Arithmetics Lists Subtyping Example � � BinTree [ α ] ::= Branch 2 BinTree [ α ] , BinTree [ α ] 1 | Leaf ( α ) 2 � � 23Tree [ β ] ::= Branch 3 23Tree [ β ] , 23Tree [ β ] , 23Tree [ β ] 1 � � | Branch 2 23Tree [ β ] , 23Tree [ β ] 2 | Leaf ( β ) 3 � � c : ( α < : β ) → BinTree [ α ] < : 23Tree [ β ] � � 2 � � 1 → c ( d ) , c ( d ) c ( d ) = 3 ( d ) 2 → Trancón y Widemann The Virtue of Vicious Circles 31 / 34

Recommend


More recommend