



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




COP 4020 — Programming Languages 1 November 29, 2007
Due: exercises 1–2 and 4–6, Monday, November 19, 2007 at 11pm; exercises 7 and 9, Monday, November 26, 2007 at 11pm. In this homework you will learn about the message passing model and basic techniques of programming in that model. The programming techniques include using port objects [Concepts] [UseModels]. A few problems also make comparisons with the other models we have studied, and also with message passing features in other languages [EvaluateModels] [MapToLanguages]. Your code should be written in the message passing model, so you should not use cells and assignment in your Oz solutions. You should use helping functions whenever you find that useful. Unless we specifically say how you are to solve a problem, feel free to use any functions from the Oz library (base environment), especially functions like Map and FoldR. For all Oz programing tasks, you must run your code using the Mozart/Oz system. For these you must also provide evidence that your program is correct (for example, test cases). Oz code with tests for various problems is available in a zip file, which you can download from the course resources web page. For testing, you may want to use tests based on our code in the course library file TestingNoStop.oz. Turn in (on WebCT) your code and, if necessary, output of your testing for all questions that require code. Please upload text files with your code that have the suffix .oz (or .java as appropriate), and text files with suffix .txt that contain the output of your testing. Please use the name of the main function as the name of the file. (In any case, don’t put any spaces in your file names!) If we provide tests and your code passes all of them, you can just indicate with a comment in the code that your code passes all the tests. Otherwise it is necessary for you to provide us test code and/or test output. If the code does not pass some tests, indicate in your code with a comment what tests did not pass, and try to say why, as this enhances communication and makes commenting on the code easier and more specific to your problem than just leaving the buggy code without comments. If you’re not sure how to use our testing code, ask us for help. Your code should compile with Oz, if it doesn’t you probably should keep working on it. If you don’t have time, at least tell us that you didn’t get it to compile. Be sure to clearly label what problem each area of code solves with a comment. Don’t hesitate to contact the staff if you are stuck at some point. Read Chapter 5 of the textbook [RH04]. (See the syllabus for optional readings.)
A Box is like a Cell, in that it holds a value (of any type). The function call {NewBox X} returns a new Box containing the value X. The procedure call {BoxExchange B Old New} atomically binds Old to the value in box B and makes New be the new value contained in Box B. The pro- cedure call {BoxAssign B V} makes the value V be the new value of Box B. The function call {BoxAccess B} returns the value contained in Box B. Your code should pass the tests shown in Figure 1 on the following page
% $Id: BoxTest.oz,v 1.1 2007/11/09 21:32:07 leavens Exp leavens $ \insert ’Box.oz’ \insert ’TestingNoStop.oz’ declare B1 B2 V1old V2old {StartTesting ’Box operations’} B1 = {NewBox 1} B2 = {NewBox 2} {BoxExchange B1 V1old 7} {BoxExchange B2 V2old 99} {Test V1old ’==’ 1} {Test V2old ’==’ 2} declare V1x V2x {BoxExchange B1 V1x 88} {BoxExchange B2 V2x 333} {Test V2x ’==’ 99} {Test V1x ’==’ 7} {Test {BoxAccess B1} ’==’ 88} {Test {BoxAccess B2} ’==’ 333} {Test {BoxAccess B2} ’==’ 333} {Test {BoxAccess B1} ’==’ 88} {BoxAssign B1 4} {Test {BoxAccess B1} ’==’ 4} {Test {BoxAccess B2} ’==’ 333} {BoxAssign B1 asymbolliteral} {Test {BoxAccess B1} ’==’ asymbolliteral} declare X=1 Y=2 Z= B={NewBox Z} {StartTesting ’Some equations’} {Test {BoxAccess {NewBox X}} ’==’ X} {BoxAssign B Y} {Test {BoxAccess B} ’==’ Y}
Figure 1: Testing code for Exercise 1 on the previous page.
% $Id: PortAsCellTest.oz,v 1.2 2007/11/18 14:09:59 leavens Exp leavens $ \insert ’PortAsCell.oz’ \insert ’TestingNoStop.oz’
{StartTesting ’MyNewPort’} % Simulating basic semantics of NewPort and Send declare Strm Port in Port = {MyNewPort Strm} {StartTesting ’MySend’} {MySend Port 3} {MySend Port 4} % Must use List.take, otherwise Test suspends... {Test {List.take Strm 2} ’==’ [3 4]} {MySend Port 5} {MySend Port 6} {Test {List.take Strm 4} ’==’ [3 4 5 6]}
{StartTesting ’MyNewPort second part’} declare S2 P2 U1 U2 in P2 = {MyNewPort S2} {StartTesting ’MySend second part’} {MySend P2 7} {MySend P2 unit } {MySend P2 true } {MySend P2 U1} {MySend P2 hmmm(x:U2)} U1 = 4020 {Test {List.take S2 5} ’==’ [7 unit true 4020 hmmm(x:U2)]} {Test {List.take Strm 4} ’==’ [3 4 5 6]}
Figure 2: Testing code for Exercise 2 on the preceding page.