Data.Tuple.Solo

OneTuple-0.4.2: Singleton Tuple

Safe Haskell | Safe
Language | Haskell98

Data.Tuple.Solo

Description

`Solo` fills the tuple gap with a singleton tuple.

`Solo` does not support the usual parenthesized tuple syntax.

`Solo`

- has the expected laziness properties
- can be pattern-matched
- ships with instances for several standard type classes, including all those supported by H98-standard tuples
- requires no language extensions, except for hierarchical modules

Note: on GHC-9.0 `getSolo` is not a record selector.

Synopsis

- data Solo a where
  - MkSolo a
  - pattern Solo :: a -> Solo a
- getSolo :: Solo a -> a

# Documentation

data Solo a #

`Solo` is the canonical lifted 1-tuple, just like `(,)` is the canonical lifted 2-tuple (pair) and `(,,)` is the canonical lifted 3-tuple (triple).

The most important feature of `Solo` is that it is possible to force its "outside" (usually by pattern matching) without forcing its "inside", because it is defined as a datatype rather than a newtype. One situation where this can be useful is when writing a function to extract a value from a data structure. Suppose you write an implementation of arrays and offer only this function to index into them:

    index :: Array a -> Int -> a

Now imagine that someone wants to extract a value from an array and store it in a lazy-valued finite map/dictionary:

    insert "hello" (arr index 12) m

This can actually lead to a space leak. The value is not actually extracted from the array until that value (now buried in a map) is forced. That means the entire array may be kept live by just that value! Often, the solution is to use a strict map, or to force the value before storing it, but for some purposes that's undesirable.

One common solution is to include an indexing function that can produce its result in an arbitrary `Applicative` context:

    indexA :: Applicative f => Array a -> Int -> f a

When using `indexA` in a pure context, `Solo` serves as a handy `Applicative` functor to hold the result. You could write a non-leaky version of the above example thus:

    case arr indexA 12 of
      Solo a -> insert "hello" a m

While such simple extraction functions are the most common uses for unary tuples, they can also be useful for fine-grained control of strict-spined data structure traversals, and for unifying the implementations of lazy and strict mapping functions.

Constructors

MkSolo a |

Bundled Patterns

pattern Solo :: a -> Solo a |

#### Instances

Instances details

MonadZip Solo | Since: base-4.15.0.0
Instance details Defined in Control.Monad.Zip Methods mzip :: Solo a -> Solo b -> Solo (a, b) # mzipWith :: (a -> b -> c) -> Solo a -> Solo b -> Solo c # munzip :: Solo (a, b) -> (Solo a, Solo b) #
Foldable1 Solo | Since: base-4.18.0.0
Instance details Defined in Data.Foldable1 Methods fold1 :: Semigroup m => Solo m -> m # foldMap1 :: Semigroup m => (a -> m) -> Solo a -> m # foldMap1' :: Semigroup m => (a -> m) -> Solo a -> m # toNonEmpty :: Solo a -> NonEmpty a # maximum :: Ord a => Solo a -> a # minimum :: Ord a => Solo a -> a # head :: Solo a -> a # last :: Solo a -> a # foldrMap1 :: (a -> b) -> (a -> b -> b) -> Solo a -> b # foldlMap1' :: (a -> b) -> (b -> a -> b) -> Solo a -> b # foldlMap1 :: (a -> b) -> (b -> a -> b) -> Solo a -> b # foldrMap1' :: (a -> b) -> (a -> b -> b) -> Solo a -> b #
Eq1 Solo | Since: base-4.15
Instance details Defined in Data.Functor.Classes Methods liftEq :: (a -> b -> Bool) -> Solo a -> Solo b -> Bool #
Ord1 Solo | Since: base-4.15
Instance details Defined in Data.Functor.Classes Methods liftCompare :: (a -> b -> Ordering) -> Solo a -> Solo b -> Ordering #
Read1 Solo | Since: base-4.15
Instance details Defined in Data.Functor.Classes Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Solo a) # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Solo a] # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Solo a) # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Solo a] #
Show1 Solo | Since: base-4.15
Instance details Defined in Data.Functor.Classes Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Solo a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Solo a] -> ShowS #
Applicative Solo | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods pure :: a -> Solo a # (<*>) :: Solo (a -> b) -> Solo a -> Solo b # liftA2 :: (a -> b -> c) -> Solo a -> Solo b -> Solo c # (*>) :: Solo a -> Solo b -> Solo b # (<*) :: Solo a -> Solo b -> Solo a #
Functor Solo | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods fmap :: (a -> b) -> Solo a -> Solo b # (<$) :: a -> Solo b -> Solo a #
Monad Solo | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods (>>=) :: Solo a -> (a -> Solo b) -> Solo b # (>>) :: Solo a -> Solo b -> Solo b # return :: a -> Solo a #
Monoid a => Monoid (Solo a) | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods mempty :: Solo a # mappend :: Solo a -> Solo a -> Solo a # mconcat :: [Solo a] -> Solo a #
Semigroup a => Semigroup (Solo a) | @since base-4.15
Instance details Defined in GHC.Internal.Base Methods (<>) :: Solo a -> Solo a -> Solo a # sconcat :: NonEmpty (Solo a) -> Solo a # stimes :: Integral b => b -> Solo a -> Solo a #
Eq a => Eq (Solo a) |
Instance details Defined in GHC.Classes Methods (==) :: Solo a -> Solo a -> Bool # (/=) :: Solo a -> Solo a -> Bool #
Ord a => Ord (Solo a) |
Instance details Defined in GHC.Classes Methods compare :: Solo a -> Solo a -> Ordering # (<) :: Solo a -> Solo a -> Bool # (<=) :: Solo a -> Solo a -> Bool # (>) :: Solo a -> Solo a -> Bool # (>=) :: Solo a -> Solo a -> Bool # max :: Solo a -> Solo a -> Solo a # min :: Solo a -> Solo a -> Solo a #

getSolo :: Solo a -> a #

Extract the value from a `Solo`. Very often, values should be extracted directly using pattern matching, to control just what gets evaluated when. `getSolo` is for convenience in situations where that is not the case:

When the result is passed to a strict function, it makes no difference whether the pattern matching is done on the "outside" or on the "inside":

    Data.Set.insert (getSolo sol) set === case sol of Solo v -> Data.Set.insert v set

A traversal may be performed in `Solo` in order to control evaluation internally, while using `getSolo` to extract the final result. A strict mapping function, for example, could be defined

    map' :: Traversable t => (a -> b) -> t a -> t b
    map' f = getSolo . traverse ((Solo $!) . f)