module Ivory.BitData.Monad where
import Ivory.Language
import qualified MonadLib.Monads as M
import Ivory.BitData.Bits
import Ivory.BitData.BitData
newtype BitDataM d a = BitDataM { runBitDataM :: M.State d a }
deriving (Functor, Monad)
clear :: BitData d => BitDataM d ()
clear = return ()
setBit :: BitData d => BitDataField d Bit -> BitDataM d ()
setBit f = BitDataM $ M.sets_ (setBitDataBit f)
clearBit :: BitData d => BitDataField d Bit -> BitDataM d ()
clearBit f = BitDataM $ M.sets_ (clearBitDataBit f)
setField :: (BitData d, BitData b,
SafeCast (BitDataRep b) (BitDataRep d))
=> BitDataField d b -> b -> BitDataM d ()
setField f x = BitDataM $ M.sets_ (\v -> setBitDataField f v x)
runBits :: BitData d => BitDataRep d -> BitDataM d a -> (a, BitDataRep d)
runBits rep mf = (result, toRep val)
where (result, val) = M.runState (fromRep rep) (runBitDataM mf)
withBits :: BitData d => BitDataRep d -> BitDataM d () -> BitDataRep d
withBits rep mf = snd (runBits rep mf)
withBitsRef :: BitData d
=> Ref s1 (Stored (BitDataRep d))
-> BitDataM d a
-> Ivory eff a
withBitsRef ref mf = do
rep <- deref ref
let (result, rep') = runBits rep mf
store ref rep'
return result