Prolog is not that hard
[Target audience: programmers.]
Prolog is not hard. It’s only different. :)
Today I want to show you, quickly, how Prolog works. I believe it’s a positive thing for programmers to know various programming paradigms.
Advantages of learning Prolog
- It’s nice to understand another approach to problems solving.
- There are problems in this world that fits Prolog perfectly.
- It helps you to understand the “awkward” syntax of Erlang. :)
The truth is out there. On that file.
In Prolog, you shouldn’t be too interested on how things will behave, but what is the truth about the problem you have in hands right now. That’s why Prolog rely, basically, on facts.
Let’s begin with a simple list of facts:
awesome(linux).
awesome(freebsd).
awesome(openbsd).
sucks(windows).
What we’re saying is: linux is awesome, freebsd is awesome, openbsd is awesome and windows sucks (you know that's true). Now, let’s focus on the syntax: first, as you see, all statements (or “facts”) ends with a period (“.”). That makes sense, since Linux is awesome, period. And Windows sucks, period.
And why on Earth didn’t I write “Linux” or any other name with a capital letter? Well, that’s because “linux” and “windows”, starting with small letters, are called “tokens”. That behavior is like “:linux” in Ruby or “linux” in Erlang. Tokens are not variables nor strings. They are names and they are unique on the runtime. And they always begin with small letters. It’s hard to get in the beginning, but I’m sure you’ll get used to.
So: “token” is a token. “Variable” is something like a variable — we'll see about it later.
Now, let’s save this facts on a file called “facts.pl” while we install the Prolog interpreter. I always use SWI-Prolog (since that’s what I learnt to use at college).
emerge swi-prolog || apt-get install -y swi-prolog
Ready? Let’s load this file and start to play.
swipl -f facts.pl
You’ll be presented to a weird prompt like “?- “. There we’ll ask some questions…
?- awesome(linux). # Your input
true. # The return
True, period. Very assertive, huh? So, based on known facts, coming from facts.pl, Prolog is able to say that, yes, Linux is awesome. Now, let’s try the opposite:
?- awesome(windows). # Your "question"
false. # The answer
Nice. And never forget to end all your questions with period!
Variables
In Prolog syntax, all variable names must start with a capital letter. I believe this came from the time people used logical programming for mathematical uses, an area where “X” and “Y” as variable names were pretty common. Let’s try that:
?- X = 10. # Could you please assign 10 to variable X?
X = 10. # Nice!
Let’s “print” X to verify if it’s all right?
?- write(X).
_G922
true
Hummmmm…
…
…
What?
Clauses
Actually, variables are bound only inside clauses. To “assign” the value 10 to X and print that afterward, you should type everything into a single clause, like this:
?- X = 10, write(X).
10 # The output of "X = 10"
X = 10. # The output of "write(X)" ;-)
And here we go, to the awesome world of clauses.
Can you see the comma after the “assignment”? We are going to call it “and”. So, in Prolog terms, we just wrote:
Bind 10 to X and write X.
You know why there’s no such a thing like a “global scope”? Because that would be like new facts. And facts should be stated as that: facts. X equals 10? So thats a fact, and you should write that the right way:
X(10). # Like in our facts.pl, remember?
(Don’t try to write the above statement on the interactive interpreter. It won’t work.)
Let’s write a clause to see what happens. We’ll be asking: is there any X such that X both is awesome and sucks?
?- awesome(X), sucks(X).
false.
But… wait! What is the value of X? I didn't assigned anything to it!
Don't panic. Prolog is smart enough to know that X is what we call a free variable. It's a variable name that is unbound and the task we just handed to Prolog is exactly to discover some value to assign to X!
The result? “No, there’s no such a thing”. Because, as we know, if it’s awesome, it doesn’t suck.
Let us ask another question: is there any X such that X is awesome or sucks (not both!)? In Prolog, “or” is written as a semi-colon (“;”). Be prepared, though: after hitting Enter, we won’t be sent back to the initial prompt. SWI-Prolog will wait for specific instructions:
?- awesome(X); sucks(X). <Enter>
X = linux
Right now, the interpreter is asking (in its typical and deceptive silent way): “do you want more”? If you type ‘;’ (semi-colon), you’ll be presented to the next X that matches the clause (as if you were saying: “linux? What more?”). If you type ‘.’ (dot), you’ll be sent back to the prompt, because you're already satisfied with the answer. Let’s type semi-colons until we’re out of options:
?- awesome(X); sucks(X). <Enter>
X = linux <;>
X = freebsd <;>
X = openbsd <;>
X = windows <;> # That is the last one!
?-
In short
We didn’t solve any problem, here, but, at least, you can get the overall feeling of the language. On the next article, we’re going to expand our facts file and try to, effectively, do something more interesting.
Prolog can be very hard to learn. But, in general, it’s very hard until you receive “That Magical Insight” — something that may or may not happen during your lifetime. Some people give up on learning, others try hard and never get it and others are blessed by the gift of “That Magical Insight”, coming suddenly, also stated as:
Wooow (Keanu Reeves style): I know Prolog!
See you later!