Prolog Tutorial for EECS 492 WN 2021

 

Facts, Rules, and Queries

Prolog programming is all about writing knowledge bases (KB), collection of facts and rules. The basic formatting is as follows:

  1. Period . at the end
  2. constants: lower-case
  3. VARIABLES: upper-case

 

Facts

The basic format for a fact is predicate(arguments).

Example 1: Use the following predicates to describe the facts:

To describe eecs280, eecs281, eecs445, eecs492 and math214, we could write:

 

The predicate may take more than 1 argument.

Example 2: Use the following predicates to describe the facts:

Given that martin has taken eecs445, and teaches eecs280 and eecs492, we could write:

 

Rules

The basic format for a fact is predicate(arguments) :- predicate(arguments). The :- should be read as “if”, or “is implied by”.

Example 1: Use the following predicates to describe the facts:

To describe this rule, we could write:

 

Queries

Store your KB in a .pl file, open it in the SWI-prolog, you should see something similar to this:

To question the KB if a fact is true, one make a query after ?-.

Example 1: Is math214 an eecs course?

Example 2: Is math214 a prerequisite of eecs492 ?

Example 3: Have martin taken eecs492 ?

Note that although taken(martin, eecs492) is not directly stated in the KB, prolog makes the logical inference from the fact that teach(martin, eecs492) and taken(X, Y) :- teach(X, Y). In other words, since Martin is teaching EECS 492, he must have taken this course earlier.

Example 4: List all courses that martin has taken?

 

Recursion

You may have noticed that in the earlier example, the logic is not complete. If one has taken one course, he/she must have taken the prerequisites.

One may write a new rule like:

It seems fine when you make the query:

However, the problem arises when you try to check all course that Martin has taken:

Oops! Looks like your program gets stuck in an infinite loop! Prolog applies backward chaining and depth-first search to do logic inference, and this will lead to an infinite search tree. Check the Recursion section for details.

To stop Prolog from an infinite loop, press ctrl + C, and press A to abort.

A better way is to avoid recursion in Prolog. Use a new predicate instead.

And the query works fine here:

 

Definite Clause Grammars

Context Free Grammars

CFG is a finite collection of rules indicating that...

CFG captures constituents and ordering.

CFG consists of...

 

An exemplar CFG is like:

Consider a sentence (a string of words): the student likes prolog. The question is, is this grammatical according to our little grammar? And if it is, what structure does it have?

A derivation is a sequence of rules applied to a string that accounts for that string:

It can be represented in:

[s [np [det the] [n student]] [vp [v likes] [np [n prolog]]]]

In tree form, it looks like:

 

Definite Clause Grammars

DCG is a notation for writing grammar that hides the underlying difference list variables.

Translate the previous CFG into DCG is straightforward:

Example 1: find out whether the student likes prolog is a sentence.

Example 2: find out if the student is a noun phrase (np).

Example 3: generate all the sentences in the grammar.

 

FAQ

General

How to locate the *.pl file in prolog?

Suppose you have a directory eecs492/prolog and all your prolog files are under this directory. You can go to this directory first, and then start Prolog /user/eecs492/xsb. Once you are in the Prolog environment, you can load the file by typing [filename].

 

When I load up my file, I receive some warnings about singleton variables. Does that matter?

The reason is that you have variables defined in your predicates that have not been referenced or used in the body. You can replace those variables with an anonymous variable _ (underscore).

For example, if you define the following "member" predicate, "Y" is not used, so you will have a "singleton" warning.

If you define it as the following, you should be fine.

 

Family Tree Problem

I have a problem with duplicate answers, is that ok?

The reason for this to happen is that you have multiple facts/rules that are matched by the prolog interpreter. Since the tutorial didn't mention how to prevent that, it is ok for your program to produce duplicate results. (no points will be taken from you in this regard). One way to fix it is to use the built-in “setoff”.

So you can make the following changes to avoid duplicates.

 

I'm having trouble figuring out how to prevent a "reflexive" type of relationship from happening. For example, Harry should not be Harry's brother and the same for his sister.

To avoid this, you will need to specify that X and Y cannot be the same as in the following:

 

Block World Problem

For a "taken" response should "on" be changed to "off"? For example, my current code returns the following. Should it respond with "I have taken the cone off the square."?

No you don't need to change "on" to "off" in the "take" case. Basically, "on ..." is modifying the thing that should be taken. In your example, "on the square" is modifying "the cone".

 

How do I print out the response?

One way of doing it is to take the input list (e.g., [put, the, red, block, on, the, green, circle]), and convert it to another output list. To come up with this output list, you need to check each member of the original list to see whether it requires any change. Once you have the output list, you can simply write each member of the output list out using "write". The key here is to do some list operations.

When implementing the predicate "command", this command will take an argument and do some processing, then provide output.

An example could be:

Then you can test it:

Certainly the above is only an example, you will need to re-implement “command” to fit that to our problem here.

 

I have np defined as follows. The problem is that the last definition of np never halts.

Your third rule np-->np, pp will loop infinitely. You can change np-->np, pp to "np-->det, n, pp, etc.

 

Is it alright to have an infinite number of chains for the prepositional section of the sentence? For example, is [put,the,cone,on,the,cone,on,the,cone,on,the,cone,on,the,cone....] valid input, or should this terminate after 1 or 2 prepositions?

It should be able to support an arbitrary number of pps as valid input. But all the testing cases will not go beyond the ones you've seen in the training data.

 

A valid sentence is take the ball, but it looks like put the ball is not. I'm not sure how to accept take but not put for my one rule that accepts a noun with an empty pp.

Good catch here! There is indeed a difference between “put” and “take”. We have not talked about lexicalized grammar, so we will not worry about this difference for now. My testing cases will be strictly following the training examples, will not be testing anything like “put that ball”.

However, for your information, to distinguish “put” and “take” in this use, we can have lexicalized grammar. Basically, instead of a general “V”, we can separate it to “V-Put” and “V-Take”. Then other grammar rules will be updated accordingly to use “V-Put” and “V-Take”.

 

Are cubes, cones, and blocks the only nouns that can have action taken against them? (e.g., you cannot take a square or circle)

You made a very good observation. To make the problem simpler, here we mainly focus on the syntax, without worrying about the semantics, e.g., what objects can or cannot be taken. So all the nouns should behave the same even though we do not see "take the square" in the example.