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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend