<commit_before>{-# LANGUAGE PolymorphicComponents
            ,MultiParamTypeClasses
            ,FunctionalDependencies
            ,FlexibleInstances
 #-}

{-| The main SAT solver monad.  Embeds `ST'.  See type `DPLLMonad'.

Most of the work done is in the form of `DPLLMonad' actions. -}
module DPLL.Monad where

import Control.Monad.Error hiding ((>=>), forM_)
import Control.Monad.ST.Strict
import Control.Monad.State.Lazy hiding ((>=>), forM_)
import Control.Monad.MonadST


instance MonadST s (DPLLMonad e st s) where
    liftST = dpllST

-- | Perform an @ST@ action in the DPLL monad.
dpllST :: ST s a -> DPLLMonad e st s a
{-# INLINE dpllST #-}
dpllST st = DPLLMonad (\k s -> st >>= \x -> k x s)

-- | @runDPLLMonad m s@ executes a `DPLLMonad' action with initial state @s@
-- until an error occurs or a result is returned.
runDPLLMonad :: (Error e) => DPLLMonad e st s a -> (st -> ST s (Either e a, st))
runDPLLMonad m = unDPLLMonad m (\x s -> return (return x, s))

execDPLLMonad :: (Error e) => DPLLMonad e st s a -> st -> ST s (Either e a)
execDPLLMonad m s = do (result, _) <- runDPLLMonad m s
                       return result

-- | @DPLLMonad e st s a@: the error type @e@, state type @st@, @ST@ thread
-- @s@ and result type @a@.
--
-- This is a monad embedding @ST@ and supporting error handling and state
-- threading.  It uses CPS to avoid checking `Left' and `Right' for every
-- `>>='; instead only checks on `catchError'. Idea adapted from
-- <http://haskell.org/haskellwiki/Performance/Monads>.
newtype DPLLMonad e st s a =
    DPLLMonad { unDPLLMonad :: forall r. (a -> (st -> ST s (Either e r, st)))
                              -> (st -> ST s (Either e r, st)) }

instance Monad (DPLLMonad e st s) where
    return x = DPLLMonad ($ x)
    m >>= f  = DPLLMonad (\k -> unDPLLMonad m (\a -> unDPLLMonad (f a) k))

instance MonadState st (DPLLMonad e st s) where
    get = DPLLMonad (\k s -> k s s)
    put s' = DPLLMonad (\k _ -> k () s')

instance (Error e) => MonadError e (DPLLMonad e st s) where
    throwError err =            -- throw away continuation
        DPLLMonad (\_ s -> return (Left err, s))
    catchError action handler = DPLLMonad
        (\k s -> do (x, s') <- runDPLLMonad action s
                    case x of
                      Left error -> unDPLLMonad (handler error) k s'
                      Right result -> k result s')
<commit_msg>renamed to the ssterrmonad<commit_after>{-# LANGUAGE PolymorphicComponents
            ,MultiParamTypeClasses
            ,FunctionalDependencies
            ,FlexibleInstances
 #-}

{-|

The main SAT solver monad.  Embeds `ST'.  See type `SSTErrMonad', which stands
for ''State ST Error Monad''.

Most of the work done is in the form of `SSTErrMonad' actions. -}
module DPLL.Monad where

import Control.Monad.Error hiding ((>=>), forM_)
import Control.Monad.ST.Strict
import Control.Monad.State.Lazy hiding ((>=>), forM_)
import Control.Monad.MonadST


instance MonadST s (SSTErrMonad e st s) where
    liftST = dpllST

-- | Perform an @ST@ action in the DPLL monad.
dpllST :: ST s a -> SSTErrMonad e st s a
{-# INLINE dpllST #-}
dpllST st = SSTErrMonad (\k s -> st >>= \x -> k x s)

-- | @runSSTErrMonad m s@ executes a `SSTErrMonad' action with initial state @s@
-- until an error occurs or a result is returned.
runSSTErrMonad :: (Error e) => SSTErrMonad e st s a -> (st -> ST s (Either e a, st))
runSSTErrMonad m = unSSTErrMonad m (\x s -> return (return x, s))

execSSTErrMonad :: (Error e) => SSTErrMonad e st s a -> st -> ST s (Either e a)
execSSTErrMonad m s = do (result, _) <- runSSTErrMonad m s
                         return result

-- | @SSTErrMonad e st s a@: the error type @e@, state type @st@, @ST@ thread
-- @s@ and result type @a@.
--
-- This is a monad embedding @ST@ and supporting error handling and state
-- threading.  It uses CPS to avoid checking `Left' and `Right' for every
-- `>>='; instead only checks on `catchError'. Idea adapted from
-- <http://haskell.org/haskellwiki/Performance/Monads>.
newtype SSTErrMonad e st s a =
    SSTErrMonad { unSSTErrMonad :: forall r. (a -> (st -> ST s (Either e r, st)))
                              -> (st -> ST s (Either e r, st)) }

instance Monad (SSTErrMonad e st s) where
    return x = SSTErrMonad ($ x)
    m >>= f  = SSTErrMonad (\k -> unSSTErrMonad m (\a -> unSSTErrMonad (f a) k))

instance MonadState st (SSTErrMonad e st s) where
    get = SSTErrMonad (\k s -> k s s)
    put s' = SSTErrMonad (\k _ -> k () s')

instance (Error e) => MonadError e (SSTErrMonad e st s) where
    throwError err =            -- throw away continuation
        SSTErrMonad (\_ s -> return (Left err, s))
    catchError action handler = SSTErrMonad
        (\k s -> do (x, s') <- runSSTErrMonad action s
                    case x of
                      Left error -> unSSTErrMonad (handler error) k s'
                      Right result -> k result s')

{'fraction_non_alphanumeric': 0.12283560806681605, 'fraction_numerical': 0.0, 'mean_word_length': 2.443197755960729, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1399871', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1684, 'n_words': 648}