Skip to content
REL
REFERENCE
stdlib

The Standard Library (stdlib)

Broad collection of Rel relations that perform essential and commonly used tasks.

^Date

^Date[year in Int, month in Int, day in Int]

Create a Date from its three components: year, month and day. The three arguments are required to be Int64.

^Date

^Date[dt in DateTime, tz in String]

Create a Date from a DateTime, with timezone tz.

The timezone argument is necessary for the Date because DateTime is an instant of time that is timezone independent. For different locations on earth (timezones), a DateTime has different dates.

^DateTime

^DateTime[year, month, day, hour, minute, second, millisecond, tz in String]

Create a DateTime from a year, month, day, hour, minute, second, and millisecond.

The timezone argument tz is necessary to correctly interpret what instant in time this is.

There can be multiple DateTime values for one set of arguments: for example, with the ending of daylight saving time at 2am, every time between 1am and 2am occurs twice and has two corresponding instants of time.

For part values that are out of range, there are no tuples (there is no error).

^DateTime

^DateTime[date, hour, minute, second, millisecond, tz in String]

Create a DateTime from a date, hour, minute, second, and millisecond.

This constructor uses the year, month, and day from the date and then constructs a DateTime in the same way as the constructor with all parts as arguments.

^DateTime

^DateTime[year, month, day, tz in String]

Create a DateTime for the given year, month, and day, with the time components all set to 0. The resulting DateTime is the first millisecond for the given date and time zone tz.

See the ^DateTime constructor with time components as arguments for more details.

^DateTime

^DateTime[date in Date, tz in String]

Create a DateTime for the given Date, with the time components all set to 0. The resulting DateTime is the first millisecond of the given Date and time zone tz.

See the ^DateTime constructor with time components as arguments for more details.

F implies G
F ⇒ G
G ⇐ F

Logical implication, for boolean (arity 0, true or false) arguments F and G.

¬

not F
¬F

Logical negation, for boolean (arity 0, true or false) argument F.

F and G
F ∧ G

Logical and (conjunction).

F or G
F ∨ G

Logical or (disjunction), for boolean (arity 0, true or false) arguments F and G.

F ≢ G

Relational inequality, see equal.

abs

abs[x]

The absolute value of x.

Examples:

abs[-2] = 2
abs[-2.0] = 2.0

acos

acos[x]
acos(x, ac)

Arccosine of x. ac is the arccosine of x given in radians.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Cosine of ac. Must be grounded.
acFloating[64]Arccosine of x in radians.

Explanation

Defined for x between -1 and 1 (inclusive). The value of ac ranges from 0 to π.

Arccosine is sometimes called “inverse cosine.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the arccosine of 0:

def output = acos[0]
//output> 1.5707963267948966

Calculate the arccosine of -1 using full expression:

def output(x) = acos(-1, x)
//output> 3.141592653589793

Confirm that 1.5707963267948966 is the arccosine of 0:

def output = acos(0, 1.5707963267948966)
//output> () //() = true

See Also

sin, cos, asin, asinh, acosh, sinh, cosh, and haversine.

acosh

acosh[x]
acosh(x, ach)

Hyperbolic arccosine. ach is the hyperbolic arccosine of x.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Hyperbolic cosine of ach. Must be grounded.
achFloating[64]Hyperbolic arccosine of x.

Explanation

Defined for x >= 1.

Hyperbolic arccosine is sometimes called “inverse hyperbolic cosine.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the hyperbolic arccosine of 90:

def output = acosh[90]
//output> 5.192925985263684

Calculate the hyperbolic arccosine of 180 using full expression:

def output(x) = acosh(180, x)
//output> 5.886096315311465

Confirm that 5.192925985263684 is the hyperbolic arccosine of 90:

def output = acosh(90, 5.192925985263684)
//output> () //() = true

See Also

sin, cos, asin, acos, asinh, acosh, sinh, and haversine.

acot

acot[x]
acot(x, act)

Arccotangent. act is the arccotangent of x.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Cotangent of act. Must be grounded.
actFloating[64]Arccotangent of x.

Explanation

Arccotangent is sometimes called “inverse cotangent.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the arccotangent of 1:

def output = acot[1]
//output> 0.7853981633974483

Calculate the arccotangent of -1 using full expression:

def output(x) = acot(-1, x)
//output = -0.7853981633974483

Confirm that 0.7853981633974483 is the arccotangent of 1:

def output = acot(1, 0.7853981633974483)
//output> ()

See Also

tan, atan, atan2, cot, tanh, and atanh.

add

add[x, y]
add(x, y, s)
x + y

Addition of two numbers. Addition of a DateTime/Date x with a time duration y.

Parameters

Numeric Data

ParameterTypeDescription
xNumberFirst summand.
yNumberSecond summand.
sNumberSum x + y.

Not all numeric values can be mixed with each other. The following combinations work:

xys
NumberSame as xSame as x
Rational, FixedDecimalSignedInt[64]Same as x
SignedInt[64]SignedInt[128], Rational, FixedDecimal, Floating[64]Same as y
SignedInt[128]SignedInt[64]SignedInt[128]
Floating[64]SignedInt[64]Floating[64]

Two of the three arguments need to be grounded. Valid grounding combinations are as follows:

  • x and y.
  • x and s.
  • y and s.

Time Data

ParameterTypeDescription
xDate, DateTime, date period, time periodFirst summand.
yDate, DateTime, date period, time periodSecond summand.
sDate, DateTime, date period, time periodSum x + y.

The following combinations work:

xys
date period, time periodSame date period, time period as xSame date period, time period as x
date period, time periodDateTimeDateTime
DateTimedate period, time periodDateTime
date periodDateDate
Datedate periodDate

Two of the three arguments need to be grounded. Valid grounding combinations are as follows:

  • x and y.
  • x and s.
  • y and s.

Explanation

Addition evaluates the sum of x and y and assigns it to s. In procedural languages, usually x and y are given. In Rel — a declarative language — addition can be thought of as a mapping where x and y are the keys and s is the value, which is functionally dependent on x and y.

However, with addition — add(x, y, s) — it is sufficient to know any two of the three arguments. The third one can always be inferred. Usually x and y are given, but knowing x and s is enough to infer y.

Examples

Addition of Numbers

Add two integers using +:

def output = 1 + 2
//output> 3

Add an integer and a float using add:

def output = add[1, 2.5]
//output> 3.5

Add two floats using full expression:

def output(x) = add(1.7, 2.8, x)
//output> 4.5

Add integer to a rational:

def output = 1 + rational[16][2, 3]
//output> 5/3

Addition of Time

Add time to a timestamp:

def output:tomorrow = datetime_now + Day[1]
def output:next_hour = datetime_now + Hour[1]

Add weeks to a date:

def output = 2022-12-24 + Week[2]
//output> 2023-01-07

Add seconds together:

def output = Second[1] + Second[2]
//output> 3

See Also

subtract, divide, and multiply.

Any

Any(x)

Holds for any x, where x exists. (Any functions as a wildcard.)

Example:

Integrity constraint that tests whether x is of any type:

def R = (1, 3) ; (1, "foo")
 
ic any_ic {subset(R, (Int, Any) )}

approx_eq

approx_eq(tolerance, x, y)

Approximate equality. Use to compare scalar numbers and check if x and y are within the absolute tolerance (tolerance) of each other.

Parameters

ParameterTypeDescription
toleranceSignedInt[64] or Floating[64]Tolerance of the approximation. A positive number. Must be grounded.
xNumberA valid number. Must be the same data type as y. Must be grounded.
yNumberA valid number. Must be the same data type as x. Must be grounded.

Explanation

“approximately equal” is defined as number values being within the absolute tolerance (tolerance) of each other, or non-number values being equal.

The parameter tolerance stands for the absolute tolerance and must be of type SignedInt[64] or Floating[64]. Also, tolerance must be a positive number; negative numbers will return false

x and y should be of the exact same data type. For example, x and y can be of type FixedDecimal or Rational, but types must have the same bits and precision.

If x or y is not a number, approx_eq defaults to eq.

Examples

Approximate equality determined as true:

def output = approx_eq(0.05, 0.1, 0.15)
//output> () // () = true

Approximate equality determined as false:

def output = approx_eq(0.01, 0.1, 0.15)
//output>    // false

See Also

equal, eq, approx_equal, and full_relation_approx_equal.

approx_equal

approx_equal(tolerance, R, S)

Approximate relational equality. To hold true, the values in the last column of R must be approximately equal to values in the last column of S given the same key (prefix).

Parameters

ParameterTypeDescription
toleranceSignedInt[64] or Floating[64]A positive integer or float. Must be grounded.
RRelationA relation with corresponding keys and last elements that can be compared to S. Must be grounded.
SRelationA relation with corresponding keys and last elements that can be compared to R. Must be grounded.

Explanation

Two relations R and S are considered “relationally approximately equal” when for each tuple (k..., x) in S there exists a tuple (k..., y) in R where x and y are considered approximately equal. This approximate equality is symmetric and holds equally true when the places of R and S are swapped.

See approx_eq for the details about approximate equality between two data values.

The parameter tolerance stands for the absolute tolerance and must be of type SignedInt[64] or Floating[64]. tolerance must be a positive number; negative numbers will evaluate to false.

Keys must match for approx_equal to be true.

All values of the last column in R and S must be of the exact same data type. For example, the values can all be of type FixedDecimal or Rational, but types must have the same bits and precision. Otherwise, approx_equal evaluates to false.

Note the correspondence between approx_equal and equal: approx_equal(0, R, S) if and only if equal(R, S).

approx_equal applies only to the values in the last column in R and S. That is, if the values are not within tolerance, approx_equal will evaluate to false even if other arguments in the relations are within tolerance. If full relation comparison functionality is required, see full_relation_approx_equal.

Examples

Approximate relational equality determined as true:

def salary1 = {("John", 10.0) ; ("Mary", 20.0); ("Paul", 17.0) ; ("Peter", 15.0) }
def salary2 = {("John", 9.99) ; ("Mary", 20.01); ("Paul", 17.0) ; ("Peter", 15.0) }
 
def output = approx_equal(0.1, salary1, salary2)
//output> () //true

Approximate relational equality determined as false:

def salary1 = {("John", 10.0) ; ("Mary", 20.0); ("Paul", 17.0) ; ("Peter", 15.0) }
def salary2 = {("John", 11.0) ; ("Mary", 21.0); ("Paul", 17.0) ; ("Peter", 15.0) }
 
def output = approx_equal(0.1, salary1, salary2)
//output>   //false

Approximate relational equality determined as false because keys are different:

def salary1 = {("John", 10.0) ; ("Mary", 20.0); ("Paul", 17.0) ; ("Peter", 15.0) }
def salary3 = {("John", 9.99) ; ("Ben", 20.0); ("Paul", 17.0) ; ("Peter", 15.0) }
 
def output = approx_equal(0.1, salary1, salary3)
//output>   //false

Approximate relational equality determined as false, even though the first arguments are within tolerance:

def coordinates1 = (1.0, 2.0); (3.0, 6.0)
def coordinates2 = (1.0000001, 2.0); (2.9999999, 6.0000001)
 
def output = approx_equal(0.001, coordinates1, coordinates2)
//output>   //false

See Also

full_relation_approx_equal, approx_eq, equal, and eq.

argmax

argmax[R]
argmax(R, am)

For a relation R, find the tuples whose last elements are largest and return those tuples with the last element omitted.

Parameters

ParameterTypeDescription
RRelationA relation whose tuples contain key-value pairs. Must be grounded.
amNumberA tuple in R with the largest last element, with last element omitted.

Explanation

If tuples in R contain keys and values, argmax returns all the keys for the largest value. Typically, argmax is used when the last elements of each tuple are numeric.

argmax is typically used with relations whose shortest tuple has length two since. Note that, for all unary relations, argmax results in a relation containing an empty tuple.

Examples

Find key for largest value of R:

def R = {("A", 7.5); ("B", 8.6); ("C", 9.7); ("D", 7.5)}
def output(am) = argmax(R, am)
//output> "C"

Find key for largest value of R where values are rationals:

def R = {("A", rational[64, 8, 3]); ("B", rational[64, 9, 7]); ("C", rational[64, 11, 4]); ("D", rational[64, 8, 3])}
def output = argmax[R]
//output> "C"

Find the teams with the largest aggregated salary:

def salary = {("Burrow", 11,515,044); ("Chase", 18,211,606); ("Allen", 77,289,124); ("Diggs", 45,466,111)}
def member = {("Bengals", "Burrow"); ("Bengals", "Chase"); ("Bills", "Allen"); ("Bills", "Diggs")}
def team = {"Bengals"; "Bills"}
def output = argmax[d in team: sum[salary[p] for p in member[d]]]
//output> "Bengals"

See Also

argmin, maximum, min, argmax, sum, product, and average.

ArgMax

ArgMax[R]

Please use argmax[R]. Deprecates in near future

argmin

argmin[R]
argmin(R, am)

For a relation R, find the tuples whose last elements are smallest and return those tuples with the last element omitted.

Parameters

ParameterTypeDescription
RRelationSource relation. Must be grounded.
amAnyA tuple in R with the smallest last element, with last element omitted.

Explanation

If tuples in R contain keys and values, argmin returns all the keys for the largest value. Typically, argmin is used when the last elements of each tuple are numeric.

argmin is typically used with relations whose shortest tuple has length two since. Note that, for all unary relations, argmin results in a relation containing an empty tuple.

Examples

Find key for smallest value of R:

def R = {("A", 7.5); ("B", 8.6); ("C", 9.7); ("D", 7.5)}
def output(am) = argmin(R, am)
//output> "A"
//        "C"

Find key for smallest value of R where values are rationals:

def R = {("A", rational[64, 8, 3]); ("B", rational[64, 9, 7]); ("C", rational[64, 11, 4]); ("D", rational[64, 7, 3])}
def output = argmin[R]
//output> "B"

Find key for smallest value of R with tuples of various arity:

def R = {("A", 7.5); ("B", 8.6); ("C", "W", 9.7); ("D", "X", 7.5)}
def output = argmin[R]
//output> "A"
//        "D", "X"

Find the teams with the smallest aggregated salary:

def salary = {("Burrow", 11,515,044); ("Chase", 18,211,606); ("Allen", 77,289,124); ("Diggs", 45,466,111)}
def member = {("Bengals", "Burrow"); ("Bengals", "Chase"); ("Bills", "Allen"); ("Bills", "Diggs")}
def team = {"Bengals"; "Bills"}
def output = argmin[d in team: sum[salary[p] for p in member[d]]]
//output> "Bills"

See Also

argmax, minimum, min, argmax, sum, product, and average.

ArgMin

ArgMin[R]

Please use argmin[R]. Deprecates in near future

arity

arity[R]

The arity of a relation. In some cases, it can be an over-approximation.

Arity is a higher-order relation that is always evaluated at compile-time.

Examples:

def output = arity[3]
1
def output = arity[{1; 2; 3}]
1
def output = arity[(1, 2)]
2
def output = arity[add]
3
def output = arity[{1; 2; (1,2)}]
1
2

Arity can be used to do meta-programming in logic. For example, the following abstraction verbalize implements specific cases using arity.

Examples:

@inline def verbalize[R] = "nullary", arity[R] = 0;
                            "unary", arity[R] = 1;
                            "binary", arity[R] = 2
def output = verbalize[true]
"nullary"
def output = verbalize[1]
"unary"

Arity can be used in higher-order abstractions to check at compile-time that they are used correctly.

Arity can be used in integrity constraints to state expectation on EDB or IDB relations. Because arity is evaluated at compile-time, it can catch mistakes in the logic before the logic executes.

Example:

def p = 1,2,3
ic { arity[p] = 3 }

Note that there is a difference between R(_, _) and arity(R) = 2. The first requires R to be non-empty, which is an runtime property of R.

asin

asin[x]
asin(x, as)

Arcsine of x. ac is the arcsine of x given in radians.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Sine of as. Must be grounded.
asFloating[64]Arcsine of x.

Explanation

Defined for x between -1 and 1 (inclusive). The value of as ranges from -π/2 to π/2.

Only 64-bit float and 64-bit integer values for x are supported.

Arcsine is sometimes called “inverse sine.”

Examples

Calculate the arcsine of 1:

def output = asin[1]
//output> 1.5707963267948966

Calculate the arcsine of -.5 using full expression:

def output(x) = asin(-.5, x)
//output> -0.5235987755982989

Confirm that 1.5707963267948966 is the arcsine of 1:

def output = asin(1, 1.5707963267948966)
//output> ()  //() = true

See Also

sin, cos, acos, asinh, acosh, sinh, cosh, and haversine.

asinh

asinh[x]
asinh(x, ash)

Hyperbolic arcsine. ash is the hyperbolic arcsine of x.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Hyperbolic sine of ash. Must be grounded.
ashFloating[64]Hyperbolic arcsine of x.

Explanation

Hyperbolic arcsine is sometimes called “inverse hyperbolic sine.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the hyperbolic arcsine of 10:

def output = asinh[10]
//output> 2.99822295029797

Calculate the hyperbolic arcsine of -1 using full expression:

def output(x) = asinh(-1, x)
//output> -0.881373587019543

Confirm that 2.99822295029797 is the hyperbolic arcsine of 10:

def output = asinh(10, 2.99822295029797)
//output> () //() = true

See Also

sin, cos, asin, acos, acosh, sinh, cosh, and haversine.

atan

atan[x]
atan(x, at)

Arctangent. at is the arctangent of x in radians.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Tangent of at. Must be gounded.
atFloating[64]Arctangent of x.

Explanation

Arctangent is sometimes called “inverse tangent.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the arctangent of π/4:

def output = atan[pi_float64/4]
//output> 0.6657737500283538

Convert degrees to radians and calculate arctangent using full expression:

def x = deg2rad[90]
def output(at) = atan(x, at)
//output> 1.0038848218538872

Confirm that 0.6657737500283538 is the tangent of π/4:

def output = atan(pi_float64/4, 0.6657737500283538)
//output> () //() = true

See Also

tan, atan2, cot, acot, tanh, and atanh.

atan2

atan2[y, x]
atan2(y, x, at)

Arctangent. at is the arctangent of the quotient y/x in radians.

Parameters

ParameterTypeDescription
yFloating[64], SignedInt[64], UnsignedInt[64]yy coordinate of the 2D point (x,y)(x, y). Must be gounded.
xUnsignedInt[64], SignedInt[64], Floating[64]xx coordinate of the 2D point (x,y)(x, y). Must be gounded.
atFloating[64]Arctangent of x.

Explanation

Arctangent is sometimes called “inverse tangent.” The parameters x and y can be thought of as the xx and yy coordinates of the 2D point (x,y)(x, y).

Examples

Calculate the arctangent of 50:

def output = atan2[100, 2.0]
//output> 1.550798992821746

See Also

atan, tan, cot, acot, tanh, and atanh.

atanh

atanh[x]
atanh(x, ath)

Hyperbolic arctangent. ath is the hyperbolic arctangent of x.

Parameters

ParameterTypeDescription
xFloating[64], SignedInt[64]Hyperbolic tangent of ath. Must be grounded.
athFloating[64]Hyperbolic arctangent of x.

Explanation

Hyperbolic arctangent is sometimes called “inverse hyperbolic tangent.”

Only 64-bit float and 64-bit integer values for x are supported.

Examples

Calculate the hyperbolic arctangent of -.7:

def output = atanh[-.7]
//output> -0.8673005276940532

Calculate the hyperbolic arctangent of .7 using full expression:

def output(x) = atanh(.7, x)
//output> 0.8673005276940532

Confirm that -0.8673005276940532 is the hyperbolic arctangent of -.7:

def output = atanh(-.7, -0.8673005276940532)
//output> () //() = true

See Also

tan, atan, atan2, cot, acot, tanh, and atanh.

auto_number

auto_number[R]

DEPRECATED

This function is deprecated and should be avoided. It will be removed soon.

AutoNumber a relation.

auto_number takes a relation R(xs...) and produces a relation R(xs..., i) where i is a distinct AutoNumberValue index.

Note that auto_number can give different results each time it is called.

Example:

def sample = 'a'
def output = auto_number[sample]
('a', AutoNumberValue(0x12))
 
def sample = {'a'; 'b'; 'c'}
def output = auto_number[sample]
('a', AutoNumberValue(0x132))
('b', AutoNumberValue(0x133))
('c', AutoNumberValue(0x134))

AutoNumber

AutoNumber(x)

DEPRECATED

This function is deprecated and should be avoided. It will be removed soon.

Test if the given value is an AutoNumberValue.

average

average[R]
average(R, m)

The average (arithmetic mean) of a relation R. average is an alias for mean. For details, see the docstring for mean.

bigint

bigint[i]

Create a BigInteger value from the given integer.

Examples:

string[factorial[bigint[50]]] = "30414093201713378043612608166064768844377641568960512000000000000"

bigint_int64_convert

Convert a BigInteger to an Int64.

Examples:

bigint_int64_convert[bigint[50]] = 50

bitwise_and

bitwise_and[x, y]
bitwise_and(x, y, z)

Bitwise and of two integers.

Parameters

ParameterTypeDescription
xSignedInt, UnsignedIntLeft operand. Must be grounded.
ySignedInt, UnsignedIntRight operand. Must be grounded.
zSignedInt, UnsignedIntThe bitwise and of x and y.

Not all numeric values can be mixed with each other. The following combinations work:

xyz
SignedInt, UnsignedIntSame as x.Same as x.
SignedInt, UnsignedIntSignedInt[64], UnsignedInt[32]Same as x.

Examples

Bitwise and of 3 and