module Data.String.Builder (
build
, literal
, Builder
, BuilderM
) where
import Control.Applicative
import Control.Monad
import Data.Monoid
import Data.String
data BuilderM a = BuilderM a ShowS
deriving Functor
instance Applicative BuilderM where
pure = return
(<*>) = ap
instance Monad BuilderM where
return a = BuilderM a id
BuilderM a xs >>= f = case f a of
BuilderM b ys -> BuilderM b (xs . ys)
type Builder = BuilderM ()
instance Monoid Builder where
mempty = return ()
#if !MIN_VERSION_base(4,11,0)
mappend = (>>)
#else
instance Semigroup Builder where
(<>) = (>>)
#endif
literal :: String -> Builder
literal = BuilderM () . showString
instance (a ~ ()) => IsString (BuilderM a) where
fromString s = literal s >> literal "\n"
build :: Builder -> String
build (BuilderM () s) = s ""