typedb
typedb copied to clipboard
Warn singleton variables in rule bodies
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 nitro
s 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