# Error Code: `UNDEFINED`

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

In Rel, a variable is undefined if there’s no lexically enclosing declaration. Undefined errors occur mostly when:

• An expression contains an undefined variable.
• An expression refers to a relation that hasn’t been defined. Referring to relations within modules requires attention.

## An Undefined Variable

A variable v that occurs in an expression is defined when:

1. The expression is in the body of a definition, and v occurs in the head of the definition.
2. A binding in a relational abstraction defines `v`.
3. `v` is bound by a universal or existential quantifier.
4. `v` is existentially bound by `from`.

For example, the following will result in `UNDEFINED` errors:

``````def P = 1; 3; 5
def R(x) { x = y - 1 and P(y) }

def output = R``````

The most common way to fix the problem is to introduce the fourth case listed above:

``````// read query

def P = 1; 3; 5
def R(x) { x = y - 1 and P(y) from y }

def output = R``````

You could also use the third case, that is, explicit existential quantification, for example like this:

``````// read query

def P = 1; 3; 5
def R(x) { exists(y in P : x = y - 1) }

def output = R``````

The first case is also easy to introduce, but this will affect the arity of `R`:

``````// read query

def P = 1; 3; 5
def R(x, y) { x = y - 1 and P(y) }

def output = R``````

## An Undefined Relation

Consider the following example that returns an undefined error:

``def zoes_friends = friends["Zoe"] ``

In the query above, the relation `zoes_friends` defines Zoe’s friends. However, the relation `friends` hasn’t been previously defined.

Here’s one way to write the example correctly:

``````// read query

def friends = {("Mike", "Anna"); ("Zoe", "Jenn")}
def zoes_friends = friends["Zoe"]
def output = zoes_friends``````

This returns Zoe’s friends list.

It’s also possible that you’ve already defined the relation `friends` but the compiler isn’t able to find it. For instance, suppose you define the relation within a module:

``````module friends_mod
def friends = {("Mike", "Anna"); ("Zoe", "Jenn")}
end``````

If you then query `zoes_friends` as in the first example, the output still returns this error. You can think of Rel modules as nested relations. In the example above, the relation `friends` is nested inside `friends_mod` and available as `friends_mod[:friends]`, equivalent to `friends_mod:friends`.

Therefore, a valid definition of `zoes_friends` could be:

``def zoes_friends = friends_mod:friends["Zoe"]``

Similarly, say while installing some logic as a model, you refer to relations that you haven’t defined yet, but that you plan to define later in a different model or query. This returns an undefined error:

In this case, you can ignore the error as long as you provide the definition later. See Working With Models for more details.