Safe Haskell | None |
---|---|
Language | Haskell2010 |
Warning: This modules' API is not stable. Use at your own risk, or better yet, use base-compat
!
This module re-exports the non-exposed
Distribution.Compat.Prelude module for
reuse by cabal-install
's
Distribution.Client.Compat.Prelude module.
It is highly discouraged to rely on this module
for Setup.hs
scripts since its API is not
stable.
- class Semigroup a where
- gmappend :: (Generic a, GSemigroup (Rep a)) => a -> a -> a
- gmempty :: (Generic a, GMonoid (Rep a)) => a
- class Typeable k (a :: k)
- class Typeable * a => Data a
- class Generic a
- class NFData a where
- genericRnf :: (Generic a, GNFData (Rep a)) => a -> ()
- class Binary t where
- class Applicative f => Alternative (f :: * -> *) where
- class (Alternative m, Monad m) => MonadPlus (m :: * -> *) where
- class IsString a where
- type IO a = WithCallStack (IO a)
- type NoCallStackIO a = IO a
- data Map k a :: * -> * -> *
- catMaybes :: [Maybe a] -> [a]
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- fromMaybe :: a -> Maybe a -> a
- maybeToList :: Maybe a -> [a]
- listToMaybe :: [a] -> Maybe a
- isNothing :: Maybe a -> Bool
- isJust :: Maybe a -> Bool
- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
- isPrefixOf :: Eq a => [a] -> [a] -> Bool
- isSuffixOf :: Eq a => [a] -> [a] -> Bool
- intercalate :: [a] -> [[a]] -> [a]
- intersperse :: a -> [a] -> [a]
- sort :: Ord a => [a] -> [a]
- sortBy :: (a -> a -> Ordering) -> [a] -> [a]
- nub :: Eq a => [a] -> [a]
- nubBy :: (a -> a -> Bool) -> [a] -> [a]
- class Foldable (t :: * -> *) where
- foldMap :: Foldable t => forall m a. Monoid m => (a -> m) -> t a -> m
- foldr :: Foldable t => forall a b. (a -> b -> b) -> b -> t a -> b
- null :: Foldable t => forall a. t a -> Bool
- length :: Foldable t => forall a. t a -> Int
- find :: Foldable t => (a -> Bool) -> t a -> Maybe a
- foldl' :: Foldable t => forall b a. (b -> a -> b) -> b -> t a -> b
- traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
- for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
- class (Functor t, Foldable t) => Traversable (t :: * -> *) where
- traverse :: Traversable t => forall (f :: * -> *) a b. Applicative f => (a -> f b) -> t a -> f (t b)
- sequenceA :: Traversable t => forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a)
- for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
- first :: Arrow a => forall b c d. a b c -> a (b, d) (c, d)
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- unless :: Applicative f => Bool -> f () -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- ap :: Monad m => m (a -> b) -> m a -> m b
- void :: Functor f => f a -> f ()
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- isSpace :: Char -> Bool
- isDigit :: Char -> Bool
- isUpper :: Char -> Bool
- isAlpha :: Char -> Bool
- isAlphaNum :: Char -> Bool
- chr :: Int -> Char
- ord :: Char -> Int
- toLower :: Char -> Char
- toUpper :: Char -> Char
- data Word :: *
- data Word8 :: *
- data Word16 :: *
- data Word32 :: *
- data Word64 :: *
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- (<<>>) :: Doc -> Doc -> Doc
Prelude
Common type-classes
The class of semigroups (types with an associative binary operation).
Since: 4.9.0.0
The class Typeable
allows a concrete representation of a type to
be calculated.
typeRep#
class Typeable * a => Data a #
The Data
class comprehends a fundamental primitive gfoldl
for
folding over constructor applications, say terms. This primitive can
be instantiated in several ways to map over the immediate subterms
of a term; see the gmap
combinators later in this class. Indeed, a
generic programmer does not necessarily need to use the ingenious gfoldl
primitive but rather the intuitive gmap
combinators. The gfoldl
primitive is completed by means to query top-level constructors, to
turn constructor representations into proper terms, and to list all
possible datatype constructors. This completion allows us to serve
generic programming scenarios like read, show, equality, term generation.
The combinators gmapT
, gmapQ
, gmapM
, etc are all provided with
default definitions in terms of gfoldl
, leaving open the opportunity
to provide datatype-specific definitions.
(The inclusion of the gmap
combinators as members of class Data
allows the programmer or the compiler to derive specialised, and maybe
more efficient code per datatype. Note: gfoldl
is more higher-order
than the gmap
combinators. This is subject to ongoing benchmarking
experiments. It might turn out that the gmap
combinators will be
moved out of the class Data
.)
Conceptually, the definition of the gmap
combinators in terms of the
primitive gfoldl
requires the identification of the gfoldl
function
arguments. Technically, we also need to identify the type constructor
c
for the construction of the result type from the folded term type.
In the definition of gmapQ
x combinators, we use phantom type
constructors for the c
in the type of gfoldl
because the result type
of a query does not involve the (polymorphic) type of the term argument.
In the definition of gmapQl
we simply use the plain constant type
constructor because gfoldl
is left-associative anyway and so it is
readily suited to fold a left-associative binary operation over the
immediate subterms. In the definition of gmapQr, extra effort is
needed. We use a higher-order accumulation trick to mediate between
left-associative constructor application vs. right-associative binary
operation (e.g., (:)
). When the query is meant to compute a value
of type r
, then the result type withing generic folding is r -> r
.
So the result of folding is a function to which we finally pass the
right unit.
With the -XDeriveDataTypeable
option, GHC can generate instances of the
Data
class automatically. For example, given the declaration
data T a b = C1 a b | C2 deriving (Typeable, Data)
GHC will generate an instance that is equivalent to
instance (Data a, Data b) => Data (T a b) where gfoldl k z (C1 a b) = z C1 `k` a `k` b gfoldl k z C2 = z C2 gunfold k z c = case constrIndex c of 1 -> k (k (z C1)) 2 -> z C2 toConstr (C1 _ _) = con_C1 toConstr C2 = con_C2 dataTypeOf _ = ty_T con_C1 = mkConstr ty_T "C1" [] Prefix con_C2 = mkConstr ty_T "C2" [] Prefix ty_T = mkDataType "Module.T" [con_C1, con_C2]
This is suitable for datatypes that are exported transparently.
Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.
A class of types that can be fully evaluated.
Since: 1.1.0.0
rnf
should reduce its argument to normal form (that is, fully
evaluate all sub-components), and then return '()'.
Generic
NFData
deriving
Starting with GHC 7.2, you can automatically derive instances
for types possessing a Generic
instance.
Note: Generic1
can be auto-derived starting with GHC 7.4
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic, Generic1) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, Generic1) instance NFData a => NFData (Foo a) instance NFData1 Foo data Colour = Red | Green | Blue deriving Generic instance NFData Colour
Starting with GHC 7.10, the example above can be written more
concisely by enabling the new DeriveAnyClass
extension:
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, Generic1, NFData, NFData1) data Colour = Red | Green | Blue deriving (Generic, NFData)
Compatibility with previous deepseq
versions
Prior to version 1.4.0.0, the default implementation of the rnf
method was defined as
rnf
a =seq
a ()
However, starting with deepseq-1.4.0.0
, the default
implementation is based on DefaultSignatures
allowing for
more accurate auto-derived NFData
instances. If you need the
previously used exact default rnf
method implementation
semantics, use
instance NFData Colour where rnf x = seq x ()
or alternatively
instance NFData Colour where rnf = rwhnf
or
{-# LANGUAGE BangPatterns #-} instance NFData Colour where rnf !_ = ()
NFData Bool | |
NFData Char | |
NFData Double | |
NFData Float | |
NFData Int | |
NFData Int8 | |
NFData Int16 | |
NFData Int32 | |
NFData Int64 | |
NFData Integer | |
NFData Natural | Since: 1.4.0.0 |
NFData Ordering | |
NFData Word | |
NFData Word8 | |
NFData Word16 | |
NFData Word32 | |
NFData Word64 | |
NFData CallStack | Since: 1.4.2.0 |
NFData () | |
NFData TyCon | NOTE: Only defined for Since: 1.4.0.0 |
NFData Void | Since: 1.4.0.0 |
NFData Unique | Since: 1.4.0.0 |
NFData Version | Since: 1.3.0.0 |
NFData ThreadId | Since: 1.4.0.0 |
NFData ExitCode | Since: 1.4.2.0 |
NFData TypeRep | NOTE: Only defined for Since: 1.4.0.0 |
NFData All | Since: 1.4.0.0 |
NFData Any | Since: 1.4.0.0 |
NFData CChar | Since: 1.4.0.0 |
NFData CSChar | Since: 1.4.0.0 |
NFData CUChar | Since: 1.4.0.0 |
NFData CShort | Since: 1.4.0.0 |
NFData CUShort | Since: 1.4.0.0 |
NFData CInt | Since: 1.4.0.0 |
NFData CUInt | Since: 1.4.0.0 |
NFData CLong | Since: 1.4.0.0 |
NFData CULong | Since: 1.4.0.0 |
NFData CLLong | Since: 1.4.0.0 |
NFData CULLong | Since: 1.4.0.0 |
NFData CBool | Since: 1.4.3.0 |
NFData CFloat | Since: 1.4.0.0 |
NFData CDouble | Since: 1.4.0.0 |
NFData CPtrdiff | Since: 1.4.0.0 |
NFData CSize | Since: 1.4.0.0 |
NFData CWchar | Since: 1.4.0.0 |
NFData CSigAtomic | Since: 1.4.0.0 |
NFData CClock | Since: 1.4.0.0 |
NFData CTime | Since: 1.4.0.0 |
NFData CUSeconds | Since: 1.4.0.0 |
NFData CSUSeconds | Since: 1.4.0.0 |
NFData CFile | Since: 1.4.0.0 |
NFData CFpos | Since: 1.4.0.0 |
NFData CJmpBuf | Since: 1.4.0.0 |
NFData CIntPtr | Since: 1.4.0.0 |
NFData CUIntPtr | Since: 1.4.0.0 |
NFData CIntMax | Since: 1.4.0.0 |
NFData CUIntMax | Since: 1.4.0.0 |
NFData Fingerprint | Since: 1.4.0.0 |
NFData SrcLoc | Since: 1.4.2.0 |
NFData ShortByteString | |
NFData ByteString | |
NFData ByteString | |
NFData IntSet | |
NFData Doc | |
NFData TextDetails | |
NFData ZonedTime | |
NFData LocalTime | |
NFData TimeOfDay | |
NFData TimeZone | |
NFData UniversalTime | |
NFData UTCTime | |
NFData NominalDiffTime | |
NFData Day | |
NFData ShortText # | |
NFData PkgconfigName # | |
NFData ComponentId # | |
NFData ModuleName # | |
NFData VersionRange # | |
NFData Version # | |
NFData PkgconfigDependency # | |
NFData PackageName # | |
NFData UnqualComponentName # | |
NFData PackageIdentifier # | |
NFData DefUnitId # | |
NFData UnitId # | |
NFData Module # | |
NFData OpenModule # | |
NFData OpenUnitId # | |
NFData ExeDependency # | |
NFData Dependency # | |
NFData MungedPackageName # | |
NFData MungedPackageId # | |
NFData LegacyExeDependency # | |
NFData a => NFData [a] | |
NFData a => NFData (Maybe a) | |
NFData a => NFData (Ratio a) | |
NFData (Ptr a) | Since: 1.4.2.0 |
NFData (FunPtr a) | Since: 1.4.2.0 |
NFData a => NFData (Complex a) | |
NFData (Fixed a) | Since: 1.3.0.0 |
NFData a => NFData (Min a) | Since: 1.4.2.0 |
NFData a => NFData (Max a) | Since: 1.4.2.0 |
NFData a => NFData (First a) | Since: 1.4.2.0 |
NFData a => NFData (Last a) | Since: 1.4.2.0 |
NFData m => NFData (WrappedMonoid m) | Since: 1.4.2.0 |
NFData a => NFData (Option a) | Since: 1.4.2.0 |
NFData a => NFData (NonEmpty a) | Since: 1.4.2.0 |
NFData (StableName a) | Since: 1.4.0.0 |
NFData a => NFData (ZipList a) | Since: 1.4.0.0 |
NFData a => NFData (Identity a) | Since: 1.4.0.0 |
NFData (IORef a) | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
NFData a => NFData (Dual a) | Since: 1.4.0.0 |
NFData a => NFData (Sum a) | Since: 1.4.0.0 |
NFData a => NFData (Product a) | Since: 1.4.0.0 |
NFData a => NFData (First a) | Since: 1.4.0.0 |
NFData a => NFData (Last a) | Since: 1.4.0.0 |
NFData a => NFData (Down a) | Since: 1.4.0.0 |
NFData (MVar a) | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
NFData a => NFData (IntMap a) | |
NFData a => NFData (SCC a) | |
NFData a => NFData (Tree a) | |
NFData a => NFData (Seq a) | |
NFData a => NFData (FingerTree a) | |
NFData a => NFData (Digit a) | |
NFData a => NFData (Node a) | |
NFData a => NFData (Elem a) | |
NFData a => NFData (Set a) | |
NFData a => NFData (Doc a) | |
NFData a => NFData (AnnotDetails a) | |
(NFData a, NFData (Key a)) => NFData (Graph a) # | |
NFData (a -> b) | This instance is for convenience and consistency with Since: 1.3.0.0 |
(NFData a, NFData b) => NFData (Either a b) | |
(NFData a, NFData b) => NFData (a, b) | |
(NFData a, NFData b) => NFData (Array a b) | |
(NFData a, NFData b) => NFData (Arg a b) | Since: 1.4.2.0 |
NFData (Proxy k a) | Since: 1.4.0.0 |
NFData (STRef s a) | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
(NFData k, NFData a) => NFData (Map k a) | |
(NFData a1, NFData a2, NFData a3) => NFData (a1, a2, a3) | |
NFData a => NFData (Const k a b) | Since: 1.4.0.0 |
NFData ((:~:) k a b) | Since: 1.4.3.0 |
(NFData a1, NFData a2, NFData a3, NFData a4) => NFData (a1, a2, a3, a4) | |
(NFData1 f, NFData1 g, NFData a) => NFData (Product * f g a) | Since: 1.4.3.0 |
(NFData1 f, NFData1 g, NFData a) => NFData (Sum * f g a) | Since: 1.4.3.0 |
NFData ((:~~:) k1 k2 a b) | Since: 1.4.3.0 |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) | |
(NFData1 f, NFData1 g, NFData a) => NFData (Compose * * f g a) | Since: 1.4.3.0 |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) | |
genericRnf :: (Generic a, GNFData (Rep a)) => a -> () #
GHC.Generics-based rnf
implementation
This is needed in order to support deepseq < 1.4
which didn't
have a Generic
-based default rnf
implementation yet.
In order to define instances, use e.g.
instance NFData MyType where rnf = genericRnf
The implementation has been taken from deepseq-1.4.2
's default
rnf
implementation.
The Binary
class provides put
and get
, methods to encode and
decode a Haskell value to a lazy ByteString
. It mirrors the Read
and
Show
classes for textual representation of Haskell types, and is
suitable for serialising Haskell values to disk, over the network.
For decoding and generating simple external binary formats (e.g. C
structures), Binary may be used, but in general is not suitable
for complex protocols. Instead use the Put
and Get
primitives
directly.
Instances of Binary should satisfy the following property:
decode . encode == id
That is, the get
and put
methods should be the inverse of each
other. A range of instances are provided for basic Haskell types.
Encode a value in the Put monad.
Decode a value in the Get monad
Encode a list of values in the Put monad. The default implementation may be overridden to be more efficient but must still have the same encoding format.
class Applicative f => Alternative (f :: * -> *) where #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
The identity of <|>
(<|>) :: f a -> f a -> f a infixl 3 #
An associative binary operation
One or more.
Zero or more.
class (Alternative m, Monad m) => MonadPlus (m :: * -> *) where #
Monads that also support choice and failure.
the identity of mplus
. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
an associative operation
MonadPlus [] | Since: 2.1 |
MonadPlus Maybe | Since: 2.1 |
MonadPlus IO | Since: 4.9.0.0 |
MonadPlus P | Since: 2.1 |
MonadPlus Option | Since: 4.9.0.0 |
MonadPlus STM | Since: 4.3.0.0 |
MonadPlus ReadPrec | Since: 2.1 |
MonadPlus ReadP | Since: 2.1 |
MonadPlus Get | Since: 0.7.1.0 |
MonadPlus Seq | |
MonadPlus Condition # | |
MonadPlus (U1 *) | Since: 4.9.0.0 |
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | Since: 4.6.0.0 |
MonadPlus (Proxy *) | Since: 4.9.0.0 |
MonadPlus f => MonadPlus (Rec1 * f) | Since: 4.9.0.0 |
MonadPlus f => MonadPlus (Alt * f) | |
(MonadPlus f, MonadPlus g) => MonadPlus ((:*:) * f g) | Since: 4.9.0.0 |
(MonadPlus f, MonadPlus g) => MonadPlus (Product * f g) | Since: 4.9.0.0 |
MonadPlus f => MonadPlus (M1 * i c f) | Since: 4.9.0.0 |
Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).
fromString :: String -> a #
IsString ShortByteString | |
IsString ByteString | |
IsString ByteString | |
IsString Doc | |
IsString CmdSpec | construct a Since: 1.2.1.0 |
IsString ShortText # | |
IsString PkgconfigName # | Since: 2.0.0.2 |
IsString ComponentId # | Since: 2.0.0.2 |
IsString AbiHash # | Since: 2.0.0.2 |
IsString ModuleName # | Construct a This is just a convenience function intended for valid module strings. It is
an error if it is used with a string that is not a valid module name. If you
are parsing user input then use |
IsString PackageName # | Since: 2.0.0.2 |
IsString UnqualComponentName # | Since: 2.0.0.2 |
IsString UnitId # | Since: 2.0.0.2 |
IsString MungedPackageName # | Since: 2.0.0.2 |
IsString FlagName # | Since: 2.0.0.2 |
(~) * a Char => IsString [a] |
Since: 2.1 |
IsString a => IsString (Identity a) | |
IsString (Seq Char) | |
IsString (Doc a) | |
IsString a => IsString (Const * a b) | Since: 4.9.0.0 |
Some types
type IO a = WithCallStack (IO a) #
type NoCallStackIO a = IO a #
A Map from keys k
to values a
.
Eq2 Map | |
Ord2 Map | |
Show2 Map | |
Functor (Map k) | |
Foldable (Map k) | |
Traversable (Map k) | |
Eq k => Eq1 (Map k) | |
Ord k => Ord1 (Map k) | |
(Ord k, Read k) => Read1 (Map k) | |
Show k => Show1 (Map k) | |
Ord k => IsList (Map k v) | |
(Eq k, Eq a) => Eq (Map k a) | |
(Data k, Data a, Ord k) => Data (Map k a) | |
(Ord k, Ord v) => Ord (Map k v) | |
(Ord k, Read k, Read e) => Read (Map k e) | |
(Show k, Show a) => Show (Map k a) | |
Ord k => Semigroup (Map k v) | |
Ord k => Monoid (Map k v) | |
(Binary k, Binary e) => Binary (Map k e) | |
(NFData k, NFData a) => NFData (Map k a) | |
ModSubst a => ModSubst (Map k a) # | |
type Item (Map k v) | |
Data.Maybe
catMaybes :: [Maybe a] -> [a] #
The catMaybes
function takes a list of Maybe
s and returns
a list of all the Just
values.
Examples
Basic usage:
>>>
catMaybes [Just 1, Nothing, Just 3]
[1,3]
When constructing a list of Maybe
values, catMaybes
can be used
to return all of the "success" results (if the list is the result
of a map
, then mapMaybe
would be more appropriate):
>>>
import Text.Read ( readMaybe )
>>>
[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[Just 1,Nothing,Just 3]>>>
catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[1,3]
mapMaybe :: (a -> Maybe b) -> [a] -> [b] #
The mapMaybe
function is a version of map
which can throw
out elements. In particular, the functional argument returns
something of type
. If this is Maybe
bNothing
, no element
is added on to the result list. If it is
, then Just
bb
is
included in the result list.
Examples
Using
is a shortcut for mapMaybe
f x
in most cases:catMaybes
$ map
f x
>>>
import Text.Read ( readMaybe )
>>>
let readMaybeInt = readMaybe :: String -> Maybe Int
>>>
mapMaybe readMaybeInt ["1", "Foo", "3"]
[1,3]>>>
catMaybes $ map readMaybeInt ["1", "Foo", "3"]
[1,3]
If we map the Just
constructor, the entire list should be returned:
>>>
mapMaybe Just [1,2,3]
[1,2,3]
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
maybeToList :: Maybe a -> [a] #
The maybeToList
function returns an empty list when given
Nothing
or a singleton list when not given Nothing
.
Examples
Basic usage:
>>>
maybeToList (Just 7)
[7]
>>>
maybeToList Nothing
[]
One can use maybeToList
to avoid pattern matching when combined
with a function that (safely) works on lists:
>>>
import Text.Read ( readMaybe )
>>>
sum $ maybeToList (readMaybe "3")
3>>>
sum $ maybeToList (readMaybe "")
0
listToMaybe :: [a] -> Maybe a #
The listToMaybe
function returns Nothing
on an empty list
or
where Just
aa
is the first element of the list.
Examples
Basic usage:
>>>
listToMaybe []
Nothing
>>>
listToMaybe [9]
Just 9
>>>
listToMaybe [1,2,3]
Just 1
Composing maybeToList
with listToMaybe
should be the identity
on singleton/empty lists:
>>>
maybeToList $ listToMaybe [5]
[5]>>>
maybeToList $ listToMaybe []
[]
But not on lists with more than one element:
>>>
maybeToList $ listToMaybe [1,2,3]
[1]
Data.List
unfoldr :: (b -> Maybe (a, b)) -> b -> [a] #
The unfoldr
function is a `dual' to foldr
: while foldr
reduces a list to a summary value, unfoldr
builds a list from
a seed value. The function takes the element and returns Nothing
if it is done producing the list or returns Just
(a,b)
, in which
case, a
is a prepended to the list and b
is used as the next
element in a recursive call. For example,
iterate f == unfoldr (\x -> Just (x, f x))
In some cases, unfoldr
can undo a foldr
operation:
unfoldr f' (foldr f z xs) == xs
if the following holds:
f' (f x y) = Just (x,y) f' z = Nothing
A simple use of unfoldr:
unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 [10,9,8,7,6,5,4,3,2,1]
isPrefixOf :: Eq a => [a] -> [a] -> Bool #
The isPrefixOf
function takes two lists and returns True
iff the first list is a prefix of the second.
isSuffixOf :: Eq a => [a] -> [a] -> Bool #
The isSuffixOf
function takes two lists and returns True
iff
the first list is a suffix of the second. The second list must be
finite.
intercalate :: [a] -> [[a]] -> [a] #
intercalate
xs xss
is equivalent to (
.
It inserts the list concat
(intersperse
xs xss))xs
in between the lists in xss
and concatenates the
result.
intersperse :: a -> [a] -> [a] #
The intersperse
function takes an element and a list and
`intersperses' that element between the elements of the list.
For example,
intersperse ',' "abcde" == "a,b,c,d,e"
Data.Foldable
class Foldable (t :: * -> *) where #
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)
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
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
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.
foldMap :: Foldable t => forall m a. Monoid m => (a -> m) -> t a -> m #
Map each element of the structure to a monoid, and combine the results.
foldr :: Foldable t => forall a b. (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
null :: Foldable t => forall a. t a -> Bool #
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.
length :: Foldable t => forall a. t a -> Int #
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.
foldl' :: Foldable t => forall b a. (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
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
.
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () #
Data.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
).
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_
.
traverse :: Traversable t => forall (f :: * -> *) a b. 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 :: Traversable t => forall (f :: * -> *) a. 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_
.
for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b) #
Control.Arrow
first :: Arrow a => forall b c d. a b c -> a (b, d) (c, d) #
Send the first component of the input through the argument arrow, and copy the rest unchanged to the output.
Control.Monad
liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r #
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
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when
.
when :: Applicative f => Bool -> f () -> f () #
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.
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
foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b #
The foldM
function is analogous to foldl
, except that its result is
encapsulated in a monad. Note that foldM
works from left-to-right over
the list arguments. This could be an issue where (
and the `folded
function' are not commutative.>>
)
foldM f a1 [x1, x2, ..., xm]
==
do a2 <- f a1 x1 a3 <- f a2 x2 ... f am xm
If right-to-left evaluation is required, the input list should be reversed.
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter
function.
Data.Char
Returns True
for any Unicode space character, and the control
characters \t
, \n
, \r
, \f
, \v
.
Selects upper-case or title-case alphabetic Unicode characters (letters). Title case is used by a small number of letter ligatures like the single-character form of Lj.
Selects alphabetic Unicode characters (lower-case, upper-case and
title-case letters, plus letters of caseless scripts and modifiers letters).
This function is equivalent to isLetter
.
isAlphaNum :: Char -> Bool #
Selects alphabetic or numeric digit Unicode characters.
Note that numeric digits outside the ASCII range are selected by this
function but not by isDigit
. Such digits may be part of identifiers
but are not used by the printer and reader to represent numbers.
Convert a letter to the corresponding lower-case letter, if any. Any other character is returned unchanged.
Convert a letter to the corresponding upper-case letter, if any. Any other character is returned unchanged.
Data.Word & Data.Int
Bounded Word | Since: 2.1 |
Enum Word | Since: 2.1 |
Eq Word | |
Integral Word | Since: 2.1 |
Data Word | Since: 4.0.0.0 |
Num Word | Since: 2.1 |
Ord Word | |
Read Word | Since: 4.5.0.0 |
Real Word | Since: 2.1 |
Show Word | Since: 2.1 |
Ix Word | Since: 4.6.0.0 |
Bits Word | Since: 2.1 |
FiniteBits Word | Since: 4.6.0.0 |
Binary Word | |
NFData Word | |
IArray UArray Word | |
Generic1 k (URec k Word) | |
MArray (STUArray s) Word (ST s) | |
Functor (URec * Word) | |
Foldable (URec * Word) | |
Traversable (URec * Word) | |
Eq (URec k Word p) | |
Ord (URec k Word p) | |
Show (URec k Word p) | |
Generic (URec k Word p) | |
data URec k Word | Used for marking occurrences of Since: 4.9.0.0 |
type Rep1 k (URec k Word) | |
type Rep (URec k Word p) | |
8-bit unsigned integer type
Bounded Word8 | Since: 2.1 |
Enum Word8 | Since: 2.1 |
Eq Word8 | Since: 2.1 |
Integral Word8 | Since: 2.1 |
Data Word8 | Since: 4.0.0.0 |
Num Word8 | Since: 2.1 |
Ord Word8 | Since: 2.1 |
Read Word8 | Since: 2.1 |
Real Word8 | Since: 2.1 |
Show Word8 | Since: 2.1 |
Ix Word8 | Since: 2.1 |
Bits Word8 | Since: 2.1 |
FiniteBits Word8 | Since: 4.6.0.0 |
Binary Word8 | |
NFData Word8 | |
IArray UArray Word8 | |
MArray (STUArray s) Word8 (ST s) | |
16-bit unsigned integer type
Bounded Word16 | Since: 2.1 |
Enum Word16 | Since: 2.1 |
Eq Word16 | Since: 2.1 |
Integral Word16 | Since: 2.1 |
Data Word16 | Since: 4.0.0.0 |
Num Word16 | Since: 2.1 |
Ord Word16 | Since: 2.1 |
Read Word16 | Since: 2.1 |
Real Word16 | Since: 2.1 |
Show Word16 | Since: 2.1 |
Ix Word16 | Since: 2.1 |
Bits Word16 | Since: 2.1 |
FiniteBits Word16 | Since: 4.6.0.0 |
Binary Word16 | |
NFData Word16 | |
IArray UArray Word16 | |
MArray (STUArray s) Word16 (ST s) | |
32-bit unsigned integer type
Bounded Word32 | Since: 2.1 |
Enum Word32 | Since: 2.1 |
Eq Word32 | Since: 2.1 |
Integral Word32 | Since: 2.1 |
Data Word32 | Since: 4.0.0.0 |
Num Word32 | Since: 2.1 |
Ord Word32 | Since: 2.1 |
Read Word32 | Since: 2.1 |
Real Word32 | Since: 2.1 |
Show Word32 | Since: 2.1 |
Ix Word32 | Since: 2.1 |
Bits Word32 | Since: 2.1 |
FiniteBits Word32 | Since: 4.6.0.0 |
Binary Word32 | |
NFData Word32 | |
IArray UArray Word32 | |
MArray (STUArray s) Word32 (ST s) | |
64-bit unsigned integer type
Bounded Word64 | Since: 2.1 |
Enum Word64 | Since: 2.1 |
Eq Word64 | Since: 2.1 |
Integral Word64 | Since: 2.1 |
Data Word64 | Since: 4.0.0.0 |
Num Word64 | Since: 2.1 |
Ord Word64 | Since: 2.1 |
Read Word64 | Since: 2.1 |
Real Word64 | Since: 2.1 |
Show Word64 | Since: 2.1 |
Ix Word64 | Since: 2.1 |
Bits Word64 | Since: 2.1 |
FiniteBits Word64 | Since: 4.6.0.0 |
Binary Word64 | |
NFData Word64 | |
IArray UArray Word64 | |
MArray (STUArray s) Word64 (ST s) | |
8-bit signed integer type
Bounded Int8 | Since: 2.1 |
Enum Int8 | Since: 2.1 |
Eq Int8 | Since: 2.1 |
Integral Int8 | Since: 2.1 |
Data Int8 | Since: 4.0.0.0 |
Num Int8 | Since: 2.1 |
Ord Int8 | Since: 2.1 |
Read Int8 | Since: 2.1 |
Real Int8 | Since: 2.1 |
Show Int8 | Since: 2.1 |
Ix Int8 | Since: 2.1 |
Bits Int8 | Since: 2.1 |
FiniteBits Int8 | Since: 4.6.0.0 |
Binary Int8 | |
NFData Int8 | |
IArray UArray Int8 | |
MArray (STUArray s) Int8 (ST s) | |
16-bit signed integer type
Bounded Int16 | Since: 2.1 |
Enum Int16 | Since: 2.1 |
Eq Int16 | Since: 2.1 |
Integral Int16 | Since: 2.1 |
Data Int16 | Since: 4.0.0.0 |
Num Int16 | Since: 2.1 |
Ord Int16 | Since: 2.1 |
Read Int16 | Since: 2.1 |
Real Int16 | Since: 2.1 |
Show Int16 | Since: 2.1 |
Ix Int16 | Since: 2.1 |
Bits Int16 | Since: 2.1 |
FiniteBits Int16 | Since: 4.6.0.0 |
Binary Int16 | |
NFData Int16 | |
IArray UArray Int16 | |
MArray (STUArray s) Int16 (ST s) | |
32-bit signed integer type
Bounded Int32 | Since: 2.1 |
Enum Int32 | Since: 2.1 |
Eq Int32 | Since: 2.1 |
Integral Int32 | Since: 2.1 |
Data Int32 | Since: 4.0.0.0 |
Num Int32 | Since: 2.1 |
Ord Int32 | Since: 2.1 |
Read Int32 | Since: 2.1 |
Real Int32 | Since: 2.1 |
Show Int32 | Since: 2.1 |
Ix Int32 | Since: 2.1 |
Bits Int32 | Since: 2.1 |
FiniteBits Int32 | Since: 4.6.0.0 |
Binary Int32 | |
NFData Int32 | |
IArray UArray Int32 | |
MArray (STUArray s) Int32 (ST s) | |
64-bit signed integer type
Bounded Int64 | Since: 2.1 |
Enum Int64 | Since: 2.1 |
Eq Int64 | Since: 2.1 |
Integral Int64 | Since: 2.1 |
Data Int64 | Since: 4.0.0.0 |
Num Int64 | Since: 2.1 |
Ord Int64 | Since: 2.1 |
Read Int64 | Since: 2.1 |
Real Int64 | Since: 2.1 |
Show Int64 | Since: 2.1 |
Ix Int64 | Since: 2.1 |
Bits Int64 | Since: 2.1 |
FiniteBits Int64 | Since: 4.6.0.0 |
Binary Int64 | |
NFData Int64 | |
IArray UArray Int64 | |
MArray (STUArray s) Int64 (ST s) | |