Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of single assignment?

I'm currently trying to master Erlang. It's the first functional programming language that I look into and I noticed that in Erlang, each assignments that you do is a single assignment. And apparently, not just in Erlang, but in many other functional programming languages, assignments are done through single assignment.

I'm really confused about why they made it like that. What exactly is the purpose of single assignment? What benefits can we get from it?

like image 495
Kemal Fadillah Avatar asked Jun 29 '12 03:06

Kemal Fadillah


2 Answers

Immutability (what you call single assignment), simplifies a lot of things because it takes out the "time" variable from your programs.

For example, in mathematics if you say

  x = y

You can replace x for y, everywhere. In operational programming languages you can't ensure that this equality holds: there is a "time" (state) associated with each line of code. This time state also leaves the door open to undesired side effects which is the enemy number one of modularity and concurrency.

For more information see this.

like image 137
Diego Avatar answered Oct 09 '22 23:10

Diego


Because of Single Assignment, Side effects are so minimal. Infact, its so hard to write code with race conditions or any side effects in Erlang. This is because, the Compiler easilly tells un-used variables, created terms which are not used, shadowed variables (especially inside funs ) e.t.c.

Another advantage that Erlang gained in this is Referential Transparency. A function in Erlang will depend only on the variables passed to it and NOT on global variables, except MACROS (and macros cannot be changed at run-time, they are constants.).

Lastly, if you watched the Erlang Movie, the Sophisticated Error Detection Mechanism which was built into Erlang depends so much on the fact that in Erlang, variables are assigned Once.

like image 45
Muzaaya Joshua Avatar answered Oct 10 '22 00:10

Muzaaya Joshua