UNIVERSITETET I OSLO

Similar documents
UNIVERSITETET I OSLO

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO

Consider the following EBNF definition of a small language:

CSci 4223 Principles of Programming Languages

CSE 341 Section 5. Winter 2018

CSE3322 Programming Languages and Implementation

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

ML Built-in Functions

CSE3322 Programming Languages and Implementation

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

Lists. Michael P. Fourman. February 2, 2010

Exercises on ML. Programming Languages. Chanseok Oh

CSCI-GA Final Exam

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CSE3322 Programming Languages and Implementation

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

COMPUTER SCIENCE TRIPOS

CSE 130, Fall 2005: Final Examination

Subtyping and Objects

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC 330, Fall 2013, Practice Problem 3 Solutions

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Higher-Order Functions

Semester Review CSC 301

CSE 130, Fall 2006: Final Examination

Introduction to SML Getting Started

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

CSE341, Fall 2011, Midterm Examination October 31, 2011

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CMSC 330, Fall 2013, Practice Problems 3

UMBC CMSC 331 Final Exam

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

1 Lexical Considerations

Programming Languages Third Edition

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Questions & Answers 28 Sept.

CSE341 Spring 2016, Midterm Examination April 29, 2016

Computing Fundamentals 2 Introduction to CafeOBJ

Metaprogramming assignment 3

Introduction to OCaml

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

CSE341, Spring 2013, Final Examination June 13, 2013

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE399: Advanced Programming. Handout 2

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS101 Introduction to Programming Languages and Compilers

CMSC 330: Organization of Programming Languages. Operational Semantics

Homework 6. What To Turn In. Reading. Problems. Handout 15 CSCI 334: Spring, 2017

Comp 311: Sample Midterm Examination

Scala : an LLVM-targeted Scala compiler

Handout 2 August 25, 2008

Recap: Functions as first-class values

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

CS558 Programming Languages

Algorithms and Programming I. Lecture#12 Spring 2015

Pace University. Fundamental Concepts of CS121 1

Inductive Data Types

Introduction to Programming Using Java (98-388)

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Intermediate Code Generation

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

CSE341 Spring 2016, Midterm Examination April 29, 2016

Overloading, Type Classes, and Algebraic Datatypes

CS 340 Spring 2019 Midterm Exam

CS 360: Programming Languages Lecture 12: More Haskell

Functional Programming Mid-term exam Tuesday 3/10/2017

UNIVERSITETET I OSLO

Principles of Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CIS 341 Final Examination 4 May 2017

CS 415 Midterm Exam Fall 2003

An introduction to C++ template programming

6. Discuss how producer-consumer problem and Dining philosophers problem are solved using concurrency in ADA. [16]

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

The return Statement

IMACS: AP Computer Science A

Sample Exam; Solutions

CSCC24 Functional Programming Scheme Part 2

Parsing Scheme (+ (* 2 3) 1) * 1

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CS422 - Programming Language Design

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

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

COMPUTER SCIENCE TRIPOS

CSE 3302 Programming Languages Lecture 8: Functional Programming

Transcription:

Exam in INF3110, November 29, 2017 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: November 29, 2017 Exam hours: 14:30 18:30 This examination paper consists of XXXX pages. Appendices: No Permitted materials: All printed and written, including the textbook Make sure that your copy of this examination paper is complete before answering. This exam consists of 3 questions that may be answered independently. If you think the text of the questions is unclear, make your own assumptions/interpretations, but be sure to write these down as part of the answers. Good luck!

Exam in INF3110, November 29, 2017 Page 2 Question 1. Runtime-systems, scoping, types (weight 40%) Exercise 1: Consider the program below, which is written in some new, hitherto unknown language (i.e., it is made up for this exam). The language supports interfaces and structs as the main units of decomposition, as well as variables and functions ("methods") within such structs. Assume that the program is correct in the given language, that is, it contains no syntax errors or type errors, and its execution does not result in a runtime error. Furthermore, assume that the program starts executing the Main function, and that print(<expr>) will print the string representation of the expression (akin to tostring() in Java) to the console/terminal. interface I { int function1(); struct A { int function1() { int x = 21; int y = x + function2(); return y; int function2() { return x;

Exam in INF3110, November 29, 2017 Page 3 struct B { int thisonegoesto = 11; int function1() { return thisonegoesto; void Main() { I myi = new A(); int result = getvalue(myi); myi = new B(); result = result + getvalue(myi); printresult(); int getvalue(i i) { return i.function1(); void printresult() { print("the result is: "); print(result); // HERE! 1a) With a reasonable semantics, and a corresponding implementation of the language, what will be printed by the statement labeled "HERE!" in the program text above? Explain your reasoning briefly.

Exam in INF3110, November 29, 2017 Page 4 1b) It is reasonable to assume that this language has some properties that differ a bit from most "mainstream" languages such as e.g. Java and C#. Discuss briefly (and informally) your interpretation of the semantics of the language, focusing on topics discussed in class. 1c) Draw the runtime stack at the point labeled "HERE!" in the program text above. That is, right after the call to print(result). Utilize your interpretation of the language's semantics from question 1b. Include objects and closures in the drawing as necessary. 1d) Consider the following Java program snippet: public class Program { public static void main(string[] args) { Object[] myargs = args; myargs[0] = 42; Explain why this piece of code is unsafe (there might be more than one reason!). Explain briefly what, from a language designer standpoint, can be done to amend the situation, should you be the creator of Java from scratch. 1e)

Exam in INF3110, November 29, 2017 Page 5 Assume now, that our made-up language should support generic function definitions (parametric polymorphism). Propose a syntax for this. You should do this by providing an EBNF notation for function signatures. (You do not need to include a definition of the function body.) The definition should support generic parameters with both covariant and contravariant constraints. Explain how this is solved with your suggestion. You can assume that standard definitions of identifiers, strings, parameter lists, etc are provided in the grammar for you. Use the following notational convention when writing your EBNF grammar: <non-terminal> terminal [ optional ] alternative1 alternative2 ( grouping ) zero-or-more-repetitions* one-or-more-repetitions+ 1f) In this question, we will allow structs to be generic, and use this to define function parameters. We will assume that there is a predefined struct called Func which has two generic parameters, representing a function s (single) formal parameter type (U), and return type (T), respectively. Example: struct Func of T and U { If you will, you can imagine of T and U replaced with your own syntax for generic parameters from the previous exercise. If you did not answer the previous question, just use the provided example syntax. We will in this question use this struct to define formal function parameters to other

Exam in INF3110, November 29, 2017 Page 6 functions. With this in mind, consider the following program: int f( (Func of int and int) g) { int x = g(42); return x; int dummy(int val) { return val; void Main() { f(dummy); Draw the runtime stack for the program above, right before the line return val is executed in the function dummy. Question 2. ML (weight 40%) 2a Evaluate the following ML expressions: a) (fn x => fn y => x @ y) (List.filter (fn x => x > 2) [1,2,3]) (List.filter (fn x => x < 2) [1,2,3]); b) map (fn x => x 1) (map (fn x => fn y => x + y) [1,2,3]); 2b Assume the standard definitions of foldl and foldr: fun foldl(f: 'a*'b->'b)(acc: 'b)(l: 'a list): 'b = case l of

Exam in INF3110, November 29, 2017 Page 7 [] => acc x::xs => foldl f (f(x,acc)) xs fun foldr(f: 'a*'b ->'b) (acc: 'b)(l: 'a list): 'b = case l of [] => acc x::xs => f (x, (foldr f acc xs)) Describe based on the example of foldr/foldl (op @) [] [1,2] the difference in stack usage during evaluation of both expressions.

Exam in INF3110, November 29, 2017 Page 8 2c 1) Define a type 'coord' to represent a position in three-dimensional space as tuple of three real numbers. 2) Give two example expression that define different values of that type. 3) The Pythagorean distance between two points (x0,y0,z0) and (x1,y1,z1) in this threedimensional space is calculated as d = sqrt((x1-x0)^2 + (y1-y0)^2 + (z1-z0)^2) Define the function distance = fn : coord * coord -> real that calculates that distance, where x0 is the value on the x-dimension of the first argument and x1 the value on the x-dimension of the second, etc. You may use the predefined functions sqrt : real -> real and pow : real * real -> real. The latter returns the first argument raised to the power of the second. 2d In a café, menu items are represented by the following datatype: datatype item = Vgn of string * real Vgt of string * real Omn of string * real; The constructors are: Vgn == vegan == no animal produce Vgt == vegetarian == no meat Omn == omnivorous == may contain animal produce The string is the item's name and the real is its price. A menu is represented as a list of items, for example: [Vgn("tofu",3.6), Vgt("quiche",2.7), Omn("haggis",1.9)]; Give the type-signatures for all your definitions in the answers a)-e) below! 1) Write a function to find all vegan items on a list. 2) Write a function which, given an order tuple and a menu, returns the total cost of the order. An order is a tuple of a food item and a quantity, for example:

Exam in INF3110, November 29, 2017 Page 9 ("tofu",3) == 3 orders of tofu. 3) Write a function which, given a list of order tuples and a menu, returns the total cost of all the orders. 2e Calculate the type for the following expression according to the ML type inference algorithm: fn x => fn y => (y x) (x (y x)); Derive the corresponding equations through help of a parse graph, and solve the resulting equation system to obtain the type of the root node. Question 3. Prolog (weight 20%) 3a 1) Define the predicate and/3 to model the boolean function "and", using the constants 0 and 1 to mean false and true respectively. 2) Assume the following definition of logical disjunction or/3: or(0,0,0). or(_,_,1). We can now ask the following query, obtaining an obviously undesirable second solution:?- or(0,0,result). Result = 0 ; Result = 1. Explain why we get two solutions! 3) Give an alternative definition of or/3 with only two clauses that exhibits the correct behaviour of logical disjunction. 3b Natural numbers can be represented in Prolog through the Peano encoding using e.g. 0 (for the constant zero), and s(x) for the successor of X (in other words X + 1 in standard arithmetic). Example: 2 = s(s(0)). 1) Write a ternary predicate add(x,y,z) that is true if Z is the sum of X and Y, where the arguments are expected to be Peano-encoded natural numbers.

Exam in INF3110, November 29, 2017 Page 10 That is, e.g. add(s(0),s(0),s(s(0)) is true. 2) Recall that the Fibonacci numbers are recursively defined as a function: f(0) = 0 ; f(1) = 1; f(n+2) = f(n+1) + f(n). Give a Prolog predicate fib(a,b), such that it is true if B is f(a). Use the Peano encoding of natural numbers, instead of Prolog's builtin math, and refer to add/3 from (a) if necessary. 3) Define a predicate fibacc/4 that uses an accumulator to calculate the Fibonacci numbers with a single recursive call instead of two. That is, fibacc(n,a,b,x) should be true, if X is the Nth Fibonacci number, using the accumulators A and B. Also give a wrapper function fiba/2 that calls fibacc/4 and correctly initialises the two accumulator-parameters.