Question

In: Computer Science

In order to complete these exercises, you will need to install prolog from swi-prolog.org. Write a...

In order to complete these exercises, you will need to install prolog from swi-prolog.org.

Write a Prolog program by creating a file with you favorite program editor like Notepad++ that contains the following facts:

  • here the predicate parent(X,Y) means X is the parent of Y

  • you can also copy/paste or download the following database from the

    course website

    female(pam). female(liz). female(ann). female(pat). male(tom). male(bob). male(jim). parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim).

    (a) Load this file into Prolog, usually this is done with the consult file predicate: ?- consult(‘<filename>’).

    On Windows you can load the fact database with the menu point File®Consult.
    also overloads a bare string list by consulting each item in the list: ['family_tree.pl'].

  • % these are the family facts
    female(pam).
    female(liz).
    female(ann).
    female(pat).
    male(tom).
    male(bob).
    male(jim).
    parent(pam,bob).   % pam is parent of bob (etc.)
    parent(tom,bob).
    parent(tom,liz).
    parent(bob,ann).
    parent(bob,pat).
    parent(pat,jim).

    will work. Once you have loaded the program pose the following queries:

    ?- female(ann).
    ?- female(jim).
    ?- parent(X,bob).
    ?- parent(tom,X).
    ?- parent(X,ann),parent(X,pat).

    What are the answers to these queries? Beware, for some queries here might be more than one answer. To get all the answers type a ';' and carriage return at the question mark.

    (b) Now, using the parent predicate formulate the following Prolog queries:

    1. Who is Pat's parent?
    2. Does Liz have a child?

SWI-Prolog

3. Who is Pat's grandparent?

(c) Given the above facts, extend the program by writing rules defining the following predicates:

sister(X,Y) -- X is the sister of Y.
son(X,Y) -- X is the son of Y.
father(X,Y) -- X is the father of Y. grandmother(X,Y) -- X is the grandmother of Y. ancestor(X,Y) -- X is an ancestor of Y.

(Hint: this predicate might come in handy: different(X,Y):- not(X=Y). Some predicate definitions might be recursive.)

Add these rules to your existing file and attach the file to your submission

Demonstrate that your program works by posing the following queries:

4. ?- sister(X,pat).
5. ?- sister(X,Y).
6. ?- son(jim,X).
7. ?- father(X,bob).
8. ?- grandmother(X,ann). 9. ?- ancestor(X,jim).

Hand in the source code of your prolog program and a proof of the program execution.

For each of parts a, b, and c, you should include screenshots of the relevant contents of the SWI-Prolog console. Part c additionally requires you to attach your .pl file.

Solutions

Expert Solution

%The complete prolog code is as follows:

female(pam).
female(liz).
female(ann).
female(pat).
male(tom).
male(bob).
male(jim).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).

different(X,Y):- not(X=Y).

% here X is the sister of Y, so we first check if X is a female
% we trhen use a third varible that signifies the parenst of X and Y
% for X to be sister of Y they must have the same parents

sister(X,Y):-female(X),parent(Z,X),parent(Z,Y),different(X,Y).

%here X is the son of Y, so we first check if X is a male
%then we find all the cases where Y is the parent of X

son(X,Y):- male(X),parent(Y,X).


%here X is the father of Y, we check if X is a male
%then we afind all the cases where X is the parent of Y

father(X,Y):-male(X),parent(X,Y).

%here X is the grandmother of Y, we first check if X is a female
%we then use Z which holds the parent of Y and then find the parents of Z

grandmother(X,Y):-female(X),parent(Z,Y),parent(X,Z),different(X,Y).


%here X is the ancestor of Y, we first find all the parents of Y
%we then recursively call the predicate, we use Z to hold the parents of Y
% then we find all the parents of Z and keep on doing it recursively until the facts get exhausted
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).

OUTPUT:

(a)

(b)

note in (b) 3 "who is pats grandparents" variable X holds pats parents and variable Y holds pats grandparents.

(c)


Related Solutions

Use logical language Prolog. Use SWISH (SWI Prolog for Sharing) Write a Prolog program that finds...
Use logical language Prolog. Use SWISH (SWI Prolog for Sharing) Write a Prolog program that finds the maximum of a list of numbers. Submission file should include a screenshot with tracing a program execution when a list only has 2 members and execution without tracing when a list has 8 elements.
You need to order US $100,000 worth of reams of paper from China to complete an...
You need to order US $100,000 worth of reams of paper from China to complete an order from an important commercial customer in 30 days. The current rate of exchange is USD/CNY = 6.5766. Ignoring other transaction expenses associated with the order, how many Yuan would you need to pay the Chinese supplier? Define the meaning of foreign exchange. Assuming that you have the choice to pay immediately or in 30 days, define, describe, and provide examples of how you...
• Write a complete prolog with your own facts using the relation: – Like(Person,Food). • Define...
• Write a complete prolog with your own facts using the relation: – Like(Person,Food). • Define the following rule: – Two persons are friends if they like the same food. • Now find: – All people who like more than one food. – All food liked by two people – All friends • likes(sarah, pizza). • likes(sarah, saleeg) . • likes(sarah, bread) . • likes(farah, rice). • likes(rana, burger) . • likes(rana, saleeg).
You need to complete n courses in order to complete your degree. Some of these courses...
You need to complete n courses in order to complete your degree. Some of these courses have prerequisites, for example: “course 1 has to be completed before course 3”. Your goal is to find an order to take all n courses and complete your degree. Observe the following input file. The first line in the file has 2 numbers n and p where n is number of courses and p is the number of prerequisites. Input: 5 3 1 2...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
To complete this exercise you need a software package that allows you to generate data from...
To complete this exercise you need a software package that allows you to generate data from the uniform and normal distributions. (i) Start by generating 500 observations xi - the explanatory variable - from the uniform distribution with range [0,10]. (Most statistical packages have a command for the Uniform[0,1] distribution; just multiply those observations by 10.) What are the sample mean and sample standard deviation of the xi? (ii) Randomly generate 500 errors, ui, from the Normal[0,36] distribution. (If you...
Suppose you are ordering pizza from a restaurant that is known to complete one order every...
Suppose you are ordering pizza from a restaurant that is known to complete one order every 10 minutes. Let N be a Poisson RV modeling the number of orders completed in an hour. (a) What is Λ? (b) What is the probability that fewer than (or equal to) 5 orders are completed in one hour? (c) What is the probability that between (or including) 4 to 8 orders are completed in an hour? (d) What is the probability that more...
Suppose you are ordering pizza from a restaurant that is known to complete one order every...
Suppose you are ordering pizza from a restaurant that is known to complete one order every 10 minutes. Let N be a Poisson RV modeling the number of orders completed in an hour. (a) What is Λ? (b) What is the probability that fewer than (or equal to) 5 orders are completed in one hour? (c) What is the probability that between (or including) 4 to 8 orders are completed in an hour? (d) What is the probability that more...
You have just received an order from an Internet retailer for some equipment you need for...
You have just received an order from an Internet retailer for some equipment you need for a business presentation. Unfortunately, some of the equipment is damaged. Write a letter to the retailer in which you explain the damage, express your dissatisfaction with the shipment, and describe what you want the retailer to do to fulfill your shipment. Develop a response that includes examples and evidence to support your ideas, and which clearly communicates the required message to your audience. Organize...
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to...
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT