This feed contains pages with tag "cs3613".

The final exam, worth 50% of your mark, is tentatively scheduled for 2PM on April 19.

Details about topics will be added to this page closer to the date of the test.

## Background

We have talked a bit about how to impliment objects using `lambda`

,
and we're going to talk more about about it the next few weeks. In
this tutorial we're going to have a look at the
Racket object system
and in particular how it's used in the Racket
gui library.

We will use the dialect
`racket/gui`

in our examples today, so start your file with

```
#lang racket/gui
```

## Creating objects

Like many languages, objects are created using `new`

. In Racket, this looks just like a function

```
#lang racket/gui
(define frame (new frame% [label "I've been Framed"]))
```

There's a lot going on in that one line, but not much to look at when we run it. In Java and Python it's easy to forget the original metaphor of sending messages to objects, but Racket in it is explicit with

```
(send <object> message <arg1> <arg2> ...)
```

### Your turn

Steal the appropriate `send`

form from the
first example in the gui manual
to get a window popping up.

### Analysis

Now that we know that does something, let's look at it a bit from a
language perspective. For each of the following, try to classify it
as a value (like `lambda`

), or syntax (like `if`

).

`new`

`frame%`

`send`

Even syntax (sometimes called `special forms`

) mostly defines *expressions*.

- What's one glaring exception?
- What do
`new`

and`send`

evaluate to as expressions? The return value`send`

is kindof a trick question.

## The event loop

It turns out we just wrote a *concurrent* program: multiple things are
happening at the same time. We get a hint that this is the case
because we are returned to the REPL with our window still showing.

In the lower REPL window of DrRacket (or Emacs), type

```
(send frame show #f)
```

Notice the window goes away. The concurrency here is managed internally by an event loop. In order to really understand the difference with sequentially executing a sequence of functions or method calls, let's handle some user generated events.

Add the following code to your example

```
(define msg (new message% [parent frame] [label ""]))
(new button% [parent frame]
[label "Panic"]
[callback
(lambda (button event)
(send msg set-label "Don't Panic"))])
```

### Analysis

We've been using, but ignoring, something Racket calls
`initialization arguments`

e.g. `label`

and `parent`

. These are conceptually very similar to
constructor arguments in object oriented languages. Unlike Java they
use a named argument syntax rather than a positional one (Python also
has optional named arguments).

The most important initialization argument here is `callback`

. This
gives a function to run when the button is pressed. It's clear now
that the control flow is not totally under the control of our code:
like most gui frameworks, the runtime waits for an event and then
hands us back control temporarily.

Some more things to notice from a programming language perspective

- We're actually discarding the return value of
`(new button% ...)`

- What happens if we switch the order of
`(define msg ...)`

and`(new button% ...)`

. This seemingly relies on using`msg`

before it is defined. What do conclude (or remember) about the top level in a racket module (cf. our long discussion about recursion).

### Your turn

- combine what we saw so far to make the button kill the window.

## Drawing

Let's add a drawing area to our UI, so we can print more reassuring text

Here's some sample code to set up a canvas and print some text in it.

```
(define canvas (new canvas% [parent frame]))
(define (panic)
(let ([dc (send canvas get-dc)])
(send dc set-scale 5 5)
(send dc set-text-foreground "blue")
(send dc draw-text "Don't Panic!" 0 0)
(send dc set-scale 1 1)))
```

Nothing happens until you call the function `(panic)`

from the REPL.

### Your turn

- Make the callback for the button draw the big blue text.
- If you have time, add some state so that each press of the button draws the text in a different place

## Subclassing

A common way to reuse a class is to subclass it. In Racket this is done by using the
`class`

form.

Let's start with the following modification, which should not break anything.

```
(define my-canvas%
(class canvas%
(super-new)))
(define canvas (new my-canvas% [parent frame]))
```

Things to observe:

- We've define a new class, inheriting from canvas%
- We've explicitly called '(super-new)' to do the superclass initialization. This allows control over how initialization arguments are processed, i.e. before or after the superclass is initialized.
- Perhaps most interestingly from a programming language point of view, classes are values in the language, and we can bind them to identifiers just like numbers and functions.

In order to overide a method in a subclass, we use `define/override`

like follows

```
(define my-canvas%
(class canvas%
(super-new)
(define/override (on-event event)
(when (send event button-down? 'any)
(panic)))))
```

### Your turn

Modify the definition of my class so that button clicks cause diameter
10 circles to be drawn at the appropriate place. You can either use
`this`

to send messages to the current object, or use the
inherit
to make the methods you need available as regular functions.

The second midterm, worth 15% of your mark, will be held in class on March. 14, 2017.

Sample Questions will be reviewed in the tutorial on March 13.

## Background

This will be another review session for the midterm. To get maximum benefit, you should probably attempt the questions before the tutorial.

## Substitution

- Evaluate the following by substitution.

```
{with {x {+ 4 2}}
{with {x {+ x 1}}
{* x x }}}
```

Identify the problem with this definition of substitution semantics:

To substitute an identifier

`i`

in an expression`e`

with an expression`v`

, replace all instances of`i`

that are not themselves binding instances, and that are not in any nested scope, with the expression`v`

.Consider the following two different substititution rules for

`with*`

** Version 1 **

```
{with* {{x1 e1[E/y]} {x2 e2[E/y]} ... {x_k e_k}[E/y]}[E/y] newbody}
where
newbody = body if y in x1..xk
else
newbody = body[E/y]
```

** Version 2 **

```
{with* {} E2}[v/z] = E2[v/z]
{with* { {x1 E1} bindings } E3}[v/z] = {with {x1 E1}
{with* bindings E3}}[v/z]
```

Evaluate the following substitution under both versions.

`{with* {{y x} {x 2} {x x}} {+ x y}}[1/x]`

Explain the differing results in terms of scope.

## Lazy evaluation

- Evaluate the following using purely lazy substitution and purely eager substitution

```
{with {x 1}
{with {x {+ x 1}}
x}}
```

- Give an example of WAE code exhibiting
*name capture*

## de Bruijn Indices

- Convert the following WAE expression to deBruijn form.

```
{with {x 1}
{with {x {+ x 1}}
{with {y x} x}}}
```

- Convert the following FLANG expression to deBruijn form.

```
{with {f {fun {x} {+ x 1}}}
{with {y 2}
{call f y}}}
```

## Substitution Caches

- Write a lookup function with this type signature to look up a symbol in a substitution cache / environment.

```
(define-type BindingPair
[pair (name : symbol) (val : s-expression)])
(define-type-alias ENV (listof BindingPair))
(define (lookup [name : symbol] [env : ENV]) : s-expression
)
```

Here are some sample tests:

```
(define test-env
(list
(pair 'f '(lambda (x) (+ x 1)))
(pair 'x '1)))
(test/exn
(lookup 'y test-env) "free identifier")
(test
(lookup 'x test-env) '1)
(test
(lookup 'f test-env)
'(lambda (x) (+ x 1)))
```

- Explain why environments use a list or list-like structure, rather than something with a faster search time like a hash table.

## Dynamic and Lexical Scope

Explain why the following evaluation rule guarantees the corresponding interpreter will not have lexical scope.

eval ({fun { x } E} , sc ) = {fun { x } E}

Evaluate the following dynamically scoped racket code using environments.

```
#lang plai
(require plai/dynamic)
(let ([blah (lambda (func x) (func x))])
(let ((x 7))
(let ((f (lambda (y) (+ x y))))
(let ((x 6))
(blah f 5)))))
```

Evaluate the same code using substitution semantics (this will get a different answer, matching racket without the the

`(require plai/dynamic)`

. Note that this is worth doing in racket by copying and modifying the expression for each substitution.Modify the following evaluation rules so that they make sense for the limited dialect of racket used in the preceding example.

```
eval(N,env) = N
eval({+ E1 E2},env) = eval(E1,env) +
eval(E2,env)
; ...
eval(x,env) = lookup(x,env)
eval({with {x E1} E2},env) = eval(E2,extend(x,eval(E1,env),env))
eval({fun {x} E},env) = <{fun {x} E}, env>
eval({call E1 E2},env1) =
if eval(E1,env1) = <{fun {x} Ef}, env2> then
eval(Ef,extend(x,eval(E2,env1),env2))
else
error!
```

Evaluate the previous example using your new environment based evaluation rules.

Compare the behaviour of the following code under dynamic scope, substitution, and environment based evaluation. This question does not need a complete trace to answer.

```
{with {f {fun {y} {+ x y}}}
{with {x 7}
{call f 1}}}
```

## Functions as data structures

Fill in the definition of `tree-max`

so that it computes the maximum
number stored in a non-empty tree.

```
#lang plai
(define (make-tree left value right)
(lambda (key)
(case key
[(getLeft) left]
[(getValue) value]
[(getRight) right])))
(define empty-tree 'emptyTree)
(define (empty-tree? val) (eq? val 'emptyTree))
(define (make-leaf num)
(make-tree empty-tree num empty-tree))
(define (tree-max tree)
(let [(l _______________)
(r _______________)
(v _______________)]
(cond
[__________________________________________ v]
[_____________________________(max v (tree-max r))]
[_____________________________(max v (tree-max l))]
[else____________________________________________ ])
(define test-tree-1
(make-tree (make-leaf 1) 2 (make-leaf 3)))
(test (tree-max test-tree-1) 3)
```

## Curried Functions

One annoying feature of working with curried functions in racket is
the need to explicitely call them repeatedly once per argument. Write
a function `call`

that takes a curried function and a list of
arguments, and evaluates the appropriate number of calls.

```
#lang plai
(define plus
(lambda (x) (lambda (y) (+ x y))))
(test ((plus 1) 2) 3)
(define plus3
(lambda (x) (lambda (y) (lambda (z) (+ x (+ y z))))))
(test (((plus3 1) 2) 3) 6)
(define (call f l)
___________________________________________________
___________________________________________________
___________________________________________________)
(test (call plus '(1 2)) 3)
(test (call plus3 '(1 2 3)) 6)
```

## Background

We have seen several cases where the syntax supported by an
interpreter can be modified by using some kind of preprocessor
(e.g. converting `with*`

into nested `with`

.
This tutorial is on *macros*, which are a way for the programmer to
change the syntax of a language without changing the language
interpreter / compiler.

All of the following should be in `#lang plai-typed`

. You can use one
source file for the whole tutorial; otherwise you may need to
duplicate some definitions which are re-used.

## Part 1. Adding FLANG syntax to racket

The first new tool is define-syntax-rule

The key idea here is a *pattern*. In the example below, the
`(with (id val) body)`

s-expression acts something like a type variant in
`type-case`

. The `with`

is treated literally, and the other identifiers
are bound to whatever s-expression is in that position. This is a bit
analogous to a regular define, except notice the extra parens in the
`with`

syntax are no problem here.

```
#lang plai-typed
(define-syntax-rule (with (id val) body)
(let [(id val)]
body))
```

The body of the define-syntax-rule the racket code to replace the with form. Here is one test; add a couple more to convince yourself it is working.

```
(test (with (x 1) (+ x x)) 2)
```

Notice there is no quoting of the `with`

in this test; it is now a
Racket expression just like `let`

.

Use `define-syntax-rule`

to define syntax for `fun`

and `call`

so that
the following tests (borrowed from the lectures) pass

```
(test {with {add3 {fun {x} {+ x 3}}}
{with {add1 {fun {x} {+ x 1}}}
{with {x 3}
{call add1 {call add3 x}}}}}
7)
(test {call {call {fun {x} {call x 1}}
{fun {x} {fun {y} {+ x y}}}}
123}
124))
```

## Part 2. Short circuit

Some Racket forms like `and`

and `or`

look like function calls, but we
know they are not because they support short-circuit evaluation. In
this part we use `define-syntax-rule`

to define `And`

and `Or`

, which
are just like the lower case version except the short-circuit
evaluation is from the right.

```
(define-syntax-rule (And a b)
(if b a #f))
(define (die)
(error 'die "don't run this"))
(test (And (die) #f) #f)
(test/exn (and (die) #f) "don't run this")
```

Define `Or`

in a similar way to `And`

.

```
(test (Or (die) #t) #t)
(test/exn (or (die) #t) "don't run this")
```

## Part 3, multiple cases.

The
syntax-rules
form can be used test a sequence of patterns against a form starting
with one identifier. The following code *almost* works to satisfy the
tests. Thinking about `cond`

, what small change do you need to make?

```
(define-syntax arghh
(syntax-rules ()
[(arghh) 0]
[(arghh a) 1]
[(arghh (a)) 2]))
(test (arghh) 0)
(test (arghh thptt) 1)
(test (arghh (die)) 2)
```

## Part 4, with*, again.

Recall `with*`

construct from Assignment 4. One of the ways to solve
this was to convert `with*`

into a nested set of `with`

forms. It
turns out this is fairly easy to do with syntax rules and recursion.
A key ingredient is the ability to
match sequences
with syntax patterns.

Fill out the following `syntax-rules`

in order to expand the `with*`

into a `with`

containing a with* (or if you prefer to skip a macro
level, into a `let`

containing a `with*`

.

Note that `...`

is really valid racket syntax here, and you can place
it in your expansion to mean "copy the matched sequence". Roughly
speaking, `(id val) ...`

matches a sequence of ```
(id1 val1) (id2 val2)
(id3 val3)
```

```
(define-syntax with*
(syntax-rules ()
[(with* () body) body]
[(with* ((first-id first-val) (id val) ...) body)
]
(test {with* {{x 5} {y {- x 3}}} {+ y y}} 4)
(test {with* {{x 5} {y {- x 3}} {y x}} {* y y}} 25)
```

## Getting started

Open a new file in `DrRacket`

and add the following definitions to the
beginning. This makes available some definitions from racket in
`plai-typed`

.

```
#lang plai-typed
(module tutorial6 typed/racket/base
(require racket/math)
(require (prefix-in plot: plot))
(provide plot sin cos pi)
(define (plot [fn : (Real -> Real) ] [from : Real] [to : Real])
(plot:plot
(plot:function fn from to)))
)
(require (typed-in 'tutorial6
[plot : ((number -> number) number number -> void)]
[pi : number]
[sin : (number -> number)]
[cos : (number -> number)]))
```

For the rest of the tutorial, add various definitions discussed to your file. Make a note of the types of the functions discussed.

## Derivatives and integrals

We can (crudely) approximate a derivative with the following higher order function.

```
(define dx 0.001)
;; compute the derivative of `f` at the given point `x`
(define (deriv f x)
(/ (- (f (+ x dx)) (f x)) dx))
```

Similarly, we can approximate the integral of a function at a point (more precisely from 0 to the point):

```
;; compute an integral of `f' at the given point `x'
(define (integrate f x)
(local
[(define (loop y acc)
(if (> y x)
(* acc dx)
(loop (+ y dx) (+ acc (f y)))))]
(loop 0 0)))
```

And say that we want to try out various functions given some `plot' function that draws graphs of numeric functions, for example:

```
(define -2pi (* -2 pi))
(define 2pi (* 2 pi))
(plot sin -2pi 2pi)
```

The problem is that `plot`

expects a single `(number -> number)`

function -- if we want to try it with a derivative, we can do this:

```
;; the derivative of sin
(define sin-deriv (lambda (x) (deriv sin x)))
(plot sin-deriv -2pi 2pi)
```

But this will get very tedious very fast -- it is much simpler to use an anonymous function:

```
(plot (lambda (x) (deriv sin x)) -2pi 2pi)
```

we can even verify that our derivative is correct by comparing a known
function to its derivative. Fill in the blank in the following with
the derivative of `sin`

```
(plot (lambda (x) (- (deriv sin x) )) -2pi 2pi)
```

But it's still not completely natural to do these things -- you need to explicitly combine functions, which is not too convenient. What can you observe about the numerical error here?

### Combinators

Instead of explicitely combining , we can write H.O. functions that will work with functional inputs and outputs. Such functions are called 'combinators. For example, we can write a function to subtract functions:

```
(define (fsub f g)
(lambda (x) (- (f x) (g x))))
```

Notice that the inferred type for fsub is a bit too general. Add type annotations to fix this.

We can similarly mamke a combinator to compute (indefinite) integrals.

```
(define (fderiv f)
(lambda (x) (deriv f x)))
```

Now we can try the same error calculation in a much easier way:

```
(plot (fsub (fderiv sin) cos) -2pi 2pi)
```

More than that -- our `fderiv`

could be created from `deriv`

automatically:

```
;; convert a double-argument function to a curried one
(define (currify f)
(lambda (x) (lambda (y) (f x y))))
;; compute the derivative function of `f'
(define fderiv2 (currify deriv))
```

Same principle with `fsub`

: we can write a function that converts a
binary arithmetical function into a function that operates on unary
numeric function. But to make things more readable we can define new
types for unary and binary numeric functions:

```
;; turns an arithmetic binary operator to a function operator
(define (binop->fbinop op)
(lambda (f g)
(lambda (x) (op (f x) (g x)))))
```

Use binop->fbinop to define a new version of fsub (e.g. calling it fsub2).

As a final exercise, define a combinator called "fintegrate" to make the following plot call work:

```
;; want to verify that `integrate' is the opposite of `deriv':
;; take a function, subtract it from its derivative's integral
(plot (fsub sin (fintegrate (fderiv2 sin))) 0 2pi)
```

This is one line if you use `currify`

.

The first midterm, worth 15% of your mark, will be held in class on Feb. 7, 2017.

Details about topics will be added to this page closer to the date of the test.

As practice for the midterm, try to do the following exercises on paper first, then check your answers.

### Racket 101

For each of the following expressions, decide what the resulting value
(or error) is in `plai-typed`

```
(if "true" 0 1)
(first (rest (list 1 2)))
(cond
[(> 3 1) 0]
[(< 3 1) 1])
(and #f (= 0 (/ 1 0)))
(cons 3 (list 1 2))
(let ([ x -1])
(let ([ x 10]
[ y (+ x 1) ])
(+ x y)))
(define (f n)
(if (<= n 1)
1
(f (- n 1))))
(f 100)
(define (g l)
(lambda (h)
(cons h l)))
(g 100)
(g (list 1 2 3))
((g (list 1 2 3)) 100)
```

### Higher order functions

Find the type of the functions `a`

, `b`

, and `c`

```
(define (a f n)
(f n))
(define (b f n)
(lambda (m)
(f n m)))
(define (c f g h)
(lambda (x y)
(f (g x) (h y))))
```

### Tail recursion

Write a tail recursive function to multiply two non-negative integers. Your function should use

`+`

but not`*`

.Write a tail recursive function to sum the elements of a list.

### Grammars

Write a ragg format grammar that (given a suitable lexer) passes the following tests.

```
(test
(string->sexpr "wow. such language. very tricky. wow.")
'(doge "wow" "." "such" (subject "language") "." "very" (adjective "tricky") "." "wow" "."))
(test
(string->sexpr "wow. such course. very tricky. wow.")
'(doge "wow" "." "such" (subject "course") "." "very" (adjective "tricky") "." "wow" "."))
(test
(string->sexpr "wow. such course. very difficult. wow.")
'(doge "wow" "." "such" (subject "course") "." "very" (adjective "difficult") "." "wow" "."))
(test
(string->sexpr "wow. such prof. very mean. wow.")
'(doge "wow" "." "such" (subject "prof") "." "very" (adjective "mean") "." "wow" "."))
```

To test your answer you can use doge-lexer.rkt and doge-driver.rkt

### Substitution

Consider an extended `with`

syntax such that each `with`

can have
multiple bindings, but the named-expression refers to the outer scope,
not the the previous bindings. In particular the following evaluates
to 3, not 4. This is the same semantics as racket's let, so you can
try more examples by replacing "with" with "let" in the code.

```
{with {{x 1}}
{with {{x 2} {y x}} {+ x y}}}
```

Give English/pseudocode rules to perform the following substitution

```
{with { {x1 e1} ... {x_l e_k} } body}[E/y]=
subst({with { {x1 e1} ... {x_l e_k} } body},E,y) =
```

## Introduction

In this assignment you will use the ragg parser toolkit to parse transcripts of encounters with dogs. These are represented as strings containing "wag", "growl", "bark", "slobber", and "bite", separated by whitespace. Each encounter is ended with a ’.’; an example input follows:

```
slobber slobber.
wag wag slobber. growl bark.
growl bark bite.
```

You should have help on `ragg`

in drracket, under "Help -> Racket Documentation -> Miscellaneous Libraries".

## Structure of solutions.

Unlike in your assignments, you will need to use multiple files for this tutorial. You can download the lexer

Write your parser in a second file starting with `#lang ragg`

, and use
the two of them from a third driver file like the following. Note that
we're using `plai`

, not `plai-typed`

. Also notice you need to use some
custom error handling code, rather than `test/exn`

.

```
#lang plai
(require "grammar-1.rkt")
(require "lexer.rkt")
(define (string->sexpr str)
(syntax->datum (parse (tokenize-string str))))
(define (catch-parse-error str)
(with-handlers ([exn:fail? (lambda (v) 'fail)])
(string->sexpr str)))
(test
(string->sexpr "bark bark.")
'(dog (sentence (angry (before-bite "bark" "bark")) ".")))
(test (catch-parse-error "bite.") 'fail)
```

## 1. Happy Dog, Angry Dog

In this question you should construct BNF grammar that enforces the following rules:

- Every dog encountered is either happy or angry.
- A happy dog always slobbers at least once.
- A happy dog may wag several times before slobbering, but once it starts slobbering, it stops wagging.
- An angry dog may growl several times before barking, but once it starts barking, it stops growling.
- An angry dog does not necessarily bite, but it always growls or barks before biting.

The examples in the introduction are all valid for these rules. Here some examples of invalid encounters.

```
bite.
bark wag.
growl bark growl.
```

Here is the beginning of a solution

```
dog: sentence +
sentence: happy "." | angry "."
```

Add tests to your driver to test each of the 5 rules given above.

## 2. To every Slobber a Wag

As an extra challenge, modify your first grammar to enforce the additional rule that the number of wags and slobbers of any happy dog is exactly equal. For this question "wag wag slobber slobber." is valid, but "wag slobber slobber." and "wag wag slobber." are not.

## Types

Use pencil and paper to work out the type of the following function. Check your answer using DrRacket.

```
#lang plai-typed
(define (f a b c)
(a (b c)))
```

### Tail recursion

Complete the following tail recursive function to count the number of
odd and even numbers in a list. You may use the built in functions
`odd?`

and `even?`

. Use the racket debugger to test the stack usage of
your function.

```
(define (helper lst odds evens)
(cond
[(empty? lst) (list odds evens)]
;; missing two cases
))
(define (odds-evens lst)
(helper lst 0 0))
(test (odds-evens (list 3 2 1 1 2 3 4 5 5 6)) (list 6 4))
```

## Part 1

For each of the following code samples, add sufficient test forms to get full coverage

### Sample 1

```
#lang plai-typed
(define (digit-num n)
(cond [(<= n 9) (some 1)]
[(<= n 99) (some 2)]
[(<= n 999) (some 3)]
[(<= n 9999) (some 4)]
[else (none)]))
```

### Sample 2

```
#lang plai-typed
(define (helper n acc)
(if (zero? n)
acc
(helper (- n 1) (* acc n))))
(define (fact n)
(helper n 1))
```

### Sample 3

For the parser example, you'll need to peruse the documentation on expressions to figure out how to test for errors. You may also find the S-expression documentation helpful in constructing test input.

```
#lang plai-typed
(define-type AE
[Num (n : number)]
[Add (l : AE) (r : AE)]
[Sub (l : AE) (r : AE)])
(define (parse [s : s-expression])
(cond
[(s-exp-number? s) (Num (s-exp->number s))]
[(s-exp-list? s)
(let* ([sl (s-exp->list s)]
[op (s-exp->symbol (first sl))]
[left (second sl)]
[right (third sl)])
(case op
[(+) (Add (parse left) (parse right))]
[(-) (Sub (parse left) (parse right))]))]
[else (error 'parse-sexpr "bad syntax")]))
```

## Part 2

Sample 3 has several bugs, namely syntax errors that crash the parser, rather than generate error messages about the input. Correct at least one of these bugs, and add a suitable test.

## Part 1

Follow the instructions at racket-setup to get racket set up for this course. If the system version of drracket is not available (e.g. in the menus) yet, then you can start drracket by opening a terminal and running

```
% ~bremner/bin/drracket
```

## Part 2

Do the Quick racket tutorial. Although you can just copy and paste the text into DrRacket, you will get more benefit from typing things in and more importantly from changing things and observing the results.

## Garbage Collection

Consider the following definitions in the format of Homework 2; the vector represents a heap, with each heap location either being a primitive or an object to other heap locations.

```
(define-type Object
[object (Listof Index)]
[primitive])
(define example-heap
(vector
(primitive) ; 0
(object (list 8)) ; 1
(object (list 1)) ; 2
(object (list 7)) ; 3
(object (list 0 3)) ; 4
(primitive) ; 5
(object (list 0)) ; 6
(object (list 4 6)) ; 7
(object (list 2)))) ; 8
```

(1) Give a minumum (smallest possible) set of roots so that a marking phase will find no garbage in the heap.

(2) Suppose the set of roots is empty. How much of the heap will be marked as live by reference counting? Suppose the value at location 7 is a primitive, how does this change your answer?

## rewrite rules

-( 1) Give a rewrite rule (in the `#lang pl broken`

dialect) for a short
circuiting `nand`

(negated and) by transforming it to not and.

```
(test (nand #f) => #t)
(test (nand #f (/ 1 0)) => #t)
(test (nand #t #f (eq? (/ 1 0) 0)) => #t)
```

- (2) Give a rewrite rule for
`and`

(here called`and2`

to reduce confusion) that uses andmap to implement it. You will probably need a computer to get the details right here. The real issue is quoting or delaying evaluation.

```
(test (and2 #f) => #f)
(test (and2 #f (/ 1 0)) => #f)
(test (and2 #t #f (eq? (/ 1 0) 0)) => #f)
```

All of the examples are either the course language "PL", or the toy language "FLANG". It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## Recursion

- (1) Use
`let`

and`lambda`

to define a recursive list length function that takes itself as a parameter, so that the following test passes.

```
#lang pl broken
(test
(let (
; ...
)
(len len '(a b c d e))) => 5)
```

- (2) Write the curried version of
`len`

, (still using`let`

and`lambda`

) that makes the following test pass.

```
#lang pl broken
(test
(let
;...
((len len) '(a b c d e))) => 5)
```

- (3) fill in the definition of make-len to make the following test pass.

```
#lang pl broken
(test
(let
((make-len
; stuff
(if (null? list)
0
(+ 1 (len (rest list)))))))))
(let ([len (make-len make-len)])
(len '(a b c d e)))) => 5)
```

## Evaluation Delay

- (1) What is the output of running this racket code?

```
#lang pl broken
(define (make-test self)
(let ([test (self self)])
(lambda (arg)
(if arg
arg
(test (not arg))))))
(display "after define make-test\n")
(define test (make-test make-test))
(display "after define test\n")
(display (test #f))
```

- (2) Modify the let statement in the above code so that it runs to completion.

## Y combinator

- (1) What is the output from the following code? Explain your answer.

```
((lambda (x) (x x)) (lambda (x)
(begin
(display "hi!\n")
(x x))))
```

- (2) Given the following definitions, trace the evalutation of
`(test #f)`

*Use a computer for this one*.

```
#lang pl broken
(define (make-recursive f)
((lambda (x) (f (lambda (n) ((x x) n))))
(lambda (x) (f (lambda (n) ((x x) n))))))
(define test
(make-recursive
(lambda (test)
(lambda (arg)
(if arg
arg
(test (not arg)))))))
```

All of the examples are either the course language "PL", or the toy language "FLANG". It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## Data structures with functions

Fill in the definitions of `empty-map`

and `extend`

such that the
given tests pass.

```
(define-type-alias Map (String -> Number))
; stuff
(define test-map
(extend "mary" 1
(extend "bob" 2
(empty-map))))
(test (test-map "bob") => 2)
(test (test-map "mary") => 1)
(test (test-map "happiness") =error> "empty map!")
```

## Embedding

- (1) Given the syntactic (i.e. not using racket procedures) definition of
an FLANG function, write the
`funcall`

function that allows calling FLANG functions from Racket. This one you probably will need a computer to get the details right, but try to get the main idea on paper.

```
(define-type VAL
[NumV Number]
[FunV Symbol FLANG ENV])
(define foo (eval (parse "{fun {x} {+ x 1}}") (EmptyEnv)))
(define bar (eval (parse "41") (EmptyEnv)))
(: funcall : VAL VAL -> VAL)
; define funcall
(test (funcall foo bar) => (NumV 42))
```

- (2) Now consider the FLANG interpreter that uses union types to represent FLANG values. Write funcall for this intepreter.

```
(define-type VAL = (U Number (VAL -> VAL)))
; write funcall; don't forget a type signature.
(test (funcall f 41) => 42)
```

All of the examples are either the course language "PL", or the toy language "FLANG". It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## Substitution Caches

- (1) Write a lookup function with this type signature to look up a symbol in a substitution cache / environment.

```
(: lookup : Symbol (Listof (List Symbol Sexp)) -> Sexp)
```

- (2) Explain why environments use a list or list-like structure, rather than something more efficient like a hash table.

## Dynamic and Lexical Scope

(1) Explain why the following evaluation rule guarantees our the corresponding interpreter will not have lexical scope.

eval ({fun { x } E} , sc ) = {fun { x } E}

(2) Evaluate the following dynamically scoped racket code.

```
#lang pl dynamic
(define (blah func x)
(func x))
;
(let ((x 7))
(let ((f (lambda (y) (+ x y))))
(let ((x 6))
(blah f 5))))
```

(3) Evaluate the same code using substitution semantics.

(4) Modify the following evaluation rules so that they make sense for the limited dialect of racket used in the preceding example.

```
eval(N,env) = N
eval({+ E1 E2},env) = eval(E1,env) +
eval(E2,env)
; ...
eval(x,env) = lookup(x,env)
eval({with {x E1} E2},env) = eval(E2,extend(x,eval(E1,env),env))
eval({fun {x} E},env) = <{fun {x} E}, env>
eval({call E1 E2},env1) =
if eval(E1,env1) = <{fun {x} Ef}, env2> then
eval(Ef,extend(x,eval(E2,env1),env2))
else
error!
```

(5) Evaluate the previous example using your new environment based evaluation rules.

(6) Compare the behaviour of the following code under dynamic scope, substitution, and environment based evaluation.

```
{with {f {fun {y} {+ x y}}}
{with {x 7}
{call f 1}}}
```

All of the examples are either the course language "PL", or the toy languages "WAE" or "FLANG" (WAE with functions). It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## Lazy evaluation

- Evaluate the following using purely lazy substitution and purely eager substitution

```
{with {x 1}
{with {x {+ x 1}}
x}}
```

- Give an example of WAE code exhibiting
*name capture*,

## de Bruijn Indices

Convert the following WAE expression to deBruijn form.

```
{with {x 1}
{with {x {+ x 1}}
{with {y x} x}}
```

All of the examples are either the course language "PL", or the toy languages "WAE" or "FLANG" (WAE with functions). It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## BNF and Parsing

Consider the subset of Racket consisting of `define`

, +,-,*, and
numbers and identifiers. Complete the following BNF for this
language, to include defining and calling functions.
If you want to experiment, you may find
lexer.rkt and
driver.rkt helpful.

```
expr: arith | define | IDENT | NUMBER
arith: "(" op NUMBER+ ")"
op: "+" | "-" | "*"
define: "(" "define" IDENT expr ")"
```

### Using BNF

The following expression reveals at least one error in the grammar given above. Correct it.

```
(define (glug x) (if (zero? x) 1 (* x (glug (- x 1)))))
```

### Using match

Fill in the following `match`

based parser for
lists of `a`

s and `b`

s

```
(define-type ABList
[Empty]
[A ABList]
[B ABList])
(: parse-sexpr : Sexpr -> ABList )
(define (parse-sexpr sexpr)
(match sexpr
;; stuff
))
(test (parse-sexpr '(a b a)) => (A (B (A (Empty)))))
(test (parse-sexpr '(a)) => (A (Empty)))
```

## More types

Give a valid type declaration for the following function

```
(define (glonk f g)
(lambda (x)
(let* ((h (g x))
(i (f h)))
i)))
```

## Substitution

- Evaluate the following by substitution.

```
{with {x {+ 4 2}}
{with {x {+ x 1}}
{* x x }}}
```

Identify the problem with this definition of substitution semantics:

To substitute an identifier

`i`

in an expression`e`

with an expression`v`

, replace all instances of`i`

that are not themselves binding instances, and that are not in any nested scope, with the expression`v`

.

## More Higher Order Functions

For each of the function type declarations below, write a PL Racket function with that type declaration. Each function should do something "sensible"; describe what that is in a few words.

```
(define-type UnaryFun = (Real -> Real))
(define-type BinaryFun = (Real Real -> Real))
(: a : UnaryFun Real -> Real )
(: b : BinaryFun Real -> UnaryFun )
(: c : BinaryFun UnaryFun UnaryFun -> BinaryFun)
(: d : UnaryFun UnaryFun -> UnaryFun)
```

All of the examples are either the course language "PL", or the toy languages "WAE" or "FLANG" (WAE with functions). It's fine (and recommended) to use a computer to check your answers, but remember that you won't have a computer on the exam.

## Racket

For each of the following untyped racket expressions, write what it evaluates to (if valid) or explain what the error is.

```
#lang pl
(if "true" 0 1)
(first (rest '(a b)))
(cond
[(> 3 1) 0]
[(< 3 1) 1])
(+ 1 2 3)
(and #f (/ 1 0))
(cons 3 (list 1 2))
(let ([ x -1])
(let ([ x (+ 10 x)]
[ y (+ x 1) ])
(+ x y)))
```

## Types

Give a valid type declaration for each of the following functions. Don't worry too much about the subtleties of different kinds of numbers.

```
(define (f) 8)
(define (g x)
(cond
[(string? x) x]
[(number? x) x]
[else #f]))
(define h (lambda (x y)
(string-length (string-append x y))))
```

## Variant types

Given the following variant type declaration, define a function to
test two shapes for equality (never mind the built-in `equal?`

predicate does this perfectly well).

```
(define-type Shape
[Square Number] ; Side length
[Circle Number] ; Radius
[Triangle Number Number]) ; height width
```

## Higher order functions

Consider the tests

```
(test ((pair 1 3) +) => 4)
(test ((pair 1 3) -) => -2)
(test ((pair 2 3) *) => 6)
```

Write the function

`pair`

to pass these tests.Give a valid type declaration for

`pair`

## Tail recursive functions

In the following tail recursive function, what should `<if-expr>`

and
`<second-argument>`

be?

```
(: sum : (Listof Number) Number -> Number )
(define (sum list acc)
(if (null? list)
; <if-expr>
(sum (rest list)
; <second-argument>
)))
( test (sum '(4 5 6) 0) => 15)
```