• Getting Started
• HELP
• Error Messages
• EXCEPTION - Ungrounded Variable

# Error code: EXCEPTION - Ungrounded Variable

This guide explains why you may experience an ungrounded variable error and how to solve it.

In Rel, a variable is grounded when it can be instantiated to a specific, finite set of values.

For instance, in the expression x: range(1, 100, 1, x), the variable x is instantiated with the values 1, 2, …, 100.

By contrast, a variable is not grounded or is ungrounded if the system is unable to determine a way to instantiate a finite set of values.

## Example: Infinite Possibilities

For example, in x: minimum(1, x, 1), there are infinitely many values of x for which minimum(1, x, 1) is true. In fact, this is the case in many relations defined in Rel Libraries, such as in the example above with minimum.

Consider another example that returns an ungrounded variable:

def my_string = substring["abcd", -2]
def output = my_string

In the example above, you tried to define the relation my_string as a substring of the "abcd" string, using the built-in substring relation. This is returned as an exception error thrown by the query optimizer.

Because this third argument is allowed to be larger than the length of the string, the number of valid values is infinite. To solve the problem in this case, another argument needs to be provided to substring that states where the substring should end. For example, the substring could end after the second character:

// query

def my_string = substring["abcd", -2, 2]
def output = my_string

## Example: Nonlinear Dependencies

Another reason an ungrounded variable error can occur is when nonlinear mathematical dependencies need to be resolved. In general, no closed form solutions exist for these nonlinear dependencies, making it impossible for the system to determine all valid values.

For example:

def output(x, y) {
x = 4
and y^2 = x
}

The code above leads to an ungrounded variable because the equation y^2=x is nonlinear. Even though in this simple case where the valid values for y are known to be 2 and -2, the system cannot yet solve them due to the nonlinear nature of the equation.

A solution here can be to rewrite the equation such that it is linear in y (if possible):

// query

def output(x, y) {
x = 4
and (y = sqrt[x] or y = -sqrt[x])
}

Or you can provide an explicit finite domain for y:

// query

def D = range[-10, 10, 1]

def output(x, y in D) {
x = 4
and y^2 = x
}
In this way, the mathematical equation turns into a filter and only (x, y) pairs that fulfill the equation are kept.