Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics (Wikipedia Definition @ http://en.wikipedia.org/wiki/Prolog).
Prolog, LISP and many other languages depend on logic syntax and are represented as facts and rules. If you have any experience in programming using languages such as C#, JAVA or C++, then you need to change the way that you are using to write your codes.
How to start?
First of all, you need to download some tools to execute your commands; one of them is Amzi Development Environment. You can find it here: http://www.amzi.com/
What you have to do is just to open new file and write down your commands, then from Listener->Start you can start execute your code.
Code Syntax:
As we said in the beginning of this lesson, prolog depends on logical relationships, like:
- Human(Ahmed)
- Car(Toyota)
- Human(Khaled)
This means that Ahmed and Khaled are humans. And Toyota is a car.
Let’s test those three lines; go to Listener -> Start then Listener -> Consult.
And in execution window, type the following question:
- ?- Human(V).
Here we ask to show all humans and put it in array named V, you can press enter to quit or “;” to continue.
*** Note: (V) must be capital. All variables in Prolog must be capital.
The second type of questions is the Yes/No questions, like this:
- ?- Human(Ahmed).
And you will get the answer depending on if you have added Ahmed to humans or not.
Relations:
In addition to previous, we can describe relationships between two items:
- father(ahmed,mohammed)
- father(ahmed,ali)
So if we ask the engine this question:
- ?- father(ahmed,U)
We will have all Ahmed's sons in U list.
Rules:
That is the point where we will start get benefit from prolog, this is the way that human brain works when thinking. For example (all cars have a door and all planes also):
- car(X) :- gotadoor(X).
- airplane(X) :- gotadoor(X).
Then let's declare some planes and cars:
- car(toyota)
- car(mazda)
- car(jagoar)
- airplane(ai100)
If we ask if mazda have a door or not, prolog engine will be smart enough to answer with yes.
More about Prolog:
This is just simple introduction to Prolog. There are a lot of features in Prolog you can start with from this link:
http://kti.mff.cuni.cz/~bartak/prolog/learning.html
Example:
Einstein Problem, one of the famous puzzles. Where the puzzle author (Einstein or anybody else) said that 98% of peoples can’t solve this problem:
There are 5 houses sitting next to each other, each with a different color, occupied by 5 guys, each from a different country, and with a favorite drink, cigarette, and pet. Here are the facts:
The Brit lives in the red house
The Swede keeps dogs as pets.
The Dane drinks tea
The green house is on the left of the white house
The green house's owner drinks coffee
The person who smokes Pall Mall rears birds
The owner of the yellow house smokes Dunhill
The man living in the center house drinks milk
The Norwegian lives in the first house
The man who smokes Blends lives next to the one who keeps cats
The man who keeps horses lives next to the man who smokes Dunhill
The owner who smokes Bluemaster drinks beer
The German smokes Prince
The Norwegian lives next to the blue house
The man who smokes Blend has a neighbor who drinks water
The question is: Who owns the fish?
If we try to translate this problem into Prolog group of facts, rules and relationships, it will be like this:
- next_to(X, Y, List) :- iright(X, Y, List).
- next_to(X, Y, List) :- iright(Y, X, List).
- iright(L, R, [L | [R | _]]).
- iright(L, R, [_ | Rest]) :- iright(L, R, Rest).
- einstein(Houses, Fish_Owner) :-
- =(Houses, [[house, norwegian, _, _, _, _], _, [house, _, _, _, milk, _], _, _]),
- member([house, brit, _, _, _, red], Houses),
- member([house, swede, dog, _, _, _], Houses),
- member([house, dane, _, _, tea, _], Houses),
- iright([house, _, _, _, _, green], [house, _, _, _, _, white], Houses),
- member([house, _, _, _, coffee, green], Houses),
- member([house, _, bird, pallmall, _, _], Houses),
- member([house, _, _, dunhill, _, yellow], Houses),
- next_to([house, _, _, dunhill, _, _], [house, _, horse, _, _, _], Houses),
- member([house, _, _, _, milk, _], Houses),
- next_to([house, _, _, marlboro, _, _], [house, _, cat, _, _, _], Houses),
- next_to([house, _, _, marlboro, _, _], [house, _, _, _, water, _], Houses),
- member([house, _, _, winfield, beer, _], Houses),
- member([house, german, _, rothmans, _, _], Houses),
- next_to([house, norwegian, _, _, _, _], [house, _, _, _, _, blue], Houses),
- member([house, Fish_Owner, fish, _, _, _], Houses).
Then if you ask prolog this question:
- ?- einstein(Houses, Fish_Owner).
You will have the right solution, try to use your mind to solve it and correct it with Prolog solution, if your answer not German, so you are Candidate to join the 98% club. It is not so bad :)