Very neat! I’d love to try taking a crack at making your predicate work in other modes, but I’m afraid I don’t know the problem domain well enough. Have you looked at the SWI-Prolog Discourse? There are lots of very helpful folks there.
Awesome, thanks for the link!