{-# 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
  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 { runNullExpanderT :: m a }

instance Monad m => Monad (NullExpanderT m) where
  return = NullExpander . return
  (NullExpander ne) >>= f = NullExpander $ do ne' <- ne; runNullExpanderT (f ne')

instance MonadTrans NullExpanderT where
  lift = NullExpander

instance Functor m => Functor (NullExpanderT m) where
  fmap f (NullExpander ne) = NullExpander $ fmap f ne

instance (Functor m, Monad m) => Applicative (NullExpanderT m) where
  (<*>) = ap
  pure = return

instance MonadIO m => MonadIO (NullExpanderT m) where
  liftIO = lift . liftIO

instance Monad m => ChExpand (NullExpanderT m) where
  expand = return

withExpansion :: Monad m => NullExpanderT m a -> m a
withExpansion = runNullExpanderT