singletons-2.5.1: A framework for generating singleton types

Copyright | (C) 2017 Ryan Scott
License | BSD-style (see LICENSE)
Maintainer | Ryan Scott
Stability | experimental
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Data.Singletons.ShowSing

Contents

- The `ShowSing` type
- Orphan instances

Description

Defines the class `ShowSing` type synonym, which is useful for defining `Show` instances for singleton types.

Synopsis

- class (forall (z :: k). Show (Sing z)) => ShowSing k

# The `ShowSing` type

class (forall (z :: k). Show (Sing z)) => ShowSing k Source #

In addition to the promoted and singled versions of the `Show` class that `singletons` provides, it is also useful to be able to directly define `Show` instances for singleton types themselves. Doing so is almost entirely straightforward, as a derived `Show` instance does 90 percent of the work. The last 10 percent—getting the right instance context—is a bit tricky, and that's where `ShowSing` comes into play.

As an example, let's consider the singleton type for lists. We want to write an instance with the following shape:

    deriving instance ??? => Show (Sing (x :: [k]))

To figure out what should go in place of `???`, observe that we require the type of each field to also be `Show` instances. In other words, we need something like `(Show (Sing (a :: k)))`. But this isn't quite right, as the type variable `a` doesn't appear in the instance head. In fact, this `a` type is really referring to an existentially quantified type variable in the `SCons` constructor, so it doesn't make sense to try and use it like this.

Luckily, the `QuantifiedConstraints` language extension provides a solution to this problem. This lets you write a context of the form `(forall a. Show (Sing (a :: k)))`, which demands that there be an instance for `Show (Sing (a :: k))` that is parametric in the use of `a`. Thus, our final instance looks like:

    deriving instance (forall a. Show (Sing (a :: k))) => Show (Sing (x :: [k]))

Because that quantified constraint is somewhat lengthy, we provide the `ShowSing` class synonym as a convenient shorthand. Thus, the above instance is equivalent to:

    deriving instance ShowSing k => Show (Sing (x :: [k]))

When singling a derived `Show` instance, `singletons` will also derive a `Show` instance for the corresponding singleton type using `ShowSing`. In other words, if you give `singletons` a derived `Show` instance, then you'll receive the following in return:

- A promoted (`PShow`) instance
- A singled (`SShow`) instance
- A `Show` instance for the singleton type

What a bargain!

Instances

(forall (z :: k). Show (Sing z)) => ShowSing k Source # |
Instance details Defined in Data.Singletons.ShowSing

# Orphan instances

Show (SSymbol s) Source # |
Instance details Methods showsPrec :: Int -> SSymbol s -> ShowS Source # show :: SSymbol s -> String Source # showList :: [SSymbol s] -> ShowS Source #
Show (SNat n) Source # |
Instance details Methods showsPrec :: Int -> SNat n -> ShowS Source # show :: SNat n -> String Source # showList :: [SNat n] -> ShowS Source #
Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing [a]) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
ShowSing a => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b, ShowSing c) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b, ShowSing c, ShowSing d) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b, ShowSing c, ShowSing d, ShowSing e) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b, ShowSing c, ShowSing d, ShowSing e, ShowSing f) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing b, ShowSing c, ShowSing d, ShowSing e, ShowSing f, ShowSing g) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
ShowSing a => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #
(ShowSing a, ShowSing [a]) => Show (Sing z) Source # |
Instance details Methods showsPrec :: Int -> Sing z -> ShowS Source # show :: Sing z -> String Source # showList :: [Sing z] -> ShowS Source #