Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HW2

Due Date: Wednesday, Sep 30, 11:59pm

  • Upload your submission to Gradescope, starting from this starter code. Each function in this file is filled in with ..., which throws an error about the code being unimplemented; your job is to fill in each function.

You should upload only a single Lean file beginning with the following two lines:

import Lait
#lait
  • Be sure to check that your submission is valid Lait code that type checks; if it is not, you will receive 0 points for that problem.

  • Your programs will be evaluated based on a number of tests. Gradescope will show you some tests when you submit, but not all of them. However, all of the tests we run will be testing the program’s specification, which will be unambiguous. You should test your code thoroughly before submitting.

  • For each problem, you should include at least 3 passing test cases different than the ones given below.

1. cond

Certain languages such as Racket have a construct called cond which functions as a multi-way conditional. We will study a language that contains cond, given by the following grammar:

<expr> := | <int> | <bool> | <expr> > <expr> | <expr> + <expr> | cond { <list-of-arm> }
<int> := 0 | 1 | 2 | ...
<bool> := true | false
<list-of-arm> := | done | <arm>, <list-of-arm>
<arm> := | if <expr> then <expr>

For example, here is a valid program in our language, CondLang:

cond {
  if 1 > 2 then 0,
  if 10 > 9 then 1,
  if 4 > 3 then 2, 
  done
}

Each cond has a list of arms, where each arm is an if-then without an else. The semantics of cond is that we evaluate each arm in order, starting with the guard. If the condition of the arm’s guard evaluates to true, then we evaluate that arm’s body. If the condition of the arm’s guard evaluates to false, then we forget about that arm and move onto the next one. Hence, the above cond should evaluate to 1, since 10 = 9 is the first arm with a true condition.

The semantics of cond should throw an error if:

  • We try to evaluate > or + with non-integers;
  • We reach an arm of a cond and it reduces to a non-boolean;
  • All arms of a cond evaluate to false and thus reach done, meaning that none of the conditions of the cond succeeded.

Below are the relevant data types for your language:

type Value := | VInt (val : Int) | VBool (val : Bool) 

type CondLang :=
 | CVal (val : Value)
 | CGreater (e1 : CondLang) (e2 : CondLang)
 | CPlus (e1 : CondLang) (e2 : CondLang)
 | CCond (arms : List<CondLang * CondLang>)

Above, the type CondLang * CondLang is a pair type. Pairs in Lait are constructed using tuple syntax (..., ...) and eliminated using fst and snd:

def myPair : Int * Bool := (3, true)
#eval fst myPair>> 3
#eval snd myPair>> true

(Note that fst and snd are keywords so must be put next to their arguments; fst on its own does not type check.)

Note that unlike the lecture notes, we don’t have separate constructors CBool and CInt for constants; instead, we inject the grammar of values into expressions. Thus, the expression 2 + 2 is represented as CPlus (CValue (VInt 2)) (CValue (VInt 2)).

1.1: Big-Step for CondLang

Implement a big-step definitional interpreter of type CondLang -> Value:

def interpCondLang (e : CondLang) : Value := ...

When you need to throw an error, you MUST use error "runtime error". For example, when converting a value to an integer:

def Value.toInt (v : Value) : Int :=
  match v with 
  | VInt i => i
  | VBool _ => error "runtime error"
  end

1.2: Small-Step for CondLang

Now, you will implement a small-step interpreter for CondLang of the following type:

def stepCondLang (e : CondLang) : CondLang := ...

As before, values step to values; thus, CVal v steps to CVal v. To step the binary operators CGreater and CPlus, you:

  • step the left-hand side unless it’s a value; otherwise,
  • step the right-hand side unless it’s a value; otherwise,
  • both are values, so you step the expression into a suitable value for the result.

To step a CCond, you step each arm in sequence. First, step the guard of the first arm in the list. If the first guard is true, you move to evaluating the right-hand side of the arm. If the first guard is false, then you remove the first arm from the list.

Let’s see some examples of stepping CCond (written in concrete syntax):

cond { if 1 > 2 then 1 + 1, if 2 > 1 then 2 + 2, done }
--> // simplify first arm
cond { if false then 1 + 1, if 2 > 1 then 2 + 2, done }
--> // throw away first arm
cond { if 2 > 1 then 2 + 2, done }
--> // simplify first arm
cond { if true then 2 + 2, done }
--> // step to body of first arm
2 + 2
-->
4

As in the big-step semantics, if you need to step something like cond { if 3 then 2, done } then you must throw error "runtime error".
Similarly, if you need to step cond { done } you throw an error.

2. Uniquify

You are tired of dealing with shadowing, so instead you’ve decided to implement a function called uniquify that, when given an abstract syntax tree for LetLang, renames all shadowed variables to unique names (i.e., names that are guaranteed not to be bound anywhere else in the program). This way, you can tell a variable’s scope just by looking for its unique binding in the program!

We will use the following abstract syntax:

type Exp := 
  | EInt (val : Int)
  | EPlus (e1 : Exp) (e2 : Exp)
  | EVar (var : String)
  | ELet (var : String) (assignment : Exp) (body : Exp)

2.1: Rename

You run into a challenge almost immediately: even if we generate unique names, we need a way to replace the existing name in your abstract syntax tree with your newly generated unique one. Notice how our current subst function from lecture does not do that.

Write a function updateNames:

def updateNames (e : Exp) (old : String) (new : String) : Exp := ...

which replaces all variables with name old in e with the name new, regardless of scoping rules (i.e., ignoring shadowing). You can safely assume that newname never appears in e.

Example behavior:

updateNames (ELet "x" (EInt 10) (EVar "x")) "x" "y" = ELet "y" (EInt 10) (EVar "y")

2.2: Uniquify

Now, we will implement uniquify : Exp -> Exp using updateNames. Your solution should additionally use the following freshName function, which you can assume is guaranteed to produce a unique name whenever it is called (don’t worry about understanding how it works; we will cover mutation later):

def freshName : Unit -> String :=
    let i := alloc 0 in 
    fun _ =>  
        let _ := set i (get i + 1) in 
        "a" ++ toString (get i)

See below for an example use:

def getTwoNames () :=
  let x := freshName () in 
  let y := freshName () in 
  x ++ " " ++ y

#eval getTwoNames ()>> "a1 a2"
#eval getTwoNames ()>> "a3 a4"

The correctness requirement for uniquify is that it allows a simpler version of an interpreter for Exp to work, which does not take shadowing into account. Here is the simpler version:

def uniqSubst (var : String) (assignment : Exp) (body : Exp) : Exp :=
  match body with 
  | EInt i => EInt i
  | EPlus e1 e2 => EPlus (uniqSubst var assignment e1) (uniqSubst var assignment e2)
  | EVar var' => if var == var' then assignment else (EVar var')
  -- No shadowing logic for let.
  | ELet var' e1 e2 => ELet var' (uniqSubst var assignment e1) (uniqSubst var assignment e2)
  end

def uniqInterp (e : Exp) : Int :=
  match e with 
  | EInt i => i
  | EPlus e1 e2 => uniqInterp e1 + uniqInterp e2
  | EVar _ => error "unbound variable"
  | ELet var e1 e2 => 
    let v1 := uniqInterp e1 in 
    let e2' := uniqSubst var (EInt v1) e2 in 
    uniqInterp e2'
  end

Now, implement uniquify:

def uniquify (e : Exp) : Exp := ...

For example, the following test should pass:

#test uniqInterp (uniquify (ELet "x" (EInt 10) (ELet "x" (EInt 20) (EVar "x")))) === 20

The above should pass because we rename the variables in the above expression to unique names, so we never re-define a variable.