prolog代写: CSE 240 Homework 13

CSE 240 Homework 13, Spring 2018 (50 points)

Due Saturday, April 21, 2018 at 11:59PM, plus one day grace period

Introduction

The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures. By the end of the assignment, you should have

  •   strong concept of logic / declarative paradigm;
  •   strong concept of facts, rules, and questions in Prlog.
  •   Basic recursive in Prolog.

    Reading: Text Chapter 5 and lecture slides. Also read Text Appendix B.4 Prolog Tutorial

    You are expected to do the majority of the assignment outside the class meetings. Should you need assistance, or have questions about the assignment, please contact the instructor or the TA during their office hours.

    You are encouraged to ask and answer questions on the course discussion board. (However, do not share your answers in the course discussion board.)

    Practice Exercises (no submission required)

1. Answer the multiple choice questions in text section 5.9 that have been covered in the lectures.

  1. 2  Complete the questions 2 through 5 in text section 5.9.
  2. 3  Look up the Unix command table in Appendix B.1 and read the Prolog tutorial in B.4.
  3. 4  Log onto general.asu.edu and execute the Unix commands and Prolog exercises.
  4. 5  Follow the Prolog tutorial in Text Appendix B.4 or the tutorials given in the course Web site, start GNU Prolog and try following simple programs (build-in functions). Don’t forget the period at the end of the statement.
    | ?- write(hello). % hello is a constant */

    | ?-write(Hello). % Hello is a variable. Its address will be displayed*/

    | ?- write(‘hello world’). % a string is printed */
    | ?- read(Y), write(‘The variable entered is ‘), write(Y), nl. /* nl prints a newline. Type a period and an enter at the end of the input */
    | ?-Xis2+2.

| ?-Yis5*8.
| ?- Y is 2**10.
| ?- length([a, b, x, y, 2, 45, z], L).

CSE240 – Introduction to Programming Languages Homework 13

1 | P a g e

| ?- append([a, b, c, d], [4, 6, 8], LL).
| ?- append(X,Y,[a,b,c]). Then, type “;” to obtain all possible answers. | ?- X is [1 | [2 | [3 | []]]], write(X). Explain the output.

6 Use trace and notrace before and after your questions in Question 5. For example

|?- trace.
| ?- X is [1 | [2 | [3 | []]]], write(X). Explain the output. |?- notrace.

Programming Assignment (50 points)

1. Consider the following diagram of a family tree:

[25 points]

/* Database for family tree. It consists of facts and rules. */

/* The section in the highlighted box above has been completed for you */

/* Insert your facts into the right place to keep the same facts together */

/* Facts */
male(abe).
male(rob).
male(jim).
female(joy).
female(ana).

father_of(abe, ana). /* abe is the father of ana */

CSE240 – Introduction to Programming Languages Homework 13

2 | P a g e

father_of(abe, rob).
father_of(abe, jim).
mother_of(joy, rob).
mother_of(joy, jim).
mother_of(joy, ana).
/* Rules */
is_male(X) :-

male(X);

/* abe is the father of ana */
/* abe is the father of ana */
/* joy is the mother of rob */
/* joy is the mother of rob */
/* joy is the mother of rob

father_of(X, _).
/* Add your rules and indicate the question number */

Enter the program using a text editor such as pico under Unix operation system and save the file as family_tree.pl. You may enter the program on your own computer using notepad and upload the program into your cse240prolog directory in general server.

Compile the program using the Prolog command:

> gplc family_tree.pl

Enter GNU Prolog programming environment by executing the Unix command gprolog. Execute the program family_tree by typing GNU Prolog command

|?- [family_tree].

To exit from GNU Prolog, type your end-of-file character at the main Prolog prompt ^d (Ctrl-d).

| ?- ^d
Ask questions by typing, e.g.:

You can find a complete set of GNU Prolog commands at http://www.gprolog.org/manual/gprolog.html.

Now, you can start to add your code into the program.

1.1 Complete the program by adding facts for the remaining members on the family tree. The section inside of the highlighted box has already been completed for you for clarification. Please pay close attention when adding the remaining famiy members, spelling counts and all letters should be lowercase. [5]

For all of the following questions, please label them. For example, if Question 1.0 asks you to define a rule called is_male (X) that returns “yes” (or “true”) if X is the father of a member of the family, then your code should look like:

/* Question 1.0 */ is_male(X) :-

CSE240 – Introduction to Programming Languages 3 | P a g e Homework 13

|?- father_of(mark, beth).
|?- mother_of(beth, tom).

male(X); father_of(X, _).

  1. 1.2  Define and add a rule into the database: is_female(X). It returns “yes” (or “true”) if X is a female or the mother of a member of the family. [3]

    Note: the system will return a “yes”, if it finds a “true” answer and there exist no more true answers. The system will return “true ?” if it finds a “true” answer and there are still possibly further matches. In this case, if you type “enter”, it will return “yes” and stop. If you type “;”, it will continue to search for further answers.

  2. 1.3  Define and add a rule into the database: grandmother_of(X, Z) that returns “yes” (or “true”) if X is a grandmother of Z. Define another rule called grandfather_of(X, Z) that returns “yes” (or “true”) if X is a grandfather of Z. [3]
  3. 1.4  Define and add a rule into the database: sibling_of(X, Y) that returns “yes” (or “true”) if X is a sibling of Y. [3]
  4. 1.5  Define and add a rule into the database: parent_of(X, Y) that returns “yes” (or “true”) if X is a parent of Y. [3]
  5. 1.6  Define and add a rule into the database: descendent_of(X, Y) that returns “yes” (or “true”) if X is a descendent of Y. Note: you will need to use recursion as well as the rule defined

above. [3]

1.7 Define and add a rule into the database: familyquestions. The question tests all the rules that you have added in the database in the previous questions. Use AND “,” to connect the subquestions and make sure that all your subquestions have a true answer. Add “write” statements to print the information. [5]

2 Consider the following database for the meals of lunch and dinner. [20 points]

lunch

dinner

entre: sandwhich sides:
chips

– potato

– salt lemonade

– lemon – sugar

entre: spaghetti sides:
bread

– egg

– flour greentea

– green – tea

2.1 Create facts for the entres: entre(X, Y) where X is lunch or dinner and Y is the entre.

CSE240 – Introduction to Programming Languages 4 | P a g e Homework 13

Create facts for the side: side(X, Y) where X is lunch or dinner Y is the side.

Create facts for the ingredients : ingredient(X, Y) where X is the side and Y is the ingredient. [10]

  1. 2.2  Create a rule meal(X, Y) where X is lunch or dinner and Y is the food that will be served. For example, meal(lunch, X) should return sandwhich, chips, and lemonade. [5]
  2. 2.3  Create a rule shoppinglist(X, Y) where X is lunch or dinner and Y is the ingredient needed.

For example, shoppinglist(dinner, X) should return egg, flour, green, and tea.

3 Write prolog recursive rule count3(L, P, Z, N), where. L is a list of integers,

P is the number of positive numbers in L; Z is the number of zeros in L;
N is the number of negative numbers in L; Test case:

Grading of Programming Assignment

The TA will grade your program following these steps:

[5] [5 points]

(1) Compile the code. If it does not compile, 20% of the points given will be deducted. For example, if there are 20 points possible, you will earn 16 points if the program fails to compile.

(2) The TA will read your program and give points based on the points allocated to each component, the readability of your code (organization of the code and comments), logic, inclusion of the required functions, and correctness of the implementations of each function.

What to Submit?

You are required to submit your solutions in a compressed format (.zip). Make sure your compressed file is label correctly – lastname_firstname13.zip.
The compressed file MUST contain the following:

CSE240 – Introduction to Programming Languages 5 | P a g e Homework 13

family_tree.pl meals.pl count3.pl

No other files should be in the compressed folder.

If multiple submissions are made, the most recent submission will be graded. (Even if the assignment is submitted late.)

Where to Submit?

All submissions must be electronically submitted to the respected homework link in the course web page where you downloaded the assignment.

Late submission deduction policy

  •   No penalty for late submissions that are received within 24 hours after the deadline;
  •   10% grade deduction for every day it is late after the grace period;
  •   No late submission after Tuesday at 11:59PM.

CSE240 – Introduction to Programming Languages 6 | P a g e Homework 13