Test on Declarative Concurrency, the Message Passing Model, and Programming Models vs. Problems

Similar documents
Homework 4: Declarative Concurrency

Special Directions for this Test

Homework 5: Message Passing

Homework 4: Declarative Concurrency

Exam for 2G1512 DatalogiII, Apr 12th

Special Directions for this Test

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Special Directions for this Test

Programming Language Concepts, cs2104 Lecture 09 ( )

Declarative Concurrency (CTM 4)

Shared state model. April 3, / 29

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Programming Language Concepts, cs2104 Lecture 04 ( )

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CISC 1600 Lecture 3.1 Introduction to Processing

CS558 Programming Languages

Topic 1: Introduction

Lecture 21: Relational Programming II. November 15th, 2011

T H E I N T E R A C T I V E S H E L L

Declarative concurrency. March 3, 2014

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Repetition Structures

CSE 331 Final Exam 3/12/12

Final Examination: Topics and Sample Problems

Mini-ML. CS 502 Lecture 2 8/28/08

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

F28PL1 Programming Languages. Lecture 12: Standard ML 2

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CIS 120 Midterm I October 2, Name (printed): Username (login id):

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Declarative Concurrency (CTM 4)

Declarative Programming Techniques

CSE P 501 Exam 8/5/04

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Maciej Sobieraj. Lecture 1

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

WELCOME! (download slides and.py files and follow along!) LECTURE 1

An Oz Subset. 1 Microsyntax for An Oz Subset. Notational Conventions. COP 4020 Programming Languages 1 January 17, 2012

Programming Languages and Compilers (CS 421)

Programming Language Concepts, CS2104 Lecture 7

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS558 Programming Languages

Programming Lecture 3

Option Values, Arrays, Sequences, and Lazy Evaluation

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CS 3360 Design and Implementation of Programming Languages. Exam 1

Intro. Scheme Basics. scm> 5 5. scm>

Sometimes it s good to be lazy

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

egrapher Language Reference Manual

CS164: Midterm I. Fall 2003

COP Programming Assignment #7

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CSCC24 Functional Programming Scheme Part 2

CS450 - Structure of Higher Level Languages

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Part I: Written Problems

CS 3360 Design and Implementation of Programming Languages. Exam 1

CGS 3066: Spring 2015 JavaScript Reference

15 150: Principles of Functional Programming. Exceptions

UMBC CMSC 331 Final Exam

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CSE3322 Programming Languages and Implementation

CMSC 201 Fall 2016 Homework 6 Functions

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

List Functions, and Higher-Order Functions

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 320: Concepts of Programming Languages

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Functional Programming and Haskell

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

1 Inheritance (8 minutes, 9 points)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CS558 Programming Languages

Mutable References. Chapter 1

CSE341, Fall 2011, Midterm Examination October 31, 2011

CMSC 330: Organization of Programming Languages. Operational Semantics

Functional Programming

Text Input and Conditionals

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Computer Components. Software{ User Programs. Operating System. Hardware

Transcription:

Fall, 2011 Name: 1 (Please don t write your id number!) COP 4020 Programming Languages 1 Test on Declarative Concurrency, the Message Passing Model, and Programming Models vs. Problems Special Directions for this Test This test has 9 questions and pages numbered 1 through 10. This test is open book and notes. If you need more space, use the back of a page. Note when you do that on the front. Before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your programs are sloppy and hard to read, you may lose some points. Correct syntax also makes a difference for programming questions. You will lose points if you do not follow the grammar when writing programs! When you write Oz code on this test, you may use anything in the demand-driven declarative concurrent model (as in chapter 4) and the message passing model (chapter 5). The problem may say which model is appropriate. However, you must not use imperative features (such as cells and assignment). (Furthermore, note that these models do not include the primitive IsDet or the library function IsFree; thus you are also prohibited from using either of these functions in your solutions.) But please use all linguistic abstractions and syntactic sugars that are helpful. You are encouraged to define functions or procedures not specifically asked for if they are useful to your programming; however, if they are not in the Oz base environment, then you must write them into your test. (This means you can use functions in the Oz base environment such as Map, FoldR, Filter, Pow, Length, Wait, IntToFloat, etc.) In the message passing model you can use NewPortObject and NewPortObject2 as if they were built-in. Recall that the grammar for the type of infinite streams with elements of type T is as follows. IStream T ::= T IStream T For Grading Question: 1 2 3 4 5 6 7 8 9 Total Points: 5 5 10 15 10 10 20 15 10 100 Score:

2 1. (5 points) [Concepts] Consider the following Oz program and test. \insert 'TestingNoStop.oz' declare fun {ListFromToOffset Initial Limit Offset} fun {From N} N {From N+1} fun {AddEach H T} Offset+H {AddEach T} IntsFromN = thread {From Initial} Added = thread {AddEach IntsFromN} in {List.take Added (((Limit-Offset)-Initial)+1)} {Test {ListFromToOffset 0 14 10} '==' [10 11 12 13 14]} The above program doesn t pass the test given at its. Briefly (a) state what the problem with the program is, and briefly say (b) what simple change would fix it (without changing the test). 2. (5 points) [Concepts] Consider the following Oz program. local A B C D E in fun lazy {Compute X} 10*X thread A = [B+0 C D E] thread B = C + E*100 thread C = {Compute D} thread D = 1 thread E = 50 + D {Wait A} {Wait B} {Wait C} {Wait D} {Wait E} {Browse A} Which of the following correctly describes what, if anything, is shown in the Browser after this program runs? A. It is impossible to tell, because the threads cause so many race conditions, the output of the program can t be predicted with any certainty. B. The program susps and never shows anything in the Browser. C. The program shows the name A in the Browser.. D. The program shows the list [5110 10 1 51] in the Browser.. E. The program shows the list [1 5 1 1001] in the Browser.. F. The program shows the list nil in the Browser.. G. The program encounters an error or throws an exception.

3 3. [Concepts] Consider the following program in the message passing model of Oz. \insert 'NewPortObject.oz' declare fun {NewProcServer} {NewPortObject state_is_ignored fun {$ State Msg} case Msg of run(p) then {P} State } PS = {NewProcServer} local Ten in Twelve proc {DelaySecs N?Var} {Delay N*1000} Var=done {S PS run(proc {$ } {DelaySecs 10 Ten} )} {S PS run(proc {$ } {DelaySecs 12 Twelve} )} % part (a) asks how long it takes to get to here {Wait Ten} {Wait Twelve} {Browse all_done} % part (b) asks how long it takes to get here You may recall that the procedure Delay waits at least the given number of milliseconds. (a) (5 points) Circle the letter of the choice below that correctly describes the behavior of this program on a modern computer. A. The two messages sent to the port PS should be both be delivered to that port within 1 second after the program starts. B. The two messages sent to the port PS should both be delivered to the port no sooner than 10 seconds after the program starts, but no later than 11 seconds after the program starts. C. The two messages sent to the port PS should both be delivered to the port no sooner than 12 seconds after the program starts, but no later than 13 seconds after the program starts. D. The two messages sent to the port PS should both be delivered to the port no sooner than 22 seconds after the program starts, but no later than 23 seconds after the program starts. (b) (5 points) Circle the letter of the choice below that correctly describes the behavior of this program on a modern computer. A. The program should finish executing, and it shows all_done in the Browser in no less than 2 seconds, but no more than 9 seconds. B. The program should finish executing, and it shows all_done in the Browser in no less than 10 seconds, but no more than 12 seconds. C. The program should finish executing, and it shows all_done in the Browser in no less than 12 seconds, but no more than 20 seconds. D. The program should finish executing, and it shows all_done in the Browser in no less than 22 seconds, but no more than 30 seconds. E. The program should finish executing, and it shows all_done in the Browser in no less than 50 seconds, but no more than 60 seconds.

4. [EvaluateModels] For each of the following, name the programming model which we studied that is the least expressive programming model that can easily solve the problem and briefly justify your answer. (a) (5 points) A program that is a structured document, such as an HTML page, and must produce a document with the same structure but without images (which appear in the original structure as a certain kind of node). 4 (b) (5 points) A program that tests a given function, by generating an infinite series of increasingly complex test cases, and feeding them into the function until either the function breaks or some previously specified limit on the complexity of the tests is reached. (c) (5 points) A program that tracks shipping of packages, where many indepent web browsers can s inquiries and many of the company s employees can update the status of packages simultaneously.

5 5. (10 points) [UseModels] Using the demand-driven concurrent model, write a function PowersOfN : <fun {$ <Int>}: <IStream Int> > that takes one argument, an Int N, and lazily returns an IStream of Ints where the ith element (counting from 1) is the value N i 1. The following has several examples. \insert 'TestingNoStop.oz' \insert 'PowersOfN.oz' {StartTesting 'PowersOfNTest $Revision: 1.1 $'} {Test {List.take {PowersOfN 2} 10} '==' [1 2 4 8 16 32 64 128 256 512]} {Test {List.take {PowersOfN 3} 8} '==' [1 3 9 27 81 243 729 2187]} {Test {List.take {PowersOfN 4} 5} '==' [1 4 16 64 256]} {Test {List.take {PowersOfN 5} 18} '==' [1 5 25 125 625 3125 15625 78125 390625 1953125 9765625 48828125 244140625 1220703125 6103515625 30517578125 152587890625 762939453125]} {Test {List.take {PowersOfN 10} 7} '==' [1 10 100 1000 10000 100000 1000000]} {DoneTesting}

6 6. (10 points) [UseModels] Using the demand-driven concurrent model, write an incremental function Averages : <fun lazy {$ <IStream <List Float>>}: <IStream Float> > that takes an infinite streams of non-empty, finite Lists of Floats, ISF, and lazily returns an infinite stream of Floats, with the ith Float in the result being the average of all the Floats in the ith list in ISF. Remember that in Oz you cannot mix Floats and Ints in arithmetic expressions! The following are examples, using the WithinTest procedure from the homework. \insert 'FloatTesting.oz' \insert 'Averages.oz' declare fun lazy {RepeatingListOf Elements} % A helper for testing only {App Elements {RepeatingListOf Elements}} fun {FromToBy From To By} % A helper for testing only %% REQUIRES: By \= 0.0 and none of From, To, or By is NaN. %% ENSURES: result is the finite list [From From+By From+2*By... To] if (By > 0.0 andthen From > To) orelse (By < 0.0 andthen From < To) then nil else From {FromToBy From+By To By} {StartTesting 'AveragesTest $Revision: 1.3 $'} {WithinTest {List.take {Averages {RepeatingListOf [[1.0] [1.0 2.0] [1.0 2.0 3.0] [1.0 2.0 3.0 4.0] {FromToBy 1.0 100.0 1.0} {FromToBy 1.0 1000.0 1.0} ]}} 7} '~=~' [1.0 1.5 2.0 2.5 50.5 500.5 1.0]} {WithinTest {List.take {Averages {RepeatingListOf [[~1.0] [~1.0 ~2.0] [~1.0 ~2.0 3.0] [~1.0 ~2.0 3.0 4.0] {FromToBy ~1.0 ~100.0 ~1.0} {FromToBy ~1.0 ~1000.0 ~2.0} ]}} 7} '~=~' [~1.0 ~1.5 0.0 1.0 ~50.5 ~500.0 ~1.0]} {WithinTest {List.take {Averages {RepeatingListOf [{FromToBy 0.0 6000.0 3.0} {FromToBy 0.0 3.0e3 100.0} ]}} 4} '~=~' [3000.0 1500.0 3000.0 1500.0]} {WithinTest {List.take {Averages {DoneTesting} {RepeatingListOf {Map {FromToBy 1.0 10.0 1.0} fun {$ N} [0.0 N 2.0*N] }}} 10} '~=~' {FromToBy 1.0 10.0 1.0}}

7 7. (20 points) [UseModels] Using the message passing model in Oz, write a function NewAccumulator: <fun {$ }: <Port>> that takes no arguments and returns a port that can be used to s messages to a new port object. The port object acts remembers the value of an integer, which represents the port object s accumulator. (You might think of the port object as a kind of calculator.) This port object responds to the following messages: add(m), where M is an Int, which adds M s value to the accumulator s value. subtract(m), where M is an Int, which makes the accumulator become its old value minus the value of M. multiply(m), where M is an Int, which makes the accumulator become its old value times the value of M. square, which makes the accumulator become the square of its old value. enter(m), where M is an Int, which makes the accumulator s value become the value of M. getvalue(?variable), where Variable is an undetermined dataflow variable. This unifies the accumulator s value with Variable, and leaves the accumulator unchanged. These are the only possible messages that can be sent to the Port returned by NewAccumulator. The following are tests. \insert 'TestingNoStop.oz' \insert 'Accumulator.oz' {StartTesting 'AccumulatorTest $Revision: 1.2 $'} Acc = {NewAccumulator} A2 = {NewAccumulator} {Test {S Acc getvalue($)} '==' 0} {Test {S A2 getvalue($)} '==' 0} {S Acc add(7)} {Test {S Acc getvalue($)} '==' 7} {S Acc subtract(5)} {Test {S Acc getvalue($)} '==' 2} {S Acc enter(10)} {Test {S Acc getvalue($)} '==' 10} {S Acc square} {Test {S Acc getvalue($)} '==' 100} {S Acc multiply(5)} {Test {S Acc getvalue($)} '==' 500} {S Acc multiply(8)} {Test {S Acc getvalue($)} '==' 4000} {S Acc add(20)} {Test {S Acc getvalue($)} '==' 4020} {S Acc enter(0)} {Test {S Acc getvalue($)} '==' 0} {DoneTesting}

8. (15 points) [UseModels] Using either the declarative concurrent or the message passing model in Oz, write all of the following functions and procedures, which will implement a <Set> abstract data type: NewSet : <fun {$ }: <Set>> SetAdd : <proc {$ <Set> <Value>}> SetDelete : <proc {$ <Set> <Value>}> SetSize: <fun {$ <Set>}: <Int>> SetMember: <fun {$ <Set> <Value>}: <Bool>> These behave as follows. {NewSet} creates a new, empty set, and returns it. {SetAdd Set Val}, puts Val into the set represented by Set. {SetDelete Set Val}, removes Val from the set represented by Set. {SetSize Set}, returns the number of distinct values in the Set argument. {SetMember Set Val}, returns true just when Val is a member of the Set argument, and false otherwise. The following are examples, written using the Test procedure from the homework. (There is space for your answer on the next page.) \insert 'Set.oz' \insert 'TestingNoStop.oz' {StartTesting 'SetTest $Revision: 1.2 $'} S = {NewSet} {Test {SetSize S} '==' 0} {SetAdd S apl} {SetAdd S cobol} {Test {SetSize S} '==' 2} {SetAdd S apl} {Test {SetSize S} '==' 2} {Test {SetMember S cobol} '==' true} {Test {SetMember S apl} '==' true} {Test {SetMember S python} '==' false} {SetAdd S python} {Test {SetMember S python} '==' true} {Test {SetSize S} '==' 3} {Test {SetMember S apl} '==' true} {SetDelete S apl} {Test {SetMember S apl} '==' false} {Test {SetSize S} '==' 2} Dynamics = {NewSet} {Test {SetSize Dynamics} '==' 0} {SetAdd Dynamics lisp} {SetAdd Dynamics scheme} {SetAdd Dynamics smalltalk} {SetAdd Dynamics python} {Test {SetSize Dynamics} '==' 4} {SetAdd Dynamics python} {Test {SetSize Dynamics} '==' 4} {Test {SetMember Dynamics lisp} '==' true} {SetAdd Dynamics lisp} {Test {SetSize Dynamics} '==' 4} {Test {SetMember Dynamics apl} '==' false} {SetAdd Dynamics apl} {Test {SetSize Dynamics} '==' 5} {Test {SetMember Dynamics apl} '==' true} {DoneTesting} 8

Please put your answer for the Set problem below. 9

9. (10 points) Using either the declarative concurrent or the message passing model in Oz, write the following functions and procedures to implement an abstract data type <ProcessLater>, which remembers a function and can run that function or change the remembered function: ProcessLaterUsing : <fun {$ <fun {$ <Value>}: <Value>}: <ProcessLater>> PLCompute : <fun {$ <ProcessLater> <Value>}: <Value>> PLUseFun : <proc {$ <ProcessLater> <fun {$ <Value>}: <Value>}> These behave as follows. {ProcessLaterUsing F} returns a new <ProcessLater> object, which remembers the one-argument function F. {PLCompute PL Arg}, runs the remembered function in PL on the argument Arg, and returns the result of that application. {PLUseFun PL G}, changes the function remembered by PL to the one-argument function G. Your program should be such that it can process PLCompute calls in parallel to some extent, without forcing each call to wait for the previous call to completely finish. The following are examples. \insert 'ProcessLater.oz' \insert 'TestingNoStop.oz' {StartTesting 'ProcessLaterTest $Revision: 1.2 $'} PL = {ProcessLaterUsing fun {$ X} X*X } {Test {PLCompute PL 3} '==' 9} {Test {PLCompute PL 5} '==' 25} {PLUseFun PL fun {$ Y} 1000*Y*Y + 10*Y + 1 } {Test {PLCompute PL 3} '==' 9031} {Test {PLCompute PL 7} '==' 49071} PL2 = {ProcessLaterUsing fun {$ X} X } {Test {PLCompute PL2 ok} '==' ok} {Test {PLCompute PL 10} '==' 100101} {PLUseFun PL2 fun {$ Ls} {Map Ls fun {$ X} X+2 } } {Test {PLCompute PL2 [4 0 2 0]} '==' [6 2 4 2]} {Test {PLCompute PL2 nil} '==' nil} {Test {PLCompute PL2 [10 20 30 40 50 60 70 80]} '==' [12 22 32 42 52 62 72 82]} {DoneTesting} 10