
Title:
Bending Numbers Solution  Programming Languages

Description:

In this homework assignment, we asked you

to do a few optimizations. The very basic

optimizations you can do are very, very

simple optimizations on binary operations, on

basic arithmetic, and the ones we specifically

asked you to do are these right here as well as

replacing expressions with constants when

applicable. So we gave you this one right here

and then, to add in the others, we just do this

great big ‘else… if…’ statement. So the first

one, we check that the operation is times, we

check that A is a number and it’s a number

equal to one. And if it is, well, one times

anything is that second thing, is B. So we just

returned B. We don’t have to wait and run this

through once we’re interpreting. That would

take more effort than we need to. Now we do

the same thing with the next one if B is one

and A is something else. We can just return A.

Now we can do the same thing with addition

and 0. We just check if the operation is equal

to plus and that A is a number and it’s a

number equal to 0, then we return B. If B is a

number equal to zero, then we return A. And

finally, the last one that we asked you to

implement is a number minus itself is 0. So

that’s fairly straightforward too. Check if the

operation’s equal to minus and then if A

equals B – and remember, you can do that

because you can check if Python tuples are

equal to each other, not just numbers – then

we return a tuple of number and 0 to indicate

the number 0 in our abstract syntax tree. Now

the constant folding is a little bit more work,

but really not too much. First, we check that

the first element of the tuples A and B are

number. Then if the operation is addition, if

the operation equals plus, then we return

number and second element of A and B added

together. So that way, we don’t have to run

that operation later on when we are actually

running the program. And we can do the same

thing if we check if the operation is minus,

then we run – then we return tuple of number

and the second element of A and B subtracted

and similarly for multiplication. We can just

do the same thing for the second elements of

A and B, multiply together. And then once we

have hoped that the operations have been

optimized in some way, we wrap it back up in

a binary operation and a tuple of BinOp, AOP

and B, and return that. And if none of this

worked, if we didn’t even get a BinOp to

begin with, then we just return the expression

because those are all the optimizations that we

have done. So let’s try that with a few of these

test cases and these are the test cases that we

provided you with. So we have zero, one and

two, numbers equal to 0.0, 1.0 and 2.0, and a

few variables that are ancient kings and

queens of Persia and Macedonia – Xerxes,

Darius, Antiochus and Musa – and then we’re

going to define a plus operation, just so we

don’t have to keep writing this tuple out, and a

minus operation and a times operation

similarly, just to save some key strokes. And

then we’re going to check whether an

expression that we’ve said, which is times 2

and 0 is equal to 0. That is their optimization

actually ran. We’re going to do the same thing

with a minus operation and a slightly more

complicated operation. Expression three is

minus plus zero plus one plus two zero two.

That was a quite a big mouthful, so let’s go

through it just a bit more indepth. Plus two

and zero, so two plus zero and then we wrap it

in this call to plus, so two plus zero plus one

and we wrap it again in a plus zero, and then

we wrap all of this up and subtract two from

whatever this is. So this really should just be

three minus two. Once you get past all of the

massive amounts of parentheses. So we print

out and make sure that that is indeed equal to

one. We do the same thing for these

expressions as well. So when we run this, we

see that we pass all of our test cases. And this

– our test cases involved a bunch of hand

checking, where we just made all these up and

handchecked that this is what they’re suppose

to be and then verified that the optimization

actually does what we think it’s going to do.

This isn’t necessarily how you would

normally do it, but it’s good enough to serve

our purposes right now.