Lab for Automated Reasoning and Analysis LARA

Exercises 06

Exercise 1

Consider the following piece of code.

  val x = 1
  val y = 2
  def p() = x + y
  def q(p: Int => Int) = p(x * y)
  def f = {
    val x = 2
    q(p + _)
  }
  def g(q: Int => Int) = {
    val y = 3
    def p() = x - y
    q(f)
  }
  print(g(_ + 2))

Determine the output of the code using static scoping and dynamic scoping.

Exercise 2

If the following programs type-check according to the rules given in the course, give the corresponding type derivation tree, otherwise give a partial tree that shows where it doesn't work

a)

  var x:Int
  var y:Int
  if (1 > 42) x
  else y
  x = y

b)

  var x: Int
  var y: Int
  if (0 > 0) {
    if (3 > 4) {x = y}
    else {y = x}
  }
  else
    if (4 > 3) {x = x}
    else {y = y}

c)

 class C {
  def f(x: Int): Int = 1+f(x)  
}
 

Exercise 3

Write down the type derivation tree for the following example. Does the program type check?

class Fun {
  val u :Boolean
  var a :Boolean 
  def f(y: Boolean): Boolean = {
    a = y
    if (u == y) {
      var u: Int = 0
      a = f(u < 0)
      return u < 0
    } else 
      return u > 0 
  }
}
 
cc10/exercises_06.txt · Last modified: 2010/11/03 10:02 by hossein
 
© EPFL 2018 - Legal notice