Tutorial 2026-02-04

Environments

CS 146 Tutorial 5

Faux Racket

exp = (fun(id) exp)
| ( exp exp)
| (with (id exp) exp)
| id
| (op exp exp)
  • Subsitution Rules

(with (x 1 )
      (with ( y ( + x 1 ))
       ( f( fun (x) ( * x 3)) y)))
  • fun(x) is Shadowing the (* 3) .

(with (y ( + 1 1 ))
 ((fun(x) ( * x 3 )) y)

with (y  2) 
 (( fun ... ) y )) 
 
 ((fun ( x) ( * x 3 )) 2 )
 ( * 2 3 ) => 6
circle-info

Do not substitute with a with or fun that redefines a var from the outer scope

circle-exclamation
  • Subst rebuilds the AST at every step.

defer substitution

  • Remember the value of the value of the var subst, only when we need to evaluate a var.

    • How?

      • Use AL ( represented by [])

Thus what does

produce?

triangle-exclamation
  • f uses n before it is defined

  • locals are being treated like globals

  • fun's and with's should have their own environment

Last updated