Prolog Partial Predicates

Prolog is homoiconic—a fancy word meaning our code is also data, so we can manipulate it as such. This means we can pass around predicates as if they were data and then call them. This has been the case since the very first Prolog, in which you could do this:

?-

Rather than leaving that X unbound, it’s best to make it explicit by wrapping it in a call predicate. Note: we’ve not given the arity for call because it varies from dialect to dialect, but you’ll generally find it sufficient.

One of the main advantages of call is that you can add additional arguments to a predicate with some arguments set, like so:

?-
?-

We’ve passed foo(A) and B around until we got to call(foo(A), B). This has called foo(A, B) to find the answer. Pure Prolog wizardry! In Tau Prolog (what’s running in your browser), we can have up to eight arguments passed to call.

If you’re coming from a functional language, you might think, “Oh, that’s just currying.” It’s not quite, but we can use it to solve the same kind of problems, such as when writing higher-order predicates like map, reduce, and filter.

All in all, call is incredibly handy, particularly when you don’t know what predicate will be called, when the required argument isn’t yet available, or when the same predicate will be called with many different arguments. However, depending on how your Prolog optimises, you may lose a performance benefit that comes from clever compile-time indexing, as call works at runtime.

Until next time, Happy Prologing!

Paul