I was browsing my overgrown to-do list in search for low-hanging fruits when I noticed a link to this talk with a tag “Prolog” that has been sitting there for over a year.
It turned out to be an amazing talk by Aja Hammerly. It gives you a perfect first glance at Prolog and it inspired me to play with it a little bit myself.
The following is a transcript of me trying to get an idea of what it’s all about.
This one is pretty straightforward. I chose SWI-Prolog. To install SWI-Prolog from brew run this:
Once installed you can launch Prolog REPL with
swipl. As with Haskell you probably want to run it from the directory where your code is. To load file
. marks the end of expression).
I chose list manipulation as my guinea pig following the steps of Learn You a Haskell for Great Good. I believe that the way language works with lists is a good indicator of language’s philosophy in general, so here we go.
First and simplest - let’s get a first element of the list. In Haskell you would write it like this:
Very similar in Prolog:
Few things can be observed from this function. Prolog’s variables start with capital letters. Prolog’s “functions” - facts or predicates - start with lower case letters. Prolog, as Haskell can pattern match head and tail of a list. And as in other languages underscore marks a placeholder. Note that in Prolog if predicate contains multiple underscores each one is a separate variable.
In functional languages list manipulation naturally leads to recursion. So let’s see how would a predicate checking if something is a member of a list would look.
It looks innocent enough but let’s see how we can use it.
That’s what I would expect. However
member can be used to find all members of the given list:
member can be used to find all lists that have given argument as an element:
In this case that would be an infinite number of lists with
1 in the first position, the second, etc.
Prolog can do this because of it’s way of pattern matching is different to what is used in Haskell or Erlang.
This question on Stack Overflow is where I got all my knowledge from.
In short - Prolog uses unification while Haskell uses one-way pattern matching. In practice it means that while in Haskell unbound variables can only occur on a left side of an expression, in Prolog they can be used on both sides.
Another important difference is pattern matching in Prolog tries to evaluate all possible solutions while in Haskell pattern matching stops when the match is found.
That’s why Prolog doesn’t stop after the first line of our definition of
But tries to evaluate every definition of the predicate.
I found that it introduces the whole new way of thinking about the problem. I spend several hours trying to think of the way to generate all possible pairs in the given list when it hit me:
As simple as that. In her talk Aja Hammerly shows how one can use Prolog to model logic circuits or solve logic puzzles and it really shines when it comes to problems like these. If you can think of a good problem for logic programming leave a comment!comments powered by Disqus