| License | BSD-style |
|---|---|
| Maintainer | Vincent Hanquez <vincent@snarc.org> |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Foundation
Contents
Description
I tried to picture clusters of information As they moved through the computer What do they look like?
Alternative Prelude
- ($) :: (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
- not :: Bool -> Bool
- otherwise :: Bool
- data Tuple2 a b = Tuple2 !a !b
- data Tuple3 a b c = Tuple3 !a !b !c
- data Tuple4 a b c d = Tuple4 !a !b !c !d
- class Fstable a where
- type ProductFirst a
- class Sndable a where
- type ProductSecond a
- class Thdable a where
- type ProductThird a
- 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
- putStr :: String -> IO ()
- putStrLn :: String -> IO ()
- getArgs :: IO [String]
- uncurry :: (a -> b -> c) -> (a, b) -> c
- curry :: ((a, b) -> c) -> a -> b -> c
- swap :: (a, b) -> (b, a)
- until :: (a -> Bool) -> (a -> a) -> a -> a
- asTypeOf :: a -> a -> a
- undefined :: HasCallStack => a
- seq :: a -> b -> b
- class Show a
- show :: Show a => a -> String
- class Eq a => Ord a where
- class Eq a where
- class Bounded a where
- class Enum a where
- succ :: a -> a
- pred :: a -> a
- toEnum :: Int -> a
- fromEnum :: a -> Int
- enumFrom :: a -> [a]
- enumFromThen :: a -> a -> [a]
- enumFromTo :: a -> a -> [a]
- enumFromThenTo :: a -> a -> a -> [a]
- class Functor f where
- class Integral a where
- class Fractional a where
- class HasNegation a where
- class Bifunctor p where
- class Functor f => Applicative f where
- class Applicative m => Monad m where
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- class IsString a where
- fromString :: String -> a
- class IsList l where
- class (Enum a, Eq a, Ord a, Integral a) => IsIntegral a where
- class (Enum a, Eq a, Ord a, Integral a, IsIntegral a) => IsNatural a where
- class Signed a where
- class Additive a where
- class Subtractive a where
- type Difference a
- class Multiplicative a where
- class (Additive a, Multiplicative a) => IDivisible a where
- class Multiplicative a => Divisible a where
- data Maybe a :: * -> *
- data Ordering :: *
- data Bool :: *
- data Char :: *
- data IO a :: * -> *
- data Either a b :: * -> * -> *
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- data Word8 :: *
- data Word16 :: *
- data Word32 :: *
- data Word64 :: *
- data Word :: *
- data Int :: *
- data Integer :: *
- data Natural :: *
- type Rational = Ratio Integer
- data Float :: *
- data Double :: *
- newtype Size ty = Size Int
- newtype Offset ty = Offset Int
- data UArray ty
- class Eq ty => PrimType ty
- data Array a
- data String
- (^^) :: (Fractional a, Integral b) => a -> b -> a
- fromIntegral :: (Integral a, Num b) => a -> b
- realToFrac :: (Real a, Fractional b) => a -> b
- class Monoid a where
- (<>) :: Monoid m => m -> m -> m
- class (IsList c, Item c ~ Element c) => Collection c where
- data NonEmpty a
- nonEmpty :: Collection c => c -> Maybe (NonEmpty c)
- class Foldable collection where
- 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
- maybeToList :: Maybe a -> [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
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<|>) :: Alternative f => forall a. f a -> f a -> f a
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- class (Typeable * e, Show e) => Exception e where
- toException :: e -> SomeException
- fromException :: SomeException -> Maybe e
- displayException :: e -> String
- class Typeable k a
- data SomeException :: *
- data IOException :: *
- data Proxy k t :: forall k. k -> * = Proxy
- asProxyTypeOf :: a -> Proxy * a -> a
- data Partial a
- partial :: a -> Partial a
- data PartialError
- fromPartial :: Partial a -> a
- type LString = String
Standard
Operators
($) :: (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.
Functions
Strict tuple (a,b)
Constructors
| Tuple2 !a !b |
Instances
| Bifunctor Tuple2 # | |
| Nthable 1 (Tuple2 a b) # | |
| Nthable 2 (Tuple2 a b) # | |
| (Eq a, Eq b) => Eq (Tuple2 a b) # | |
| (Data a, Data b) => Data (Tuple2 a b) # | |
| (Ord a, Ord b) => Ord (Tuple2 a b) # | |
| (Show a, Show b) => Show (Tuple2 a b) # | |
| Generic (Tuple2 a b) # | |
| Sndable (Tuple2 a b) # | |
| Fstable (Tuple2 a b) # | |
| (Hashable a, Hashable b) => Hashable (Tuple2 a b) # | |
| type NthTy 1 (Tuple2 a b) # | |
| type NthTy 2 (Tuple2 a b) # | |
| type Rep (Tuple2 a b) # | |
| type ProductSecond (Tuple2 a b) # | |
| type ProductFirst (Tuple2 a b) # | |
Strict tuple (a,b,c)
Constructors
| Tuple3 !a !b !c |
Instances
| Nthable 1 (Tuple3 a b c) # | |
| Nthable 2 (Tuple3 a b c) # | |
| Nthable 3 (Tuple3 a b c) # | |
| (Eq a, Eq b, Eq c) => Eq (Tuple3 a b c) # | |
| (Data a, Data b, Data c) => Data (Tuple3 a b c) # | |
| (Ord a, Ord b, Ord c) => Ord (Tuple3 a b c) # | |
| (Show a, Show b, Show c) => Show (Tuple3 a b c) # | |
| Generic (Tuple3 a b c) # | |
| Thdable (Tuple3 a b c) # | |
| Sndable (Tuple3 a b c) # | |
| Fstable (Tuple3 a b c) # | |
| (Hashable a, Hashable b, Hashable c) => Hashable (Tuple3 a b c) # | |
| type NthTy 1 (Tuple3 a b c) # | |
| type NthTy 2 (Tuple3 a b c) # | |
| type NthTy 3 (Tuple3 a b c) # | |
| type Rep (Tuple3 a b c) # | |
| type ProductThird (Tuple3 a b c) # | |
| type ProductSecond (Tuple3 a b c) # | |
| type ProductFirst (Tuple3 a b c) # | |
Strict tuple (a,b,c,d)
Constructors
| Tuple4 !a !b !c !d |
Instances
| Nthable 1 (Tuple4 a b c d) # | |
| Nthable 2 (Tuple4 a b c d) # | |
| Nthable 3 (Tuple4 a b c d) # | |
| Nthable 4 (Tuple4 a b c d) # | |
| (Eq a, Eq b, Eq c, Eq d) => Eq (Tuple4 a b c d) # | |
| (Data a, Data b, Data c, Data d) => Data (Tuple4 a b c d) # | |
| (Ord a, Ord b, Ord c, Ord d) => Ord (Tuple4 a b c d) # | |
| (Show a, Show b, Show c, Show d) => Show (Tuple4 a b c d) # | |
| Generic (Tuple4 a b c d) # | |
| Thdable (Tuple4 a b c d) # | |
| Sndable (Tuple4 a b c d) # | |
| Fstable (Tuple4 a b c d) # | |
| (Hashable a, Hashable b, Hashable c, Hashable d) => Hashable (Tuple4 a b c d) # | |
| type NthTy 1 (Tuple4 a b c d) # | |
| type NthTy 2 (Tuple4 a b c d) # | |
| type NthTy 3 (Tuple4 a b c d) # | |
| type NthTy 4 (Tuple4 a b c d) # | |
| type Rep (Tuple4 a b c d) # | |
| type ProductThird (Tuple4 a b c d) # | |
| type ProductSecond (Tuple4 a b c d) # | |
| type ProductFirst (Tuple4 a b c d) # | |
Class of product types that have a first element
Minimal complete definition
Associated Types
type ProductFirst a #
Methods
fst :: a -> ProductFirst a #
Class of product types that have a second element
Minimal complete definition
Associated Types
type ProductSecond a #
Methods
snd :: a -> ProductSecond a #
Class of product types that have a third element
Minimal complete definition
Associated Types
type ProductThird a #
Methods
thd :: a -> ProductThird a #
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 NothingFalse
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 IntLeft 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) s3>>>either length (*2) n6
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.
Returns a list of the program's command line arguments (not including the program name).
uncurry :: (a -> b -> c) -> (a, b) -> c #
uncurry converts a curried function to a function on pairs.
until :: (a -> Bool) -> (a -> a) -> a -> a #
yields the result of applying until p ff until p holds.
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.
Type classes
Conversion of values to readable Strings.
Derived instances of Show have the following properties, which
are compatible with derived instances of Read:
- The result of
showis 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
showsPrecwill produce infix applications of the constructor. - the representation will be enclosed in parentheses if the
precedence of the top-level constructor in
xis less thand(associativity is ignored). Thus, ifdis0then the result is never surrounded in parentheses; ifdis11it is always surrounded in parentheses, unless it is an atomic expression. - If the constructor is defined using record syntax, then
showwill 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 = 5Note that right-associativity of :^: is ignored. For example,
produces the stringshow(Leaf 1 :^: Leaf 2 :^: Leaf 3)"Leaf 1 :^: (Leaf 2 :^: Leaf 3)".
Instances
show :: Show a => a -> String #
Use the Show class to create a String.
Note that this is not efficient, since an intermediate [Char] is going to be created before turning into a real String.
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.
Instances
The Eq class defines equality (==) and inequality (/=).
All the basic datatypes exported by the Prelude are instances of Eq,
and Eq may be derived for any datatype whose constituents are also
instances of Eq.
Instances
| 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 HandlePosn | |
| 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 ThreadId | |
| Eq BlockReason | |
| Eq ThreadStatus | |
| Eq Errno | |
| Eq AsyncException | |
| Eq ArrayException | |
| Eq ExitCode | |
| Eq IOErrorType | |
| Eq Handle | |
| Eq BufferMode | |
| Eq Newline | |
| Eq NewlineMode | |
| 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 IODeviceType | |
| Eq SeekMode | |
| Eq All | |
| Eq Any | |
| Eq Fixity | |
| Eq Associativity | |
| Eq SourceUnpackedness | |
| Eq SourceStrictness | |
| Eq DecidedStrictness | |
| Eq MaskingState | |
| Eq IOException | |
| Eq ErrorCall | |
| Eq ArithException | |
| Eq Fingerprint | |
| Eq SomeNat | |
| Eq SomeSymbol | |
| Eq IOMode | |
| Eq Lexeme | |
| Eq Number | |
| Eq GeneralCategory | |
| Eq SrcLoc | |
| Eq PartialError # | |
| Eq Sign # | |
| Eq Bitmap # | |
| Eq AsciiString # | |
| Eq Encoding # | |
| Eq ValidationFailure # | |
| Eq String # | |
| Eq FileName # | |
| Eq FilePath # | |
| Eq Relativity # | |
| Eq Endianness # | |
| Eq Arch # | |
| Eq OS # | |
| Eq a => Eq [a] | |
| Eq a => Eq (Maybe a) | |
| Eq a => Eq (Ratio a) | |
| Eq (StablePtr 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 (TVar 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 (Size ty) # | |
| Eq (Offset ty) # | |
| (PrimType ty, Eq ty) => Eq (UArray ty) # | |
| Eq a => Eq (NonEmpty a) # | |
| Eq a => Eq (Array a) # | |
| PrimType ty => Eq (ChunkedUArray ty) # | |
| Eq input => Eq (ParserError input) # | |
| (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) | |
| (Ix i, Eq e) => Eq (Array i e) | |
| Eq a => Eq (Arg a b) | |
| Eq (Proxy k s) | |
| Eq (STRef s a) | |
| (Eq a, Eq b) => Eq (Tuple2 a b) # | |
| 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 (STArray s i e) | |
| Eq a => Eq (Const k a b) | |
| Eq (f a) => Eq (Alt k f a) | |
| Eq (Coercion k a b) | |
| Eq ((:~:) k a b) | |
| (Eq a, Eq b, Eq c) => Eq (Tuple3 a b c) # | |
| 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 (Tuple4 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.
Instances
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
andsuccmaxBoundshould result in a runtime error.predminBound fromEnumandtoEnumshould give a runtime error if the result value is not representable in the result type. For example,is an error.toEnum7 ::BoolenumFromandenumFromThenshould 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 = minBoundInstances
The Functor class is used for types that can be mapped over.
Instances of Functor should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor for lists, Maybe and IO
satisfy these laws.
Minimal complete definition
Instances
Integral Literal support
e.g. 123 :: Integer 123 :: Word8
Minimal complete definition
Methods
fromInteger :: Integer -> a #
Instances
class Fractional a where #
Fractional Literal support
e.g. 1.2 :: Double 0.03 :: Float
Minimal complete definition
Methods
fromRational :: Rational -> a #
Instances
Formally, the class Bifunctor represents a bifunctor
from Hask -> Hask.
Intuitively it is a bifunctor where both the first and second arguments are covariant.
You can define a Bifunctor by either defining bimap or by
defining both first and second.
If you supply bimap, you should ensure that:
bimapidid≡id
If you supply first and second, ensure:
firstid≡idsecondid≡id
If you supply both, you should also ensure:
bimapf g ≡firstf.secondg
These ensure by parametricity:
bimap(f.g) (h.i) ≡bimapf h.bimapg ifirst(f.g) ≡firstf.firstgsecond(f.g) ≡secondf.secondg
Since: 4.8.0.0
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
pureid<*>v = v- composition
pure(.)<*>u<*>v<*>w = u<*>(v<*>w)- homomorphism
puref<*>purex =pure(f x)- interchange
u
<*>purey =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).
Instances
class Applicative m => Monad m where #
The Monad class defines the basic operations over a monad,
a concept from a branch of mathematics known as category theory.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an abstract datatype of actions.
Haskell's do expressions provide a convenient syntax for writing
monadic expressions.
Instances of Monad should satisfy the following laws:
Furthermore, the Monad and Applicative operations should relate as follows:
The above laws imply:
and that pure and (<*>) satisfy the applicative functor laws.
The instances of Monad for lists, Maybe and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Instances
| Monad [] | |
| Monad Maybe | |
| Monad IO | |
| Monad U1 | |
| Monad Par1 | |
| Monad P | |
| Monad Identity | |
| Monad Min | |
| Monad Max | |
| Monad First | |
| Monad Last | |
| Monad Option | |
| Monad NonEmpty | |
| Monad STM | |
| Monad Dual | |
| Monad Sum | |
| Monad Product | |
| Monad First | |
| Monad Last | |
| Monad ReadPrec | |
| Monad ReadP | |
| Monad Partial # | |
| 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 (MonadRandomState gen) # | |
| Monad (Parser input) # | |
| (Monad f, Monad g) => Monad ((:*:) f g) | |
| Monad f => Monad (Alt * f) | |
| Monad st => Monad (Builder col st) # | |
| Monad m => Monad (ReaderT r m) # | |
| (Functor m, Monad m) => Monad (StateT s m) # | |
| Monad f => Monad (M1 i c f) | |
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=, but with the arguments interchanged.
Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).
The IsList class and its methods are intended to be used in
conjunction with the OverloadedLists extension.
Since: 4.7.0.0
Associated Types
The Item type function returns the type of items of the structure
l.
Instances
| IsList CallStack | Be aware that 'fromList . toList = id' only for unfrozen Since: 4.9.0.0 |
| IsList Version | Since: 4.8.0.0 |
| IsList Bitmap # | |
| IsList AsciiString # | |
| IsList String # | |
| IsList [a] | |
| IsList (NonEmpty a) | |
| PrimType ty => IsList (UArray ty) # | |
| Collection c => IsList (NonEmpty c) # | |
| IsList (Array ty) # | |
| PrimType ty => IsList (ChunkedUArray ty) # | |
Numeric type classes
class (Enum a, Eq a, Ord a, Integral a) => IsIntegral a where #
Number literals, convertible through the generic Integer type.
all number are Enum'erable, meaning that you can move to next element
Minimal complete definition
Instances
class (Enum a, Eq a, Ord a, Integral a, IsIntegral a) => IsNatural a where #
Minimal complete definition
types that have sign and can be made absolute
Represent class of things that can be added together, contains a neutral element and is commutative.
x + azero = x azero + x = x x + y = y + x
class Subtractive a where #
Represent class of things that can be subtracted.
Note that the result is not necessary of the same type as the operand depending on the actual type.
For example:
(-) :: Int -> Int -> Int (-) :: DateTime -> DateTime -> Seconds (-) :: Ptr a -> Ptr a -> PtrDiff (-) :: Natural -> Natural -> Maybe Natural
Minimal complete definition
Associated Types
type Difference a #
Methods
(-) :: a -> a -> Difference a infixl 6 #
Instances
class Multiplicative a where #
Represent class of things that can be multiplied together
x * midentity = x midentity * x = x
Methods
Identity element over multiplication
Multiplication of 2 elements that result in another element
(^) :: (IsNatural n, IDivisible n) => a -> n -> a infixr 8 #
Raise to power, repeated multiplication e.g. > a ^ 2 = a * a > a ^ 10 = (a ^ 5) * (a ^ 5) .. (^) :: (IsNatural n) => a -> n -> a
Instances
class (Additive a, Multiplicative a) => IDivisible a where #
Represent types that supports an euclidian division
(x ‘div‘ y) * y + (x ‘mod‘ y) == x
class Multiplicative a => Divisible a where #
Minimal complete definition
Data 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.
Instances
| 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 |
| 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 Failure 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).
Instances
| Bounded Char | |
| Enum Char | |
| Eq Char | |
| Data Char | |
| Ord Char | |
| Read Char | |
| Show Char | |
| Ix Char | |
| Storable Char | |
| PrimType Char # | |
| StorableFixed Char # | |
| Storable 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 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 IntString or an Int. The Left constructor can be used only on
Strings, and the Right constructor can be used only on Ints:
>>>let s = Left "foo" :: Either String Int>>>sLeft "foo">>>let n = Right 3 :: Either String Int>>>nRight 3>>>:type ss :: Either String Int>>>:type nn :: 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) sLeft "foo">>>fmap (*2) nRight 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 Ints.
>>>:{let parseMultiple :: Either String Int parseMultiple = do x <- parseEither '1' y <- parseEither '2' return (x + y)>>>:}
>>>parseMultipleRight 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)>>>:}
>>>parseMultipleLeft "parse error"
Instances
| Bifunctor Either | |
| Monad (Either e) | |
| Functor (Either a) | |
| Applicative (Either e) | |
| Foldable (Either a) | |
| Generic1 (Either a) | |
| MonadFailure (Either a) # | |
| (Eq a, Eq b) => Eq (Either a b) | |
| (Data a, Data b) => Data (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) | |
| type Rep1 (Either a) | |
| type Failure (Either a) # | |
| type Rep (Either a b) | |
| type (==) (Either k k1) a b | |
Numbers
8-bit signed integer type
Instances
16-bit signed integer type
Instances
32-bit signed integer type
Instances
64-bit signed integer type
Instances
8-bit unsigned integer type
Instances
16-bit unsigned integer type
Instances
32-bit unsigned integer type
Instances
64-bit unsigned integer type
Instances
Instances
| Bounded Word | |
| Enum Word | |
| Eq Word | |
| Integral Word | |
| Data Word | |
| Num Word | |
| Ord Word | |
| Read Word | |
| Real Word | |
| Show Word | |
| Ix Word | |
| Storable Word | |
| Bits Word | |
| FiniteBits Word | |
| HasNegation Word # | |
| Integral Word # | |
| IsNatural Word # | |
| IsIntegral Word # | |
| Additive Word # | |
| Subtractive Word # | |
| IDivisible Word # | |
| Multiplicative Word # | |
| Functor (URec Word) | |
| Foldable (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 Difference Word # | |
| type Rep1 (URec Word) | |
| type Rep (URec Word p) | |
A fixed-precision integer type with at least the range [-2^29 .. 2^29-1].
The exact range for a given implementation can be determined by using
minBound and maxBound from the Bounded class.
Instances
| Bounded Int | |
| Enum Int | |
| Eq Int | |
| Integral Int | |
| Data Int | |
| Num Int | |
| Ord Int | |
| Read Int | |
| Real Int | |
| Show Int | |
| Ix Int | |
| Storable Int | |
| Bits Int | |
| FiniteBits Int | |
| HasNegation Int # | |
| Integral Int # | |
| IsIntegral Int # | |
| Additive Int # | |
| Subtractive Int # | |
| IDivisible Int # | |
| Multiplicative Int # | |
| Signed Int # | |
| Functor (URec Int) | |
| Foldable (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 Difference Int # | |
| type Rep1 (URec Int) | |
| type Rep (URec Int p) | |
Invariant: Jn# and Jp# are used iff value doesn't fit in S#
Useful properties resulting from the invariants:
Instances
Type representing arbitrary-precision non-negative integers.
Operations whose result would be negative
.throw (Underflow :: ArithException)
Since: 4.8.0.0
Instances
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.
Instances
| Eq Float | |
| Floating Float | |
| Data Float | |
| Ord Float | |
| Read Float | |
| RealFloat Float | |
| Storable Float | |
| Fractional Float # | |
| Integral Float # | |
| Additive Float # | |
| Subtractive Float # | |
| Divisible Float # | |
| Multiplicative Float # | |
| IntegralRounding Float # | |
| PrimType Float # | |
| StorableFixed Float # | |
| Storable 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) | |
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.
Instances
| Eq Double | |
| Floating Double | |
| Data Double | |
| Ord Double | |
| Read Double | |
| RealFloat Double | |
| Storable Double | |
| Fractional Double # | |
| Integral Double # | |
| Additive Double # | |
| Subtractive Double # | |
| Divisible Double # | |
| Multiplicative Double # | |
| IntegralRounding Double # | |
| PrimType Double # | |
| StorableFixed Double # | |
| Storable 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) | |
Size of a data structure.
More specifically, it represents the number of elements of type ty that fit
into the data structure.
>>>lengthSize (fromList ['a', 'b', 'c', '🌟']) :: Size CharSize 4
Same caveats as Offset apply here.
Offset in a data structure consisting of elements of type ty.
Int is a terrible backing type which is hard to get away from, considering that GHC/Haskell are mostly using this for offset. Trying to bring some sanity by a lightweight wrapping.
Collection types
An array of type built on top of GHC primitive.
The elements need to have fixed sized and the representation is a packed contiguous array in memory that can easily be passed to foreign interface
Instances
| PrimType ty => IsList (UArray ty) # | |
| (PrimType ty, Eq ty) => Eq (UArray ty) # | |
| Data ty => Data (UArray ty) # | |
| (PrimType ty, Ord ty) => Ord (UArray ty) # | |
| (PrimType ty, Show ty) => Show (UArray ty) # | |
| PrimType ty => Monoid (UArray ty) # | |
| PrimType ty => Buildable (UArray ty) # | |
| PrimType ty => Foldable (UArray ty) # | |
| PrimType ty => IndexedCollection (UArray ty) # | |
| PrimType ty => InnerFunctor (UArray ty) # | |
| PrimType ty => Collection (UArray ty) # | |
| PrimType ty => Sequential (UArray ty) # | |
| PrimType ty => Zippable (UArray ty) # | |
| PrimType a => Hashable (UArray a) # | |
| type Item (UArray ty) # | |
| type Element (UArray ty) # | |
| type Mutable (UArray ty) # | |
| type Step (UArray ty) # | |
Represent the accessor for types that can be stored in the UArray and MUArray.
Types need to be a instance of storable and have fixed sized.
Minimal complete definition
primSizeInBytes, primBaUIndex, primMbaURead, primMbaUWrite, primAddrIndex, primAddrRead, primAddrWrite
Array of a
Instances
| Functor Array # | |
| IsList (Array ty) # | |
| Eq a => Eq (Array a) # | |
| Data ty => Data (Array ty) # | |
| Ord a => Ord (Array a) # | |
| Show a => Show (Array a) # | |
| Monoid (Array a) # | |
| Buildable (Array ty) # | |
| Foldable (Array ty) # | |
| IndexedCollection (Array ty) # | |
| InnerFunctor (Array ty) # | |
| Collection (Array ty) # | |
| Sequential (Array ty) # | |
| BoxedZippable (Array ty) # | |
| Zippable (Array ty) # | |
| type Item (Array ty) # | |
| type Element (Array ty) # | |
| type Mutable (Array ty) # | |
| type Step (Array ty) # | |
Opaque packed array of characters in the UTF8 encoding
Instances
| IsList String # | |
| Eq String # | |
| Data String # | |
| Ord String # | |
| Show String # | |
| IsString String # | |
| Monoid String # | |
| Buildable String # | |
| InnerFunctor String # | |
| Collection String # | |
| Sequential String # | |
| Zippable String # | |
| Hashable String # | |
| type Item String # | |
| type Element String # | |
| type Mutable String # | |
| type Step String # | |
Numeric functions
(^^) :: (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
Monoids
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 =
foldrmappend 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 newtypes and make those instances
of Monoid, e.g. Sum and Product.
Instances
| Monoid Ordering | |
| Monoid () | |
| Monoid All | |
| Monoid Any | |
| Monoid Bitmap # | |
| Monoid AsciiString # | |
| Monoid String # | |
| Monoid FileName # | |
| 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) | |
| PrimType ty => Monoid (UArray ty) # | |
| Monoid (Array a) # | |
| Monoid (ChunkedUArray 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) | |
Collection
class (IsList c, Item c ~ Element c) => Collection c where #
A set of methods for ordered colection
Methods
Check if a collection is empty
Length of a collection (number of Element c)
elem :: forall a. (Eq a, a ~ Element c) => Element c -> c -> Bool #
Check if a collection contains a specific element
This is the inverse of notElem.
notElem :: forall a. (Eq a, a ~ Element c) => Element c -> c -> Bool #
Check if a collection does *not* contain a specific element
This is the inverse of elem.
maximum :: forall a. (Ord a, a ~ Element c) => NonEmpty c -> Element c #
Get the maximum element of a collection
minimum :: forall a. (Ord a, a ~ Element c) => NonEmpty c -> Element c #
Get the minimum element of a collection
Instances
| Collection Bitmap # | |
| Collection AsciiString # | |
| Collection String # | |
| Collection [a] # | |
| PrimType ty => Collection (UArray ty) # | |
| Collection c => Collection (NonEmpty c) # | |
| Collection (Array ty) # | |
| PrimType ty => Collection (ChunkedUArray ty) # | |
NonEmpty property for any Collection
This can only be made, through the nonEmpty smart contructor
nonEmpty :: Collection c => c -> Maybe (NonEmpty c) #
Smart constructor to create a NonEmpty collection
If the collection is empty, then Nothing is returned Otherwise, the collection is wrapped in the NonEmpty property
Folds
class Foldable collection where #
Give the ability to fold a collection on itself
Methods
foldl :: (a -> Element collection -> a) -> a -> collection -> a #
Left-associative fold of a structure.
In the case of lists, foldl, when applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
Note that to produce the outermost application of the operator the entire input list must be traversed. This means that foldl' will diverge if given an infinite list.
Also note that if you want an efficient left-fold, you probably want to use foldl' instead of foldl. The reason for this is that latter does not force the "inner" results (e.g. z f x1 in the above example) before applying them to the operator (e.g. to (f x2)). This results in a thunk chain O(n) elements long, which then must be evaluated from the outside-in.
foldl' :: (a -> Element collection -> a) -> a -> collection -> a #
Left-associative fold of a structure but with strict application of the operator.
foldr :: (Element collection -> a -> a) -> a -> collection -> a #
Right-associative fold of a structure.
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
foldr' :: (Element collection -> a -> a) -> a -> collection -> a #
Right-associative fold of a structure, but with strict application of the operator.
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 Maybes 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]
maybeToList :: Maybe a -> [a] #
The maybeToList function returns an empty list when given
Nothing or a singleton list when not given Nothing.
Examples
Basic usage:
>>>maybeToList (Just 7)[7]
>>>maybeToList Nothing[]
One can use maybeToList to avoid pattern matching when combined
with a function that (safely) works on lists:
>>>import Text.Read ( readMaybe )>>>sum $ maybeToList (readMaybe "3")3>>>sum $ maybeToList (readMaybe "")0
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
Function
Applicative
(<$>) :: 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 Stringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to an Either Int IntEither IntString using show:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "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)
(<|>) :: Alternative f => forall a. f a -> f a -> f a #
An associative binary operation
Monad
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right Kleisli composition of monads.
Exceptions
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 MyExceptionThe 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 = frontendExceptionFromExceptionWe can now catch a MismatchedParentheses exception as
MismatchedParentheses, SomeFrontendException or
SomeCompilerException, but not other types, e.g. IOException:
*Main> throw MismatchedParenthesescatche -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatche -> putStrLn ("Caught " ++ show (e :: SomeFrontendException)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatche -> putStrLn ("Caught " ++ show (e :: SomeCompilerException)) Caught MismatchedParentheses *Main> throw MismatchedParenthesescatche -> putStrLn ("Caught " ++ show (e :: IOException)) *** Exception: MismatchedParentheses
Minimal complete definition
Nothing
Instances
The class Typeable allows a concrete representation of a type to
be calculated.
Minimal complete definition
data SomeException :: * #
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.
Instances
data IOException :: * #
Exceptions that occur in the IO monad.
An IOException records a more specific error type, a descriptive
string and maybe the handle that was used when the error was
flagged.
Instances
Proxy
data Proxy k t :: forall k. k -> * #
A concrete, poly-kinded proxy type
Constructors
| Proxy |
Instances
| Monad (Proxy *) | |
| Functor (Proxy *) | |
| Applicative (Proxy *) | |
| Foldable (Proxy *) | |
| Generic1 (Proxy *) | |
| Alternative (Proxy *) | |
| MonadPlus (Proxy *) | |
| Bounded (Proxy k s) | |
| Enum (Proxy k s) | |
| Eq (Proxy k s) | |
| Data t => Data (Proxy * t) | |
| 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) | |
| type Rep1 (Proxy *) | |
| type Rep (Proxy k t) | |
asProxyTypeOf :: a -> Proxy * a -> a #
asProxyTypeOf is a type-restricted version of const.
It is usually used as an infix operator, and its typing forces its first
argument (which is usually overloaded) to have the same type as the tag
of the second.
Partial
Partialiality wrapper.
Create a value that is partial. this can only be
unwrap using the fromPartial function
data PartialError #
An error related to the evaluation of a Partial value that failed.
it contains the name of the function and the reason for failure
Instances
fromPartial :: Partial a -> a #
Dewrap a possible partial value