Skip to content
  • Rel
  • Basic Syntax

Rel Primer: Basic Syntax

An introduction to the basic syntax of Rel.


This Rel Primer is an introduction to the main features of Rel. It assumes basic knowledge of database concepts and programming.

Rel is RelationalAI’s language for building models and querying data. Rel is inspired by the relational calculus, the Datalog and HiLog programming/query languages, and a number of modeling formalisms and tools, such as Alloy.

Rel has a simple but powerful syntax designed to express a large set of relational operations, build declarative models, and capture general domain knowledge, including, in particular, Knowledge Graphs.


Everything in Rel is a relation. Rel imports data into relations, combines relations to define new ones, and queries relations to get answers back — in the form of new relations.

Relations are sets of tuples.

A tuple is a list of elements, where the order matters. It corresponds to a row in a traditional database. The elements are the basic objects in the database — for example, numbers, strings, symbols, or dates. A tuple may be specified in Rel by separating its elements with commas (,) and enclosing the list of elements in parentheses (()).

For example, ("Neymar", "PSG") is a tuple with two elements which represent the name of a soccer player and the club he currently plays for. ("store8", "product156", 12.99) is a tuple with three elements which represent a store ID, a product ID, and the price of the product at that store. Note: Rel, uses normalized relations to represent data.

A relation may be defined explicitly by using semicolons (;) to separate tuples and curly braces to enclose them:

// query
def players  = { ("Neymar", "PSG"); // single-line comment
                 ("Messi", "BFC");
                 ("Werner", "Chelsea");
                 ("Pulisic", "Chelsea")
   This is a multi-line comment in Rel.
def output = players
Loading players...

Note that the order of the tuples does not matter, since relations are sets of tuples. The order of the elements inside each tuple, in contrast, can matter a lot.

Rel chooses a default order to display the tuples in the relation, which may be different from the order used to write the relation down, as seen in this example.

You should not assume that tuples will appear in any particular order; but you can use utilities like sort, top and bottom to sort and rank them.


By convention, our Rel query examples usually display the contents of the output relation. This is the same behavior that RAI Notebook query cells have — where you can also enter a single stand-alone Rel expression and get the result.

Arity and Cardinality

If you think of relations as tables, the arity of a relation is the number of columns, and the cardinality is the number of rows.

Single Elements Are Relations

In Rel, a single elements is identified with a relation of arity 1 and cardinality 1. For example, the number 7 is the same as the relation {(7)}. Curly braces are not required for single-element relations; so {4} and 4 are the same relation, with arity 1 and cardinality 1.

The constants true and false are also relations, of arity 0. There are only two of these: false is {} (the empty relation with arity 0), and true is {()}, that is, the relation with one empty tuple (arity 0 and cardinality 1). This might seem strange at first, but will play quite nicely with other features of the language, as we will see below.

This way, everything is a relation, including the results of arithmetical operations (+, -, *, etc.) and boolean operations (and, or, not, etc.).

Relations Are Sets

As we have mentioned a few times already, Rel relations are sets of tuples. This means that order does not matter and duplicates are ignored — we only keep one of each different tuple. Thus, for example, {1; 1; 1} is the same unary relation as {1}. And {(1, 2); (3, 4); (1, 2)} is the same binary relation as {(3, 4); (1, 2)}.

This is important when computing aggregations, as explained in the group-by section in the next part of this Primer.

Thinking of relations as tables again, this means that, unlike columns, the order of the rows does not matter, and there are no duplicate rows.

The parser allows but does not require parentheses and brackets when there is no ambiguity. So we can write:

// query
def output = 1, 2 ; 3, 4
Loading one-two-three-four...

In Rel, relations are sets of tuples; they don’t have duplicates.

() as a Membership Test

For a relation r, the expression r(x) may be seen as testing set membership, and will hold exactly for those x in r. In general, r(t1,...,tn) is true if and only if (t1,...,tn) is a tuple in r.

Furthermore, r can be any expression that evaluates to a relation. For example:

// query
def output(x) = {1; 2}(x) or {2; 3}(x)
Loading membership-test...

Base Versus Derived Relations

In our documentation, small relations with sample data are often built into the Rel code, as in the def players = {...} definition above. Typically, though, data will be stored on disk, as relations, often representing “raw data” from outside sources, such as JSON or CSV files. Once written to disk, no definitions are necessary for these relations, which we call stored relations. In contrast, relations defined by rules are derived relations, also known in the database world as views.

In the Datalog world, stored relations are known as base relations, and derived ones as derived relations.

Derived relations are transient, local only to a query, unless (1) we write their results back to disk, creating new stored relations, or (2) we install their definitions (hence the term, “installed view”).

Database logic will be a set of rules. Each derived relation can refer to other derived relations, relations defined in libraries such as the stdlib, and base relations.

Some quick things to know about rules (defs):

  • Their order does not matter.
  • Definitions are unioned, so they add up. If we have:
// query
def myrel = {}
def myrel = 1; 2
def myrel = 2; 3

then myrel will be {1; 2; 3}.

Schemas Are Optional

Rel does not require pre-defined schemas. When you create a base relation or a derived relation in Rel, the system automatically tracks the arity and type of the tuples in the relation. Unlike traditional database systems, you do not have to specify this information beforehand as a “database schema.”

You can, however, enforce schemas if you want, in the form of integrity constraints that restrict the arity and types of relations, for instance — see the IC concept guide for examples.

Uses of , and ;

Both , and ; are operators in their own right, and it is good to familiarize yourself with the multiple ways they can be used.

Tupling, Filtering, Conjunction and Products (,)

The , operator can:

  • make tuples
  • serve as a boolean filter (in analogy to “and”)
  • denote cross-products

In the following example, , gives the cross-product of relations {1; 2; 3} and {4; 5}, where 4 or 5 is appended to each of {1; 2; 3}:

// query
def output = {1; 2; 3}, {4; 5}
Loading cross-product-count...

The cross-product of {1} and {2} is {(1, 2)}, so we can see tupling as a special case of cross-product.

Taking advantage of true and false being relations, , can also be used as a boolean filter, analogous to an and operation:

// query
def myelements = 1; 2; 3; 4; 5; 6; 7; 8; 9
def output(x) = myelements(x), x > 3, x < 7
Loading comma-as-boolean-filter...

This works because true is the relation {()} (arity 0 and cardinality 1), and false is the relation {} (arity 0 and cardinality 0). Any cross-product with {} will be {}, so R, false is always false. And the cross-product of any relation R with {()} is R, hence R, true is always R.

We can also use the usual connectives and, or, implies, not — but, unlike the comma operator, they always expect arity 0 (true or false). We will return to this later.

Union and Disjunction (;)

While , builds tuples and can concatenate them, the ; operator builds relations, and can union them:

// query
def output = 2; {2;2;3} ; {3;3;4}
Loading union-and-disjunction-1...
// query
def rel1 = (1, 2); (4, 8); (3, 6)
def rel2 = (3, 6); (2, 4)
def output = rel1; rel2
Loading union-and-disjunction-2...

The ; operator can be thought of as a disjunction (“or”), and it behaves exactly as or for arity 0 (boolean) expressions: true or false is true, false or false is false, and so on. While or works only for arity 0 expressions, the ; operator also works with higher arities, and still represents a choice. For example, {1; 2}(x) serves as 1=x or 2=x here:

// query
def output(x, y) = {1; 2}(x) and {4; 5; 6}(y) and x + y < 7
Loading union-and-disjunction-3...


We have already seen a few examples of relation definitions (def’s) in Rel, and their meaning should be intuitively clear. A Rel model is a collection of such definitions (called “Rel sources”), which can be defined in terms of each other, even recursively. The RAI query engine answers queries by combining these definitions with known data to compute a requested result. The query comes in the form of a relation, often called output, that we want to compute.

The order in which the definitions are written down is not relevant. For example:

// query
def output(x) = odddata(x) and x > 5
def mydata = {1; 2; 3; 4; 5; 7; 8}
def odddata(x) = mydata(x) and x%2 = 1
def mydata = {9; 10; 11}
Loading definitions...

Here, the two separate definitions for mydata are unioned, and it is not a problem that output goes first, or that odddata is introduced before mydata, on which it depends.

Relational Abstraction (:)

Recall that a relation is a set of tuples. One way to describe a relation is to write down a formula that is true exactly for those tuples in the relation and false for all others.

We can see this most clearly in definitions that introduce a variable for each column in the left-hand side of the definition, as in def myrel(x1, x2, ..., xn) = ... and then give a boolean expression on the right that constrains those variables:

// query
def mydomain(x) = range(1, 7, 1, x) // x will range over 1; 2; 3; 4; 5; 6; 7
def myrel(x, y) = mydomain(x) and mydomain(y) and x + y = 9
def output = myrel
Loading relational-abstraction...

Note: The Rel standard library has many utilities including the one used here: range(low, high, stride, x) constrains x to range over all the elements between low and high, inclusive, skipping by stride each time.

This style is preferred, since it leads to more readable definitions. However, relations can also be specified using relational abstraction, indicated by : in Rel. The definitions above are equivalent to:

// query
def mydomain = x : range(1, 7, 1, x)
def myrel = x, y : mydomain(x) and mydomain(y) and x + y = 9
def output = myrel
Loading relational-abstraction-2...

This use of : is similar to standard mathematical notation to describe a set. There are variables on the left of the :, a boolean condition over those variables on the right, and the set contains all the combinations of elements for those variables (tuples) which make the condition true.

But in Rel, : can do much more. The expression Expr in <vars> : Expr does not have to be a boolean formula (that is, it can have arity greater than 0). If Expr has arity nn, the corresponding elements from Expr are appended to <vars>, and the total arity will be the number of variables in vars plus nn. For example:

// query
def mydomain = x : range(1, 4, 1, x)
def myrel = x : x * 5, mydomain(x)
def output = myrel
Loading relational-abstraction-3...

This is similar to how , works. For both : and ,, when the right-hand side is a boolean expression (has arity 0), it works as a filter. If the right-hand side has arity greater than 0, then the expression is appended to the tuple.

This gives us a way to do group-by aggregations, as we will see later in the group-by section.


The arity of bindings : expression is the number of variables in bindings plus the arity of expression.

Style Note: When we define a relation using the def myrel = x1, ..., xn : Expr style, the arity of myrel depends on the arity of the expression on the right (Expr), which might not be clear at first sight. We just know that myrel will have arity of at least nn. In contrast, the def myrel(x1, ..., xn) = ... definition explicitly calls out the arity of the relation — exactly nn — which is why we prefer it.

Binding Shortcuts: in and where

It is often useful to put filters in the left-hand side of : — which we call the bindings. For this, we can use in and where, which can be used directly wherever variables are introduced, to restrict the domain of the variables. For example:

// query
def mydomain(x in range[1, 7, 1]) = true // same as: def mydomain = range[1, 7, 1]
def myrel = x in mydomain, y in mydomain where x + y = 9 : x * y
def output = myrel
Loading in-and-where...

Note that while in restricts the domain of a single variable, where can restrict combinations of variables.


Note: in (and its equivalent symbol, ) is not a standalone boolean predicate, and can be used only in bindings. If you want to express x in R in a boolean formula, just write R(x).

We can already define many different kinds of relations using the machinery we have seen. Next we introduce a feature that is widely used in Rel to make our code more concise.

Partial Relational Application ([])

Relations that represent functions often have their inputs first and the results second. Similarly, database tables are often defined with keys first and values second. It is natural, then, and very useful, to have a notation that easily identifies the latter based on the former.

Square brackets restrict a relation to a certain prefix, and then remove the prefix (project, in relational parlance). For example, myrel["a", "b"] indicates all the tuples in myrel whose first element is "a" and second element is "b".

This is what we mean by partial relational application. For example:

// query
def myrel = {(1, 2) ; (1, 4); (3, 6)}
def output = myrel[1]
Loading relational-abstraction-brackets...

Here’s another example, where we fix and then remove the first two columns:

// query
def myrel = {(1, 2, 3, 4); (1, 2, 6, 7); (1, 3, 10, 11)}
def output = myrel[1, 2]
Loading relational-abstraction-brackets-2...

The expression myrel[x, y] is equivalent to myrel[x][y].

As an example from the Rel Standard Library, the function add is a ternary relation, where add(x, y, z) holds if z is the sum of x and y. You can write add[x, y] to indicate the sum of x and y; the expression add[1, 2] fixes x and y and evaluates to a single-element relation containing all of values of z for which add(x, y, z) is true.

In this example, there is only one result, but this does not have to be the case, as you will see next.

[] is Not Always a Function

In many settings, [] looks like a function call — as in add[1, 2], or cos[2 * pi_float64] — but we should remember that the output might be empty, or not unique. In the example below, neighbor[x] can have zero, one, or two elements for each x:

// query
def mydomain(x) = range(1, 5, 1, x) // numbers from 1 to 5, inclusive
def neighbor(x in mydomain, y in mydomain) = (y = x + 1) or (y = x - 1)
def output = x in {2; 3} : neighbor[x]
Loading not-always-a-function...

In technical terms, [] is relational application, which is a generalization of functional application. (Functions are special cases of relations: they are relations where the values are uniquely determined by the keys.) Rel code that uses [] could always be written without it, by introducing new variables, but [] makes it more concise and natural to read.

Operator Distribution

If S is a unary relation, Rel can also evaluate R[S], which will be the union of R[x] over all the tuples x in S. For example,

def output = neighbor[{1; 3}]

will give {2; 4}, since neighbor[1] is {2} and neighbor[3] is {2; 4}.

Other operations distribute similarly. For example, 1 + {2; 3} is {3; 4}.

Note: In the expression r[S], the relation S must have arity 1 (unless r is a “higher-order” @inline def – see the advanced syntax Primer).

Using [] In Definitions

Square brackets can be used in the left-hand side of definitions, making them more concise, and often highlighting a functional dependency. For example:

// query
def f[x in {3}, y in {4; 5}] = x + y
def output = f
Loading brackets-in-definitions...

Here is another example:

// query
def myelements = (1, 2); (3, 4); (5, 6)
def output[x] = myelements[x], x > 1, x < 4
Loading brackets-in-definitions-2...

An equivalent query using logical connectives is:

// query
def myelements = (1, 2); (3, 4); (5, 6)
def output(x, y) = myelements(x, y) and x > 1 and x < 4
Loading brackets-in-definitions-3...

In this case, we prefer the second formulation, using and instead of ,, since it makes it easier to find typos and mistakes. If any of the formulas used with and has an arity greater than 0, we will get a compiler error. In contrast, in the previous formulation, the comma operator (,) will happily compute the cross product of any relations it gets, regardless of their arity.

  • When writing def myrel[v1,...,vn] = e, the arity of myrel will be n plus the arity of e.
  • This is also true of def myrel(v1,...,vn) = e, but in this case, e must have arity 0, and myrel has arity n.
  • When n is 0, you just have def myrel = e, and the arity of myrel is the arity of e.

We can use constants on the left-hand side:

// query
def output[1, 2] = 3, 4
Loading brackets-in-definitions-4...

An example that combines [], in, and , as a tuple constructor and filter:

// query
def mydomain = range[1, 5, 1] // numbers from 1 to 5, inclusive
def output[x in mydomain, y in mydomain] = x - y, x + y, x * y, x + y = 5
Loading brackets-in-definitions-5...

Caveat: Not all combinations of in and where are yet supported on the head of the def (that is, the left of the =), but we can combine them on the body (the right of the =). The above definition is equivalent to:

def output = x in mydomain, y in mydomain where x + y = 5 : x - y, x + y, x * y

() As a Special Case

() can be thought of as a special case of partial relational application, with remaining arity 0: The key is the full tuple, and the value is the empty tuple (()), that is, true.

Thinking About Arity

() Versus []

The Rel compiler checks that all the arities in the code match up and reports an error if they do not. When we write, say, myrel(x,y,z), we are indicating that myrel must have arity 3, and the result has arity 0 — which is the arity expected by the boolean connectives (and, or, implies, etc.), and means that it is either true or false.

Writing myrel[x,y,z] implies that the arity of myrel is at least 3, but it can, of course, be higher. The arity of myrel[x,y,z] is equal to arity(myrel) - 3.

If myrel has arity 3, we can still write myrel[1,2,3]; it will be equivalent to myrel(1,2,3) — either true or false.

Boolean Formulas Have Arity 0

Writing, say, {1} and p(x), or sin[x] or q(x), gives an arity error. The boolean connectives require arity 0 for both arguments, and their result has arity 0.


When we write myrel[1,2,3], we expect myrel to have arity of at least 3. If we write myrel(1,2,3), we require myrel to have arity of exactly 3.

Arity and Defs

When we write def myrel(a,b,c) = E, the arity of E must be 0 (it should have a boolean value), and the arity of myrel is 3.

When we write def myrel[a,b,c] = E, then E can have arbitrary arity, and the arity of myrel will be 3 plus the arity of E.

The Standard Library

Rel includes a Standard Library with many functions and utilities. For example, range, maximum, minimum, sin, cos, and many other mathematical operations. The stdlib also includes “higher-order” definitions that take relations as arguments, such as argmax, argmin, and first.


The query help[:name] — or, thanks to module syntax, just help:name — will display a brief docstring for each utility. For example, help:argmax, help:range, or help:min.


We are planning to encapsulate these utilities in modules, but for now, they are imported into the main Rel namespace. Therefore, strange things can happen if you add your own definitions for the same names. Some names to avoid: domain, function, total, first, last, equal. A quick way to check if a name is reserved is to try help:name.


This article has covered the basics of Rel syntax. The next one in this Rel Primer series focuses on aggregations, group-by and joins, followed by a Primer on more advanced Rel features.

Was this doc helpful?