base-4.17.0.0: Basic libraries
Copyright(c) The University of Glasgow 2001
LicenseBSD-style (see the file libraries/base/LICENSE)
Maintainerlibraries@haskell.org
Stabilitystable
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Tuple

Description

Functions associated with the tuple data types.

Synopsis
  • data Solo a where
  • fst :: (a, b) -> a
  • snd :: (a, b) -> b
  • curry :: ((a, b) -> c) -> a -> b -> c
  • uncurry :: (a -> b -> c) -> (a, b) -> c
  • swap :: (a, b) -> (b, a)

Documentation

data Solo a Source #

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 -> (a) 

Instances

Instances details
MonadFix Solo Source #

Since: base-4.15

Instance details

Defined in Control.Monad.Fix

Methods

mfix :: (a -> Solo a) -> Solo a Source #

MonadZip Solo Source #

Since: base-4.15.0.0

Instance details

Defined in Control.Monad.Zip

Methods

mzip :: Solo a -> Solo b -> Solo (a, b) Source #

mzipWith :: (a -> b -> c) -> Solo a -> Solo b -> Solo c Source #

munzip :: Solo (a, b) -> (Solo a, Solo b) Source #

Foldable Solo Source #

Since: base-4.15

Instance details

Defined in Data.Foldable

Methods

fold :: 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 #

null :: Solo a -> Bool 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 #

sum :: Num a => Solo a -> a Source #

product :: Num a => Solo a -> a Source #

Eq1 Solo Source #

Since: base-4.15

Instance details

Defined in Data.Functor.Classes

Methods

liftEq :: (a -> b -> Bool) -> Solo a -> Solo b -> Bool Source #

Ord1 Solo Source #

Since: base-4.15

Instance details

Defined in Data.Functor.Classes

Methods

liftCompare :: (a -> b -> Ordering) -> Solo a -> Solo b -> Ordering Source #

Read1 Solo Source #

Since: base-4.15

Instance details

Defined in Data.Functor.Classes

Methods

liftReadsPrec :: (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

Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Solo a -> ShowS Source #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Solo a] -> ShowS Source #

Traversable Solo Source #

Since: base-4.15

Instance details

Defined in Data.Traversable

Methods

traverse :: Applicative f => (a -> f b) -> Solo a -> f (Solo b) Source #

sequenceA :: Applicative f => Solo (f a) -> f (Solo a) Source #

mapM :: Monad m => (a -> m b) -> Solo a -> m (Solo b) Source #

sequence :: Monad m => Solo (m a) -> m (Solo a) Source #

Applicative Solo Source #

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

pure :: a -> Solo a Source #

(<*>) :: Solo (a -> b) -> Solo a -> Solo b Source #

liftA2 :: (a -> b -> c) -> Solo a -> Solo b -> Solo c Source #

(*>) :: Solo a -> Solo b -> Solo b Source #

(<*) :: Solo a -> Solo b -> Solo a Source #

Functor Solo Source #

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

fmap :: (a -> b) -> Solo a -> Solo b Source #

(<$) :: a -> Solo b -> Solo a Source #

Monad Solo Source #

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

(>>=) :: Solo a -> (a -> Solo b) -> Solo b Source #

(>>) :: Solo a -> Solo b -> Solo b Source #

return :: a -> Solo a Source #

Generic1 Solo Source # 
Instance details

Defined in GHC.Generics

Associated Types

type Rep1 Solo :: k -> Type Source #

Methods

from1 :: forall (a :: k). Solo a -> Rep1 Solo a Source #

to1 :: forall (a :: k). Rep1 Solo a -> Solo a Source #

Data a => Data (a) Source #

Since: base-4.15

Instance details

Defined in Data.Data

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> (a) -> c (a) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (a) Source #

toConstr :: (a) -> Constr Source #

dataTypeOf :: (a) -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (a)) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (a)) Source #

gmapT :: (forall b. Data b => b -> b) -> (a) -> (a) Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> (a) -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> (a) -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> (a) -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> (a) -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> (a) -> m (a) Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> (a) -> m (a) Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> (a) -> m (a) Source #

Monoid a => Monoid (a) Source #

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

mempty :: (a) Source #

mappend :: (a) -> (a) -> (a) Source #

mconcat :: [(a)] -> (a) Source #

Semigroup a => Semigroup (a) Source #

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

(<>) :: (a) -> (a) -> (a) Source #

sconcat :: NonEmpty (a) -> (a) Source #

stimes :: Integral b => b -> (a) -> (a) Source #

Bounded a => Bounded (a) Source # 
Instance details

Defined in GHC.Enum

Methods

minBound :: (a) Source #

maxBound :: (a) Source #

Enum a => Enum (a) Source # 
Instance details

Defined in GHC.Enum

Methods

succ :: (a) -> (a) Source #

pred :: (a) -> (a) Source #

toEnum :: Int -> (a) Source #

fromEnum :: (a) -> Int Source #

enumFrom :: (a) -> [(a)] Source #

enumFromThen :: (a) -> (a) -> [(a)] Source #

enumFromTo :: (a) -> (a) -> [(a)] Source #

enumFromThenTo :: (a) -> (a) -> (a) -> [(a)] Source #

Generic (a) Source # 
Instance details

Defined in GHC.Generics

Associated Types

type Rep (a) :: Type -> Type Source #

Methods

from :: (a) -> Rep (a) x Source #

to :: Rep (a) x -> (a) Source #

Ix a => Ix (a) Source # 
Instance details

Defined in GHC.Ix

Methods

range :: ((a), (a)) -> [(a)] Source #

index :: ((a), (a)) -> (a) -> Int Source #

unsafeIndex :: ((a), (a)) -> (a) -> Int Source #

inRange :: ((a), (a)) -> (a) -> Bool Source #

rangeSize :: ((a), (a)) -> Int Source #

unsafeRangeSize :: ((a), (a)) -> Int Source #

Read a => Read (a) Source #

Since: base-4.15

Instance details

Defined in GHC.Read

Show a => Show (a) Source #

Since: base-4.15

Instance details

Defined in GHC.Show

Methods

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

show :: (a) -> String Source #

showList :: [(a)] -> ShowS Source #

Eq a => Eq (a) 
Instance details

Defined in GHC.Classes

Methods

(==) :: (a) -> (a) -> Bool Source #

(/=) :: (a) -> (a) -> Bool Source #

Ord a => Ord (a) 
Instance details

Defined in GHC.Classes

Methods

compare :: (a) -> (a) -> Ordering Source #

(<) :: (a) -> (a) -> Bool Source #

(<=) :: (a) -> (a) -> Bool Source #

(>) :: (a) -> (a) -> Bool Source #

(>=) :: (a) -> (a) -> Bool Source #

max :: (a) -> (a) -> (a) Source #

min :: (a) -> (a) -> (a) Source #

type Rep1 Solo Source #

Since: base-4.15

Instance details

Defined in GHC.Generics

type Rep1 Solo = D1 ('MetaData "Solo" "GHC.Tuple.Prim" "ghc-prim" 'False) (C1 ('MetaCons "MkSolo" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
type Rep (a) Source #

Since: base-4.15

Instance details

Defined in GHC.Generics

type Rep (a) = D1 ('MetaData "Solo" "GHC.Tuple.Prim" "ghc-prim" 'False) (C1 ('MetaCons "MkSolo" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

fst :: (a, b) -> a Source #

Extract the first component of a pair.

snd :: (a, b) -> b Source #

Extract the second component of a pair.

curry :: ((a, b) -> c) -> a -> b -> c Source #

curry converts an uncurried function to a curried function.

Examples

Expand
>>> curry fst 1 2
1

uncurry :: (a -> b -> c) -> (a, b) -> c Source #

uncurry converts a curried function to a function on pairs.

Examples

Expand
>>> 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.