Concepts of Programming Languages Supervision Work

Table of Contents

Please send in work by e-mail 48 hours before the supervision. Scanned copies are okay, you don’t have to typeset the work, as long as it is legible. Please convert any Microsoft Word documents to PDF before sending.

Supervision 1

Question 1.1   Introduction

Chose one of the following options to do

Option 1.1.1

An author writes:

Most successful language design efforts share three important characteristics:

  1. Motivating Application: The language was designed so that a specific kind of program could be written more easily.
  2. Abstract Machine: There is a simple and unambiguous program execution model.
  3. Theoretical Foundations: Theoretical understanding was the basis for including certain capabilities and omitting others.

Briefly discuss the merits and/or shortcomings of one of the above three statements, giving examples and/or counterexamples from procedural, applicative, logical, and/or object-oriented programming languages. [8 marks]

Option 1.1.2

Pick on a language of your chosing, and critique it. You might like to consider:

  • Origins of its ideas (what influenced it, and what did it influence)
  • Why was it created
  • What is it used for
  • Good points
  • Bad points
  • How future languages (if any) improved on it

[8 marks]

Question 1.2   DynamicScope

Chose one of the following options to do

Option 1.2.1

If you can (i.e. don’t spend too much time on this, but if you did try, include your thoughts), give an two operational semantics for the following simple language, one with dynamic scoping, and one with lexical scoping:

\[ E ::= \ x\ |\ fn\ x\ \Rightarrow\ E\ | \ E\ E \]

You may assume an encoding for numbers, booleans, and syntax for let...in...end and if...then...else without providing one.

(Hint, you will have to use an environment model of execution, rather than a substitution model, and your first attempt at making an environment-model based semantics might well have dynamic scoping.) [8 marks]

Option 1.2.2

What mistake led to dynamic scoping? How is dynamic scoping different from having only global variables? Do you know of any place where dynamic scoping is still used?

Consider the following two program fragments.


(defvar x 1)
(defun g (z) (+ x z))
(defun f (y)
  (+ (g 1)
     (let ((x (+ y 3)))
       (g (+ y x)))))
(f 2)

val x = 1;
fun g z = x + z;
fun f y =
    g 1 +
    let val x = y + 3;
    in  g (y + x) end;
f 2;

What are their respective output values when run in their corresponding interpreters? Justify your answer, explaining it in a conceptual manner. [8 marks]

Question 1.3   ObjectOriented

Outline the key features that a language must have to be called object-oriented. Further, briefly discuss to what extent the programming languages Simula, Smalltalk, C++, and Java has them. [6 marks]

Question 1.4   ParameterPassing

Define the following parameter-passing mechanisms: pass-by-value, pass-by-reference, pass-by-value/result, and pass-by-name. Briefly comment on their merits and drawbacks. [5 marks]

Question 1.5   Aliasing

What is aliasing in the context of programming languages? Explain the contexts in which it arises and provide examples of the phenomenon. In what kinds of languages is it generally considered bad, and in what kinds of languages is it considered useful? [5 marks]

Question 1.6   Typing

Recall that Algol 60 has a primitive static type system. In particular, in Algol 60, the type of a procedure parameter does not include the types of its parameters. Thus, for instance, in the Algol 60 code

procedure P(procedure F)
begin F(true) end ;

the types of the parameters of the procedure parameter F are not specified. Explain why this piece of code is statically type correct. Explain also why a call P(Q) may produce a run-time type error, and exemplify your answer by exhibiting a declaration for Q with this effect. Why does this problem not arise in Standard ML? [3 marks]

Question 1.7   Typing Subtyping

Consider the Simula declarations

CLASS A
A CLASS B;

which have the effect of producing the subtype relation B<:A, and

REF(A) a
REF(B) b;

Recall that Simula uses the semantically incorrect principle that if B<:A then REF(B)<:REF(A) and consider now the following Simula code

PROCEDURE ASSIGNa(REF(A) x)
BEGIN x :- a END ;
ASSIGNa(b);

Does it statically type check? If so, will it cause a run-time type error? Justify your answers. [3 marks]

Question 1.8   UntaggedUnions

What is the problem with making the tag optional in Pascal’s variant records? [1 mark]

Question 1.9   Typing

What is the type of the expression

fn a => fn b => fn c => (a b) (b c)

given by the SML interpreter? Explain how this is inferred. [6 marks]

Question 1.10   Typing

For each of the following ML declarations, either justify their ability to be soundly used or give a program using them which would violate type safety: [2 marks]

  1. exception poly of ’a;
  2. val ml = ref [];

Supervision 2

Question 2.1   Parallelism

You manage two junior programmers and overhear the following conversation:

A: “I don’t know why anyone needs a language other than Java, it provides clean thread-based parallel programming.”

B: “Maybe, but I write my parallel programs in a functional programming language because they are then embarrassingly parallel.”

Discuss the correctness of these statements and the extent to which they cover the range of languages for parallel programming. [6 marks]

Question 2.2   Parallelism

What is the difference between internal and external iteration? [1 mark]

Question 2.3   Scripting

Scripting languages and dynamically typed languages are identical; discuss [3 marks]

Question 2.4   PrototyeOriented JavaScript

Discuss the notion of ‘class’ in relation to JavaScript. [2 marks]

Question 2.5   Monad

Explain what is meant by a monad in a programming language, giving the two fundamental operations of a monad along with their types. [3 marks]

Question 2.6   Monad

Consider the use of a monad for input-output. For the purposes of this question, take the IO monad as including two operations readint and writeint which respectively read integers from stdin and write integers to stdout. Give the types of these operators. [2 marks]

Question 2.7   Monad

Assume MLreadint and MLwriteint are primitives with side effects for input/output and consider the ML expression add1 of type int:

let val x = MLreadint() in MLwriteint(x+1); x end

Give an equivalent expression which uses the IO monad instead of side-effects, and state its type. [3 marks]

Give a function run2diff which can be applied to the previous answer. When so applied it should give a value in the IO monad which corresponds to ML code that runs add1 twice and returns the difference between the values read. [4 marks]

Question 2.8   Variance

State what happens when attempting to compile and execute the following Java fragment (explaining the origin of any error messages or exceptions which might arise). [4 marks]

Object n = new Integer(42), o = new String("Whoops");
Object [] v;
Integer [] w = new Integer[10];
v = w;
v[4] = n;
v[5] = o;

Question 2.9   Variance

Consider the Java code:

Object n = new Integer(42);
ArrayList<? extends Object> v1;
ArrayList<Object> v2;
ArrayList<Integer> w = new ArrayList<>(10);

Explain any differences in behaviour between assignments v1 = w and v2 = w and also between method calls v1.set(4,n) and v2.set(4,n). [4 marks]

Question 2.10   Variance Scala

In the programming language Scala, a generic class like the following one

abstract class Stack[A] {
  def push(x : A) : Stack[A];
  def top : A;
  def pop : Stack[A];
}

is non-variant by default. Why? Modify it to make it co-variant. [6 marks]

Question 2.11   Modules

Consider the declarations

structure Z = struct   type t = int; val z = 0 end;
structure A = Z :  sig type t;       val z : t end;
structure B = Z :> sig type t = int; val z : t end;
structure C = Z :> sig type t;       val z : t end;

in the SML Modules language. Explain the behaviour of the SML interpreter on inputting each of the expressions. [6 marks]

Z.z = A.z;
Z.z = B.z;
Z.z = C.z;

Question 2.12   TypeClasses

What are the similarities and differences between Haskell type classes, overloading, and dynamic dispatch used in object-oriented languages? [3 marks]

Author: Robert Kovacsics (rmk35)

Created: 2020-05-12 Tue 16:38

Validate