License | BSD-style |
---|---|
Maintainer | Vincent Hanquez <vincent@snarc.org> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
internal re-export of all the good base bits
- ($) :: (a -> b) -> a -> b
- ($!) :: (a -> b) -> a -> b
- (&&) :: Bool -> Bool -> Bool
- (||) :: Bool -> Bool -> Bool
- (.) :: Category k cat => forall b c a. cat b c -> cat a b -> cat a c
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- not :: Bool -> Bool
- otherwise :: Bool
- fst :: (a, b) -> a
- snd :: (a, b) -> b
- id :: Category k cat => forall a. cat 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
- error :: HasCallStack => [Char] -> a
- and :: Foldable t => t Bool -> Bool
- undefined :: HasCallStack => a
- seq :: a -> b -> b
- class Show a where
- class Eq a => Ord a where
- class Eq a where
- class Bounded a where
- class Enum a where
- class Functor f where
- class Functor f => Applicative f where
- class Applicative m => Monad m where
- data Maybe a :: * -> *
- data Ordering :: *
- data Bool :: *
- data Int :: *
- data Integer :: *
- data Char :: *
- class Integral a where
- class Fractional a where
- class HasNegation a where
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- data Word8 :: *
- data Word16 :: *
- data Word32 :: *
- data Word64 :: *
- data Word :: *
- data Double :: *
- data Float :: *
- data IO a :: * -> *
- class IsList l where
- class IsString a where
- class Generic a
- data Either a b :: * -> * -> *
- class Typeable * a => Data a where
- mkNoRepType :: String -> DataType
- data DataType :: *
- class Typeable k a
- class Monoid a where
- (<>) :: Monoid m => m -> m -> m
- class (Typeable * e, Show e) => Exception e
- throw :: Exception e => e -> a
- throwIO :: Exception e => e -> IO a
- data Ptr a :: * -> * = Ptr Addr#
- ifThenElse :: Bool -> a -> a -> a
- internalError :: [Char] -> a
Documentation
($) :: (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.
(<$>) :: 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)
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]
error :: HasCallStack => [Char] -> a #
error
stops execution and displays an error message.
undefined :: HasCallStack => a #
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.
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)"
.
showsPrec :: Int -> a -> ShowS #
Convert a value to a readable String
.
showsPrec
should satisfy the law
showsPrec d x r ++ s == showsPrec d x (r ++ s)
Derived instances of Read
and Show
satisfy the following:
That is, readsPrec
parses the string produced by
showsPrec
, and delivers the value that showsPrec
started with.
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.
compare :: a -> a -> Ordering #
(<) :: a -> a -> Bool infix 4 #
(<=) :: a -> a -> Bool infix 4 #
(>) :: a -> a -> Bool infix 4 #
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 () | |
Eq TyCon | |
Eq BigNat | |
Eq SpecConstrAnnotation | |
Eq Natural | |
Eq Void | |
Eq Constr | Equality of constructors |
Eq DataRep | |
Eq ConstrRep | |
Eq Fixity | |
Eq Version | |
Eq CDev | |
Eq CIno | |
Eq CMode | |
Eq COff | |
Eq CPid | |
Eq CSsize | |
Eq CGid | |
Eq CNlink | |
Eq CUid | |
Eq CCc | |
Eq CSpeed | |
Eq CTcflag | |
Eq CRLim | |
Eq Fd | |
Eq AsyncException | |
Eq ArrayException | |
Eq ExitCode | |
Eq IOErrorType | |
Eq WordPtr | |
Eq IntPtr | |
Eq CChar | |
Eq CSChar | |
Eq CUChar | |
Eq CShort | |
Eq CUShort | |
Eq CInt | |
Eq CUInt | |
Eq CLong | |
Eq CULong | |
Eq CLLong | |
Eq CULLong | |
Eq CFloat | |
Eq CDouble | |
Eq CPtrdiff | |
Eq CSize | |
Eq CWchar | |
Eq CSigAtomic | |
Eq CClock | |
Eq CTime | |
Eq CUSeconds | |
Eq CSUSeconds | |
Eq CIntPtr | |
Eq CUIntPtr | |
Eq CIntMax | |
Eq CUIntMax | |
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 GeneralCategory | |
Eq SrcLoc | |
Eq PinnedStatus # | |
Eq Endianness # | |
Eq Char7 # | |
Eq Word128 # | |
Eq Word256 # | |
Eq FileSize # | |
Eq RecastDestinationSize # | |
Eq RecastSourceSize # | |
Eq OutOfBoundOperation # | |
Eq Addr # | |
Eq ValidationFailure # | |
Eq AsciiString # | |
Eq String # | |
Eq Encoding # | |
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 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 (ZipList a) | |
Eq (ForeignPtr 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 (BE a) # | |
Eq a => Eq (LE a) # | |
Eq (FinalPtr a) # | |
Eq (CountOf ty) # | |
Eq (Offset ty) # | |
Eq a => Eq (NonEmpty a) # | |
Eq (Zn n) # | |
Eq (Zn64 n) # | |
(PrimType ty, Eq ty) => Eq (Block ty) # | |
(PrimType ty, Eq ty) => Eq (UArray ty) # | |
Eq a => Eq (Array a) # | |
(Eq b, Eq a) => 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 b, Eq a) => Eq (These a b) # | |
PrimType a => Eq (BlockN n a) # | |
Eq c => Eq (K1 i c p) | |
(Eq (g p), Eq (f p)) => Eq ((:+:) f g p) | |
(Eq (g p), Eq (f 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 (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
.
Class Enum
defines operations on sequentially ordered types.
The enumFrom
... methods are used in Haskell's translation of
arithmetic sequences.
Instances of Enum
may be derived for any enumeration type (types
whose constructors have no fields). The nullary constructors are
assumed to be numbered left-to-right by fromEnum
from 0
through n-1
.
See Chapter 10 of the Haskell Report for more details.
For any type that is an instance of class Bounded
as well as Enum
,
the following should hold:
- The calls
andsucc
maxBound
should result in a runtime error.pred
minBound
fromEnum
andtoEnum
should give a runtime error if the result value is not representable in the result type. For example,
is an error.toEnum
7 ::Bool
enumFrom
andenumFromThen
should be defined with an implicit bound, thus:
enumFrom x = enumFromTo x maxBound enumFromThen x y = enumFromThenTo x y bound where bound | fromEnum y >= fromEnum x = maxBound | otherwise = minBound
the successor of a value. For numeric types, succ
adds 1.
the predecessor of a value. For numeric types, pred
subtracts 1.
Convert from an Int
.
Convert to an Int
.
It is implementation-dependent what fromEnum
returns when
applied to a value that is too large to fit in an Int
.
Used in Haskell's translation of [n..]
.
enumFromThen :: a -> a -> [a] #
Used in Haskell's translation of [n,n'..]
.
enumFromTo :: a -> a -> [a] #
Used in Haskell's translation of [n..m]
.
enumFromThenTo :: a -> a -> a -> [a] #
Used in Haskell's translation of [n,n'..m]
.
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).
Lift a value.
(<*>) :: f (a -> b) -> f a -> f b infixl 4 #
Sequential application.
(*>) :: f a -> f b -> f b infixl 4 #
Sequence actions, discarding the value of the first argument.
(<*) :: f a -> f b -> f a infixl 4 #
Sequence actions, discarding the value of the second argument.
Applicative [] | |
Applicative Maybe | |
Applicative IO | |
Applicative U1 | |
Applicative Par1 | |
Applicative Identity | |
Applicative Min | |
Applicative Max | |
Applicative First | |
Applicative Last | |
Applicative Option | |
Applicative NonEmpty | |
Applicative ZipList | |
Applicative Dual | |
Applicative Sum | |
Applicative Product | |
Applicative First | |
Applicative Last | |
Applicative ((->) a) | |
Applicative (Either e) | |
Applicative f => Applicative (Rec1 f) | |
Monoid a => Applicative ((,) a) | |
Monad m => Applicative (WrappedMonad m) | |
Arrow a => Applicative (ArrowMonad a) | |
Applicative (Proxy *) | |
Applicative (ST s) | |
(Applicative f, Applicative g) => Applicative ((:*:) f g) | |
(Applicative f, Applicative g) => Applicative ((:.:) f g) | |
Arrow a => Applicative (WrappedArrow a b) | |
Monoid m => Applicative (Const * m) | |
Applicative f => Applicative (Alt * f) | |
Monad m => Applicative (Reader r m) # | |
Monad m => Applicative (State s m) # | |
Applicative f => Applicative (M1 i c f) | |
Monad state => Applicative (Builder collection mutCollection step state err) # | |
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.
(>>=) :: m a -> (a -> m b) -> m b infixl 1 #
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
(>>) :: m a -> m b -> m b infixl 1 #
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
Inject a value into the monadic type.
Fail with a message. This operation is not part of the
mathematical definition of a monad, but is invoked on pattern-match
failure in a do
expression.
As part of the MonadFail proposal (MFP), this function is moved
to its own class MonadFail
(see Control.Monad.Fail for more
details). The definition here will be removed in a future
release.
Monad [] | |
Monad Maybe | |
Monad IO | |
Monad U1 | |
Monad Par1 | |
Monad Identity | |
Monad Min | |
Monad Max | |
Monad First | |
Monad Last | |
Monad Option | |
Monad NonEmpty | |
Monad Dual | |
Monad Sum | |
Monad Product | |
Monad First | |
Monad Last | |
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 (ST s) | |
(Monad f, Monad g) => Monad ((:*:) f g) | |
Monad f => Monad (Alt * f) | |
Monad m => Monad (Reader r m) # | |
Monad m => Monad (State r m) # | |
Monad f => Monad (M1 i c f) | |
Monad state => Monad (Builder collection mutCollection step state err) # | |
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 | |
Generic1 Maybe | |
Alternative Maybe | |
MonadPlus Maybe | |
MonadFailure Maybe # | |
Eq a => Eq (Maybe a) | |
Data a => Data (Maybe a) | |
Ord a => Ord (Maybe a) | |
Read a => Read (Maybe a) | |
Show a => Show (Maybe a) | |
Generic (Maybe a) | |
Semigroup a => Semigroup (Maybe a) | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
NormalForm a => NormalForm (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) | |
From (Maybe a) (Either () a) # | |
type Rep1 Maybe | |
type Failure Maybe # | |
type Rep (Maybe a) | |
data Sing (Maybe a) | |
type (==) (Maybe k) a b | |
type DemoteRep (Maybe a) (KProxy (Maybe a)) | |
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.
Invariant: Jn#
and Jp#
are used iff value doesn't fit in S#
Useful properties resulting from the invariants:
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 | |
Data Char | |
Ord Char | |
Read Char | |
Show Char | |
Storable Char | |
Subtractive Char # | |
NormalForm Char # | |
PrimMemoryComparable Char # | |
PrimType Char # | |
Functor (URec Char) | |
Foldable (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 Difference Char # | |
type Rep1 (URec Char) | |
type Rep (URec Char p) | |
Integral Literal support
e.g. 123 :: Integer 123 :: Word8
fromInteger :: Integer -> a #
class Fractional a where #
Fractional Literal support
e.g. 1.2 :: Double 0.03 :: Float
fromRational :: Rational -> a #
class HasNegation a where #
Negation support
e.g. -(f x)
8-bit signed integer type
16-bit signed integer type
32-bit signed integer type
64-bit signed integer type
8-bit unsigned integer type
16-bit unsigned integer type
32-bit unsigned integer type
64-bit unsigned integer type
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 | |
Data Double | |
Ord Double | |
Read Double | |
RealFloat Double | |
Storable Double | |
HasNegation Double # | |
Fractional Double # | |
Integral Double # | |
Additive Double # | |
Divisible Double # | |
Multiplicative Double # | |
Subtractive Double # | |
NormalForm Double # | |
PrimType Double # | |
Functor (URec Double) | |
Foldable (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 Difference Double # | |
type Rep1 (URec Double) | |
type Rep (URec Double p) | |
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 | |
Data Float | |
Ord Float | |
Read Float | |
RealFloat Float | |
Storable Float | |
HasNegation Float # | |
Fractional Float # | |
Integral Float # | |
Additive Float # | |
Divisible Float # | |
Multiplicative Float # | |
Subtractive Float # | |
NormalForm Float # | |
PrimType Float # | |
Functor (URec Float) | |
Foldable (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 Difference Float # | |
type Rep1 (URec Float) | |
type Rep (URec Float 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.
The IsList
class and its methods are intended to be used in
conjunction with the OverloadedLists extension.
Since: 4.7.0.0
The fromList
function constructs the structure l
from the given
list of Item l
fromListN :: Int -> [Item l] -> l #
The fromListN
function takes the input list's length as a hint. Its
behaviour should be equivalent to fromList
. The hint can be used to
construct the structure l
more efficiently compared to fromList
. If
the given hint does not equal to the input list's length the behaviour of
fromListN
is not specified.
The toList
function extracts a list of Item l
from the structure l
.
It should satisfy fromList . toList = id.
IsList CallStack | Be aware that 'fromList . toList = id' only for unfrozen Since: 4.9.0.0 |
IsList Version | Since: 4.8.0.0 |
IsList AsciiString # | |
IsList String # | |
IsList [a] | |
IsList (NonEmpty a) | |
IsList c => IsList (NonEmpty c) # | |
PrimType ty => IsList (Block ty) # | |
PrimType ty => IsList (UArray ty) # | |
IsList (Array ty) # | |
Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).
fromString :: String -> a #
Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.
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"
Bifunctor Either | |
Monad (Either e) | |
Functor (Either a) | |
Applicative (Either e) | |
Foldable (Either a) | |
Generic1 (Either a) | |
MonadFailure (Either a) # | |
From (Maybe a) (Either () a) # | |
(Eq b, Eq a) => Eq (Either a b) | |
(Data a, Data b) => Data (Either a b) | |
(Ord b, Ord a) => Ord (Either a b) | |
(Read b, Read a) => Read (Either a b) | |
(Show b, Show a) => Show (Either a b) | |
Generic (Either a b) | |
Semigroup (Either a b) | |
(NormalForm l, NormalForm r) => NormalForm (Either l r) # | |
From (Either a b) (These a b) # | |
type Rep1 (Either a) | |
type Failure (Either a) # | |
type Rep (Either a b) | |
type (==) (Either k k1) a b | |
class Typeable * a => Data a where #
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.
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> a -> c a #
Left-associative fold operation for constructor applications.
The type of gfoldl
is a headache, but operationally it is a simple
generalisation of a list fold.
The default definition for gfoldl
is
, which is
suitable for abstract datatypes with no substructures.const
id
gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c a #
Unfolding constructor applications
Obtaining the constructor from a given datum. For proper terms, this is meant to be the top-level constructor. Primitive datatypes are here viewed as potentially infinite sets of values (i.e., constructors).
dataTypeOf :: a -> DataType #
The outer type constructor of the type
dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c a) #
Mediate types and unary type constructors.
In Data
instances of the form T a
, dataCast1
should be defined
as gcast1
.
The default definition is
, which is appropriate
for non-unary type constructors.const
Nothing
dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a) #
Mediate types and binary type constructors.
In Data
instances of the form T a b
, dataCast2
should be
defined as gcast2
.
The default definition is
, which is appropriate
for non-binary type constructors.const
Nothing
gmapT :: (forall b. Data b => b -> b) -> a -> a #
A generic transformation that maps over the immediate subterms
The default definition instantiates the type constructor c
in the
type of gfoldl
to an identity datatype constructor, using the
isomorphism pair as injection and projection.
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r #
A generic query with a left-associative binary operator
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r #
A generic query with a right-associative binary operator
gmapQ :: (forall d. Data d => d -> u) -> a -> [u] #
A generic query that processes the immediate subterms and returns a list of results. The list is given in the same order as originally specified in the declaration of the data constructors.
gmapQi :: Int -> (forall d. Data d => d -> u) -> a -> u #
A generic query that processes one child by index (zero-based)
gmapM :: Monad m => (forall d. Data d => d -> m d) -> a -> m a #
A generic monadic transformation that maps over the immediate subterms
The default definition instantiates the type constructor c
in
the type of gfoldl
to the monad datatype constructor, defining
injection and projection using return
and >>=
.
gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a #
Transformation of at least one immediate subterm does not fail
gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a #
Transformation of one immediate subterm with success
mkNoRepType :: String -> DataType #
Constructs a non-representation for a non-representable type
Representation of datatypes. A package of constructor representations with names of type and module.
The class Typeable
allows a concrete representation of a type to
be calculated.
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
.
Identity of mappend
An associative operation
Fold a list using the monoid.
For most types, the default definition for mconcat
will be
used, but the function is included in the class definition so
that an optimized version can be provided for specific types.
Monoid Ordering | |
Monoid () | |
Monoid All | |
Monoid Any | |
Monoid AsciiString # | |
Monoid String # | |
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 (CountOf ty) # | |
PrimType ty => Monoid (Block ty) # | |
PrimType ty => Monoid (UArray ty) # | |
Monoid (Array a) # | |
Monoid b => Monoid (a -> b) | |
(Monoid a, Monoid b) => Monoid (a, b) | |
Monoid (Proxy k s) | |
(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) | |
class (Typeable * e, Show e) => Exception e #
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
throw :: Exception e => e -> a #
Throw an exception. Exceptions may be thrown from purely
functional code, but may only be caught within the IO
monad.
throwIO :: Exception e => e -> IO a #
A variant of throw
that can only be used within the IO
monad.
Although throwIO
has a type that is an instance of the type of throw
, the
two functions are subtly different:
throw e `seq` x ===> throw e throwIO e `seq` x ===> x
The first example will cause the exception e
to be raised,
whereas the second one won't. In fact, throwIO
will only cause
an exception to be raised when it is used within the IO
monad.
The throwIO
variant should be used in preference to throw
to
raise an exception within the IO
monad because it guarantees
ordering with respect to other IO
operations, whereas throw
does not.
A value of type
represents a pointer to an object, or an
array of objects, which may be marshalled to or from Haskell values
of type Ptr
aa
.
The type a
will often be an instance of class
Storable
which provides the marshalling operations.
However this is not essential, and you can provide your own operations
to access the pointer. For example you might write small foreign
functions to get or set the fields of a C struct
.
Eq (Ptr a) | |
Data a => Data (Ptr a) | |
Functor (URec (Ptr ())) | |
Ord (Ptr a) | |
Show (Ptr a) | |
Foldable (URec (Ptr ())) | |
Generic1 (URec (Ptr ())) | |
Storable (Ptr a) | |
NormalForm (Ptr a) # | |
Eq (URec (Ptr ()) p) | |
Ord (URec (Ptr ()) p) | |
Generic (URec (Ptr ()) p) | |
type Rep1 (URec (Ptr ())) | |
data URec (Ptr ()) | Used for marking occurrences of |
type Rep (URec (Ptr ()) p) | |
ifThenElse :: Bool -> a -> a -> a #
for support of if .. then .. else
internalError :: [Char] -> a #
Only to use internally for internal error cases