Concepts of program design Exam January 31, 13:30 16:30

Similar documents
Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Introduction to Functional Programming in Haskell 1 / 56

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

COSE212: Programming Languages. Lecture 0 Course Overview

Lecture 8: Summary of Haskell course + Type Level Programming

CSci 450: Org. of Programming Languages Overloading and Type Classes

IA010: Principles of Programming Languages

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

SIMON THOMPSON LEARNING FUNCTIONAL PROGRAMMING

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Functional Languages. Hwansoo Han

G Programming Languages - Fall 2012

Concepts of programming languages

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Refactoring to Functional. Hadi Hariri

Functional Programming. Pure Functional Programming

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

(Advanced) topics in Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Type-indexed functions in Generic Haskell

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Stop coding Pascal. Saturday, April 6, 13

Programming with Math and Logic

Programming Language Concepts, CS2104 Lecture 7

CIS 341 Final Examination 4 May 2017


CS 11 Haskell track: lecture 1

CSE-321 Programming Languages 2010 Final

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Programming. Pure Functional Languages

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

Functional Logic Programming Language Curry

Simple Unification-based Type Inference for GADTs

Functional Programming Patterns And Their Role Instructions

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany

Types and Type Inference

Com S 541. Programming Languages I

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Anders Møller Michael Schwartzbach Erik Ernst Hans Hüttel

Metaprogramming assignment 3

Compilers and computer architecture: Semantic analysis

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

CMSC 331 Final Exam Section 0201 December 18, 2000

Simply-Typed Lambda Calculus

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CS 320: Concepts of Programming Languages

Chapter 11 :: Functional Languages

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Concepts of programming languages

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

JVM ByteCode Interpreter

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Pierce Ch. 3, 8, 11, 15. Type Systems

Exercise 1 ( = 24 points)

Featherweight Scala. Week 14

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

CIS 500 Software Foundations Midterm I

Programming Languages (PL)

Advanced features of Functional Programming (Haskell)

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Haskell Monads CSC 131. Kim Bruce

Kripke-Style Contextual Modal Type Theory

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Projects for Advanced Functional Programming 2017

Type families and data kinds

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Programming Paradigms Written Exam (6 CPs)

CS 415 Midterm Exam Fall 2003

CS457/557 Functional Languages

CS 242. Fundamentals. Reading: See last slide

An introduction to functional programming. July 23, 2010

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

Functional Programming

Programming Languages Fall 2014

Introducing Wybe a language for everyone

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Denotational Semantics. Domain Theory

Introduction to OCaml

CS 320 Midterm Exam. Fall 2018

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Do Extraterrestrials Use Functional Programming?

Concepts of programming languages

MSO Exam November 7, 2016, 13:30 15:30, Educ-Gamma

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Programming Languages

Declarative concurrency. March 3, 2014

COMP 3141 Software System Design and Implementation

Graphical Untyped Lambda Calculus Interactive Interpreter

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Concepts of Programming Design

The Design Recipe Fall 2018

Transcription:

Concepts of program design 2016 2017 Exam January 31, 13:30 16:30 Name: Student number: Please read the following instructions carefully: Fill in your name and student number above. Be prepared to identify yourself with your student card when you submit your exam. This is a closed-book exam. You are forbidden from accessing any external material, notes, electronic material, or online resources. Answer each open question in the space provided. Write clearly and legibly. Answer each multiple choice question by marking the space provided. A maximum of 100 points can be obtained by the questions of this exam, to be divided by 10 to yield the mark for the exam. Please do not write in the space below. Question Points Score Multiple choice 24 DSLs and Metaprogramming 18 Semantics 29 Parallelism and Concurrency 29 Total: 100

Concepts of program design Exam, Page 1 of 11 January 31, 2017 Multiple choice In the multiple choice questions below, you can earn two points for each correct answer. If you fill in an incorrect answer one point will be deducted from your score. If you leave a question blank, no points will be deducted or awarded. These rules only apply to the multiple choice questions of this exam. (a) (2 points) Which of the following statements is true? To control access to mutable memory, Rust s borrowing mechanism restricts how pointers and address information may be passed between methods. To control access to mutable memory, Rust s IO monad restricts side-effects to those functions that return a value in the IO monad. To control access to mutable memory, Rust s system of algebraic effects restrict when memory operations may be performed. All of the above. (b) (2 points) Both Elm and Purescript are both languages that compile to Javascript. Which of the following statements is true? Like Javascript, both Elm and Purescript embrace dynamic typing. Compiled Elm code is executed in a run-time system implemented in Javascript; Purescript aims to generate human-readable Javascript. Both Elm and Purescript restrict effects using an IO monad, inspired by Haskell. Elm and Purescript extend Javascript with a full blown object system, including inheritance and dynamic dispatch. (c) (2 points) Which of the following statements is true? Protocols in Swift define precisely how classes and objects defined in Objective-C may interface with Swift code. Protocols are used to count the number of references to an object to facilitate garbage collection. Protocols in Swift allow you to overload certain function or operator names. Protocols in Swift restrict access to the network or shared data sources. (d) (2 points) Which of the following statements is true? OCaml allows you to organize code into modules; each module contains several methods, known as functors. OCaml allows you to organize code into functors; each functor contains several methods and data types. OCaml allows you to organize code into modules; in OCaml functors on the other hand, provide a uniform abstraction over data types that support a map operator, such as lists and trees. OCaml allows you to organize code into modules; modules may be passed as arguments to functors to create new modules. (e) (2 points) Which of the following statements is false? Templates in C++ allow you to define generic or polymorphic functions that operate on many different types. Templates enable a form of metaprogramming that C++ allows you to generate new code at compile time. Templates in C++ are Turing complete, allowing programmers to run arbitrary computations at compile time. Templates in C++ are statically checked to never produce ill-typed code.

Concepts of program design Exam, Page 2 of 11 January 31, 2017 (f) (2 points) Given the following Prolog definitions: foo(a). foo(b). foo(c). bar(c). bar(b). baz(x) :- foo(x), bar(x). What is the result of the query baz(x). X = a; X = b; X = c. X = b; X = c. X = c; X = b. X = c; X = b; X = a. (g) (2 points) Which of the following statements is false? Like Purescript, Idris tracks the usage of different kinds of effects using an Eff monad; Like Haskell and OCaml, Idris provides language support for interfacing with C code; Idris s dependent types allow values to depend on data types. Idris s powerful type system allows you to prove properties about your programs. (h) (2 points) Which of the following statements is false? Scala runs on the Java Virtual Machine (JVM), making it easy to interface with existing Java libraries. Scala is a hybrid functional-object oriented language that supports many different styles of programming. Scala s traits allow you to define an interface; traits can be combined using mixins. Like most object oriented languages, any class in Scala can only inherit from a single trait. (i) (2 points) What is the result of evaluating the following expression in Racket: (eval (first ((+ 1 2) (+ 3 4)))) ((+ 1 2) (+ 3 4))) 7 3 a dynamic failure (j) (2 points) Which of the following lambda terms could correspond to the expression λλ(λ0)1 written using De Bruijn indices: \x -> \x -> (\x -> x) x; \y -> \x -> (\x -> x) x; \y -> \y -> (\x -> x) x; \x -> \y -> (\x -> x) x;

Concepts of program design Exam, Page 3 of 11 January 31, 2017 (k) (4 points) Give two examples of how a programming language presented by one of the students during this course uses a specific static semantics to control or limit the dynamic behaviour of a program. DSLs and Metaprogramming (a) (2 points) Explain the difference between a shallow and a deep embedding of a domain specific language. (b) (2 points) Why would you choose to embed a domain specific language, rather than implementing a new language from scratch? What are the drawbacks of doing so?

Concepts of program design Exam, Page 4 of 11 January 31, 2017 (c) (2 points) Briefly describe two different approaches for representing variable binding in an embedded domain specific language. (d) (2 points) What is a staged programming language? (e) (4 points) Template programming in C++ places severe restrictions on the programs that can be used to generate new code; Template Haskell is far more liberal, allowing arbitrary code to be executed. Explain the pros and cons of both these approaches using an example.

Concepts of program design Exam, Page 5 of 11 January 31, 2017 (f) (3 points) In this course, we have seen examples of metaprogramming in both Racket and Haskell. Why is Template Haskell so much more complicated than Racket s quote-eval mechanism? (g) (3 points) Explain why it is difficult to statically type metaprograms. Semantics For this question, we will consider a small expression language consisting of: boolean constants true and false; if-then-else expressions; the constant 0; the successor operation succ that increments its argument; the iszero function that checks that returns true when its argument is 0 and false otherwise. The values of this language consist of the boolean constants true and false, 0, and any number of succ applications to 0.

Concepts of program design Exam, Page 6 of 11 January 31, 2017 We can give a dynamic semantics for the boolean fragment of the language as follows: if true then t2 else t3 t2 if false then t2 else t3 t3 If-T If-F t1 t1 if t1 then t2 else t3 if t1 then t2 else t3 If t t Stop t1 t2 t2 t3 t1 t3 Step (a) (4 points) Explain the difference between static semantics and dynamic semantics. (b) (5 points) Give a derivation showing that if (if true then false else true) then true else 0 0

Concepts of program design Exam, Page 7 of 11 January 31, 2017 (c) (6 points) Extend this dynamic semantics with rules for 0, succ, and iszero. Your definitions should align with the choices made by most programming languages. (d) (5 points) In this expression language, there are two types of expressions Bool (booleans) and Nat (natural numbers). Suggest a static semantics for this expression language by defining a relation e : t, relating expressions e and types t (the language does not have any variables, so your relation will not need a typing environment Γ). Your definitions should align with the choices made by most statically typed programming languages.

Concepts of program design Exam, Page 8 of 11 January 31, 2017 (e) (4 points) Explain what the properties progress and preservation are and how they relate dynamic and static semantics. (f) (5 points) Sketch how to prove the following statement: For any expressions t1, t2, t3, and for any value v, when if t1 then t2 else t3 v either t2 v or t3 v.

Concepts of program design Exam, Page 9 of 11 January 31, 2017 Parallelism and Concurrency (a) (4 points) Parellelism and concurrency are two different notions. Give a brief definition of each and explain how they are different. (b) (4 points) Briefly sketch two different mechanisms for writing concurrent programs we have seen in this course and two different mechanisms for writing parallel programs that we have seen in this course.

Concepts of program design Exam, Page 10 of 11 January 31, 2017 (c) (2 points) What property do Erlang and Haskell share that makes both these languages suitable for parallelism? (d) (2 points) Erlang claims to be designed for writing robust systems, yet the approach to error handling is Let it crash! explain this apparent contradiction. (e) (2 points) What is the effect of linking two Erlang processes? (f) (2 points) When parallelizing sequential code, you often need to control the size of the tasks that are to be run in parallel. Explain one approach for doing so in either Erlang or Haskell. (g) (8 points) Consider the following definition of mergesort in Erlang. merge_sort([]) -> []; merge_sort([x]) -> [X]; merge_sort(xs) -> {Ys,Zs} = split(xs), merge(merge_sort(ys),merge_sort(zs)). merge([],ys) -> Ys; merge(xs,[]) -> Xs; merge([x Xs],[Y Ys]) when X =< Y -> [X merge(xs,[y Ys])]; merge([x Xs],[Y Ys]) -> [Y merge([x Xs],Ys)]. Using spawn_link, self, and message passing, write a parallel Erlang version of merge_sort. You may reuse functions defined above in your answer without including their definitions. Ensure that the task granularity is not so fine that the overheads of parallelism dominate the run-time

Concepts of program design Exam, Page 11 of 11 January 31, 2017 (h) (5 points) Laziness is a blessing and a curse when it comes to parallelizing code. what is meant by this statement? Illustrate your answer with examples and by referencing techniques for parallel programming that we have seen in the context of this course.