show-combinators-0.2.0.0: Combinators to write Show instances
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Show.Combinators

Description

Combinators to write Show instances.

The following type illustrates the common use cases.

data MyType a
  = C a a                   -- a regular constructor
  | a :+: a                 -- an infix constructor
  | R { f1 :: a, f2 :: a }  -- a record

infixl 4 :+:

instance Show a => Show (MyType a) where
  showsPrec = flip precShows where
    precShows (C a b) = showCon "C" @| a @| b
    precShows (c :+: d) = showInfix' ":+:" 4 c d
    precShows (R {f1 = e, f2 = f}) =
      showRecord "R" ("f1" .=. e &| "f2" .=. f)
Synopsis

Documentation

class Show a where #

Minimal complete definition

showsPrec | show

Methods

showsPrec :: Int -> a -> ShowS #

show :: a -> String #

showList :: [a] -> ShowS #

Instances

Instances details
Show ByteArray 
Instance details

Defined in Data.Array.Byte

Methods

showsPrec :: Int -> ByteArray -> ShowS #

show :: ByteArray -> String #

showList :: [ByteArray] -> ShowS #

Show Timeout 
Instance details

Defined in System.Timeout

Methods

showsPrec :: Int -> Timeout -> ShowS #

show :: Timeout -> String #

showList :: [Timeout] -> ShowS #

Show Void 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Void -> ShowS #

show :: Void -> String #

showList :: [Void] -> ShowS #

Show CallStack 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> CallStack -> ShowS #

show :: CallStack -> String #

showList :: [CallStack] -> ShowS #

Show SrcLoc 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> SrcLoc -> ShowS #

show :: SrcLoc -> String #

showList :: [SrcLoc] -> ShowS #

Show KindRep 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> KindRep -> ShowS #

show :: KindRep -> String #

showList :: [KindRep] -> ShowS #

Show Module 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Module -> ShowS #

show :: Module -> String #

showList :: [Module] -> ShowS #

Show Ordering 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Ordering -> ShowS #

show :: Ordering -> String #

showList :: [Ordering] -> ShowS #

Show TrName 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> TrName -> ShowS #

show :: TrName -> String #

showList :: [TrName] -> ShowS #

Show TyCon 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> TyCon -> ShowS #

show :: TyCon -> String #

showList :: [TyCon] -> ShowS #

Show TypeLitSort 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> TypeLitSort -> ShowS #

show :: TypeLitSort -> String #

showList :: [TypeLitSort] -> ShowS #

Show Integer 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Integer -> ShowS #

show :: Integer -> String #

showList :: [Integer] -> ShowS #

Show Natural 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Natural -> ShowS #

show :: Natural -> String #

showList :: [Natural] -> ShowS #

Show () 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> () -> ShowS #

show :: () -> String #

showList :: [()] -> ShowS #

Show Bool 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Bool -> ShowS #

show :: Bool -> String #

showList :: [Bool] -> ShowS #

Show Char 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Char -> ShowS #

show :: Char -> String #

showList :: [Char] -> ShowS #

Show Double 
Instance details

Defined in GHC.Internal.Float

Methods

showsPrec :: Int -> Double -> ShowS #

show :: Double -> String #

showList :: [Double] -> ShowS #

Show Float 
Instance details

Defined in GHC.Internal.Float

Methods

showsPrec :: Int -> Float -> ShowS #

show :: Float -> String #

showList :: [Float] -> ShowS #

Show Int 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Int -> ShowS #

show :: Int -> String #

showList :: [Int] -> ShowS #

Show Levity 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Levity -> ShowS #

show :: Levity -> String #

showList :: [Levity] -> ShowS #

Show RuntimeRep 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> RuntimeRep -> ShowS #

show :: RuntimeRep -> String #

showList :: [RuntimeRep] -> ShowS #

Show VecCount 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> VecCount -> ShowS #

show :: VecCount -> String #

showList :: [VecCount] -> ShowS #

Show VecElem 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> VecElem -> ShowS #

show :: VecElem -> String #

showList :: [VecElem] -> ShowS #

Show Word 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Word -> ShowS #

show :: Word -> String #

showList :: [Word] -> ShowS #

Show a => Show (Complex a) 
Instance details

Defined in Data.Complex

Methods

showsPrec :: Int -> Complex a -> ShowS #

show :: Complex a -> String #

showList :: [Complex a] -> ShowS #

Show a => Show (First a) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> First a -> ShowS #

show :: First a -> String #

showList :: [First a] -> ShowS #

Show a => Show (Last a) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> Last a -> ShowS #

show :: Last a -> String #

showList :: [Last a] -> ShowS #

Show a => Show (Max a) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> Max a -> ShowS #

show :: Max a -> String #

showList :: [Max a] -> ShowS #

Show a => Show (Min a) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> Min a -> ShowS #

show :: Min a -> String #

showList :: [Min a] -> ShowS #

Show m => Show (WrappedMonoid m) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> WrappedMonoid m -> ShowS #

show :: WrappedMonoid m -> String #

showList :: [WrappedMonoid m] -> ShowS #

Show a => Show (NonEmpty a) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> NonEmpty a -> ShowS #

show :: NonEmpty a -> String #

showList :: [NonEmpty a] -> ShowS #

Show a => Show (Maybe a) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Maybe a -> ShowS #

show :: Maybe a -> String #

showList :: [Maybe a] -> ShowS #

Show a => Show (Solo a) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> Solo a -> ShowS #

show :: Solo a -> String #

showList :: [Solo a] -> ShowS #

Show a => Show [a] 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> [a] -> ShowS #

show :: [a] -> String #

showList :: [[a]] -> ShowS #

HasResolution a => Show (Fixed a) 
Instance details

Defined in Data.Fixed

Methods

showsPrec :: Int -> Fixed a -> ShowS #

show :: Fixed a -> String #

showList :: [Fixed a] -> ShowS #

(Show a, Show b) => Show (Arg a b) 
Instance details

Defined in Data.Semigroup

Methods

showsPrec :: Int -> Arg a b -> ShowS #

show :: Arg a b -> String #

showList :: [Arg a b] -> ShowS #

(Show a, Show b) => Show (a, b) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b) -> ShowS #

show :: (a, b) -> String #

showList :: [(a, b)] -> ShowS #

(Show a, Show b, Show c) => Show (a, b, c) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c) -> ShowS #

show :: (a, b, c) -> String #

showList :: [(a, b, c)] -> ShowS #

(Show (f a), Show (g a)) => Show (Product f g a) 
Instance details

Defined in Data.Functor.Product

Methods

showsPrec :: Int -> Product f g a -> ShowS #

show :: Product f g a -> String #

showList :: [Product f g a] -> ShowS #

(Show (f a), Show (g a)) => Show (Sum f g a) 
Instance details

Defined in Data.Functor.Sum

Methods

showsPrec :: Int -> Sum f g a -> ShowS #

show :: Sum f g a -> String #

showList :: [Sum f g a] -> ShowS #

(Show a, Show b, Show c, Show d) => Show (a, b, c, d) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d) -> ShowS #

show :: (a, b, c, d) -> String #

showList :: [(a, b, c, d)] -> ShowS #

Show (f (g a)) => Show (Compose f g a) 
Instance details

Defined in Data.Functor.Compose

Methods

showsPrec :: Int -> Compose f g a -> ShowS #

show :: Compose f g a -> String #

showList :: [Compose f g a] -> ShowS #

(Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e) -> ShowS #

show :: (a, b, c, d, e) -> String #

showList :: [(a, b, c, d, e)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f) => Show (a, b, c, d, e, f) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f) -> ShowS #

show :: (a, b, c, d, e, f) -> String #

showList :: [(a, b, c, d, e, f)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g) => Show (a, b, c, d, e, f, g) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g) -> ShowS #

show :: (a, b, c, d, e, f, g) -> String #

showList :: [(a, b, c, d, e, f, g)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h) => Show (a, b, c, d, e, f, g, h) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h) -> ShowS #

show :: (a, b, c, d, e, f, g, h) -> String #

showList :: [(a, b, c, d, e, f, g, h)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i) => Show (a, b, c, d, e, f, g, h, i) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i) -> String #

showList :: [(a, b, c, d, e, f, g, h, i)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j) => Show (a, b, c, d, e, f, g, h, i, j) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k) => Show (a, b, c, d, e, f, g, h, i, j, k) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j, k) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j, k) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j, k)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l) => Show (a, b, c, d, e, f, g, h, i, j, k, l) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j, k, l) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j, k, l) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j, k, l)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j, k, l, m) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j, k, l, m) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j, k, l, m)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j, k, l, m, n) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j, k, l, m, n)] -> ShowS #

(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n, Show o) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) 
Instance details

Defined in GHC.Internal.Show

Methods

showsPrec :: Int -> (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) -> ShowS #

show :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) -> String #

showList :: [(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)] -> ShowS #

type ShowS = String -> String #

showString :: String -> ShowS #

showParen :: Bool -> ShowS -> ShowS #

showChar :: Char -> ShowS #

shows :: Show a => a -> ShowS #

showListWith :: (a -> ShowS) -> [a] -> ShowS #

type PrecShowS = Int -> ShowS Source #

Type of strings representing expressions, parameterized by the surrounding precedence level.

This is the return type of flip showsPrec.

Simple constructors and applications

showCon :: String -> PrecShowS Source #

Show a constructor.

Possible constructor names are:

  • regular constructors (e.g., "Left");
  • parenthesized infix constructors (e.g., "(:)");
  • smart constructors, for abstract types (e.g., "Map.fromList").

Example with smart constructor

Expand
instance (Show k, Show v) => Show (Map k v) where
  showsPrec = flip precShows where
    precShows m = showCon "Map.fromList" @| Map.toList m

-- Example output:
-- > Map.fromList [(33, True), (55, False)]

showApp :: PrecShowS -> PrecShowS -> PrecShowS infixl 2 Source #

Show a function application.

(@|) :: Show a => PrecShowS -> a -> PrecShowS infixl 2 Source #

Show a function application.

This is an infix shorthand for showApp when the argument type is an instance of Show.

showF @| x = showApp showF (flip showsPrec x)

Infix constructors

showInfix :: String -> Int -> PrecShowS -> PrecShowS -> PrecShowS Source #

Show an applied infix operator with a given precedence.

showInfix' :: (Show a, Show b) => String -> Int -> a -> b -> PrecShowS Source #

Show an applied infix operator with a given precedence.

This is a shorthand for showInfix when the arguments types are instances of Show.

showInfix' op prec x y =
  showInfix op prec (flip showsPrec x) (flip showsPrec y)

Combinators for associative operators

Use with care, see warning under showInfixl.

showInfixl :: String -> Int -> PrecShowS -> PrecShowS -> PrecShowS Source #

Show an applied infix operator which is left associative (infixl). Use with care.

Warning

This combinator assumes that, if there is another infix operator to the left, it is either left associative with the same precedence, or it has a different precedence. An expression containing two operators at the same level with different associativities is ambiguous and will not be shown correctly with showInfixl and showInfixr.

By default, prefer showInfix and showInfix'.

showInfixl' :: (Show a, Show b) => String -> Int -> a -> b -> PrecShowS Source #

Show an applied infix operator which is left associative (infixl). Use with care, see showInfixl.

This is a shorthand for showInfixl when the arguments types are instances of Show.

By default, prefer showInfix and showInfix'.

showInfixr :: String -> Int -> PrecShowS -> PrecShowS -> PrecShowS Source #

Show an applied infix operator which is right associative (infixr). Use with care.

Warning

This combinator assumes that, if there is another infix operator to the right, it is either right associative with the same precedence, or it has a different precedence. An expression containing two operators at the same level with different associativities is ambiguous and will not be shown correctly with showInfixl and showInfixr.

By default, prefer showInfix and showInfix'.

Example usage

Expand
showList :: Show a => [a] -> PrecShowS
showList [] = showCon "[]"
showList (x : xs) = showInfixr ":" 5 (flip showsPrec x) (showList xs)

-- Example output:
-- > 0 : 1 : 2 : 3 : []

showInfixr' :: (Show a, Show b) => String -> Int -> a -> b -> PrecShowS Source #

Show an applied infix operator which is right associative (infixr). Use with care, see showInfixr.

This is a shorthand for showInfixr when the arguments types are instances of Show.

By default, prefer showInfix and showInfix'.

Records

type ShowFields = ShowS Source #

Strings representing a set of record fields separated by commas. They can be constructed using (.=.) and (@|), or using showField and appendFields.

showRecord :: String -> ShowFields -> PrecShowS Source #

Show a record. The first argument is the constructor name. The second represents the set of record fields.

showField :: String -> PrecShowS -> ShowFields Source #

Show a single record field: a field name and a value separated by '='.

(.=.) :: Show a => String -> a -> ShowFields infixr 8 Source #

Show a single record field: a field name and a value separated by '='.

This is an infix shorthand for showField when the value type is an instance of Show.

field .=. x   =   showField field (flip showsPrec x)

noFields :: ShowFields Source #

Empty set of record fields.

appendFields :: ShowFields -> ShowFields -> ShowFields infixr 1 Source #

Separate two nonempty sets of record fields by a comma.

(&|) :: ShowFields -> ShowFields -> ShowFields infixr 1 Source #

An infix synonym of appendFields.