Hacker News new | ask | show | jobs
by ChrisSD 2989 days ago
I agree with the thoughts on the = sign but I'm not so sure about mutations.

> If mutation is so great, why do mathematicians use recursion so much? Huh? Huh?

> Well, I’ve got two counterpoints. The first is that the goal here is to reason about the sequence, not to describe it in a way that can be efficiently carried out by a computer.

Most high level languages try to avoid making the programmer describe the most efficient way to handle variables. The idea is to describe your algorithms and how they connect and allow the compiler (or interpreter) to figure out how to use registers etc to implement it. Of course that ideal breaks down sometimes but most high level programmers don't normally need to stress the low level details too much.

> My second point is that mathematical notation is so flexible and adaptable that it doesn’t need mutation the same way programming languages need it. In mathematics we have no stack overflows, no register limits or page swaps, no limitations on variable names or memory allocation, our brains do the continuation passing for us, and we can rewrite history ad hoc and pile on abstractions as needed to achieve a particular goal.

It's true that there's a limit to abstractions even the highest level languages can make if they want to remain general purpose. However I think languages can handle immutable variables as a default.

That's not to say I agree that programming should always follow mathematical notation. But I also don't think it's a bad ideal in many cases.

3 comments

The thing that blew my mind was that there are some mathematical programming languages where the point is not to ever actually run the program. Just type-checking it is enough to prove the result. (In the "programs are proofs" sense.) In these languages, it's important not to allow infinite loops because you will never test the code.

Even though there's a correspondence, there's always going to be a difference between writing a program so that you can actually run it and writing proofs, where you don't, and ridiculously inefficient algorithms don't matter at all, so long as they don't diverge.

https://aphyr.com/posts/342-typing-the-technical-interview

>"You… do realize that the type system is meant to constrain values, right?”

>“No,” you inform him, matter-of-factly. “No, that doesn’t sound right.”

> To achieve the same result with recursion requires a whole other can of worms: memoization and tail recursive style and compiler optimizations to shed stack frames. It’s a lot more work to understand all that (to get to an equivalent solution) than it is to understand mutation!

It's true, you can't rely on tail call optimizations on every language. But practically all modern compilers for imperative languages transform your code into SSA form[1] in one of their intermediate stages, so the code:

  int x = 1
  x = x + 1
will invariably be transformed into:

  int x0 = 1
  int x1 = x0 + 1
in one of the intermediate stages - before applying further optimizations and finally converting it to stack-based bytecode or register-based machine code.

This means that mutating a variable, at least when dealing with local primitive values, is equivalent to assigning the value to a new variable.

Even in other cases, in-place mutation is not necessarily more efficient than copying and modifying values. For multi-threaded code, mutation often requires relying on synchronization which can be more expensive than copying around data.

> Simply stated, the goals of mathematics and programming are quite differently aligned. The former is about understanding a thing, and the latter is more often about describing a concrete process under threat of limited resources.

In the current day and age, if a programmer would wants to write the most efficient code, they need to understand a lot about their multi-stage optimizing compiler, Out-of-order CPUs, OS threads and so on - and they would still need to benchmark their implementation against others. It is not clear anymore that mutation is always faster.

I think that in 99% of the cases, clear, safe and maintainable code trump the micro-optimizations that may (and often may not) be gained from using mutable data. Immutable data is generally easier to reason about, always safer from data races and other bugs, and - in most languages languages - more maintainable.

The age of limited resources and straightforward compilers is long over, but it left mutable variables as its heritage. I'd argue they're still common not because they are necessary to deal with hardware limitations, but just because generations of programmers have gotten so used to them, it's hard to give up on them.

[1] https://en.wikipedia.org/wiki/Static_single_assignment_form

>> If mutation is so great, why do mathematicians use recursion so much?

> Huh? Huh?

The deal here is surely that induction and other recursive approaches are conducive to being reasoned about in traditional mathematical contexts (e.g. taking a walk). Mutation is impossible to keep track of, mentally. Though others' mileage will vary on that.

For the record the "Huh? Huh?" is a quote from the article.
OK. Downvotes happily accepted; but I think we all operate in "peephole" mode now; the cost of tracing sources back to the original is significant.

There is no karma to be had from complaining about HN's software, and yet it is so very inadequate.

That it was a quote from the article seemed clear in the post in which the quote was presented, so while I agree that it can be difficult to clearly present quotes in some situations on HN and a good blockquote formatting facility would be preferable, I don't think that's really a problem here.
As I read the comments on this post, the top reply contains multiple mathematical symbols which aren't rendering on my recent Android phone. You can't pretend that HN has a huge proactive team working on these issues.

I'm loathe to infer that you think that the problem is "me" because I think the underlying question is whether the comments should be a friendly place for people who do not have time or inclination to read the article. You may see that as an appalling, lazy, degeneration in discourse; the reality is that reading the comments without wading through a blog post is a valid tactic. If clearer methods of quoting were available the two or three of us involved here would have wasted less time.

The "engineer-forward" alternative in which everyone strives to speak from a totalizing position of authority is just, frankly (as a technical person myself) unattainable.

Just to be clear, I am literally saying that IMHO it should be OK to converse in the comments without reading the link in question. If HN can't accommodate this, it's not a friendly platform for humans.