Curried Predicates in Prolog

Prolog is not a functional language – Not yet, at least. Sometimes, that’s a shame, especially when you want to use meta-predicates such as “forall” or “findall”, and use a predicate with partial arguments as the goal – As you would have done in functional programming languages.

Luckily, that specific scenario is actually possible – In SWI-Prolog, at least. The “call” meta-predicate, and its derivatives, may take a functor and extra arguments as parameters. Then, the extra arguments are concatenated to the arguments of the functor, and the resulting functor acts as a goal.

If this is all Prolog-Chinese for you, here’s an example:

Consider the following predicate:

is_child_of(Parent, Child) :-
  some_conditions...

It succeeds if the second argument unifies with a child of the first argument – Whatever that means.

Now, suppose you have a list of elements, and you want to find only those who are children of “foo”:

apply:include(is_child_of(foo), List).

There are many more ways to do this, but you get the idea: “call(is_child_of(X), Y)” is the same as “call(is_child_of(X, Y)“, which is the same as running “is_child_of(X, Y)“.

Addendum

Why did I said that Prolog is not yet a functional programming language? Because there is currently a debate in the SWI-Prolog mailing list about introducing lambda expressions as a part of Prolog. Paulo Moura even went ahead and introduced it as a part of Logtalk (see this section). There’s also Ulrich’s lambda library. Some people don’t really approve of this, but hey, no language feature is perfect.

Posted Friday, December 4th, 2009 under Programming.

Tags:

Leave a Reply

Security Code: