Sequencing & Mutation

Table of Contents

Are there computations that AAQZ4 can not do?

No

AAQZ4 is turing complete

1. Sequencing

if we just put sequencing in AAQZ4 as it is now we would get nothing out of it…

{(x) => {+ x 4}
        {* x 2}
        {- x 7}}

The only reason to have sequencing is for side effects

  • printing, read user input
  • mutation

AAQZ5 takes use to squencing because we now have printing and reading user input

2. Mutation

def f1():
 x = 0
 y = 0
 while x < 7
    y += x
    x += 1
 return y

def f2(obj):
 x = 0

 while x < 7:
    obj.y += x
    x += 1
 return


which function is worse to debug

f2

because f1 is doing mutation without the user having to even know if the function does mutation or not. This is called a Binding mutation or Rebinding.

f2 is visible to the caller because they can see what the mutations were.

  • ’obj.y += x’ is called structural mutation

2.1. Structural Mutation

lst = [1, 2, 3]
thing = f(lst)
print(lst)

What should this print… We dont know since f could of mutated the lst in any way that we dont know at the surface.

  • makes testing really hard
    • need to make sure that the state of the array is right

class Box:
    def __init__(self, val):
        self.val = val
    def unbox(self):
        return self.val
    def set_box(self, new_val):
        self.val = new_val

def change_box_to_five(b):
    b.set_box(5)

my_box = Box(17)
print(my_box.unbox()) # -> 17

change_box_to_five(my_box)
print(my_box.unbox()) # -> 5

my-box and b are in different scopes so how does change-box-to-five mutate mybox?

my-box is not passed as its object but rather its reference to it.

mybox points to Box(17) in memory and so does b within the function change-box-to-five

For this to work in AAQZ we need to modify interp to take in a store.

Date: 2024-11-01 Fri 00:00

Author: Anthony Rossi

Created: 2024-11-01 Fri 11:48