Prelude.Classy
Contents
Description
Prelude replacement, use the NoImplicitPrelude extension before importing this.
It deliberately omits all list-handling functions, import Data.List or use the generic versions.
- module Data.Bool
- bool :: a -> a -> Bool -> a
- module Data.Maybe
- module Data.Either
- module Data.Eq
- module Data.Ord
- 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]
- data Char
- type String = [Char]
- fst :: (a, b) -> a
- snd :: (a, b) -> b
- lines :: String -> [String]
- words :: String -> [String]
- unlines :: [String] -> String
- unwords :: [String] -> String
- class Show a where
- class Read a where
- read :: Read a => String -> a
- data IO a
- putChar :: Char -> IO ()
- putStr :: String -> IO ()
- putStrLn :: String -> IO ()
- print :: Show a => a -> IO ()
- getChar :: IO Char
- getLine :: IO String
- readFile :: FilePath -> IO String
- writeFile :: FilePath -> String -> IO ()
- appendFile :: FilePath -> String -> IO ()
- readIO :: Read a => String -> IO a
- readLn :: Read a => IO a
- curry :: ((a, b) -> c) -> a -> b -> c
- uncurry :: (a -> b -> c) -> (a, b) -> c
- first :: Arrow a => forall b c d. a b c -> a (b, d) (c, d)
- second :: Arrow a => forall b c d. a b c -> a (d, b) (d, c)
- id :: a -> a
- const :: a -> b -> a
- (.) :: (b -> c) -> (a -> b) -> a -> c
- flip :: (a -> b -> c) -> b -> a -> c
- ($) :: (t1 -> t) -> t1 -> t
- until :: (a -> Bool) -> (a -> a) -> a -> a
- data Int
- data Integer
- class Bounded a where
- class (Eq a, Show a) => Num a where
- class (Real a, Enum a) => Integral a where
- subtract :: Num a => a -> a -> a
- even :: Integral a => a -> Bool
- odd :: Integral a => a -> Bool
- (^) :: (Num a, Integral b) => a -> b -> a
- fromIntegral :: (Integral a, Num b) => a -> b
- module Control.Applicative
- module Control.Monad
- module Data.Monoid
- (<>) :: Monoid a => a -> a -> a
- module Data.Foldable
- module Data.Traversable
- asTypeOf :: a -> a -> a
- error :: [Char] -> a
- undefined :: a
- seq :: a -> b -> b
- ($!) :: (t1 -> t) -> t1 -> t
Basic/legacy types
module Data.Bool
Arguments
| :: a | Returned if the bool is True |
| -> a | Returned if the bool is False |
| -> Bool | |
| -> a |
An either/maybe equivalent for Bool, often known as if'
module Data.Maybe
module Data.Either
module Data.Eq
module Data.Ord
class Enum a where
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 ::Bool -
enumFromandenumFromThenshould 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
Methods
succ :: a -> a
the successor of a value. For numeric types, succ adds 1.
pred :: a -> a
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.
enumFrom :: a -> [a]
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].
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 charachers), 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 Prelude.toEnum and Prelude.fromEnum from the
Prelude.Enum class respectively (or equivalently ord and chr).
fst :: (a, b) -> a
Extract the first component of a pair.
snd :: (a, b) -> b
Extract the second component of a pair.
lines breaks a string up into a list of strings at newline
characters. The resulting strings do not contain newlines.
words breaks a string up into a list of words, which were delimited
by white space.
class Show a where
Conversion of values to readable Strings.
Minimal complete definition: showsPrec or show.
Derived instances of Show have the following properties, which
are compatible with derived instances of Text.Read.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 = 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)".
Methods
Arguments
| :: 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 Text.Read.Read and Show satisfy the following:
-
(x,"")is an element of(.Text.Read.readsPrecd (showsPrecd x ""))
That is, Text.Read.readsPrec parses the string produced by
showsPrec, and delivers the value that showsPrec started with.
Instances
| Show Bool | |
| Show Char | |
| Show Double | |
| Show Float | |
| Show Int | |
| Show Integer | |
| Show Ordering | |
| Show () | |
| Show All | |
| Show Any | |
| Show a => Show [a] | |
| Integral a => Show (Ratio a) | |
| Show a => Show (Dual a) | |
| Show a => Show (Sum a) | |
| Show a => Show (Product a) | |
| Show a => Show (First a) | |
| Show a => Show (Last a) | |
| Show a => Show (Maybe a) | |
| (Show a, Show b) => Show (Either a b) | |
| (Show a, Show b) => Show (a, b) | |
| (Show a, Show b, Show c) => Show (a, b, c) | |
| (Show a, Show b, Show c, Show d) => Show (a, b, c, d) | |
| (Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e) | |
| (Show a, Show b, Show c, Show d, Show e, Show f) => Show (a, b, c, d, e, f) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g) => Show (a, b, c, d, e, f, g) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h) => Show (a, b, c, d, e, f, g, h) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i) => Show (a, b, c, d, e, f, g, h, i) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j) => Show (a, b, c, d, e, f, g, h, i, j) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k) => Show (a, b, c, d, e, f, g, h, i, j, k) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l) => Show (a, b, c, d, e, f, g, h, i, j, k, l) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | |
| (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n, Show o) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) |
class Read a where
Parsing of Strings, producing values.
Minimal complete definition: readsPrec (or, for GHC only, readPrec)
Derived instances of Read make the following assumptions, which
derived instances of Text.Show.Show obey:
- If the constructor is defined to be an infix operator, then the
derived
Readinstance 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
Readwill parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration. - The derived
Readinstance 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 98 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
Methods
Arguments
| :: 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 Text.Show.Show satisfy the following:
-
(x,"")is an element of(.readsPrecd (Text.Show.showsPrecd x ""))
That is, readsPrec parses the string produced by
Text.Show.showsPrec, and delivers the value that
Text.Show.showsPrec started with.
Instances
| Read Bool | |
| Read Char | |
| Read Double | |
| Read Float | |
| Read Int | |
| Read Integer | |
| Read Ordering | |
| Read () | |
| Read All | |
| Read Any | |
| Read Lexeme | |
| Read a => Read [a] | |
| (Integral a, Read a) => Read (Ratio 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 (Maybe a) | |
| (Read a, Read b) => Read (Either a b) | |
| (Read a, Read b) => Read (a, b) | |
| (Ix a, Read a, Read b) => Read (Array a b) | |
| (Read a, Read b, Read c) => Read (a, b, c) | |
| (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.
Basic I/O
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.
The print function outputs a value of any printable type to the
standard output device.
Printable types are those that are instances of class Show; print
converts values to strings for output using the show operation and
adds a newline.
For example, a program to print the first 20 integers and their powers of 2 could be written as:
main = print ([(n, 2^n) | n <- [0..19]])
readFile :: FilePath -> IO String
The readFile function reads a file and
returns the contents of the file as a string.
The file is read strictly, as with getContents.
writeFile :: FilePath -> String -> IO ()
The computation writeFile file str function writes the string str,
to the file file.
appendFile :: FilePath -> String -> IO ()
The computation appendFile file str function appends the string str,
to the file file.
Note that writeFile and appendFile write a literal string
to a file. To write a value of any printable type, as with print,
use the show function to convert the value to a string first.
main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
Basic function composition
first :: Arrow a => forall b c d. a b c -> a (b, d) (c, d)
Send the first component of the input through the argument arrow, and copy the rest unchanged to the output.
second :: Arrow a => forall b c d. a b c -> a (d, b) (d, c)
A mirror image of first.
The default definition may be overridden with a more efficient version if desired.
id :: a -> a
Identity function.
const :: a -> b -> a
Constant function.
(.) :: (b -> c) -> (a -> b) -> a -> c
Function composition.
flip :: (a -> b -> c) -> b -> a -> c
takes its (first) two arguments in the reverse order of flip ff.
Integer math
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
Prelude.minBound and Prelude.maxBound from the Prelude.Bounded class.
data Integer
Arbitrary-precision integers.
class Bounded a where
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 (Eq a, Show a) => Num a where
Basic numeric class.
Minimal complete definition: all except negate or (-)
Methods
(+) :: a -> a -> a
(*) :: a -> a -> a
(-) :: a -> a -> a
negate :: a -> a
Unary negation.
abs :: a -> a
Absolute value.
signum :: a -> a
Sign of a number.
The functions abs and signum should satisfy the law:
abs x * signum x == x
For real numbers, the signum is either -1 (negative), 0 (zero)
or 1 (positive).
fromInteger :: Integer -> a
Conversion from an Integer.
An integer literal represents the application of the function
fromInteger to the appropriate value of type Integer,
so such literals have type (.
Num a) => a
class (Real a, Enum a) => Integral a where
Methods
quot :: a -> a -> a
integer division truncated toward zero
rem :: a -> a -> a
integer remainder, satisfying
(x `quot` y)*y + (x `rem` y) == x
div :: a -> a -> a
integer division truncated toward negative infinity
mod :: a -> a -> a
integer modulus, satisfying
(x `div` y)*y + (x `mod` y) == x
quotRem :: a -> a -> (a, a)
divMod :: a -> a -> (a, a)
conversion to Integer
fromIntegral :: (Integral a, Num b) => a -> b
general coercion from integral types
Monad hierarchy
module Control.Applicative
module Control.Monad
Monoids, Foldables and other goodies
module Data.Monoid
module Data.Foldable
module Data.Traversable
Misc.
asTypeOf :: a -> a -> a
undefined :: a
seq :: a -> b -> b
Evaluates its first argument to head normal form, and then returns its second argument as the result.