mini 1.0.0.0 → 1.0.1.0
raw patch · 8 files changed
+168/−123 lines, 8 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Mini.Transformers.EitherT: data EitherT e m a
- Mini.Transformers.ParserT: data ParserT s m a
- Mini.Transformers.ReaderT: data ReaderT r m a
- Mini.Transformers.StateT: data StateT s m a
- Mini.Transformers.WriterT: data WriterT w m a
+ Mini.Transformers.EitherT: EitherT :: m (Either e a) -> EitherT e m a
+ Mini.Transformers.EitherT: newtype EitherT e m a
+ Mini.Transformers.MaybeT: MaybeT :: m (Maybe a) -> MaybeT m a
+ Mini.Transformers.MaybeT: anticipate :: Monad m => MaybeT m a -> MaybeT m (Maybe a)
+ Mini.Transformers.MaybeT: instance GHC.Base.Monad m => GHC.Base.Alternative (Mini.Transformers.MaybeT.MaybeT m)
+ Mini.Transformers.MaybeT: instance GHC.Base.Monad m => GHC.Base.Applicative (Mini.Transformers.MaybeT.MaybeT m)
+ Mini.Transformers.MaybeT: instance GHC.Base.Monad m => GHC.Base.Functor (Mini.Transformers.MaybeT.MaybeT m)
+ Mini.Transformers.MaybeT: instance GHC.Base.Monad m => GHC.Base.Monad (Mini.Transformers.MaybeT.MaybeT m)
+ Mini.Transformers.MaybeT: instance Mini.Transformers.Class.MonadTrans Mini.Transformers.MaybeT.MaybeT
+ Mini.Transformers.MaybeT: newtype MaybeT m a
+ Mini.Transformers.MaybeT: nothing :: Monad m => MaybeT m a
+ Mini.Transformers.MaybeT: runMaybeT :: MaybeT m a -> m (Maybe a)
+ Mini.Transformers.ParserT: ParserT :: ([s] -> m (Either [ParseError s] (a, [s]))) -> ParserT s m a
+ Mini.Transformers.ParserT: instance (GHC.Base.Monad m, GHC.Base.Monoid a) => GHC.Base.Monoid (Mini.Transformers.ParserT.ParserT s m a)
+ Mini.Transformers.ParserT: instance (GHC.Base.Monad m, GHC.Base.Semigroup a) => GHC.Base.Semigroup (Mini.Transformers.ParserT.ParserT s m a)
+ Mini.Transformers.ParserT: newtype ParserT s m a
+ Mini.Transformers.ReaderT: ReaderT :: (r -> m a) -> ReaderT r m a
+ Mini.Transformers.ReaderT: newtype ReaderT r m a
+ Mini.Transformers.StateT: StateT :: (s -> m (a, s)) -> StateT s m a
+ Mini.Transformers.StateT: newtype StateT s m a
+ Mini.Transformers.WriterT: WriterT :: m (a, w) -> WriterT w m a
+ Mini.Transformers.WriterT: newtype WriterT w m a
Files
- CHANGELOG.md +7/−0
- Mini/Transformers/EitherT.hs +13/−24
- Mini/Transformers/MaybeT.hs +80/−0
- Mini/Transformers/ParserT.hs +27/−38
- Mini/Transformers/ReaderT.hs +12/−15
- Mini/Transformers/StateT.hs +15/−33
- Mini/Transformers/WriterT.hs +12/−12
- mini.cabal +2/−1
CHANGELOG.md view
@@ -1,3 +1,10 @@+1.0.1.0 [2024-03-10]+--------------------+* Export transformers newtype constructors+* Add Semigroup and Monoid instances to ParserT+* Streamline transformers documentation+* Create Mini.Transformers.MaybeT+ 1.0.0.0 [2024-03-07] -------------------- * Fix Mini.Data.Map.insert
Mini/Transformers/EitherT.hs view
@@ -1,18 +1,16 @@-{- | Extension of a monad with the 'Either' ability to interrupt a sequence of-actions and terminate with a value--}+-- | Extend a monad with the ability to terminate a computation with a value module Mini.Transformers.EitherT ( -- * Type- EitherT,+ EitherT (+ EitherT+ ), - -- * Termination- left,+ -- * Runner+ runEitherT, - -- * Anticipation+ -- * Operations+ left, anticipate,-- -- * Runners- runEitherT, ) where import Control.Applicative (@@ -36,12 +34,10 @@ - Type -} -{- | A monad with early termination type /e/, inner monad /m/, and return type-/a/--}+-- | A terminable transformer with termination /e/, inner monad /m/, return /a/ newtype EitherT e m a = EitherT { runEitherT :: m (Either e a)- -- ^ Unwrap an 'EitherT'+ -- ^ Unwrap an 'EitherT' computation } instance (Monad m) => Functor (EitherT e m) where@@ -72,20 +68,13 @@ lift = EitherT . fmap Right {-- - Termination+ - Operations -} --- | Terminate an action sequence with the given value+-- | Terminate the computation with a value left :: (Monad m) => e -> EitherT e m a left = EitherT . pure . Left -{-- - Anticipation- -}--{- | Run the given action and decide what to do depending on the return type--> anticipate foo >>= either bar baz--}+-- | Run a computation and get its result anticipate :: (Monad m) => EitherT e m a -> EitherT e m (Either e a) anticipate = lift . runEitherT . (Right <$>) >=> either (pure . Left) pure
+ Mini/Transformers/MaybeT.hs view
@@ -0,0 +1,80 @@+-- | Extend a monad with the ability to terminate a computation without a value+module Mini.Transformers.MaybeT (+ -- * Type+ MaybeT (+ MaybeT+ ),++ -- * Runner+ runMaybeT,++ -- * Operations+ nothing,+ anticipate,+) where++import Control.Applicative (+ Alternative (+ empty,+ (<|>)+ ),+ )+import Control.Monad (+ ap,+ liftM,+ (>=>),+ )+import Mini.Transformers.Class (+ MonadTrans (+ lift+ ),+ )++{-+ - Type+ -}++-- | A terminable transformer with inner monad /m/, return /a/+newtype MaybeT m a = MaybeT+ { runMaybeT :: m (Maybe a)+ -- ^ Unwrap a 'MaybeT' computation+ }++instance (Monad m) => Functor (MaybeT m) where+ fmap = liftM++instance (Monad m) => Applicative (MaybeT m) where+ pure = MaybeT . pure . Just+ (<*>) = ap++instance (Monad m) => Alternative (MaybeT m) where+ empty = MaybeT $ pure Nothing+ m <|> n =+ MaybeT $+ runMaybeT m+ >>= maybe+ (runMaybeT n)+ (pure . Just)++instance (Monad m) => Monad (MaybeT m) where+ m >>= k =+ MaybeT $+ runMaybeT m+ >>= maybe+ (pure Nothing)+ (runMaybeT . k)++instance MonadTrans MaybeT where+ lift = MaybeT . fmap Just++{-+ - Operations+ -}++-- | Terminate the computation without a value+nothing :: (Monad m) => MaybeT m a+nothing = empty++-- | Run a computation and get its result+anticipate :: (Monad m) => MaybeT m a -> MaybeT m (Maybe a)+anticipate = lift . runMaybeT . (Just <$>) >=> maybe (pure Nothing) pure
Mini/Transformers/ParserT.hs view
@@ -1,13 +1,15 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE TupleSections #-} --- | Turning strings into things+-- | Extend a monad with the ability to parse symbol sequences module Mini.Transformers.ParserT ( -- * Types- ParserT,+ ParserT (+ ParserT+ ), ParseError, - -- * Runners+ -- * Runner runParserT, -- * Parsers@@ -55,12 +57,10 @@ - Types -} -{- | A monad for parsing symbols of type /s/ with inner monad /m/ and return-type /a/--}+-- | A transformer parsing symbols /s/, inner monad /m/, return /a/ newtype ParserT s m a = ParserT { runParserT :: [s] -> m (Either [ParseError s] (a, [s]))- -- ^ Unwrap a 'ParserT' given a string of symbols+ -- ^ Unwrap a 'ParserT' computation with a sequence of symbols to parse } instance (Monad m) => Functor (ParserT s m) where@@ -94,7 +94,13 @@ instance MonadTrans (ParserT s) where lift m = ParserT $ \ss -> m <&> Right . (,ss) --- | Abstract representation of a parse error for symbols of type /s/+instance (Monad m, Semigroup a) => Semigroup (ParserT s m a) where+ m <> n = (<>) <$> m <*> n++instance (Monad m, Monoid a) => Monoid (ParserT s m a) where+ mempty = pure mempty++-- | Abstract representation of a parse error for symbols /s/ data ParseError s = EndOfInput | Unexpected s@@ -107,15 +113,10 @@ EmptyError -> "empty" {-- - Common Parsers+ - Parsers -} -{- | From a predicate to a parser for symbols satisfying the predicate--> digit = sat Data.Char.isDigit->-> spaces = Control.Applicative.many $ sat Data.Char.isSpace--}+-- | Parse symbols satisfying a predicate sat :: (Applicative m) => (s -> Bool) -> ParserT s m s sat p = ParserT $ \case@@ -126,23 +127,23 @@ (pure $ Right (s, ss)) $ p s --- | A parser for any symbol+-- | Parse any symbol item :: (Applicative m) => ParserT s m s item = sat $ const True --- | A parser for the given symbol+-- | Parse a symbol symbol :: (Applicative m, Eq s) => s -> ParserT s m s symbol = sat . (==) --- | A parser for the given string of symbols+-- | Parse a sequence of symbols string :: (Monad m, Traversable t, Eq s) => t s -> ParserT s m (t s) string = traverse symbol --- | A parser for any of the given symbols+-- | Parse symbols included in a collection oneOf :: (Applicative m, Foldable t, Eq s) => t s -> ParserT s m s oneOf = sat . flip elem --- | A parser for any symbol excluding the given symbols+-- | Parse symbols excluded from a collection noneOf :: (Applicative m, Foldable t, Eq s) => t s -> ParserT s m s noneOf = sat . flip notElem @@ -150,33 +151,23 @@ - Combinators -} -{- | Turn a parser and another parser into a parser for zero or more of the-former separated by the latter--}+-- | Parse zero or more @p@ separated by @q@ via @p \`sepBy\` q@ sepBy :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] sepBy p = option [] . sepBy1 p -{- | Turn a parser and another parser into a parser for one or more of the-former separated by the latter--}+-- | Parse one or more @p@ separated by @q@ via @p \`sepBy1\` q@ sepBy1 :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] sepBy1 p sep = (:) <$> p <*> many (sep *> p) -{- | Turn a parser and another parser into a parser for zero or more of the-former separated and ended by the latter--}+-- | Parse zero or more @p@ separated and ended by @q@ via @p \`endBy\` q@ endBy :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] endBy p = option [] . endBy1 p -{- | Turn a parser and another parser into a parser for one or more of the-former separated and ended by the latter--}+-- | Parse one or more @p@ separated and ended by @q@ via @p \`endBy1\` q@ endBy1 :: (Monad m, Eq s) => ParserT s m a -> ParserT s m b -> ParserT s m [a] endBy1 p sep = sepBy1 p sep <* sep -{- | Turn a first, second and third parser into a parser for the third enclosed-between the first and the second, returning the result of the third--}+-- | Parse @p@ enclosed by @a@ and @b@ via @between a b p@ between :: (Monad m) => ParserT s m open@@ -185,8 +176,6 @@ -> ParserT s m a between open close p = open *> p <* close -{- | From a default value and a parser to the parser returning the default value-in case of failure--}+-- | Parse @p@ returning @a@ in case of failure via @option a p@ option :: (Monad m, Eq s) => a -> ParserT s m a -> ParserT s m a option a p = p <|> pure a
Mini/Transformers/ReaderT.hs view
@@ -1,13 +1,15 @@--- | Extension of a monad with a read-only environment+-- | Extend a monad with a read-only environment module Mini.Transformers.ReaderT ( -- * Type- ReaderT,-- -- * Reading- ask,+ ReaderT (+ ReaderT+ ), - -- * Runners+ -- * Runner runReaderT,++ -- * Operations+ ask, ) where import Control.Applicative (@@ -30,10 +32,10 @@ - Type -} --- | A monad with read-only type /r/, inner monad /m/, and return type /a/+-- | A transformer with read-only /r/, inner monad /m/, return /a/ newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a- -- ^ Unwrap a 'ReaderT' given a read-only value+ -- ^ Unwrap a 'ReaderT' computation with an initial read-only value } instance (Monad m) => Functor (ReaderT r m) where@@ -54,14 +56,9 @@ lift = ReaderT . const {-- - Reading+ - Operations -} -{- | Fetch the read-only value--> foo = do-> r <- ask-> bar r--}+-- | Fetch the read-only environment ask :: (Monad m) => ReaderT r m r ask = ReaderT pure
Mini/Transformers/StateT.hs view
@@ -1,21 +1,19 @@ {-# LANGUAGE TupleSections #-} --- | Extension of a monad with a modifiable environment+-- | Extend a monad with a modifiable environment module Mini.Transformers.StateT ( -- * Type- StateT,+ StateT (+ StateT+ ), - -- * Reading- get,+ -- * Runner+ runStateT, - -- * Modifying+ -- * Operations+ get, modify,-- -- * Writing put,-- -- * Runners- runStateT, ) where import Control.Applicative (@@ -42,17 +40,17 @@ - Type -} --- | A monad with modifiable type /s/, inner monad /m/, and return type /a/+-- | A transformer with state /s/, inner monad /m/, return /a/ newtype StateT s m a = StateT { runStateT :: s -> m (a, s)- -- ^ Unwrap a 'StateT' given a starting state+ -- ^ Unwrap a 'StateT' computation with an initial state } instance (Monad m) => Functor (StateT s m) where fmap = liftM instance (Monad m) => Applicative (StateT s m) where- pure a = StateT $ \s -> pure (a, s)+ pure a = StateT $ pure . (a,) (<*>) = ap instance (Monad m, Alternative m) => Alternative (StateT s m) where@@ -66,33 +64,17 @@ lift m = StateT $ \s -> m <&> (,s) {-- - Reading+ - Operations -} -{- | Fetch the current value of the state--> foo = do-> s <- get-> bar s--}+-- | Fetch the current state get :: (Monad m) => StateT s m s get = StateT $ \s -> pure (s, s) -{-- - Modifying- -}--{- | Update the current value of the state with the given operation--> foo = modify $ \s -> bar s--}+-- | Update the current state with an operation modify :: (Monad m) => (s -> s) -> StateT s m () modify f = StateT $ pure . ((),) . f -{-- - Writing- -}---- | Set the state to the given value+-- | Overwrite the current state with a value put :: (Monad m) => s -> StateT s m () put = StateT . const . pure . ((),)
Mini/Transformers/WriterT.hs view
@@ -1,15 +1,17 @@ {-# LANGUAGE TupleSections #-} --- | Extension of a monad with a write-only environment+-- | Extend a monad with an accumulative write-only environment module Mini.Transformers.WriterT ( -- * Type- WriterT,-- -- * Writing- tell,+ WriterT (+ WriterT+ ), - -- * Runners+ -- * Runner runWriterT,++ -- * Operations+ tell, ) where import Control.Applicative (@@ -35,12 +37,10 @@ - Type -} -{- | A monad with monoidal write-only type /w/, inner monad /m/, and return type-/a/--}+-- | A transformer with monoidal write-only /w/, inner monad /m/, return /a/ newtype WriterT w m a = WriterT { runWriterT :: m (a, w)- -- ^ Unwrap a 'WriterT'+ -- ^ Unwrap a 'WriterT' computation } instance (Monad m, Monoid w) => Functor (WriterT w m) where@@ -64,9 +64,9 @@ lift m = WriterT $ m <&> (,mempty) {-- - Writing+ - Operations -} --- | Append the given value to the output+-- | Append a value to the write-only environment tell :: (Monad m) => w -> WriterT w m () tell = WriterT . pure . ((),)
mini.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: mini-version: 1.0.0.0+version: 1.0.1.0 license: MIT license-file: LICENSE copyright: (c) 2023-2024 Victor Wallsten@@ -33,6 +33,7 @@ Mini.Transformers.WriterT Mini.Transformers.StateT Mini.Transformers.ParserT+ Mini.Transformers.MaybeT build-depends: base == 4.* default-language: