Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module reexports the modules that every program using SubHask will need. You should import it instead of Prelude.
- module SubHask.Algebra
- module SubHask.Category
- module SubHask.Compatibility.Base
- class Show a where
- class Read a where
- read :: Read a => String -> a
- class Storable a where
- type String = [Char]
- type FilePath = String
- data Char :: *
- data Int :: *
- data Int8 :: *
- data Int16 :: *
- data Int32 :: *
- data Int64 :: *
- data Integer :: *
- data Float :: *
- data Double :: *
- type Rational = Ratio Integer
- data Bool :: *
- data IO a :: * -> *
- data ST s a :: * -> * -> *
- data Maybe a :: * -> *
- data Either a b :: * -> * -> *
- build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
- (++) :: [a] -> [a] -> [a]
- all :: Foldable t => (a -> Bool) -> t a -> Bool
- map :: (a -> b) -> [a] -> [b]
- asTypeOf :: a -> a -> a
- undefined :: a
- otherwise :: Bool
- error :: [Char] -> a
- seq :: a -> b -> b
- assert :: String -> Bool -> a -> a
- ifThenElse :: Bool -> t -> t -> t
- module Data.Proxy
- module Data.Typeable
- module GHC.TypeLits
- module Control.DeepSeq
- class Arbitrary a where
- data Constraint :: BOX
- module SubHask.Monad
- module SubHask.SubType
Documentation
module SubHask.Algebra
module SubHask.Category
module SubHask.Compatibility.Base
classes
class Show a where
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)"
.
:: Int | the operator precedence of the enclosing
context (a number from |
-> a | the value to be converted to 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.
class Read a where
Parsing of String
s, producing values.
Derived instances of Read
make the following assumptions, which
derived instances of Show
obey:
- If the constructor is defined to be an infix operator, then the
derived
Read
instance will parse only infix applications of the constructor (not the prefix form). - Associativity is not used to reduce the occurrence of parentheses, although precedence may be.
- If the constructor is defined using record syntax, the derived
Read
will parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration. - The derived
Read
instance allows arbitrary Haskell whitespace between tokens of the input string. Extra parentheses are also allowed.
For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance of Read
in Haskell 2010 is equivalent to
instance (Read a) => Read (Tree a) where readsPrec d r = readParen (d > app_prec) (\r -> [(Leaf m,t) | ("Leaf",s) <- lex r, (m,t) <- readsPrec (app_prec+1) s]) r ++ readParen (d > up_prec) (\r -> [(u:^:v,w) | (u,s) <- readsPrec (up_prec+1) r, (":^:",t) <- lex s, (v,w) <- readsPrec (up_prec+1) t]) r where app_prec = 10 up_prec = 5
Note that right-associativity of :^:
is unused.
The derived instance in GHC is equivalent to
instance (Read a) => Read (Tree a) where readPrec = parens $ (prec app_prec $ do Ident "Leaf" <- lexP m <- step readPrec return (Leaf m)) +++ (prec up_prec $ do u <- step readPrec Symbol ":^:" <- lexP v <- step readPrec return (u :^: v)) where app_prec = 10 up_prec = 5 readListPrec = readListPrecDefault
:: Int | the operator precedence of the enclosing
context (a number from |
-> ReadS a |
attempts to parse a value from the front of the string, returning a list of (parsed value, remaining string) pairs. If there is no successful parse, the returned list is empty.
Derived instances of Read
and Show
satisfy the following:
That is, readsPrec
parses the string produced by
showsPrec
, and delivers the value that
showsPrec
started with.
Read Bool | |
Read Char | |
Read Double | |
Read Float | |
Read Int | |
Read Int8 | |
Read Int16 | |
Read Int32 | |
Read Int64 | |
Read Integer | |
Read Ordering | |
Read Word | |
Read Word8 | |
Read Word16 | |
Read Word32 | |
Read Word64 | |
Read () | |
Read QCGen | |
Read ByteString | |
Read ByteString | |
Read Void | Reading a |
Read SomeNat | |
Read SomeSymbol | |
Read Natural | |
Read Version | |
Read ExitCode | |
Read WordPtr | |
Read IntPtr | |
Read CChar | |
Read CSChar | |
Read CUChar | |
Read CShort | |
Read CUShort | |
Read CInt | |
Read CUInt | |
Read CLong | |
Read CULong | |
Read CLLong | |
Read CULLong | |
Read CFloat | |
Read CDouble | |
Read CPtrdiff | |
Read CSize | |
Read CWchar | |
Read CSigAtomic | |
Read CClock | |
Read CTime | |
Read CUSeconds | |
Read CSUSeconds | |
Read CIntPtr | |
Read CUIntPtr | |
Read CIntMax | |
Read CUIntMax | |
Read All | |
Read Any | |
Read Arity | |
Read Fixity | |
Read Associativity | |
Read Lexeme | |
Read ShortByteString | |
Read IntSet | |
Read POrdering | |
Read K3 | |
Read H3 | |
Read a => Read [a] | |
(Integral a, Read a) => Read (Ratio a) | |
Read (U1 p) | |
Read p => Read (Par1 p) | |
Read a => Read (Mass a) | |
Read a => Read (Approximate a) | |
Read a => Read (Maybe a) | |
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the
|
HasResolution a => Read (Fixed a) | |
Read a => Read (Complex a) | |
Read a => Read (ZipList a) | |
Read a => Read (Dual a) | |
Read a => Read (Sum a) | |
Read a => Read (Product a) | |
Read a => Read (First a) | |
Read a => Read (Last a) | |
Read a => Read (Down a) | |
Read a => Read (Only a) | |
(Read a, Storable a) => Read (Vector a) | |
Read e => Read (IntMap e) | |
(Read a, Ord a) => Read (Set a) | |
Read a => Read (Tree a) | |
Read a => Read (Seq a) | |
Read a => Read (ViewL a) | |
Read a => Read (ViewR a) | |
Read a => Read (Vector a) | |
(Read a, Prim a) => Read (Vector a) | |
(Eq a, Hashable a, Read a) => Read (HashSet a) | |
(Floating a, Read a) => Read (Log a) | |
Read a => Read (Min a) | |
Read a => Read (Max a) | |
Read a => Read (First a) | |
Read a => Read (Last a) | |
Read m => Read (WrappedMonoid m) | |
Read a => Read (Option a) | |
Read a => Read (NonEmpty a) | |
Read a0 => Read (WithPreludeOrd a) | |
Read t0 => Read (NonNegative t) | |
Read (Z n0) => Read (VedicSquare n) | |
(Read v, Read (Scalar v)) => Read (Ball v) | |
Read (Z (Order a0)) => Read (ZIndex a) | |
Read v => Read (Box v) | |
Read a0 => Read (Jaccard a) | |
Read a0 => Read (Hamming a) | |
Read a0 => Read (Levenshtein a) | |
Read s0 => Read (Uncompensated s) | |
Read a0 => Read (Lexical a) | |
Read a0 => Read (ComponentWise a) | |
Read r0 => Read (Goedel_ r) | |
Read b0 => Read (Boolean2Ring b) | |
Read a => Read (Seq a) | |
Read e => Read (IntMap e) | |
Read e => Read (IntMap' e) | |
Read (ByteString Char) | |
Read a0 => Read (PartitionOnNewline a) | |
(Read a, Read b) => Read (Either a b) | |
Read (f p) => Read (Rec1 f p) | |
(Read a, Read b) => Read (a, b) | |
(Ix a, Read a, Read b) => Read (Array a b) | |
Read a => Read (Const a b) | |
Read (Proxy k s) | |
(Read1 f, Read a) => Read (IdentityT f a) | |
(Ord k, Read k, Read e) => Read (Map k e) | |
(Read (f (Cofree f a)), Read a) => Read (Cofree f a) | |
(Functor f, Read (f a)) => Read (Yoneda f a) | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) | |
(Read1 f, Read a) => Read (Backwards f a) | |
(Read1 m, Read a) => Read (ListT m a) | |
(Read1 m, Read a) => Read (MaybeT m a) | |
(Read a, Read b) => Read (Arg a b) | |
(Read1 g, Read a) => Read (Apply g a) | |
(Read1 f, Read a) => Read (Reverse f a) | |
Read a => Read (Constant a b) | |
(Read x, Read y) => Read (Labeled' x y) | |
Read (Z ((^) p0 k0)) => Read (Galois p k) | |
Read c => Read (K1 i c p) | |
(Read (f p), Read (g p)) => Read ((:+:) f g p) | |
(Read (f p), Read (g p)) => Read ((:*:) f g p) | |
Read (f (g p)) => Read ((:.:) f g p) | |
(Read a, Read b, Read c) => Read (a, b, c) | |
Read (f a) => Read (Alt k f a) | |
(~) k a b => Read ((:~:) k a b) | |
(Read (f a), Read (g a)) => Read (Coproduct f g a) | |
(Functor f, Read1 f, Read1 g, Read a) => Read (Compose f g a) | |
(Read e, Read1 m, Read a) => Read (ErrorT e m a) | |
(Read e, Read1 m, Read a) => Read (ExceptT e m a) | |
(Read w, Read1 m, Read a) => Read (WriterT w m a) | |
(Read w, Read1 m, Read a) => Read (WriterT w m a) | |
Read b => Read (Tagged k s b) | |
(Read1 f, Read1 g, Read a) => Read (Product f g a) | |
Read a0 => Read ((/) k a b) | |
Read (f p) => Read (M1 i c f p) | |
(Read a, Read b, Read c, Read d) => Read (a, b, c, d) | |
(Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) | |
(Read a, Read b, Read c, Read d, Read e, Read f) => Read (a, b, c, d, e, f) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g) => Read (a, b, c, d, e, f, g) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h) => Read (a, b, c, d, e, f, g, h) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i) => Read (a, b, c, d, e, f, g, h, i) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j) => Read (a, b, c, d, e, f, g, h, i, j) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k) => Read (a, b, c, d, e, f, g, h, i, j, k) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l) => Read (a, b, c, d, e, f, g, h, i, j, k, l) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | |
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n, Read o) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) |
The read
function reads input from a string, which must be
completely consumed by the input process.
class Storable a where
The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.
Memory addresses are represented as values of type
, for some
Ptr
aa
which is an instance of class Storable
. The type argument to
Ptr
helps provide some valuable type safety in FFI code (you can't
mix pointers of different types without an explicit cast), while
helping the Haskell type system figure out which marshalling method is
needed for a given pointer.
All marshalling between Haskell and a foreign language ultimately
boils down to translating Haskell data structures into the binary
representation of a corresponding data structure of the foreign
language and vice versa. To code this marshalling in Haskell, it is
necessary to manipulate primitive data types stored in unstructured
memory blocks. The class Storable
facilitates this manipulation on
all types for which it is instantiated, which are the standard basic
types of Haskell, the fixed size Int
types (Int8
, Int16
,
Int32
, Int64
), the fixed size Word
types (Word8
, Word16
,
Word32
, Word64
), StablePtr
, all types from Foreign.C.Types,
as well as Ptr
.
sizeOf, alignment, (peek | peekElemOff | peekByteOff), (poke | pokeElemOff | pokeByteOff)
Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.
Computes the alignment constraint of the argument. An
alignment constraint x
is fulfilled by any address divisible
by x
. The value of the argument is not used.
peekElemOff :: Ptr a -> Int -> IO a
Read a value from a memory area regarded as an array
of values of the same kind. The first argument specifies
the start address of the array and the second the index into
the array (the first element of the array has index
0
). The following equality holds,
peekElemOff addr idx = IOExts.fixIO $ \result -> peek (addr `plusPtr` (idx * sizeOf result))
Note that this is only a specification, not necessarily the concrete implementation of the function.
pokeElemOff :: Ptr a -> Int -> a -> IO ()
Write a value to a memory area regarded as an array of values of the same kind. The following equality holds:
pokeElemOff addr idx x = poke (addr `plusPtr` (idx * sizeOf x)) x
peekByteOff :: Ptr b -> Int -> IO a
Read a value from a memory location given by a base address and offset. The following equality holds:
peekByteOff addr off = peek (addr `plusPtr` off)
pokeByteOff :: Ptr b -> Int -> a -> IO ()
Write a value to a memory location given by a base address and offset. The following equality holds:
pokeByteOff addr off x = poke (addr `plusPtr` off) x
Read a value from the given memory location.
Note that the peek and poke functions might require properly
aligned addresses to function correctly. This is architecture
dependent; thus, portable code should ensure that when peeking or
poking values of some type a
, the alignment
constraint for a
, as given by the function
alignment
is fulfilled.
Write the given value to the given memory location. Alignment
restrictions might apply; see peek
.
data types
File and directory names are values of type String
, whose precise
meaning is operating system dependent. Files can be opened, yielding a
handle which can then be used to operate on the contents of that file.
data Char :: *
The character type Char
is an enumeration whose values represent
Unicode (or equivalently ISO/IEC 10646) characters (see
http://www.unicode.org/ for details). This set extends the ISO 8859-1
(Latin-1) character set (the first 256 characters), which is itself an extension
of the ASCII character set (the first 128 characters). A character literal in
Haskell has type Char
.
To convert a Char
to or from the corresponding Int
value defined
by Unicode, use toEnum
and fromEnum
from the
Enum
class respectively (or equivalently ord
and chr
).
data Int :: *
A fixed-precision integer type with at least the range [-2^29 .. 2^29-1]
.
The exact range for a given implementation can be determined by using
minBound
and maxBound
from the Bounded
class.
data Int8 :: *
8-bit signed integer type
Bounded Int8 | |
Enum Int8 | |
Eq Int8 | |
Integral Int8 | |
Num Int8 | |
Ord Int8 | |
Read Int8 | |
Real Int8 | |
Show Int8 | |
Ix Int8 | |
Arbitrary Int8 | |
CoArbitrary Int8 | |
Storable Int8 | |
Bits Int8 | |
FiniteBits Int8 | |
Serial Int8 | |
FromField Int8 | Accepts a signed decimal number. Ignores whitespace. |
ToField Int8 | Uses decimal encoding with optional sign. |
NFData Int8 | |
Hashable Int8 | |
Prim Int8 | |
Unbox Int8 | |
Lift Int8 | |
IArray UArray Int8 | |
Vector Vector Int8 | |
MVector MVector Int8 | |
MArray (STUArray s) Int8 (ST s) | |
type Unsigned Int8 = Word8 | |
type Signed Int8 = Int8 | |
data Vector Int8 = V_Int8 (Vector Int8) | |
data MVector s Int8 = MV_Int8 (MVector s Int8) |
data Int16 :: *
16-bit signed integer type
Bounded Int16 | |
Enum Int16 | |
Eq Int16 | |
Integral Int16 | |
Num Int16 | |
Ord Int16 | |
Read Int16 | |
Real Int16 | |
Show Int16 | |
Ix Int16 | |
Arbitrary Int16 | |
CoArbitrary Int16 | |
Storable Int16 | |
Bits Int16 | |
FiniteBits Int16 | |
SerialEndian Int16 | |
Serial Int16 | |
FromField Int16 | Accepts a signed decimal number. Ignores whitespace. |
ToField Int16 | Uses decimal encoding with optional sign. |
NFData Int16 | |
Hashable Int16 | |
Prim Int16 | |
Unbox Int16 | |
Lift Int16 | |
IArray UArray Int16 | |
Vector Vector Int16 | |
MVector MVector Int16 | |
MArray (STUArray s) Int16 (ST s) | |
type Unsigned Int16 = Word16 | |
type Signed Int16 = Int16 | |
data Vector Int16 = V_Int16 (Vector Int16) | |
data MVector s Int16 = MV_Int16 (MVector s Int16) |
data Int32 :: *
32-bit signed integer type
Bounded Int32 | |
Enum Int32 | |
Eq Int32 | |
Integral Int32 | |
Num Int32 | |
Ord Int32 | |
Read Int32 | |
Real Int32 | |
Show Int32 | |
Ix Int32 | |
Arbitrary Int32 | |
CoArbitrary Int32 | |
Storable Int32 | |
Bits Int32 | |
FiniteBits Int32 | |
SerialEndian Int32 | |
Serial Int32 | |
FromField Int32 | Accepts a signed decimal number. Ignores whitespace. |
ToField Int32 | Uses decimal encoding with optional sign. |
NFData Int32 | |
Hashable Int32 | |
Prim Int32 | |
Unbox Int32 | |
Lift Int32 | |
IArray UArray Int32 | |
Vector Vector Int32 | |
MVector MVector Int32 | |
MArray (STUArray s) Int32 (ST s) | |
type Unsigned Int32 = Word32 | |
type Signed Int32 = Int32 | |
data Vector Int32 = V_Int32 (Vector Int32) | |
data MVector s Int32 = MV_Int32 (MVector s Int32) |
data Int64 :: *
64-bit signed integer type
Bounded Int64 | |
Enum Int64 | |
Eq Int64 | |
Integral Int64 | |
Num Int64 | |
Ord Int64 | |
Read Int64 | |
Real Int64 | |
Show Int64 | |
Ix Int64 | |
Arbitrary Int64 | |
CoArbitrary Int64 | |
Storable Int64 | |
Bits Int64 | |
FiniteBits Int64 | |
SerialEndian Int64 | |
Serial Int64 | |
FromField Int64 | Accepts a signed decimal number. Ignores whitespace. |
ToField Int64 | Uses decimal encoding with optional sign. |
NFData Int64 | |
Hashable Int64 | |
Prim Int64 | |
Unbox Int64 | |
Lift Int64 | |
IArray UArray Int64 | |
Vector Vector Int64 | |
MVector MVector Int64 | |
MArray (STUArray s) Int64 (ST s) | |
type Unsigned Int64 = Word64 | |
type Signed Int64 = Int64 | |
data Vector Int64 = V_Int64 (Vector Int64) | |
type Scalar Int64 = Int64 Source | |
data MVector s Int64 = MV_Int64 (MVector s Int64) |
data Integer :: *
Invariant: Jn#
and Jp#
are used iff value doesn't fit in S#
Useful properties resulting from the invariants:
data Float :: *
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.
data Double :: *
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.
data Bool :: *
Bounded Bool | |
Enum Bool | |
Eq Bool | |
Ord Bool | |
Read Bool | |
Show Bool | |
Ix Bool | |
Generic Bool | |
Arbitrary Bool | |
CoArbitrary Bool | |
Storable Bool | |
Serial Bool | |
NFData Bool | |
Hashable Bool | |
Unbox Bool | |
Lift Bool | |
IsMutable Bool Source | |
Boolean Bool Source | |
Heyting Bool Source | |
Complemented Bool Source | |
Bounded Bool Source | |
Ord_ Bool Source | |
Graded Bool Source | |
Enum Bool Source | |
Lattice_ Bool Source | |
MinBound_ Bool Source | |
POrd_ Bool Source | |
Eq_ Bool Source | |
IArray UArray Bool | |
Vector Vector Bool | |
MVector MVector Bool | |
Constructible (UArray Bool) Source | |
Monoid (UArray Bool) Source | |
MArray (STUArray s) Bool (ST s) | |
type Rep Bool = D1 D1Bool ((:+:) (C1 C1_0Bool U1) (C1 C1_1Bool U1)) | |
data Vector Bool = V_Bool (Vector Word8) | |
type Logic Bool = Bool Source | |
data MVector s Bool = MV_Bool (MVector s Word8) | |
data Mutable m Bool = Mutable_ConT_GHC_Types_Bool (PrimRef m Bool) Source | |
type (==) Bool a b = EqBool a b |
data IO a :: * -> *
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 ST s a :: * -> * -> *
The strict state-transformer monad.
A computation of type
transforms an internal state indexed
by ST
s as
, and returns a value of type a
.
The s
parameter is either
- an uninstantiated type variable (inside invocations of
runST
), or RealWorld
(inside invocations ofstToIO
).
It serves to keep the internal states of different invocations
of runST
separate from each other and from invocations of
stToIO
.
The >>=
and >>
operations are strict in the state (though not in
values stored in the state). For example,
runST
(writeSTRef _|_ v >>= f) = _|_
Monad (ST s) | |
Functor (ST s) | |
Applicative (ST s) | |
PrimMonad (ST s) | |
PrimBase (ST s) | |
MArray (STUArray s) Word (ST s) | |
MArray (STUArray s) Word8 (ST s) | |
MArray (STUArray s) Word64 (ST s) | |
MArray (STUArray s) Word32 (ST s) | |
MArray (STUArray s) Word16 (ST s) | |
MArray (STUArray s) Int (ST s) | |
MArray (STUArray s) Int8 (ST s) | |
MArray (STUArray s) Int64 (ST s) | |
MArray (STUArray s) Int32 (ST s) | |
MArray (STUArray s) Int16 (ST s) | |
MArray (STUArray s) Float (ST s) | |
MArray (STUArray s) Double (ST s) | |
MArray (STUArray s) Char (ST s) | |
MArray (STUArray s) Bool (ST s) | |
MArray (STArray s) e (ST s) | |
MArray (STUArray s) (StablePtr a) (ST s) | |
MArray (STUArray s) (Ptr a) (ST s) | |
MArray (STUArray s) (FunPtr a) (ST s) | |
Show (ST s a) | |
type PrimState (ST s) = s |
data Maybe a :: * -> *
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.
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"
Prelude functions
build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
(++) :: [a] -> [a] -> [a] infixr 5
Append two lists, i.e.,
[x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn] [x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...]
If the first list is not finite, the result is the first list.
all :: Foldable t => (a -> Bool) -> t a -> Bool
Determines whether all elements of the structure satisfy the predicate.
map :: (a -> b) -> [a] -> [b]
map
f xs
is the list obtained by applying f
to each element
of xs
, i.e.,
map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] map f [x1, x2, ...] == [f x1, f x2, ...]
asTypeOf :: a -> a -> a
undefined :: a
seq :: a -> b -> b
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.
subhask functions
assert :: String -> Bool -> a -> a Source
FIXME: Move to a better spot Add rewrite rules to remove with optimization -O
ifThenElse :: Bool -> t -> t -> t Source
Modules
module Data.Proxy
module Data.Typeable
module GHC.TypeLits
module Control.DeepSeq
Non-base types
class Arbitrary a where
Random generation and shrinking of values.
Nothing
A generator for values of the given type.
shrink :: a -> [a]
Produces a (possibly) empty list of all the possible immediate shrinks of the given value. The default implementation returns the empty list, so will not try to shrink the value.
Most implementations of shrink
should try at least three things:
- Shrink a term to any of its immediate subterms.
- Recursively apply
shrink
to all immediate subterms. - Type-specific shrinkings such as replacing a constructor by a simpler constructor.
For example, suppose we have the following implementation of binary trees:
data Tree a = Nil | Branch a (Tree a) (Tree a)
We can then define shrink
as follows:
shrink Nil = [] shrink (Branch x l r) = -- shrink Branch to Nil [Nil] ++ -- shrink to subterms [l, r] ++ -- recursively shrink subterms [Branch x' l' r' | (x', l', r') <- shrink (x, l, r)]
There are a couple of subtleties here:
- QuickCheck tries the shrinking candidates in the order they
appear in the list, so we put more aggressive shrinking steps
(such as replacing the whole tree by
Nil
) before smaller ones (such as recursively shrinking the subtrees). - It is tempting to write the last line as
[Branch x' l' r' | x' <- shrink x, l' <- shrink l, r' <- shrink r]
but this is the wrong thing! It will force QuickCheck to shrinkx
,l
andr
in tandem, and shrinking will stop once one of the three is fully shrunk.
There is a fair bit of boilerplate in the code above.
We can avoid it with the help of some generic functions;
note that these only work on GHC 7.2 and above.
The function genericShrink
tries shrinking a term to all of its
subterms and, failing that, recursively shrinks the subterms.
Using it, we can define shrink
as:
shrink x = shrinkToNil x ++ genericShrink x where shrinkToNil Nil = [] shrinkToNil (Branch _ l r) = [Nil]
genericShrink
is a combination of subterms
, which shrinks
a term to any of its subterms, and recursivelyShrink
, which shrinks
all subterms of a term. These may be useful if you need a bit more
control over shrinking than genericShrink
gives you.
A final gotcha: we cannot define shrink
as simply
as this shrinks shrink
x = Nil:genericShrink
xNil
to Nil
, and shrinking will go into an
infinite loop.
If all this leaves you bewildered, you might try
to begin with,
after deriving shrink
= genericShrink
Generic
for your type. However, if your data type has any
special invariants, you will need to check that genericShrink
can't break those invariants.
module SubHask.Monad
module SubHask.SubType