CSE307
Spring 2009
Stony Brook
Principles of Programming Languages
Annie Liu
Assignment 1
Handout A1
Jan. 29, 2009
Due Feb. 10

Comparing Languages --- Reading Facts

This assignment has two goals: writing programs to read data in the form of facts, and comparing languages while doing this.

Facts generalize tables and relations in relational databases and are data in logic databases. They are mostly flat tuples as in databases, spreadsheets, etc., but may also have nested structures in components of the tuples. Therefore, they are excellent examples of the kinds of data one need to process in most applications. So we will start with reading data in the form of facts.

Reading facts

We will read facts such as follows:

enrolled(amy, 307, s09).
enrolled(amy, 305, f09).
enrolled(tom, 308, s09).
enrolled(tom, 307, s09).
assignment(307, 1, readfacts).
assignment(307, 2, parserules).
exercise(307, 1, buildtree(write(div(sum,2)))).
enrolled(joe, 306, s07).
Precisely, each fact is of the form
p(v1,...,vk).  
where p is the name of a relation, also called predicate, and each of v1 through vk is a value that may be an integer, the name of an object, or a structure the form
c(v1,...,vj)
where c is the name of a constructor, and each of v1 through vj is recursively a value.

For simplicity, you assume that each fact is specified on a single, separate line.

Your program should be able to read any number of facts of any relation name and any number of argument values. Ideally, your program should also be able to read argument values of any number of nesting levels, but if you have trouble handling them, then state this explicitly, and you can handle values of at most one level of nesting and receive most (80%) of the credits (the point is that you don't need to write messy code that does not work and is hard to grade, and you get more points off as a result), i.e., you could write buildtree(exp) instead of buildtree(write(div(sum,2))) in the example fact about exercise above. Your program should build internal object structures to represent the facts.

Finally, your program should write all facts out in sorted fashion, sorted by the relation name, the argument values from left to right, and the constructor names from outside to inside and from left to right, in that order. To get some statistics, your program should also compute the total numbers of facts, and number of facts for each relation. For example, for the sample facts given above, the following results may be produced:

facts sorted:
assignment(307, 1, readfacts).
assignment(307, 2, parserules).
enrolled(amy, 305, f09).
enrolled(amy, 307, s09).
enrolled(joe, 306, s07).
enrolled(tom, 307, s09).
enrolled(tom, 308, s09).
exercise(307, 1, buildtree(write(div(sum, 2)))).

total number of facts: 8
number of facts of assignment: 2 
number of facts of enrolled: 5
number of facts of exercise: 1

When run, your program should take 2 arguments from command line: an input file name and an output file name; read data from the input file; and write the results to the output file, containing the sorted facts and the statistics.

Comparing languages

Write your program in 3 different languages: Java, C/C++, and ML (or whichever language you learned in CSE113, or another language that you choose). See pointers to these languages under Other Resources on the course homepage.

In addition to the usual requirements, your README file should compare your programming experience in these different languages. What was easy or hard in which languages? What features of which languages make which parts of the code clearer or messier? Are there noticeable differences in speed when you try on larger input data? Be sure to describe any features of your code that the TA might not immediately notice.

Extra credit suggestions

Write your program in some other languages. A language that differs more significantly from the ones above, such as Lisp, Scheme, Haskell, and Prolog, would be particularly interesting.


Handins

Hand in everything electronically, using Blackboard, by 5pm on the due date. Your handin should include your README file, code, test data, and anything else you have to show your work.

Grading

This assignment will be graded based on 100 points, allocated in proportion to the estimated amount of work for each part. You may do this assignment in a team of two people; the two people will receive the same points. Exceptionally well thought-out and well written work will receive appropriate extra credit. Extra credit work will be graded based on the estimated amount of extra work.