Officially out now: The TypeDB 3.0 Roadmap >>

NOT (Negation)

A negation of a pattern requires that pattern to be false. A negation is itself a pattern.

Syntax

A negation of a pattern is of the form:

Syntax
not "{" <pattern> "}" ;

where <pattern> can be any valid pattern. Note that at least one variable of <pattern> must be bound outside the negation.

Behavior

A negation is true, if its negated pattern is false. Negations can be used in a match clause or a condition of a rule to set a constraint of not matching the negated pattern.

When using negations in rules, complicated logical behavior can arise: we use the theory of stratified negation to compute the behavior of patterns in these cases.

Negation of statement

For this example, use a database with the IAM schema and sample data loaded.

The most direct approach to using negation is to specify a pattern for all results that need to be excluded.

Let’s try to get all users except those who have a full-name attribute with a particular value:

Negation exclusion example
match
$u isa user;
not {$u has full-name "Kevin Morrison";};
fetch
$u as "Not Kevin": full-name;
See example output
Output example (partial)
{
    "Not Kevin": {
        "full-name": [ { "value": "Bob Fake", "type": { "label": "full-name", "root": "attribute", "value_type": "string" } } ],
        "type": { "label": "person", "root": "entity" }
    }
}

Negation in rules

For this example, use a database with the IAM schema and sample data loaded.

Negation can be used in a rule condition in the same way as it can be used in a match clause.

There is no need to negate the conclusion of a rule, as the rule will not repeat itself if there is nothing to change.

Let’s create a rule to create complementary valued attributes for the validity of permission relations:

Negation in a rule example
define
rule permission-non-validity: when {
    $permission isa permission;
    not { $permission has validity true; };
} then {
    $permission has validity false;
};

Negation with cycles

The following example yields an error!!

In contrast to the previous example, we cannot write a rule of the following form:

Negation in a rule example
define
rule give-all-permissions: when {
    $permission isa permission;
    not { $permission has validity true; };
} then {
    $permission has validity true;
};

The rule creates a logical cycle, which violates the principles of of stratified negation.

Learn more

Learn more about conjunction in TypeQL.

Learn more about disjunction in TypeQL.

Learn more about statements in TypeQL patterns.