Monday, November 16, 2009

Introduction to Prolog

 

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:

 

Code Snippet
  1. Human(Ahmed)
  2. Car(Toyota)
  3. 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:

Code Snippet
  1. ?- 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:

Code Snippet
  1. ?- 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:

Code Snippet
  1. father(ahmed,mohammed)
  2. father(ahmed,ali)

So if we ask the engine this question:

Code Snippet
  1. ?- 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):

Code Snippet
  1. car(X) :- gotadoor(X).
  2. airplane(X) :- gotadoor(X).

Then let's declare some planes and cars:

Code Snippet
  1. car(toyota)
  2. car(mazda)
  3. car(jagoar)
  4. 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:

Code Snippet
  1. next_to(X, Y, List) :- iright(X, Y, List).
  2. next_to(X, Y, List) :- iright(Y, X, List).
  3. iright(L, R, [L | [R | _]]).
  4. iright(L, R, [_ | Rest]) :- iright(L, R, Rest).
  5. einstein(Houses, Fish_Owner) :-
  6. =(Houses, [[house, norwegian, _, _, _, _], _, [house, _, _, _, milk, _], _, _]),
  7. member([house, brit, _, _, _, red], Houses),
  8. member([house, swede, dog, _, _, _], Houses),
  9. member([house, dane, _, _, tea, _], Houses),
  10. iright([house, _, _, _, _, green], [house, _, _, _, _, white], Houses),
  11. member([house, _, _, _, coffee, green], Houses),
  12. member([house, _, bird, pallmall, _, _], Houses),
  13. member([house, _, _, dunhill, _, yellow], Houses),
  14. next_to([house, _, _, dunhill, _, _], [house, _, horse, _, _, _], Houses),
  15. member([house, _, _, _, milk, _], Houses),
  16. next_to([house, _, _, marlboro, _, _], [house, _, cat, _, _, _], Houses),
  17. next_to([house, _, _, marlboro, _, _], [house, _, _, _, water, _], Houses),
  18. member([house, _, _, winfield, beer, _], Houses),
  19. member([house, german, _, rothmans, _, _], Houses),
  20. next_to([house, norwegian, _, _, _, _], [house, _, _, _, _, blue], Houses),
  21. member([house, Fish_Owner, fish, _, _, _], Houses).

Then if you ask prolog this question:

Code Snippet
  1. ?- 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 :)

0 comments:

Post a Comment