| Copyright | (c) The University of Glasgow 1992-2002 |
|---|---|
| License | see libraries/base/LICENSE |
| Maintainer | ghc-devs@haskell.org |
| Stability | internal |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | Unsafe |
| Language | Haskell2010 |
GHC.Internal.Base
Description
Basic data types and classes.
Synopsis
- data Void
- absurd :: Void -> a
- vacuous :: Functor f => f Void -> f a
- class Semigroup a where
- class Semigroup a => Monoid a where
- class Functor (f :: Type -> Type) where
- class Functor f => Applicative (f :: Type -> Type) where
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- liftA :: Applicative f => (a -> b) -> f a -> f b
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- thenA :: Applicative f => f a -> f b -> f b
- join :: Monad m => m (m a) -> m a
- class Applicative m => Monad (m :: Type -> Type) where
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- when :: Applicative f => Bool -> f () -> f ()
- sequence :: Monad m => [m a] -> m [a]
- mapM :: Monad m => (a -> m b) -> [a] -> m [b]
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
- liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
- ap :: Monad m => m (a -> b) -> m a -> m b
- thenM :: Monad m => m a -> m b -> m b
- class Applicative f => Alternative (f :: Type -> Type) where
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- data NonEmpty a = a :| [a]
- foldr :: (a -> b -> b) -> b -> [a] -> b
- build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
- augment :: (forall b. (a -> b -> b) -> b -> b) -> [a] -> [a]
- map :: (a -> b) -> [a] -> [b]
- mapFB :: (elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst
- (++) :: [a] -> [a] -> [a]
- otherwise :: Bool
- type String = [Char]
- unsafeChr :: Int -> Char
- ord :: Char -> Int
- eqString :: String -> String -> Bool
- minInt :: Int
- maxInt :: Int
- id :: a -> a
- assert :: Bool -> a -> a
- breakpoint :: a -> a
- breakpointCond :: Bool -> a -> a
- data Opaque = O a
- const :: a -> b -> a
- (.) :: (b -> c) -> (a -> b) -> a -> c
- flip :: (a -> b -> c) -> b -> a -> c
- ($) :: (a -> b) -> a -> b
- ($!) :: (a -> b) -> a -> b
- until :: (a -> Bool) -> (a -> a) -> a -> a
- asTypeOf :: a -> a -> a
- returnIO :: a -> IO a
- bindIO :: IO a -> (a -> IO b) -> IO b
- thenIO :: IO a -> IO b -> IO b
- failIO :: String -> IO a
- unIO :: IO a -> State# RealWorld -> (# State# RealWorld, a #)
- getTag :: forall {lev} (a :: TYPE ('BoxedRep lev)). DataToTag a => a -> Int#
- quotInt :: Int -> Int -> Int
- remInt :: Int -> Int -> Int
- divInt :: Int -> Int -> Int
- modInt :: Int -> Int -> Int
- quotRemInt :: Int -> Int -> (Int, Int)
- divModInt :: Int -> Int -> (Int, Int)
- shift_mask :: Int# -> Int# -> Int#
- shiftL# :: Word# -> Int# -> Word#
- shiftRL# :: Word# -> Int# -> Word#
- iShiftL# :: Int# -> Int# -> Int#
- iShiftRA# :: Int# -> Int# -> Int#
- iShiftRL# :: Int# -> Int# -> Int#
Documentation
>>>import GHC.Internal.Num
Uninhabited data type
Since: base-4.8.0.0
Instances
| Semigroup Void Source # | Since: base-4.9.0.0 |
| Eq Void Source # | Since: base-4.8.0.0 |
| Ord Void Source # | Since: base-4.8.0.0 |
| Data Void Source # | Since: base-4.8.0.0 |
Defined in GHC.Internal.Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Void -> c Void Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Void Source # toConstr :: Void -> Constr Source # dataTypeOf :: Void -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Void) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Void) Source # gmapT :: (forall b. Data b => b -> b) -> Void -> Void Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Void -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Void -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Void -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Void -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Void -> m Void Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Void -> m Void Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Void -> m Void Source # | |
| Exception Void Source # | Since: base-4.8.0.0 |
Defined in GHC.Internal.Exception.Type Methods toException :: Void -> SomeException Source # fromException :: SomeException -> Maybe Void Source # displayException :: Void -> String Source # backtraceDesired :: Void -> Bool Source # | |
| Generic Void Source # | |
| Ix Void Source # | Since: base-4.8.0.0 |
Defined in GHC.Internal.Ix | |
| Read Void Source # | Reading a Since: base-4.8.0.0 |
| Show Void Source # | Since: base-4.8.0.0 |
| Lift Void Source # | Since: template-haskell-2.15.0.0 |
| type Rep Void Source # | Since: base-4.8.0.0 |
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
class Semigroup a where Source #
The class of semigroups (types with an associative binary operation).
Instances should satisfy the following:
You can alternatively define sconcat instead of (<>), in which case the
laws are:
Since: base-4.9.0.0
Methods
(<>) :: a -> a -> a infixr 6 Source #
An associative operation.
Examples
>>>[1,2,3] <> [4,5,6][1,2,3,4,5,6]
>>>Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
>>>putStr "Hello, " <> putStrLn "World!"Hello, World!
sconcat :: NonEmpty a -> a Source #
Reduce a non-empty list with <>
The default definition should be sufficient, but this can be overridden for efficiency.
Examples
For the following examples, we will assume that we have:
>>>import Data.List.NonEmpty (NonEmpty (..))
>>>sconcat $ "Hello" :| [" ", "Haskell", "!"]"Hello Haskell!"
>>>sconcat $ Just [1, 2, 3] :| [Nothing, Just [4, 5, 6]]Just [1,2,3,4,5,6]
>>>sconcat $ Left 1 :| [Right 2, Left 3, Right 4]Right 2
stimes :: Integral b => b -> a -> a Source #
Repeat a value n times.
The default definition will raise an exception for a multiplier that is <= 0.
This may be overridden with an implementation that is total. For monoids
it is preferred to use stimesMonoid.
By making this a member of the class, idempotent semigroups
and monoids can upgrade this to execute in \(\mathcal{O}(1)\) by
picking stimes = or stimesIdempotentstimes =
respectively.stimesIdempotentMonoid
Examples
>>>stimes 4 [1][1,1,1,1]
>>>stimes 5 (putStr "hi!")hi!hi!hi!hi!hi!
>>>stimes 3 (Right ":)")Right ":)"
Instances
| Semigroup Void Source # | Since: base-4.9.0.0 |
| Semigroup All Source # | Since: base-4.9.0.0 |
| Semigroup Any Source # | Since: base-4.9.0.0 |
| Semigroup Event Source # | Since: base-4.10.0.0 |
| Semigroup Lifetime Source # | Since: base-4.10.0.0 |
| Semigroup ExceptionContext Source # | |
Defined in GHC.Internal.Exception.Context Methods (<>) :: ExceptionContext -> ExceptionContext -> ExceptionContext Source # sconcat :: NonEmpty ExceptionContext -> ExceptionContext Source # stimes :: Integral b => b -> ExceptionContext -> ExceptionContext Source # | |
| Semigroup Ordering Source # | Since: base-4.9.0.0 |
| Semigroup () Source # | Since: base-4.9.0.0 |
| Semigroup (NonEmpty a) Source # | Since: base-4.9.0.0 |
| Bits a => Semigroup (And a) Source # | Since: base-4.16 |
| FiniteBits a => Semigroup (Iff a) Source # | This constraint is arguably
too strong. However, as some types (such as Since: base-4.16 |
| Bits a => Semigroup (Ior a) Source # | Since: base-4.16 |
| Bits a => Semigroup (Xor a) Source # | Since: base-4.16 |
| Semigroup a => Semigroup (Identity a) Source # | Since: base-4.9.0.0 |
| Ord a => Semigroup (Max a) Source # | Since: base-4.11.0.0 |
| Ord a => Semigroup (Min a) Source # | Since: base-4.11.0.0 |
| Semigroup (First a) Source # | Since: base-4.9.0.0 |
| Semigroup (Last a) Source # | Since: base-4.9.0.0 |
| Semigroup a => Semigroup (Down a) Source # | Since: base-4.11.0.0 |
| Semigroup a => Semigroup (Dual a) Source # | Since: base-4.9.0.0 |
| Semigroup (Endo a) Source # | Since: base-4.9.0.0 |
| Num a => Semigroup (Product a) Source # | Since: base-4.9.0.0 |
| Num a => Semigroup (Sum a) Source # | Since: base-4.9.0.0 |
| (Generic a, Semigroup (Rep a ())) => Semigroup (Generically a) Source # | Since: base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods (<>) :: Generically a -> Generically a -> Generically a Source # sconcat :: NonEmpty (Generically a) -> Generically a Source # stimes :: Integral b => b -> Generically a -> Generically a Source # | |
| Semigroup p => Semigroup (Par1 p) Source # | Since: base-4.12.0.0 |
| Semigroup a => Semigroup (STM a) Source # | Since: base-4.17.0.0 |
| Semigroup a => Semigroup (Q a) Source # | Since: ghc-internal-2.17.0.0 |
| Semigroup a => Semigroup (IO a) Source # | Since: base-4.10.0.0 |
| Semigroup a => Semigroup (Maybe a) Source # | Since: base-4.9.0.0 |
| Semigroup a => Semigroup (Solo a) Source # | Since: base-4.15 |
| Semigroup [a] Source # | Since: base-4.9.0.0 |
| Semigroup a => Semigroup (ST s a) Source # | Since: base-4.23.0.0 |
| Semigroup (Either a b) Source # | Since: base-4.9.0.0 |
| Semigroup (Proxy s) Source # | Since: base-4.9.0.0 |
| Semigroup (U1 p) Source # | Since: base-4.12.0.0 |
| Semigroup (V1 p) Source # | Since: base-4.12.0.0 |
| Semigroup a => Semigroup (ST s a) Source # | Since: base-4.11.0.0 |
| (Semigroup a, Semigroup b) => Semigroup (a, b) Source # | Since: base-4.9.0.0 |
| Semigroup b => Semigroup (a -> b) Source # | Since: base-4.9.0.0 |
| Semigroup a => Semigroup (Const a b) Source # | Since: base-4.9.0.0 |
| (Applicative f, Semigroup a) => Semigroup (Ap f a) Source # | Since: base-4.12.0.0 |
| Alternative f => Semigroup (Alt f a) Source # | Since: base-4.9.0.0 |
| Semigroup (f p) => Semigroup (Rec1 f p) Source # | Since: base-4.12.0.0 |
| (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) Source # | Since: base-4.9.0.0 |
| (Semigroup (f p), Semigroup (g p)) => Semigroup ((f :*: g) p) Source # | Since: base-4.12.0.0 |
| Semigroup c => Semigroup (K1 i c p) Source # | Since: base-4.12.0.0 |
| (Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) Source # | Since: base-4.9.0.0 |
| Semigroup (f (g p)) => Semigroup ((f :.: g) p) Source # | Since: base-4.12.0.0 |
| Semigroup (f p) => Semigroup (M1 i c f p) Source # | Since: base-4.12.0.0 |
| (Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e) Source # | Since: base-4.9.0.0 |
class Semigroup a => Monoid a where Source #
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following:
- Right identity
x<>mempty= x- Left identity
mempty<>x = x- Associativity
x(<>(y<>z) = (x<>y)<>zSemigrouplaw)- Concatenation
mconcat=foldr(<>)mempty
You can alternatively define mconcat instead of mempty, in which case the
laws are:
- Unit
mconcat(purex) = x- Multiplication
mconcat(joinxss) =mconcat(fmapmconcatxss)- Subclass
mconcat(toListxs) =sconcatxs
The method names refer to the monoid of lists under concatenation, but there are many other instances.
Some types can be viewed as a monoid in more than one way,
e.g. both addition and multiplication on numbers.
In such cases we often define newtypes and make those instances
of Monoid, e.g. Sum and Product.
NOTE: Semigroup is a superclass of Monoid since base-4.11.0.0.
Methods
Identity of mappend
Examples
>>>"Hello world" <> mempty"Hello world"
>>>mempty <> [1, 2, 3][1,2,3]
mappend :: a -> a -> a Source #
An associative operation
NOTE: This method is redundant and has the default
implementation since base-4.11.0.0.
Should it be implemented manually, since mappend = (<>)mappend is a synonym for
(<>), it is expected that the two functions are defined the same
way. In a future GHC release mappend will be removed from Monoid.
Fold a list using the monoid.
For most types, the default definition for mconcat will be
used, but the function is included in the class definition so
that an optimized version can be provided for specific types.
>>>mconcat ["Hello", " ", "Haskell", "!"]"Hello Haskell!"
Instances
| Monoid All Source # | Since: base-2.1 |
| Monoid Any Source # | Since: base-2.1 |
| Monoid Event Source # | Since: base-4.4.0.0 |
| Monoid Lifetime Source # |
Since: base-4.8.0.0 |
| Monoid ExceptionContext Source # | |
Defined in GHC.Internal.Exception.Context Methods mempty :: ExceptionContext Source # mappend :: ExceptionContext -> ExceptionContext -> ExceptionContext Source # mconcat :: [ExceptionContext] -> ExceptionContext Source # | |
| Monoid Ordering Source # | Since: base-2.1 |
| Monoid () Source # | Since: base-2.1 |
| FiniteBits a => Monoid (And a) Source # | This constraint is arguably too strong. However,
as some types (such as Since: base-4.16 |
| FiniteBits a => Monoid (Iff a) Source # | This constraint is arguably
too strong. However, as some types (such as Since: base-4.16 |
| Bits a => Monoid (Ior a) Source # | Since: base-4.16 |
| Bits a => Monoid (Xor a) Source # | Since: base-4.16 |
| Monoid a => Monoid (Identity a) Source # | Since: base-4.9.0.0 |
| Ord a => Monoid (Max a) Source # | Since: base-4.8.0.0 |
| Ord a => Monoid (Min a) Source # | Since: base-4.8.0.0 |
| Monoid (First a) Source # | Since: base-2.1 |
| Monoid (Last a) Source # | Since: base-2.1 |
| Monoid a => Monoid (Down a) Source # | Since: base-4.11.0.0 |
| Monoid a => Monoid (Dual a) Source # | Since: base-2.1 |
| Monoid (Endo a) Source # | Since: base-2.1 |
| Num a => Monoid (Product a) Source # | Since: base-2.1 |
| Num a => Monoid (Sum a) Source # | Since: base-2.1 |
| (Generic a, Monoid (Rep a ())) => Monoid (Generically a) Source # | Since: base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods mempty :: Generically a Source # mappend :: Generically a -> Generically a -> Generically a Source # mconcat :: [Generically a] -> Generically a Source # | |
| Monoid p => Monoid (Par1 p) Source # | Since: base-4.12.0.0 |
| Monoid a => Monoid (STM a) Source # | Since: base-4.17.0.0 |
| Monoid a => Monoid (Q a) Source # | Since: ghc-internal-2.17.0.0 |
| Monoid a => Monoid (IO a) Source # | Since: base-4.9.0.0 |
| Semigroup a => Monoid (Maybe a) Source # | Lift a semigroup into Since 4.11.0: constraint on inner Since: base-2.1 |
| Monoid a => Monoid (Solo a) Source # | Since: base-4.15 |
| Monoid [a] Source # | Since: base-2.1 |
| Monoid a => Monoid (ST s a) Source # | Since: base-4.23.0.0 |
| Monoid (Proxy s) Source # | Since: base-4.7.0.0 |
| Monoid (U1 p) Source # | Since: base-4.12.0.0 |
| Monoid a => Monoid (ST s a) Source # | Since: base-4.11.0.0 |
| (Monoid a, Monoid b) => Monoid (a, b) Source # | Since: base-2.1 |
| Monoid b => Monoid (a -> b) Source # | Since: base-2.1 |
| Monoid a => Monoid (Const a b) Source # | Since: base-4.9.0.0 |
| (Applicative f, Monoid a) => Monoid (Ap f a) Source # | Since: base-4.12.0.0 |
| Alternative f => Monoid (Alt f a) Source # | Since: base-4.8.0.0 |
| Monoid (f p) => Monoid (Rec1 f p) Source # | Since: base-4.12.0.0 |
| (Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) Source # | Since: base-2.1 |
| (Monoid (f p), Monoid (g p)) => Monoid ((f :*: g) p) Source # | Since: base-4.12.0.0 |
| Monoid c => Monoid (K1 i c p) Source # | Since: base-4.12.0.0 |
| (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) Source # | Since: base-2.1 |
| Monoid (f (g p)) => Monoid ((f :.: g) p) Source # | Since: base-4.12.0.0 |
| Monoid (f p) => Monoid (M1 i c f p) Source # | Since: base-4.12.0.0 |
| (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) Source # | Since: base-2.1 |
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:
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
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 instance that allows
both the last and the penultimate parameters to be mapped over.Bifunctor
Examples
Convert from a to a Maybe IntMaybe String
using show:
>>>fmap show NothingNothing>>>fmap show (Just 3)Just "3"
Convert from an to an
Either Int IntEither 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)
Instances
| Functor NonEmpty Source # | Since: base-4.9.0.0 |
| Functor Handler Source # | Since: base-4.6.0.0 |
| Functor Identity Source # | Since: base-4.8.0.0 |
| Functor First Source # | Since: base-4.8.0.0 |
| Functor Last Source # | Since: base-4.8.0.0 |
| Functor Down Source # | Since: base-4.11.0.0 |
| Functor Dual Source # | Since: base-4.8.0.0 |
| Functor Product Source # | Since: base-4.8.0.0 |
| Functor Sum Source # | Since: base-4.8.0.0 |
| Functor ZipList Source # | Since: base-2.1 |
| Functor NoIO Source # | Since: base-4.8.0.0 |
| Functor Par1 Source # | Since: base-4.9.0.0 |
| Functor GenClosure Source # | |
Defined in GHC.Internal.Heap.Closures Methods fmap :: (a -> b) -> GenClosure a -> GenClosure b Source # (<$) :: a -> GenClosure b -> GenClosure a Source # | |
| Functor GenStackField Source # | |
Defined in GHC.Internal.Heap.Closures Methods fmap :: (a -> b) -> GenStackField a -> GenStackField b Source # (<$) :: a -> GenStackField b -> GenStackField a Source # | |
| Functor GenStackFrame Source # | |
Defined in GHC.Internal.Heap.Closures Methods fmap :: (a -> b) -> GenStackFrame a -> GenStackFrame b Source # (<$) :: a -> GenStackFrame b -> GenStackFrame a Source # | |
| Functor GenStgStackClosure Source # | |
Defined in GHC.Internal.Heap.Closures Methods fmap :: (a -> b) -> GenStgStackClosure a -> GenStgStackClosure b Source # (<$) :: a -> GenStgStackClosure b -> GenStgStackClosure a Source # | |
| Functor STM Source # | Since: base-4.3.0.0 |
| Functor Q Source # | |
| Functor TyVarBndr Source # | |
| Functor ReadP Source # | Since: base-2.1 |
| Functor ReadPrec Source # | Since: base-2.1 |
| Functor IO Source # | Since: base-2.1 |
| Functor Maybe Source # | Since: base-2.1 |
| Functor Solo Source # | Since: base-4.15 |
| Functor [] Source # | Since: base-2.1 |
| Functor (Array i) Source # | Since: base-2.1 |
| Arrow a => Functor (ArrowMonad a) Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow Methods fmap :: (a0 -> b) -> ArrowMonad a a0 -> ArrowMonad a b Source # (<$) :: a0 -> ArrowMonad a b -> ArrowMonad a a0 Source # | |
| Functor (ST s) Source # | Since: base-2.1 |
| Functor (Either a) Source # | Since: base-3.0 |
| Functor (StateL s) Source # | Since: base-4.0 |
| Functor (StateR s) Source # | Since: base-4.0 |
| Functor (Proxy :: Type -> Type) Source # | Since: base-4.7.0.0 |
| Functor (U1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (V1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (ST s) Source # | Since: base-2.1 |
| Functor ((,) a) Source # | Since: base-2.1 |
| Functor m => Functor (Kleisli m a) Source # | Since: base-4.14.0.0 |
| Functor (Const m :: Type -> Type) Source # | Since: base-2.1 |
| Monad m => Functor (StateT s m) Source # | Since: base-4.18.0.0 |
| Functor f => Functor (Ap f) Source # | Since: base-4.12.0.0 |
| Functor f => Functor (Alt f) Source # | Since: base-4.8.0.0 |
| (Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) Source # | Since: base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods fmap :: (a -> b) -> Generically1 f a -> Generically1 f b Source # (<$) :: a -> Generically1 f b -> Generically1 f a Source # | |
| Functor f => Functor (Rec1 f) Source # | Since: base-4.9.0.0 |
| Functor (URec (Ptr ()) :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (URec Char :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (URec Double :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (URec Float :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (URec Int :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor (URec Word :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor ((,,) a b) Source # | Since: base-4.14.0.0 |
| (Functor f, Functor g) => Functor (f :*: g) Source # | Since: base-4.9.0.0 |
| (Functor f, Functor g) => Functor (f :+: g) Source # | Since: base-4.9.0.0 |
| Functor (K1 i c :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Functor ((,,,) a b c) Source # | Since: base-4.14.0.0 |
| (Functor f, Functor g) => Functor (f :.: g) Source # | Since: base-4.9.0.0 |
| Functor f => Functor (M1 i c f) Source # | Since: base-4.9.0.0 |
| Functor ((,,,,) a b c d) Source # | Since: base-4.18.0.0 |
| Functor ((,,,,,) a b c d e) Source # | Since: base-4.18.0.0 |
| Functor ((,,,,,,) a b c d e f) Source # | Since: base-4.18.0.0 |
class Functor f => Applicative (f :: Type -> Type) where Source #
A functor with application, providing operations to
A minimal complete definition must include implementations of pure
and one of either <*> or liftA2. If it defines both, then they must behave
the same as their default definitions:
(<*>) =liftA2id
liftA2f x y = f<$>x<*>y
Further, any definition must satisfy the following:
- Identity
pureid<*>v = v- Composition
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)- Homomorphism
puref<*>purex =pure(f x)- Interchange
u
<*>purey =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
liftA2p (liftA2q u v) =liftA2f u .liftA2g v
If f is also a Monad, it should satisfy
(which implies that pure and <*> satisfy the applicative functor laws).
Methods
Lift a value into the Structure.
Examples
>>>pure 1 :: Maybe IntJust 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
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
>>>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
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 3Just 3
>>>Nothing *> Just 3Nothing
Of course a more interesting use case would be to have effectful computations instead of just returning pure values.
>>>import Data.Char>>>import GHC.Internal.Text.ParserCombinators.ReadP>>>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
| Applicative NonEmpty Source # | Since: base-4.9.0.0 |
Defined in GHC.Internal.Data.NonEmpty | |
| Applicative Identity Source # | Since: base-4.8.0.0 |
Defined in GHC.Internal.Data.Functor.Identity | |
| Applicative First Source # | Since: base-4.8.0.0 |
| Applicative Last Source # | Since: base-4.8.0.0 |
| Applicative Down Source # | Since: base-4.11.0.0 |
| Applicative Dual Source # | Since: base-4.8.0.0 |
| Applicative Product Source # | Since: base-4.8.0.0 |
Defined in GHC.Internal.Data.Semigroup.Internal | |
| Applicative Sum Source # | Since: base-4.8.0.0 |
| Applicative ZipList Source # | f <$> ZipList xs1 <*> ... <*> ZipList xsN
= ZipList (zipWithN f xs1 ... xsN)where (\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 |
Defined in GHC.Internal.Functor.ZipList | |
| Applicative NoIO Source # | Since: base-4.8.0.0 |
| Applicative Par1 Source # | Since: base-4.9.0.0 |
| Applicative STM Source # | Since: base-4.8.0.0 |
| Applicative Q Source # | |
| Applicative ReadP Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Text.ParserCombinators.ReadP | |
| Applicative ReadPrec Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Text.ParserCombinators.ReadPrec | |
| Applicative IO Source # | Since: base-2.1 |
| Applicative Maybe Source # | Since: base-2.1 |
| Applicative Solo Source # | Since: base-4.15 |
| Applicative [] Source # | Since: base-2.1 |
| Arrow a => Applicative (ArrowMonad a) Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow Methods pure :: 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 # | |
| Applicative (ST s) Source # | Since: base-2.1 |
| Applicative (Either e) Source # | Since: base-3.0 |
Defined in GHC.Internal.Data.Either | |
| Applicative (StateL s) Source # | Since: base-4.0 |
Defined in GHC.Internal.Data.Functor.Utils | |
| Applicative (StateR s) Source # | Since: base-4.0 |
Defined in GHC.Internal.Data.Functor.Utils | |
| Applicative (Proxy :: Type -> Type) Source # | Since: base-4.7.0.0 |
| Applicative (U1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Applicative (ST s) Source # | Since: base-4.4.0.0 |
| Monoid a => Applicative ((,) a) Source # | For tuples, the ("hello ", (+15)) <*> ("world!", 2002)
("hello world!",2017)Since: base-2.1 |
| Applicative m => Applicative (Kleisli m a) Source # | Since: base-4.14.0.0 |
Defined in GHC.Internal.Control.Arrow Methods pure :: 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 |
Defined in GHC.Internal.Data.Functor.Const | |
| Monad m => Applicative (StateT s m) Source # | Since: base-4.18.0.0 |
Defined in GHC.Internal.Data.Functor.Utils Methods pure :: 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 |
| Applicative f => Applicative (Alt f) Source # | Since: base-4.8.0.0 |
| (Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f) Source # | Since: base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods pure :: 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 |
| (Monoid a, Monoid b) => Applicative ((,,) a b) Source # | Since: base-4.14.0.0 |
Defined in GHC.Internal.Base | |
| (Applicative f, Applicative g) => Applicative (f :*: g) Source # | Since: base-4.9.0.0 |
Defined in GHC.Internal.Generics | |
| Monoid c => Applicative (K1 i c :: Type -> Type) Source # | Since: base-4.12.0.0 |
| (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) Source # | Since: base-4.14.0.0 |
Defined in GHC.Internal.Base Methods pure :: 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 f, Applicative g) => Applicative (f :.: g) Source # | Since: base-4.9.0.0 |
Defined in GHC.Internal.Generics | |
| Applicative f => Applicative (M1 i c f) Source # | Since: base-4.9.0.0 |
Defined in GHC.Internal.Generics | |
(<**>) :: 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
in that the effects are resolved in the order the arguments are
presented.flip (<*>)
Examples
>>>(<**>) (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]}
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.
This function can be used to define `fmap = liftA`, if Applicative is already
defined for a data type.
Examples
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.
thenA :: Applicative f => f a -> f b -> f b Source #
Sequence two Applicative actions, discarding the result of the first one.
Defined as `thenA fa fb = (id fa) <* fb`.
This can be used to explicitly define `(*>) = thenA`, which is the default definition.
Since: ghc-internal-4.23.0.0
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.
'' can be understood as the join bssdo expression
do bs <- bss bs
Examples
>>>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
class Applicative m => Monad (m :: Type -> Type) where Source #
The Monad class defines the basic operations over a monad,
a concept from a branch of mathematics known as category theory.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an abstract datatype of actions.
Haskell's do expressions provide a convenient syntax for writing
monadic expressions.
Instances of Monad should satisfy the following:
- Left identity
returna>>=k = k a- Right identity
m>>=return= m- Associativity
m>>=(\x -> k x>>=h) = (m>>=k)>>=h
Furthermore, the Monad and Applicative operations should relate as follows:
The above laws imply:
and that pure and (<*>) satisfy the applicative functor laws.
The instances of Monad for List, Maybe and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Methods
(>>=) :: m a -> (a -> m b) -> m b infixl 1 Source #
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
'as ' can be understood as the >>= bsdo expression
do a <- as bs a
An alternative name for this function is 'bind', because it is used to introduce bindings in monadic contexts, but some people may refer to it as 'flatMap', which results from it being equivalent to
\x f ->join(fmapf x) :: Monad m => m a -> (a -> m b) -> m b
which can be seen as mapping a value with
Monad m => m a -> m (m b) and then 'flattening' m (m b) to m b using join.
(>>) :: m a -> m b -> m b infixl 1 Source #
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
'as ' can be understood as the >> bsdo expression
do as bs
or in terms of as(>>=)
as >>= const bs
Inject a value into the monadic type.
This function should not be different from its default implementation
as pure. The justification for the existence of this function is
merely historic.
Instances
| Monad NonEmpty Source # | Since: base-4.9.0.0 |
| Monad Identity Source # | Since: base-4.8.0.0 |
| Monad First Source # | Since: base-4.8.0.0 |
| Monad Last Source # | Since: base-4.8.0.0 |
| Monad Down Source # | Since: base-4.11.0.0 |
| Monad Dual Source # | Since: base-4.8.0.0 |
| Monad Product Source # | Since: base-4.8.0.0 |
| Monad Sum Source # | Since: base-4.8.0.0 |
| Monad NoIO Source # | Since: base-4.4.0.0 |
| Monad Par1 Source # | Since: base-4.9.0.0 |
| Monad STM Source # | Since: base-4.3.0.0 |
| Monad Q Source # | |
| Monad ReadP Source # | Since: base-2.1 |
| Monad ReadPrec Source # | Since: base-2.1 |
| Monad IO Source # | Since: base-2.1 |
| Monad Maybe Source # | Since: base-2.1 |
| Monad Solo Source # | Since: base-4.15 |
| Monad [] Source # | Since: base-2.1 |
| ArrowApply a => Monad (ArrowMonad a) Source # | Since: base-2.1 |
Defined in GHC.Internal.Control.Arrow Methods (>>=) :: ArrowMonad a a0 -> (a0 -> ArrowMonad a b) -> ArrowMonad a b Source # (>>) :: ArrowMonad a a0 -> ArrowMonad a b -> ArrowMonad a b Source # return :: a0 -> ArrowMonad a a0 Source # | |
| Monad (ST s) Source # | Since: base-2.1 |
| Monad (Either e) Source # | Since: base-4.4.0.0 |
| Monad (Proxy :: Type -> Type) Source # | Since: base-4.7.0.0 |
| Monad (U1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Monad (ST s) Source # | Since: base-2.1 |
| Monoid a => Monad ((,) a) Source # | Since: base-4.9.0.0 |
| Monad m => Monad (Kleisli m a) Source # | Since: base-4.14.0.0 |
| Monad m => Monad (StateT s m) Source # | Since: base-4.18.0.0 |
| Monad f => Monad (Ap f) Source # | Since: base-4.12.0.0 |
| Monad f => Monad (Alt f) Source # | Since: base-4.8.0.0 |
| Monad f => Monad (Rec1 f) Source # | Since: base-4.9.0.0 |
| (Monoid a, Monoid b) => Monad ((,,) a b) Source # | Since: base-4.14.0.0 |
| (Monad f, Monad g) => Monad (f :*: g) Source # | Since: base-4.9.0.0 |
| (Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) Source # | Since: base-4.14.0.0 |
| Monad f => Monad (M1 i c f) Source # | Since: base-4.9.0.0 |
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 Source #
Same as >>=, but with the arguments interchanged.
as >>= f == f =<< as
when :: Applicative f => Bool -> f () -> f () Source #
Conditional execution of Applicative expressions. For example,
Examples
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:
sequence :: Monad m => [m a] -> m [a] Source #
Evaluate each action in the sequence from left to right, and collect the results.
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.
This function can be used to define `liftA2 = liftM2`, if Monad is already
defined for a data type.
Examples
>>>liftM2 (+) [0,1] [0,2][0,2,1,3]
>>>liftM2 (+) (Just 1) NothingNothing
>>>liftM2 (+) (+ 3) (* 2) 518
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).
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
This function can be used to define `(*) = ap`, if Monad is already
defined for a data type.
Examples
>>>pure (\x y z -> x + y * z) `ap` Just 1 `ap` Just 5 `ap` Just 10Just 51
thenM :: Monad m => m a -> m b -> m b Source #
Sequence two monadic actions, discarding the result of the first one.
Defined as `thenM ma mb = ma >>= const mb`.
This can be used to define `(*>) = thenM`.
Since: ghc-internal-4.23.0.0
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
>>>Nothing <|> Just 42Just 42
>>>[1, 2] <|> [3, 4][1,2,3,4]
>>>empty <|> print (2^15)32768
Methods
The identity of <|>
empty <|> a == a a <|> empty == a
(<|>) :: f a -> f a -> f a infixl 3 Source #
An associative binary operation
One or more.
Examples
>>>some (putStr "la")lalalalalalalalala... * goes on forever *
>>>some Nothingnothing
>>>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.
Zero or more.
Examples
>>>many (putStr "la")lalalalalalalalala... * goes on forever *
>>>many NothingJust []
>>>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
| Alternative ZipList Source # | Since: base-4.11.0.0 |
| Alternative STM Source # | Takes the first non- Since: base-4.8.0.0 |
| Alternative ReadP Source # | Since: base-4.6.0.0 |
| Alternative ReadPrec Source # | Since: base-4.6.0.0 |
| Alternative IO Source # | Takes the first non-throwing Since: base-4.9.0.0 |
| Alternative Maybe Source # | Picks the leftmost Since: base-2.1 |
| Alternative [] Source # | Combines lists by concatenation, starting from the empty list. Since: base-2.1 |
| ArrowPlus a => Alternative (ArrowMonad a) Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow Methods empty :: ArrowMonad a a0 Source # (<|>) :: ArrowMonad a a0 -> ArrowMonad a a0 -> ArrowMonad a a0 Source # some :: ArrowMonad a a0 -> ArrowMonad a [a0] Source # many :: ArrowMonad a a0 -> ArrowMonad a [a0] Source # | |
| Alternative (Proxy :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Alternative (U1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| Alternative m => Alternative (Kleisli m a) Source # | Since: base-4.14.0.0 |
| Alternative f => Alternative (Ap f) Source # | Since: base-4.12.0.0 |
| Alternative f => Alternative (Alt f) Source # | Since: base-4.8.0.0 |
| (Generic1 f, Alternative (Rep1 f)) => Alternative (Generically1 f) Source # | Since: base-4.17.0.0 |
Defined in GHC.Internal.Generics Methods empty :: Generically1 f a Source # (<|>) :: Generically1 f a -> Generically1 f a -> Generically1 f a Source # some :: Generically1 f a -> Generically1 f [a] Source # many :: Generically1 f a -> Generically1 f [a] Source # | |
| Alternative f => Alternative (Rec1 f) Source # | Since: base-4.9.0.0 |
| (Alternative f, Alternative g) => Alternative (f :*: g) Source # | Since: base-4.9.0.0 |
| (Alternative f, Applicative g) => Alternative (f :.: g) Source # | Since: base-4.9.0.0 |
| Alternative f => Alternative (M1 i c f) Source # | Since: base-4.9.0.0 |
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where Source #
Monads that also support choice and failure.
Minimal complete definition
Nothing
Methods
The identity of mplus. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
mplus :: m a -> m a -> m a Source #
An associative operation. The default definition is
mplus = (<|>)
Instances
| MonadPlus STM Source # | Takes the first non- Since: base-4.3.0.0 |
| MonadPlus ReadP Source # | Since: base-2.1 |
| MonadPlus ReadPrec Source # | Since: base-2.1 |
| MonadPlus IO Source # | Takes the first non-throwing Since: base-4.9.0.0 |
| MonadPlus Maybe Source # | Picks the leftmost Since: base-2.1 |
| MonadPlus [] Source # | Combines lists by concatenation, starting from the empty list. Since: base-2.1 |
| (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) Source # | Since: base-4.6.0.0 |
Defined in GHC.Internal.Control.Arrow Methods mzero :: ArrowMonad a a0 Source # mplus :: ArrowMonad a a0 -> ArrowMonad a a0 -> ArrowMonad a a0 Source # | |
| MonadPlus (Proxy :: Type -> Type) Source # | Since: base-4.9.0.0 |
| MonadPlus (U1 :: Type -> Type) Source # | Since: base-4.9.0.0 |
| MonadPlus m => MonadPlus (Kleisli m a) Source # | Since: base-4.14.0.0 |
| MonadPlus f => MonadPlus (Ap f) Source # | Since: base-4.12.0.0 |
| MonadPlus f => MonadPlus (Alt f) Source # | Since: base-4.8.0.0 |
| MonadPlus f => MonadPlus (Rec1 f) Source # | Since: base-4.9.0.0 |
| (MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) Source # | Since: base-4.9.0.0 |
| MonadPlus f => MonadPlus (M1 i c f) Source # | Since: base-4.9.0.0 |
Non-empty (and non-strict) list type.
Since: base-4.9.0.0
Constructors
| a :| [a] infixr 5 |
Instances
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)...)
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
>>>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]
(++) :: [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
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
>>>[1, 2, 3] ++ [4, 5, 6][1,2,3,4,5,6]
>>>[] ++ [1, 2, 3][1,2,3]
>>>[3, 2, 1] ++ [][3,2,1]
String is an alias for a list of characters.
String constants in Haskell are values of type String.
That means if you write a string literal like "hello world",
it will have the type [Char], which is the same as String.
Note: You can ask the compiler to automatically infer different types
with the -XOverloadedStrings language extension, for example
"hello world" :: Text. See IsString for more information.
Because String is just a list of characters, you can use normal list functions
to do basic string manipulation. See Data.List for operations on lists.
Performance considerations
[Char] is a relatively memory-inefficient type.
It is a linked list of boxed word-size characters, internally it looks something like:
╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭────╮
│ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ [] │
╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰────╯
v v v
'a' 'b' 'c'The String "abc" will use 5*3+1 = 16 (in general 5n+1)
words of space in memory.
Furthermore, operations like (++) (string concatenation) are O(n)
(in the left argument).
For historical reasons, the base library uses String in a lot of places
for the conceptual simplicity, but library code dealing with user-data
should use the text
package for Unicode text, or the the
bytestring package
for binary data.
eqString :: String -> String -> Bool Source #
This String equality predicate is used when desugaring
pattern-matches against strings.
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
>>>length $ filter id [True, True, False, True]3
>>>Just (Just 3) >>= idJust 3
>>>foldr id 0 [(^3), (*5), (+2)]1000
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.
breakpoint :: a -> a Source #
breakpointCond :: Bool -> a -> 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
>>>const 42 "hello"42
>>>map (const 42) [0..3][42,42,42,42]
(.) :: (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
>>>map ((*2) . length) [[], [0, 1, 2], [0]][0,6,2]
>>>foldr (.) id [(+1), (*3), (^3)] 225
>>>let (...) = (.).(.) in ((*2)...(+)) 5 1030
flip :: (a -> b -> c) -> b -> a -> c Source #
takes its (first) two arguments in the reverse order of flip ff.
flip f x y = f y x
flip . flip = id
Examples
>>>flip (++) "hello" "world""worldhello"
>>>let (.>) = flip (.) in (+1) .> show $ 5"6"
($) :: (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 specialized from ida -> 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
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->IntstrSum s =sum(mapMaybereadMaybe(wordss))
we can deploy the function application operator:
-- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum ::String->IntstrSum s =sum$mapMaybereadMaybe$wordss
($) 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)
($) 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.
until :: (a -> Bool) -> (a -> a) -> a -> a Source #
yields the result of applying until p ff until p holds.
getTag :: forall {lev} (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_mask :: Int# -> Int# -> Int# Source #
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
shiftL# :: Word# -> Int# -> Word# Source #
Shift the argument left by the specified number of bits (which must be non-negative).
shiftRL# :: Word# -> Int# -> Word# Source #
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#)
iShiftL# :: Int# -> Int# -> Int# Source #
Shift the argument left by the specified number of bits (which must be non-negative).