1-- | A small prelude used in @zinza@ generated
2-- template modules.
3module Distribution.ZinzaPrelude (
4    Writer,
5    execWriter,
6    tell,
7    -- * Re-exports
8    forM_,
9    Generic,
10    PackageName,
11    Version,
12    prettyShow
13    ) where
14
15import Distribution.Compat.Prelude
16import Prelude ()
17
18import Control.Monad                  (forM_)
19import Distribution.Pretty            (prettyShow)
20import Distribution.Types.PackageName (PackageName)
21import Distribution.Types.Version     (Version)
22
23newtype Writer a = W { unW :: ShowS -> (ShowS, a) }
24
25instance Functor Writer where
26    fmap = liftM
27
28instance Applicative Writer where
29    pure x = W $ \ss -> (ss, x)
30    (<*>) = ap
31
32instance Monad Writer where
33    return = pure
34    m >>= k = W $ \s1 ->
35        let (s2, x) = unW m s1
36        in unW (k x) s2
37    {-# INLINE (>>=) #-}
38
39execWriter :: Writer a -> String
40execWriter w = fst (unW w id) ""
41
42tell :: String -> Writer ()
43tell s = W $ \s' -> (s' . showString s, ())
44