module Stg.Util (
show',
Validate(..),
commaSep,
) where
import Data.Bifunctor
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Prettyprint.Doc
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 `mappend` y)
Failure x <*> Success _ = Failure x
commaSep :: [Doc ann] -> Doc ann
commaSep = encloseSep mempty mempty (comma <> space)