Viva Questions: 2. Ram is taller then Mohan.
3. My name is Subodh.
1- First create a source file for the genealogical logicbase application. Start by adding a few 4. Apple is fruit.
members of your family tree. It is important to be accurate, since we will be exploring family 5. Orange is fruit.
relationships. Your own knowledge of who your relatives are will verify the correctness of your 6. Ram is male.
Prolog programs.
AIM: Write simple queries for following facts.
2- Enter a two-argument predicate that records the parent-child relationship. One argument
represents the parent, and the other the child. It doesn't matter in which order you enter the Simple Queries
arguments, as long as you are consistent. Often Prolog programmers adopt the convention that
parent(A,B) is interpreted "A is the parent of B". Now that we have some facts in our Prolog program, we can consult the program in the listener
and query, or call, the facts. This chapter, and the next, will assume the Prolog program
3- Create a source file for the customer order entry program. We will begin it with three record contains only facts. Queries against programs with rules will be covered in a later chapter.
types (predicates). The first is customer/3 where the three arguments are
Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact
arg1 that matches the goal, then the query succeeds and the listener responds with 'yes.' If there is
no matching fact, then the query fails and the listener responds with 'no.'
Customer name
arg2 Prolog's pattern matching is called unification. In the case where the logicbase contains only
City facts, unification succeeds if the following three conditions hold.
EXPERIMENT NO. 1 arg3
Credit rating (aaa, bbb, etc) ● The predicate named in the goal and logicbase are the same.
● Both predicates have the same arity.
4- Next add clauses that define the items that are for sale. It should also have three arguments
AIM: Write a program in prolog to implement simple facts and Queries ● All of the arguments are the same.
arg1 Before proceeding, review figure 3.1, which has a listing of the program so far.
Item identification number
1. Ram likes mango.
The first query we will look at asks if the office is a room in the game. To pose this, we would
2. Seema is a girl. arg2 enter that goal followed by a period at the listener prompt.
3. Bill likes Cindy. Item name
4. Rose is red. arg3 ?- room(office).
5. John owns gold. The reorder point for inventory (when at or below this level, reorder) yes
Clauses
likes(ram ,mango). 5- Next add an inventory record for each item. It has two arguments. Prolog will respond with a 'yes' if a match was found. If we wanted to know if the attic was a
girl(seema).
room, we would enter that goal.
red(rose). arg1
likes(bill ,cindy). Item identification number (same as in the item record) ?- room(attic).
owns(john ,gold). arg2 no
Amount in stock Solution:-
Goal
?- likes (ram,What). Assignment: clauses
What = mango.
likes(ram ,mango).
1 solution. Aim: Write facts for following: girl(seema).
1. Ram likes apple. red(rose).
likes(bill ,cindy). harder(b,4).
owns(john ,gold). harder(d,2).
queries
and predict the answers to these queries. EXPERIMENT NO. 2
?-likes(ram,What).
What= mango ?- harder(a,X). AIM: Write a program in prolog to implement simple arithmetic.
?-likes(Who,cindy). ?- harder(c,X).
Who= cindy ?- harder(X,1). Arithmetic
?- harder(X,4).
?-red(What). Prolog must be able to handle arithmetic in order to be a useful general purpose programming
What= rose 3- Enter the listener and reproduce some of the example queries you have seen against language. However, arithmetic does not fit nicely into the logical scheme of things.
?-owns(Who,What). location/2. List or print location/2 for reference if you need it. Remember to respond with a
Who= john semicolon (;) for multiple answers. Trace the query. That is, the concept of evaluating an arithmetic expression is in contrast to the straight pattern
What= gold matching we have seen so far. For this reason, Prolog provides the built-in predicate 'is' that
evaluates arithmetic expressions. Its syntax calls for the use of operators, which will be
Viva Questions: described in more detail in chapter 12.
4- Pose queries against the genealogical logicbase that:
1- Consider the following Prolog logic base X is <arithmetic expression>
● Confirm a parent relationship such as parent(dennis, diana)
easy(1). ● Find someone's parent such as parent(X, diana) The variable X is set to the value of the arithmetic expression. On backtracking it is unassigned.
easy(2). ● Find someone's children such as parent(dennis, X)
easy(3). ● List all parent-children such as parent(X,Y) The arithmetic expression looks like an arithmetic expression in any other programming
gizmo(a,1). language.
gizmo(b,3). 5- If parent/2 seems to be working, you can add additional family members to get a larger
gizmo(a,2). logicbase. Remember to include the corresponding male/1 or female/1 predicate for each Here is how to use Prolog as a calculator.
gizmo(d,5). individual added.
gizmo(c,3). ?- X is 2 + 2.
gizmo(a,3). X=4
gizmo(c,4).
?- X is 3 * 4 + 2.
and predict the answers to the queries below, including all alternatives when the semicolon (;) X = 14
is entered after an answer.
Parentheses clarify precedence.
?- easy(2).
?- easy(X). ?- X is 3 * (4 + 2).
?- gizmo(a,X). X = 18
?- gizmo(X,3).
?- gizmo(d,Y). ?- X is (8 / 4) / 2. X=1
?- gizmo(X,X).
In addition to 'is,' Prolog provides a number of operators that compare two numbers. These
2- Consider this logicbase, include 'greater than', 'less than', 'greater or equal than', and 'less or equal than.' They behave
more logically, and succeed or fail according to whether the comparison is true or false. Notice
harder(a,1). the order of the symbols in the greater or equal than and less than or equal operators. They are
harder(c,X).
specifically constructed not to look like an arrow, so that you can use arrow symbols in your 1- Write a predicate valid_order/3 that checks whether a customer order is valid. The EXPERIMENT NO. 3
programs without confusion. arguments should be customer, item, and quantity. The predicate should succeed only if the
customer is a valid customer with a good credit rating, the item is in stock, and the quantity
X>Y ordered is less than the quantity in stock.
X<Y Aim:- Write a program to solve the Monkey Banana problem.
X >= Y 2- Write a reorder/1 predicate which checks inventory levels in the inventory record against the
X =< Y reorder quantity in the item record. It should write a message indicating whether or not it's Imagine a room containing a monkey, chair and some bananas. That have been hanged from
time to reorder. the center of ceiling. If the monkey is clever enough he can reach the bananas by placing the
Here are a few examples of their use. chair directly below the bananas and climb on the chair .
The problem is to prove the monkey can reach the bananas.
?- 4 > 3.
Yes Production Rules
?- 4 < 3.
No Assignment: can_reach🡪clever,close.
?- X is 2 + 2, X > 3.
X=4 get_on:🡪 can_climb.
?- X is 2 + 2, 3 >= X. Aim:Using the following facts answer the question
No 1. Find car make that cost is exactly 2,00,000/- under🡪in room,in_room, in_room,can_climb.
?- 3+4 > 3*2. 2. Find car make that cost is less then 5 lacs.
Yes 3. List all the cars available. Close🡪get_on,under| tall
4. Is there any car which cost is more then 10 lacs.
Production rules:
Parse Tree
c_to_f f is c * 9 / 5 +32
freezing f < = 32
Rules: can_reach(A, B)
c_to_f(C,F) :- clever(A) close(A,B)
F is C * 9 / 5 + 32.
freezing(F) :- clever(monkey) get_on(A,C) under(C,B)
F =< 32.
Queries : A=monkey can_climb(A,C) nroom(X) inroom(C) inroom(B ) can_climb(X,C,B)
?- c_to_f(100,X). C=chair X=monkey C=chair B=banana
X = 212
Yes So Can_climb(monkey,chair) close(monkey,banana)
?- freezing(15).
Yes A=monkey B=banana
?- freezing(45). Solution:-
No
Clauses:
Viva Questions: in_room(bananas).
in_room(chair).
in_room(monkey).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, bananas).
can_reach(X, Y):-clever(X),close(X, Y).
get_on(X,Y):- can_climb(X,Y).
under(Y,Z):-in_room(X),in_room(Y),
in_room(Z),can_climb(X,Y,Z).
close(X,Z):-get_on(X,Y), under(Y,Z);
tall(Y).
Queries:
?- can_reach(A, B).
A = monkey.
B = banana.
?- can_reach(monkey, banana).
Yes.