Safe Haskell | None |
---|---|
Language | Haskell2010 |
Language.Haskell.HGrep.Prelude
Contents
- data Bool :: *
- bool :: a -> a -> Bool -> a
- (&&) :: Bool -> Bool -> Bool
- (||) :: Bool -> Bool -> Bool
- not :: Bool -> Bool
- otherwise :: Bool
- data Char :: *
- data Integer :: *
- data Int :: *
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- data Word64 :: *
- fromIntegral :: (Integral a, Num b) => a -> b
- fromRational :: Fractional a => Rational -> a
- class Monoid a where
- (<>) :: Monoid m => m -> m -> m
- class Functor f where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- ($>) :: Functor f => f a -> b -> f b
- void :: Functor f => f a -> f ()
- with :: Functor f => f a -> (a -> b) -> f b
- class Bifunctor p where
- class Functor f => Applicative f where
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- class Applicative f => Alternative f where
- asum :: (Foldable t, Alternative f) => t (f a) -> f a
- class Applicative m => Monad m where
- join :: Monad m => m (m a) -> m a
- class (Alternative m, Monad m) => MonadPlus m where
- guard :: Alternative f => Bool -> f ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- class Monad m => MonadIO m where
- data Either a b :: * -> * -> *
- either :: (a -> c) -> (b -> c) -> Either a b -> c
- note :: a -> Maybe b -> Either a b
- data Maybe a :: * -> *
- fromMaybe :: a -> Maybe a -> a
- maybe :: b -> (a -> b) -> Maybe a -> b
- hush :: Either a b -> Maybe b
- fst :: (a, b) -> a
- snd :: (a, b) -> b
- curry :: ((a, b) -> c) -> a -> b -> c
- uncurry :: (a -> b -> c) -> (a, b) -> c
- class Enum a where
- class Eq a where
- class Read a where
- readEither :: Read a => String -> Either String a
- readMaybe :: Read a => String -> Maybe a
- class Show a where
- type ShowS = String -> String
- showString :: String -> ShowS
- class Foldable t where
- for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
- class Eq a => Ord a where
- data Ordering :: *
- comparing :: Ord a => (b -> a) -> b -> b -> Ordering
- class (Functor t, Foldable t) => Traversable t where
- for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
- traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
- id :: a -> a
- (.) :: (b -> c) -> (a -> b) -> a -> c
- ($) :: (a -> b) -> a -> b
- ($!) :: (a -> b) -> a -> b
- (&) :: a -> (a -> b) -> b
- const :: a -> b -> a
- flip :: (a -> b -> c) -> b -> a -> c
- fix :: (a -> a) -> a
- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
- seq :: a -> b -> b
- data IO a :: * -> *
- type FilePath = String
- undefined :: HasCallStack => a
- error :: HasCallStack => [Char] -> a
- trace :: [Char] -> a -> a
- traceM :: Applicative f => [Char] -> f ()
- traceIO :: [Char] -> IO ()
Primitive types
Bool
Instances
Bounded Bool | |
Enum Bool | |
Eq Bool | |
Data Bool | |
Ord Bool | |
Read Bool | |
Show Bool | |
Ix Bool | |
Generic Bool | |
Lift Bool | |
Storable Bool | |
Outputable Bool | |
Hashable Bool | |
Unbox Bool | |
IArray UArray Bool | |
SingI Bool False | |
SingI Bool True | |
Vector Vector Bool | |
MVector MVector Bool | |
SingKind Bool (KProxy Bool) | |
MArray (STUArray s) Bool (ST s) | |
type Rep Bool | |
data Sing Bool | |
data Vector Bool | |
data MVector s Bool | |
type (==) Bool a b | |
type DemoteRep Bool (KProxy Bool) | |
Case analysis for the Bool
type.
evaluates to bool
x y px
when p
is False
, and evaluates to y
when p
is True
.
This is equivalent to if p then y else x
; that is, one can
think of it as an if-then-else construct with its arguments
reordered.
Examples
Basic usage:
>>>
bool "foo" "bar" True
"bar">>>
bool "foo" "bar" False
"foo"
Confirm that
and bool
x y pif p then y else x
are
equivalent:
>>>
let p = True; x = "bar"; y = "foo"
>>>
bool x y p == if p then y else x
True>>>
let p = False
>>>
bool x y p == if p then y else x
True
Since: 4.7.0.0
Char
The character type Char
is an enumeration whose values represent
Unicode (or equivalently ISO/IEC 10646) characters (see
http://www.unicode.org/ for details). This set extends the ISO 8859-1
(Latin-1) character set (the first 256 characters), which is itself an extension
of the ASCII character set (the first 128 characters). A character literal in
Haskell has type Char
.
To convert a Char
to or from the corresponding Int
value defined
by Unicode, use toEnum
and fromEnum
from the
Enum
class respectively (or equivalently ord
and chr
).
Instances
Bounded Char | |
Enum Char | |
Eq Char | |
Data Char | |
Ord Char | |
Read Char | |
Show Char | |
Ix Char | |
Lift Char | |
Storable Char | |
Outputable Char | |
Hashable Char | |
Prim Char | |
Unbox Char | |
ErrorList Char | |
IArray UArray Char | |
Vector Vector Char | |
ConvertibleStrings String String | |
ConvertibleStrings String StrictByteString | |
ConvertibleStrings String LazyByteString | |
ConvertibleStrings String StrictText | |
ConvertibleStrings String LazyText | |
ConvertibleStrings StrictByteString String | |
ConvertibleStrings LazyByteString String | |
ConvertibleStrings StrictText String | |
ConvertibleStrings LazyText String | |
MVector MVector Char | |
KnownSymbol n => Reifies Symbol n String | |
Functor (URec Char) | |
IsString (Seq Char) | |
Foldable (URec Char) | |
Traversable (URec Char) | |
Generic1 (URec Char) | |
MArray (STUArray s) Char (ST s) | |
Eq (URec Char p) | |
Ord (URec Char p) | |
Show (URec Char p) | |
Generic (URec Char p) | |
Annotate (SourceText, FastString) | |
data URec Char | Used for marking occurrences of |
data Vector Char | |
data MVector s Char | |
type Rep1 (URec Char) | |
type Rep (URec Char p) | |
Int
A fixed-precision integer type with at least the range [-2^29 .. 2^29-1]
.
The exact range for a given implementation can be determined by using
minBound
and maxBound
from the Bounded
class.
Instances
Bounded Int | |
Enum Int | |
Eq Int | |
Integral Int | |
Data Int | |
Num Int | |
Ord Int | |
Read Int | |
Real Int | |
Show Int | |
Ix Int | |
Lift Int | |
Storable Int | |
Uniquable Int | |
Outputable Int | |
Hashable Int | |
Prim Int | |
Unbox Int | |
IArray UArray Int | |
Vector Vector Int | |
MVector MVector Int | |
Functor (URec Int) | |
Foldable (URec Int) | |
Traversable (URec Int) | |
Generic1 (URec Int) | |
Reifies * Z Int | |
MArray (STUArray s) Int (ST s) | |
Reifies * n Int => Reifies * (D n) Int | |
Reifies * n Int => Reifies * (SD n) Int | |
Reifies * n Int => Reifies * (PD n) Int | |
Eq (URec Int p) | |
Ord (URec Int p) | |
Show (URec Int p) | |
Generic (URec Int p) | |
data URec Int | Used for marking occurrences of |
data Vector Int | |
data MVector s Int | |
type Rep1 (URec Int) | |
type Rep (URec Int p) | |
8-bit signed integer type
Instances
Bounded Int8 | |
Enum Int8 | |
Eq Int8 | |
Integral Int8 | |
Data Int8 | |
Num Int8 | |
Ord Int8 | |
Read Int8 | |
Real Int8 | |
Show Int8 | |
Ix Int8 | |
Lift Int8 | |
Storable Int8 | |
Bits Int8 | |
FiniteBits Int8 | |
Hashable Int8 | |
Prim Int8 | |
Unbox Int8 | |
IArray UArray Int8 | |
Vector Vector Int8 | |
MVector MVector Int8 | |
MArray (STUArray s) Int8 (ST s) | |
data Vector Int8 | |
data MVector s Int8 | |
16-bit signed integer type
Instances
Bounded Int16 | |
Enum Int16 | |
Eq Int16 | |
Integral Int16 | |
Data Int16 | |
Num Int16 | |
Ord Int16 | |
Read Int16 | |
Real Int16 | |
Show Int16 | |
Ix Int16 | |
Lift Int16 | |
Storable Int16 | |
Bits Int16 | |
FiniteBits Int16 | |
Hashable Int16 | |
Prim Int16 | |
Unbox Int16 | |
IArray UArray Int16 | |
Vector Vector Int16 | |
MVector MVector Int16 | |
MArray (STUArray s) Int16 (ST s) | |
data Vector Int16 | |
data MVector s Int16 | |
32-bit signed integer type
Instances
Bounded Int32 | |
Enum Int32 | |
Eq Int32 | |
Integral Int32 | |
Data Int32 | |
Num Int32 | |
Ord Int32 | |
Read Int32 | |
Real Int32 | |
Show Int32 | |
Ix Int32 | |
Lift Int32 | |
Storable Int32 | |
Bits Int32 | |
FiniteBits Int32 | |
Outputable Int32 | |
Hashable Int32 | |
Prim Int32 | |
Unbox Int32 | |
IArray UArray Int32 | |
Vector Vector Int32 | |
MVector MVector Int32 | |
MArray (STUArray s) Int32 (ST s) | |
data Vector Int32 | |
data MVector s Int32 | |
64-bit signed integer type
Instances
Bounded Int64 | |
Enum Int64 | |
Eq Int64 | |
Integral Int64 | |
Data Int64 | |
Num Int64 | |
Ord Int64 | |
Read Int64 | |
Real Int64 | |
Show Int64 | |
Ix Int64 | |
Lift Int64 | |
Storable Int64 | |
Bits Int64 | |
FiniteBits Int64 | |
Outputable Int64 | |
Hashable Int64 | |
Prim Int64 | |
Unbox Int64 | |
IArray UArray Int64 | |
Vector Vector Int64 | |
MVector MVector Int64 | |
MArray (STUArray s) Int64 (ST s) | |
data Vector Int64 | |
data MVector s Int64 | |
Word
64-bit unsigned integer type
Instances
Real
fromIntegral :: (Integral a, Num b) => a -> b #
general coercion from integral types
fromRational :: Fractional a => Rational -> a #
Conversion from a Rational
(that is
).
A floating literal stands for an application of Ratio
Integer
fromRational
to a value of type Rational
, so such literals have type
(
.Fractional
a) => a
Algebraic structures
Monoid
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:
mappend mempty x = x
mappend x mempty = x
mappend x (mappend y z) = mappend (mappend x y) z
mconcat =
foldr
mappend 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
.
Methods
Identity of mappend
An associative operation
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.
Instances
Functor
The Functor
class is used for types that can be mapped over.
Instances of Functor
should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor
for lists, Maybe
and IO
satisfy these laws.
Minimal complete definition
Instances
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
An infix synonym for fmap
.
The name of this operator is an allusion to $
.
Note the similarities between their types:
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b
Whereas $
is function application, <$>
is function
application lifted over a Functor
.
Examples
Convert from a
to a Maybe
Int
using Maybe
String
show
:
>>>
show <$> Nothing
Nothing>>>
show <$> Just 3
Just "3"
Convert from an
to an Either
Int
Int
Either
Int
String
using show
:
>>>
show <$> Left 17
Left 17>>>
show <$> Right 17
Right "17"
Double each element of a list:
>>>
(*2) <$> [1,2,3]
[2,4,6]
Apply even
to the second element of a pair:
>>>
even <$> (2,2)
(2,True)
($>) :: Functor f => f a -> b -> f b infixl 4 #
Flipped version of <$
.
Examples
Replace the contents of a
with a constant Maybe
Int
String
:
>>>
Nothing $> "foo"
Nothing>>>
Just 90210 $> "foo"
Just "foo"
Replace the contents of an
with a constant
Either
Int
Int
String
, resulting in an
:Either
Int
String
>>>
Left 8675309 $> "foo"
Left 8675309>>>
Right 8675309 $> "foo"
Right "foo"
Replace each element of a list with a constant String
:
>>>
[1,2,3] $> "foo"
["foo","foo","foo"]
Replace the second element of a pair with a constant String
:
>>>
(1,2) $> "foo"
(1,"foo")
Since: 4.7.0.0
void :: Functor f => f a -> f () #
discards or ignores the result of evaluation, such
as the return value of an void
valueIO
action.
Examples
Replace the contents of a
with unit:Maybe
Int
>>>
void Nothing
Nothing>>>
void (Just 3)
Just ()
Replace the contents of an
with unit,
resulting in an Either
Int
Int
:Either
Int
'()'
>>>
void (Left 8675309)
Left 8675309>>>
void (Right 8675309)
Right ()
Replace every element of a list with unit:
>>>
void [1,2,3]
[(),(),()]
Replace the second element of a pair with unit:
>>>
void (1,2)
(1,())
Discard the result of an IO
action:
>>>
mapM print [1,2]
1 2 [(),()]>>>
void $ mapM print [1,2]
1 2
Bifunctor
Formally, the class Bifunctor
represents a bifunctor
from Hask
-> Hask
.
Intuitively it is a bifunctor where both the first and second arguments are covariant.
You can define a Bifunctor
by either defining bimap
or by
defining both first
and second
.
If you supply bimap
, you should ensure that:
bimap
id
id
≡id
If you supply first
and second
, ensure:
first
id
≡id
second
id
≡id
If you supply both, you should also ensure:
bimap
f g ≡first
f.
second
g
These ensure by parametricity:
bimap
(f.
g) (h.
i) ≡bimap
f h.
bimap
g ifirst
(f.
g) ≡first
f.
first
gsecond
(f.
g) ≡second
f.
second
g
Since: 4.8.0.0
Instances
Bifunctor Either | |
Bifunctor (,) | |
Bifunctor Arg | |
Bifunctor (K1 i) | |
Bifunctor ((,,) x1) | |
Bifunctor (Const *) | |
Functor f => Bifunctor (CofreeF f) | |
Functor f => Bifunctor (FreeF f) | |
Bifunctor (Tagged *) | |
Bifunctor (Constant *) | |
Bifunctor ((,,,) x1 x2) | |
Bifunctor ((,,,,) x1 x2 x3) | |
Bifunctor p => Bifunctor (WrappedBifunctor * * p) | |
Functor g => Bifunctor (Joker * * g) | |
Bifunctor p => Bifunctor (Flip * * p) | |
Functor f => Bifunctor (Clown * * f) | |
Bifunctor ((,,,,,) x1 x2 x3 x4) | |
(Bifunctor f, Bifunctor g) => Bifunctor (Product * * f g) | |
(Bifunctor p, Bifunctor q) => Bifunctor (Sum * * p q) | |
Bifunctor ((,,,,,,) x1 x2 x3 x4 x5) | |
(Functor f, Bifunctor p) => Bifunctor (Tannen * * * f p) | |
(Bifunctor p, Functor f, Functor g) => Bifunctor (Biff * * * * p f g) | |
Applicative
class Functor f => Applicative f where #
A functor with application, providing operations to
A minimal complete definition must include implementations of these functions satisfying the following laws:
- 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
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 #
Sequential application.
(*>) :: f a -> f b -> f b infixl 4 #
Sequence actions, discarding the value of the first argument.
(<*) :: f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
Instances
(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4 #
A variant of <*>
with the arguments reversed.
Alternative
class Applicative f => Alternative f where #
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 #
An associative binary operation
One or more.
Zero or more.
Instances
asum :: (Foldable t, Alternative f) => t (f a) -> f a #
The sum of a collection of actions, generalizing concat
.
Monad
class Applicative m => Monad m where #
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 laws:
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
(>>=) :: m a -> (a -> m b) -> m b infixl 1 #
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
(>>) :: m a -> m b -> m b infixl 1 #
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
Inject a value into the monadic type.
Fail with a message. This operation is not part of the
mathematical definition of a monad, but is invoked on pattern-match
failure in a do
expression.
As part of the MonadFail proposal (MFP), this function is moved
to its own class MonadFail
(see Control.Monad.Fail for more
details). The definition here will be removed in a future
release.
Instances
join :: Monad m => m (m a) -> m a #
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.
MonadPlus
class (Alternative m, Monad m) => MonadPlus m where #
Monads that also support choice and failure.
Methods
the identity of mplus
. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
an associative operation
Instances
MonadIO
class Monad m => MonadIO m where #
Monads in which IO
computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO
monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Minimal complete definition
Instances
MonadIO IO | |
MonadIO m => MonadIO (CatchT m) | |
MonadIO m => MonadIO (IterT m) | |
MonadIO m => MonadIO (MaybeT m) | |
MonadIO m => MonadIO (ListT m) | |
MonadIO m => MonadIO (IdentityT * m) | |
(Functor f, MonadIO m) => MonadIO (FreeT f m) | |
(Error e, MonadIO m) => MonadIO (ErrorT e m) | |
MonadIO m => MonadIO (ExceptT e m) | |
MonadIO m => MonadIO (StateT s m) | |
MonadIO m => MonadIO (StateT s m) | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
MonadIO m => MonadIO (ContT * r m) | |
MonadIO m => MonadIO (ReaderT * r m) | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
Data structures
Either
data Either a b :: * -> * -> * #
The Either
type represents values with two possibilities: a value of
type
is either Either
a b
or Left
a
.Right
b
The Either
type is sometimes used to represent a value which is
either correct or an error; by convention, the Left
constructor is
used to hold an error value and the Right
constructor is used to
hold a correct value (mnemonic: "right" also means "correct").
Examples
The type
is the type of values which can be either
a Either
String
Int
String
or an Int
. The Left
constructor can be used only on
String
s, and the Right
constructor can be used only on Int
s:
>>>
let s = Left "foo" :: Either String Int
>>>
s
Left "foo">>>
let n = Right 3 :: Either String Int
>>>
n
Right 3>>>
:type s
s :: Either String Int>>>
:type n
n :: Either String Int
The fmap
from our Functor
instance will ignore Left
values, but
will apply the supplied function to values contained in a Right
:
>>>
let s = Left "foo" :: Either String Int
>>>
let n = Right 3 :: Either String Int
>>>
fmap (*2) s
Left "foo">>>
fmap (*2) n
Right 6
The Monad
instance for Either
allows us to chain together multiple
actions which may fail, and fail overall if any of the individual
steps failed. First we'll write a function that can either parse an
Int
from a Char
, or fail.
>>>
import Data.Char ( digitToInt, isDigit )
>>>
:{
let parseEither :: Char -> Either String Int parseEither c | isDigit c = Right (digitToInt c) | otherwise = Left "parse error">>>
:}
The following should work, since both '1'
and '2'
can be
parsed as Int
s.
>>>
:{
let parseMultiple :: Either String Int parseMultiple = do x <- parseEither '1' y <- parseEither '2' return (x + y)>>>
:}
>>>
parseMultiple
Right 3
But the following should fail overall, since the first operation where
we attempt to parse 'm'
as an Int
will fail:
>>>
:{
let parseMultiple :: Either String Int parseMultiple = do x <- parseEither 'm' y <- parseEither '2' return (x + y)>>>
:}
>>>
parseMultiple
Left "parse error"
Instances
Bifunctor Either | |
Hashable2 Either | |
Swapped Either | |
Monad (Either e) | |
Functor (Either a) | |
Applicative (Either e) | |
Foldable (Either a) | |
Traversable (Either a) | |
Generic1 (Either a) | |
Hashable a => Hashable1 (Either a) | |
Apply (Either a) | |
Bind (Either a) | |
(Eq b, Eq a) => Eq (Either a b) | |
(Data a, Data b) => Data (Either a b) | |
(Ord b, Ord a) => Ord (Either a b) | |
(Read b, Read a) => Read (Either a b) | |
(Show b, Show a) => Show (Either a b) | |
Generic (Either a b) | |
Semigroup (Either a b) | |
(Lift a, Lift b) => Lift (Either a b) | |
(Outputable a, Outputable b) => Outputable (Either a b) | |
(Hashable a, Hashable b) => Hashable (Either a b) | |
type Rep1 (Either a) | |
type Rep (Either a b) | |
type (==) (Either k k1) a b | |
either :: (a -> c) -> (b -> c) -> Either a b -> c #
Case analysis for the Either
type.
If the value is
, apply the first function to Left
aa
;
if it is
, apply the second function to Right
bb
.
Examples
We create two values of type
, one using the
Either
String
Int
Left
constructor and another using the Right
constructor. Then
we apply "either" the length
function (if we have a String
)
or the "times-two" function (if we have an Int
):
>>>
let s = Left "foo" :: Either String Int
>>>
let n = Right 3 :: Either String Int
>>>
either length (*2) s
3>>>
either length (*2) n
6
Maybe
The Maybe
type encapsulates an optional value. A value of type
either contains a value of type Maybe
aa
(represented as
),
or it is empty (represented as Just
aNothing
). Using Maybe
is a good way to
deal with errors or exceptional cases without resorting to drastic
measures such as error
.
The Maybe
type is also a monad. It is a simple kind of error
monad, where all errors are represented by Nothing
. A richer
error monad can be built using the Either
type.
Instances
Monad Maybe | |
Functor Maybe | |
Applicative Maybe | |
Foldable Maybe | |
Traversable Maybe | |
Generic1 Maybe | |
Alternative Maybe | |
MonadPlus Maybe | |
Hashable1 Maybe | |
Apply Maybe | |
Bind Maybe | |
Eq a => Eq (Maybe a) | |
Data a => Data (Maybe a) | |
Ord a => Ord (Maybe a) | |
Read a => Read (Maybe a) | |
Show a => Show (Maybe a) | |
Generic (Maybe a) | |
Semigroup a => Semigroup (Maybe a) | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
Lift a => Lift (Maybe a) | |
Outputable a => Outputable (Maybe a) | |
Annotate (Maybe Role) | |
Hashable a => Hashable (Maybe a) | |
Ixed (Maybe a) | |
At (Maybe a) | |
SingI (Maybe a) (Nothing a) | |
SingKind a (KProxy a) => SingKind (Maybe a) (KProxy (Maybe a)) | |
SingI a a1 => SingI (Maybe a) (Just a a1) | |
type Rep1 Maybe | |
type Rep (Maybe a) | |
data Sing (Maybe a) | |
type Index (Maybe a) | |
type IxValue (Maybe a) | |
type (==) (Maybe k) a b | |
type DemoteRep (Maybe a) (KProxy (Maybe a)) | |
fromMaybe :: a -> Maybe a -> a #
The fromMaybe
function takes a default value and and Maybe
value. If the Maybe
is Nothing
, it returns the default values;
otherwise, it returns the value contained in the Maybe
.
Examples
Basic usage:
>>>
fromMaybe "" (Just "Hello, World!")
"Hello, World!"
>>>
fromMaybe "" Nothing
""
Read an integer from a string using readMaybe
. If we fail to
parse an integer, we want to return 0
by default:
>>>
import Text.Read ( readMaybe )
>>>
fromMaybe 0 (readMaybe "5")
5>>>
fromMaybe 0 (readMaybe "")
0
maybe :: b -> (a -> b) -> Maybe a -> b #
The maybe
function takes a default value, a function, and a Maybe
value. If the Maybe
value is Nothing
, the function returns the
default value. Otherwise, it applies the function to the value inside
the Just
and returns the result.
Examples
Basic usage:
>>>
maybe False odd (Just 3)
True
>>>
maybe False odd Nothing
False
Read an integer from a string using readMaybe
. If we succeed,
return twice the integer; that is, apply (*2)
to it. If instead
we fail to parse an integer, return 0
by default:
>>>
import Text.Read ( readMaybe )
>>>
maybe 0 (*2) (readMaybe "5")
10>>>
maybe 0 (*2) (readMaybe "")
0
Apply show
to a Maybe Int
. If we have Just n
, we want to show
the underlying Int
n
. But if we have Nothing
, we return the
empty string instead of (for example) "Nothing":
>>>
maybe "" show (Just 5)
"5">>>
maybe "" show Nothing
""
Tuple
uncurry :: (a -> b -> c) -> (a, b) -> c #
uncurry
converts a curried function to a function on pairs.
Typeclasses
Enum
Class Enum
defines operations on sequentially ordered types.
The enumFrom
... methods are used in Haskell's translation of
arithmetic sequences.
Instances of Enum
may be derived for any enumeration type (types
whose constructors have no fields). The nullary constructors are
assumed to be numbered left-to-right by fromEnum
from 0
through n-1
.
See Chapter 10 of the Haskell Report for more details.
For any type that is an instance of class Bounded
as well as Enum
,
the following should hold:
- The calls
andsucc
maxBound
should result in a runtime error.pred
minBound
fromEnum
andtoEnum
should give a runtime error if the result value is not representable in the result type. For example,
is an error.toEnum
7 ::Bool
enumFrom
andenumFromThen
should be defined with an implicit bound, thus:
enumFrom x = enumFromTo x maxBound enumFromThen x y = enumFromThenTo x y bound where bound | fromEnum y >= fromEnum x = maxBound | otherwise = minBound
Methods
the successor of a value. For numeric types, succ
adds 1.
the predecessor of a value. For numeric types, pred
subtracts 1.
Convert from an Int
.
Convert to an Int
.
It is implementation-dependent what fromEnum
returns when
applied to a value that is too large to fit in an Int
.
Used in Haskell's translation of [n..]
.
enumFromThen :: a -> a -> [a] #
Used in Haskell's translation of [n,n'..]
.
enumFromTo :: a -> a -> [a] #
Used in Haskell's translation of [n..m]
.
enumFromThenTo :: a -> a -> a -> [a] #
Used in Haskell's translation of [n,n'..m]
.
Instances
Eq
The Eq
class defines equality (==
) and inequality (/=
).
All the basic datatypes exported by the Prelude are instances of Eq
,
and Eq
may be derived for any datatype whose constituents are also
instances of Eq
.
Instances
Read
Parsing of String
s, producing values.
Derived instances of Read
make the following assumptions, which
derived instances of Show
obey:
- If the constructor is defined to be an infix operator, then the
derived
Read
instance will parse only infix applications of the constructor (not the prefix form). - Associativity is not used to reduce the occurrence of parentheses, although precedence may be.
- If the constructor is defined using record syntax, the derived
Read
will parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration. - The derived
Read
instance allows arbitrary Haskell whitespace between tokens of the input string. Extra parentheses are also allowed.
For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance of Read
in Haskell 2010 is equivalent to
instance (Read a) => Read (Tree a) where readsPrec d r = readParen (d > app_prec) (\r -> [(Leaf m,t) | ("Leaf",s) <- lex r, (m,t) <- readsPrec (app_prec+1) s]) r ++ readParen (d > up_prec) (\r -> [(u:^:v,w) | (u,s) <- readsPrec (up_prec+1) r, (":^:",t) <- lex s, (v,w) <- readsPrec (up_prec+1) t]) r where app_prec = 10 up_prec = 5
Note that right-associativity of :^:
is unused.
The derived instance in GHC is equivalent to
instance (Read a) => Read (Tree a) where readPrec = parens $ (prec app_prec $ do Ident "Leaf" <- lexP m <- step readPrec return (Leaf m)) +++ (prec up_prec $ do u <- step readPrec Symbol ":^:" <- lexP v <- step readPrec return (u :^: v)) where app_prec = 10 up_prec = 5 readListPrec = readListPrecDefault
Methods
attempts to parse a value from the front of the string, returning a list of (parsed value, remaining string) pairs. If there is no successful parse, the returned list is empty.
Derived instances of Read
and Show
satisfy the following:
That is, readsPrec
parses the string produced by
showsPrec
, and delivers the value that
showsPrec
started with.
The method readList
is provided to allow the programmer to
give a specialised way of parsing lists of values.
For example, this is used by the predefined Read
instance of
the Char
type, where values of type String
should be are
expected to use double quotes, rather than square brackets.
Proposed replacement for readsPrec
using new-style parsers (GHC only).
readListPrec :: ReadPrec [a] #
Proposed replacement for readList
using new-style parsers (GHC only).
The default definition uses readList
. Instances that define readPrec
should also define readListPrec
as readListPrecDefault
.
Instances
Read Bool | |
Read Char | |
Read Double | |
Read Float | |
Read Int | |
Read Int8 | |
Read Int16 | |
Read Int32 | |
Read Int64 | |
Read Integer | |
Read Ordering | |
Read Word | |
Read Word8 | |
Read Word16 | |
Read Word32 | |
Read Word64 | |
Read () | |
Read Color | |
Read ColorIntensity | |
Read ConsoleLayer | |
Read BlinkSpeed | |
Read Underlining | |
Read ConsoleIntensity | |
Read SGR | |
Read Void | Reading a |
Read Version | |
Read CDev | |
Read CIno | |
Read CMode | |
Read COff | |
Read CPid | |
Read CSsize | |
Read CGid | |
Read CNlink | |
Read CUid | |
Read CCc | |
Read CSpeed | |
Read CTcflag | |
Read CRLim | |
Read Fd | |
Read ExitCode | |
Read BufferMode | |
Read Newline | |
Read NewlineMode | |
Read CChar | |
Read CSChar | |
Read CUChar | |
Read CShort | |
Read CUShort | |
Read CInt | |
Read CUInt | |
Read CLong | |
Read CULong | |
Read CLLong | |
Read CULLong | |
Read CFloat | |
Read CDouble | |
Read CPtrdiff | |
Read CSize | |
Read CWchar | |
Read CSigAtomic | |
Read CClock | |
Read CTime | |
Read CUSeconds | |
Read CSUSeconds | |
Read CIntPtr | |
Read CUIntPtr | |
Read CIntMax | |
Read CUIntMax | |
Read All | |
Read Any | |
Read Fixity | |
Read Associativity | |
Read SourceUnpackedness | |
Read SourceStrictness | |
Read DecidedStrictness | |
Read Lexeme | |
Read GeneralCategory | |
Read ByteString | |
Read ByteString | |
Read IntSet | |
Read ColourPrefs | |
Read PCREOption | |
Read PCREExecOption | |
Read DatatypeVariant | |
Read a => Read [a] | |
Read a => Read (Maybe a) | |
(Integral a, Read a) => Read (Ratio a) | |
Read (V1 p) | |
Read (U1 p) | |
Read p => Read (Par1 p) | |
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the
|
Read a => Read (Min a) | |
Read a => Read (Max a) | |
Read a => Read (First a) | |
Read a => Read (Last a) | |
Read m => Read (WrappedMonoid m) | |
Read a => Read (Option a) | |
Read a => Read (NonEmpty a) | |
Read a => Read (Complex a) | |
Read a => Read (ZipList a) | |
Read a => Read (Dual a) | |
Read a => Read (Sum a) | |
Read a => Read (Product a) | |
Read a => Read (First a) | |
Read a => Read (Last a) | |
Read a => Read (Down a) | |
Read e => Read (IntMap e) | |
Read a => Read (Tree a) | |
Read a => Read (Seq a) | |
Read a => Read (ViewL a) | |
Read a => Read (ViewR a) | |
(Read a, Ord a) => Read (Set a) | |
Read a => Read (Vector a) | |
(Read a, Prim a) => Read (Vector a) | |
(Read a, Storable a) => Read (Vector a) | |
(Eq a, Hashable a, Read a) => Read (HashSet a) | |
Read a => Read (Array a) | |
(Read b, Read a) => Read (Either a b) | |
Read (f p) => Read (Rec1 f p) | |
(Read a, Read b) => Read (a, b) | |
(Ix a, Read a, Read b) => Read (Array a b) | |
(Read b, Read a) => Read (Arg a b) | |
Read (Proxy k s) | |
(Ord k, Read k, Read e) => Read (Map k e) | |
(Read (f (Cofree f a)), Read a) => Read (Cofree f a) | |
Read (w (a, CoiterT w a)) => Read (CoiterT w a) | |
Read (m (Either a (IterT m a))) => Read (IterT m a) | |
(Read1 m, Read a) => Read (MaybeT m a) | |
(Functor f, Read (f a)) => Read (Yoneda f a) | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) | |
(Read1 m, Read a) => Read (ListT m a) | |
Read c => Read (K1 i c p) | |
(Read (g p), Read (f p)) => Read ((:+:) f g p) | |
(Read (g p), Read (f p)) => Read ((:*:) f g p) | |
Read (f (g p)) => Read ((:.:) f g p) | |
(Read a, Read b, Read c) => Read (a, b, c) | |
Read a => Read (Const k a b) | This instance would be equivalent to the derived instances of the
|
Read (f a) => Read (Alt k f a) | |
Coercible k a b => Read (Coercion k a b) | |
(~) k a b => Read ((:~:) k a b) | |
Read (p a a) => Read (Join k p a) | |
Read (p (Fix k p a) a) => Read (Fix k p a) | |
(Read1 f, Read a) => Read (IdentityT * f a) | |
(Read (f b), Read a) => Read (CofreeF f a b) | |
Read (w (CofreeF f a (CofreeT f w a))) => Read (CofreeT f w a) | |
(Read (f b), Read a) => Read (FreeF f a b) | |
Read (m (FreeF f a (FreeT f m a))) => Read (FreeT f m a) | |
(Read1 f, Read a) => Read (Backwards * f a) | |
(Read e, Read1 m, Read a) => Read (ErrorT e m a) | |
(Read e, Read1 m, Read a) => Read (ExceptT e m a) | |
(Read w, Read1 m, Read a) => Read (WriterT w m a) | |
(Read w, Read1 m, Read a) => Read (WriterT w m a) | |
Read b => Read (Tagged k s b) | |
(Read1 f, Read a) => Read (Reverse * f a) | |
Read a => Read (Constant k a b) | |
Read (f p) => Read (M1 i c f p) | |
(Read a, Read b, Read c, Read d) => Read (a, b, c, d) | |
(Read1 f, Read1 g, Read a) => Read (Sum * f g a) | |
(Read1 f, Read1 g, Read a) => Read (Product * f g a) | |
(Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) | |
(Read1 f, Read1 g, Read a) => Read (Compose * * f g a) | |
Read (p a b) => Read (WrappedBifunctor k1 k p a b) | |
Read (g b) => Read (Joker k1 k g a b) | |
Read (p b a) => Read (Flip k k1 p a b) | |
Read (f a) => Read (Clown k1 k f a b) | |
(Read a, Read b, Read c, Read d, Read e, Read f) => Read (a, b, c, d, e, f) | |
(Read (g a b), Read (f a b)) => Read (Product k1 k f g a b) | |
(Read (q a b), Read (p a b)) => Read (Sum k1 k p q a b) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g) => Read (a, b, c, d, e, f, g) | |
Read (f (p a b)) => Read (Tannen k2 k1 k f p a b) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h) => Read (a, b, c, d, e, f, g, h) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i) => Read (a, b, c, d, e, f, g, h, i) | |
Read (p (f a) (g b)) => Read (Biff k3 k2 k1 k p f g a b) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j) => Read (a, b, c, d, e, f, g, h, i, j) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k) => Read (a, b, c, d, e, f, g, h, i, j, k) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l) => Read (a, b, c, d, e, f, g, h, i, j, k, l) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n, Read o) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | |
readMaybe :: Read a => String -> Maybe a #
Parse a string using the Read
instance.
Succeeds if there is exactly one valid result.
Since: 4.6.0.0
Show
Conversion of values to readable String
s.
Derived instances of Show
have the following properties, which
are compatible with derived instances of Read
:
- The result of
show
is a syntactically correct Haskell expression containing only constants, given the fixity declarations in force at the point where the type is declared. It contains only the constructor names defined in the data type, parentheses, and spaces. When labelled constructor fields are used, braces, commas, field names, and equal signs are also used. - If the constructor is defined to be an infix operator, then
showsPrec
will produce infix applications of the constructor. - the representation will be enclosed in parentheses if the
precedence of the top-level constructor in
x
is less thand
(associativity is ignored). Thus, ifd
is0
then the result is never surrounded in parentheses; ifd
is11
it is always surrounded in parentheses, unless it is an atomic expression. - If the constructor is defined using record syntax, then
show
will produce the record-syntax form, with the fields given in the same order as the original declaration.
For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance of Show
is equivalent to
instance (Show a) => Show (Tree a) where showsPrec d (Leaf m) = showParen (d > app_prec) $ showString "Leaf " . showsPrec (app_prec+1) m where app_prec = 10 showsPrec d (u :^: v) = showParen (d > up_prec) $ showsPrec (up_prec+1) u . showString " :^: " . showsPrec (up_prec+1) v where up_prec = 5
Note that right-associativity of :^:
is ignored. For example,
produces the stringshow
(Leaf 1 :^: Leaf 2 :^: Leaf 3)"Leaf 1 :^: (Leaf 2 :^: Leaf 3)"
.
Methods
showsPrec :: Int -> a -> ShowS #
Convert a value to a readable String
.
showsPrec
should satisfy the law
showsPrec d x r ++ s == showsPrec d x (r ++ s)
Derived instances of Read
and Show
satisfy the following:
That is, readsPrec
parses the string produced by
showsPrec
, and delivers the value that showsPrec
started with.
Instances
ShowS
showString :: String -> ShowS #
utility function converting a String
to a show function that
simply prepends the string unchanged.
Foldable
Data structures that can be folded.
For example, given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Foldable Tree where foldMap f Empty = mempty foldMap f (Leaf x) = f x foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r
This is suitable even for abstract types, as the monoid is assumed
to satisfy the monoid laws. Alternatively, one could define foldr
:
instance Foldable Tree where foldr f z Empty = z foldr f z (Leaf x) = f x z foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l
Foldable
instances are expected to satisfy the following laws:
foldr f z t = appEndo (foldMap (Endo . f) t ) z
foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
fold = foldMap id
sum
, product
, maximum
, and minimum
should all be essentially
equivalent to foldMap
forms, such as
sum = getSum . foldMap Sum
but may be less defined.
If the type is also a Functor
instance, it should satisfy
foldMap f = fold . fmap f
which implies that
foldMap f . fmap g = foldMap (f . g)
Methods
fold :: Monoid m => t m -> m #
Combine the elements of a structure using a monoid.
foldMap :: Monoid m => (a -> m) -> t a -> m #
Map each element of the structure to a monoid, and combine the results.
foldr :: (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure.
In the case of lists, foldr
, when 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)...)
Note that, since the head of the resulting expression is produced by
an application of the operator to the first element of the list,
foldr
can produce a terminating expression from an infinite list.
For a general Foldable
structure this should be semantically identical
to,
foldr f z =foldr
f z .toList
foldr' :: (a -> b -> b) -> b -> t a -> b #
Right-associative fold of a structure, but with strict application of the operator.
foldl :: (b -> a -> b) -> b -> t a -> b #
Left-associative fold of a structure.
In the case of lists, foldl
, when applied to a binary
operator, a starting value (typically the left-identity of the operator),
and a list, reduces the list using the binary operator, from left to
right:
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
Note that to produce the outermost application of the operator the
entire input list must be traversed. This means that foldl'
will
diverge if given an infinite list.
Also note that if you want an efficient left-fold, you probably want to
use foldl'
instead of foldl
. The reason for this is that latter does
not force the "inner" results (e.g. z
in the above example)
before applying them to the operator (e.g. to f
x1(
). This results
in a thunk chain f
x2)O(n)
elements long, which then must be evaluated from
the outside-in.
For a general Foldable
structure this should be semantically identical
to,
foldl f z =foldl
f z .toList
foldl' :: (b -> a -> b) -> b -> t a -> b #
Left-associative fold of a structure but with strict application of the operator.
This ensures that each step of the fold is forced to weak head normal
form before being applied, avoiding the collection of thunks that would
otherwise occur. This is often what you want to strictly reduce a finite
list to a single, monolithic result (e.g. length
).
For a general Foldable
structure this should be semantically identical
to,
foldl f z =foldl'
f z .toList
foldr1 :: (a -> a -> a) -> t a -> a #
A variant of foldr
that has no base case,
and thus may only be applied to non-empty structures.
foldr1
f =foldr1
f .toList
foldl1 :: (a -> a -> a) -> t a -> a #
A variant of foldl
that has no base case,
and thus may only be applied to non-empty structures.
foldl1
f =foldl1
f .toList
List of elements of a structure, from left to right.
Test whether the structure is empty. The default implementation is optimized for structures that are similar to cons-lists, because there is no general way to do better.
Returns the size/length of a finite structure as an Int
. The
default implementation is optimized for structures that are similar to
cons-lists, because there is no general way to do better.
elem :: Eq a => a -> t a -> Bool infix 4 #
Does the element occur in the structure?
maximum :: Ord a => t a -> a #
The largest element of a non-empty structure.
minimum :: Ord a => t a -> a #
The least element of a non-empty structure.
The sum
function computes the sum of the numbers of a structure.
product :: Num a => t a -> a #
The product
function computes the product of the numbers of a
structure.
Instances
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () #
Ord
The Ord
class is used for totally ordered datatypes.
Instances of Ord
can be derived for any user-defined
datatype whose constituent types are in Ord
. The declared order
of the constructors in the data declaration determines the ordering
in derived Ord
instances. The Ordering
datatype allows a single
comparison to determine the precise ordering of two objects.
Minimal complete definition: either compare
or <=
.
Using compare
can be more efficient for complex types.
Methods
compare :: a -> a -> Ordering #
(<) :: a -> a -> Bool infix 4 #
(<=) :: a -> a -> Bool infix 4 #
(>) :: a -> a -> Bool infix 4 #
Instances
comparing :: Ord a => (b -> a) -> b -> b -> Ordering #
comparing p x y = compare (p x) (p y)
Useful combinator for use in conjunction with the xxxBy
family
of functions from Data.List, for example:
... sortBy (comparing fst) ...
Traversable
class (Functor t, Foldable t) => Traversable t where #
Functors representing data structures that can be traversed from left to right.
A definition of traverse
must satisfy the following laws:
- naturality
t .
for every applicative transformationtraverse
f =traverse
(t . f)t
- identity
traverse
Identity = Identity- composition
traverse
(Compose .fmap
g . f) = Compose .fmap
(traverse
g) .traverse
f
A definition of sequenceA
must satisfy the following laws:
- naturality
t .
for every applicative transformationsequenceA
=sequenceA
.fmap
tt
- identity
sequenceA
.fmap
Identity = Identity- composition
sequenceA
.fmap
Compose = Compose .fmap
sequenceA
.sequenceA
where an applicative transformation is a function
t :: (Applicative f, Applicative g) => f a -> g a
preserving the Applicative
operations, i.e.
and the identity functor Identity
and composition of functors Compose
are defined as
newtype Identity a = Identity a instance Functor Identity where fmap f (Identity x) = Identity (f x) instance Applicative Identity where pure x = Identity x Identity f <*> Identity x = Identity (f x) newtype Compose f g a = Compose (f (g a)) instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap (fmap f) x) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)
(The naturality law is implied by parametricity.)
Instances are similar to Functor
, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
This is suitable even for abstract types, as the laws for <*>
imply a form of associativity.
The superclass instances should satisfy the following:
- In the
Functor
instance,fmap
should be equivalent to traversal with the identity applicative functor (fmapDefault
). - In the
Foldable
instance,foldMap
should be equivalent to traversal with a constant applicative functor (foldMapDefault
).
Methods
traverse :: Applicative f => (a -> f b) -> t a -> f (t b) #
Map each element of a structure to an action, evaluate these actions
from left to right, and collect the results. For a version that ignores
the results see traverse_
.
sequenceA :: Applicative f => t (f a) -> f (t a) #
Evaluate each action in the structure from left to right, and
and collect the results. For a version that ignores the results
see sequenceA_
.
mapM :: Monad m => (a -> m b) -> t a -> m (t b) #
Map each element of a structure to a monadic action, evaluate
these actions from left to right, and collect the results. For
a version that ignores the results see mapM_
.
sequence :: Monad m => t (m a) -> m (t a) #
Evaluate each monadic action in the structure from left to
right, and collect the results. For a version that ignores the
results see sequence_
.
Instances
for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b) #
traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f () #
Map each element of a structure to an action, evaluate these
actions from left to right, and ignore the results. For a version
that doesn't ignore the results see traverse
.
Combinators
($) :: (a -> b) -> a -> b infixr 0 #
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
($!) :: (a -> b) -> a -> b infixr 0 #
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.
const x
is a unary function which evaluates to x
for all inputs.
For instance,
>>>
map (const 42) [0..3]
[42,42,42,42]
flip :: (a -> b -> c) -> b -> a -> c #
takes its (first) two arguments in the reverse order of flip
ff
.
is the least fixed point of the function fix
ff
,
i.e. the least defined x
such that f x = x
.
The value of seq a b
is bottom if a
is bottom, and
otherwise equal to b
. seq
is usually introduced to
improve performance by avoiding unneeded laziness.
A note on evaluation order: the expression seq a b
does
not guarantee that a
will be evaluated before b
.
The only guarantee given by seq
is that the both a
and b
will be evaluated before seq
returns a value.
In particular, this means that b
may be evaluated before
a
. If you need to guarantee a specific order of evaluation,
you must use the function pseq
from the "parallel" package.
System
IO
A value of type
is a computation which, when performed,
does some I/O before returning a value of type IO
aa
.
There is really only one way to "perform" an I/O action: bind it to
Main.main
in your program. When your program is run, the I/O will
be performed. It isn't possible to perform I/O from an arbitrary
function, unless that function is itself in the IO
monad and called
at some point, directly or indirectly, from Main.main
.
IO
is a monad, so IO
actions can be combined using either the do-notation
or the >>
and >>=
operations from the Monad
class.
File and directory names are values of type String
, whose precise
meaning is operating system dependent. Files can be opened, yielding a
handle which can then be used to operate on the contents of that file.
Partial functions
undefined :: HasCallStack => a Source #
Warning: undefined
is unsafe