Types 2

What Are Types?

Types in SL3

Flow-Based vs. Inference-Based Typing

Return Type Inference

Higher Order Functions

Function Types

Type Checking

Type Checking Details

Lab

???

Step 1: Kick the Tires of the SL3 Interpreter

  1. Put this program into the interpreter
    val twice = { f : (Int)=>Int, a : Int => f(f(a)) };
    val triple = { x : Int => 3 * x };
    twice(triple, 2)

    What type do you get for the result?

  2. What is reported as the type of triple? Hint: Just modify the program to evaluate triple instead of twice(triple, 2).
  3. The type is written as a complex SLFun expression. Translate it back into a SL3 type of the form (paramTypes)=>returnType
  4. What is the type of twice? Translate it into a SL3 type

Step 2: Type Errors

  1. The following is a type error since the types of the branches don't agree
    val x = 1;
    if (x < 2) x else x < 2

    What happens when you run the SL3 interpreter with this input?

  2. Which part of the interpreter code diagnosed the error?
  3. Now consider this program that lies about a recursive type:
    def fac : Bool = { x : Int => if (x == 0) 1 else x * fac(x - 1) };
    fac(3)

    You'll get an exception when you run the program, but the stack trace doesn't tell you much about the cause of the error. Run the debugger instead. When the stack trace hits, the program will stop on the line assert(typeof(right, types) == SL3Int()). What is the value of right?

Step 3: Code Cleanup