typedb icon indicating copy to clipboard operation
typedb copied to clipboard

Warn singleton variables in rule bodies

Open krishnangovindraj opened this issue 2 years ago • 0 comments

Problem to Solve

I had the following rule:

   rule nitro-rule:
    when{
        $c isa atom_c; $n isa atom_n; $o1 isa atom_o;  $o2 isa atom_o;
        (participant: $n, participant: $o1) isa double_bond
        (participant: $n, participant: $o2) isa double_bond;
        (participant: $c, participant: $n) isa single_bond;

    }then{
        (member: $n, member: $o1, member: $o2) isa nitro;
    };

But... I had a type in my second double_bond, where I mistyped $o1 in place of $o2. This lead to a huge blow-up in inferred nitros as each wild atom_o lead to a new nitro.

Current Workaround

Stare at the code till I realise what I've done.

Proposed Solution

Prolog (or most implementations of it), report any variable that is used exactly once in the head/or body of the rule. Similarly, a warning for any variable that occurs only once in the body of a rule (or a more specific condition) would catch a few such mistakes. In cases where this was really the intention of the programmer, starting the variable name to start with the _ character causes prolog to recognise it as a singleton.

Additional Information

SWI-Prolog manual on singletons

krishnangovindraj avatar May 25 '22 08:05 krishnangovindraj