module Stg.Util (
show',
Validate(..),
commaSep,
bulletList,
pluralS,
) where
import Data.Bifunctor
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Text.PrettyPrint.ANSI.Leijen hiding ((<>))
show' :: Show a => a -> Text
show' = T.pack . show
data Validate err a = Failure err | Success a
instance Functor (Validate a) where
fmap _ (Failure err) = Failure err
fmap f (Success x) = Success (f x)
instance Bifunctor Validate where
first _ (Success x) = Success x
first f (Failure err) = Failure (f err)
second = fmap
bimap f _ (Failure l) = Failure (f l)
bimap _ g (Success r) = Success (g r)
instance Monoid a => Applicative (Validate a) where
pure = Success
Success f <*> Success x = Success (f x)
Success _ <*> Failure x = Failure x
Failure x <*> Failure y = Failure (x <> y)
Failure x <*> Success _ = Failure x
commaSep :: [Doc] -> Doc
commaSep = encloseSep mempty mempty (comma <> space)
bulletList :: [Doc] -> Doc
bulletList = align . vsep . map ((" - " <>) . align)
pluralS :: [a] -> Doc
pluralS [_] = ""
pluralS _ = "s"