Following are examples using bind
from class. Note that I’ve done no
proofreading, so the notes reflect the state they were in when my
lecture ended.
bind x=5 in x+x
== 5+5
== 10
bind x=5 in
bind y=6 in x+y
== bind y=6 in 5+y
== 5+6
== 11
bind x=5 in
bind x=6 in x+x
== bind x=6 in x+x
== 6+6
== 12
bind x=5 in
bind x=6+x in x+x
== bind x=6+5 in x+x
== 11+11
== 22
bind x=5 in
x + bind y=6 in x+y
== 5 + bind y=6 in 5+y
== 5 + 5 + 6
== 5 + 11
== 16
bind x=5 in
x + bind x=6 in x+x
== 5 + bind x=6 in x+x
== 5 + 6 + 6
== 5 + 12
== 17
bind x=5 in
x + y
== 5 + y
== !
bind x=x+1 in x
== !
Examples using abstract syntax.
eval (Bind “x” (Num 5)
(Bind “y” (Num 6)
(Plus (Id “x”) (Id “y”))))
== (Bind "y" (Num 6)
(Plus (Num 5) (Id "y")))
== (Plus (Num 5) (Num 6))
== (Num 11)
eval (Bind “x” (Num 5) (Plus (Id “x”) (Id “x”))))
== (Plus (Num 5) (Num 5))
== (Num 10)
eval (Bind “x” (Num 5)
(Bind “x” (um 6)
(Plus (Id “x”) (Id “x”))))
== (Bind "x" (Num 6)
(Plus (Id "x") (Id "x")))
== (Plus (Num 6) (Num 6))
== 12
eval (Bind “x” (Num 5)
(Bind “y” (Num 5)
(Bind “x” (Num 6)
(Bind “y” (Num 4)
(Bind “z” (Num 7)
(Id “z”))))))
==
Examples using deferred substitution.
eval (Bind “x” (Num 5) [("x",(Num 5))]
(Bind “y” (Num 6) [("y",(Num 6)),("x",(Num 5))]
(Plus (Id "x") (Id "y")))) [("y",(Num 6)),("x",(Num 5))]
== (Plus (Num 5) (Nun 6))
== 5+6
== 11
eval (Bind “x” (Num 5) [("x",(Num 5))] -- Environment
(Bind “x” (Num 6) [("x",(Num 6)),("x",(Num 5))] -- Shadowing "x"
(Plus (Id "x") (Id "x"))) [("x",(Num 6)),("x",(Num 5))]
== (Plus (Num 6) (Num 6))
== 6+6
== 12
eval (Bind “x” (Num 5) [("x",(Num 5))]
(Bind “x” (Num 6) [("x",(Num 6)),("x",(Num 5))]
(Plus (Num 6) (Id "y")))) [("x",(Num 6)),("x",(Num 5))]
== (Plus (Num 6)) ????
== Nothing
eval (Bind “x” (Num 5) [("x",(Num 5))]
(Plus (Num 5) [("x",(Num 5))]
(Bind “x” (Num 6) [("x",(Num 6)),("x",(Num 5))]
(Plus (Id "x") (Id "x")))) []
== (Plus (Num 5) (Plus (Num 6) (Num 6)))
== 5 + 12
== 17
eval (Bind “x” (Num 5) [("x",(Num 5))]
(Plus
(Bind “x” (Num 6) [("x",(Num 6)),("x",(Num 5))]
(Plus 6 6)) []
(Id "x"))) []
==