Compiled: Tue Dec 04 23:59:41 PST 2012 from source file: frege/prelude/PreludeBase.fr
Copyright © 2011, 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 implicetly 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 programmaticaly 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.
This is the principal return type for java methods that are expected to throw exceptions.
Frozen MyType is an abbrevation for MyType Immutable
Mutable MyType s is an abbrevation for ST s (MyType s)
String values are based on Java's java.lang.String objects. String is an alias for StringJ Char
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
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.
Long, Int, Bool, Char, Integer
a .. b is the list [a, succ a, succ (succ a), ..., b ] if a < b, or [a] if a == b or the empty list if a > b.
This is the default implementation of the compare operator, that makes each Enum type an Ord type automatically.
a <=> b = (ord a).<=> (ord b)
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 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, StringJ, Float, Char, Double, (), Bool, Int
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.
provided for Haskell compatibility as an alias for Eq.!=
Check for equality. This function is required in all instances.
Ayn Rand's favorite law
a == a
shall be obeyed by all implementations.
Compute a hash code.
The follwoing 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 hashcodes more likely than not.
Class Integral provides bit arithmetic, division and remainder operations for integral numbers.
binary and, supposed to work like Java & on all integral types
one's complement, supposed to work like Java operator ~
every integral number can be converted to a big Integer
binary or, supposed to work like Java | on all integral types
binary left shift, supposed to work like Java << on all integral types
binary right shift, supposed to work like Java >> on all integral types
binary exclusive or, supposed to work like Java ^ on all integral types
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.
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))
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
unsigned right shift, supposed to work like Java >>> on all integral types
The Num class provides the operators (Num.+), (Num.-) and (Num.*) as well as some functions that are common for all numeric types.
Integer, Double, Float, 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
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 funtions 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 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.
StringJ, Integer, Long, Float, Char, Double, Bool, Int
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
The Real class provides the division operator (Real./).
the division operator
inherited from Enum
inherited from Enum
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)
Integer is an instance of Enum
inherited from 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
inherited from Enum
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
inherited from Eq./=
inherited from Eq./=
inherited from Eq./=
inherited from Eq./=
the Eq.hashCode is that of the Long value used to represent the Double
inherited from Eq./=
bit representation of a float serves as hashCode
Uses the java != operator for comparision of Int values.
inherited from Eq./=
Uses the java == operator for comparision of Int values.
The Eq.hashCode of an Int is the identity
Uses the java != operator for comparision of Long values.
inherited from Eq./=
Uses the java == operator for comparision of Long values.
hash code is upper half and lower half xor'ed
inherited from Eq./=
get the has code
Computes binary /and/ of two integers. Uses the java &-operator.
Computes binary /or/ of two integers. Uses the java |-operator.
Computes binary /exclusive or/ of two integers. Uses the java ^-operator.
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.even
inherited from Integral.gcd
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.odd
inherited from Integral.quotRem
unsigned right shift
Integer is an instance of Integral
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.even
inherited from Num.isInfinite
inherited from Num.isNaN
inherited from Num.isNumber
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.odd
inherited from Integral.quotRem
inherited from Num.subtract
unsigned right shift on big integers does not really make sense ...
Computes binary /and/ of two integers. Uses the java &-operator.
Computes binary /or/ of two long integers. Uses the java |-operator.
Computes binary /exclusive or/ of two long integers. Uses the java ^-operator.
inherited from Integral.div
inherited from Integral.divMod
inherited from Integral.even
inherited from Integral.gcd
inherited from Integral.lcm
inherited from Integral.mod
inherited from Integral.odd
inherited from Integral.quotRem
unsigned right shift
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
applys 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 comparision of Int values.
Uses the java <= operator for comparision of Int values.
Uses the java > operator for comparision of Int values.
Uses the java >= operator for comparision of Int values.
inherited from Ord.compare
inherited from Ord.max
inherited from Ord.min
inherited from Eq./=
inherited from Ord.compare
Uses the java < operator for comparision of Long values.
Uses the java <= operator for comparision of Long values.
Uses the java > operator for comparision of Long values.
Uses the java >= operator for comparision 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
inherited from Num.sign
inherited from Num.subtract
inherited from Num.abs
inherited from Num.isNumber
inherited from Num.sign
inherited from Num.subtract
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
Bool values are based on Java's primitive boolean values. Note that true and false are literals, not constructors.
Char values are based on Java's primitive char values.
We need to do some reflection from frege code. For example, when we catch an JException thrown from Java code. we might want to know what it is.
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 speicified in the IEEE Standard for Binary Floating-Point Arithmetic.
Applies the java narrowing primitive conversion from double to float
Returns the largest (closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer.
Special cases:
Returns the closest Long value to the argument. The result is rounded to an integer by adding 1/2, taking the Double.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
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 speicified in the IEEE Standard for Binary Floating-Point Arithmetic.
Applies the java widening primitive conversion from float to double.
Returns the largest (closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer.
Special cases:
Returns the closest Int value to the argument. The result is rounded to an integer by adding 1/2, taking the Float.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:
Some native values can be detached from the stateful realm so as to make it possible to apply them to pure (native) functions, see Freezable.
Such values will have Immutable as last type argument.
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 spezialized 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.
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.
convert to a hexadecimal string
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.
/Warning/! Throws ArithmeticException when divisor is negative.
construction from a Long, see also StringJ.aton and StringJ.integer
Frege wrapper for java exceptions.
give the name of this exception
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.
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.
Matcher values are based on Java's java.util.regex.Matcher objects. Code generation relies on the existence of this type and its operations.
The native Matcher functions that correspond to java methods of the java.util.regex.Matcher class that modify the state of the object they are invoked on (Matcher.find, Matcher.matches, Matcher.replaceFirst, Matcher.replaceAll, Matcher.usePattern, Matcher.useAnchoringBounds) are implemented so that they make a copy of the Matcher and invoke the impure java method on the copy.
Frege Matchers can thus be regarded as read-only values and the functions defined here as pure. If you need to pass a Matcher to other native functions, be sure that the function is pure. If it is not because it would modify the matcher, and you do not need the match result, always pass a clone of the Matcher (see Matcher.clone)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation where group 0 denotes the entire pattern.
If the specified capturing group failed to match, the return value will be -1.
The follwoing property holds:
(m.group n == Nothing) ==> (m.end n < 0)
Tries a match and if it succeeds, returns Just m, where m is a new Matcher that contains the result. If there is no match, Nothing is returned.
The following java fragment appends all matched substrings of a string:
String s = "cats and dogs are not concatenated."; Pattern p = Pattern.compile("cat|dog"); String result = ""; Matcher m = p.matcher(s); while (m.find()) result += m.group(0); // "catdogcat"
The follwoing frege fragment computes the same result:
result = loop m "" where s = "cats and dogs are not concatenated." p = #cat|dog# m = p.matcher s loop :: Matcher -> String -> String loop m1 r | Just m2 <- m1.find = loop m2 (r++m2.match) | otherwise = r
Retrieves the input subsequence captured by the given group during the previous match operation.
Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression (m.group 0) retrieves that portion of the input string that was matched by the pattern.
If the match was successful but the group specified failed to match any part of the input sequence, then Maybe.Nothing is returned. Note that some groups, for example (a?), match the empty string. This functon will return Just "" when such a group successfully matches the empty string in the input.
The folloing property holds for a Matcher /m/ with input sequence /s/ and group index /g/:
isJust (m.group g) ==> (m.group g) == Just (s.substr (m.start g) (m.end g))
Matcher.match m returns the input subsequence matched by the previous match. The result is undefined if the last match was not successful.
For a Matcher m with input sequence s, the following holds:
isJust (m.group 0) ==> unJust (m.group 0) == m.match
Note that some patterns, for example a?, match the empty string. This method will return the empty string when the pattern successfully matches the empty string in the input.
Tries to match the entire string and returns Just m on success and otherwise Nothing.
Like Matcher.replaceFirst, but replaces all matches.
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
This method scans the input sequence from the start looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences.
Note that backslashes (\\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
Given the regular expression #dog#, the input "zzzdogzzzdogzzz", and the replacement string "cat", an invocation of this function on a matcher for that expression would yield the string "zzzcatzzzdogzzz".
Returns the start index of the subsequence captured by the given group during the previous match operation where group 0 denotes the entire pattern.
If the specified capturing group failed to match, the return value will be -1.
The follwoing property holds:
(m.group n == Nothing) ==> (m.start n < 0)
Returns the string representation of this matcher. The string representation of a Matcher contains information that may be useful for debugging. The exact format is unspecified.
Requires or forbids the matcher to acknowledge anchors.
Makes a new Matcher and causes it to use a different Regex for future matches.
The original matchers position in the input and its last append position is copied, but information about the last match, if any, is not.
This is most useful with patterns that start with the \\G anchor.
Note that, due to a java bug, if the last find operation matched the empty string, the next find will fail. For a workaround see Matcher.usePatternAndFind
Makes a new Matcher with a different Regex and tries to find a match. If the last find on the original Matcher returned an empty result, it calls mnew.find(morig.end(0)) to work around a bug in the java API. Therefore, this function must only be used on a matcher whose last match attempt was successful.
The type of the result of Ord.<=>
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.
Regex values are based on Java's java.util.regex.Pattern objects. All regular expression literals are values of this type.
(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 independend 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 will have a phantom type paramter 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 :: () -> ST s (Date s)
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 :: Date s -> ST s String
and a computation which yields the current time in string form:
now = do date <- Date.new () return date.toString
This looks almost like java already! now has type ST s String and we can run the computation with now.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
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 run an IO action.
State s a is an abstrac data type that resembles a stateful computation with state s and result a, i.e. functions of type s -> (a, s) where the state is immutable.
monadic bind for the State monad
change the State
get the current State
update the State
lift a value to the State monad
run a stateful computation
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
interpret this string as regex (unsafe, does not catch exceptions)
see regcomp for an alternative
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
Get character at index.
This will allow to use a string like an array, e.g. "xyz".[1]
Safe way to parse an integer from a string. java.lang.NumberFormatException will be catched 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
Polymorphic variant of elemAt
Always returns a character (what else?) for nonempty strings.
The length of a String
Safe way to parse a long integer from a string. See StringJ.int
quote regular expression metacharacters in string
quote replacement string metacharacters in string
convert to lower case
convert to upper case
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 from a string and a cause
create an Undefined value from a JException. The message will be taken from the exception.
convert an Undefined value to a JException
This is actually a no-op, since frege.RT.Undefined is a subclass of java.lang.Exception. However, the type checker knows nothing about subclasses.
This function is used in the Monad (Exception a).
list type
list construction
empty list
The Java ! operator on booleans
This checks for object identity or inequality of primitive values using Java's != operator. It evaluates its arguments, so undefined values cannot be compared.
s !~ p == !(s ~ p)
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, becuase $ is right associative, f $ g $ h y is f (g (h y))
Same as `$` but argument is strict
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
m /~ p
is like
m ?~ p
but instead of the matcher it returns the matched string, if any.
Function composition.
(f <~ g) is a function whose argument is passed to /g/, and the result to /f/, yielding the overall result.
One can imagine that the data flow from right to left through a function pipe.
(a) <~ (b) = \x -> a (b x)
This checks for object identity or equality of primitive values using Java's == operator. It evaluates its arguments, so undefined values cannot be compared.
string =~ regex
tries to match string against regex and returns Just matcher if it succeeds, Nothing otherwise.
m ?~ p binds pattern p to the matcher m and tries a match. There must have been a successful match on m before.
Returns Maybe.Nothing if match fails, else (Maybe.Just m).
This function is most usefull in conjunction with patterns that use the G-anchor when one wants to extract multiple differnt adjacent items from a string.
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 }}
a ^^ b is true if either a or b is true, but not both.
a ^^ b = if a then not b else b
asTypeOf a b is a with the type of b.
This is a type restricted version of const.
this is just an alias for comparing
comparing f applies a proejction function on both sides of Ord.<=>. Example usage:
sortBy (comparing snd) [(1, "z"), (2, "b")] == [(2, "b"), (1, "z")]
const a is a function that returns a regardless of its argument.
Determines the constructor of a value. This is used like
constructor arg
where arg is any frege value.
Returns 0 for product types and native types or the /constructor number/ for constructed types. The /constructor number/ is a small integer stored in every constructed value. It indicates by what data constructor a value was constructed.
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.
curry f passes the next two arguments as 2-tuple to f
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")]
Construct an undefined value with an informative message.
Exchange first and second argument of a function, i.e.
> flip f a b = f b a
return the first element of a 2-tuple
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.
The identity function
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.
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.
not b is true if b is false, otherwise true. Uses java's ! operator.
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.
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 = ...
print a string to the standard output stream
print a string to the standard output stream and append a new line.
a `seq` b evaluates a before it returns b
This is a right associative operator with precedence 15.
return the second element of a 2-tuple
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 tuple. The difference to (,) is that both argument values will be evaluated before the tuple is constructed. Thus:
fst (42; undefined) == undefined fst (42, undefined) == 42
Implementation specific: The compiler will rewrite (a;b) as (PreludeBase.strictTuple2 a b).
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.
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.
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.
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.
Constructs an undefined value from a java exception and throws it.
print a string to the standard error stream
print a string to the standard error stream and append a new line.
Passes the elements of a 2-tuple as arguments to a function.
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.
This is the standard undefined value.
using f applies a projection function f on both sides of Eq.==. The example for uniqBy could be written easier
uniqBy (using fst) [(1, 1), (2, 2), (2, 3), (3,4), (2,5)]
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
string ~ regex
true if string matches regex, false otherwise
Function composition.
(f ~> g) is a function whose argument is first passed to /f/, and the result to /g/, yielding the overall result.
One can imagine that the data flows from left to right through a function pipe.
(a) ~> (b) = \x -> b (a x)
("string" ~~ #r??#) == Just "rin"
Tries a match and returns Just x where x is the matched substring or Nothing if there was no match.
string ~~~ regex
Matches string with regex and returns a function that can be used to extract the matched part of the string and the captured substrings.
let f = "frege" ~~~ #(..).(..)# in [ f i | i <- 0..3 ]
yields
[Just "frege", Just "fr", Just "ge", Nothing]
Another operator for function composition, which is identical to <~
> (f • g) a = f (g a)
Eq_String./=, Eq_String.!=, Eq_String.==, Ord_String.>, Ord_String.<=, Ord_String.<, Ord_String.>=
StringJ.quoteReplacement, StringJ.quote, StringJ.toLowerCase, StringJ.toUpperCase
&&, ^^, oder, und, ||, Eq_Bool./=, Eq_Bool.!=, Eq_Bool.==, Ord_Bool.>, Ord_Bool.<=, Ord_Bool.<, Ord_Bool.>=, Ord_Bool.max, Ord_Bool.min
Eq_Char./=, Eq_Char.!=, Eq_Char.==, Ord_Char.>, Ord_Char.<=, Ord_Char.<, Ord_Char.>=
Char.isLowerCase, Char.isLetterOrDigit, Char.isWhitespace, Char.isUpperCase
Enum_Char.pred, Enum_Char.succ, Char.toLowerCase, Char.toUpperCase
Eq_Double./=, Eq_Double.!=, Eq_Double.==, Ord_Double.>, Ord_Double.<=, Ord_Double.<, Ord_Double.>=
Ord_Double.max, Ord_Double.min, Real_Double./, Real_Double.+, Real_Double.*, Real_Double.-, Real_Double.subtract
Real_Double.isNaN, Real_Double.isInfinite, Real_Double.isNumber
Eq_Float./=, Eq_Float.!=, Eq_Float.==, Ord_Float.>, Ord_Float.<=, Ord_Float.<, Ord_Float.>=
Ord_Float.max, Ord_Float.min, Real_Float./, Real_Float.+, Real_Float.*, Real_Float.-, Real_Float.subtract
Real_Float.isNaN, Real_Float.isInfinite, Real_Float.isNumber
Eq_Int./=, Eq_Int.!=, Eq_Int.==, Ord_Int.>, Ord_Int.<=, Ord_Int.<, Ord_Int.>=
Integral_Int.bxor, Integral_Int.band, Integral_Int.bshl, Integral_Int.bor, Integral_Int.bshr, Integral_Int.gcd, Integral_Int.div, Integral_Int.quot, Integral_Int.mod, Integral_Int.lcm, Integral_Int.rem, Integral_Int.ushr, Num_Int.-, Num_Int.+, Num_Int.*, Num_Int.subtract, Ord_Int.max, Ord_Int.min
Enum_Bool.from, Integral_Int.even, Integral_Int.odd, Num_Int.isNaN, Num_Int.isInfinite, Num_Int.isNumber
Enum_Int.from, Enum_Int.pred, Enum_Int.ord, Enum_Int.succ, Eq_Int.hashCode, Integral_Int.bcmpl, Num_Int.fromInt, Num_Int.abs, Num_Int.sign, Num_Int.negate
Enum_Integer.from, Integral_Int.big, Integral_Integer.fromInt
Integral_Integer.bshr, Integral_Integer.bshl, Integral_Integer.ushr
Ord_Integer.>, Ord_Integer.<, Ord_Integer.!=, Ord_Integer./=, Ord_Integer.<=, Ord_Integer.==, Ord_Integer.>=
Integral_Integer.-, Integral_Integer.+, Integral_Integer.*, Integral_Integer.band, Integral_Integer.bor, Integral_Integer.bxor, Integral_Integer.div, Integral_Integer.lcm, Integral_Integer.gcd, Integral_Integer.rem, Integral_Integer.mod, Integral_Integer.quot, Integral_Integer.subtract, Ord_Integer.max, Ord_Integer.min, Integer.nMod
Integral_Integer.isInfinite, Integral_Integer.even, Integral_Integer.isNaN, Integral_Integer.isNumber, Integral_Integer.odd
Enum_Integer.ord, Integral_Integer.sign, Ord_Integer.hashCode, Integer.bitLength, Integer.int
Enum_Integer.pred, Enum_Integer.succ, Integral_Integer.bcmpl, Integral_Integer.abs, Integral_Integer.big, Integral_Integer.negate
JException.catched, JException.getLocalizedMessage, JException.getMessage
Eq_Long./=, Eq_Long.!=, Eq_Long.==, Ord_Long.>, Ord_Long.<=, Ord_Long.<, Ord_Long.>=
Integral_Long.bxor, Integral_Long.band, Integral_Long.bor, Integral_Long.gcd, Integral_Long.div, Integral_Long.quot, Integral_Long.mod, Integral_Long.lcm, Integral_Long.rem, Num_Long.-, Num_Long.+, Num_Long.*, Num_Long.subtract, Ord_Long.max, Ord_Long.min
Integral_Long.even, Integral_Long.odd, Num_Long.isNaN, Num_Long.isInfinite, Num_Long.isNumber
Enum_Long.pred, Enum_Long.succ, Integral_Long.bcmpl, Num_Long.abs, Num_Long.negate
Bounded_Int.minBound, Bounded_Int.maxBound, Num_Int.one, Num_Int.zero
Bounded_Long.minBound, Bounded_Long.maxBound, Num_Long.one, Num_Long.zero
gcda, Integral.bxor, Integral.bor, Integral.band, Integral.gcd, Integral.div, Integral.quot, Integral.mod, Integral.lcm, Integral.rem