Getting Started: Prolog (part I)
Dear iTech reader,
In next semester, i will take course on Logic Programming using prolog. Logic programming is a family of declarative-type programming. Why declarative? It is because prolog represents problem in declarative statements based on goals. It is also known as the goal-oriented.
So this is my first program taken from Ivan Bratko's book: Prolog: Programming for Artificial Intelligence. Some have been rewritten also to help myself understanding prolog.
Representing a clause in Prolog
We have this clause:
"Budi is playing footbal"
We can rewrite it in Prolog as:
play(Budi,footbal).
We should notice something. As declarative-type programming, prolog isn't suited for numeric computation. Therefore, it is best-suited for solving problem which is involving relation between objects. For example: family tree relation.
Statement:
For all X and Z,
X is a mother of Z if
X is a parent of Z.
X is a female
1 | mother(X,Z) :- parent(X,Z),female(X). |
At the snipped prolog code above. First, we define relation by facts: X is a parent of Z and X is a female. Second, we define relation mother by rule of first facts. Comma (,) is a sign of conjunction of the conditions which relation should be satisfied if both true.
Exercise (Ivan Bratko,13):
a. Everybody who has a child is happy (introduce a one-argument relation happy).
b. For all X, if X has a child who has a sister then X has two children (introduce new relation hastwochildren).
(to be continued)
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


keren mas... kalo bisa sekalian tutorial cara make SWIProlog nya juga ya :p
1 happy(X) :- parent(X)
2 hastwochildren(X) :- parent(X,A),parent(X,B),sister(A,B)
~cmiiw
_on:
1. Dari hasil percobaan di SWI-Prolog berdasarkan knowledges berikut.
parent(Budi).
happy(X) :- parent(X).
lalu di query
?- happy(X) hasilnya adalah
X = Budi
Yes (ditemukan)
~sepertinya bener
atau ada bantahan yang lain? atau contoh lain?
2. kalau yang kedua sister(A,B) nya harus didefinisikan terlebih dulu karena A bisa sama dengan B. Oleh karena itu bisa menggunakan built-in function distinct menjadi distinct(A,B). Tapi kalo ada pendapat lain boleh dikemukakan di sini
echo_n says:
25 August 2009 at 18:24
done...
check this out: http://itech.brutefoz.com/2009/08/26/getting-started-swiprolog-installrun/
nah itu, gw jg bingung yg sister..
misal salah 1 dari A ato B itu cowo, itu gmn ceritanya?
~wah saya lom kenalan ama oom distinct, nais inpo gan..