## Calculating WAE Expressions

Moderator: Konzepte der Programmiersprachen

saifposinc
Neuling
Beiträge: 7
Registriert: 10. Dez 2018 19:20

### Calculating WAE Expressions

Hello,

I didn't understand the topic: "Calculating WAE Expressions" in V02.pdf > slide 21. Can someone please explain these things simply:

How come we get these lines?:
...
case Let(boundId, namedExpr, boundExpr) => {
interp(subst(boundExpr, boundId, Num(interp(namedExpr))))
}
case Id(name) => sys.error("found unbound id " + name)
...

where are we using the "subst" function?

[I already know the definition of substitution, also know Binding instance, scope, bound instance, free instance and their positions in a function]

Also, didn't understand this part (slide 26), how come we are getting these:
...
case Sub(lhs, rhs) => Sub(subst(lhs, substId, value), subst(rhs, substId, value))
case Let(boundId, namedExpr, boundExpr) => {
val substNamedExpr = subst(namedExpr, substId, value)
if (boundId == substId)
Let(boundId, substNamedExpr, boundExpr)
else
Let(boundId, substNamedExpr, subst(boundExpr, substId, value))
}
...

And if you know, please refer book chapter or link that elaborates the topic.

-Saif

a10r
Erstie
Beiträge: 11
Registriert: 4. Nov 2012 13:53

### Re: Calculating WAE Expressions

It's hard to help you when you're being so vague about what exactly you don't understand. Try to ask more precise questions. If you have trouble with substitution, evaluate some expressions on paper first. Then the code should become obvious.

The first snippet in you post has a call to subst in the Let case. subst takes an expression (here: boundExpr) and replaces a symbol (here: boundId) with some other expression (here: the interpreted result of namedExpr).

The second snippet is a part of the subst function. Add and Sub cases do nothing special, they simply recurse down into the sub-expressions. The condition in the Let case is due to shadowing. Once you hit a Let node that has the same boundId as the id you are currently substituting with (substId), the scope of substId ends and you have to stop substituting. There is an example on shadowing on slide 18.