Data.Tuple
| Copyright | (c) The University of Glasgow 2001 |
|---|---|
| License | BSD-style (see the file libraries/base/LICENSE) |
| Maintainer | libraries@haskell.org |
| Stability | stable |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Description
Functions associated with the tuple data types.
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 | Deprecated: The Solo constructor has been renamed to MkSolo to avoid punning. |
Instances
| Foldable1 Solo Source | Since: base-4.18.0.0 |
||||
Defined in Data.Foldable1 Methodsfold1 :: Semigroup m => Solo m -> m Source foldMap1 :: Semigroup m => (a -> m) -> Solo a -> m Source foldMap1' :: Semigroup m => (a -> m) -> Solo a -> m Source toNonEmpty :: Solo a -> NonEmpty a Source maximum :: Ord a => Solo a -> a Source minimum :: Ord a => Solo a -> a Source foldrMap1 :: (a -> b) -> (a -> b -> b) -> Solo a -> b Source foldlMap1' :: (a -> b) -> (b -> a -> b) -> Solo a -> b Source foldlMap1 :: (a -> b) -> (b -> a -> b) -> Solo a -> b Source foldrMap1' :: (a -> b) -> (a -> b -> b) -> Solo a -> b Source | |||||
| Eq1 Solo Source | Since: base-4.15 |
||||
| Ord1 Solo Source | Since: base-4.15 |
||||
Defined in Data.Functor.Classes | |||||
| Read1 Solo Source | Since: base-4.15 |
||||
Defined in Data.Functor.Classes MethodsliftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Solo a) Source liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Solo a] Source liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Solo a) Source liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Solo a] Source | |||||
| Show1 Solo Source | Since: base-4.15 |
||||
| Applicative Solo Source | Since: base-4.15 |
||||
| Functor Solo Source | Since: base-4.15 |
||||
| Monad Solo Source | Since: base-4.15 |
||||
| MonadFix Solo Source | Since: base-4.15 |
||||
Defined in GHC.Internal.Control.Monad.Fix | |||||
| MonadZip Solo Source | Since: ghc-internal-4.15.0.0 |
||||
| Foldable Solo Source | Since: base-4.15 |
||||
Defined in GHC.Internal.Data.Foldable Methodsfold :: Monoid m => Solo m -> m Source foldMap :: Monoid m => (a -> m) -> Solo a -> m Source foldMap' :: Monoid m => (a -> m) -> Solo a -> m Source foldr :: (a -> b -> b) -> b -> Solo a -> b Source foldr' :: (a -> b -> b) -> b -> Solo a -> b Source foldl :: (b -> a -> b) -> b -> Solo a -> b Source foldl' :: (b -> a -> b) -> b -> Solo a -> b Source foldr1 :: (a -> a -> a) -> Solo a -> a Source foldl1 :: (a -> a -> a) -> Solo a -> a Source toList :: Solo a -> [a] Source length :: Solo a -> Int Source elem :: Eq a => a -> Solo a -> Bool Source maximum :: Ord a => Solo a -> a Source minimum :: Ord a => Solo a -> a Source | |||||
| Traversable Solo Source | Since: base-4.15 |
||||
| Generic1 Solo Source | |||||
Defined in GHC.Internal.Generics Associated Types
| |||||
| Monoid a => Monoid (Solo a) Source | Since: base-4.15 |
||||
| Semigroup a => Semigroup (Solo a) Source | Since: base-4.15 |
||||
| Data a => Data (Solo a) Source | Since: base-4.15 |
||||
Defined in GHC.Internal.Data.Data Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Solo a -> c (Solo a) Source gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Solo a) Source toConstr :: Solo a -> Constr Source dataTypeOf :: Solo a -> DataType Source dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Solo a)) Source dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Solo a)) Source gmapT :: (forall b. Data b => b -> b) -> Solo a -> Solo a Source gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Solo a -> r Source gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Solo a -> r Source gmapQ :: (forall d. Data d => d -> u) -> Solo a -> [u] Source gmapQi :: Int -> (forall d. Data d => d -> u) -> Solo a -> u Source gmapM :: Monad m => (forall d. Data d => d -> m d) -> Solo a -> m (Solo a) Source gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Solo a -> m (Solo a) Source gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Solo a -> m (Solo a) Source | |||||
| Bounded a => Bounded (Solo a) Source | |||||
| Enum a => Enum (Solo a) Source | |||||
Defined in GHC.Internal.Enum Methodssucc :: Solo a -> Solo a Source pred :: Solo a -> Solo a Source toEnum :: Int -> Solo a Source fromEnum :: Solo a -> Int Source enumFrom :: Solo a -> [Solo a] Source enumFromThen :: Solo a -> Solo a -> [Solo a] Source enumFromTo :: Solo a -> Solo a -> [Solo a] Source enumFromThenTo :: Solo a -> Solo a -> Solo a -> [Solo a] Source | |||||
| Generic (Solo a) Source | |||||
Defined in GHC.Internal.Generics Associated Types
| |||||
| Ix a => Ix (Solo a) Source | |||||
Defined in GHC.Internal.Ix Methodsrange :: (Solo a, Solo a) -> [Solo a] Source index :: (Solo a, Solo a) -> Solo a -> Int Source unsafeIndex :: (Solo a, Solo a) -> Solo a -> Int Source inRange :: (Solo a, Solo a) -> Solo a -> Bool Source rangeSize :: (Solo a, Solo a) -> Int Source unsafeRangeSize :: (Solo a, Solo a) -> Int Source | |||||
| Read a => Read (Solo a) Source | Since: base-4.15 |
||||
| Show a => Show (Solo a) Source | Since: base-4.15 |
||||
| Eq a => Eq (Solo a) Source | |||||
| Ord a => Ord (Solo a) Source | |||||
Defined in GHC.Classes | |||||
| type Rep1 Solo Source | Since: base-4.15 |
||||
Defined in GHC.Internal.Generics | |||||
| type Rep (Solo a) Source | Since: base-4.15 |
||||
Defined in GHC.Internal.Generics | |||||
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)
Extract the first component of a pair.
Extract the second component of a pair.
curry :: ((a, b) -> c) -> a -> b -> c Source
Convert an uncurried function to a curried function.
Examples
>>> curry fst 1 2 1
uncurry :: (a -> b -> c) -> (a, b) -> c Source
uncurry converts a curried function to a function on pairs.
Examples
>>> uncurry (+) (1,2) 3
>>> uncurry ($) (show, 1) "1"
>>> map (uncurry max) [(1,2), (3,4), (6,8)] [2,4,8]
swap :: (a, b) -> (b, a) Source
Swap the components of a pair.
© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).
https://downloads.haskell.org/~ghc/9.12.1/docs/libraries/base-4.21.0.0-8e62/Data-Tuple.html