<commit_before>{-# LANGUAGE BangPatterns, StandaloneDeriving, UndecidableInstances #-}

-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2014 <NAME>
-- License     : BSD3
--
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : portable
--
----------------------------------------------------------------------------

module Quaazar.Utils.Scoped (
    -- * Scoped monad
    MonadScoped(..)
    -- * IO scoped monad transformer
  , IOScopedT
  , runIOScopedT
    -- * Re-exported
  , module Control.Monad.Base
  ) where

import Control.Applicative ( Applicative )
import Control.Monad.Base ( MonadBase(..) )
import Control.Monad.Journal ( MonadJournal(..) )
import Control.Monad.Trans ( MonadIO(..), MonadTrans )
import Control.Monad.Trans.Journal ( JournalT )
import Control.Monad.Trans.State ( StateT, modify, runStateT )
import Data.Monoid ( Monoid )

class (MonadBase b m) => MonadScoped b m where
  scoped :: b () -> m ()

newtype IOScopedT m a = IOScopedT { unIOScopedT :: StateT (IO ()) m a } deriving (Applicative,Functor,Monad,MonadTrans)

deriving instance (MonadBase IO m) => MonadBase IO (IOScopedT m)
deriving instance (MonadIO m) => MonadIO (IOScopedT m)
deriving instance (MonadJournal w m,Monoid w) => MonadJournal w (IOScopedT m)

instance (MonadBase IO m) => MonadScoped IO (IOScopedT m) where
  scoped a = IOScopedT $ modify (>>a)

runIOScopedT :: (MonadIO m) => IOScopedT m a -> m a
runIOScopedT scope = do
  (a,cleanup) <- runStateT (unIOScopedT scope) (return ())
  liftIO cleanup
  return a<commit_msg>We don’t need transformers to derive via GND.<commit_after>{-# LANGUAGE BangPatterns, StandaloneDeriving, UndecidableInstances #-}

-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2014 <NAME>
-- License     : BSD3
--
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : portable
--
----------------------------------------------------------------------------

module Quaazar.Utils.Scoped (
    -- * Scoped monad
    MonadScoped(..)
    -- * IO scoped monad transformer
  , IOScopedT
  , runIOScopedT
    -- * Re-exported
  , module Control.Monad.Base
  ) where

import Control.Applicative ( Applicative )
import Control.Monad.Base ( MonadBase(..) )
import Control.Monad.Journal ( MonadJournal(..) )
import Control.Monad.Trans ( MonadIO(..), MonadTrans )
import Control.Monad.Trans.State ( StateT, modify, runStateT )
import Data.Monoid ( Monoid )

class (MonadBase b m) => MonadScoped b m where
  scoped :: b () -> m ()

newtype IOScopedT m a = IOScopedT { unIOScopedT :: StateT (IO ()) m a } deriving (Applicative,Functor,Monad,MonadTrans)

deriving instance (MonadBase IO m) => MonadBase IO (IOScopedT m)
deriving instance (MonadIO m) => MonadIO (IOScopedT m)
deriving instance (MonadJournal w m,Monoid w) => MonadJournal w (IOScopedT m)

instance (MonadBase IO m) => MonadScoped IO (IOScopedT m) where
  scoped a = IOScopedT $ modify (>>a)

runIOScopedT :: (MonadIO m) => IOScopedT m a -> m a
runIOScopedT scope = do
  (a,cleanup) <- runStateT (unIOScopedT scope) (return ())
  liftIO cleanup
  return a
{'fraction_non_alphanumeric': 0.21120150187734668, 'fraction_numerical': 0.0031289111389236545, 'mean_word_length': 2.4637053087757312, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '331332', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1073, 'n_words': 283}