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)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Code, News, Prolog, Uncategorized

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.

Comments

5 Responses to “Getting Started: Prolog (part I)”

Leave Comment

(required)

(required)


Spam Protection by WP-SpamFree