I had been dealing with Prolog for a long time and I just want to say something about it.
What is Prolog?
Prolog is a programming language that allows non-deterministic programming. Here is logic in pure form available, so abstract thinking is required here. The language works in principle with the predicate logic of the first level in the rough. Why in the rough, Prolog can reach the ideal only partially.
Important to note here is not to work with the question "How?" but with the question "What must apply so that this works?"
Small prehistory still in advance! Prolog was developed for knowledge based systems, AI. and is also used further on. e.g. the Google Translator also works with Prolog with the same core logic as here. Prolog is also often used in NLP (Natural Language Processing). So Prolog plays an important role in artificial intelligence.
Time for practice
Well, theory aside, let's get to practice.
I have attached a few links here for Prolog programming:
- Download : https://www.swi-prolog.org/Download.html
- Webshell: https://swish.swi-prolog.org/
- Source: https://github.com/SWI-Prolog/swipl-devel
After compiling / installing or visiting the webshell, a nice little shell awaits us. Which looks something like this(Webshell).
This shell can't do much at first. This is because no program is stored yet. We should do that.
Let's start with something easy, the facts. A fact is nothing else than e.g. "Alex is father of somebody. Alex is male". So predicates. In Prolog you can represent something like this. Important syntax: Lines always end with a dot ".". Variables are always capitalized at the beginning and constants are generally written in lower case.
male(alex).
If you now read the program into Prolog, you can make queries with the shell. Think of it like databases. While I'm at it, you can build databases in Prolog. As in the other languages, there are comments.
% Kommentar zu irgentwas.
If we create a query on the small prologue example then it looks like shown in the example. The query would be as a question like this: "Who is male?"
The query below is written in german
What happens here exactly? Prolog goes through all the facts in the program, checking if any match. Facts that start with male. If so, the possible results are listed for and a true is returned. If nothing was found, we get a false.
Now let's extend the prolog program with another fact e.g. Simone is female. Now we can also create rules instead of facts.
female(simone).
A small example, since Simone is involved. Let's assume that Alex loves Simone. Here one could express it also more abstractly e.g. " loves " or "Who loves whom? But you can also represent it as a predicate logical formula:
= is male
= is female
= loves
loves(X, Y) :- male(X), female(Y).
The structure of the rule looks like this: On the left side we have the relation "loves(X, Y)". On the right side have the condition what must be satisfied for the left to hold. In short "If right is true then it follows that left is also true.". Between the left and right part is ":-". That represents the implication thus the arrow .
Logical links in Prolog are represented with a comma for an AND/AND, for example. An OR/OR is represented either with a semocolon or a new line.
If we now create some queries on it, it would look like this. I just created 5 queries to show how ingenious the system is. Ok, actually Prolog is stupid, but well you know what I mean.
The queries below are written in german
The first query is similar to the first one with Alex. From the second query on, you can see what Prolog can do. I have simply in the second query: "loves(alex, Y).", nothing else than the question expressed: "Who does Alex love?". Prolog answers, with = "simone" and a true since it found a result.
Similar also in the third query, with the question: "By whom is Simone loved?". Again, we get an answer from Prolog: = alex" and a true at the end.
In the fourth query I simply asked the question: "Who loves whom?" and also here an answer from Prolog in the case: = "alex", = "simone" also again a true at the end.
But what if I put someone else, for or . We see this in the fifth query, where Prolog directly outputs a false. A true and or a result means that Prolog has found something, otherwise there is a false.
Again, here is the question what happens here?
In the query "female(X)", Prolog goes through all the facts and looks to see what matches. Since we have entered an instead of simone here, every fact female comes into question for Prolog. But what happens internally is a calculation or a unification (I will talk about this in another post).
With rules, however, it is somewhat different, since Prolog first takes the left rule page and checks whether there is such a rule. If there is a rule, Prolog takes out the variables and checks the right rule side. Here we have two facts male and female. Here it is checked for both whether the facts exist. If yes, then the variables are assigned according to the possibilities.
Here one can say that the left side of the rule is all-quantified, and the right side of the rule is existence-quantified. One can also say, if there is something for the right side, then it is valid for the left side. But there must be something for everything on the right side of the rule.
I hope I could give a small insight into the topic of Prolog 😄