ghc-internal-9.1001.0: Basic libraries

GHC.Internal.Base

Description

Basic data types and classes.

Synopsis

# Documentation

($) :: (a -> b) -> a -> b infixr 0 Source # ($) is the function application operator.

Applying ($) to a function f and an argument x gives the same result as applying f to x directly. The definition is akin to this: ($) :: (a -> b) -> a -> b
($) f x = f x This is id specialized from a -> a to (a -> b) -> (a -> b) which by the associativity of (->) is the same as (a -> b) -> a -> b. On the face of it, this may appear pointless! But it's actually one of the most useful and important operators in Haskell. The order of operations is very different between ($) and normal function application. Normal function application has precedence 10 - higher than any operator - and associates to the left. So these two definitions are equivalent:

expr = min 5 1 + 5
expr = ((min 5) 1) + 5

($) has precedence 0 (the lowest) and associates to the right, so these are equivalent: expr = min 5$ 1 + 5
expr = (min 5) (1 + 5)

#### Examples

Expand

A common use cases of ($) is to avoid parentheses in complex expressions. For example, instead of using nested parentheses in the following Haskell function: -- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum (mapMaybe readMaybe (words s)) we can deploy the function application operator: -- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum$ mapMaybe readMaybe $words s ($) is also used as a section (a partially applied operator), in order to indicate that we wish to apply some yet-unspecified function to a given value. For example, to apply the argument 5 to a list of functions:

applyFive :: [Int]
applyFive = map ($5) [(+1), (2^)] >>> [6, 32] #### Technical Remark (Representation Polymorphism) Expand ($) is fully representation-polymorphic. This allows it to also be used with arguments of unlifted and even unboxed kinds, such as unboxed integers:

fastMod :: Int -> Int -> Int
fastMod (I# x) (I# m) = I# $remInt# x m ($!) :: (a -> b) -> a -> b infixr 0 Source #

Strict (call-by-value) application operator. It takes a function and an argument, evaluates the argument to weak head normal form (WHNF), then calls the function with that value.

(++) :: [a] -> [a] -> [a] infixr 5 Source #

(++) appends two lists, i.e.,

[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn]
[x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]

If the first list is not finite, the result is the first list.

#### Performance considerations

Expand

This function takes linear time in the number of elements of the first list. Thus it is better to associate repeated applications of (++) to the right (which is the default behaviour): xs ++ (ys ++ zs) or simply xs ++ ys ++ zs, but not (xs ++ ys) ++ zs. For the same reason concat = foldr (++) [] has linear performance, while foldl (++) [] is prone to quadratic slowdown

#### Examples

Expand
>>> [1, 2, 3] ++ [4, 5, 6]
[1,2,3,4,5,6]
>>> [] ++ [1, 2, 3]
[1,2,3]
>>> [3, 2, 1] ++ []
[3,2,1]

(.) :: (b -> c) -> (a -> b) -> a -> c infixr 9 Source #

Right to left function composition.

(f . g) x = f (g x)
f . id = f = id . f

#### Examples

Expand
>>> map ((*2) . length) [[], [0, 1, 2], [0]]
[0,6,2]
>>> foldr (.) id [(+1), (*3), (^3)] 2
25
>>> let (...) = (.).(.) in ((*2)...(+)) 5 10
30

(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4 Source #

A variant of <*> with the types of the arguments reversed. It differs from flip (<*>) in that the effects are resolved in the order the arguments are presented.

#### Examples

Expand
>>> (<**>) (print 1) (id <$print 2) 1 2 >>> flip (<*>) (print 1) (id <$ print 2)
2
1
>>> ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]
ZipList {getZipList = [5.0,10.0,2.0]}

(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 Source #

Same as >>=, but with the arguments interchanged.

as >>= f == f =<< as

absurd :: Void -> a Source #

Since Void values logically don't exist, this witnesses the logical reasoning tool of "ex falso quodlibet".

>>> let x :: Either Void Int; x = Right 5
>>> :{
case x of
Right r -> r
Left l  -> absurd l
:}
5

Since: base-4.8.0.0

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

In many situations, the liftM operations can be replaced by uses of ap, which promotes function application.

return f ap x1 ap ... ap xn

is equivalent to

liftM<n> f x1 x2 ... xn

#### Examples

Expand
>>> pure (\x y z -> x + y * z) ap Just 1 ap Just 5 ap Just 10
Just 51

asTypeOf :: a -> a -> a Source #

asTypeOf is a type-restricted version of const. It is usually used as an infix operator, and its typing forces its first argument (which is usually overloaded) to have the same type as the second.

assert :: Bool -> a -> a Source #

If the first argument evaluates to True, then the result is the second argument. Otherwise an AssertionFailed exception is raised, containing a String with the source file and line number of the call to assert.

Assertions can normally be turned on or off with a compiler flag (for GHC, assertions are normally on unless optimisation is turned on with -O or the -fignore-asserts option is given). When assertions are turned off, the first argument to assert is ignored, and the second argument is returned as the result.

augment :: (forall b. (a -> b -> b) -> b -> b) -> [a] -> [a] Source #

A list producer that can be fused with foldr. This function is merely

augment g xs = g (:) xs

but GHC's simplifier will transform an expression of the form foldr k z (augment g xs), which may arise after inlining, to g k (foldr k z xs), which avoids producing an intermediate list.

bindIO :: IO a -> (a -> IO b) -> IO b Source #

breakpoint :: a -> a Source #

build :: (forall b. (a -> b -> b) -> b -> b) -> [a] Source #

A list producer that can be fused with foldr. This function is merely

build g = g (:) []

but GHC's simplifier will transform an expression of the form foldr k z (build g), which may arise after inlining, to g k z, which avoids producing an intermediate list.

const :: a -> b -> a Source #

const x y always evaluates to x, ignoring its second argument.

const x = \_ -> x

This function might seem useless at first glance, but it can be very useful in a higher order context.

#### Examples

Expand
>>> const 42 "hello"
42
>>> map (const 42) [0..3]
[42,42,42,42]

divInt :: Int -> Int -> Int Source #

Used to implement div for the Integral typeclass. This performs integer division on its two parameters, truncated towards negative infinity.

#### Example

Expand
>>> 10 divInt 2
5
>>> 10 div 2
5

divModInt :: Int -> Int -> (Int, Int) Source #

Used to implement divMod for the Integral typeclass. This gives a tuple equivalent to

(div x y, mod x y)

#### Example

Expand
>>> divModInt 10 2
(5,0)
>>> divMod 10 2
(5,0)

This String equality predicate is used when desugaring pattern-matches against strings.

flip :: (a -> b -> c) -> b -> a -> c Source #

flip f takes its (first) two arguments in the reverse order of f.

flip f x y = f y x
flip . flip = id

#### Examples

Expand
>>> flip (++) "hello" "world"
"worldhello"
>>> let (.>) = flip (.) in (+1) .> show $5 "6" foldr :: (a -> b -> b) -> b -> [a] -> b Source # foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left: foldr f z [x1, x2, ..., xn] == x1 f (x2 f ... (xn f z)...) getTag :: forall {lev :: Levity} (a :: TYPE ('BoxedRep lev)). DataToTag a => a -> Int# Source # Returns the tag of a constructor application; this function was once used by the deriving code for Eq, Ord and Enum. Shift the argument left by the specified number of bits (which must be non-negative). Shift the argument right (signed) by the specified number of bits (which must be non-negative). The RA means "right, arithmetic" (as opposed to RL for logical) Shift the argument right (unsigned) by the specified number of bits (which must be non-negative). The RL means "right, logical" (as opposed to RA for arithmetic) id :: a -> a Source # Identity function. id x = x This function might seem useless at first glance, but it can be very useful in a higher order context. #### Examples Expand >>> length$ filter id [True, True, False, True]
3
>>> Just (Just 3) >>= id
Just 3
>>> foldr id 0 [(^3), (*5), (+2)]
1000

join :: Monad m => m (m a) -> m a Source #

The join function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.

'join bss' can be understood as the do expression

do bs <- bss
bs

#### Examples

Expand
>>> join [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[1,2,3,4,5,6,7,8,9]
>>> join (Just (Just 3))
Just 3

A common use of join is to run an IO computation returned from an STM transaction, since STM transactions can't perform IO directly. Recall that

atomically :: STM a -> IO a

is used to run STM transactions atomically. So, by specializing the types of atomically and join to

atomically :: STM (IO b) -> IO (IO b)
join       :: IO (IO b)  -> IO b

we can compose them as

join . atomically :: STM (IO b) -> IO b

to run an STM transaction and the IO action it returns.

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

Lift a function to actions. Equivalent to Functor's fmap but implemented using only Applicative's methods: liftA f a = pure f <*> a

As such this function may be used to implement a Functor instance from an Applicative one.

#### Examples

Expand

Using the Applicative instance for Lists:

>>> liftA (+1) [1, 2]
[2,3]

Or the Applicative instance for Maybe

>>> liftA (+1) (Just 3)
Just 4

liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d Source #

Lift a ternary function to actions.

liftM :: Monad m => (a1 -> r) -> m a1 -> m r Source #

Promote a function to a monad. This is equivalent to fmap but specialised to Monads.

liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r Source #

Promote a function to a monad, scanning the monadic arguments from left to right.

#### Examples

Expand
>>> liftM2 (+) [0,1] [0,2]
[0,2,1,3]
>>> liftM2 (+) (Just 1) Nothing
Nothing
>>> liftM2 (+) (+ 3) (* 2) 5
18

liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r Source #

Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).

liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r Source #

Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).

liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r Source #

Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).

map :: (a -> b) -> [a] -> [b] Source #

$$\mathcal{O}(n)$$. map f xs is the list obtained by applying f to each element of xs, i.e.,

map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]
map f [x1, x2, ...] == [f x1, f x2, ...]

this means that map id == id

#### Examples

Expand
>>> map (+1) [1, 2, 3]
[2,3,4]
>>> map id [1, 2, 3]
[1,2,3]
>>> map (\n -> 3 * n + 1) [1, 2, 3]
[4,7,10]

mapFB :: (elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst Source #

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

mapM f is equivalent to sequence . map f.

modInt :: Int -> Int -> Int Source #

Used to implement mod for the Integral typeclass. This performs the modulo operation, satisfying

((x div y) * y) + (x mod y) == x

#### Example

Expand
>>> 7 modInt 3
1
>>> 7 mod 3
1

The fromEnum method restricted to the type Char.

otherwise is defined as the value True. It helps to make guards more readable. eg.

f x | x < 0     = ...
| otherwise = ...

quotInt :: Int -> Int -> Int Source #

Used to implement quot for the Integral typeclass. This performs integer division on its two parameters, truncated towards zero.

#### Example

Expand
>>> quotInt 10 2
5
>>> quot 10 2
5

quotRemInt :: Int -> Int -> (Int, Int) Source #

Used to implement quotRem for the Integral typeclass. This gives a tuple equivalent to

(quot x y, mod x y)

#### Example

Expand
>>> quotRemInt 10 2
(5,0)
>>> quotRem 10 2
(5,0)

remInt :: Int -> Int -> Int Source #

Used to implement rem for the Integral typeclass. This gives the remainder after integer division of its two parameters, satisfying

((x quot y) * y) + (x rem y) == x

#### Example

Expand
>>> remInt 3 2
1
>>> rem 3 2
1

returnIO :: a -> IO a Source #

sequence :: Monad m => [m a] -> m [a] Source #

Evaluate each action in the sequence from left to right, and collect the results.

Shift the argument left by the specified number of bits (which must be non-negative).

Shift the argument right by the specified number of bits (which must be non-negative). The RL means "right, logical" (as opposed to RA for arithmetic) (although an arithmetic right shift wouldn't make sense for Word#)

This function is used to implement branchless shifts. If the number of bits to shift is greater than or equal to the type size in bits, then the shift must return 0. Instead of doing a test, we use a mask obtained via this function which is branchless too.

shift_mask m b | b < m = 0xFF..FF | otherwise = 0

thenIO :: IO a -> IO b -> IO b Source #

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

until p f yields the result of applying f until p holds.

vacuous :: Functor f => f Void -> f a Source #

If Void is uninhabited then any Functor that holds only values of type Void is holding no values. It is implemented in terms of fmap absurd.

Since: base-4.8.0.0

when :: Applicative f => Bool -> f () -> f () Source #

Conditional execution of Applicative expressions. For example,

#### Examples

Expand
when debug (putStrLn "Debugging")

will output the string Debugging if the Boolean value debug is True, and otherwise do nothing.

>>> putStr "pi:" >> when False (print 3.14159)
pi:

class Applicative f => Alternative (f :: Type -> Type) where Source #

A monoid on applicative functors.

If defined, some and many should be the least solutions of the equations:

#### Examples

Expand
>>> Nothing <|> Just 42
Just 42
>>> [1, 2] <|> [3, 4]
[1,2,3,4]
>>> empty <|> print (2^15)
32768

Minimal complete definition

Methods

empty :: f a Source #

The identity of <|>

empty <|> a     == a
a     <|> empty == a

(<|>) :: f a -> f a -> f a infixl 3 Source #

An associative binary operation

some :: f a -> f [a] Source #

One or more.

#### Examples

Expand
>>> some (putStr "la")
lalalalalalalalala... * goes on forever *
>>> some Nothing
nothing
>>> take 5 <$> some (Just 1) * hangs forever * Note that this function can be used with Parsers based on Applicatives. In that case some parser will attempt to parse parser one or more times until it fails. many :: f a -> f [a] Source # Zero or more. #### Examples Expand >>> many (putStr "la") lalalalalalalalala... * goes on forever * >>> many Nothing Just [] >>> take 5 <$> many (Just 1)
* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case many parser will attempt to parse parser zero or more times until it fails.

#### Instances

Instances details

class Functor f => Applicative (f :: Type -> Type) where Source #

A functor with application, providing operations to

• embed pure expressions (pure), and
• sequence computations and combine their results (<*> and liftA2).

A minimal complete definition must include implementations of pure and of either <*> or liftA2. If it defines both, then they must behave the same as their default definitions:

(<*>) = liftA2 id
liftA2 f x y = f <$> x <*> y Further, any definition must satisfy the following: Identity pure id <*> v = v Composition pure (.) <*> u <*> v <*> w = u <*> (v <*> w) Homomorphism pure f <*> pure x = pure (f x) Interchange u <*> pure y = pure ($ y) <*> u

The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:

As a consequence of these laws, the Functor instance for f will satisfy

It may be useful to note that supposing

forall x y. p (q x y) = f x . g y

it follows from the above that

liftA2 p (liftA2 q u v) = liftA2 f u . liftA2 g v

If f is also a Monad, it should satisfy

(which implies that pure and <*> satisfy the applicative functor laws).

Minimal complete definition

pure, ((<*>) | liftA2)

Methods

pure :: a -> f a Source #

Lift a value into the Structure.

#### Examples

Expand
>>> pure 1 :: Maybe Int
Just 1
>>> pure 'z' :: [Char]
"z"
>>> pure (pure ":D") :: Maybe [String]
Just [":D"]

(<*>) :: f (a -> b) -> f a -> f b infixl 4 Source #

Sequential application.

A few functors support an implementation of <*> that is more efficient than the default one.

#### Example

Expand

Used in combination with (<$>), (<*>) can be used to build a record. >>> data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz} >>> produceFoo :: Applicative f => f Foo >>> produceBar :: Applicative f => f Bar >>> produceBaz :: Applicative f => f Baz >>> mkState :: Applicative f => f MyState >>> mkState = MyState <$> produceFoo <*> produceBar <*> produceBaz

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

Lift a binary function to actions.

Some functors support an implementation of liftA2 that is more efficient than the default one. In particular, if fmap is an expensive operation, it is likely better to use liftA2 than to fmap over the structure and then use <*>.

This became a typeclass method in 4.10.0.0. Prior to that, it was a function defined in terms of <*> and fmap.

#### Example

Expand
>>> liftA2 (,) (Just 3) (Just 5)
Just (3,5)
>>> liftA2 (+) [1, 2, 3] [4, 5, 6]
[5,6,7,6,7,8,7,8,9]

(*>) :: f a -> f b -> f b infixl 4 Source #

Sequence actions, discarding the value of the first argument.

#### Examples

Expand

If used in conjunction with the Applicative instance for Maybe, you can chain Maybe computations, with a possible "early return" in case of Nothing.

>>> Just 2 *> Just 3
Just 3
>>> Nothing *> Just 3
Nothing

Of course a more interesting use case would be to have effectful computations instead of just returning pure values.

>>> import Data.Char
>>> let p = string "my name is " *> munch1 isAlpha <* eof
>>> readP_to_S p "my name is Simon"
[("Simon","")]

(<*) :: f a -> f b -> f a infixl 4 Source #

Sequence actions, discarding the value of the second argument.

#### Instances

Instances details
 Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Base Methodspure :: a -> NonEmpty a Source #(<*>) :: NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b Source #liftA2 :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c Source #(*>) :: NonEmpty a -> NonEmpty b -> NonEmpty b Source #(<*) :: NonEmpty a -> NonEmpty b -> NonEmpty a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Conc.Sync Methodspure :: a -> STM a Source #(<*>) :: STM (a -> b) -> STM a -> STM b Source #liftA2 :: (a -> b -> c) -> STM a -> STM b -> STM c Source #(*>) :: STM a -> STM b -> STM b Source #(<*) :: STM a -> STM b -> STM a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Functor.Identity Methodspure :: a -> Identity a Source #(<*>) :: Identity (a -> b) -> Identity a -> Identity b Source #liftA2 :: (a -> b -> c) -> Identity a -> Identity b -> Identity c Source #(*>) :: Identity a -> Identity b -> Identity b Source #(<*) :: Identity a -> Identity b -> Identity a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Monoid Methodspure :: a -> First a Source #(<*>) :: First (a -> b) -> First a -> First b Source #liftA2 :: (a -> b -> c) -> First a -> First b -> First c Source #(*>) :: First a -> First b -> First b Source #(<*) :: First a -> First b -> First a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Monoid Methodspure :: a -> Last a Source #(<*>) :: Last (a -> b) -> Last a -> Last b Source #liftA2 :: (a -> b -> c) -> Last a -> Last b -> Last c Source #(*>) :: Last a -> Last b -> Last b Source #(<*) :: Last a -> Last b -> Last a Source # Source # Since: base-4.11.0.0 Instance detailsDefined in GHC.Internal.Data.Ord Methodspure :: a -> Down a Source #(<*>) :: Down (a -> b) -> Down a -> Down b Source #liftA2 :: (a -> b -> c) -> Down a -> Down b -> Down c Source #(*>) :: Down a -> Down b -> Down b Source #(<*) :: Down a -> Down b -> Down a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methodspure :: a -> Dual a Source #(<*>) :: Dual (a -> b) -> Dual a -> Dual b Source #liftA2 :: (a -> b -> c) -> Dual a -> Dual b -> Dual c Source #(*>) :: Dual a -> Dual b -> Dual b Source #(<*) :: Dual a -> Dual b -> Dual a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methodspure :: a -> Product a Source #(<*>) :: Product (a -> b) -> Product a -> Product b Source #liftA2 :: (a -> b -> c) -> Product a -> Product b -> Product c Source #(*>) :: Product a -> Product b -> Product b Source #(<*) :: Product a -> Product b -> Product a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methodspure :: a -> Sum a Source #(<*>) :: Sum (a -> b) -> Sum a -> Sum b Source #liftA2 :: (a -> b -> c) -> Sum a -> Sum b -> Sum c Source #(*>) :: Sum a -> Sum b -> Sum b Source #(<*) :: Sum a -> Sum b -> Sum a Source # Source # f <$> ZipList xs1 <*> ... <*> ZipList xsN = ZipList (zipWithN f xs1 ... xsN)where zipWithN refers to the zipWith function of the appropriate arity (zipWith, zipWith3, zipWith4, ...). For example:(\a b c -> stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..] = ZipList (zipWith3 (\a b c -> stimes c [a, b]) "abcd" "567" [1..]) = ZipList {getZipList = ["a5","b6b6","c7c7c7"]}Since: base-2.1 Instance detailsDefined in GHC.Internal.Functor.ZipList Methodspure :: a -> ZipList a Source #(<*>) :: ZipList (a -> b) -> ZipList a -> ZipList b Source #liftA2 :: (a -> b -> c) -> ZipList a -> ZipList b -> ZipList c Source #(*>) :: ZipList a -> ZipList b -> ZipList b Source #(<*) :: ZipList a -> ZipList b -> ZipList a Source # Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.GHCi Methodspure :: a -> NoIO a Source #(<*>) :: NoIO (a -> b) -> NoIO a -> NoIO b Source #liftA2 :: (a -> b -> c) -> NoIO a -> NoIO b -> NoIO c Source #(*>) :: NoIO a -> NoIO b -> NoIO b Source #(<*) :: NoIO a -> NoIO b -> NoIO a Source # Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> Par1 a Source #(<*>) :: Par1 (a -> b) -> Par1 a -> Par1 b Source #liftA2 :: (a -> b -> c) -> Par1 a -> Par1 b -> Par1 c Source #(*>) :: Par1 a -> Par1 b -> Par1 b Source #(<*) :: Par1 a -> Par1 b -> Par1 a Source # Source # Instance detailsDefined in GHC.Internal.TH.Syntax Methodspure :: a -> Q a Source #(<*>) :: Q (a -> b) -> Q a -> Q b Source #liftA2 :: (a -> b -> c) -> Q a -> Q b -> Q c Source #(*>) :: Q a -> Q b -> Q b Source #(<*) :: Q a -> Q b -> Q a Source # Source # Since: base-4.6.0.0 Instance details Methodspure :: a -> ReadP a Source #(<*>) :: ReadP (a -> b) -> ReadP a -> ReadP b Source #liftA2 :: (a -> b -> c) -> ReadP a -> ReadP b -> ReadP c Source #(*>) :: ReadP a -> ReadP b -> ReadP b Source #(<*) :: ReadP a -> ReadP b -> ReadP a Source # Source # Since: base-4.6.0.0 Instance details Methodspure :: a -> ReadPrec a Source #(<*>) :: ReadPrec (a -> b) -> ReadPrec a -> ReadPrec b Source #liftA2 :: (a -> b -> c) -> ReadPrec a -> ReadPrec b -> ReadPrec c Source #(*>) :: ReadPrec a -> ReadPrec b -> ReadPrec b Source #(<*) :: ReadPrec a -> ReadPrec b -> ReadPrec a Source # Source # Since: base-2.1 Instance detailsDefined in GHC.Internal.Base Methodspure :: a -> IO a Source #(<*>) :: IO (a -> b) -> IO a -> IO b Source #liftA2 :: (a -> b -> c) -> IO a -> IO b -> IO c Source #(*>) :: IO a -> IO b -> IO b Source #(<*) :: IO a -> IO b -> IO a Source # Source # Since: base-2.1 Instance detailsDefined in GHC.Internal.Base Methodspure :: a -> Maybe a Source #(<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b Source #liftA2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c Source #(*>) :: Maybe a -> Maybe b -> Maybe b Source #(<*) :: Maybe a -> Maybe b -> Maybe a Source # Source # Since: base-4.15 Instance detailsDefined in GHC.Internal.Base Methodspure :: 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 # Source # Since: base-2.1 Instance detailsDefined in GHC.Internal.Base Methodspure :: a -> [a] Source #(<*>) :: [a -> b] -> [a] -> [b] Source #liftA2 :: (a -> b -> c) -> [a] -> [b] -> [c] Source #(*>) :: [a] -> [b] -> [b] Source #(<*) :: [a] -> [b] -> [a] Source # Arrow a => Applicative (ArrowMonad a) Source # Since: base-4.6.0.0 Instance detailsDefined in GHC.Internal.Control.Arrow Methodspure :: a0 -> ArrowMonad a a0 Source #(<*>) :: ArrowMonad a (a0 -> b) -> ArrowMonad a a0 -> ArrowMonad a b Source #liftA2 :: (a0 -> b -> c) -> ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a c Source #(*>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b Source #(<*) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a a0 Source # Source # Since: base-2.1 Instance detailsDefined in GHC.Internal.Control.Monad.ST.Lazy.Imp Methodspure :: a -> ST s a Source #(<*>) :: ST s (a -> b) -> ST s a -> ST s b Source #liftA2 :: (a -> b -> c) -> ST s a -> ST s b -> ST s c Source #(*>) :: ST s a -> ST s b -> ST s b Source #(<*) :: ST s a -> ST s b -> ST s a Source # Source # Since: base-3.0 Instance detailsDefined in GHC.Internal.Data.Either Methodspure :: a -> Either e a Source #(<*>) :: Either e (a -> b) -> Either e a -> Either e b Source #liftA2 :: (a -> b -> c) -> Either e a -> Either e b -> Either e c Source #(*>) :: Either e a -> Either e b -> Either e b Source #(<*) :: Either e a -> Either e b -> Either e a Source # Source # Since: base-4.0 Instance detailsDefined in GHC.Internal.Data.Functor.Utils Methodspure :: a -> StateL s a Source #(<*>) :: StateL s (a -> b) -> StateL s a -> StateL s b Source #liftA2 :: (a -> b -> c) -> StateL s a -> StateL s b -> StateL s c Source #(*>) :: StateL s a -> StateL s b -> StateL s b Source #(<*) :: StateL s a -> StateL s b -> StateL s a Source # Source # Since: base-4.0 Instance detailsDefined in GHC.Internal.Data.Functor.Utils Methodspure :: a -> StateR s a Source #(<*>) :: StateR s (a -> b) -> StateR s a -> StateR s b Source #liftA2 :: (a -> b -> c) -> StateR s a -> StateR s b -> StateR s c Source #(*>) :: StateR s a -> StateR s b -> StateR s b Source #(<*) :: StateR s a -> StateR s b -> StateR s a Source # Applicative (Proxy :: Type -> Type) Source # Since: base-4.7.0.0 Instance detailsDefined in GHC.Internal.Data.Proxy Methodspure :: a -> Proxy a Source #(<*>) :: Proxy (a -> b) -> Proxy a -> Proxy b Source #liftA2 :: (a -> b -> c) -> Proxy a -> Proxy b -> Proxy c Source #(*>) :: Proxy a -> Proxy b -> Proxy b Source #(<*) :: Proxy a -> Proxy b -> Proxy a Source # Applicative (U1 :: Type -> Type) Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> U1 a Source #(<*>) :: U1 (a -> b) -> U1 a -> U1 b Source #liftA2 :: (a -> b -> c) -> U1 a -> U1 b -> U1 c Source #(*>) :: U1 a -> U1 b -> U1 b Source #(<*) :: U1 a -> U1 b -> U1 a Source # Source # Since: base-4.4.0.0 Instance detailsDefined in GHC.Internal.ST Methodspure :: a -> ST s a Source #(<*>) :: ST s (a -> b) -> ST s a -> ST s b Source #liftA2 :: (a -> b -> c) -> ST s a -> ST s b -> ST s c Source #(*>) :: ST s a -> ST s b -> ST s b Source #(<*) :: ST s a -> ST s b -> ST s a Source # Monoid a => Applicative ((,) a) Source # For tuples, the Monoid constraint on a determines how the first values merge. For example, Strings concatenate:("hello ", (+15)) <*> ("world!", 2002) ("hello world!",2017)Since: base-2.1 Instance detailsDefined in GHC.Internal.Base Methodspure :: a0 -> (a, a0) Source #(<*>) :: (a, a0 -> b) -> (a, a0) -> (a, b) Source #liftA2 :: (a0 -> b -> c) -> (a, a0) -> (a, b) -> (a, c) Source #(*>) :: (a, a0) -> (a, b) -> (a, b) Source #(<*) :: (a, a0) -> (a, b) -> (a, a0) Source # Applicative m => Applicative (Kleisli m a) Source # Since: base-4.14.0.0 Instance detailsDefined in GHC.Internal.Control.Arrow Methodspure :: a0 -> Kleisli m a a0 Source #(<*>) :: Kleisli m a (a0 -> b) -> Kleisli m a a0 -> Kleisli m a b Source #liftA2 :: (a0 -> b -> c) -> Kleisli m a a0 -> Kleisli m a b -> Kleisli m a c Source #(*>) :: Kleisli m a a0 -> Kleisli m a b -> Kleisli m a b Source #(<*) :: Kleisli m a a0 -> Kleisli m a b -> Kleisli m a a0 Source # Monoid m => Applicative (Const m :: Type -> Type) Source # Since: base-2.0.1 Instance detailsDefined in GHC.Internal.Data.Functor.Const Methodspure :: a -> Const m a Source #(<*>) :: Const m (a -> b) -> Const m a -> Const m b Source #liftA2 :: (a -> b -> c) -> Const m a -> Const m b -> Const m c Source #(*>) :: Const m a -> Const m b -> Const m b Source #(<*) :: Const m a -> Const m b -> Const m a Source # Monad m => Applicative (StateT s m) Source # Since: base-4.18.0.0 Instance detailsDefined in GHC.Internal.Data.Functor.Utils Methodspure :: a -> StateT s m a Source #(<*>) :: StateT s m (a -> b) -> StateT s m a -> StateT s m b Source #liftA2 :: (a -> b -> c) -> StateT s m a -> StateT s m b -> StateT s m c Source #(*>) :: StateT s m a -> StateT s m b -> StateT s m b Source #(<*) :: StateT s m a -> StateT s m b -> StateT s m a Source # Applicative f => Applicative (Ap f) Source # Since: base-4.12.0.0 Instance detailsDefined in GHC.Internal.Data.Monoid Methodspure :: a -> Ap f a Source #(<*>) :: Ap f (a -> b) -> Ap f a -> Ap f b Source #liftA2 :: (a -> b -> c) -> Ap f a -> Ap f b -> Ap f c Source #(*>) :: Ap f a -> Ap f b -> Ap f b Source #(<*) :: Ap f a -> Ap f b -> Ap f a Source # Applicative f => Applicative (Alt f) Source # Since: base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methodspure :: a -> Alt f a Source #(<*>) :: Alt f (a -> b) -> Alt f a -> Alt f b Source #liftA2 :: (a -> b -> c) -> Alt f a -> Alt f b -> Alt f c Source #(*>) :: Alt f a -> Alt f b -> Alt f b Source #(<*) :: Alt f a -> Alt f b -> Alt f a Source # (Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f) Source # Since: base-4.17.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> Generically1 f a Source #(<*>) :: Generically1 f (a -> b) -> Generically1 f a -> Generically1 f b Source #liftA2 :: (a -> b -> c) -> Generically1 f a -> Generically1 f b -> Generically1 f c Source #(*>) :: Generically1 f a -> Generically1 f b -> Generically1 f b Source #(<*) :: Generically1 f a -> Generically1 f b -> Generically1 f a Source # Applicative f => Applicative (Rec1 f) Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> Rec1 f a Source #(<*>) :: Rec1 f (a -> b) -> Rec1 f a -> Rec1 f b Source #liftA2 :: (a -> b -> c) -> Rec1 f a -> Rec1 f b -> Rec1 f c Source #(*>) :: Rec1 f a -> Rec1 f b -> Rec1 f b Source #(<*) :: Rec1 f a -> Rec1 f b -> Rec1 f a Source # (Monoid a, Monoid b) => Applicative ((,,) a b) Source # Since: base-4.14.0.0 Instance detailsDefined in GHC.Internal.Base Methodspure :: a0 -> (a, b, a0) Source #(<*>) :: (a, b, a0 -> b0) -> (a, b, a0) -> (a, b, b0) Source #liftA2 :: (a0 -> b0 -> c) -> (a, b, a0) -> (a, b, b0) -> (a, b, c) Source #(*>) :: (a, b, a0) -> (a, b, b0) -> (a, b, b0) Source #(<*) :: (a, b, a0) -> (a, b, b0) -> (a, b, a0) Source # (Applicative f, Applicative g) => Applicative (f :*: g) Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> (f :*: g) a Source #(<*>) :: (f :*: g) (a -> b) -> (f :*: g) a -> (f :*: g) b Source #liftA2 :: (a -> b -> c) -> (f :*: g) a -> (f :*: g) b -> (f :*: g) c Source #(*>) :: (f :*: g) a -> (f :*: g) b -> (f :*: g) b Source #(<*) :: (f :*: g) a -> (f :*: g) b -> (f :*: g) a Source # Monoid c => Applicative (K1 i c :: Type -> Type) Source # Since: base-4.12.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> K1 i c a Source #(<*>) :: K1 i c (a -> b) -> K1 i c a -> K1 i c b Source #liftA2 :: (a -> b -> c0) -> K1 i c a -> K1 i c b -> K1 i c c0 Source #(*>) :: K1 i c a -> K1 i c b -> K1 i c b Source #(<*) :: K1 i c a -> K1 i c b -> K1 i c a Source # (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) Source # Since: base-4.14.0.0 Instance detailsDefined in GHC.Internal.Base Methodspure :: a0 -> (a, b, c, a0) Source #(<*>) :: (a, b, c, a0 -> b0) -> (a, b, c, a0) -> (a, b, c, b0) Source #liftA2 :: (a0 -> b0 -> c0) -> (a, b, c, a0) -> (a, b, c, b0) -> (a, b, c, c0) Source #(*>) :: (a, b, c, a0) -> (a, b, c, b0) -> (a, b, c, b0) Source #(<*) :: (a, b, c, a0) -> (a, b, c, b0) -> (a, b, c, a0) Source # Applicative ((->) r) Source # Since: base-2.1 Instance detailsDefined in GHC.Internal.Base Methodspure :: a -> r -> a Source #(<*>) :: (r -> (a -> b)) -> (r -> a) -> r -> b Source #liftA2 :: (a -> b -> c) -> (r -> a) -> (r -> b) -> r -> c Source #(*>) :: (r -> a) -> (r -> b) -> r -> b Source #(<*) :: (r -> a) -> (r -> b) -> r -> a Source # (Applicative f, Applicative g) => Applicative (f :.: g) Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> (f :.: g) a Source #(<*>) :: (f :.: g) (a -> b) -> (f :.: g) a -> (f :.: g) b Source #liftA2 :: (a -> b -> c) -> (f :.: g) a -> (f :.: g) b -> (f :.: g) c Source #(*>) :: (f :.: g) a -> (f :.: g) b -> (f :.: g) b Source #(<*) :: (f :.: g) a -> (f :.: g) b -> (f :.: g) a Source # Applicative f => Applicative (M1 i c f) Source # Since: base-4.9.0.0 Instance detailsDefined in GHC.Internal.Generics Methodspure :: a -> M1 i c f a Source #(<*>) :: M1 i c f (a -> b) -> M1 i c f a -> M1 i c f b Source #liftA2 :: (a -> b -> c0) -> M1 i c f a -> M1 i c f b -> M1 i c f c0 Source #(*>) :: M1 i c f a -> M1 i c f b -> M1 i c f b Source #(<*) :: M1 i c f a -> M1 i c f b -> M1 i c f a Source #

class Functor (f :: Type -> Type) where Source #

A type f is a Functor if it provides a function fmap which, given any types a and b lets you apply any function from (a -> b) to turn an f a into an f b, preserving the structure of f. Furthermore f needs to adhere to the following:

Identity
fmap id == id
Composition
fmap (f . g) == fmap f . fmap g

Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.

Minimal complete definition

fmap

Methods

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

fmap is used to apply a function of type (a -> b) to a value of type f a, where f is a functor, to produce a value of type f b. Note that for any type constructor with more than one parameter (e.g., Either), only the last type parameter can be modified with fmap (e.g., b in Either a b).

Some type constructors with two parameters or more have a Bifunctor instance that allows both the last and the penultimate parameters to be mapped over.

#### Examples

Expand

Convert from a Maybe Int to a Maybe String using show:

>>> fmap show Nothing
Nothing
>>> fmap show (Just 3)
Just "3"

Convert from an Either Int Int to an Either Int String using show:

>>> fmap show (Left 17)
Left 17
>>> fmap show (Right 17)
Right "17"

Double each element of a list:

>>> fmap (*2) [1,2,3]
[2,4,6]

Apply even to the second element of a pair:

>>> fmap even (2,2)
(2,True)

It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over with fmap).

It explains why fmap can be used with tuples containing values of different types as in the following example:

>>> fmap even ("hello", 1.0, 4)
("hello",1.0,True)

Just 'a'