Copyright © 2011 - 2015, Ingo Wechsung
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This package provides basic definitions for the Frege language.
The Prelude packages are imported explicitly or implicitly during compilation of any other package. They define basic data structures, classes and functions.
The types and constructors for lists, unit type and tuple types are not defined here: They are provided programmatically by the compiler when it compiles a package with the name frege.prelude.PreludeBase. Nevertheless, they are considered part of the Prelude, thus qualified names like (,) are okay.
The packages are implementation specific insofar as the compiler may assume that certain items are defined here in a certain way. Changes may thus lead to compiler crashes or java code that will be rejected by the java compiler.
Unit type
Unit value
2-tuple
2-tuple constructor
3-tuple
3-tuple constructor
4-tuple
4-tuple constructor
5-tuple
5-tuple constructor
6-tuple
6-tuple constructor
7-tuple
7-tuple constructor
8-tuple
8-tuple constructor
9-tuple
9-tuple constructor
10-tuple
10-tuple constructor
11-tuple
11-tuple constructor
12-tuple
12-tuple constructor
13-tuple
13-tuple constructor
14-tuple
14-tuple constructor
15-tuple
15-tuple constructor
16-tuple
16-tuple constructor
17-tuple
17-tuple constructor
18-tuple
18-tuple constructor
19-tuple
19-tuple constructor
20-tuple
20-tuple constructor
21-tuple
21-tuple constructor
22-tuple
22-tuple constructor
23-tuple
23-tuple constructor
24-tuple
24-tuple constructor
25-tuple
25-tuple constructor
26-tuple
26-tuple constructor
function
list type
list construction
empty list
The type class Eq provides operators Eq.==, Eq.!= and Eq.hashCode.
All types whose values can be compared for equality should be instances of this class. For algebraic data types instances can be automatically derived if all components are themselves instances of Eq.
Integer, Long, Char, Bool, (), Float, Double, Int, [], StringJ
Check for inequality. The default implementation obeys the laws
!(a != a)
(a != b) == !(a == b)
(a == b) != (a != b)
These laws shall also be obeyed in all implementations.
Check for equality. This function is required in all instances.
The basic law of philosophy
a == a
shall be obeyed by all implementations.
Compute a hash code.
The following rules shall hold in all instances:
a == b ==> hashCode a == hashCode b
hashCode a != hashCode b ==> a != b
In addition, unequal values should produce unequal hash codes more likely than not.
The hash code in derived instances for algebraic data types is computed as follows:
hashCode v = case v of Con_1 f_1 ... f_k -> fold mkhash 1 [1, hashCode f_1, ..., hashCode f_k ] ... Con_n f_1 ... f_k -> fold mkhash 1 [n, hashCode f_1, ..., hashCode f_k ] where mkhash a b = (31 * a) + b
Here, the Con_i with i>0 are the constructors, and in each clause the f_j with j >= 0 are bound to the fields of the constructor under consideration.
Determines the constructor of a value. This is used like
constructor arg
where arg is any frege value.
Returns 0 for function types, product types and native types or the constructor number for constructed types. The constructor number is a small integer associated with every constructed value. It indicates the data constructor a value was constructed with.
The compiler assigns constructor numbers starting from 0 to the constructors defined in a data definition in the order of their appearance.
Examples
constructor [] == 0 constructor (a:as) == 1 constructor "string" == 0 // native value
This function is strict in its argument, i.e.
constructor undefined == undefined
Implementation specific: This function is used in derived instances of Eq and Ord.
Bool values are based on Java's primitive boolean values. Note that true and false are literals, not constructors.
For compatibility with Haskell, the HaskellBool type defines constructors HaskellBool.True and HaskellBool.False.
When the identifiers True or False are used in patterns or expressions, qualified or unqualified, and name resolution detects that they resolve to HaskellBool.True or HaskellBool.False they will be replaced by literals true or false.
This is, of course, a hack, but I see no other way to bridge the gap between redefinable constructor ids and literals. For example, we couldn't simply define True as another literal keyword, because some Haskell code might use Prelude.True or could have code like.
import Prelude hiding(True, False) data TriBool = False | Perhaps | True
will be replaced by literal false
will be replaced by literal true
This is a constant with the value true. It is most often used as the last alternative in pattern guards:
foo n | n >= 0 = ... | otherwise = ...
warning: will soon be replaced with bool This is used by code generation when a conditional expression appears in a lazy context, i.e.
(42, if foo then bar else baz)
lazyif a b c evaluates to b if a is true, otherwise to c.
This is used by code generation when a conditional expression appears in a lazy context, i.e.
(42, if foo then bar else baz)
bool a b c evaluates to a if c is true, otherwise to b.
Int values are based on Java's primitive int values.
The existence of this type is assumed in numerous places in the compiler.
Like with all native Java types, be they primitive or reference types, Frege holds the raw int in boxed form. However, in certain cases the compiler will optimize the boxing away:
sum a b c = a + b + c
can compute the sum of 3 Ints, Longs, Doubles or any other values of a type that is an instance of type class Num, but it may be somewhat slower than functions specialized for a given type.
According to the Java Language Specification, int values are 32 bit wide signed two's complement integers (§4.2). Java operations on int do not indicate overflow or underflow in any way (§4.2.2). Instead, just the low 32 bits of every result are retained.
Computes binary /and/ of two integers. Uses the java &-operator.
Computes binary /exclusive or/ of two integers. Uses the java ^-operator.
Computes binary /or/ of two integers. Uses the java |-operator.
i.char returns the Char value whose ordinal number is i
Result is only valid for integers in the range 0..65535
convert an Int to a Float, i.e. 2.float == 2.0f.
For large integers, the result may have been be rounded.
Returns the value obtained by rotating the two's complement binary representation of the specified int value left
by the specified number of bits.
Returns the value obtained by rotating the two's complement binary representation of the specified int value
right by the specified number of bits.
The number of bits used to represent an int value in two's complement binary form
convert to a hexadecimal string
unsigned right shift
Integer is a type for integer numbers of unlimited size, It has instances for Eq, Ord, Show and Integral.
This is derived from java.math.BigInteger.
Returns the number of bits in the minimal two's-complement representation of this Integer, excluding a sign bit.
Warning! Throws ArithmeticException when divisor is negative.
unsigned right shift on big integers does not really make sense ...
construction from a Long, see also StringJ.aton and StringJ.integer
Long values are based on Java's primitive long values.
According to the Java Language Specification, long values are 64 bit wide signed two's complement integers (§4.2). Java operations on long do not indicate overflow or underflow in any way (§4.2.2). Instead, just the low 64 bits of every result are retained.
Computes binary and of two integers. Uses the java &-operator.
Computes binary exclusive or of two long integers. Uses the java ^-operator.
Computes binary or of two long integers. Uses the java |-operator.
Convert an Long to a Double, i.e. 42L.double == 42.0.
For large numbers, the result may have been be rounded.
Convert an Long to a Float, i.e. 42L.float == 42.0f.
For large numbers, the result may have been be rounded.
Uses a java cast to convert a Long to an Int. This is a narrowing primitive conversion in java parlance.
Returns the value obtained by rotating the two's complement binary representation
of the specified long value left by the specified number of bits.
Returns the value obtained by rotating the two's complement binary representation
of the specified long value right by the specified number of bits.
unsigned right shift
Char values are based on Java's primitive char values.
This type has many native functions based on the methods in java.lang.Character.
Most is... functions work on Char and Int (codepoint).
Likewise, most to...Case functions work on codepoints.
Returns the numeric value of the character argument in the specified radix.
If the character is not a digit in the given radix, -1 is returned.
The radix must be in the range 2..36, otherwise also -1 is returned.
Char.forDigit d r
returns the character for the digit d in radix r
d must not be negative and lower than r, and r must be in the range 2..36
When the arguments are invalid the character '\u0000' is returned.
Return the Unicode name of the code point or null if it is unassigned.
Return the Unicode name of the code point or null if it is unassigned.
Return the Unicode name of the code point or null if it is unassigned.
Returns a value indicating a character's general category.
Returns a value indicating a character's general category.
Returns a value indicating a character's general category.
The leading surrogate (which is a high surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding.
If the specified value is not a supplementary character, an unspecified character is returned.
See also: Char.isSupplementaryCodePoint, Char.lowSurrogate, Char.toCodePoint
The following holds:
isSupplementaryCodePoint x ==> (x == toCodePoint (highSurrogate x) (lowSurrogate x))
Determines if a character (Unicode code point) is defined in Unicode.
A character is defined if at least one of the following is true:
Determines if a character (Unicode code point) is defined in Unicode.
A character is defined if at least one of the following is true:
Determines if a character (Unicode code point) is defined in Unicode.
A character is defined if at least one of the following is true:
Determines if the given char value is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).
Such values do not represent characters by themselves, but are used in the representation of supplementary characters in the UTF-16 encoding.
Determines if the specified character is an ISO control character. A character is considered to be an ISO control character if its code is in the range "\u0000" through "\u001F" or in the range "\u007F" through "\u009F".
Determines if the specified character is an ISO control character. A character is considered to be an ISO control character if its code is in the range "\u0000" through "\u001F" or in the range "\u007F" through "\u009F".
Determines if the specified character is an ISO control character. A character is considered to be an ISO control character if its code is in the range "\u0000" through "\u001F" or in the range "\u007F" through "\u009F".
Determines if the given char value is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit).
Such values do not represent characters by themselves, but are used in the representation of supplementary characters in the UTF-16 encoding.
Determines whether the specified character (Unicode code point) is in the supplementary character range.
This is roughly equivalent to
(> ord Char.maxBound)
except that it also checks if the value is lower than 0x110000
If, for some integer n
not (Char.isSupplementaryCodePoint n) && (n > ord Char.maxBound)
then n is not a codepoint at all.
Determines if the given char value is a Unicode surrogate code unit.
Such values do not represent characters by themselves, but are used in the representation of supplementary characters in the UTF-16 encoding.
A char value is a surrogate code unit if and only if it is either a low-surrogate code unit or a high-surrogate code unit.
Determines whether the specified pair of char values is a valid Unicode surrogate pair.
Determines whether the specified code point is a valid Unicode code point value.
This is the case if it is not negative and lower than 0x110000
The trailing surrogate (which is a low surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding.
If the specified value is not a supplementary character, an unspecified character is returned.
See also: Char.isSupplementaryCodePoint, Char.highSurrogate, Char.toCodePoint
The following holds:
isSupplementaryCodePoint x ==> (x == toCodePoint (highSurrogate x) (lowSurrogate x))
Char.toCodePoint high low
Converts the specified surrogate pair to its supplementary code point value. This function does not validate the specified surrogate pair. The caller must validate it using Char.isSurrogatePair if necessary.
The lowercase equivalent of the character, if any; otherwise, the character itself.
The lowercase equivalent of the character, if any; otherwise, the character itself.
The lowercase equivalent of the character, if any; otherwise, the character itself.
The titlecase equivalent of the character, if any; otherwise, the character itself.
The titlecase equivalent of the character, if any; otherwise, the character itself.
The titlecase equivalent of the character, if any; otherwise, the character itself.
The uppercase equivalent of the character, if any; otherwise, the character itself.
The uppercase equivalent of the character, if any; otherwise, the character itself.
The uppercase equivalent of the character, if any; otherwise, the character itself.
String values are based on Java's java.lang.String objects. String is an alias for StringJ Char
For technical reasons, the native string type is defined with a phantom type, which allows us to treat strings like character lists.
The following rules apply:
Concatenate two strings, uses Java's + operator
Like StringJ.double, but the exception is not checked, thus only good when one knows for sure that the parse will succeed.
Like StringJ.float, but the exception is not checked, thus only good when one knows for sure that the parse will succeed.
Like StringJ.int, but the exception is not checked, thus only good when one knows for sure that the parse will succeed.
Like StringJ.long, but the exception is not checked, thus only good when one knows for sure that the parse will succeed.
Like StringJ.integer, but the exception is not checked, thus only good when one knows for sure that the parse will succeed.
retrieve character at index
StringJ.compareTo is used in the Ord instance of String
This is the native String.concat, just renamed because naming conflicts
Safe way to parse a Double value from a string. See StringJ.int
Safe way to parse a Float value from a string. See StringJ.int
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
Format 1 to 9 values
May throw IllegalFormatException, if the type of any argument does not fit the format specifier.
str.indexOf ch
the first index of ch in str, or -1 if not present
str.indexOf ch
the first index of ch in str, or -1 if not present
str.indexOf ch
the first index of ch in str, or -1 if not present
str.indexOf ch
the first index of ch in str, or -1 if not present
str.indexOf ch
the first index of ch in str, or -1 if not present
Safe way to parse an integer from a string. java.lang.NumberFormatException will be caught and returned as Either.Left value. When the parse succeeds, the integer is returned in the Either.Right value.
Use like this:
case s.int of Left exc -> ... -- s is not well formed Right i -> ... -- the parsed value is in i
Safe way to parse a big Integer value from a string. See StringJ.int
The length of a String
Safe way to parse a long integer from a string. See StringJ.int
notify a single thread waiting on the objects monitor
notify all threads waiting on the objects monitor
Retrieve element of String at index
Because it is impossible to create a String from anything but Chars, the type is not even wrong.
Will be needed in the String instance of ListLike that expects a type with kind ->.
quote regular expression metacharacters in string
quote replacement string metacharacters in string
replace a character in a string
true if the second string is a prefix of the first one
see substr
convert to lower case
convert to upper case
wait on Objects monitor
substr s start end returns the sub string of s that starts with the character at position start and extends to the character at position end-1.
This uses the native method substring of class java.lang.String. It will throw an IndexOutOfBoundsException if start is negative or larger than end or if end is greater than the length of s.
strtail s n returns a new string that is a substring of string s. The substring begins with the character at the specified index and extends to the end of s.
This uses the native method substring of class java.lang.String. It will throw an IndexOutOfBoundsException if n is negative or larger than the length of s.
Float values are based on Java's primitive float values.
According to the Java Language Specification §4.2.3, float values are 32-bit-precision binary floating point values. The values and the operations on it behave as specified in the IEEE Standard for Binary Floating-Point Arithmetic.
Applies the java widening primitive conversion from float to double.
Returns the closest Int value to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int.
The following property holds:
(f < Int.maxBound.float && f > Int.minBound.float) ==> (f.int.float == (f + 0.5f).floor)
Special cases:
Double values are Java's primitive double values.
According to the Java Language Specification §4.2.3, double values are 64-bit-precision binary floating point values. The values and the operations on it behave as specified in the IEEE Standard for Binary Floating-Point Arithmetic.
Applies the java narrowing primitive conversion from double to float
Returns the closest Long value to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long.
The following property holds:
(d < Long.maxBound.double && d > Long.minBound.double) ==> (d.long.double == (d + 0.5d).floor)
Special cases:
bit representation of a double
The Java Object type
for any value, we can get its Java class
notify a single thread waiting on the objects monitor
notify all threads waiting on the objects monitor
wait on Objects monitor
warning: probably catches more exceptions than you want to handle, use (Exception1|Exception2|Result) This is the principal return type for java methods that are expected to throw exceptions.
It is strongly recommended to use more specific exception types.
We need to do some reflection from frege code. For example, when we catch a Throwable thrown from Java code. we might want to know what it is.
Frege wrapper for java Throwables.
give the class name of this exception
Used to construct a strict tuple. Don't use anymore!
Constructs a strict 3-tuple. See remarks for strictTuple2.
Constructs a strict 4-tuple. See remarks for strictTuple2.
Constructs a strict 5-tuple. See remarks for strictTuple2.
Constructs a strict 6-tuple. See remarks for strictTuple2.
Constructs a strict 7-tuple. See remarks for strictTuple2.
Constructs a strict 8-tuple. See remarks for strictTuple2.
Constructs a strict 9-tuple. See remarks for strictTuple2.
Constructs a strict 10-tuple. See remarks for strictTuple2.
Constructs a strict 11-tuple. See remarks for strictTuple2.
Constructs a strict 12-tuple. See remarks for strictTuple2.
Constructs a strict 13-tuple. See remarks for strictTuple2.
Constructs a strict 14-tuple. See remarks for strictTuple2.
Constructs a strict 15-tuple. See remarks for strictTuple2.
Constructs a strict 16-tuple. See remarks for strictTuple2.
Constructs a strict 17-tuple. See remarks for strictTuple2.
Constructs a strict 18-tuple. See remarks for strictTuple2.
Constructs a strict 19-tuple. See remarks for strictTuple2.
Constructs a strict 20-tuple. See remarks for strictTuple2.
Constructs a strict 21-tuple. See remarks for strictTuple2.
Constructs a strict 22-tuple. See remarks for strictTuple2.
Constructs a strict 23-tuple. See remarks for strictTuple2.
Constructs a strict 24-tuple. See remarks for strictTuple2.
Constructs a strict 25-tuple. See remarks for strictTuple2.
Constructs a strict 26-tuple. See remarks for strictTuple2.
a `seq` b evaluates a before it returns b
This is a right associative operator with precedence 15.
An unchecked subclass of java.lang.RuntimeException for the Frege Runtime
An instance of Undefined will be thrown if undefined or (error msg) is evaluated.
Throw this Undefined, this will abort the computation evaluating it.
Actually, the return type is not correct, since it never returns.
create an Undefined value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
create an Undefined value from a cause (Throwable). The message will be taken from the cause.
create an Undefined value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
create an Undefined value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
A subclass of Undefined, used by the runtime to signal failed pattern matches
A subclass of Undefined, used by the runtime to signal failed guards
java.lang.NumberFormatException needed for string conversion operations
declared protected to avoid name conflicts with NumberFormatException
create an NumberFormatException value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
create an NumberFormatException value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
create an NumberFormatException value with a message (String) or with a message and a cause. The cause is another exception that caused this one.
Forward declaration of ClassNotFoundException
This is the standard undefined value.
nowarn: application of error will diverge
Construct an undefined value with an informative message.
When evaluated, an Undefined exception will be thrown.
nowarn: diverge
Constructs an undefined value from a java exception and throws it.
The Java ! operator on booleans
complement
The Java && operator on booleans. Note that since this is a native function, the second argument appears strict to the compiler when in fact it is lazy at the Java level. This can lead to inconsistent results in some cases. For example, the following program correctly prints false in the first line of the output, but then aborts:
main _ = do stdout << (false && undefined) << "\n" stdout << conj false undefined << "\n" where conj a b = a && b
Note that the very same behaviour is seen in the following java program
public class And { static boolean undef() { if (true) throw new Error("undefined"); return false; } static boolean conj(boolean a, boolean b) { return a&&b; } public static void main(String[] args) { System.out.println(false && undef()); System.out.println(conj(false, undef())); } }
One could thus say that && behaves exactly like the Java operator including the fact that it cannot be replaced by a function without changing the semantics of a program.
For an alternative see und
The Java || operator on booleans.
Note that since this is a native function, the second argument appears strict to the compiler when in fact it is lazy at the Java level.
This can lead to inconsistent results in cases where the wrong strictness of the second argument propagates to arguments of the function that contains an application of ||. (The documentation for && has an example.)
See oder for an alternative
Like &&, but second argument is lazy. The `und` operator has the same precedence and arity as &&. The definition is
a `und` b = if a then b else false
This should really be named and, but Haskell 2010 uses this already for lists. Hence we use the german word und.
Like ||, but second argument is lazy. The `oder` operator has the same precedence and arity as ||. The definition is
a `oder` b = if a then true else b
This should really be named or, but Haskell 2010 uses this already for lists. Hence we use the german word oder.
not b is true if b is false, otherwise true. Uses java's ! operator.
a ^^ b is true if either a or b is true, but not both.
a ^^ b = if a then not b else b
This checks for object identity or equality of primitive values using Java's == operator. It evaluates its arguments, so undefined values cannot be compared.
This checks for object identity or inequality of primitive values using Java's != operator. It evaluates its arguments, so undefined values cannot be compared.
Ordering encodes the results of comparisons, see also Ord.<=>
The Ord class provides relational operators as well as the functions Ord.max and Ord.min. The default implementation defines them all in terms of the compare operator Ord.<=>.
Making some type an instance of Ord makes it automatically an instance of Eq if it is not one already and if it has an implementation for Eq.hashCode. The operators Eq.== and Eq.!= will be defined in terms of Ord.<=>.
Instances of Ord can be derived automatically for algebraic data types when all elements of the type are themselves instances of Ord and when the type is an instance of Eq.
Integer, Char, Bool, Float, Double, Int, StringJ, Long
This implementation for the (Eq.!=) operator is being used in instances of Ord when the instantiated type is not already an instance of Eq.
Relational < operator. Obeys the following laws:
if a < b && b < c then a < c a < b == b > a
Relational <= operator. Obeys the following laws:
if a <= b && b <= c then a <= c a <= b == b >= a a <= b == !(a > b)
This operator must be defined in all instances. It compares its operands and returns Ordering.Lt if the first is lower than the second, Ordering.Gt if the first is greater than the second and Ordering.Eq otherwise.
The following shall be invariantly true:
case a <=> b of { Eq -> a == b; _ -> a != b }
This implementation for the (Eq.==) operator is being used in instances of Ord when the instantiated type is not already an instance of Eq.
Relational > operator. Obeys the following laws:
if a > b && b > c then a > c a > b == b < a
Relational >= operator. Obeys the following laws:
if a >= b && b >= c then a >= c a >= b == b <= a a >= b == !(a < b)
max a b = if a > b then a else b
min a b = if a < b then a else b
Class Enum defines operations on sequentially ordered types.
A type that is an instance of Enum is also an instance of Ord (and, in turn, of Eq).
Instances of Enum may be derived for any enumeration type (types whose constructors have no fields). If there is no Eq.hashCode provided, it will be the same as Enum.ord.
Enumeration types have special syntactic support with the range list:
[a .. b] [a ..] [a,b .. c] [a,b ..]
Int, Bool, Char, Integer, Long
This is the default implementation of the compare operator, that makes each Enum type an Ord type automatically.
a <=> b = (ord a).<=> (ord b)
used in translating @[n..]
used in translating @[n,n'..]
used in translating @[n,n'..m]
used in translating @[n..m]
T.from i maps the Int value i to a value of T, such that
ord (T.from i) == i
unless there is no value e of T so that ord e == i. In the latter case, the result is undefined.
default implementation for Eq.hashCode is same as Enum.ord
ord e returns the ordinal number associated with the value e. For enumeration types, Enum.ord is the same as constructor, for Int, it is the identity function. Some types, like Long, cannot map all their values to Int, in such cases the result of applying Enum.ord may be meaningless.
pred e is the predecessor of e or undefined if there is no predecessor.
succ e is the successor of e or undefined if there is no such successor.
The Num class provides the operators (Num.+), (Num.-) and (Num.*) as well as some functions that are common for all numeric types.
Integer, Float, Double, Int, Long
Computes the product of two numbers
Computes the sum of two numbers
Computes the difference of two numbers
Computes the absolute value
converts an Int value to the instantiated type
converts an Integer value to the instantiated type
Floating point number types may have special values for infinity and negative infinity. isFinite n yields true if n is an infinite value and false in all other cases.
The default implementation always returns false so that implementors of instances for types without special values for infinity need not care.
See also Num.isNumber.
Floating point number types may have a special values for not a number (NaN). For example, 0d / 0d is NaN. isNaN n yields true if n is the special value that indicates that n is not a number and false in all other cases.
The default implementation always returns false so that implementors of instances for types without such a special values need not care.
See also Num.isNumber.
Returns true if n is neither infinite (see Num.isInfinite) nor NaN (see Num.isNaN).
Note that certain properties for functions on numbers are true only under the assumption that the argument values are numbers.
The default implementation is
isNumber n = !(isInfinite n) && !(isNaN n)
so that the function should always compute the right answer as long as Num.isInfinite and Num.isNaN do.
Negates a number n such that if n is a number
n + negate n == 0
the number 1 in the instantiated type
sign n is -1 if n<0, 1 if n>0 and 0 otherwise
use (subtract a) instead of \\b -> b-a in sections
the number 0 in the instantiated type
The Real class provides the division operator (Real./).
Class Integral provides division and remainder operations for integral numbers.
x ^ n raise x to the n-th power
The exponent must be positive.
every integral number can be converted to a big Integer
integer division
Integral.gcd x y is the non-negative factor of both x and y of which every common factor of x and y is also a factor; for example Integral.gcd 4 2 = 2, Integral.gcd (-4) 6 = 2, Integral.gcd 0 4 = 4. Integral.gcd 0 0 = 0. (That is, the common divisor that is \"greatest\" in the divisibility preordering.)
Note: Since for signed fixed-width integer types, Num.abs Bounded.minBound < 0, the result may be negative if one of the arguments is Bounded.minBound (and necessarily is if the other is 0 or Bounded.minBound) for such types.
worker function for Integral.gcd, needs not pollute the namespace
Integral.lcm x y is the smallest positive integer that both x and y divide.
This modulo operator works so that
forAll arbitrary (\a -> forAll arbitrary (\b -> (a `div` b) * b + (a `mod` b) = a))
In addition, it is the case that
forAll arbitrary (\a -> forAll arbitrary (\b -> @(a `quot` b) == (a `div` b) || (a `quot` b) == (a `div` b)-1))
helper for Integral.^
helper for Integral.^
Haskell compatibility
The remainder á la Java operator % - a `rem` b has same sign as a
forAll arbitrary (\a -> forAll arbitrary (\b -> (a `quot` b) * b + (a `rem` b) = a
This behaviour is the same as in Haskell
A class for data types that have a lower and an upper bound.
Instances of Bounded can be derived automatically for enumeration types.
the upper bound
the lower bound
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Maybe.Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Maybe.Just and returns the result.
apply first function to value in Either.Left or second function to value in Either.Right
return the first element of a 2-tuple
return the second element of a 2-tuple
A head strict variant of (:)
This will be used in list comprehensions
a $ b is the same as a b, but because of $'s low precedence one can write f $ x+y instead of f (x+y). Also, because $ is right associative, f $ g $ h y is f (g (h y))
Same as `$` but argument is strict
const a is a function that returns a regardless of its argument.
asTypeOf a b is a with the type of b.
This is a type restricted version of const.
Exchange first and second argument of a function, i.e.
flip f a b = f b a
Passes the elements of a 2-tuple as arguments to a function.
curry f passes the next two arguments as 2-tuple to f
In patterns, the @-operator is used to bind a name to a complex pattern
f (x@a:as) = e
is the same as
f arg = case arg of { x -> case x of { a:as -> e }}
using f applies a projection function f on both sides of Eq.==. Example usage:
uniqBy (using fst) [(1, 1), (2, 2), (2, 3), (3,4), (2,5)]
comparing f applies a projection function on both sides of Ord.<=>. Example usage:
sortBy (comparing snd) [(1, "z"), (2, "b")] == [(2, "b"), (1, "z")]
Alias for comparing
descending f applies a projection function on both sides of Ord.<=>, but flips arguments. Example usage:
sortBy (descending fst) [(1, "z"), (2, "b")] == [(2, "b"), (1, "z")]
g `on` f
Apply a projection function f on both operands of g
comparing f = (<=>) `on` f
(ST s a) is an abstract data type and is a computation that encapsulates side effects in state thread s and returns a value of type a.
The type s can be understood as a compiler generated unique index for state threads. Every state thread is independent of each other and keeps track of mutable variables created in it. For detailed information, read the paper Lazy Functional State Threads.
Every mutable native data type shall be wrapped in a Mutable with phantom type parameter s. that tells to what state thread the value belongs. For example, the new method of the java class java.util.Date could be accessed like this:
data Date s = native java.util.Date where native new :: Long -> ST s (Mutable s Date)
Inside ST actions, Date values can be created and manipulated with impure native methods at will. However, such a value can never escape its ST thread.
Because ST s is an instance of Monad, ST actions can be combined, which ensures sequenced execution. For example, we could add another method to the Date type that converts the date to a string:
native toString :: Mutable s Date -> ST s String
and a computation which yields the current time in string form:
epoch = do date <- Date.new 0L return date.toString
This looks almost like java already! epoch has type ST s String and we can run the computation with epoch.run (see ST.run below), which gives us a nice, pure, immutable, functional, correct String value.
The IO type is just an alias for ST RealWorld, and can be thought of as indexing a unique global state thread. Values of type IO a are also called IO actions.
Any ST value can also be used in the IO thread.
This guarantees that
warning: You are breaking the rules. Expect an arbitrary result and program crashes.
nowarn: performUnsafe
Run a stateful action with type ST r a and return a result of type a. The overall computation ST.run st is pure, though inside the ST action local mutable state can be employed.
This is possible only if the result type a of the state action does not mention r and if r is a type variable. Hence, it is not possible to ST.run an IO action.
This abstract data type identifies the global state (disk, network, you name it). Values of type ST RealWorld a are likely to perform input/output, manipulate global data and so on.
warning: use stderr.print instead
print a String to the standard error stream
warning: use stderr.println instead
print a String to the standard error stream and append a new line.
warning: use stdout.print instead
print a String to the standard output stream
warning: use stdout.println instead
print a String to the standard output stream and append a new line.
c.ord is the ordinal (integer) value of the character c. It holds: c.ord.char == c, see Int.char. (But note that i.char.ord is not necessarily i)
from i = i
ord i = i
pred i is the same as i-1 except for pred Int.minBound, which is undefined
succ i is the same as i+1 except for succ Int.maxBound, which is undefined
Integer is an instance of Enum
Integer.from i is the same as Int.big i
ord b is only defined if the value of b is in the range Bounded_Int.minBound .. Bounded_Int.maxBound
succ b is the same as b + 1.big
succ b is the same as b + 1.big
Long.from i returns a Long with the same numeric value as i.
ord l is only valid if Int.minBound.long <= l && l <= Int.maxBound
pred a is the same as a-1L except for pred Long.minBound, which is undefined
succ a is the same as a+1L except for succ Long.maxBound, which is undefined
the Eq.hashCode is that of the Long value used to represent the Double
bit representation of a float serves as hashCode
Uses the java != operator for comparison of Int values.
Uses the java == operator for comparison of Int values.
The Eq.hashCode of an Int is the identity
get the hash code
inherited from Eq.!=
two lists are equal if their heads and tails are equal or if the lists are empty
inherited from Integral.^
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.fromIntegral
inherited from Integral.gcd
inherited from Integral.gcdHelper
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.powf
inherited from Integral.powg
inherited from Integral.quotRem
Integer is an instance of Integral
inherited from Integral.^
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.fromIntegral
inherited from Integral.gcdHelper
inherited from Num.isInfinite
inherited from Num.isNaN
inherited from Num.isNumber
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.powf
inherited from Integral.powg
inherited from Integral.quotRem
inherited from Num.subtract
inherited from Integral.^
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.fromIntegral
inherited from Integral.gcd
inherited from Integral.gcdHelper
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.powf
inherited from Integral.powg
inherited from Integral.quotRem
Uses the java * operator to multiply 2 Int values.
Uses the java + operator to add 2 Int values.
Uses the java - operator to subtract one Int value from another.
Returns the negated argument if it is negative, otherwise the argument itself.
This does not work for Bounded_Int.minBound since there is no corresponding positive value that can be represented as an Int. Rather
abs Int.minBound == Int.minBound
For Int values, this is the identity function.
inherited from Num.isInfinite
inherited from Num.isNaN
inherited from Num.isNumber
negate i computes 0-i using the java negation operator -.
This does not work for Bounded_Int.minBound since there is no corresponding positive value that can be represented as an Int. Rather
negate Int.minBound == Int.minBound
the integer constant 1
inherited from Num.sign
inherited from Num.subtract
the integer constant 0
Uses the java * operator to multiply two Long values.
Uses the java + operator to add two Long values.
Uses the java - operator to subtract a Long value from another.
Returns the negated argument if it is negative, otherwise the argument itself.
This does not work for Bounded_Long.minBound since there is no corresponding positive value that can be represented as a Long. Rather
abs Long.minBound == Long.minBound
applies the widening primitive conversion (JLS §5.1.2) from int to long
inherited from Num.isInfinite
inherited from Num.isNaN
inherited from Num.isNumber
negate a computes 0L-a using the java negation operator -.
This does not work for Bounded_Long.minBound since there is no corresponding positive value that can be represented as a Long. Rather
negate Long.minBound == Long.minBound
The constant 1L.
inherited from Num.sign
inherited from Num.subtract
The constant 0L.
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
Uses the java < operator for comparison of Int values.
Uses the java <= operator for comparison of Int values.
Uses the java > operator for comparison of Int values.
Uses the java >= operator for comparison of Int values.
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Ord.compare
Uses the java < operator for comparison of Long values.
Uses the java <= operator for comparison of Long values.
Uses the java > operator for comparison of Long values.
Uses the java >= operator for comparison of Long values.
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Num.abs
inherited from Num.isNumber
the NaN value provided by the Java runtime
negative infinity
positive infinity
inherited from Num.sign
inherited from Num.subtract
inherited from Num.abs
inherited from Num.isNumber
the NaN value provided by the Java runtime
negative infinity
positive infinity
inherited from Num.sign
inherited from Num.subtract
Eq_String.!=, Eq_String.==, Ord_String.>, Ord_String.<, Ord_String.<=, Ord_String.>=, StringJ.startsWith, StringJ.contentEquals, StringJ.contains
StringJ.lastIndexOf𝖈, StringJ.indexOf𝖈, StringJ.compareTo, StringJ.compareToIgnoreCase
printStr, printStrLn, traceStr, traceStrLn, StringJ.wait, StringJ.notifyAll, StringJ.notify
StringJ.toUpperCase, StringJ.trim, StringJ.toLowerCase, StringJ.quoteReplacement, StringJ.quote
&&, ^^, oder, und, ||, Eq_Bool.!=, Eq_Bool.==, Ord_Bool.min, Ord_Bool.max, Ord_Bool.>, Ord_Bool.<, Ord_Bool.<=, Ord_Bool.>=
Eq_Char.!=, Eq_Char.==, Ord_Char.>, Ord_Char.<, Ord_Char.<=, Ord_Char.>=, Char.isSurrogatePair
Char.isUpperCase𝖆, Char.isWhitespace𝖆, Char.isSurrogate, Char.isLowSurrogate, Char.isLetter𝖆, Char.isLowerCase𝖆, Char.isLetterOrDigit𝖆, Char.isHighSurrogate, Char.isDigit𝖆, Char.isDefined𝖆, Char.isISOControl𝖆
Enum_Char.pred, Enum_Char.succ, Char.toUpperCase𝖆, Char.toTitleCase𝖆, Char.toLowerCase𝖆
Eq_Double.!=, Eq_Double.==, Ord_Double.>, Ord_Double.<, Ord_Double.<=, Ord_Double.>=
Ord_Double.min, Ord_Double.max, Real_Double.subtract, Real_Double./, Real_Double.+, Real_Double.*, Real_Double.-
Real_Double.isNaN, Real_Double.isInfinite, Real_Double.isNumber
Eq_Float.!=, Eq_Float.==, Ord_Float.>, Ord_Float.<, Ord_Float.<=, Ord_Float.>=
Ord_Float.min, Ord_Float.max, Real_Float.subtract, Real_Float./, Real_Float.+, Real_Float.*, Real_Float.-
Real_Float.isNaN, Real_Float.isInfinite, Real_Float.isNumber
Eq_Int.!=, Eq_Int.==, Ord_Int.>, Ord_Int.<, Ord_Int.<=, Ord_Int.>=
Integral_Int.rem, Integral_Int.quot, Integral_Int.mod, Integral_Int.gcdHelper, Integral_Int.gcd, Integral_Int.div, Integral_Int.lcm, Num_Int.subtract, Num_Int.-, Num_Int.*, Num_Int.+, Ord_Int.min, Ord_Int.max, Int.ushiftR, Int.shiftL, Int.shiftR, Int.rotateL, Int.rotateR, Int..^., Int..&., Int..|.
Enum_Bool.from, Integral_Int.odd, Integral_Int.even, Num_Int.isNaN, Num_Int.isNumber, Num_Int.isInfinite, Char.isWhitespace𝖇, Char.isValidCodePoint, Char.isUpperCase𝖇, Char.isLowerCase𝖇, Char.isSupplementaryCodePoint, Char.isLetter𝖇, Char.isISOControl𝖇, Char.isLetterOrDigit𝖇, Char.isDigit𝖇, Char.isDefined𝖇
chr, Enum_Char.from, Char.lowSurrogate, Char.highSurrogate, Int.char
?, Enum_Int.succ, Enum_Int.ord, Enum_Int.from, Enum_Int.pred, Eq_Int.hashCode, Num_Int.negate, Num_Int.sign, Num_Int.fromInt, Num_Int.abs, Char.toUpperCase𝖇, Char.toTitleCase𝖇, Char.toLowerCase𝖇, Char.getType𝖇, Int.complement
Enum_Integer.from, Integral_Int.big, Integral_Integer.fromInt
Ord_Integer.>, Ord_Integer.==, Ord_Integer.!=, Ord_Integer.<, Ord_Integer.<=, Ord_Integer.>=
Integral_Integer.subtract, Integral_Integer.quot, Integral_Integer.rem, Integral_Integer.lcm, Integral_Integer.mod, Integral_Integer.gcd, Integral_Integer.gcdHelper, Integral_Integer.div, Integral_Integer.+, Integral_Integer.*, Integral_Integer.-, Ord_Integer.min, Ord_Integer.max, Integer.nMod, Integer..^., Integer..&., Integer..|.
Integral_Integer.odd, Integral_Integer.isNaN, Integral_Integer.isInfinite, Integral_Integer.even, Integral_Integer.isNumber
Enum_Integer.ord, Integral_Integer.sign, Num_Int.fromInteger, Ord_Integer.hashCode, Integer.int, Integer.bitLength
Enum_Integer.succ, Enum_Integer.pred, Integral_Integer.negate, Integral_Integer.fromInteger, Integral_Integer.big, Integral_Integer.abs, Integer.complement
Long.ushiftR, Long.shiftL, Long.rotateL, Long.rotateR, Long.shiftR
Eq_Long.!=, Eq_Long.==, Ord_Long.>, Ord_Long.<, Ord_Long.<=, Ord_Long.>=
Integral_Long.rem, Integral_Long.quot, Integral_Long.mod, Integral_Long.gcdHelper, Integral_Long.gcd, Integral_Long.div, Integral_Long.lcm, Num_Long.subtract, Num_Long.-, Num_Long.*, Num_Long.+, Ord_Long.min, Ord_Long.max, Long..^., Long..&., Long..|.
Integral_Long.odd, Integral_Long.even, Num_Long.isNaN, Num_Long.isNumber, Num_Long.isInfinite
Enum_Long.succ, Enum_Long.pred, Num_Long.negate, Num_Long.abs, Long.complement
Throwable.getMessage, Throwable.caught, Throwable.getLocalizedMessage
Real_Double.zero, Real_Double.one, Real_Double.positiveInfinity, Real_Double.nan, Real_Double.negativeInfinity
Real_Float.zero, Real_Float.one, Real_Float.positiveInfinity, Real_Float.nan, Real_Float.negativeInfinity
Bounded_Int.minBound, Bounded_Int.maxBound, Num_Int.zero, Num_Int.one, Int.size, Long.size
Bounded_Long.minBound, Bounded_Long.maxBound, Num_Long.zero, Num_Long.one
Integral.rem, Integral.mod, Integral.quot, Integral.gcdHelper, Integral.gcd, Integral.div, Integral.lcm
Char.toUpperCase, Char.toTitleCase, Char.toLowerCase, Char.isWhitespace, Char.isUpperCase, Char.isLowerCase, Char.isLetter, Char.isLetterOrDigit, Char.isISOControl, Char.isDigit, Char.isDefined, Char.getType, Char.getName, NumberFormatException.new, StringJ.indexOf, StringJ.lastIndexOf, StringJ.format, Undefined.new