From a9252b129a167623d9e3ff08cc87256c789126c5 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Sun, 23 Feb 2020 22:24:59 +0100 Subject: [PATCH] Start a Box module to describe inclusion relations between different types and get a MonadState action on the top type for any modification down there --- Hufflepdf.cabal | 1 + src/PDF/Box.hs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/PDF/Box.hs diff --git a/Hufflepdf.cabal b/Hufflepdf.cabal index 710156b..c6e2477 100644 --- a/Hufflepdf.cabal +++ b/Hufflepdf.cabal @@ -17,6 +17,7 @@ cabal-version: >=1.10 library exposed-modules: PDF + , PDF.Box , PDF.CMap , PDF.Content , PDF.EOL diff --git a/src/PDF/Box.hs b/src/PDF/Box.hs new file mode 100644 index 0000000..f88c2e1 --- /dev/null +++ b/src/PDF/Box.hs @@ -0,0 +1,23 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE FunctionalDependencies #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE UndecidableInstances #-} +module PDF.Box ( + Box(..) + , modifyAt + ) where + +import Control.Monad.State (MonadState(..)) + +class Box m i a b | m a i -> b where + r :: i -> a -> m b + w :: i -> a -> b -> m 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 + +instance (Box m i a b, Box m j b c) => Box m (i, j) a c