Docsity

Assignment 5: Prolog Program - Programming Languages | ECS 140A, Assignments of Programming Languages

Toggle sidebar
bg1
Assignment 5: Prolog Program
Due: Thursday, 4 June 2009 at 11:59pm
Overview
The purpose of this assignment is for you to gain some experience designing and implementing Prolog
programs. This assignment is broken into three parts. The first part is a fairly straightforward Prolog warm-
up. Part 2 involves writing a set of simple programs for manipulating lists and variables. Part 3 requires you
to use Prolog’s control mechanism to implement a predicate for scheduling.
Part 1: Simple Queries
You will be given sets of facts of the following form:
1. course(number, prereq, units)
Here number is an atom denoting the course number (for instance, ecs140a), prereq a list of course
numbers, and units a number indicating the number of units associated with a course.
2. student(name, courses taken)
Here name is an atom denoting a student’s name, and courses taken is a list of courses that the
student has taken.
3. instructor(name, course list)
Here name specifies the name of the instructor who teaches the set of courses in the list course list.
An example database of facts is shown below:
course(ecs40, [ecs30], 4).
course(ecs122a, [ecs100, ecs110], 3).
.
.
.
student(john, [ecs30, ecs40]).
.
.
.
instructor(jim, [ecs30, ecs110]).
Write the following separate queries:
(a) Find all courses with 3 or 4 credits (fc course).
(b) Find all courses whose immediate pre-requisite is ecs110 (prereq 110).
(c) Find names of all students in ecs140a (ecs140a students).
(d) Find the names of all instructors who teach john’s courses (instructor names).
1
pf3
pf4

Partial preview of the text

Download Assignment 5: Prolog Program - Programming Languages | ECS 140A and more Assignments Programming Languages in PDF only on Docsity!

Assignment 5: Prolog Program

Due: Thursday, 4 June 2009 at 11:59pm

Overview

The purpose of this assignment is for you to gain some experience designing and implementing Prolog programs. This assignment is broken into three parts. The first part is a fairly straightforward Prolog warm- up. Part 2 involves writing a set of simple programs for manipulating lists and variables. Part 3 requires you to use Prolog’s control mechanism to implement a predicate for scheduling.

Part 1: Simple Queries

You will be given sets of facts of the following form:

  1. course(number, prereq, units)

Here number is an atom denoting the course number (for instance, ecs140a), prereq a list of course numbers, and units a number indicating the number of units associated with a course.

  1. student(name, courses taken)

Here name is an atom denoting a student’s name, and courses taken is a list of courses that the student has taken.

  1. instructor(name, course list)

Here name specifies the name of the instructor who teaches the set of courses in the list course list. An example database of facts is shown below:

course(ecs40, [ecs30], 4). course(ecs122a, [ecs100, ecs110], 3). . . . student(john, [ecs30, ecs40]). . . . instructor(jim, [ecs30, ecs110]).

Write the following separate queries:

(a) Find all courses with 3 or 4 credits (fc course). (b) Find all courses whose immediate pre-requisite is ecs110 (prereq 110). (c) Find names of all students in ecs140a (ecs140a students). (d) Find the names of all instructors who teach john’s courses (instructor names).

(e) Find the names of all students who are in jim’s class (students). (f) Find all pre-requisites of a course (allprereq). (This will involve finding not only the immediate prerequisites of a course, but pre-requisite courses of pre-requisites and so on.)

When you are through testing your queries interactively, prepare them for use by the testing program by defining predicates with the names shown above.

Part 2: List Manipulation Predicates

The goal of this part of the homework is to familiarize you with the notions of lists and predicate definitions in Prolog. This part requires you to define a number of simple predicates:

  1. Write a predicate, all length, that takes a list and counts the number of atoms that occur in the list at all levels.

?-all_length([a, b, c], X) X = 3; no ?-all_length([a, [b,c], [d,[e,f]]], Y) Y = 6; no

  1. Define a predicate, equal a b(L), which returns true if L contains an equal number of a and b terms.
  2. Define a predicate, swap prefix suffix(K,L,S), such that swap prefix suffix(K,L,S) is true if - K is a sub-list of L, and - S is the list obtained by appending the suffix of L following an occurrence of K in L, with K and with the prefix that precedes that same occurrence of K in L. ?-swap_prefix_suffix([c, d], [a, b, c, d, e], S). yes. S=[e, c, d, a, b] ?-swap_prefix_suffix([c, e], [a, b, c, d, e], S). no. ?-swap_prefix_suffix(K, [a, b, c, d, e], [b, c, d, e, a]). yes. K=[a] Look at the class notes for the definitions of prefix, suffix, and sublist.
  3. Define a predicate, palin(A) that is true if the list A is a palindrome, that is, it reads the same backwards as forwards. For instance, [1, 2, 3, 2, 1] is a palindrome, but [1, 2] is not.
  4. A good sequence consists either of the single number 0, or of the number 1 followed by two other good sequences: thus, [1,0,1,0,0] is a good sequence, but [1,1,0,0] is not. Define a relation good(A) that is true if A is a good sequence.
  • use is for arithmetic assignment and = for binding (make sure that you understand this important difference!). Also, do not forget to handle base cases (e.g., empty list) and to specify cases in the right order.
  • The test program will be provided. It exercises the predicates that you write; hence there is no test data. Details regarding the test program will appear on the class web page. You may define additional helper predicates that your main predicates use. Be sure, though, to name the main predicates as specified since the test program uses those names.
  • When developing your program, you might find it easier to test your predicates first interactively before using the test program.
  • Grading will be divided as follows:

Part Percentage 1 30 2 45 3 25

  • A message giving exact details of what to turn in, where the provided test files are, etc., will be posted to the newsgroup and the class web page.
  • Get started now to avoid the last minute rush.