Escolar Documentos
Profissional Documentos
Cultura Documentos
and
Logic
Programming
Lecture
7
-
Expression
Evalua%on
Context
&
Environment
Model
!
Yotam
Harchol
Fall
2015
Todays Agenda
Expression
Evalua0on
Context
for
Evalua0on
Environment
Model
Exercise
7
Expression
Evalua%on
Expression
deni0on:
Examples:
(+
1
2)
(*
7
8)
3
Exercise 1.1
Expression
Evalua%on
Recursive
expression
deni0on:
Examples:
(+
1
(*
7
8))
(*
(+
1
2)
(/
(+
3
1)
2))
5
Exercise 1.2
Examples:
(+
a
(*
b
c))
(*
(+
1
x)
(/
y
z))
Context
Exercise 2.1
10
Exercise 2.2
11
Exercise 3
12
Context
Context is the dictionary that
helps the interpreter to
translate from symbols to
values on the Eval stage
On Apply stage, a new, nested
function context is created
Global Context
+,
-,
/,
*,
quo0ent,
if,
lambda,
#t,
#f,
User
dened
contexts
Program Context
Inner contexts
Func0on Context
Parameter
Argument
couples
Inner
contexts
Inner Context
!
..
Param/Args
Inner
contexts
13
Context
A
new
context
is
created
when
a
lambda
expression
is
applied
The
new
context
contains
the
bindings
of
parameters
and
arguments
-
symbols
to
values
Dene
adds
a
binding
to
current
context
Let
is
lambda
-
so
it
creates
a
new
context...
14
Environment Model
15
Environment
Model
(define a 3)
(define foo (lambda (x y) (+ x y a)))
(define goo (lambda (a b) (* a (foo a b))))
(goo 1 2)
System Context
+:
-:
*:
/:
primitive
primitive
primitive
primitive
proc."
proc."
proc."
proc.
User Context
a: 3"
foo: user proc."
goo: user proc.
parameters: x, y
body: (+ x y a)
parameters: a, b
body: (* a (foo a b))
a: 1"
b: 2
Static Context:
Function is evaluated
in the context where
it was defined
x: 1"
y: 2
Dynamic
context:
Func0ons
body
is
evaluated
in
the
context
in
which
it
is
applied
19
Environment
Model
(define a 3)
(define foo (lambda (x y) (+ x y a)))
(define goo (lambda (a b) (* a (foo a b))))
(goo 1 2)
System Context
+:
-:
*:
/:
primitive
primitive
primitive
primitive
proc."
proc."
proc."
proc.
User Context
a: 3"
foo: user proc."
goo: user proc.
parameters: x, y
body: (+ x y a)
parameters: a, b
body: (* a (foo a b))
a: 1"
b: 2
Static Context:
Dynamic
Context:
Functionisisevaluated
evaluated
Function
in
in the
thecontext
contextwhere
where
it itwas
defined
is applied
x: 1"
y: 2
Using dynamic
context - what are
the values of the
symbols in blue?
Lambda
Expression
We
said
that
calling
to
(lambda
(...)
(...))
returns
a
black
box
which
is
an
internal
representa2on
of
a
func2on
!
Exercise 4
*
For
now,
assume
func0ons
can
only
get
a
xed
number
of
arguments
**
Note
that
the
current
expression
deni0on
does
not
allow
passing
func0ons
as
arguments
25
Ques%on
26
27
Example
code:
((lambda
(foo)
((lambda
(pi)
(foo
5))
10))
(lambda
(r)
(*
pi
r
r)))
29
Exercise 5
*
For
now,
assume
func0ons
can
only
get
a
xed
number
of
arguments
**
Note
that
the
current
expression
deni0on
does
allow
passing
func0ons
as
arguments
30
Evalua%ng
Macro
Lets
dene
now
an
expression
as
follows:
Evalua%ng
Macro
When
an
expression
begins
with
a
macro
expression
(such
as
(macro
(x
y
z)
(list
+
x
y
z))),
evaluator
should:
1.
Evaluate
the
arguments
2.
Apply
the
macro:
2.1.
Bind
parameters
to
evaluated
arguments
and
add
the
new
bindings
to
the
context
(crea0ng
a
new
context)
2.2.
Evaluate
the
body
of
the
macro
expression
in
the
new
context
Returned
value
is
an
expression
(created
by
the
macro)!
3.
Evaluate
the
returned
expression
in
the
context
of
run0me
32
Evalua%ng
Macro
Example:
Context
is:
((a
7)
(b
8)
(c
9))
Expression:
((macro
(x
y
z)
(list
+
x
y
z))
a
b
c)
!
Important Note
34
Exercise 6
* For now, assume func0ons can only get a xed number of arguments
35
nLambda
In
our
interpreter
we
invent
a
new
type
of
func0on
deni0on:
nLambda
expressions
nLambda
is
just
like
a
lambda,
except
for
the
fact
that
its
arguments
are
not
evaluated
before
the
func0on
is
called
nLambda
is
a
useful
tool
for
crea0ng
special
forms
!
Example:
(nlambda
(x)
x)
-
what
does
it
do?
36
Exercise
7
-
Mission
Func0ons
of
exercise
7:
<
15
lines
<
15
lines
Already
wriqen
<
20
short
lines
2
lines
2
lines
3
lines
Already
wriqen
1
line
2 lines
Exercise
7
-
Mission
8
pages
of
instruc0ons
and
guidelines
Hard
part
-
read
and
understand
Easy
part
-
write
the
code
Some
ini0al
code
is
supplied
-
do
not
change
it!
Addi0onal
test
code
(framework
+
sample
tests)
is
supplied
-
use
it
to
test
your
code
(you
may
add
more
tests,
as
only
few
are
supplied)
39
Exercise
7
-
Contexts
Our
interpreter
has
three
dierent
context
levels:
System
context
Contains
constant
system
deni0ons,
such
as
system
func0ons
and
constant
values
User
context
Contains
constant
user
deni0ons
(similarly
to
system
context,
but
as
a
second
level
context
below
it)
Run0me
context
Ini0ally
this
context
is
empty
(when
evalua0on
begins).
It
gets
lled
as
func0ons
are
being
called
Symbol
lookup
in
context:
1.
Look
in
current
run0me
context
2.
Look
in
user
context
3.
Look
in
system
context
40
Exercise 7 - Flags
Denes
the
context
model
of
the
interpreter.
Your
code
must
support
both
static
and
dynamic
modes.
Bonus:
implement
an
extended
version
of
the
switch
macro
inside
the
interpreter.
You
must
set
the
BONUS_ENABLED
to
#t
if
and
only
if
you
want
this
bonus
to
be
graded.
41
Exercise
7
-
Limits
Our
interpreter
will
use
the
following
func0ons
as
primi0ves
from
Dr.
Racket:
+ - * / < > <= >= <> = eq? equal? null? pair? cons
car cdr
43
Exercise
7
-
Steps
1. eval-args:
a
func0on
that
evaluates
a
list
of
arguments
2. bind:
binds
parameters
to
arguments
Should
also
support
func0ons
with
variable
number
of
arguments
3. eval-symbol:
evaluates
the
value
of
a
single
symbol:
If
the
symbol
is
a
special
keyword,
return
it.
If
the
symbol
represents
some
primi0ve
value,
return
the
primi0ve*.
Otherwise,
perform
lookups
in
contexts,
as
explained
before.
4. eval-if:
evaluates
an
if
expression
*
This
is
the
only
place
in
the
exercise
where
you
are
allowed
to
use
the
Racket
eval
func0on
44
Exercise
7
-
Steps
5. Evaluate
func0on
call:
Evaluate
an
expression
which
involves
a
func0on
(lambda
/
macro
/
nlambda
/
primi0ve
func0on)
call.
Some
parts
of
this
sec0on
are
similar
to
the
examples
we
did
in
class.
You
may
use
them,
but
not
as
is,
as
the
code
in
the
exercise
should
be
dierent
(does
not
do
exactly
the
same).
6. evaluate:
main
evalua0on
func0on
7. Bonus:
Add
the
class
macro
to
your
user
context
45
Example Run
46
Tes%ng
Framework
The
assert
macro
(supplied
with
the
exercise)
can
be
used
to
run
a
func0on
and
compare
its
return
value
to
some
expected
return
value
If
return
value
is
as
expected,
nothing
happens
Otherwise,
an
error
is
raised
You
can
put
as
many
assert
calls
as
you
wish
at
the
end
of
your
code,
to
automa0cally
test
it
However,
do
NOT
leave
such
test
code
in
your
submiqed
le!
47