How do I use rules in TypeQL to determine the subset relationship between sets?

I have two entity type, one is ‘person’ and the other is ‘hobby’. I want to use a rule to create a new relation. This relation should satisfy the condition: it should determine whether the hobbies of any one person are a superset of the hobbies of another person。
i try to implement it use such logic:
“Person1 has all the hobbies of Person2, and Person1 has at least one hobby that Person2 does not have, while Person2 does not have any hobbies that Person1 lacks. However, this cannot be implemented as it prompts that nested ‘not’ syntax is not supported.”

Hi Gavin,
A workaround we’ve seen before is to introduce a rule to express the nested negation.

So the query:


# $p2's hobbies are a subset or equal of $p1's
not {
  $p2 has hobby $h;
  not {
    $p1 has hobby $h;
  }; 
};
# $p1 has a hobby $p2 doesn't.
$p1 has hobby $h1;
not {$p2 has hobby $h1;};

Would get translated to a rule + a query

# rule:
rule non-empty-diff:
when {
  $pL has hobby $h;
  not {
    $pR has hobby $h;
  };  
}  then {
   (left  $pL, right: $pR ) isa non-empty-diff; # left \setdiff right
}

# query:
$p1 isa person; $p2 isa person; #...
not { ($left: $p2, right: $p1) isa  non-empty-diff; }; # $p2 has no hobbies that $p1 doesn't .
($left: $p1, right: $p2) isa  non-empty-diff; # $p1 has atleast one hobby $p2 doesn't. 

(Please check if it’s correct, since my TypeQL has become a bit rusty.)

thks, krishnan。My current solution is also based on using rules and match queries, similar to your approach. However, from my perspective, since TypeDB is an inference engine, wouldn’t it be better if it natively supported judgments about collections of relations? For instance, supporting ‘nested not’ syntax? This is just my personal opinion and is for your reference. Thanks again.

I agree about 'nested-not’s. Supporting those is something we plan to do in the 2.26 release.

1 Like

so happy to hear that

hi,krishnan,sorry to bother you again。i got some question。Is there a plan for TypeDB to support recursion, such as enabling multi-level structure inference through rules? Consider this scenario: whether there is an inclusion relationship between the animals that people like. For example, Person A likes dogs and cats, while Person B likes cats. In this case, it’s considered that A and B have an inclusion relationship in their preferences. This rule is straightforward, but in real life, it’s often the case that one person likes mammals, while another person likes specific animals like cats and dogs. Since mammals include cats and dogs, this situation should also be considered an inclusion relationship in preferences. The problem arises when the animal category has a hierarchical structure. Mammals can be further divided into many sub-levels, making it unclear how to implement this in TypeDB. How can this be achieved?