Jump to content

A quick walk through some PROLOG basics


WarFox
 Share

Recommended Posts

For one of my classes this semester, we had a week where we took a quick look into PROLOG. This language is probably not what most people are used to working with. It follows the logic paradigm, unlike LISP that is functional or C that is imperative. The logic paradigm is an interesting one with much of their uses being in artificial intelligence. As my instructor said, the point is to get rid of programming all together.

 

The mechanics of the language are simple. It is written using three main parts that comprise of a program; facts, rules, and queries. The programmer writes a set of facts and rules. When interpreted, the interpreter will construct a deductive database. From that database, a user will submit queries and the computer will respond with an answer. Essentially, based off of the deductive database, the computer will "figure out" the answer to the query on it's own.

 

Before sharing some sample code, I will provide a few resources. For computers of all platforms, a popular interpreter for PROLOG is swi-prolog. WIth a quick google search, swi-prolog also provides a website where it can be used in browser. If installing the interpreter on one own's computer, a prolog file that constructs the deductive database ends in a ".pl" extension. Also, on my linux system, once swi-prolog is installed, the interpreter is started by typing "prolog" into the terminal. To be able to invoke the interpreter with a ".pl" file, start the prolog interpreter, then submit a query of "consult(FileNameWithoutExtension)."

 

Statements in prolog are based around predicates and are to be thought about in english. An example of a fact would be a dog is a pet. Predicates have a relationships to objects. In prolog terms, dog is an object. Stating this fact in prolog would look like this:

pet(dog).

When this file is user in the interpreter and we submit a query asking if a dog is a pet, this is what it will look like.

?- consult(ex1).
true.

?- pet(dog).
true.

 

To exit the interpreter, input the following line into the interpreter.

halt.

 

Now, rules are based around if-statements. If statements in prolog are represented as ":-". Look at the following example.

pet(dog).
dog(sparky).
owner(tux).

owns(tux, sparky) :- dog(sparky), pet(dog), owner(tux).

We have defined three facts. The last line is stating a rule. We are saying, tux owns sparky IF sparky is a dog AND a dog is a pet AND owner is tux. Commas (,) in prolog are representative of AND.

Running this in an interpreter with some queries.

?- consult(ex2).
true.

?- owns(tux, sparky).
true.

?- owns(me, sparky).
false.

If we ask if tux owns sparky, we get true as the output. However, if we ask if me owns sparky, is returns false.

 

Now to make it a little more complex.

father(tom).
mother(lisa).
boy(chandler).
girl(mila).

father_of(X, Y) :- father(X), boy(Y); girl(Y).
mother_of(X, Y) :- mother(X), boy(Y); girl(Y).

This this script, a mother, father, boy and are defined. We are setting rules asking who is the father of who and who is the mother of who. We can query this and get responses. A quick not is that the semi-colon (;) represent OR. So we are saying, X is the Father_of Y if x is a father and y is a boy or y is a girl.

 

Variables in PROLOG start with captical letters.

?- consult(ex3).
true.

?- father_of(tom, lisa).
false.

?- father_of(tom, mila).
true.

?- mother_of(lisa, tom).
false.

Now, we can also make some queries that will return something interesting.

?- father_of(tom, WHO).
WHO = chandler ;
WHO = mila.

?- mother_of(who, mila).
false.

?- mother_of(WHO, mila).
WHO = lisa.

By using WHO as a parameter in a query, it will show who all are the children of tom.

 

This is the conclusion of just a quick showing of PROLOG. I was eager to post this becuase it doesn't seem like a whole lot of people are too familiar with the logic paradigm.

 

  • I Like This! 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...