|
Due Dec.02 (Mon) in class.
It is intended that you can finish Friday before the break,
but I will have it due afterwards.
(Note that the last homework will be available before the break,
should you want to work on that.)
Submit:
a hardcopy with the prolog queries for this file,
and just the additional tests for O3
and just the function(s) that changed between O2 and O3.
On D2L, submit a file for your prolog solution,
as well as one set of files for O5/O6.
We continue to build on the language implementation started in hw06. You can implement this homework in either Java or Racket. Please indicate in your submitted file, what sections of code are udpated and what is unchanged from hw06/hw06-soln. You don't need to turn in any hardcopy of unchanged-code (but submit a fully-working copy in the drop-box).
O3 is just like O2, except we now
allow one variable to shadow another.
For example, we might have a
Thus, when substituting,
only substitute “free occurrences” of an
(5pts)
Fill in the following blanks:
Re-indent
the following two O3 expressions so that
each
let x := 5 in (let x := (x add 1) in (x add 2) end;) end; let y := let z := 4 in (let y := 99 in z end;)end; in (let z := 5 in ((let z := 10 in y end;) add (y add z)) end;)end; |
You can put all your answers in comments near the top of your main file (O3.rkt or Expr.java), and in the hardcopy code-excerpts.
(5pts)
Make the necessary changes to O2
to enable shadowing.
(You are encouraged to build on your solution,
but you can also use the posted O2 solutions.)
The change should be quite small:
similar to the difference between
change-blue-to-brown vs. change-blue-to-brown-stopping-at-green.
(26pts) O4 adds (non-recursive) functions and function-application to our language:
Expr ::= … | FuncExpr | FuncApplyExpr FuncExpr ::= {Id => Expr} FuncApplyExpr ::= [Expr @ Expr] |
Here is an example of a function; it happens to compute a number’s absolute value:
{ x => if x isNeg then (-1 mul x) else x;} |
A
[{ x => if x isNeg then (-1 mul x) else x;} @ -5] let abs := {x => if x isNeg then (-1 mul x) else x;} in [abs @ -5] end; |
To evaluate a function-application
Observe that our programs can now evaluate to either of two types:
numbers or functions.
In Java,
we'll need a class which can represent either,
as the return type for our
Make test cases for
Note: You won't be able to evaluate function-applications for recursive functions yet (see O5), but we can still write the test cases! (You can comment out that one test case for now, since it'll trigger a run-time exception otherwise.)
(define (make-adder n) (lambda (m) (+ n m))) ; Two examples of applying this function: ; (make-adder 3) ; evals to (lambda (m) (+ 3 m)) ((make-adder 3) 4) ; evals to 7 |
Note that we're restricting O4 to only deal with unary functions (functions of one argument).
To think about: If we wanted to 'fake' functions of 2 arguments in N4, how could we do it? For example, you might think about how to write a function that effectively takes in two numbers i,j and returns 2·i+j. Think about howmake-adder does this.
The interpreter project is based on the first chapters of Programming Languages and Interpretation, by Shriram Krishnamurthi. As a result, this homework assignment is covered by the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. Although we're using a different dialect of racket than that book, you might find it helpful to skim it.
1
The notation
“
eval(parse("let x := 5 in (x plus 3) end;")) = eval(parse("(5 plus 3)")) = eval(parse("8")) |
2
nor any “binding occurrences”:
The first
©2014, Ian Barland, Radford University Last modified 2014.Sep.27 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |