Variables - scopes, binding & optionality

Schema for the examples
#!test[schema]
define
attribute name, value string;
attribute age, value integer;
attribute start-date, value date;

entity person, owns name, owns age;
entity company, owns name;

relation friendship, relates friend @card(2), owns start-date;
relation employment, relates employee, relates employer;
person plays employment:employee, plays friendship:friend;
company owns age, plays employment:employer;

Scopes

Patterns comprise statements containing variables. Given a TypeQL pattern, the scope of a variable is:

  • the outermost conjunction in which the variable appears.

  • If the variable appears in all branches of a disjunction, it counts as appearing in the parent conjunction.

  • If a variable is scoped to the root conjunction of a stage, it appears in the root conjunction of the next stage.

A key principle of valid TypeQL pattern is:

Every variable must have a unique scope.

Hence, having disjoint blocks in which a variable appears is an error.

Input, Bound & Internal variables

A variable can be either bound by or input to a constraint or subpattern. This is called the "binding mode" of a variable. Additionally, we call variables internal to a subpattern if it is not visible outside the subpattern.

Constraints

A variable is input to a constraint if it must be bound to some value before the constraint is evaluated. Any variable which isn’t an input is bound by a constraint when it is evaluated.

Most constraints bind all the variables involved. The exceptions which are input variables are:

  • Both variables in a comparison

  • Both variables in an is statement

  • All variables in the rhs of an expression assignment.

  • All arguments to a function call.

Conjunctions

A variable is input to a conjunction if it is input to some constraint or subpattern in it, and not bound by some other constraint or subpattern in it.

Subpatterns

The definition for variables which are input or bound by subpatterns is similar, but the rules to determine the variable’s binding mode is more involved.

  • Negations

    • A variable that is scoped to a parent of the negation (i.e. not internal) is input to the negation.

    • A variable that is scoped to a negation is internal to the negation.

    • Hence, no variables are bound by the negation.

  • Optionals

    • A variable that is scoped to a parent of the optional is input to the optional.

    • A variable that is scoped to an optional must be bound by the optional.

    • Hence, no variables are internal to an optional (except those internal to subpatterns of the optional)

  • Disjunctions

    • A variable is bound by a disjunction if it is bound by all its branches.

    • A variable is input to a disjunction if it appears in some but not all branches OR is input to one of the branches.

    • A variable that is scoped to a branch of a disjunction is internal to that branch.

The semantics of optionals and negations depend on whether a variable is input to them or not. Disjunction inputs have less to do with semantics and more to do with evaluation. In all cases, an input variable which cannot be satisfied is a compile-time error.

Optionality

When an optional block fails, it returns a single row with all the non-input variables bound to none value. Using an optional variable in a constraint which is not defined for the none value is invalid and results in a compile-time error.

isset

An isset constraint can be used to assert that a variable is not none. isset $var; fails if $var is none, leaving only rows where $var is not none - safe for use in constraints.

#!test[read]
match
    $p isa person;
    try { $p has age $age; };
match
    try { isset $age; let $double-age = 2 * $age; };

Here, $double-age will hold double of $age if $p has one. Else both $age and $double-age will be none

Short-circuiting expressions

It is common to want to optionally compute a value involving optional inputs. One way to do this is using a try as above. Alternately, the ? operator can be used to "short-circuit" the expression when an optional-variable is none.

#!test[read]
match
    $p isa person;
    try { $p has age $age; };
match
    let $double-age = 2 * $age?;

As before, $double-age will hold double of $age if $p has one. Else they will both be none.

Short-circuiting fetch expressions

The ? operator can also be used to short-circuit fetch expressions.

#!test[read]
match
    $p isa person;
    try {
        $_ isa friendship, links (friend: $p, friend: $q),
            has start-date $since;
    };
fetch {
    "p": { $p.* },
    "q": { $q?.* },
    "since": $since?,
};

Examples: Scopes & Binding modes

Example 1: Scopes

In the following, the scope of the variable $p is the root. The scope of $n is also the root as it occurs in both branches. $a is scoped to the second branch of the disjunction.

#!test[read]
match
    $p isa person;
    {
        $p has name $n;
        { $n == "John"; } or { $n == "James"; };
    } or {
        $p has name $n, has age $a;
        { $a < 26; } or { $a > 60; };
    };

For the following example, the variable $p and $a are both scoped to the root.

#!test[read]
match
    { $p isa person; $p has age $a; } or
    { $p isa company; try { $p has age $a; }; };

Example 2: Disjoint scope errors

The following is an error because $p has two different scopes - The first and second branches of the disjunction.

#!test[read, fail_at=runtime]
match
{ $p isa person; } or { $p isa company; } or { $q isa person; };

This is fine because $p is visible at the root, since it is in both branches of the second disjunction.

#!test[read]
match
{ $p isa person; } or { $p isa company; } or { $q isa person; };
{ $p has name "Steve"; } or { $p has age 20; };

The following is also fine, as the $p is visible in the root of the first match, and hence visible in the root of the second match. Thus, its scope in the second match is also the root.

#!test[read]
match
    { $p has name "Steve"; } or { $p has age 20; };
match
    { $p isa person; } or { $p isa company; } or { $q isa person; };

Example 3: Variable scopes & naming across stages

The following is an error as the variables $n and $a have multiple scopes.

#!test[read, fail_at=runtime]
match
$p isa person;
{ $p has $name; } or { $p has $age; };
{ $name isa name == "Steve"; } or { $age isa age == 10; };

The following example is no longer an error, but means something very different. Despite having the same name, The two usages of $name actually refer to two different variables (as do the two usages of $age).

#!test[read]
match
$p isa person;
    { $p has $name; } or { $p has $age; };
match
    { $name isa name == "Steve"; } or { $age isa age == 10; };

Example 4: Variable scopes & block inputs.

The following query tries to find two friends who are not employed. Here, The scope of $c is the root - making it an input to either negation. However, there is no constraint which can bind $c in the root and the negations have an unsatisfied input.

#!test[read, fail_at=runtime]
match
$f isa friendship, links (friend: $p, friend: $q);
not { employment (employer: $c, employee: $p); };
not { employment (employer: $c, employee: $q); };

Similarly, the following pattern also fails because $c is an unsatisfied input to the optional blocks.

#!test[read, fail_at=runtime]
match
$f isa friendship, links (friend: $p, friend: $q);
try { employment (employer: $c, employee: $p); };
try { employment (employer: $c, employee: $q); };

The correct query to find two unemployed friends uses different names for the company in the two not blocks.

#!test[read]
match
$f isa friendship, links (friend: $p, friend: $q);
not { employment (employer: $c1, employee: $p); };
not { employment (employer: $c2, employee: $q); };

Introducing $c outside the negations would make it an input to either, and returns (friendship, company) pairs where neither friend works at $c

#!test[read]
match
$f isa friendship, links (friend: $p, friend: $q);
$c isa company;
not { employment (employer: $c, employee: $p); };
not { employment (employer: $c, employee: $q); };