开发者

Simulating side-effects with threads

开发者 https://www.devze.com 2023-01-06 12:58 出处:网络
I was watching this Channel 9 talk and a very interesting statement is made in the around the 60 minutes in.

I was watching this Channel 9 talk and a very interesting statement is made in the around the 60 minutes in.

He said that even with completely pure functions once you introduce threads you c开发者_如何学Can simulate side effects.

The way he outlined this was using C-omega notation, which I'm not familiar with, and this already has the relevant language support to do this.

The problem I have is how do you implement the get and set message handlers without assignments? Surely the handlers would have to change a value so that the thread's internal loop can record a new value?

What am I missing? How would I implement that in LISP, for example, that has no special constructs?


The problem I have is how do you implement the get and set message handlers without assignments? Surely the handlers would have to change a value so that the thread's internal loop can record a new value?

Yes you are correct. Erik is assuming that each thread has its own message queue. Sending a message with Value(n) clearly changes the state of the message queue. He makes it sound more mysterious than it is: he is simply trading assignments for another form of mutable state. He is taking advantage of the 'precondition' feature of the language to make it syntactically clean.

As he says, you can use conventional IO (he mentions console IO) to do the same thing. Imagine Value(n) writes n to a file and the precondition Value(T t) checks whether the current value of the state is t (in the file). Voilà, mutable state without assignments (but IO is involved now!).

0

精彩评论

暂无评论...
验证码 换一张
取 消