b. Give an example of a compound term in this language. What are its parts called?
c. Define: arity, functorial form, unification, resolution, backtracking.
Give examples of
Question 3
a. Draw a diagram of the Prolog data structure used to store (1): f(1,X),
(2): 1 + X, (3): teacher(dick), and (4):+ X.
b. Show how 1+(2*3) is stored in Prolog. Show 1+2*3. Show (1+2)*3. Which of these are identical?
c. Draw a UML class diagram of the Prolog data structure.
Question 4
A friend of yours needs to learn Prolog and has sent you EMail requesting
help... write a reply explaining what they need to know and giving examples
they should try during their first meeting with Prolog .
Question 5
Give a list of simple hints that you would give to a C or C++ programmer
who had to learn Prolog.
Question 6
Trace what happens if you input the query underlined below
?- f( [1,2,3], S ).with a Prolog database that has the following facts and rules about f in it:
f( [], 0 ).
f ( [ H | T ], S ) :- f( T, X), S is X + H.
Question 7
a. What is the Closed World Assumption?
b. Give an example of its effect.
c. Why is this an important limitation?
Question 8
a. Give a list of questions that are frequently asked by C++ programmers
when they are attempting to learn Prolog for the first time.
b. Give good answers to each of the questions generated in part a above.
Answer to Question 3
a. Explain how (1): f(1,X), (2): 1 + X, (3): teacher(dick), and (4):+ X are
stored in Prolog.
All structures in prolog are stored in functorial form. This is a data structure where every node is an object that has one functor and zero or more parameters or arguments. The number of parameters is called the arity, In the above cases:
| Expression | Functor | Arity | Parameters |
|---|---|---|---|
| 1 + X | + | 2 | constant 1, variable X |
| f(1,X) | f | 2 | constant 1, variable X |
| teacher(dick) | teacher | 1 | constant dick |
| +X | + | 1 | variable X. |
Here are the object diagrams using the UML:
b. Draw a diagram of 1+(2*3). Show 1+2*3.
(Note. Precedence makes the first two expressions the same.... )
c. Draw a UML class diagram of Prolog data structures.
Note
This is an example of the Composite pattern.
Answer to Question 5
Trace what happens if you input the query underlined below
?- f([1,2,3], S).and a Prolog data base has the following facts and rules in it:
f([], 0).
f([ H | T ], S) :- f( T, ST), S is ST+H.
Try it on the computers!
1 ?- trace, f([1,2,3], S).
Here is another form of answer that shows each call and its matching exit:
Call: f([1,2,3], S)
| Call: f([2,3], ST)
| | Call: f([3], ST)
| | | Call: f([], ST)
| | | Exit: f([], 0)
| | | --(ST=0)
| | | Call: S is 0 + 3
| | | Exit: S=3
| | Exit: f([3], 3)
| | --(ST=3)
| | Call: S is 3 + 2
| | Exit: S=5
| Exit: f([2,3], 5)
| --(ST=5)
| Call: S is 5 + 1
| Exit: S=6
Exit: f([1,2,3], 6)
--(S=6)