CS 360
Winter 2016
Programming Language Concepts
CS 360-001 Tuesday/Thursday 15:30-16:50 (Rush 014)
CS 360-002 Tuesday/Thursday 14:00-15:20 (Rush 014)
CS 360-003 Tuesday 18:30-21:20 (University Crossings 153)

Instructor:
Geoffrey Mainland
mainland+cs360@drexel.edu
Office: University Crossings 106
Office hours: Mondays 3pm–5pm; Thursdays 5pm–6pm.
Teaching Assistants:
Pavan Kantharaju
Matthew Roll
Warning! This material is for an old version of the course.

In this assignment, you will write several Prolog functions.

You must implement the functions as specified. You may write other helper functions and define test data in your file, but you may not change the functions’ names or the number or order of arguments.

This assignment is worth 40 points. There are 41 possible points.

Standard homework instructions

Your code must run either on tux or on the course VM using the version of gprolog that we provide.

A solution template is available in the DrexelCS360/homeworks GitHub repository. You should only need to modify the files provided by the template—please do not check in any files beyond those that the template provides.

Your solutions to all problems must be in the file hw4/hw4.pl in your CS360 repository.

Using gprolog

Below are a few tips for using gprolog (taken from here).

Please see the list of Programming Languages Resources for links to supplemental Prolog material.

Starting the interpreter

Unfortunately gprolog does not have a nice interface like DrRacket. You will have to use it from the command like, either on tux or on the course VM. For example:

[mainland@curry ~]$ gprolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?-

Exiting the interpreter

In order to exit Prolog, at the | ?- prompt type halt. Note that you must type a period after the command halt in the interpreter. This will return you to the shell terminal prompt.

| ?- halt.
[mainland@curry ~]$

Loading a file

Completing an assignment in Prolog can be a work of frustration if, each time you start the Prolog interpreter, you have have to re-enter the code you were working on. Prolog has a procedure for loading a file containing code you have defined into the current environment. The command to load a file is consult. The following example demonstrates loading a file named hw4.pl, and then listing the file:

[mainland@curry hw4]$ gprolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- consult('hw4.pl').
compiling /home/mainland/cs360/hw4/hw4.pl for byte code...
/home/mainland/cs360/hw4/hw4.pl:1: warning: singleton variables [X,L] for sum/2
/home/mainland/cs360/hw4/hw4.pl:3: warning: singleton variables [X,L] for min/2
/home/mainland/cs360/hw4/hw4.pl:5: warning: singleton variables [X,Y] for min_sort/2
/home/mainland/cs360/hw4/hw4.pl:7: warning: singleton variables [X] for sentence/1
/home/mainland/cs360/hw4/hw4.pl compiled, 11 lines read - 581 bytes written, 3 ms

yes
| ?- listing.

sentence(_).

min(_, _).

min_sort(_, _).

sum(_, _).

yes
| ?- 

Problem 1: sum( X, L ) (10 points)

Write a Prolog program to find the sum, X, of a list of numbers, L.

Hint: Remember to use is instead of = to state a constraint that a variable is the value of an arithmetic expression.

Problem 2: min( X, L ) (10 points)

Write a Prolog program to find the minimum, X, of a list of numbers, L.

Here is an example interpreter session:

| ?- min( 3, [ 2, 3, 4] ) .

no
| ?- min( 3, [] ) .

no
| ?- min( 3, [9,3,48] ) .

true ?

yes
| ?- min( X, [289,923,2983] ) .

X = 289 ? ;

no

Hint: Write down a recursive specification for what it means for a number to be the minimum of a list of numbers.

Problem 3: min_sort( X, Y ) (10 points)

Write a Prolog program that sorts a list of numbers using min sort. min_sort( X, Y ) is true if Y is sorted, and contains the same elements as X. Recall that min sort finds the minimum element of the list and then recursively sorts the list obtained by removing the minimum element.

Hint: Use your min function from the previous problem in your solution to this problem.

Here is an example interpreter session:

| ?- min_sort([3,2,1],Y).

Y = [1,2,3] ? ;

no
| ?- 

Problem 4: sentence( X ) (10 points)

Consider the simple grammar for sentences on page 208 of Programming Languages: Principles and Practice (reproduced below). Write a Prolog program that parses sentences (represented as lists of words) using this grammar.

This grammar states that a sentence consists of a noun phrase, followed by a verb phrase, followed by a period. It also states that an article is either the word “a” or the word “the”. We will see more grammars later in the course—if it is not clear how this grammar specifies the structure of a sentence, please ask us to clarify on the discussion board.

Hint: A list of words is a sentence if the list is obtained by appending a list which is a noun phrase, a list which is a verb phrase, and a list whose single element is a period. Your program can be used to check if a given sentence, i.e., parse, can be generated by the grammar.

Here is an example interpreter session:

| ?- sentence( [ the, girl, sees, a, dog, '.' ] ) .

true ?

Can you use the same program to generate all possible sentences that can be derived from the grammar? Does the order of the subgoals in your rules make a difference? You must answer these two questions in a comment in your submission.

Problem 5: Homework Statistics (1 point)

How long did it take you to complete each problem? Please tell us in a comment in hw4.pl. You must tell us how long each problem took you to receive the point.