Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Maybe a :: * -> *
- data Ordering :: *
- data Bool :: *
- data Char :: *
- data IO a :: * -> *
- data Either a b :: * -> * -> *
- data ByteString :: *
- data Text :: *
- data Map k a :: * -> * -> *
- data HashMap k v :: * -> * -> *
- data IntMap a :: * -> *
- data Set a :: * -> *
- data HashSet a :: * -> *
- data IntSet :: *
- data Seq a :: * -> *
- newtype Identity a :: * -> * = Identity {
- runIdentity :: a
- data SomeException :: * where
- data SomeAsyncException :: * where
- type String = [Char]
- type FilePath = String
- data Word :: *
- data Word8 :: *
- data Word16 :: *
- data Word32 :: *
- data Word64 :: *
- data Int :: *
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- data Integer :: *
- type Rational = Ratio Integer
- data Float :: *
- data Double :: *
- data Proxy k t :: forall k. k -> * = Proxy
- class Eq a => Ord a where
- class Eq a where
- class Bounded a where
- class Show a where
- class Read a where
- class Functor f where
- class Functor f => Applicative f where
- class Applicative f => Alternative f where
- class Applicative m => Monad m where
- class Monad m => MonadIO m where
- class MonadTrans t where
- class Monad m => MonadReader r m | m -> r where
- class Monad m => MonadThrow m
- class (Typeable * e, Show e) => Exception e where
- toException :: e -> SomeException
- fromException :: SomeException -> Maybe e
- displayException :: e -> String
- class MonadThrow m => MonadCatch m
- class MonadCatch m => MonadMask m
- class Foldable t where
- fold :: Monoid m => t m -> m
- foldMap :: Monoid m => (a -> m) -> t a -> m
- foldr :: (a -> b -> b) -> b -> t a -> b
- foldr' :: (a -> b -> b) -> b -> t a -> b
- foldl :: (b -> a -> b) -> b -> t a -> b
- foldl' :: (b -> a -> b) -> b -> t a -> b
- toList :: t a -> [a]
- null :: t a -> Bool
- length :: t a -> Int
- elem :: Eq a => a -> t a -> Bool
- toList :: Foldable t => forall a. t a -> [a]
- null :: Foldable t => forall a. t a -> Bool
- length :: Foldable t => forall a. t a -> Int
- elem :: Foldable t => forall a. Eq a => a -> t a -> Bool
- class (Functor t, Foldable t) => Traversable t where
- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
- sequenceA :: Applicative f => t (f a) -> f (t a)
- class Typeable k a
- class IsString a where
- fromString :: String -> a
- class Hashable a where
- hashWithSalt :: Int -> a -> Int
- hash :: a -> Int
- class Semigroup a where
- class Monoid a where
- class Num a where
- class (Num a, Ord a) => Real a where
- toRational :: a -> Rational
- class (Real a, Enum a) => Integral a where
- class Num a => Fractional a where
- (/) :: a -> a -> a
- recip :: a -> a
- fromRational :: Rational -> a
- class Fractional a => Floating a where
- class (Real a, Fractional a) => RealFrac a where
- class (RealFrac a, Floating a) => RealFloat a where
- floatRadix :: a -> Integer
- floatDigits :: a -> Int
- floatRange :: a -> (Int, Int)
- decodeFloat :: a -> (Integer, Int)
- encodeFloat :: Integer -> Int -> a
- exponent :: a -> Int
- significand :: a -> a
- scaleFloat :: Int -> a -> a
- isNaN :: a -> Bool
- isInfinite :: a -> Bool
- isDenormalized :: a -> Bool
- isNegativeZero :: a -> Bool
- isIEEE :: a -> Bool
- atan2 :: a -> a -> a
- ($) :: (a -> b) -> a -> b
- (&) :: a -> (a -> b) -> b
- ($!) :: (a -> b) -> a -> b
- (&&) :: Bool -> Bool -> Bool
- (||) :: Bool -> Bool -> Bool
- (.) :: (b -> c) -> (a -> b) -> a -> c
- not :: Bool -> Bool
- otherwise :: Bool
- fst :: (a, b) -> a
- snd :: (a, b) -> b
- id :: a -> a
- maybe :: b -> (a -> b) -> Maybe a -> b
- either :: (a -> c) -> (b -> c) -> Either a b -> c
- flip :: (a -> b -> c) -> b -> a -> c
- const :: a -> b -> a
- odd :: Integral a => a -> Bool
- even :: Integral a => a -> Bool
- uncurry :: (a -> b -> c) -> (a, b) -> c
- curry :: ((a, b) -> c) -> a -> b -> c
- asTypeOf :: a -> a -> a
- seq :: a -> b -> b
- fix :: (a -> a) -> a
- (^) :: (Num a, Integral b) => a -> b -> a
- (^^) :: (Fractional a, Integral b) => a -> b -> a
- subtract :: Num a => a -> a -> a
- fromIntegral :: (Integral a, Num b) => a -> b
- realToFrac :: (Real a, Fractional b) => a -> b
- sum :: (Foldable f, Num a) => f a -> a
- product :: (Foldable f, Num a) => f a -> a
- foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b
- foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
- for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
- sequenceA_ :: (Foldable t, Applicative f) => t (f a) -> f ()
- asum :: (Foldable t, Alternative f) => t (f a) -> f a
- mapM_ :: (Applicative m, Foldable f) => (a -> m b) -> f a -> m ()
- forM_ :: (Applicative m, Foldable f) => f a -> (a -> m b) -> m ()
- sequence_ :: (Applicative m, Foldable f) => f (m a) -> m ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- concat :: Foldable t => t [a] -> [a]
- concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
- and :: Foldable t => t Bool -> Bool
- or :: Foldable t => t Bool -> Bool
- any :: Foldable t => (a -> Bool) -> t a -> Bool
- all :: Foldable t => (a -> Bool) -> t a -> Bool
- notElem :: (Foldable t, Eq a) => a -> t a -> Bool
- find :: Foldable t => (a -> Bool) -> t a -> Maybe a
- mapM :: (Applicative m, Traversable t) => (a -> m b) -> t a -> m (t b)
- sequence :: (Applicative m, Traversable t) => t (m a) -> m (t a)
- for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
- forM :: (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b)
- mapAccumL :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c)
- mapAccumR :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c)
- ($>) :: Functor f => f a -> b -> f b
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- void :: Functor f => f a -> f ()
- liftA :: Applicative f => (a -> b) -> f a -> f b
- liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- optional :: Alternative f => f a -> f (Maybe a)
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- forever :: Applicative f => f a -> f b
- join :: Monad m => m (m a) -> m a
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
- replicateM_ :: Applicative m => Int -> m a -> m ()
- guard :: Alternative f => Bool -> f ()
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- ap :: Monad m => m (a -> b) -> m a -> m b
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
- threadDelay :: Int -> IO ()
- data MVar a :: * -> *
- newEmptyMVar :: IO (MVar a)
- newMVar :: a -> IO (MVar a)
- takeMVar :: MVar a -> IO a
- putMVar :: MVar a -> a -> IO ()
- readMVar :: MVar a -> IO a
- swapMVar :: MVar a -> a -> IO a
- tryTakeMVar :: MVar a -> IO (Maybe a)
- tryPutMVar :: MVar a -> a -> IO Bool
- isEmptyMVar :: MVar a -> IO Bool
- withMVar :: MVar a -> (a -> IO b) -> IO b
- withMVarMasked :: MVar a -> (a -> IO b) -> IO b
- modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
- modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b
- modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO ()
- modifyMVarMasked :: MVar a -> (a -> IO (a, b)) -> IO b
- tryReadMVar :: MVar a -> IO (Maybe a)
- mkWeakMVar :: MVar a -> IO () -> IO (Weak (MVar a))
- data Chan a :: * -> *
- newChan :: IO (Chan a)
- writeChan :: Chan a -> a -> IO ()
- readChan :: Chan a -> IO a
- dupChan :: Chan a -> IO (Chan a)
- asks :: MonadReader r m => (r -> a) -> m a
- throwIO :: (MonadThrow m, Exception e) => e -> m a
- throwM :: (MonadThrow m, Exception e) => e -> m a
- throwTo :: (Exception e, MonadIO m) => ThreadId -> e -> m ()
- catch :: (MonadCatch m, Exception e) => m a -> (e -> m a) -> m a
- catchIO :: MonadCatch m => m a -> (IOException -> m a) -> m a
- catchAny :: MonadCatch m => m a -> (SomeException -> m a) -> m a
- catchDeep :: (MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> (e -> m a) -> m a
- catchAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => m a -> (SomeException -> m a) -> m a
- handle :: (MonadCatch m, Exception e) => (e -> m a) -> m a -> m a
- handleIO :: MonadCatch m => (IOException -> m a) -> m a -> m a
- handleAny :: MonadCatch m => (SomeException -> m a) -> m a -> m a
- handleDeep :: (MonadCatch m, Exception e, MonadIO m, NFData a) => (e -> m a) -> m a -> m a
- handleAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => (SomeException -> m a) -> m a -> m a
- try :: (MonadCatch m, Exception e) => m a -> m (Either e a)
- tryIO :: MonadCatch m => m a -> m (Either IOException a)
- tryAny :: MonadCatch m => m a -> m (Either SomeException a)
- tryDeep :: (MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> m (Either e a)
- tryAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => m a -> m (Either SomeException a)
- onException :: MonadMask m => m a -> m b -> m a
- bracket :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c
- bracket_ :: MonadMask m => m a -> m b -> m c -> m c
- finally :: MonadMask m => m a -> m b -> m a
- withException :: (MonadMask m, Exception e) => m a -> (e -> m b) -> m a
- bracketOnError :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c
- bracketOnError_ :: MonadMask m => m a -> m b -> m c -> m c
- displayException :: Exception e => e -> String
- (&&&) :: Arrow a => forall b c c'. a b c -> a b c' -> a b (c, c')
- (***) :: Arrow a => forall b c b' c'. a b c -> a b' c' -> a (b, b') (c, c')
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- catMaybes :: [Maybe a] -> [a]
- fromMaybe :: a -> Maybe a -> a
- isJust :: Maybe a -> Bool
- isNothing :: Maybe a -> Bool
- listToMaybe :: [a] -> Maybe a
- partitionEithers :: [Either a b] -> ([a], [b])
- lefts :: [Either a b] -> [a]
- rights :: [Either a b] -> [b]
- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
- comparing :: Ord a => (b -> a) -> b -> b -> Ordering
- say :: MonadIO m => Text -> m ()
- sayString :: MonadIO m => String -> m ()
- sayShow :: (MonadIO m, Show a) => a -> m ()
- sayErr :: MonadIO m => Text -> m ()
- sayErrString :: MonadIO m => String -> m ()
- sayErrShow :: (MonadIO m, Show a) => a -> m ()
- hSay :: MonadIO m => Handle -> Text -> m ()
- hSayString :: MonadIO m => Handle -> String -> m ()
- hSayShow :: (MonadIO m, Show a) => Handle -> a -> m ()
- data IORef a :: * -> *
- newIORef :: a -> IO (IORef a)
- readIORef :: IORef a -> IO a
- writeIORef :: IORef a -> a -> IO ()
- modifyIORef :: IORef a -> (a -> a) -> IO ()
- modifyIORef' :: IORef a -> (a -> a) -> IO ()
- atomicModifyIORef :: IORef a -> (a -> (a, b)) -> IO b
- atomicModifyIORef' :: IORef a -> (a -> (a, b)) -> IO b
- atomicWriteIORef :: IORef a -> a -> IO ()
- mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
- data Handle :: *
- data IOMode :: *
- stdin :: Handle
- stdout :: Handle
- stderr :: Handle
- hClose :: Handle -> IO ()
- withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
- readFile :: FilePath -> IO ByteString
- writeFile :: FilePath -> ByteString -> IO ()
- readFileUtf8 :: MonadIO m => FilePath -> m Text
- writeFileUtf8 :: MonadIO m => FilePath -> Text -> m ()
- encodeUtf8 :: Text -> ByteString
- decodeUtf8 :: ByteString -> Text
- class NFData a where
- rnf :: a -> ()
- deepseq :: NFData a => a -> b -> b
- ($!!) :: NFData a => (a -> b) -> a -> b
- force :: NFData a => a -> a
- (++) :: Monoid m => m -> m -> m
- readMaybe :: Read a => String -> Maybe a
- readEither :: Read a => String -> Either String a
Types
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.
Monad Maybe | |
Functor Maybe | |
Applicative Maybe | |
Foldable Maybe | |
Traversable Maybe | |
Generic1 Maybe | |
Alternative Maybe | |
MonadPlus Maybe | |
MonadThrow Maybe | |
Eq a => Eq (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) | |
NFData a => NFData (Maybe a) | |
Hashable a => Hashable (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 (==) (Maybe k) a b | |
type DemoteRep (Maybe a) (KProxy (Maybe a)) | |
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
).
Bounded Char | |
Enum Char | |
Eq Char | |
Ord Char | |
Read Char | |
Show Char | |
Lift Char | |
NFData Char | |
Hashable Char | |
ErrorList Char | |
Functor (URec Char) | |
IsString (Seq Char) | |
Foldable (URec Char) | |
Traversable (URec Char) | |
Generic1 (URec Char) | |
Eq (URec Char p) | |
Ord (URec Char p) | |
Show (URec Char p) | |
Generic (URec Char p) | |
data URec Char | Used for marking occurrences of |
type Rep1 (URec Char) | |
type Rep (URec Char p) | |
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.
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"
Monad (Either e) | |
Functor (Either a) | |
Applicative (Either e) | |
Foldable (Either a) | |
Traversable (Either a) | |
Generic1 (Either a) | |
(~) * e SomeException => MonadThrow (Either e) | |
(~) * e SomeException => MonadCatch (Either e) | Since: 0.8.3 |
(~) * e SomeException => MonadMask (Either e) | Since: 0.8.3 |
(Eq a, Eq b) => Eq (Either a b) | |
(Ord a, Ord b) => Ord (Either a b) | |
(Read a, Read b) => Read (Either a b) | |
(Show a, Show b) => Show (Either a b) | |
Generic (Either a b) | |
Semigroup (Either a b) | |
(Lift a, Lift b) => Lift (Either a b) | |
(NFData a, NFData b) => NFData (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 | |
data ByteString :: * #
A space-efficient representation of a Word8
vector, supporting many
efficient operations.
A ByteString
contains 8-bit bytes, or by using the operations from
Data.ByteString.Char8 it can be interpreted as containing 8-bit
characters.
A Map from keys k
to values a
.
Functor (Map k) | |
Foldable (Map k) | |
Traversable (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) | |
(NFData k, NFData a) => NFData (Map k a) | |
type Item (Map k v) | |
data HashMap k v :: * -> * -> * #
A map from keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Functor (HashMap k) | |
Foldable (HashMap k) | |
Traversable (HashMap k) | |
(Eq k, Hashable k) => IsList (HashMap k v) | |
(Eq k, Eq v) => Eq (HashMap k v) | |
(Data k, Data v, Eq k, Hashable k) => Data (HashMap k v) | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) | |
(Show k, Show v) => Show (HashMap k v) | |
(Eq k, Hashable k) => Semigroup (HashMap k v) | |
(Eq k, Hashable k) => Monoid (HashMap k v) | |
(NFData k, NFData v) => NFData (HashMap k v) | |
(Hashable k, Hashable v) => Hashable (HashMap k v) | |
type Item (HashMap k v) | |
A map of integers to values a
.
A set of values a
.
A set of values. A set cannot contain duplicate values.
Foldable HashSet | |
(Eq a, Hashable a) => IsList (HashSet a) | |
(Hashable a, Eq a) => Eq (HashSet a) | |
(Data a, Eq a, Hashable a) => Data (HashSet a) | |
(Eq a, Hashable a, Read a) => Read (HashSet a) | |
Show a => Show (HashSet a) | |
(Hashable a, Eq a) => Semigroup (HashSet a) | |
(Hashable a, Eq a) => Monoid (HashSet a) | |
NFData a => NFData (HashSet a) | |
Hashable a => Hashable (HashSet a) | |
type Item (HashSet a) | |
A set of integers.
General-purpose finite sequences.
Monad Seq | |
Functor Seq | |
Applicative Seq | |
Foldable Seq | |
Traversable Seq | |
Alternative Seq | |
MonadPlus Seq | |
IsList (Seq a) | |
Eq a => Eq (Seq a) | |
Data a => Data (Seq a) | |
Ord a => Ord (Seq a) | |
Read a => Read (Seq a) | |
Show a => Show (Seq a) | |
IsString (Seq Char) | |
Semigroup (Seq a) | |
Monoid (Seq a) | |
NFData a => NFData (Seq a) | |
type Item (Seq a) | |
newtype Identity a :: * -> * #
Identity functor and monad. (a non-strict monad)
Since: 4.8.0.0
Identity | |
|
data SomeException :: * where #
The SomeException
type is the root of the exception type hierarchy.
When an exception of type e
is thrown, behind the scenes it is
encapsulated in a SomeException
.
data SomeAsyncException :: * where #
Superclass for asynchronous exceptions.
Since: 4.7.0.0
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.
Numbers
Bounded Word | |
Enum Word | |
Eq Word | |
Integral Word | |
Num Word | |
Ord Word | |
Read Word | |
Real Word | |
Show Word | |
Lift Word | |
NFData Word | |
Hashable Word | |
Functor (URec Word) | |
Foldable (URec Word) | |
Traversable (URec Word) | |
Generic1 (URec Word) | |
Eq (URec Word p) | |
Ord (URec Word p) | |
Show (URec Word p) | |
Generic (URec Word p) | |
data URec Word | Used for marking occurrences of |
type Rep1 (URec Word) | |
type Rep (URec Word p) | |
8-bit unsigned integer type
16-bit unsigned integer type
32-bit unsigned integer type
64-bit unsigned integer type
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.
Bounded Int | |
Enum Int | |
Eq Int | |
Integral Int | |
Num Int | |
Ord Int | |
Read Int | |
Real Int | |
Show Int | |
Lift Int | |
NFData Int | |
Hashable Int | |
Functor (URec Int) | |
Foldable (URec Int) | |
Traversable (URec Int) | |
Generic1 (URec 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 |
type Rep1 (URec Int) | |
type Rep (URec Int p) | |
8-bit signed integer type
16-bit signed integer type
32-bit signed integer type
64-bit signed integer type
Single-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE single-precision type.
Eq Float | |
Floating Float | |
Ord Float | |
Read Float | |
RealFloat Float | |
Lift Float | |
NFData Float | |
Hashable Float | |
Functor (URec Float) | |
Foldable (URec Float) | |
Traversable (URec Float) | |
Generic1 (URec Float) | |
Eq (URec Float p) | |
Ord (URec Float p) | |
Show (URec Float p) | |
Generic (URec Float p) | |
data URec Float | Used for marking occurrences of |
type Rep1 (URec Float) | |
type Rep (URec Float p) | |
Double-precision floating point numbers. It is desirable that this type be at least equal in range and precision to the IEEE double-precision type.
Eq Double | |
Floating Double | |
Ord Double | |
Read Double | |
RealFloat Double | |
Lift Double | |
NFData Double | |
Hashable Double | |
Functor (URec Double) | |
Foldable (URec Double) | |
Traversable (URec Double) | |
Generic1 (URec Double) | |
Eq (URec Double p) | |
Ord (URec Double p) | |
Show (URec Double p) | |
Generic (URec Double p) | |
data URec Double | Used for marking occurrences of |
type Rep1 (URec Double) | |
type Rep (URec Double p) | |
data Proxy k t :: forall k. k -> * #
A concrete, poly-kinded proxy type
Monad (Proxy *) | |
Functor (Proxy *) | |
Applicative (Proxy *) | |
Foldable (Proxy *) | |
Traversable (Proxy *) | |
Generic1 (Proxy *) | |
Alternative (Proxy *) | |
MonadPlus (Proxy *) | |
Bounded (Proxy k s) | |
Enum (Proxy k s) | |
Eq (Proxy k s) | |
Ord (Proxy k s) | |
Read (Proxy k s) | |
Show (Proxy k s) | |
Ix (Proxy k s) | |
Generic (Proxy k t) | |
Semigroup (Proxy k s) | |
Monoid (Proxy k s) | |
NFData (Proxy k a) | Since: 1.4.0.0 |
type Rep1 (Proxy *) | |
type Rep (Proxy k t) | |
Type classes
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.
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
.
Eq Bool | |
Eq Char | |
Eq Double | |
Eq Float | |
Eq Int | |
Eq Int8 | |
Eq Int16 | |
Eq Int32 | |
Eq Int64 | |
Eq Integer | |
Eq Ordering | |
Eq Word | |
Eq Word8 | |
Eq Word16 | |
Eq Word32 | |
Eq Word64 | |
Eq TypeRep | |
Eq Exp | |
Eq Match | |
Eq Clause | |
Eq Pat | |
Eq Type | |
Eq Dec | |
Eq Name | |
Eq FunDep | |
Eq TyVarBndr | |
Eq InjectivityAnn | |
Eq Overlap | |
Eq () | |
Eq TyCon | |
Eq Handle | |
Eq BigNat | |
Eq SpecConstrAnnotation | |
Eq Void | |
Eq Version | |
Eq HandlePosn | |
Eq ThreadId | |
Eq BlockReason | |
Eq ThreadStatus | |
Eq AsyncException | |
Eq ArrayException | |
Eq ExitCode | |
Eq IOErrorType | |
Eq BufferMode | |
Eq Newline | |
Eq NewlineMode | |
Eq All | |
Eq Any | |
Eq Fixity | |
Eq Associativity | |
Eq SourceUnpackedness | |
Eq SourceStrictness | |
Eq DecidedStrictness | |
Eq MaskingState | |
Eq IOException | |
Eq ErrorCall | |
Eq ArithException | |
Eq SomeNat | |
Eq SomeSymbol | |
Eq IOMode | |
Eq SrcLoc | |
Eq ByteString | |
Eq IntSet | |
Eq Extension | |
Eq ModName | |
Eq PkgName | |
Eq Module | |
Eq OccName | |
Eq NameFlavour | |
Eq NameSpace | |
Eq Loc | |
Eq Info | |
Eq ModuleInfo | |
Eq Fixity | |
Eq FixityDirection | |
Eq Lit | |
Eq Body | |
Eq Guard | |
Eq Stmt | |
Eq Range | |
Eq TypeFamilyHead | |
Eq TySynEqn | |
Eq FamFlavour | |
Eq Foreign | |
Eq Callconv | |
Eq Safety | |
Eq Pragma | |
Eq Inline | |
Eq RuleMatch | |
Eq Phases | |
Eq RuleBndr | |
Eq AnnTarget | |
Eq SourceUnpackedness | |
Eq SourceStrictness | |
Eq DecidedStrictness | |
Eq Con | |
Eq Bang | |
Eq FamilyResultSig | |
Eq TyLit | |
Eq Role | |
Eq AnnLookup | |
Eq CodePoint | |
Eq DecoderState | |
Eq UnicodeException | |
Eq a => Eq [a] | |
Eq a => Eq (Maybe a) | |
Eq a => Eq (Ratio a) | |
Eq (Ptr a) | |
Eq (FunPtr a) | |
Eq (V1 p) | |
Eq (U1 p) | |
Eq p => Eq (Par1 p) | |
Eq (ForeignPtr a) | |
Eq a => Eq (Identity a) | |
Eq a => Eq (Min a) | |
Eq a => Eq (Max a) | |
Eq a => Eq (First a) | |
Eq a => Eq (Last a) | |
Eq m => Eq (WrappedMonoid m) | |
Eq a => Eq (Option a) | |
Eq a => Eq (NonEmpty a) | |
Eq a => Eq (Complex a) | |
Eq (Chan a) | |
Eq a => Eq (ZipList a) | |
Eq (TVar a) | |
Eq a => Eq (Dual a) | |
Eq a => Eq (Sum a) | |
Eq a => Eq (Product a) | |
Eq a => Eq (First a) | |
Eq a => Eq (Last a) | |
Eq (IORef a) | |
Eq a => Eq (Down a) | |
Eq (MVar a) | |
Eq a => Eq (Seq a) | |
Eq a => Eq (ViewL a) | |
Eq a => Eq (ViewR a) | |
Eq a => Eq (IntMap a) | |
Eq a => Eq (Set a) | |
(Hashable a, Eq a) => Eq (HashSet a) | |
(Eq a, Eq b) => Eq (Either a b) | |
Eq (f p) => Eq (Rec1 f p) | |
Eq (URec Char p) | |
Eq (URec Double p) | |
Eq (URec Float p) | |
Eq (URec Int p) | |
Eq (URec Word p) | |
Eq (URec (Ptr ()) p) | |
(Eq a, Eq b) => Eq (a, b) | |
Eq a => Eq (Arg a b) | |
Eq (Proxy k s) | |
Eq (STRef s a) | |
(Eq k, Eq a) => Eq (Map k a) | |
(Eq k, Eq v) => Eq (Leaf k v) | |
(Eq k, Eq v) => Eq (HashMap k v) | |
Eq c => Eq (K1 i c p) | |
(Eq (f p), Eq (g p)) => Eq ((:+:) f g p) | |
(Eq (f p), Eq (g p)) => Eq ((:*:) f g p) | |
Eq (f (g p)) => Eq ((:.:) f g p) | |
(Eq a, Eq b, Eq c) => Eq (a, b, c) | |
Eq a => Eq (Const k a b) | |
Eq (f a) => Eq (Alt k f a) | |
Eq ((:~:) k a b) | |
(Eq e, Eq1 m, Eq a) => Eq (ErrorT e m a) | |
Eq (f p) => Eq (M1 i c f p) | |
(Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d) | |
(Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (a, b, c, d, e) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f) => Eq (a, b, c, d, e, f) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g) => Eq (a, b, c, d, e, f, g) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h) => Eq (a, b, c, d, e, f, g, h) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i) => Eq (a, b, c, d, e, f, g, h, i) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j) => Eq (a, b, c, d, e, f, g, h, i, j) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k) => Eq (a, b, c, d, e, f, g, h, i, j, k) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l) => Eq (a, b, c, d, e, f, g, h, i, j, k, l) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | |
(Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i, Eq j, Eq k, Eq l, Eq m, Eq n, Eq o) => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | |
The Bounded
class is used to name the upper and lower limits of a
type. Ord
is not a superclass of Bounded
since types that are not
totally ordered may also have upper and lower bounds.
The Bounded
class may be derived for any enumeration type;
minBound
is the first constructor listed in the data
declaration
and maxBound
is the last.
Bounded
may also be derived for single-constructor datatypes whose
constituent types are in Bounded
.
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)"
.
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
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 Void | Reading a |
Read Version | |
Read ExitCode | |
Read BufferMode | |
Read Newline | |
Read NewlineMode | |
Read All | |
Read Any | |
Read Fixity | |
Read Associativity | |
Read SourceUnpackedness | |
Read SourceStrictness | |
Read DecidedStrictness | |
Read SomeNat | |
Read SomeSymbol | |
Read IOMode | |
Read Lexeme | |
Read GeneralCategory | |
Read ByteString | |
Read IntSet | |
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 a => Read (Seq a) | |
Read a => Read (ViewL a) | |
Read a => Read (ViewR a) | |
Read e => Read (IntMap e) | |
(Read a, Ord a) => Read (Set a) | |
(Eq a, Hashable a, Read a) => Read (HashSet a) | |
(Read a, Read b) => 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 a, Read b) => Read (Arg a b) | |
Read (Proxy k s) | |
(Ord k, Read k, Read e) => Read (Map k e) | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) | |
Read c => Read (K1 i c p) | |
(Read (f p), Read (g p)) => Read ((:+:) f g p) | |
(Read (f p), Read (g 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) | |
(~) k a b => Read ((:~:) k a b) | |
(Read e, Read1 m, Read a) => Read (ErrorT e m a) | |
Read (f p) => Read (M1 i c f p) | |
(Read a, Read b, Read c, Read d) => Read (a, b, c, d) | |
(Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) | |
(Read a, Read b, Read c, Read d, Read e, Read f) => Read (a, b, c, d, e, f) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g) => Read (a, b, c, d, e, f, g) | |
(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 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) | |
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.
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).
class Applicative f => Alternative f where #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
Alternative [] | |
Alternative Maybe | |
Alternative IO | |
Alternative U1 | |
Alternative P | |
Alternative Option | |
Alternative STM | |
Alternative ReadPrec | |
Alternative ReadP | |
Alternative Seq | |
Alternative f => Alternative (Rec1 f) | |
MonadPlus m => Alternative (WrappedMonad m) | |
ArrowPlus a => Alternative (ArrowMonad a) | |
Alternative (Proxy *) | |
(Alternative f, Alternative g) => Alternative ((:*:) f g) | |
(Alternative f, Applicative g) => Alternative ((:.:) f g) | |
(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) | |
Alternative f => Alternative (Alt * f) | |
(Functor m, Monad m, Error e) => Alternative (ErrorT e m) | |
Alternative f => Alternative (M1 i c f) | |
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.
Monad [] | |
Monad Maybe | |
Monad IO | |
Monad U1 | |
Monad Par1 | |
Monad Q | |
Monad P | |
Monad Identity | |
Monad Min | |
Monad Max | |
Monad First | |
Monad Last | |
Monad Option | |
Monad NonEmpty | |
Monad Complex | |
Monad STM | |
Monad Dual | |
Monad Sum | |
Monad Product | |
Monad First | |
Monad Last | |
Monad ReadPrec | |
Monad ReadP | |
Monad Seq | |
Monad ((->) r) | |
Monad (Either e) | |
Monad f => Monad (Rec1 f) | |
Monoid a => Monad ((,) a) | |
Monad m => Monad (WrappedMonad m) | |
ArrowApply a => Monad (ArrowMonad a) | |
Monad (Proxy *) | |
Monad (State s) | |
(Monad f, Monad g) => Monad ((:*:) f g) | |
Monad f => Monad (Alt * f) | |
(Monad m, Error e) => Monad (ErrorT e m) | |
Monad f => Monad (M1 i c f) | |
class MonadTrans t where #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift
is a monad transformation:
MonadTrans (ErrorT e) | |
class Monad m => MonadReader r m | m -> r where #
See examples in Control.Monad.Reader.
Note, the partially applied function type (->) r
is a simple reader monad.
See the instance
declaration below.
MonadReader r m => MonadReader r (MaybeT m) | |
MonadReader r m => MonadReader r (ListT m) | |
MonadReader r ((->) r) | |
(Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
(Monoid w, MonadReader r m) => MonadReader r (WriterT w m) | |
MonadReader r m => MonadReader r (StateT s m) | |
MonadReader r m => MonadReader r (StateT s m) | |
MonadReader r m => MonadReader r (IdentityT * m) | |
MonadReader r m => MonadReader r (ExceptT e m) | |
(Error e, MonadReader r m) => MonadReader r (ErrorT e m) | |
Monad m => MonadReader r (ReaderT * r m) | |
MonadReader r' m => MonadReader r' (ContT * r m) | |
(Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
(Monad m, Monoid w) => MonadReader r (RWST r w s m) | |
class Monad m => MonadThrow m #
A class for monads in which exceptions may be thrown.
Instances should obey the following law:
throwM e >> x = throwM e
In other words, throwing an exception short-circuits the rest of the monadic computation.
MonadThrow [] | |
MonadThrow Maybe | |
MonadThrow IO | |
MonadThrow Q | |
MonadThrow STM | |
(~) * e SomeException => MonadThrow (Either e) | |
MonadThrow m => MonadThrow (ListT m) | |
MonadThrow m => MonadThrow (MaybeT m) | Throws exceptions into the base monad. |
MonadThrow m => MonadThrow (ExceptT e m) | Throws exceptions into the base monad. |
(Error e, MonadThrow m) => MonadThrow (ErrorT e m) | Throws exceptions into the base monad. |
MonadThrow m => MonadThrow (StateT s m) | |
MonadThrow m => MonadThrow (StateT s m) | |
(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) | |
(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) | |
MonadThrow m => MonadThrow (IdentityT * m) | |
MonadThrow m => MonadThrow (ContT * r m) | |
MonadThrow m => MonadThrow (ReaderT * r m) | |
(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) | |
(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) | |
class (Typeable * e, Show e) => Exception e where #
Any type that you wish to throw or catch as an exception must be an
instance of the Exception
class. The simplest case is a new exception
type directly below the root:
data MyException = ThisException | ThatException deriving (Show, Typeable) instance Exception MyException
The default method definitions in the Exception
class do what we need
in this case. You can now throw and catch ThisException
and
ThatException
as exceptions:
*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException)) Caught ThisException
In more complicated examples, you may wish to define a whole hierarchy of exceptions:
--------------------------------------------------------------------- -- Make the root exception type for all the exceptions in a compiler data SomeCompilerException = forall e . Exception e => SomeCompilerException e deriving Typeable instance Show SomeCompilerException where show (SomeCompilerException e) = show e instance Exception SomeCompilerException compilerExceptionToException :: Exception e => e -> SomeException compilerExceptionToException = toException . SomeCompilerException compilerExceptionFromException :: Exception e => SomeException -> Maybe e compilerExceptionFromException x = do SomeCompilerException a <- fromException x cast a --------------------------------------------------------------------- -- Make a subhierarchy for exceptions in the frontend of the compiler data SomeFrontendException = forall e . Exception e => SomeFrontendException e deriving Typeable instance Show SomeFrontendException where show (SomeFrontendException e) = show e instance Exception SomeFrontendException where toException = compilerExceptionToException fromException = compilerExceptionFromException frontendExceptionToException :: Exception e => e -> SomeException frontendExceptionToException = toException . SomeFrontendException frontendExceptionFromException :: Exception e => SomeException -> Maybe e frontendExceptionFromException x = do SomeFrontendException a <- fromException x cast a --------------------------------------------------------------------- -- Make an exception type for a particular frontend compiler exception data MismatchedParentheses = MismatchedParentheses deriving (Typeable, Show) instance Exception MismatchedParentheses where toException = frontendExceptionToException fromException = frontendExceptionFromException
We can now catch a MismatchedParentheses
exception as
MismatchedParentheses
, SomeFrontendException
or
SomeCompilerException
, but not other types, e.g. IOException
:
*Main> throw MismatchedParenthesescatch
e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatch
e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatch
e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatch
e -> putStrLn ("Caught " ++ show (e :: IOException)) *** Exception: MismatchedParentheses
Nothing
class MonadThrow m => MonadCatch m #
A class for monads which allow exceptions to be caught, in particular
exceptions which were thrown by throwM
.
Instances should obey the following law:
catch (throwM e) f = f e
Note that the ability to catch an exception does not guarantee that we can
deal with all possible exit points from a computation. Some monads, such as
continuation-based stacks, allow for more than just a success/failure
strategy, and therefore catch
cannot be used by those monads to properly
implement a function such as finally
. For more information, see
MonadMask
.
MonadCatch IO | |
MonadCatch STM | |
(~) * e SomeException => MonadCatch (Either e) | Since: 0.8.3 |
MonadCatch m => MonadCatch (ListT m) | |
MonadCatch m => MonadCatch (MaybeT m) | Catches exceptions from the base monad. |
MonadCatch m => MonadCatch (ExceptT e m) | Catches exceptions from the base monad. |
(Error e, MonadCatch m) => MonadCatch (ErrorT e m) | Catches exceptions from the base monad. |
MonadCatch m => MonadCatch (StateT s m) | |
MonadCatch m => MonadCatch (StateT s m) | |
(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) | |
(MonadCatch m, Monoid w) => MonadCatch (WriterT w m) | |
MonadCatch m => MonadCatch (IdentityT * m) | |
MonadCatch m => MonadCatch (ReaderT * r m) | |
(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) | |
(MonadCatch m, Monoid w) => MonadCatch (RWST r w s m) | |
class MonadCatch m => MonadMask m #
A class for monads which provide for the ability to account for all
possible exit points from a computation, and to mask asynchronous
exceptions. Continuation-based monads, and stacks such as ErrorT e IO
which provide for multiple failure modes, are invalid instances of this
class.
Note that this package does provide a MonadMask
instance for CatchT
.
This instance is only valid if the base monad provides no ability to
provide multiple exit. For example, IO
or Either
would be invalid base
monads, but Reader
or State
would be acceptable.
Instances should ensure that, in the following code:
f `finally` g
The action g
is called regardless of what occurs within f
, including
async exceptions.
MonadMask IO | |
(~) * e SomeException => MonadMask (Either e) | Since: 0.8.3 |
MonadMask m => MonadMask (StateT s m) | |
MonadMask m => MonadMask (StateT s m) | |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
MonadMask m => MonadMask (IdentityT * m) | |
MonadMask m => MonadMask (ReaderT * r m) | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
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)
Foldable [] | |
Foldable Maybe | |
Foldable V1 | |
Foldable U1 | |
Foldable Par1 | |
Foldable Identity | |
Foldable Min | |
Foldable Max | |
Foldable First | |
Foldable Last | |
Foldable Option | |
Foldable NonEmpty | |
Foldable Complex | |
Foldable ZipList | |
Foldable Dual | |
Foldable Sum | |
Foldable Product | |
Foldable First | |
Foldable Last | |
Foldable Digit | |
Foldable Node | |
Foldable Elem | |
Foldable FingerTree | |
Foldable Seq | |
Foldable ViewL | |
Foldable ViewR | |
Foldable IntMap | |
Foldable Set | |
Foldable HashSet | |
Foldable (Either a) | |
Foldable f => Foldable (Rec1 f) | |
Foldable (URec Char) | |
Foldable (URec Double) | |
Foldable (URec Float) | |
Foldable (URec Int) | |
Foldable (URec Word) | |
Foldable (URec (Ptr ())) | |
Foldable ((,) a) | |
Foldable (Array i) | |
Foldable (Arg a) | |
Foldable (Proxy *) | |
Foldable (Map k) | |
Foldable (HashMap k) | |
Foldable (K1 i c) | |
(Foldable f, Foldable g) => Foldable ((:+:) f g) | |
(Foldable f, Foldable g) => Foldable ((:*:) f g) | |
(Foldable f, Foldable g) => Foldable ((:.:) f g) | |
Foldable (Const * m) | |
Foldable f => Foldable (ErrorT e f) | |
Foldable f => Foldable (M1 i c f) | |
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.
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
).
The class Typeable
allows a concrete representation of a type to
be calculated.
Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).
The class of types that can be converted to a hash value.
Minimal implementation: hashWithSalt
.
Nothing
The class of semigroups (types with an associative binary operation).
Since: 4.9.0.0
Nothing
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
.
Monoid Ordering | |
Monoid () | |
Monoid All | |
Monoid Any | |
Monoid ByteString | |
Monoid IntSet | |
Monoid [a] | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
Monoid a => Monoid (IO a) | |
Ord a => Monoid (Max a) | |
Ord a => Monoid (Min a) | |
Monoid a => Monoid (Identity a) | |
(Ord a, Bounded a) => Monoid (Min a) | |
(Ord a, Bounded a) => Monoid (Max a) | |
Monoid m => Monoid (WrappedMonoid m) | |
Semigroup a => Monoid (Option a) | |
Monoid a => Monoid (Dual a) | |
Monoid (Endo a) | |
Num a => Monoid (Sum a) | |
Num a => Monoid (Product a) | |
Monoid (First a) | |
Monoid (Last a) | |
Monoid (Seq a) | |
Monoid (IntMap a) | |
Ord a => Monoid (Set a) | |
(Hashable a, Eq a) => Monoid (HashSet a) | |
Monoid b => Monoid (a -> b) | |
(Monoid a, Monoid b) => Monoid (a, b) | |
Monoid (Proxy k s) | |
Ord k => Monoid (Map k v) | |
(Eq k, Hashable k) => Monoid (HashMap k v) | |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) | |
Monoid a => Monoid (Const k a b) | |
Alternative f => Monoid (Alt * f a) | |
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) | |
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) | |
Numeric
Basic numeric class.
Num Int | |
Num Int8 | |
Num Int16 | |
Num Int32 | |
Num Int64 | |
Num Integer | |
Num Word | |
Num Word8 | |
Num Word16 | |
Num Word32 | |
Num Word64 | |
Num CodePoint | |
Num DecoderState | |
Integral a => Num (Ratio a) | |
Num a => Num (Identity a) | |
Num a => Num (Min a) | |
Num a => Num (Max a) | |
RealFloat a => Num (Complex a) | |
Num a => Num (Sum a) | |
Num a => Num (Product a) | |
Num a => Num (Const k a b) | |
Num (f a) => Num (Alt k f a) | |
class Num a => Fractional a where #
Fractional numbers, supporting real division.
fromRational, (recip | (/))
Integral a => Fractional (Ratio a) | |
Fractional a => Fractional (Identity a) | |
RealFloat a => Fractional (Complex a) | |
Fractional a => Fractional (Const k a b) | |
class Fractional a => Floating a where #
Trigonometric and hyperbolic functions and related functions.
class (Real a, Fractional a) => RealFrac a where #
Extracting components of fractions.
class (RealFrac a, Floating a) => RealFloat a where #
Efficient, machine-independent access to the components of a floating-point number.
Functions
($) :: (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.
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
""
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
flip :: (a -> b -> c) -> b -> a -> c #
takes its (first) two arguments in the reverse order of flip
ff
.
const x
is a unary function which evaluates to x
for all inputs.
For instance,
>>>
map (const 42) [0..3]
[42,42,42,42]
uncurry :: (a -> b -> c) -> (a, b) -> c #
uncurry
converts a curried function to a function on pairs.
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.
is the least fixed point of the function fix
ff
,
i.e. the least defined x
such that f x = x
.
Numeric
(^^) :: (Fractional a, Integral b) => a -> b -> a infixr 8 #
raise a number to an integral power
fromIntegral :: (Integral a, Num b) => a -> b #
general coercion from integral types
realToFrac :: (Real a, Fractional b) => a -> b #
general coercion to fractional types
Foldable
foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b #
Monadic fold over the elements of a structure, associating to the right, i.e. from right to left.
foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b #
Monadic fold over the elements of a structure, associating to the left, i.e. from left to right.
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 () #
sequenceA_ :: (Foldable t, Applicative f) => t (f a) -> f () #
Evaluate each action in the structure from left to right, and
ignore the results. For a version that doesn't ignore the results
see sequenceA
.
asum :: (Foldable t, Alternative f) => t (f a) -> f a #
The sum of a collection of actions, generalizing concat
.
mapM_ :: (Applicative m, Foldable f) => (a -> m b) -> f a -> m () Source #
Synonym for traverse_
; different from base to generalize to
Applicative
.
Since: 0.1.0.0
forM_ :: (Applicative m, Foldable f) => f a -> (a -> m b) -> m () Source #
Flipped version of mapM_
.
Since: 0.1.0.0
sequence_ :: (Applicative m, Foldable f) => f (m a) -> m () Source #
Synonym for sequence_
; different from base to generalize to
Applicative
.
Since: 0.1.0.0
concat :: Foldable t => t [a] -> [a] #
The concatenation of all the elements of a container of lists.
concatMap :: Foldable t => (a -> [b]) -> t a -> [b] #
Map a function over all the elements of a container and concatenate the resulting lists.
any :: Foldable t => (a -> Bool) -> t a -> Bool #
Determines whether any element of the structure satisfies the predicate.
all :: Foldable t => (a -> Bool) -> t a -> Bool #
Determines whether all elements of the structure satisfy the predicate.
Traversable
mapM :: (Applicative m, Traversable t) => (a -> m b) -> t a -> m (t b) Source #
Synonym for traverse
; different from base to generalize to
Applicative
.
Since: 0.1.0.0
sequence :: (Applicative m, Traversable t) => t (m a) -> m (t a) Source #
Synonym for sequenceA
; different from base to generalize to
Applicative
.
Since: 0.1.0.0
for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b) #
forM :: (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b) Source #
Flipped version of mapM
.
Since: 0.1.0.0
mapAccumL :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c) #
mapAccumR :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c) #
Functor
($>) :: 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
(<$>) :: 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)
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
Applicative
liftA :: Applicative f => (a -> b) -> f a -> f b #
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c #
Lift a binary function to actions.
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d #
Lift a ternary function to actions.
Alternative
optional :: Alternative f => f a -> f (Maybe a) #
One or none.
Monad
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=
, but with the arguments interchanged.
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right Kleisli composition of monads.
forever :: Applicative f => f a -> f b #
repeats the action infinitely.forever
act
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.
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.
foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m () #
Like foldM
, but discards the result.
replicateM_ :: Applicative m => Int -> m a -> m () #
Like replicateM
, but discards the result.
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.
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when
.
Concurrent
threadDelay :: Int -> IO () #
Suspends the current thread for a given number of microseconds (GHC only).
There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
An MVar
(pronounced "em-var") is a synchronising variable, used
for communication between concurrent threads. It can be thought of
as a a box, which may be empty or full.
newEmptyMVar :: IO (MVar a) #
Create an MVar
which is initially empty.
Return the contents of the MVar
. If the MVar
is currently
empty, takeMVar
will wait until it is full. After a takeMVar
,
the MVar
is left empty.
There are two further important properties of takeMVar
:
takeMVar
is single-wakeup. That is, if there are multiple threads blocked intakeMVar
, and theMVar
becomes full, only one thread will be woken up. The runtime guarantees that the woken thread completes itstakeMVar
operation.- When multiple threads are blocked on an
MVar
, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built usingMVar
s.
putMVar :: MVar a -> a -> IO () #
Put a value into an MVar
. If the MVar
is currently full,
putMVar
will wait until it becomes empty.
There are two further important properties of putMVar
:
putMVar
is single-wakeup. That is, if there are multiple threads blocked inputMVar
, and theMVar
becomes empty, only one thread will be woken up. The runtime guarantees that the woken thread completes itsputMVar
operation.- When multiple threads are blocked on an
MVar
, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built usingMVar
s.
Atomically read the contents of an MVar
. If the MVar
is
currently empty, readMVar
will wait until its full.
readMVar
is guaranteed to receive the next putMVar
.
readMVar
is multiple-wakeup, so when multiple readers are
blocked on an MVar
, all of them are woken up at the same time.
Compatibility note: Prior to base 4.7, readMVar
was a combination
of takeMVar
and putMVar
. This mean that in the presence of
other threads attempting to putMVar
, readMVar
could block.
Furthermore, readMVar
would not receive the next putMVar
if there
was already a pending thread blocked on takeMVar
. The old behavior
can be recovered by implementing 'readMVar as follows:
readMVar :: MVar a -> IO a readMVar m = mask_ $ do a <- takeMVar m putMVar m a return a
tryTakeMVar :: MVar a -> IO (Maybe a) #
A non-blocking version of takeMVar
. The tryTakeMVar
function
returns immediately, with Nothing
if the MVar
was empty, or
if the Just
aMVar
was full with contents a
. After tryTakeMVar
,
the MVar
is left empty.
tryPutMVar :: MVar a -> a -> IO Bool #
A non-blocking version of putMVar
. The tryPutMVar
function
attempts to put the value a
into the MVar
, returning True
if
it was successful, or False
otherwise.
isEmptyMVar :: MVar a -> IO Bool #
Check whether a given MVar
is empty.
Notice that the boolean value returned is just a snapshot of
the state of the MVar. By the time you get to react on its result,
the MVar may have been filled (or emptied) - so be extremely
careful when using this operation. Use tryTakeMVar
instead if possible.
withMVar :: MVar a -> (a -> IO b) -> IO b #
withMVar
is an exception-safe wrapper for operating on the contents
of an MVar
. This operation is exception-safe: it will replace the
original contents of the MVar
if an exception is raised (see
Control.Exception). However, it is only atomic if there are no
other producers for this MVar
.
withMVarMasked :: MVar a -> (a -> IO b) -> IO b #
Like withMVar
, but the IO
action in the second argument is executed
with asynchronous exceptions masked.
Since: 4.7.0.0
modifyMVar_ :: MVar a -> (a -> IO a) -> IO () #
An exception-safe wrapper for modifying the contents of an MVar
.
Like withMVar
, modifyMVar
will replace the original contents of
the MVar
if an exception is raised during the operation. This
function is only atomic if there are no other producers for this
MVar
.
modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b #
A slight variation on modifyMVar_
that allows a value to be
returned (b
) in addition to the modified value of the MVar
.
modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO () #
Like modifyMVar_
, but the IO
action in the second argument is executed with
asynchronous exceptions masked.
Since: 4.6.0.0
modifyMVarMasked :: MVar a -> (a -> IO (a, b)) -> IO b #
Like modifyMVar
, but the IO
action in the second argument is executed with
asynchronous exceptions masked.
Since: 4.6.0.0
tryReadMVar :: MVar a -> IO (Maybe a) #
dupChan :: Chan a -> IO (Chan a) #
Duplicate a Chan
: the duplicate channel begins empty, but data written to
either channel from then on will be available from both. Hence this creates
a kind of broadcast channel, where data written by anyone is seen by
everyone else.
(Note that a duplicated channel is not equal to its original.
So: fmap (c /=) $ dupChan c
returns True
for all c
.)
Reader
:: MonadReader r m | |
=> (r -> a) | The selector function to apply to the environment. |
-> m a |
Retrieves a function of the current environment.
Exceptions
throwIO :: (MonadThrow m, Exception e) => e -> m a #
Synonym for throw
Since: 0.1.0.0
throwM :: (MonadThrow m, Exception e) => e -> m a #
Synonym for throw
Since: 0.1.0.0
throwTo :: (Exception e, MonadIO m) => ThreadId -> e -> m () #
Throw an asynchronous exception to another thread
It's usually a better idea to use the async package, see https://github.com/fpco/safe-exceptions#quickstart
Since: 0.1.0.0
catch :: (MonadCatch m, Exception e) => m a -> (e -> m a) -> m a #
Same as upstream catch
, but will not catch asynchronous
exceptions
Since: 0.1.0.0
catchIO :: MonadCatch m => m a -> (IOException -> m a) -> m a #
catch
specialized to only catching IOException
s
Since: 0.1.3.0
catchAny :: MonadCatch m => m a -> (SomeException -> m a) -> m a #
catch
specialized to catch all synchronous exception
Since: 0.1.0.0
catchDeep :: (MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> (e -> m a) -> m a #
Same as catch
, but fully force evaluation of the result value
to find all impure exceptions.
Since: 0.1.1.0
catchAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => m a -> (SomeException -> m a) -> m a #
catchDeep
specialized to catch all synchronous exception
Since: 0.1.1.0
handle :: (MonadCatch m, Exception e) => (e -> m a) -> m a -> m a #
Flipped version of catch
Since: 0.1.0.0
handleIO :: MonadCatch m => (IOException -> m a) -> m a -> m a #
handle
specialized to only catching IOException
s
Since: 0.1.3.0
handleAny :: MonadCatch m => (SomeException -> m a) -> m a -> m a #
Flipped version of catchAny
Since: 0.1.0.0
handleDeep :: (MonadCatch m, Exception e, MonadIO m, NFData a) => (e -> m a) -> m a -> m a #
Flipped version of catchDeep
Since: 0.1.1.0
handleAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => (SomeException -> m a) -> m a -> m a #
Flipped version of catchAnyDeep
Since: 0.1.1.0
try :: (MonadCatch m, Exception e) => m a -> m (Either e a) #
Same as upstream try
, but will not catch asynchronous
exceptions
Since: 0.1.0.0
tryIO :: MonadCatch m => m a -> m (Either IOException a) #
try
specialized to only catching IOException
s
Since: 0.1.3.0
tryAny :: MonadCatch m => m a -> m (Either SomeException a) #
try
specialized to catch all synchronous exceptions
Since: 0.1.0.0
tryDeep :: (MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> m (Either e a) #
Same as try
, but fully force evaluation of the result value
to find all impure exceptions.
Since: 0.1.1.0
tryAnyDeep :: (MonadCatch m, MonadIO m, NFData a) => m a -> m (Either SomeException a) #
tryDeep
specialized to catch all synchronous exceptions
Since: 0.1.1.0
onException :: MonadMask m => m a -> m b -> m a #
Async safe version of onException
Since: 0.1.0.0
bracket :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c #
Async safe version of bracket
Since: 0.1.0.0
withException :: (MonadMask m, Exception e) => m a -> (e -> m b) -> m a #
Like onException
, but provides the handler the thrown
exception.
Since: 0.1.0.0
bracketOnError :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c #
Async safe version of bracketOnError
Since: 0.1.0.0
bracketOnError_ :: MonadMask m => m a -> m b -> m c -> m c #
Async safe version of bracketOnError_
Since: 0.1.0.0
displayException :: Exception e => e -> String #
Arrow
(&&&) :: Arrow a => forall b c c'. a b c -> a b c' -> a b (c, c') #
Fanout: send the input to both argument arrows and combine their output.
The default definition may be overridden with a more efficient version if desired.
(***) :: Arrow a => forall b c b' c'. a b c -> a b' c' -> a (b, b') (c, c') #
Split the input between the two argument arrows and combine their output. Note that this is in general not a functor.
The default definition may be overridden with a more efficient version if desired.
Maybe
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]
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]
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
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]
Either
partitionEithers :: [Either a b] -> ([a], [b]) #
Partitions a list of Either
into two lists.
All the Left
elements are extracted, in order, to the first
component of the output. Similarly the Right
elements are extracted
to the second component of the output.
Examples
Basic usage:
>>>
let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
>>>
partitionEithers list
(["foo","bar","baz"],[3,7])
The pair returned by
should be the same
pair as partitionEithers
x(
:lefts
x, rights
x)
>>>
let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
>>>
partitionEithers list == (lefts list, rights list)
True
Ord
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) ...
Say
say :: MonadIO m => Text -> m () #
Send a Text
to standard output, appending a newline, and chunking the
data. By default, the chunk size is 2048 characters, so any messages below
that size will be sent as one contiguous unit. If larger messages are used,
it is possible for interleaving with other threads to occur.
Since: 0.1.0.0
sayErrString :: MonadIO m => String -> m () #
Same as sayString
, but data is sent to standard error.
Since: 0.1.0.0
sayErrShow :: (MonadIO m, Show a) => a -> m () #
Same as sayShow
, but data is sent to standard error.
Since: 0.1.0.0
hSayString :: MonadIO m => Handle -> String -> m () #
IORef
A mutable variable in the IO
monad
writeIORef :: IORef a -> a -> IO () #
Write a new value into an IORef
modifyIORef :: IORef a -> (a -> a) -> IO () #
Mutate the contents of an IORef
.
Be warned that modifyIORef
does not apply the function strictly. This
means if the program calls modifyIORef
many times, but seldomly uses the
value, thunks will pile up in memory resulting in a space leak. This is a
common mistake made when using an IORef as a counter. For example, the
following will likely produce a stack overflow:
ref <- newIORef 0 replicateM_ 1000000 $ modifyIORef ref (+1) readIORef ref >>= print
To avoid this problem, use modifyIORef'
instead.
modifyIORef' :: IORef a -> (a -> a) -> IO () #
Strict version of modifyIORef
Since: 4.6.0.0
atomicModifyIORef :: IORef a -> (a -> (a, b)) -> IO b #
Atomically modifies the contents of an IORef
.
This function is useful for using IORef
in a safe way in a multithreaded
program. If you only have one IORef
, then using atomicModifyIORef
to
access and modify it will prevent race conditions.
Extending the atomicity to multiple IORef
s is problematic, so it
is recommended that if you need to do anything more complicated
then using MVar
instead is a good idea.
atomicModifyIORef
does not apply the function strictly. This is important
to know even if all you are doing is replacing the value. For example, this
will leak memory:
ref <- newIORef '1' forever $ atomicModifyIORef ref (\_ -> ('2', ()))
Use atomicModifyIORef'
or atomicWriteIORef
to avoid this problem.
atomicModifyIORef' :: IORef a -> (a -> (a, b)) -> IO b #
Strict version of atomicModifyIORef
. This forces both the value stored
in the IORef
as well as the value returned.
Since: 4.6.0.0
atomicWriteIORef :: IORef a -> a -> IO () #
Variant of writeIORef
with the "barrier to reordering" property that
atomicModifyIORef
has.
Since: 4.6.0.0
IO
Haskell defines operations to read and write characters from and to files,
represented by values of type Handle
. Each value of this type is a
handle: a record used by the Haskell run-time system to manage I/O
with file system objects. A handle has at least the following properties:
- whether it manages input or output or both;
- whether it is open, closed or semi-closed;
- whether the object is seekable;
- whether buffering is disabled, or enabled on a line or block basis;
- a buffer (whose length may be zero).
Most handles will also have a current I/O position indicating where the next
input or output operation will occur. A handle is readable if it
manages only input or both input and output; likewise, it is writable if
it manages only output or both input and output. A handle is open when
first allocated.
Once it is closed it can no longer be used for either input or output,
though an implementation cannot re-use its storage while references
remain to it. Handles are in the Show
and Eq
classes. The string
produced by showing a handle is system dependent; it should include
enough information to identify the handle for debugging. A handle is
equal according to ==
only to itself; no attempt
is made to compare the internal state of different handles for equality.
See openFile
Computation hClose
hdl
makes handle hdl
closed. Before the
computation finishes, if hdl
is writable its buffer is flushed as
for hFlush
.
Performing hClose
on a handle that has already been closed has no effect;
doing so is not an error. All other operations on a closed handle will fail.
If hClose
fails for any reason, any further operations (apart from
hClose
) on the handle will still fail as if hdl
had been successfully
closed.
withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r #
opens a file using withBinaryFile
name mode actopenBinaryFile
and passes the resulting handle to the computation act
. The handle
will be closed on exit from withBinaryFile
, whether by normal
termination or by raising an exception.
readFile :: FilePath -> IO ByteString #
Read an entire file strictly into a ByteString
.
writeFile :: FilePath -> ByteString -> IO () #
Write a ByteString
to a file.
readFileUtf8 :: MonadIO m => FilePath -> m Text Source #
Read a file assuming a UTF-8 character encoding.
This leverages decodeUtf8
, so in the event of a character
encoding issue, replacement characters will be used.
Since: 0.1.0.0
writeFileUtf8 :: MonadIO m => FilePath -> Text -> m () Source #
Write a file using a UTF-8 character encoding.
Since: 0.1.0.0
Character encoding
encodeUtf8 :: Text -> ByteString #
Encode text using UTF-8 encoding.
decodeUtf8 :: ByteString -> Text Source #
A total function for decoding a ByteString
into Text
using a
UTF-8 character encoding. This uses lenientDecode
in the case of
any encoding errors.
Since: 0.1.0.0
deepseq
A class of types that can be fully evaluated.
Since: 1.1.0.0
Nothing
NFData Bool | |
NFData Char | |
NFData Double | |
NFData Float | |
NFData Int | |
NFData Int8 | |
NFData Int16 | |
NFData Int32 | |
NFData Int64 | |
NFData Integer | |
NFData Word | |
NFData Word8 | |
NFData Word16 | |
NFData Word32 | |
NFData Word64 | |
NFData CallStack | Since: 1.4.2.0 |
NFData TypeRep | NOTE: Only defined for Since: 1.4.0.0 |
NFData () | |
NFData TyCon | NOTE: Only defined for Since: 1.4.0.0 |
NFData Natural | Since: 1.4.0.0 |
NFData Void | Since: 1.4.0.0 |
NFData Version | Since: 1.3.0.0 |
NFData Unique | Since: 1.4.0.0 |
NFData ThreadId | Since: 1.4.0.0 |
NFData ExitCode | Since: 1.4.2.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 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 All | Since: 1.4.0.0 |
NFData Any | Since: 1.4.0.0 |
NFData Fingerprint | Since: 1.4.0.0 |
NFData SrcLoc | Since: 1.4.2.0 |
NFData ByteString | |
NFData IntSet | |
NFData UnicodeException | |
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 (Identity a) | Since: 1.4.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 (Fixed a) | Since: 1.3.0.0 |
NFData a => NFData (Complex a) | |
NFData (StableName a) | Since: 1.4.0.0 |
NFData a => NFData (ZipList a) | Since: 1.4.0.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 (IORef a) | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.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 (Digit a) | |
NFData a => NFData (Node a) | |
NFData a => NFData (Elem a) | |
NFData a => NFData (FingerTree a) | |
NFData a => NFData (Seq a) | |
NFData a => NFData (IntMap a) | |
NFData a => NFData (Set a) | |
NFData a => NFData (Array a) | |
NFData a => NFData (HashSet 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 k, NFData v) => NFData (Leaf k v) | |
(NFData k, NFData v) => NFData (HashMap k v) | |
(NFData a, NFData b, NFData c) => NFData (a, b, c) | |
NFData a => NFData (Const k a b) | Since: 1.4.0.0 |
(NFData a, NFData b, NFData c, NFData d) => NFData (a, b, c, d) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) | |
(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) | |
deepseq :: NFData a => a -> b -> b #
deepseq
: fully evaluates the first argument, before returning the
second.
The name deepseq
is used to illustrate the relationship to seq
:
where seq
is shallow in the sense that it only evaluates the top
level of its argument, deepseq
traverses the entire data structure
evaluating it completely.
deepseq
can be useful for forcing pending exceptions,
eradicating space leaks, or forcing lazy I/O to happen. It is
also useful in conjunction with parallel Strategies (see the
parallel
package).
There is no guarantee about the ordering of evaluation. The
implementation may evaluate the components of the structure in
any order or in parallel. To impose an actual order on
evaluation, use pseq
from Control.Parallel in the
parallel
package.
Since: 1.1.0.0
($!!) :: NFData a => (a -> b) -> a -> b infixr 0 #
the deep analogue of $!
. In the expression f $!! x
, x
is
fully evaluated before the function f
is applied to it.
Since: 1.2.0.0
a variant of deepseq
that is useful in some circumstances:
force x = x `deepseq` x
force x
fully evaluates x
, and then returns it. Note that
force x
only performs evaluation when the value of force x
itself is demanded, so essentially it turns shallow evaluation into
deep evaluation.
force
can be conveniently used in combination with ViewPatterns
:
{-# LANGUAGE BangPatterns, ViewPatterns #-} import Control.DeepSeq someFun :: ComplexData -> SomeResult someFun (force -> !arg) = {- 'arg' will be fully evaluated -}
Another useful application is to combine force
with
evaluate
in order to force deep evaluation
relative to other IO
operations:
import Control.Exception (evaluate) import Control.DeepSeq main = do result <- evaluate $ force $ pureComputation {- 'result' will be fully evaluated at this point -} return ()
Since: 1.2.0.0
Monoids
(++) :: Monoid m => m -> m -> m infixr 5 Source #
Operator version of mappend
.
In base, this operator is known as <>
. However, this is the name
of the operator for Semigroup
as well. Once Semigroup
is a
superclass of Monoid
, this historical accident will be
unimportant. In the meanwhile, SafePrelude
deals with this
situation by making <>
the Semigroup
operator, and ++
the
Monoid
operator.
Since: 0.1.0.0