Reversible effects as inverse arrows Chris Heunen Robin Kaarsgaard - - PowerPoint PPT Presentation

reversible effects as inverse arrows
SMART_READER_LITE
LIVE PREVIEW

Reversible effects as inverse arrows Chris Heunen Robin Kaarsgaard - - PowerPoint PPT Presentation

Reversible effects as inverse arrows Chris Heunen Robin Kaarsgaard Martti Karvonen 1 / 17 Outline Arrows add non-functional side-effects to functional languages Reversible languages take semantics in inverse categories Arrows


slide-1
SLIDE 1

Reversible effects as inverse arrows

Chris Heunen Robin Kaarsgaard Martti Karvonen

1 / 17

slide-2
SLIDE 2

Outline

◮ Arrows add non-functional side-effects to functional languages ◮ Reversible languages take semantics in inverse categories Arrows categories Inverse arrows inverse categories dagger arrows dagger categories ◮ Many examples of inverse arrows

2 / 17

slide-3
SLIDE 3

Reversible and invertible programming

Functional languages not stateful by definition, easing reversibility (e.g. Theseus, RFun)

3 / 17

slide-4
SLIDE 4

Monads

return : X → M X (> > =) : M X → (X → M Y ) → M Y such that: return x > > = f = fx m > > = return = m (m > > = f) > > = g = m > > = (λx.fx > > = g)

4 / 17

slide-5
SLIDE 5

Monads

return : X → M X (> > =) : M X → (X → M Y ) → M Y think: ◮ M X is (effectful) computation of type X ◮ return x is constant computation ◮ composition > > = should behave

4 / 17

slide-6
SLIDE 6

Arrows

arr : (X → Y ) → A X Y (> > >) : A X Y → A Y Z → A X Z firstX,Y,Z : A X Y → A (X ⊗ Z) (Y ⊗ Z) such that: (a > > > b) > > > c = a > > > (b > > > c) arr(g ◦ f) = arr f > > > arr g arr id > > > a = a = a > > > arr id firstX,Y,I a > > > arr ρY = arr ρX > > > a firstX,Y,Z a > > > arr(idY ⊗ f) = arr(idX ⊗ f) > > > firstX,Y,Z a (firstX,Y,Z⊗V a) > > > arr αY,Z,V = arr αX,Z,V > > > first(first a) first(arr f) = arr(f ⊗ id) first(a > > > b) = (first a) > > > (first b)

5 / 17

slide-7
SLIDE 7

Arrows

arr : (X → Y ) → A X Y (> > >) : A X Y → A Y Z → A X Z firstX,Y,Z : A X Y → A (X ⊗ Z) (Y ⊗ Z) think: ◮ A X Y is type of effectful computations from X to Y ◮ arr makes pure computation effectful ◮ composition > > > behaves properly ◮ first lets effectful computations interact with environment A X Y Z Z first A

5 / 17

slide-8
SLIDE 8

Dagger and inverse arrows

inv : A X Y → A Y X such that: inv(inv a) = a inv a > > > inv b = inv(b > > > a) arr(f†) = inv(arr f) inv(first a) = first(inv a) (a > > > inv a) > > > a = a (a > > > inv a) > > > (b > > > inv b) = (b > > > inv b) > > > (a > > > inv a)

6 / 17

slide-9
SLIDE 9

Dagger and inverse arrows

inv : A X Y → A Y X think: ◮ inv turns effectful computations around ◮ cooperates with pure computations and environments ◮ inv a ‘undoes’ a

6 / 17

slide-10
SLIDE 10

Example: reversible state

type State S X Y = X → (S ⊸ (X ⊗ S)) type RState S X Y = X ⊗ S ↔ Y ⊗ S instance Arrow (RState S) where arr f (x, s) = (f x, s) (a > > > b)(x, s) = b(a(x, s)) first a ((x, z), s) = let (x ′, s′) = a(x, s) in ((x ′, z), s′) instance InverseArrow (RStateS) where inv a (y, s) = a†(y, s)

7 / 17

slide-11
SLIDE 11

Example: reversible state

type State S X Y = X → (S ⊸ (X ⊗ S)) type RState S X Y = X ⊗ S ↔ Y ⊗ S instance Arrow (RState S) where arr f (x, s) = (f x, s) (a > > > b)(x, s) = b(a(x, s)) first a ((x, z), s) = let (x ′, s′) = a(x, s) in ((x ′, z), s′) instance InverseArrow (RStateS) where inv a (y, s) = a†(y, s) get : RState S X (X ⊗ S) get (x, s) = ((x, s), s) update : (S ↔ S) → RState S X X update f (x, s) = (x, f s)

7 / 17

slide-12
SLIDE 12

Example: rewriter

class Group G where gunit : G gmul : G → (G ↔ G) ginv : G ↔ G type Rewriter G X Y = X ⊗ G ↔ Y ⊗ G

8 / 17

slide-13
SLIDE 13

Example: rewriter

class Group G where gunit : G gmul : G → (G ↔ G) ginv : G ↔ G type Rewriter G X Y = X ⊗ G ↔ Y ⊗ G rewrite : G → RewriterG X X rewrite a (x, b) = (x, gmul a b)

8 / 17

slide-14
SLIDE 14

Example: vector transformations

type Vector X Y = [X ] ↔ [Y ] instance Arrow (Vector) where arr f xs = map f xs (a > > > b) xs = b (a xs) first a ps = let (xs, zs) = zip† ps in zip (a xs, zs) instance InverseArrow (Vector) where inv a ys = a† ys map : (a ↔ b) → ([a] ↔ [b]) map f [] = [] map f (x::xs) = (f x)::(map f xs) zip : ([a], [b]) ↔ [(a, b)] zip([], []) = [] zip(x::xs, y::ys) = (x, y)::(zip(xs, ys))

9 / 17

slide-15
SLIDE 15

Example: serialization

serialize : X ↔ Serialized X type Serializer X Y = X ↔ Serialized Y instance Arrow (Serializer) where arr f x = serialize (f x) (a > > > b) x = b (serialize†(a x)) first a (x, z) = serialize(serialize†(a x), z) instance InverseArrow (Serializer) where inv a y = serialize(a†(serialize y))

10 / 17

slide-16
SLIDE 16

Example: error handling

type Error E X Y = X ⊕ E ↔ Y ⊕ E instance WeakArrow (Error E) where arr f (InL x) = InL (f x) arr f (InR e) = InR e (a > > > b) x = b (a x) instance InverseWeakArrow (Error E) where inv a y = a† y

11 / 17

slide-17
SLIDE 17

Example: error handling

type Error E X Y = X ⊕ E ↔ Y ⊕ E instance WeakArrow (Error E) where arr f (InL x) = InL (f x) arr f (InR e) = InR e (a > > > b) x = b (a x) instance InverseWeakArrow (Error E) where inv a y = a† y raise : (X ↔ E) → (E ↔ E ⊕ E) → Error E X Y raise f p x = InR (p† (arr f x)))

11 / 17

slide-18
SLIDE 18

Example: superoperators

Quantum physical maps f : X → Y don’t just take states to states. Must respect entanglement with environment: so f ⊗ id : X ⊗ E → Y ⊗ E takes states to states. Leads to CPM construction, not a monad, but dagger arrow: A X Y = { completely positive maps X∗ ⊗ X → Y ∗ ⊗ Y } arr f = f∗ ⊗ f a > > > b = b ◦ a firstX,Y,Z a = a ⊗ idZ∗⊗Z inv a = a†

12 / 17

slide-19
SLIDE 19

Examples: many more

◮ Pure functions: program inverter ◮ Dagger Frobenius monads, restriction monads ◮ Control flow: ArrowChoice ◮ Computation in context: type Reader C X Y = X ⊗ C ↔ Y ⊗ C ◮ Information effects [James & Sabry]: irreversible computation in pure reversible setting with inverse arrow for implicit communication with heap and garbage dump ◮ Reversible IO: must be built into programming language ◮ Reversible recursion: type separating non/terminating functions

13 / 17

slide-20
SLIDE 20

Reversible categories

◮ In dagger category, X

f

→ Y has partner X

f†

← Y with f†† = f ◮ In inverse category, moreover:

◮ f ◦ f † ◦ f = f ◮ f † ◦ f ◦ g† ◦ g = g† ◦ g ◦ f † ◦ f

◮ If monoidal, also want (f ⊗ g)† = f† ⊗ g† Examples: ◮ Any groupoid ◮ Sets and relations ◮ Sets and partial injections (universal) ◮ Hilbert spaces

14 / 17

slide-21
SLIDE 21

Arrows, categorically

◮ Monoid: object M with maps M ⊗ M → M ← I satisfying laws ◮ Monad on C is monoid in endofunctor category [C, C] ◮ Profunctors Cop × C → Set are monoidal under (F ⊗ G)(X, Z) = Y F(X, Y ) × G(Y, Z) Theorem (2006): Arrow = strong monoid in [Cop × C, Set]

15 / 17

slide-22
SLIDE 22

Dagger arrows, categorically

◮ Involutive monoidal category has functor (−): C → C with f = f and coherent natural X ⊗ Y ≃ Y ⊗ X ◮ Involutive monoid is monoid with monoid map i: M → M satisfying i ◦ i = id Lemma: if C is dagger, then [Cop × C, Set] is involutive F(f, g) = F(g†, f†) αX,Y = αY,X Theorem: Dagger arrow = involutive monoid in [Cop × C, Set] Inverse arrow makes three additional diagrams commute

16 / 17

slide-23
SLIDE 23

Conclusion

◮ definition of inverse arrow ◮ supports many examples ◮ has clean categorical structure ◮ informs sound reversible programming language design ◮ (un)do-notation

17 / 17

slide-24
SLIDE 24

References

◮ “Arrows, like Monads, are Monoids”

  • C. Heunen, B. Jacobs, MFPS, 2006

◮ “Categorical semantics for Arrows”

  • B. Jacobs, C. Heunen, I. Hasuo, Journal of Functional

Programming, 2009 ◮ “Reversible monadic programming”

  • C. Heunen, M. Karvonen, MFPS, 2015

◮ “Monads on dagger categories”

  • C. Heunen, M. Karvonen, Theory and Applications of

Categories, 2016 ◮ “Join inverse categories as models of reversible recursion”

  • H. B. Axelsen, R. Kaarsgaard, FoSSaCS, 2016

◮ “Join inverse categories and reversible recursion”

  • R. Kaarsgaard, H. B. Axelsen, R. Gluck, Journal of Logical and

Algebraic Methods in Programming, 2017

1 / 6

slide-25
SLIDE 25

Involutive monoidal categories

Functor (−): C → C with f = f, coherent natural X ⊗ Y ≃ Y ⊗ X: X ⊗ (Y ⊗ Z) (X ⊗ Y ) ⊗ Z X ⊗ Z ⊗ Y Y ⊗ X ⊗ Z (Z ⊗ Y ) ⊗ X Z ⊗ (Y ⊗ X) α χ ⊗ id χ id ⊗ χ α α X ⊗ Y Y ⊗ X X ⊗ Y X ⊗ Y χ id χ id

2 / 6

slide-26
SLIDE 26

Inverse arrow laws

L: [Cop × C, Set] → [Cop × C, Set] LM(X, Y ) = M(X, X) LM(f, g) = f† ◦ (−) ◦ f For M involutive monoid: L+M(X, Y ) = {a† ◦ a ∈ M(X, X) | a ∈ M(X, Z) for some Z}

3 / 6

slide-27
SLIDE 27

Inverse arrow law 1

g† ◦ g ◦ b† ◦ b = b† ◦ b ◦ g† ◦ g for pure g L+(hom) × LM → LM (g† ◦ g, a) → g† ◦ g ◦ a L+M × L+(hom) LM × L+(hom) L+(hom) × L+M L+(hom) × LM LM σ

4 / 6

slide-28
SLIDE 28

Inverse arrow law 2

a† ◦ a ◦ b† ◦ b = b† ◦ b ◦ a† ◦ a L+M × L+M → LM (a† ◦ a, b† ◦ b) → a† ◦ a ◦ b† ◦ b L+M × L+M L+M × L+M LM σ

5 / 6

slide-29
SLIDE 29

Inverse arrow law 3

a ◦ a† ◦ a = a DM ֒ → M × M × M DM(X, Y ) = {(a, a†, a) | a ∈ M(X, Y )} M DM M id

6 / 6