Prolog Labs

Lab 0: Chain of Command

Translate the following into a Prolog knowledge base:

smith is a captain

jones and johnson are sergeants

campbell, kiljoy, rogers, and farmer are privates

 

captains supervise sergeants

sergeants supervise privates

 

X commands Y if X supervises Y

X commands Y if X supervises Y's supervisor

Lab 1: Defining a simple knowledge base

We can think of a Prolog knowledge base as a collection of actual and virtual tables, just like a database.

Prolog queries can emulate SQL queries such as project, select, and join.

The file sjsu.pl contains the following tables:

student(S) asserts S is a student

instructor(Instructor)

taken(Student, Course, Result)

teaches(Instructor, Course)

prerequisite(Course, PreReqCourse)

Create rules for defining the following virtual tables:

academic(X) if X is a student or instructor

passed(X, Y) if X is a student who has passed course Y

studentOf(X, Y) if student X has taken a course from instructor Y

upperDivisionCSstudent(X) if X is a student who has passed all lower division CS courses

teachesPreReq(X) if X is an instructor who teaches a prerequisite course

canTake(X, Y) if X is a student who has passed all of the prerequisites for course Y

Careful, this last one is tricky. You might think this would work:

canTake(X, Y) :- prerequisite(Y, Z), taken(X, Z, pass).

But this is true if X has taken any prerequisite of course Y.

Try using Prolog's meta-predicate:

foreach(predicate1, predicate2) if everything satisfying predicate1 also satisfies predicate2

I call this a meta-predicate because its operands are predicates, not terms.

Lab 2: Recursion

Create and test a knowledge bases for the following domain.

Homer is the parent of Bart, Lisa, and Maggie. Abe and Mona are Homer's parents.

Marge is also the parent of Bart, Lisa, and Maggie. Clancy and Jacquelin are her parents.

Clancy and Jacquelin are also the parents Selma and Patty.

Parents are ancestors as are ancestors of parents.

Siblings share a parent.

Lab 3: Arithmetic

We can use structures to represent positive integers in Prolog. For example:

0 = zero
1 = inc(zero)
2 = inc (inc (zero))
3 = inc (inc (inc (zero)))
etc.

Here inc(x) stands for the increment (add one) function.

Define and test the predicate add(X, Y, Z), which represents the relationship Z = X + Y.

Here's a start:

add(X, zero, ???).

add(X, inc(Y), Z) :- ???.

Define and test the predicate mul(X, Y, Z) which represents the relationship Z = X * Y.

Define and test the predicate exp(X, Y, Z) which represents the relationship Z = X ^ Y.

Define and test the predicate less(X, Y), which represents the relationship X < Y.

Here is a sample session:

% 3 + 2 = 5
?- add(inc(inc(inc(zero))), inc(inc(zero)), Z).
Z = inc(inc(inc(inc(inc(zero)))))

% 3 * 2 = 6
?- mul(inc(inc(inc(zero))), inc(inc(zero)), Z).
Z = inc(inc(inc(inc(inc(inc(zero))))))

% 3 ^ 2 = 9
?- exp(inc(inc(inc(zero))), inc(inc(zero)), Z).
Z = inc(inc(inc(inc(inc(inc(inc(inc(inc(zero)))))))))

% 3 < 2 = false
?- less(inc(inc(inc(zero))), inc(inc(zero))).
false.

 

Lab 4: Org Charts, etc.

In Acme Inc. Jones is Smith's supervisor. Jones is the supervisor of Wilson and Jackson. Employee X supervises employee Y if there is a chain of command from X down to Y.

Lab 5: Evaluating expressions

We can use structures to represent syntax trees in Prolog. For example, the expression

(3 * 4) + (5 + 6)

can be represented by the syntax tree:

sum(prod(num(3), num(4)), sum(num(5), num(6)))

Write an evaluator for the language of sums and products:

?- eval(sum(prod(num(3), num(4)), sum(num(5), num(6))), X).
X = 23.

Lab 6: Polynomials

A polynomial is a sum of monomials. For example:

p(x) = 3x2 + 2x + 5

We can represent a polynomial in Prolog as a list of monomials:

[mono(3, 2), mono(2, 1), mono(5, 0)]

Implement the p0redicate:

eval(P, X, Y) :- P(X) = Y

Lab 7: List Processing

Implement the following predicates:

?- sum([1, 2, 3, 4], S).
S = 10
?- prodSquares([1, 2, 3, 4], P).
P = 576  % product of squares
?- mapSquares([1, 2, 3, 4], X).
X = [1, 4, 9, 16]

Where:

square(X, Y) :- Y is X * X.

Lab 8: Trees

Implement height(Tree, H) where H is the height of a tree of the form leaf or parent(LeftChild, RightChild)

Lab 9: UML Class Diagrams

See UML in Prolog

Here's a simplified version of the problem: Jedi 1 in Prolog

Lab 10: Jedi 1.0 in Prolog

See Jedi 1.0 in Prolog

Lab 11: Finite State Machines

See Finite State Machines as Knowledge Bases

Lab 12: Boolean Expressions

Here's a grammar for the language of Boolean Expressions (BEXP):

bexp ::= true | false | not(bexp) | and(bexp, bexp) | or(bexp, bexp)

Implement an execute predicate for BEXP. For example:

?- exec(or(and(true, false), not(false)), R).
R = true