{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DataKinds #-}
module Ivory.Language.BitData.Monad where
import Prelude ()
import Prelude.Compat
import Data.List (intercalate)
import qualified MonadLib as M
import Ivory.Language.BitData.Bits
import Ivory.Language.BitData.BitData
import Ivory.Language.Cast
import Ivory.Language.Ref
import Ivory.Language.Area
import Ivory.Language.Monad
import Ivory.Language.Comment
newtype BitDataM d a = BitDataM
{ runBitDataM :: M.StateT d (M.WriterT [String] M.Id) a
} deriving (Functor, Monad, Applicative)
clear :: BitData d => BitDataM d ()
clear = return ()
setBit :: BitData d => BitDataField d Bit -> BitDataM d ()
setBit f = BitDataM $ do
M.put ["setBit " ++ bitDataFieldName f]
M.sets_ (setBitDataBit f)
clearBit :: BitData d => BitDataField d Bit -> BitDataM d ()
clearBit f = BitDataM $ do
M.put ["clearBit " ++ bitDataFieldName f]
M.sets_ (clearBitDataBit f)
setField :: (BitData d, BitData b,
SafeCast (BitDataRep b) (BitDataRep d))
=> BitDataField d b -> b -> BitDataM d ()
setField f x = BitDataM $ do
M.put ["setField " ++ bitDataFieldName f]
M.sets_ (\v -> setBitDataField f v x)
runBits :: BitData d => BitDataRep d -> BitDataM d a -> (a, BitDataRep d, [String])
runBits rep mf = (res, toRep val, s)
where
((res, val), s) = M.runId $ M.runWriterT $ M.runStateT (fromRep rep) (runBitDataM mf)
withBits :: BitData d => BitDataRep d -> BitDataM d () -> BitDataRep d
withBits rep mf = let (_, r, _) = runBits rep mf in r
withBitsRef :: BitData d
=> Ref s1 ('Stored (BitDataRep d))
-> BitDataM d a
-> Ivory eff a
withBitsRef ref mf = do
rep <- deref ref
let (res, rep', ss) = runBits rep mf
comment ("withBitsRef: " ++ intercalate ", " ss)
store ref rep'
return res