{-# LANGUAGE Safe #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides generic string expansion
module Text.Chatty.Expansion where

import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class

-- | Typeclass for all string-expanding monads.
class Monad e => ChExpand e where
  -- | Expand the given string.
  expand :: String -> e String

newtype NullExpanderT m a = NullExpander { NullExpanderT m a -> m a
runNullExpanderT :: m a }

instance Monad m => Monad (NullExpanderT m) where
  return :: a -> NullExpanderT m a
return = m a -> NullExpanderT m a
forall (m :: * -> *) a. m a -> NullExpanderT m a
NullExpander (m a -> NullExpanderT m a) -> (a -> m a) -> a -> NullExpanderT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
  (NullExpander m a
ne) >>= :: NullExpanderT m a -> (a -> NullExpanderT m b) -> NullExpanderT m b
>>= a -> NullExpanderT m b
f = m b -> NullExpanderT m b
forall (m :: * -> *) a. m a -> NullExpanderT m a
NullExpander (m b -> NullExpanderT m b) -> m b -> NullExpanderT m b
forall a b. (a -> b) -> a -> b
$ do a
ne' <- m a
ne; NullExpanderT m b -> m b
forall (m :: * -> *) a. NullExpanderT m a -> m a
runNullExpanderT (a -> NullExpanderT m b
f a
ne')

instance MonadTrans NullExpanderT where
  lift :: m a -> NullExpanderT m a
lift = m a -> NullExpanderT m a
forall (m :: * -> *) a. m a -> NullExpanderT m a
NullExpander

instance Functor m => Functor (NullExpanderT m) where
  fmap :: (a -> b) -> NullExpanderT m a -> NullExpanderT m b
fmap a -> b
f (NullExpander m a
ne) = m b -> NullExpanderT m b
forall (m :: * -> *) a. m a -> NullExpanderT m a
NullExpander (m b -> NullExpanderT m b) -> m b -> NullExpanderT m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f m a
ne

instance (Functor m, Monad m) => Applicative (NullExpanderT m) where
  <*> :: NullExpanderT m (a -> b) -> NullExpanderT m a -> NullExpanderT m b
(<*>) = NullExpanderT m (a -> b) -> NullExpanderT m a -> NullExpanderT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> NullExpanderT m a
pure = a -> NullExpanderT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

instance MonadIO m => MonadIO (NullExpanderT m) where
  liftIO :: IO a -> NullExpanderT m a
liftIO = m a -> NullExpanderT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> NullExpanderT m a)
-> (IO a -> m a) -> IO a -> NullExpanderT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance Monad m => ChExpand (NullExpanderT m) where
  expand :: String -> NullExpanderT m String
expand = String -> NullExpanderT m String
forall (m :: * -> *) a. Monad m => a -> m a
return

withExpansion :: Monad m => NullExpanderT m a -> m a
withExpansion :: NullExpanderT m a -> m a
withExpansion = NullExpanderT m a -> m a
forall (m :: * -> *) a. NullExpanderT m a -> m a
runNullExpanderT