by theseoafs · 11y ago · 0 comments
Because in the computer, things need to mutate, and so a language that doesn't mutate is therefore far from the computer.
EDIT: Put another way: Haskell does support mutation, but inside of particular monads. So obviously mutation is useful. Learn why it's useful in Haskell and you'll see why it's useful elsewhere.
For example, in OCaml you have the notion of "functional record updates". Basically you update structs by getting a whole copy back with the modified fields changed accordingly. Under the hood though, what you get is just the changes and the other fields point to the original struct.
Basically there is a difference between what your code says and what the compiler outputs, but in the end the result is the same, the compiler is just smart enough to rewrite it for you. You're right that it's further from the computer, but only for what you type in your editor. I highly recommend the book Functional Data Structures (it's on Amazon) if these sort of brilliant compiler optimizations interest you.
The beauty of pure FP is that from your code's point of view there is no mutation. The only functional language you mentioned in your post is OCaml, which is impure and supports ad-hoc mutation, like every other language in the ML family, and like every other functional language except for a select few like Haskell and Miranda and their derivatives.
Moreover Haskell supports other kinds of mutation besides fusion and tail-call elimination; it just does it behind a pure interface. See, for example, http://hackage.haskell.org/package/array-0.1.0.0/docs/Data-A... .
So we're back at the same place: obviously mutation is useful since even Haskell goes out of its way to make sure you have access to it.
Now, OO tries to do the same kind of thing - hiding things behind an abstraction that you don't have to think about from outside the abstraction. The problem is, all the abstractions leak. Does Haskell's hiding mutation behind a purely functional interface leak? I don't know enough to say, but I think it's a critical question.
I hear this argument a lot but I don't buy it. You could also say
"In the computer, things are highly asynchronous quantum-level electrical fluctuations, and so a language that doesn't model highly asynchronous quantum-level electrical fluctuations is therefore far from the computer."
which is clearly invalid. Some explanation is needed of why the argument for mutation holds, but for quantum fluctuations does not hold.
When you code, you're building on top of assembly / cpu registers and instructions. The fact that X86/64 runs on a bunch of electrical fluctuations is actually inconsequential and an implementation detail.
Any x86/64 processor built of transistors naturally needs to take into account the behavior of the transistors it's made up of. But once you've gone above the micro-code/processor level, those details are fully abstracted.
Regardless what programming language you use, that is abstracted away identically and matters not.
However, the fact that registers in the process and memory locations exist and mutate is not something abstracted away from every language. It's abstracted in some, but not others, so it's valid to say that this difference is within the scope of the programming language.
That is why the argument differs. If the above isn't clear, I'd be happy to give another go at explaining it.
You could write a language which expressed manipulations of the electrical fluctuations in the same way that DNA is a programming language which expresses manipulations of ambient signaling protein levels to operate our cellular computers.
Or, taken another way, GLSL is a language which shares a lot of similarity with "functional" languages (despite its C like syntax)---and it certainly doesn't run on an x86 architecture!
Too often I see people conflating "X is good" for "X is good for everything" when that is so rarely, if ever, true.
There are plenty of better examples to make this point with- named variables and functions, control structures, etc. And these make it clear that the argument is really about the importance of this particular abstraction: where and when memory is used for which values.
> Haskell does support mutation, but inside of particular monads.
You are mistaken. Monads do not involve mutation; that would defeat their entire purpose. Although it's true that the monadic code is being compiled into assembly that involves mutation, just as $LANGUAGE code is being compiled into assembly that includes a lot of goto.
Some monads, IO, ST, State, do support mutation in the internal language. Then their implementation, their representation in the meta language, Haskell, can be "pure or whatever who cares".
http://www.reddit.com/r/haskell/comments/30l46z/haskell_for_all_algebraic_side_effects/cptlduvIndeed. IO and ST involve mutation. They just so happen to be instances of Monad, but that's tangential.
In reality they're implemented in a mutation-heavy way for performaance reasons, but on the language level absolutely no mutation happens ( it's the whole World -> (Result, World) analogy)
putStrLn "What is your name?"
>> getLine >>=
\name -> putStrLn ("Hi, " ++ name ++ "!")
It's just a giant chain of instructions, each previous result feeding into the next. Nowhere do we assign anything, modify anything, etc. No mutation.