Data-Driven-World / d2w_ml_notes

Notes for Data Driven World

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The second example in pure function is a bit confusing (State Machine)

Jiang-Wenchao opened this issue · comments

Another example is a function that returns true if all the inputs are true. This is an AND gate implemented as a function.

Why is this example regarded as a pure function, not a state machine?

Actually, it is a good example of a state machine. Suppose we have n truth values, say i_1, i_2, ... i_n. We can view the last input i_n as the input, and the s_n = AND(i_0, ...i_(n-1)) is the state. Then we have

O_t = AND(i_n, s_n) #

@kurniawano

Here is how I read this.

It depends on how we interpret "input" and "previous state".

For example, given a stack object o. Is o.push(i) a state machine?

def push(self,i):
    self.items + [i]
    return self

Actually self here can be treated as (1) the one of the inputs or as (2) the previous state.
If it is (1), this is how state is implemented in a pure functional programming language, i.e. a stateful computation is a function that takes the current state as part of the input and returns the next state as part of its output. e.g.

def push(stack,i):
    stack.items +[i]
    return stack

if it is (2) it is what we call state machine here.

Regards,
Kenny Lu

@Jiang-Wenchao , thanks for raising this. @luzhuomi is right in the sense that for object's method it depends on how we define it. self actually contains the state of the object so since Python always insert it into the first argument, we can see it as an input also to the method. The only thing is that when the method modifies the self object, it will have side effect. That's why in our (or MIT's) design, our get_next_values() must not modify self. Rather any state change should be returned as one of the output.

For the AND gate, however, the first (n-1)th input is not the "state" of the AND gate. At least this assumes when there is no cycle. If there is a cycle, where the output of the AND gate can be connected to the input of the AND gate (through some delay or register) then it's a different story.

Maybe what I can do is to clarify that the context of this AND gate is when there is no cycle.