Example of refactoring code to SSU

Prolog has Single Sided Unification (SSU). If the code is determinate then one should consider refactoring the code to SSU. When refactoring code to SSU not all changes are straightforward.

Here is an patch showing parts of Red-Black trees being refactored to use SSU.

This is a follow-on to an earlier refactoring to use SSU here: ENHANCED: library(rbtrees): error checks and more test cases. · SWI-Prolog/swipl-devel@0ad9803 · GitHub

… and which introduced a bug in is_rbtree/1 – the lesson is that with SSU you must be careful to add “fail” rules that are implicit with “:-” clauses.

1 Like

Which is unlike to what happens in Picat. In my experience demanding a rule to match is exactly what makes our => rules so good at improving reliability of code and the debugging experience. If the failure is not obvious I rerun the program after using

?- gtrap(_).

This often traps the debugger on the errornous goal. Go up a few frames, use retry and it is typically easy to find what is wrong.

1 Like