The standard prelude of Curry with type classes. All exported functions, data types, type classes and methods defined in this module are always available in any Curry program.
Exported Datatypes: Bool, Char, DET, Either, FilePath, Float, Int, IO, IOError, Maybe, Ordering, ReadS, ShowS, String, Success
Exported Functions: !!, $, $!, $!!, $#, $##, &, &&, &>, ++, ., /==, <$>, =:<<=, =:<=, =:=, =<<, ?, ^, all, and, any, anyOf, ap, appendFile, apply, asTypeOf, break, catch, chr, concat, concatMap, cond, const, constrEq, curry, doSolve, drop, dropWhile, either, elem, ensureNotFree, ensureSpine, eqString, error, even, failed, filter, flip, foldl, foldl1, foldr, foldr1, fromIntegral, fst, getChar, getLine, groundNormalForm, head, id, ifThenElse, ioError, isAlpha, isAlphaNum, isBinDigit, isDigit, isHexDigit, isLower, isOctDigit, isSpace, isUpper, iterate, length, lex, liftM2, lines, lookup, map, mapM, mapM_, maybe, normalForm, not, notElem, null, odd, or, ord, otherwise, PEVAL, print, putChar, putStr, putStrLn, read, readFile, readParen, reads, realToFrac, repeat, replicate, reverse, seq, sequence, sequence_, showChar, showParen, shows, showString, showTuple, snd, solve, span, splitAt, success, tail, take, takeWhile, uncurry, unknown, unlines, until, unwords, unzip, unzip3, userError, words, writeFile, zip, zip3, zipWith, zipWith3, ||
Exported Classes: Alternative, Applicative, Bounded, Data, Enum, Eq, Floating, Fractional, Functor, Integral, Monad, MonadFail, Monoid, Num, Ord, Read, Real, RealFrac, Show
data Char
The externally defined type of characters.
Known instances:
data Int
The externally defined type of integers.
Known instances:
data Float
The externally defined type of float point numbers.
Known instances:
Data Float
Eq Float
Ord Float
Show Float
Read Float
Num Float
Fractional Float
Real Float
RealFrac Float
Floating Float
data Bool
The type of Boolean values.
Constructors:
False
:: Bool
True
:: Bool
Known instances:
data Ordering
Ordering type. Useful as a result of comparison functions.
Constructors:
LT
:: Ordering
EQ
:: Ordering
GT
:: Ordering
Known instances:
data Maybe a
The Maybe
type can be used for values which could also be absent.
Constructors:
Nothing
:: Maybe a
Just
:: a -> Maybe a
Known instances:
Monoid a => Monoid (Maybe a)
Functor Maybe
Applicative Maybe
Alternative Maybe
Monad Maybe
MonadFail Maybe
Eq a => Eq (Maybe a)
Ord a => Ord (Maybe a)
Show a => Show (Maybe a)
Read a => Read (Maybe a)
data Either a b
The Either
type can be used to combine values of two different types.
Constructors:
Left
:: a -> Either a b
Right
:: b -> Either a b
Known instances:
Functor (Either a)
Applicative (Either a)
Monad (Either a)
(Eq a, Eq b) => Eq (Either a b)
(Ord a, Ord b) => Ord (Either a b)
(Show a, Show b) => Show (Either a b)
(Read a, Read b) => Read (Either a b)
class Data
a
The class Data
defines a strict equality operator ===
and a non-deterministic operation aValue
which yields
all values of the given type.
To ensure that the operator ===
always corresponds to the
syntactic equality of values and aValue
enumerates all values,
instances of Data
are automatically derived and cannot be
defined in a valid Curry program.
For data types contain functional values, Data
instances
are not derived.
Free variables have always a data context to ensure that possible
values for the type of the free variable can be enumerated.
Note that the class Data
is different from the class Eq
,
since the latter defines only an equivalence relation rather
than syntactic equality.
Methods:
|
|
(/==)
:: Data a => a -> a -> Bool
The negation of strict equality.
class Eq
a
The class Eq
defines an equality (==
) and an inequality (/=
) method.
Instances of this class should define an equivalence relationship
on values. For basic data types, the instances are defined as
syntactic equality of values.
Methods:
|
|
class Eq a => Ord
a
The class Ord
defines operations to compare values of the given type
with respect to a total ordering.
A minimal instance definition for some type must define <=
or compare
.
Methods:
|
|
|
|
|
|
class Show
a
The class Show
contains methods to transform values into
a string representation.
Methods:
type ShowS
= String -> String
The type synonym ShowS
represents strings as difference lists.
Composing functions of this type allows concatenation of lists
in constant time.
shows
:: Show a => a -> String -> String
Converts a showable value to a show function that prepends this value.
showChar
:: Char -> String -> String
Converts a character to a show function that prepends the character.
showString
:: String -> String -> String
Converts a string to a show function that prepends the string.
showParen
:: Bool -> (String -> String) -> String -> String
If the first argument is True
, Converts a show function to a
show function adding enclosing brackets, otherwise the show function
is returned unchanged.
showTuple
:: [String -> String] -> String -> String
Converts a list of show functions to a show function combining the given show functions to a tuple representation.
class Read
a
The class Read
contains method to parse strings to return values
corresponding to the textual representation as produced by show
.
Methods:
type ReadS a
= String -> [(a, String)]
The type synonym ReadS
represent a parser for values of type a.
Such a parser is a function that takes a String and
returns a list of possible parses as (a,String)
pairs.
Thus, if the result is the empty list, there is no parse, i.e.,
the input string is not valid.
reads
:: Read a => String -> [(a, String)]
A parser to read data from a string.
For instance, reads "42" :: [(Int,String)]
returns [(42,[])]
, and
reads "hello" :: [(Int,String)]
returns []
.
readParen
:: Bool -> (String -> [(a, String)]) -> String -> [(a, String)]
readParen True p
parses what p
parses, but surrounded with parentheses.
readParen False p
parses what p
parses, but the string to be parsed
can be optionally with parentheses.
Reads data of the given type from a string.
The operations fails if the data cannot be parsed.
For instance read "42" :: Int
evaluates to 42
,
and read "hello" :: Int
fails.
lex
:: String -> [(String, String)]
Reads a single lexeme from the given string.
Initial white space is discarded and the characters of the lexeme
are returned. If the input string contains only white space,
lex
returns the empty string as lexeme.
If there is no legal lexeme at the beginning of the input string,
the operation fails, i.e., []
is returned.
class Bounded
a
Instances of the class Bounded
are types with minmal and maximal values.
Methods:
|
|
class Enum
a
The class Enum
provides methods to enumerate values of the given
type in a sequential order.
If a type is an instance of Enum
, one can use the standard
notation for arithmetic sequences to enumerate list of values.
Methods:
|
|
|
|
|
|
class Num
a
The class of basic numeric values.
For type wich are instances of Num
, one can write values
as integers which are converted by an implicit fromInt
application.
Methods:
|
|
|
|
|
|
class Num a => Fractional
a
The class Fractional
defines numbers with a division operation.
Methods:
|
|
class (Num a, Ord a) => Real
a
The class of real numbers which can be mapped to floats.
Methods:
class (Real a, Enum a) => Integral
a
The class of Integral
numbers supports integer division operators.
Methods:
|
|
|
|
|
|
even
:: Integral a => a -> Bool
Returns whether an integer is even.
odd
:: Integral a => a -> Bool
Returns whether an integer is odd.
fromIntegral
:: (Integral a, Num b) => a -> b
General coercion from integral types.
realToFrac
:: (Real a, Fractional b) => a -> b
General coercion to fractional types.
(^)
:: (Num a, Integral b) => a -> b -> a
Raises a number to a non-negative integer power.
class (Real a, Fractional a) => RealFrac
a
Instances of the class RealFrac
supports extracting components
of Fractional
values.
Methods:
|
class Fractional a => Floating
a
The class Floating
defines Fractional
s with
trigonometric and hyperbolic and related functions.
Methods:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Monoid
a
The class Monoid
defines types with an associative
binary operation mappend
having an identity mempty
.
Methods:
|
|
|
class Functor
f
The class Functor
defines a general mapping of values contained
in structures.
A type constructor f
is a Functor if it provides a function fmap
which applies a function of type (a -> b)
to all values contained
in a structure of type f a
yielding a structure of type f b
.
Methods:
|
|
class Functor f => Applicative
f
The class Applicative
defines a functor structure
with application operators to apply functions and argument
contained in structure and combining their results back into a structure.
Methods:
|
|
|
|
|
class Applicative f => Alternative
f
A monoid on applicative functors.
If defined, some and many should be the least solutions of the equations:
Methods:
The identity of <|> |
An associative binary operation
|
One or more. |
Zero or more. |
class Applicative m => Monad
m
The class Monad
defines operators for the sequential composition
of computations.
For instances of Monad
, the standard do
notation can be used.
Methods:
|
|
|
class Monad m => MonadFail
m
The class MonadFail
adds a fail
operation to a monadic structure.
Methods:
(=<<)
:: Monad b => (a -> b c) -> b a -> b c
Same as >>=, but with the arguments interchanged.
ap
:: Monad a => a (b -> c) -> a b -> a c
Promotes function application to a monad. For instance,
> pure not `ap` Just True Just False
This is useful to promote application of functions with larger arities
to a monad, as liftM2
for arity 2
. For instance,
> pure (\x y z -> x + y * z) `ap` Just 7 `ap` Just 5 `ap` Just 7 Just 42
liftM2
:: Monad d => (a -> b -> c) -> d a -> d b -> d c
Promotes a binary function to a monad.
The function arguments are scanned from left to right.
For instance, liftM2 (+) [1,2] [3,4]
evaluates to [4,5,5,6]
, and
liftM2 (,) [1,2] [3,4]
evaluates to [(1,3),(1,4),(2,3),(2,4)]
.
sequence
:: Monad a => [a b] -> a [b]
Executes a sequence of monadic actions and collects all results in a list.
sequence_
:: Monad a => [a b] -> a ()
Executes a sequence of monadic actions and ignores the results.
mapM
:: Monad b => (a -> b c) -> [a] -> b [c]
Maps a monadic action function on a list of elements. The results of all monadic actions are collected in a list.
mapM_
:: Monad b => (a -> b c) -> [a] -> b ()
Maps a monadic action function on a list of elements. The results of all monadic actions are ignored.
Returns true if the argument is an uppercase letter.
Returns true if the argument is an lowercase letter.
Returns true if the argument is a letter.
Returns true if the argument is a decimal digit.
isAlphaNum
:: Char -> Bool
Returns true if the argument is a letter or digit.
isBinDigit
:: Char -> Bool
Returns true if the argument is a binary digit.
isOctDigit
:: Char -> Bool
Returns true if the argument is an octal digit.
isHexDigit
:: Char -> Bool
Returns true if the argument is a hexadecimal digit.
Returns true if the argument is a white space.
Converts a character into its ASCII value.
Converts a Unicode value into a character. The conversion is total, i.e., for out-of-bound values, the smallest or largest character is generated.
type String
= [Char]
The type String
is a type synonym for list of characters so that
all list operations can be used on strings.
Breaks a string into a list of lines where a line is terminated at a newline character. The resulting lines do not contain newline characters.
Concatenates a list of strings with terminating newlines.
Breaks a string into a list of words where the words are delimited by white spaces.
Concatenates a list of strings with a blank between two strings.
head
:: [a] -> a
Computes the first element of a list.
tail
:: [a] -> [a]
Computes the remaining elements of a list.
Is a list empty?
(++)
:: [a] -> [a] -> [a]
Concatenates two lists. Since it is flexible, it could be also used to split a list into two sublists etc.
Computes the length of a list.
List index (subscript) operator, head has index 0.
map
:: (a -> b) -> [a] -> [b]
Maps a function on all elements of a list.
foldl
:: (a -> b -> a) -> a -> [b] -> a
Accumulates all list elements by applying a binary operator from left to right.
foldl1
:: (a -> a -> a) -> [a] -> a
Accumulates a non-empty list from left to right.
foldr
:: (a -> b -> b) -> b -> [a] -> b
Accumulates all list elements by applying a binary operator from right to left.
foldr1
:: (a -> a -> a) -> [a] -> a
Accumulates a non-empty list from right to left:
filter
:: (a -> Bool) -> [a] -> [a]
Filters all elements satisfying a given predicate in a list.
zip
:: [a] -> [b] -> [(a, b)]
Joins two lists into one list of pairs. If one input list is shorter than the other, the additional elements of the longer list are discarded.
zip3
:: [a] -> [b] -> [c] -> [(a, b, c)]
Joins three lists into one list of triples. If one input list is shorter than the other, the additional elements of the longer lists are discarded.
zipWith
:: (a -> b -> c) -> [a] -> [b] -> [c]
Joins two lists into one list by applying a combination function to
corresponding pairs of elements. Thus zip = zipWith (,)
zipWith3
:: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
Joins three lists into one list by applying a combination function to
corresponding triples of elements. Thus zip3 = zipWith3 (,,)
unzip
:: [(a, b)] -> ([a], [b])
Transforms a list of pairs into a pair of lists.
unzip3
:: [(a, b, c)] -> ([a], [b], [c])
Transforms a list of triples into a triple of lists.
concat
:: [[a]] -> [a]
Concatenates a list of lists into one list.
concatMap
:: (a -> [b]) -> [a] -> [b]
Maps a function from elements to lists and merges the result into one list.
iterate
:: (a -> a) -> a -> [a]
Infinite list of repeated applications of a function f to an element x.
Thus, iterate f x = [x, f x, f (f x), ...]
.
repeat
:: a -> [a]
Infinite list where all elements have the same value.
Thus, repeat x = [x, x, x, ...]
.
List of length n where all elements have the same value.
Returns prefix of length n.
Returns suffix without first n elements.
splitAt
:: Int -> [a] -> ([a], [a])
splitAt n xs
is equivalent to (take n xs, drop n xs)
takeWhile
:: (a -> Bool) -> [a] -> [a]
Returns longest prefix with elements satisfying a predicate.
dropWhile
:: (a -> Bool) -> [a] -> [a]
Returns suffix without takeWhile prefix.
span
:: (a -> Bool) -> [a] -> ([a], [a])
span p xs
is equivalent to (takeWhile p xs, dropWhile p xs)
break
:: (a -> Bool) -> [a] -> ([a], [a])
break p xs
is equivalent to
(takeWhile (not . p) xs, dropWhile (not . p) xs)
.
Thus, it breaks a list at the first occurrence of an element satisfying p.
reverse
:: [a] -> [a]
Reverses the order of all elements in a list.
Computes the conjunction of a Boolean list.
Computes the disjunction of a Boolean list.
any
:: (a -> Bool) -> [a] -> Bool
Is there an element in a list satisfying a given predicate?
all
:: (a -> Bool) -> [a] -> Bool
Is a given predicate satisfied by all elements in a list?
elem
:: Eq a => a -> [a] -> Bool
Element of a list?
notElem
:: Eq a => a -> [a] -> Bool
Not element of a list?
lookup
:: Eq a => a -> [(a, b)] -> Maybe b
Looks up a key in an association list.
(<$>)
:: Functor c => (a -> b) -> c a -> c b
Apply a function of type (a -> b)
, given as the left argument,
to a value of type f a
, where f
is a functor,
to get a value of type f b
.
Basically, this is an infix operator version of fmap
.
($)
:: (a -> b) -> a -> b
Right-associative application.
($!)
:: (a -> b) -> a -> b
Right-associative application with strict evaluation of its argument to head normal form.
($!!)
:: (a -> b) -> a -> b
Right-associative application with strict evaluation of its argument to normal form.
($#)
:: (a -> b) -> a -> b
Right-associative application with strict evaluation of its argument to a non-variable term.
($##)
:: (a -> b) -> a -> b
Right-associative application with strict evaluation of its argument to ground normal form.
seq
:: a -> b -> b
Evaluates the first argument to head normal form (which could also be a free variable) and returns the second argument.
ensureNotFree
:: a -> a
Evaluates the argument to head normal form and returns it. Suspends until the result is bound to a non-variable term.
ensureSpine
:: [a] -> [a]
Evaluates the argument to spine form and returns it. Suspends until the result is bound to a non-variable spine.
normalForm
:: a -> a
Evaluates the argument to normal form and returns it.
groundNormalForm
:: a -> a
Evaluates the argument to ground normal form and returns it. Suspends as long as the normal form of the argument is not ground.
(.)
:: (a -> b) -> (c -> a) -> c -> b
Function composition.
id
:: a -> a
Identity function.
const
:: a -> b -> a
Constant function.
asTypeOf
:: a -> a -> a
asTypeOf
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 second.
curry
:: ((a, b) -> c) -> a -> b -> c
Converts an uncurried function to a curried function.
uncurry
:: (a -> b -> c) -> (a, b) -> c
Converts an curried function to a function on pairs.
flip
:: (a -> b -> c) -> b -> a -> c
flip f
is identical to f
, but with the order of arguments reversed.
until
:: (a -> Bool) -> (a -> a) -> a -> a
Repeats application of a function until a predicate holds.
Sequential conjunction on Booleans.
Sequential disjunction on Booleans.
Negation on Booleans.
Useful name for the last condition in a sequence of conditional equations.
ifThenElse
:: Bool -> a -> a -> a
The standard conditional. It suspends if the condition is a free variable.
maybe
:: a -> (b -> a) -> Maybe b -> a
The maybe
function takes a default value, a function, and a Maybe
value.
If the Maybe
value is Nothing
, the default value is returned.
Otherwise, the function is applied to the value inside the Just
and the result is returned.
either
:: (a -> b) -> (c -> b) -> Either a c -> b
Apply a case analysis to a value of the Either type.
If the value is Left x
, the first function is applied to x
.
If the value is Right y
, the second function is applied to y
.
fst
:: (a, b) -> a
Selects the first component of a pair.
snd
:: (a, b) -> b
Selects the second component of a pair.
failed
:: a
A non-reducible polymorphic function. It is useful to express a failure in a search branch of the execution.
Aborts the execution with an error message.
data IO _
The externally defined type of IO actions.
Known instances:
An action that reads a character from standard output and returns it.
An action that reads a line from standard input and returns it.
An action that puts its character argument on standard output.
Action to print a string on standard output.
Action to print a string with a newline on standard output.
Converts a term into a string and prints it.
type FilePath
= String
The FilePath
is j type synonym for strings.
It is useful to mark in type signatures if a file path is required.
readFile
:: String -> IO String
An action that (lazily) reads a file and returns its contents.
writeFile
:: String -> String -> IO ()
An action that writes a file.
appendFile
:: String -> String -> IO ()
An action that appends a string to a file.
It behaves like writeFile
if the file does not exist.
data IOError
The (abstract) type of error values. Currently, it distinguishes between general I/O errors, user-generated errors (see userError), failures and non-determinism errors during I/O computations. These errors can be caught by catch. Each error contains a string shortly explaining the error. This type might be extended in the future to distinguish further error situations.
Constructors:
IOError
:: String -> IOError
UserError
:: String -> IOError
FailError
:: String -> IOError
NondetError
:: String -> IOError
Known instances:
userError
:: String -> IOError
A user error value is created by providing a description of the error situation as a string.
Raises an I/O exception with a given error value.
catch
:: IO a -> (IOError -> IO a) -> IO a
Catches a possible error or failure during the execution of an
I/O action. catch act errfun
executes the I/O action act
.
If an exception or failure occurs during this I/O action, the
function errfun
is applied to the error value.
type Success
= Bool
The type synonym for constraints. It is included for backward compatibility and should be no longer used.
The always satisfiable constraint. It is included for backward compatibility and should be no longer used.
Enforce a Boolean condition to be true.
The computation fails if the argument evaluates to False
.
Solves a constraint as an I/O action. Note: The constraint should be always solvable in a deterministic way.
(=:=)
:: Data a => a -> a -> Bool
The equational constraint.
(e1 =:= e2)
is satisfiable if both sides e1
and e2
can be
reduced to a unifiable data term (i.e., a term without defined
function symbols).
(=:<=)
:: Data a => a -> a -> Bool
Non-strict equational constraint.
This operation is not intended to be used in source programs
but it is used to implement
functional patterns.
Conceptually, (e1 =:<= e2)
is satisfiable if e1
can be evaluated
to some pattern (data term) that matches e2
, i.e., e2
is
an instance of this pattern.
The Data
context is required since the resulting pattern might be
non-linear so that it abbreviates some further equational constraints,
see Section 7.
Internal operation to implement equational constraints. It is used by the strict equality optimizer but should not be used in regular programs.
(=:<<=)
:: Data a => a -> a -> Bool
Non-strict equational constraint for linear functional patterns. Thus, it must be ensured that the first argument is always (after evalutation by narrowing) a linear pattern. Experimental and only supported in PAKCS.
Concurrent conjunction.
An expression like (c1 & c2)
is evaluated by evaluating
the c1
and c2
in a concurrent manner.
Conditional expression.
An expression like (c &> e)
is evaluated by evaluating the first
argument to True
and then evaluating e
.
The expression has no value if the condition does not evaluate to True
.
(?)
:: a -> a -> a
Non-deterministic choice par excellence.
The value of x ? y
is either x
or y
.
anyOf
:: [a] -> a
Returns non-deterministically any element of a list.
Evaluates to a fresh free variable.
apply
:: (a -> b) -> a -> b
eqString
:: String -> String -> Bool
type DET a
= a
Identity type synonym used to mark deterministic operations. Used by the Curry preprocessor.
PEVAL
:: a -> a
Identity function used by the partial evaluator to mark expressions to be partially evaluated.