Implement transitivity instance, extract a part of modifyAt as a convenient 'edit' function useful elsewhere and present a right-infix version of (,) to allow writing the nested tuple indexes more conveniently

This commit is contained in:
Tissevert 2020-02-24 17:27:37 +01:00
parent a9252b129a
commit e607f9cd37
1 changed files with 14 additions and 5 deletions

View File

@ -5,19 +5,28 @@
{-# LANGUAGE UndecidableInstances #-}
module PDF.Box (
Box(..)
, (.@)
, edit
, modifyAt
) where
import Control.Monad.State (MonadState(..))
class Box m i a b | m a i -> b where
class Monad m => Box m i a b | m a i -> b where
r :: i -> a -> m b
w :: i -> a -> b -> m a
edit :: Box m i a b => i -> (b -> m b) -> a -> m a
edit i f a =
r i a >>= f >>= w i a
modifyAt :: (MonadState a m, Box m i a b) => i -> (b -> m b) -> m ()
modifyAt i f = do
a <- get
r i a >>= f >>= w i a >>= put
modifyAt i f = get >>= edit i f >>= put
instance (Box m i a b, Box m j b c) => Box m (i, j) a c
instance (Box m i a b, Box m j b c) => Box m (i, j) a c where
r (i, j) a = r i a >>= r j
w (i, j) a c = r i a >>= flip (w j) c >>= w i a
infixr 6 .@
(.@) :: a -> b -> (a, b)
(.@) = (,)