RU beehive logo ITEC dept promo banner
ITEC 380
2018fall
ibarland

T6: environments
extra credit

Due Dec.13 (Thu) 17:00
Submit: T6.rkt and T6-test.rkt (or, T6-java.jar) on D2L, and hardcopy with the changed code from T4 (tagged ;>>>T5 and ;>>>T6), under Dr. Barland's door.

Extra Credit: I'll take your percentage score on this assignment, and replace your lowest homework-score.

We continue to build on the T language implementation from previous homeworks (T2 sol'n posted; T4 sol'n discussed on Monday and full source by Mon 17:00. Note that T5 can be based off of the T2 solution for full credit). You may implement this homework in either Java or Racket (or another language, if you clear it with me). Label each section of lines you change with a comment “;>>>T5”. You don't need to turn in any hardcopy of unchanged-code (but do submit a fully-working copy in the drop-box, including all necessary files).

  1. T5 (25pts; This problem and the next are really the crux of the project.)
    Deferred evaluation: T5 doesn't add any new syntax, but it is a different evaluation model which will give us more expressive semantics.

    There are two problems1 with the substitution approach used in T2–T4: we fundamentaly can't create recursive functions, and it's hopeless for ever assigning to variables. We solve these problems with deferred substitution:

    Your test cases should include a recursive function, as well as the example below. Also, since eval now takes an extra argument, that suggests three to four check-expects with various environments (lists of bindings):

    Re-naming 'eval': Uh-oh, it'd be a pain to have to go and adapt all our existing test-cases for eval, to take an extra argument. I would advise something like3: search-replace all occurrences of eval in your tests to be instead calling “eval-helper”. Make that a function which simply calls eval with an empty-environment. Then, go change eval to have a second input, and add some two-argument test-cases as just mentioned.

    A step sideways: This algorithm as described lets us add recursive functions, but it also doesn't handle some expressions that T4 did! For example, so make-adder be all func m -> func n -> #n boii m# in call call make-adder(3)(4) gives an error "unbound identifier: m" if no substitution has been done. The problem will be fixed in T6: calling call make-adder(3) returns a function whose body still includes m and n, but lacks the fact that we'd like it's m to be bound to 3. One approach might be to have eval return both a value and a set of bindings to use that value with. T6 will take a slightly different approach, storing the necessary bindings inside the function-representation.)

    Note that this gives us dynamic scoping (which we'll mention in class):

    so m be all 100 
    in so addM be all func x -> #x boii m#
       in #so m be all 5 in call addM(3)
           boii
           so m be all 4 in call addM(3)#
    
    evaluates to 15, not 206 as we might prefer.

  2. (25pts total) T6: Implement static scope (closures). Copy your T0-T4 file/project to a new T55. You shouldn't need any additional test cases for T6; the tests for T0-T5 should suffice, although one or two T5 examples depending on dynamic binding should now have a new expected-result.
  3. Further extra-credit options (of varying difficulty):

Mutation in Racket

If you want to use mutation in your racket-implementation, use Advanced Student language. This language level includes set! (to assign to variables), and set-struct-field! (to assign to struct fields). Note that these two actions are very different, which is why racket gives them separate names; in Java assigning-to-field and assigning-to-local-variable can look identical (syntactically), despite giving rise to very different behavior.

Since the mutators return #void, and we want to return a (useful) value from every expression, we will use mutation inside a begin expression:

(define times-called 0)
(define (triplify-and-print n)
   (begin (printf "We use `begin` to call a function for a side-effect *and* return a value too.\n")
          (set! times-called (add1 times-called))
          (printf "This function has been called ~a time~a.\n"
                  times-called 
                  (if (= times-called 1) "" "s"))
          (* 3 n)))

(triplify-and-print 5)
(triplify-and-print 2)
     

Btw, it's worth noting that in full-racket, begin is implicit in almost all forms (e.g. function-bodies and cond-branches).


1 A third issue, pointed out in Programming Languages and Interpretation, is that evaluating deeply nested lets is an O(n) algorithm.      
2 Note that the list/map you recur with has the additional binding, but that recursive call shouldn't add/modify the list/map used at the top level. Since java.util.Map is inherently mutable, you'll want to make a new copy of that map recurring.      
3 In full-racket, we'd certainly want to overload eval, making the environment be an optional argument with a default value. But we'll stick with intermediate-student, to better catch errors (including any errors about accidentally not including a 2nd-argument to eval in places that you really mean to, namely: each recursive call).      
4 “What, racket uses references-to-structs instead of actual structs? Why didn't you tell us that earlier, Barland?” Because in the absence of mutation, no program's results can distinguish between having an object vs a reference-to-object-which-is-always-dereferenced. So it was like a religious-opinion difference, until getting mutation in Advanced Student.      
5 Presumably you do this for each new homework, but it's a particularly good idea for this one, since T5 is not just T4 plus new code, but instead replaces some of the T4 code (subst) with a different approach.      
6 You can think about which implementation you prefer, when using a language: when seeing the program's internal representation of your code (when debugging, or seeing the compiled result of your code).      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.