free theorems about monadic code
play

Free Theorems about Monadic Code Janis Voigtl ander University of - PowerPoint PPT Presentation

Free Theorems about Monadic Code Janis Voigtl ander University of Bonn EWCE11 Functional Programming and Reasoning Goodies of (pure) FP: declarative abstraction/modularity referential transparency 1 Functional Programming


  1. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do return v b ← return u c ← return v return b ( return u ) > > = ( λ a → m ) = m [ u / a ] 7

  2. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do return v b ← return u c ← return v return b ( return v ) > > m = m 7

  3. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do b ← return u c ← return v return b ( return v ) > > m = m 7

  4. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do b ← return u c ← return v return b ( return u ) > > = ( λ b → m ) = m [ u / b ] 7

  5. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do c ← return v return u ( return u ) > > = ( λ b → m ) = m [ u / b ] 7

  6. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do c ← return v return u ( return v ) > > = ( λ c → m ) = m [ v / c ] 7

  7. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do return u ( return v ) > > = ( λ c → m ) = m [ v / c ] 7

  8. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do return u Purity is propagated! 7

  9. A Slightly More Simple Example Assume m 1 , m 2 are pure. That is, m 1 = ( return u ) and m 2 = ( return v ) for some u , v . Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do return u Purity is propagated! What about other “invariants”? 7

  10. Propagating Invariants f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  11. Propagating Invariants Assume m 1 , m 2 :: State σ τ , f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  12. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  13. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  14. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  15. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  16. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  17. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  18. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s m 2 b ← m 1 c ← m 2 return b 8

  19. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 b ← m 1 c ← m 2 return b 8

  20. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s b ← m 1 c ← m 2 return b 8

  21. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s s b ← m 1 c ← m 2 return b 8

  22. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s s b ← m 1 s c ← m 2 return b 8

  23. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s s b ← m 1 s s c ← m 2 return b 8

  24. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s s b ← m 1 s s c ← m 2 s return b 8

  25. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a s s f m 1 m 2 = do m 1 s s a ← m 1 s s m 2 s s b ← m 1 s s c ← m 2 s s return b 8

  26. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 8

  27. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 State ( λ s → ( b , s )) 8

  28. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← State ( λ s → ( · · · , s )) State ( λ s → ( b , s )) 8

  29. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← State ( λ s → ( · · · , s )) State ( λ s → ( b , s )) (State ( λ s → ( · · · , s ))) > > = ( λ c → State ( λ s → ( b , s ))) = ? 8

  30. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 State ( λ s → ( b , s )) (State ( λ s → ( · · · , s ))) > > = ( λ c → State ( λ s → ( b , s ))) = ? 8

  31. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 State ( λ s → ( b , s )) 8

  32. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← State ( λ s → ( · · · , s )) State ( λ s → ( b , s )) 8

  33. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← State ( λ s → ( · · · , s )) State ( λ s → ( b , s )) (State ( λ s → ( · · · , s ))) > > = ( λ b → State ( λ s → ( b , s ))) = ? 8

  34. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > = ( λ b → State ( λ s → ( b , s ))) = ? 8

  35. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 State ( λ s → ( · · · , s )) 8

  36. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) 8

  37. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > (State ( λ s → ( · · · , s ))) = ? 8

  38. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > (State ( λ s → ( · · · , s ))) = ? 8

  39. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 State ( λ s → ( · · · , s )) 8

  40. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) 8

  41. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > = ( λ a → State ( λ s → ( · · · , s ))) = ? 8

  42. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > = ( λ a → State ( λ s → ( · · · , s ))) = ? 8

  43. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 State ( λ s → ( · · · , s )) 8

  44. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) 8

  45. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do State ( λ s → ( · · · , s )) State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > (State ( λ s → ( · · · , s ))) = ? 8

  46. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do State ( λ s → ( · · · , s )) (State ( λ s → ( · · · , s ))) > > (State ( λ s → ( · · · , s ))) = ? 8

  47. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do State ( λ s → ( · · · , s )) Yes! 8

  48. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do State ( λ s → ( · · · , s )) Yes! What about other invariants, other monads, . . . ? 8

  49. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b What about other invariants, other monads, . . . ? 8

  50. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 “induction over normal form” b ← m 1 c ← m 2 return b What about other invariants, other monads, . . . ? 8

  51. Propagating Invariants Assume m 1 , m 2 :: State σ τ , but execState m i = id . Can we show that execState ( f m 1 m 2 ) = id ? Then: f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 “induction over normal form” b ← m 1 [Prehofer ’99] c ← m 2 return b What about other invariants, other monads, . . . ? 8

  52. Consider a More Specific Type Instead of f :: Monad m ⇒ m a → m a → m a now f :: Monad m ⇒ m Int → m Int → m Int 9

  53. Consider a More Specific Type Instead of f :: Monad m ⇒ m a → m a → m a now f :: Monad m ⇒ m Int → m Int → m Int Then more possible behaviours of f are possible: f :: Monad m ⇒ m Int → m Int → m Int f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 9

  54. Consider a More Specific Type Instead of f :: Monad m ⇒ m a → m a → m a now f :: Monad m ⇒ m Int → m Int → m Int Then more possible behaviours of f are possible: f :: Monad m ⇒ m Int → m Int → m Int f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 if b > 0 then return ( a + b ) else do c ← m 2 return b 9

  55. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 10

  56. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) f :: Monad m ⇒ m a → m a → m a f m 1 m 2 = do m 1 a ← m 1 m 2 b ← m 1 c ← m 2 return b 10

  57. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 return b return b 10

  58. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 return b return b 10

  59. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 return b return b return b = h ( return b ) 10

  60. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 h ( return b ) return b 10

  61. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 h ( return b ) return b 10

  62. Reasoning via Monad Embedding Assume m 1 , m 2 :: State σ τ , but execState m i = id . An m has this property iff it is an h -image for h :: Reader σ a → State σ a h (Reader g ) = State ( λ s → ( g s , s )) Then: f :: Monad m ⇒ m a → m a → m a f ( h m ′ 1 ) ( h m ′ 2 ) = do h m ′ f m ′ 1 m ′ 2 = do m ′ 1 1 a ← h m ′ a ← m ′ 1 1 h m ′ m ′ 2 2 b ← h m ′ b ← m ′ 1 1 c ← h m ′ c ← m ′ 2 2 h ( return b ) return b ( h m ′ 2 ) > > = ( λ c → h ( return b )) = ? 10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend