UNBOUND VARIABLE

# Error Code: `UNBOUND_VARIABLE`

This guide explains why an unbound variable error may occur and describes how to fix it.

A variable in Rel is considered to be unbound if there’s no known source of values for it. The difference between ungrounded and unbound variables refers to how the system detects the error, i.e., the system reports unbound errors when the problem can be detected by applying purely syntactic criteria. In this sense, unbound implies ungrounded. The unbound errors come from the front-end compiler, whereas the ungrounded ones come from the query optimizer.

The following example returns an unbound variable error:

``def r(x, y) = {x = 1}`` This example defines the relation `r` and declares the value for the variable `x`. However, it doesn’t declare the value for the variable `y`, and therefore the system can’t evaluate it. Here’s how to write the example correctly:

``````// read query

def r(x, y) = {x = 1 and y = 2}
def output = r``````

Specifying a finite domain for `y` would also work, if that is what you want:

``````// read query

def P = 1; 3; 5
def r(x, y in P) = {x = 1}

def output(x, y) = r(x, y)``````

The compiler reports an unbound error when it cannot determine the type of a variable. In those cases you can suppress the error by explicitly providing a type.

For example, the following example results in a number of error messages:

``````def P[a, 0] = 1
def P[a, 1] = a

def output = P[3, 1]``````

You can fix the problems by specifying a type:

``````// read query

def P[a in Int, 0] = 1
def P[a in Int, 1] = a

def output = P[3, 1]``````

You can achieve the same effect by specifying a more limited domain:

``````// read query

def Domain = 0; 1; 2; 3; 4

def P[a in Domain, 0] = 1
def P[a in Domain, 1] = a

def output = P[3, 1]``````

See Grounded Variables in the Rel Language Reference manual and Grounded and Ungrounded Variables in the Rel Primer: Advanced Syntax guide for more details.