Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This uses the OneTuple
compatibility library to backport Solo
to old
versions of GHC. Note that OneTuple
makes use of pattern synonyms, which
cannot be defined on pre-7.8 versions of GHC. As such, it is not feasible
to backport the Solo
data constructor on pre-7.8 versions of GHC, as
OneTuple
defines this as a pattern synonym.
Documentation
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.
MkSolo a |
pattern Solo :: a -> (a) |
Instances
Foldable Solo | Since: base-4.15 |
Defined in Data.Foldable fold :: Monoid m => Solo m -> m # foldMap :: Monoid m => (a -> m) -> Solo a -> m # foldMap' :: Monoid m => (a -> m) -> Solo a -> m # foldr :: (a -> b -> b) -> b -> Solo a -> b # foldr' :: (a -> b -> b) -> b -> Solo a -> b # foldl :: (b -> a -> b) -> b -> Solo a -> b # foldl' :: (b -> a -> b) -> b -> Solo a -> b # foldr1 :: (a -> a -> a) -> Solo a -> a # foldl1 :: (a -> a -> a) -> Solo a -> a # elem :: Eq a => a -> Solo a -> Bool # maximum :: Ord a => Solo a -> a # | |
Foldable1 Solo | Since: base-4.18.0.0 |
Defined in Data.Foldable1 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 # 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 # | |
Traversable Solo | Since: base-4.15 |
Applicative Solo | Since: base-4.15 |
Functor Solo | Since: base-4.15 |
Monad Solo | Since: base-4.15 |
Monoid a => Monoid (a) | Since: base-4.15 |
Semigroup a => Semigroup (a) | Since: base-4.15 |
Bounded a => Bounded (a) | |
Enum a => Enum (a) | |
Read a => Read (a) | Since: base-4.15 |
Show a => Show (a) | Since: base-4.15 |
Eq a => Eq (a) | |
Ord a => Ord (a) | |