Copyright | (c) The University of Glasgow 1992-2002 |
---|---|
License | see libraries/base/LICENSE |
Maintainer | cvs-ghc@haskell.org |
Stability | internal |
Portability | non-portable (GHC extensions) |
Safe Haskell | Unsafe |
Language | Haskell2010 |
GHC.Base
Description
Basic data types and classes.
Synopsis
- data NonEmpty a = a :| [a]
- class Applicative m => Monad m where
- class Functor f => Applicative f where
- class Applicative f => Alternative f where
- class Functor f where
- class (Alternative m, Monad m) => MonadPlus m where
- type String = [Char]
- class Semigroup a => Monoid a where
- class Semigroup a where
- data Opaque = forall a. O a
- ord :: Char -> Int
- (.) :: (b -> c) -> (a -> b) -> a -> c
- (<**>) :: 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
- id :: a -> a
- when :: Applicative f => Bool -> f () -> f ()
- assert :: Bool -> a -> a
- mapM :: Monad m => (a -> m b) -> [a] -> m [b]
- sequence :: Monad m => [m a] -> m [a]
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- join :: Monad m => m (m a) -> m a
- 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
- failIO :: String -> IO a
- ($) :: forall r a (b :: TYPE r). (a -> b) -> a -> b
- otherwise :: Bool
- foldr :: (a -> b -> b) -> b -> [a] -> b
- const :: a -> b -> a
- flip :: (a -> b -> c) -> b -> a -> c
- (++) :: [a] -> [a] -> [a]
- map :: (a -> b) -> [a] -> [b]
- ($!) :: forall r a (b :: TYPE r). (a -> b) -> a -> b
- shiftL# :: Word# -> Int# -> Word#
- shiftRL# :: Word# -> Int# -> Word#
- iShiftL# :: Int# -> Int# -> Int#
- iShiftRA# :: Int# -> Int# -> Int#
- iShiftRL# :: Int# -> Int# -> Int#
- build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
- augment :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a] -> [a]
- breakpoint :: a -> a
- breakpointCond :: Bool -> a -> a
- unIO :: IO a -> State# RealWorld -> (# State# RealWorld, a #)
- until :: (a -> Bool) -> (a -> a) -> a -> a
- asTypeOf :: a -> a -> a
- eqString :: String -> String -> Bool
- returnIO :: a -> IO a
- bindIO :: IO a -> (a -> IO b) -> IO b
- thenIO :: IO a -> IO b -> IO b
- mapFB :: (elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst
- unsafeChr :: Int -> Char
- maxInt :: Int
- minInt :: Int
- getTag :: 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#
- module GHC.Classes
- module GHC.CString
- module GHC.Magic
- module GHC.Magic.Dict
- module GHC.Types
- module GHC.Prim
- module GHC.Prim.Ext
- module GHC.Prim.PtrEq
- module GHC.Err
- module GHC.Maybe
Documentation
Non-empty (and non-strict) list type.
Since: base-4.9.0.0
Constructors
Instances
Instances details
Instance details
Defined in Data.Foldable
Methods
fold :: Monoid m => NonEmpty m -> m Source #
foldMap :: Monoid m => (a -> m) -> NonEmpty a -> m Source #
foldMap' :: Monoid m => (a -> m) -> NonEmpty a -> m Source #
foldr :: (a -> b -> b) -> b -> NonEmpty a -> b Source #
foldr' :: (a -> b -> b) -> b -> NonEmpty a -> b Source #
foldl :: (b -> a -> b) -> b -> NonEmpty a -> b Source #
foldl' :: (b -> a -> b) -> b -> NonEmpty a -> b Source #
foldr1 :: (a -> a -> a) -> NonEmpty a -> a Source #
foldl1 :: (a -> a -> a) -> NonEmpty a -> a Source #
toList :: NonEmpty a -> [a] Source #
null :: NonEmpty a -> Bool Source #
length :: NonEmpty a -> Int Source #
elem :: Eq a => a -> NonEmpty a -> Bool Source #
maximum :: Ord a => NonEmpty a -> a Source #
minimum :: Ord a => NonEmpty a -> a Source #
Instance details
Defined in Data.Functor.Classes
Instance details
Defined in Data.Functor.Classes
Methods
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (NonEmpty a) Source #
liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [NonEmpty a] Source #
liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (NonEmpty a) Source #
liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [NonEmpty a] Source #
Instance details
Defined in Data.Traversable
Instance details
Defined in GHC.Base
Instance details
Defined in Data.Data
Methods
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> NonEmpty a -> c (NonEmpty a) Source #
gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (NonEmpty a) Source #
toConstr :: NonEmpty a -> Constr Source #
dataTypeOf :: NonEmpty a -> DataType Source #
dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (NonEmpty a)) Source #
dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (NonEmpty a)) Source #
gmapT :: (forall b. Data b => b -> b) -> NonEmpty a -> NonEmpty a Source #
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> NonEmpty a -> r Source #
gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> NonEmpty a -> r Source #
gmapQ :: (forall d. Data d => d -> u) -> NonEmpty a -> [u] Source #
gmapQi :: Int -> (forall d. Data d => d -> u) -> NonEmpty a -> u Source #
gmapM :: Monad m => (forall d. Data d => d -> m d) -> NonEmpty a -> m (NonEmpty a) Source #
gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> NonEmpty a -> m (NonEmpty a) Source #
gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> NonEmpty a -> m (NonEmpty a) Source #
Instance details
Defined in GHC.Base
Methods
compare :: NonEmpty a -> NonEmpty a -> Ordering Source #
(<) :: NonEmpty a -> NonEmpty a -> Bool Source #
(<=) :: NonEmpty a -> NonEmpty a -> Bool Source #
(>) :: NonEmpty a -> NonEmpty a -> Bool Source #
(>=) :: NonEmpty a -> NonEmpty a -> Bool Source #
Instance details
Defined in GHC.Generics
Instance details
Defined in GHC.Generics
class Applicative m => Monad m 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
return
a>>=
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 lists, Maybe
and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Methods
(>>=) :: forall a b. 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
(>>) :: forall a b. 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
Inject a value into the monadic type.
Instances
Instances details
Instance details
Defined in Control.Applicative
Methods
(>>=) :: WrappedMonad m a -> (a -> WrappedMonad m b) -> WrappedMonad m b Source #
(>>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b Source #
return :: a -> WrappedMonad m a Source #
Instance details
Defined in 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 #
class Functor f => Applicative f where Source #
A functor with application, providing operations to
- embed pure expressions (
pure
), and - sequence computations and combine their results (
<*>
andliftA2
).
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).
Methods
Lift a value.
(<*>) :: 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)
(*>) :: 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
>>>
import 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
Instances details
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 details
Defined in Control.Applicative
Instance details
Defined in Data.Complex
Instance details
Defined in Data.Functor.Identity
Instance details
Defined in Data.Semigroup.Internal
Instance details
Defined in Text.ParserCombinators.ReadP
Instance details
Defined in Text.ParserCombinators.ReadPrec
Instance details
Defined in GHC.Base
Instance details
Defined in Control.Applicative
Methods
pure :: a -> WrappedMonad m a Source #
(<*>) :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b Source #
liftA2 :: (a -> b -> c) -> WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m c Source #
(*>) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m b Source #
(<*) :: WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m a Source #
Instance details
Defined in 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 #
Instance details
Defined in Data.Either
For tuples, the Monoid
constraint on a
determines
how the first values merge.
For example, String
s concatenate:
("hello ", (+15)) <*> ("world!", 2002) ("hello world!",2017)
Since: base-2.1
Instance details
Defined in Control.Applicative
Methods
pure :: a0 -> WrappedArrow a b a0 Source #
(<*>) :: WrappedArrow a b (a0 -> b0) -> WrappedArrow a b a0 -> WrappedArrow a b b0 Source #
liftA2 :: (a0 -> b0 -> c) -> WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b c Source #
(*>) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b b0 Source #
(<*) :: WrappedArrow a b a0 -> WrappedArrow a b b0 -> WrappedArrow a b a0 Source #
Instance details
Defined in 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 #
Instance details
Defined in Data.Functor.Const
Instance details
Defined in Data.Semigroup.Internal
Instance details
Defined in GHC.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 #
Instance details
Defined in GHC.Generics
Instance details
Defined in GHC.Base
Instance details
Defined in Data.Functor.Product
Methods
pure :: a -> Product f g a Source #
(<*>) :: Product f g (a -> b) -> Product f g a -> Product f g b Source #
liftA2 :: (a -> b -> c) -> Product f g a -> Product f g b -> Product f g c Source #
(*>) :: Product f g a -> Product f g b -> Product f g b Source #
(<*) :: Product f g a -> Product f g b -> Product f g a Source #
Instance details
Defined in GHC.Generics
Instance details
Defined in GHC.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 #
Instance details
Defined in Data.Functor.Compose
Methods
pure :: a -> Compose f g a Source #
(<*>) :: Compose f g (a -> b) -> Compose f g a -> Compose f g b Source #
liftA2 :: (a -> b -> c) -> Compose f g a -> Compose f g b -> Compose f g c Source #
(*>) :: Compose f g a -> Compose f g b -> Compose f g b Source #
(<*) :: Compose f g a -> Compose f g b -> Compose f g a Source #
Instance details
Defined in GHC.Generics
class Applicative f => Alternative f where Source #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
Methods
The identity of <|>
(<|>) :: f a -> f a -> f a infixl 3 Source #
An associative binary operation
One or more.
Zero or more.
Instances
Instances details
Instance details
Defined in Control.Applicative
Methods
empty :: WrappedMonad m a Source #
(<|>) :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a Source #
some :: WrappedMonad m a -> WrappedMonad m [a] Source #
many :: WrappedMonad m a -> WrappedMonad m [a] Source #
Instance details
Defined in 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 #
Instance details
Defined in Control.Applicative
Methods
empty :: WrappedArrow a b a0 Source #
(<|>) :: WrappedArrow a b a0 -> WrappedArrow a b a0 -> WrappedArrow a b a0 Source #
some :: WrappedArrow a b a0 -> WrappedArrow a b [a0] Source #
many :: WrappedArrow a b a0 -> WrappedArrow a b [a0] Source #
Instance details
Defined in GHC.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 #
class Functor f 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 https://www.schoolofhaskell.com/user/edwardk/snippets/fmap or
https://github.com/quchen/articles/blob/master/second_functor_law.md
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
Expand
Convert from a
to a Maybe
IntMaybe String
using show
:
>>>
fmap show Nothing
Nothing>>>
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
Instances details
Instance details
Defined in Control.Applicative
Methods
fmap :: (a -> b) -> WrappedMonad m a -> WrappedMonad m b Source #
(<$) :: a -> WrappedMonad m b -> WrappedMonad m a Source #
Instance details
Defined in Control.Arrow
Methods
fmap :: (a0 -> b) -> ArrowMonad a a0 -> ArrowMonad a b Source #
(<$) :: a0 -> ArrowMonad a b -> ArrowMonad a a0 Source #
Instance details
Defined in Control.Applicative
Methods
fmap :: (a0 -> b0) -> WrappedArrow a b a0 -> WrappedArrow a b b0 Source #
(<$) :: a0 -> WrappedArrow a b b0 -> WrappedArrow a b a0 Source #
Instance details
Defined in GHC.Generics
Methods
fmap :: (a -> b) -> Generically1 f a -> Generically1 f b Source #
(<$) :: a -> Generically1 f b -> Generically1 f a Source #
class (Alternative m, Monad m) => MonadPlus m 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
Instances details
Instance details
Defined in Control.Arrow
Methods
mzero :: ArrowMonad a a0 Source #
mplus :: ArrowMonad a a0 -> ArrowMonad a a0 -> ArrowMonad a a0 Source #
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)<>
zSemigroup
law)- Concatenation
mconcat
=foldr
(<>
)mempty
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 newtype
s and make those instances
of Monoid
, e.g. Sum
and Product
.
NOTE: Semigroup
is a superclass of Monoid
since base-4.11.0.0.
Minimal complete definition
Methods
Identity of mappend
>>>
"Hello world" <> mempty
"Hello world"
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
Instances details
This constraint is arguably too strong. However,
as some types (such as Natural
) have undefined complement
, this is the
only safe choice.
Since: base-4.16
This constraint is arguably
too strong. However, as some types (such as Natural
) have undefined
complement
, this is the only safe choice.
Since: base-4.16
on comparisons always returns mempty
EQ
. Without
newtypes this equals
.pure
(pure
EQ)
mempty :: Comparison a mempty = Comparison _ _ -> EQ
Instance details
Defined in Data.Functor.Contravariant
Methods
mempty :: Comparison a Source #
mappend :: Comparison a -> Comparison a -> Comparison a Source #
mconcat :: [Comparison a] -> Comparison a Source #
on equivalences always returns mempty
True
. Without
newtypes this equals
.pure
(pure
True)
mempty :: Equivalence a mempty = Equivalence _ _ -> True
Instance details
Defined in Data.Functor.Contravariant
Methods
mempty :: Equivalence a Source #
mappend :: Equivalence a -> Equivalence a -> Equivalence a Source #
mconcat :: [Equivalence a] -> Equivalence a Source #
on predicates always returns mempty
True
. Without
newtypes this equals
.pure
True
mempty :: Predicate a mempty = _ -> True
Instance details
Defined in Data.Semigroup
Methods
mempty :: WrappedMonoid m Source #
mappend :: WrappedMonoid m -> WrappedMonoid m -> WrappedMonoid m Source #
mconcat :: [WrappedMonoid m] -> WrappedMonoid m Source #
Instance details
Defined in GHC.Generics
Methods
mempty :: Generically a Source #
mappend :: Generically a -> Generically a -> Generically a Source #
mconcat :: [Generically a] -> Generically a Source #
Lift a semigroup into Maybe
forming a Monoid
according to
http://en.wikipedia.org/wiki/Monoid: "Any semigroup S
may be
turned into a monoid simply by adjoining an element e
not in S
and defining e*e = e
and e*s = s = s*e
for all s ∈ S
."
Since 4.11.0: constraint on inner a
value generalised from
Monoid
to Semigroup
.
Since: base-2.1
without newtypes is mempty
@(Op a b)mempty @(b->a)
= _ -> mempty
.
mempty :: Op a b mempty = Op _ -> mempty
class Semigroup a where Source #
The class of semigroups (types with an associative binary operation).
Instances should satisfy the following:
Since: base-4.9.0.0
Minimal complete definition
Methods
(<>) :: a -> a -> a infixr 6 Source #
An associative operation.
>>>
[1,2,3] <> [4,5,6]
[1,2,3,4,5,6]
sconcat :: NonEmpty a -> a Source #
Reduce a non-empty list with <>
The default definition should be sufficient, but this can be overridden for efficiency.
>>>
import Data.List.NonEmpty (NonEmpty (..))
>>>
sconcat $ "Hello" :| [" ", "Haskell", "!"]
"Hello Haskell!"
stimes :: Integral b => b -> a -> a Source #
Repeat a value n
times.
Given that this works on a Semigroup
it is allowed to fail if
you request 0 or fewer repetitions, and the default definition
will do so.
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 stimesIdempotent
stimes =
respectively.stimesIdempotentMonoid
>>>
stimes 4 [1]
[1,1,1,1]
Instances
Instances details
This constraint is arguably
too strong. However, as some types (such as Natural
) have undefined
complement
, this is the only safe choice.
Since: base-4.16
(
on comparisons combines results with <>
)(
. Without newtypes this equals <>
)
@Ordering
.liftA2
(liftA2
(<>
))
(<>) :: Comparison a -> Comparison a -> Comparison a Comparison cmp <> Comparison cmp' = Comparison a a' -> cmp a a' <> cmp a a'
Instance details
Defined in Data.Functor.Contravariant
Methods
(<>) :: Comparison a -> Comparison a -> Comparison a Source #
sconcat :: NonEmpty (Comparison a) -> Comparison a Source #
stimes :: Integral b => b -> Comparison a -> Comparison a Source #
(
on equivalences uses logical conjunction <>
)(
on the results. Without newtypes this equals &&
)
.liftA2
(liftA2
(&&))
(<>) :: Equivalence a -> Equivalence a -> Equivalence a Equivalence equiv <> Equivalence equiv' = Equivalence a b -> equiv a b && equiv' a b
Instance details
Defined in Data.Functor.Contravariant
Methods
(<>) :: Equivalence a -> Equivalence a -> Equivalence a Source #
sconcat :: NonEmpty (Equivalence a) -> Equivalence a Source #
stimes :: Integral b => b -> Equivalence a -> Equivalence a Source #
(
on predicates uses logical conjunction <>
)(
on
the results. Without newtypes this equals &&
)
.liftA2
(&&)
(<>) :: Predicate a -> Predicate a -> Predicate a Predicate pred <> Predicate pred' = Predicate a -> pred a && pred' a
Instance details
Defined in Data.Semigroup
Methods
(<>) :: WrappedMonoid m -> WrappedMonoid m -> WrappedMonoid m Source #
sconcat :: NonEmpty (WrappedMonoid m) -> WrappedMonoid m Source #
stimes :: Integral b => b -> WrappedMonoid m -> WrappedMonoid m Source #
Instance details
Defined in GHC.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 #
(
without newtypes is <>
) @(Op a b)(
=
<>
) @(b->a)liftA2 (
. This lifts the <>
)Semigroup
operation
(
over the output of <>
)a
.
(<>) :: Op a b -> Op a b -> Op a b Op f <> Op g = Op a -> f a <> g a
Since: base-4.9.0.0
(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4 Source #
A variant of <*>
with the arguments reversed.
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.
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d Source #
Lift a ternary function to actions.
when :: Applicative f => Bool -> f () -> f () Source #
Conditional execution of Applicative
expressions. For example,
when debug (putStrLn "Debugging")
will output the string Debugging
if the Boolean value debug
is True
, and otherwise do nothing.
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.
sequence :: Monad m => [m a] -> m [a] Source #
Evaluate each action in the sequence from left to right, and collect the results.
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 Source #
Same as >>=
, but with the arguments interchanged.
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
Expand
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
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. For example,
liftM2 (+) [0,1] [0,2] = [0,2,1,3] liftM2 (+) (Just 1) Nothing = Nothing
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
).
($) :: forall r a (b :: TYPE r). (a -> b) -> a -> b infixr 0 Source #
Application operator. This operator is redundant, since ordinary
application (f x)
means the same as (f
. However, $
x)$
has
low, right-associative binding precedence, so it sometimes allows
parentheses to be omitted; for example:
f $ g $ h x = f (g (h x))
It is also useful in higher-order situations, such as
,
or map
($
0) xs
.zipWith
($
) fs xs
Note that (
is representation-polymorphic in its result type, so that
$
)foo
where $
Truefoo :: Bool -> Int#
is well-typed.
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)...)
const x y
always evaluates to x
, ignoring its second argument.
>>>
const 42 "hello"
42
>>>
map (const 42) [0..3]
[42,42,42,42]
flip :: (a -> b -> c) -> b -> a -> c Source #
takes its (first) two arguments in the reverse order of flip
ff
.
>>>
flip (++) "hello" "world"
"worldhello"
(++) :: [a] -> [a] -> [a] infixr 5 Source #
Append 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.
WARNING: This function takes linear time in the number of elements of the first list.
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, ...]
>>>
map (+1) [1, 2, 3]
[2,3,4]
($!) :: forall r a (b :: TYPE r). (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.
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).
iShiftRA# :: Int# -> Int# -> Int# Source #
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)
iShiftRL# :: Int# -> Int# -> Int# Source #
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)
breakpoint :: a -> a Source #
breakpointCond :: Bool -> a -> a Source #
until :: (a -> Bool) -> (a -> a) -> a -> a Source #
yields the result of applying until
p ff
until p
holds.
eqString :: String -> String -> Bool Source #
This String
equality predicate is used when desugaring
pattern-matches against strings.
Returns the tag of a constructor application; this function is used by the deriving code for Eq, Ord and Enum.
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
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
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
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
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)
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)
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
module GHC.Classes
module GHC.CString
module GHC.Magic
module GHC.Magic.Dict
module GHC.Types
module GHC.Prim
module GHC.Prim.Ext
module GHC.Prim.PtrEq
module GHC.Err
module GHC.Maybe