<commit_before>-- | This module defines the `MaybeT` monad transformer.

module Control.Monad.Maybe.Trans
  ( MaybeT(..), runMaybeT, mapMaybeT
  , module Control.Monad.Trans.Class
  ) where

import Prelude

import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Monad.Cont.Class (class MonadCont, callCC)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
import Control.Monad.State.Class (class MonadState, state)
import Control.Monad.Trans.Class (class MonadTrans, lift)
import Control.Monad.Writer.Class (class MonadWriter, class MonadTell, pass, listen, tell)
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)

import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import Data.Tuple (Tuple(..))

-- | The `MaybeT` monad transformer.
-- |
-- | This monad transformer extends the base monad, supporting failure and alternation via
-- | the `MonadPlus` type class.
newtype MaybeT m a = MaybeT (m (Maybe a))

-- | Run a computation in the `MaybeT` monad.
runMaybeT :: forall m a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT x) = x

-- | Change the result type of a `MaybeT` monad action.
mapMaybeT :: forall m1 m2 a b. (m1 (Maybe a) -> m2 (Maybe b)) -> MaybeT m1 a -> MaybeT m2 b
mapMaybeT f (MaybeT m) = MaybeT (f m)

derive instance newtypeMaybeT :: Newtype (MaybeT m a) _

instance functorMaybeT :: Monad m => Functor (MaybeT m) where
  map = liftA1

instance applyMaybeT :: Monad m => Apply (MaybeT m) where
  apply = ap

instance applicativeMaybeT :: Monad m => Applicative (MaybeT m) where
  pure = MaybeT <<< pure <<< Just

instance bindMaybeT :: Monad m => Bind (MaybeT m) where
  bind (MaybeT x) f = MaybeT do
    v <- x
    case v of
      Nothing -> pure Nothing
      Just y -> case f y of
        MaybeT m -> m

instance monadMaybeT :: Monad m => Monad (MaybeT m)

instance monadTransMaybeT :: MonadTrans MaybeT where<commit_msg>Relax constraints for MaybeT Functor/Apply/Applicative instances<commit_after>-- | This module defines the `MaybeT` monad transformer.

module Control.Monad.Maybe.Trans
  ( MaybeT(..), runMaybeT, mapMaybeT
  , module Control.Monad.Trans.Class
  ) where

import Prelude

import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Monad.Cont.Class (class MonadCont, callCC)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
import Control.Monad.State.Class (class MonadState, state)
import Control.Monad.Trans.Class (class MonadTrans, lift)
import Control.Monad.Writer.Class (class MonadWriter, class MonadTell, pass, listen, tell)
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)

import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import Data.Tuple (Tuple(..))

-- | The `MaybeT` monad transformer.
-- |
-- | This monad transformer extends the base monad, supporting failure and alternation via
-- | the `MonadPlus` type class.
newtype MaybeT m a = MaybeT (m (Maybe a))

-- | Run a computation in the `MaybeT` monad.
runMaybeT :: forall m a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT x) = x

-- | Change the result type of a `MaybeT` monad action.
mapMaybeT :: forall m1 m2 a b. (m1 (Maybe a) -> m2 (Maybe b)) -> MaybeT m1 a -> MaybeT m2 b
mapMaybeT f (MaybeT m) = MaybeT (f m)

derive instance newtypeMaybeT :: Newtype (MaybeT m a) _

instance functorMaybeT :: Functor m => Functor (MaybeT m) where
  map f (MaybeT ma) = MaybeT (map f <$> ma)

instance applyMaybeT :: Apply m => Apply (MaybeT m) where
  apply (MaybeT f) (MaybeT x) = MaybeT (apply <$> f <*> x)

instance applicativeMaybeT :: Applicative m => Applicative (MaybeT m) where
  pure = MaybeT <<< pure <<< Just

instance bindMaybeT :: Monad m => Bind (MaybeT m) where
  bind (MaybeT x) f = MaybeT do
    x >>= case _ of
      Nothing -> pure Nothing
      Just y -> case f y of MaybeT m -> m

instance monadMaybeT :: Monad m => Monad (MaybeT m)

instance monadTransMaybeT :: MonadTrans MaybeT where
{'fraction_non_alphanumeric': 0.11005103172842245, 'fraction_numerical': 0.002884402041269137, 'mean_word_length': 4.0651685393258425, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 32, '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': '6182300', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1473, 'n_words': 539}