Take the 2-minute tour ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

Functional programming employs persistent data structures and immutable objects. My question is why is it crucial to have such data structures here? I want to understand at a low level what would happen if the data structure is not persistent? Would the program crash more often?

share|improve this question
    
there is a pretty good extended discussion of this in abelson & sussman, structure and interpretation of computer programs –  vzn Feb 21 '13 at 16:46
add comment

3 Answers

up vote 6 down vote accepted

When you work with immutable data objects, functions have the property that every time you call them with the same inputs, they produce the same outputs. This makes it easier to conceptualize computations and get them right. It also makes them easier to test.

That is just a start. Since mathematics has long worked with functions, there are plenty of reasoning techniques that we can borrow from mathematics, and use them for rigorous reasoning about programs. The most important advantage from my point of view is that the type systems for functional programs are well-developed. So, if you make a mistake somewhere, the chances are very high that it will show up as a type mismatch. So, typed functional programs tend to be a lot more reliable than imperative programs.

When you work with mutable data objects, in contrast, you first have the cognitive load of remembering and managing the multiple states that the object goes through during a computation. You have to take care to do things in the right order, making sure that all the properties you need for a particular step are satisfied at that point. It is easy to make mistakes, and the type systems are not powerful enough to catch those mistakes.

Mathematics never worked with mutable data objects. So, there are no reasoning techniques we can borrow from them. There are plenty of our own techniques developed in Computer Science, especially Floyd-Hoare Logic. However, these are more challenging to master than standard mathematical techniques, most students can't handle them, and so they rarely get taught.

For a quick overview of how the two paradigms differ, you might consult the first few handouts of my lecture notes on Principles of Programming Languages.

share|improve this answer
    
This makes a lot of sense to me. Thanks for sharing your PPTs. Do you share video recordings of the same as well? –  gpuguy Feb 21 '13 at 9:50
    
@gpuguy. I don't use powerpoint all that much. Whiteboard is my favourite medium. But the handouts should be quite readable by themselves. –  Uday Reddy Feb 21 '13 at 10:07
    
+1 Mathematics never worked with mutable data objects. Also the link to your lecture notes. –  Guy Coder Feb 21 '13 at 11:40
add comment

It is easier to correctly work with persistent data structures than it is to work with mutable data structures. This, I would say, is the main advantage.

Of course, theoretically speaking, anything we do with persistent data structures we can also do with mutable ones, and vice versa. In many cases persitent data structures incure extra costs, usually because parts of them have to be copied. These considerations would have made persistent data structures much less attractive 30 years ago when supercomputers had less memory than your mobile phone. But nowadays the main bottlenecks in production of software seem to be development time and maintainance costs. Thus we are willing to sacrifice some efficiency in execution for efficiency in development.

Why is it easer to use persistent data structures? Because humans are really bad at tracking aliasing and other kinds of unexpected interactions between different parts of a program. They automatically think that because two things are called x and y, then have nothing in commmon. Afer all, it takes effort to figure out that "the morning star" and "the evening star" are really the same thing. Similarly, it is very easy to forget that a data structure may change because other threads are working with it, or because we called a method which happens to change the data structure, etc. Many of these concerns are just not present when we work with persistent data structures.

Persistent data structures also have other, technical advantages. It is typically easier to optimize them. For example, you're always free to copy a persistent data structure onto some other node in your cloud if you wish, there is no worry of synchronization.

share|improve this answer
    
when it has so many advantages then why not use persistent data structure in imperative languages as well? –  gpuguy Feb 21 '13 at 9:43
3  
Perhaps soon you will ask "Why use imperative languages?" –  Andrej Bauer Feb 21 '13 at 13:47
3  
But seriously, there are datastructures which are hard to replace with persistent ones, for example number-crunching programs which uses arrays and matrices are much faster with traditional data structures because hardware is optimized for that sort of thing. –  Andrej Bauer Feb 21 '13 at 13:48
    
@gpuguy. Persistent data structures can be, and should be, used in imperative languages as well, whenever they are applicable and suitable. To be able to use them, the language should support garbage collection-based memory management. Many modern languages have that: Java, C#, Scala, Python, Ruby, Javascript etc. –  Uday Reddy Feb 22 '13 at 10:46
    
Arguably, one big advantage is the more abstract interface compared to mutable datastructures. You can change stuff under the hood (cf immutability vs refential integrity) but don't have to. –  Raphael Feb 24 '13 at 17:53
add comment

Adding to others' answers, and reinforcing a mathematical approach, functional programming also has a nice synergy with Relational Algebra, and Galois Connections.

This is extremely useful in the area of Formal Methods.
For instance:

  • Formal proofs in program verification are simplified with Extended Static Checking;
  • A number of properties from Relational Algebra are useful in SAT solving, with tools such as Alloy;
  • Galois Connections allow a calculational approach to software specification, as seen in this blog, with a reference to a paper, by Shin-Cheng Mu and José Nuno Oliveira.
  • Galois Connections (and Functional Programming) can be used in a Design by Contract fashion, since they are a more general concept than Hoare Logic.

Example

The Hoare triple $\{p\} P \{q\}$ can be expressed as the contract $[P] \cdot \phi_p \subseteq \phi_q \cdot [P]$, where

  • $[P]$ is the relation that denotes the semantics of program $P$;
  • $\phi_p$ (resp. $\phi_q)$ denotes the coreflexive relation that captures predicate $p$ (resp. $q$);

This approach also allows weakest pre-condition and strongest post-condition calculation, which comes in handy in a number of situations.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.