News for hackers. Stuff that matters.
Re:Imperative Haskell by cousin_it on Wednesday May 31, 2017 @03:32AM (#14450939)
Purity is tricky to define. For example (stolen from Reddit user gasche), if you can measure how much time a computation takes, then Haskell is impure, because a lazy value takes longer to compute the first time than the second. You could try to patch it up by saying the impurity must be observable via pure code, but that makes the definition circular. And that raises another problem, where printing to standard output becomes "pure" if the standard output can't be observed by pure code (which is true in Haskell).

Another problem with seeing purity as fundamental is that our physical reality isn't a persistent data structure (past states aren't accessible), so the best algorithms possible won't treat it as one. Among the tons of papers describing fast algorithms, practically none are using purity, unless they required purity to begin with.

And there's a third problem, specific to Haskell, that might interest you. The article is using ST to implement an impure computation. You'd think that ST itself can be implemented in pure Haskell, maybe with the usual logarithmic slowdown. But unfortunately no one knows how to do it, and there's a strong suspicion that the (pure) type signature of runST has no pure implementation at all, even with quadratic or exponential slowdowns. The reason is tricky to explain, but it's well covered on StackOverflow and Reddit.

(That's not even going into the issues of quicksort. Suffice to say that a pure quicksort is hard to write, ST or no. The problem is that you need good pivot selection to prevent the quadratic worst case, but randomized pivot selection will lead to observable impurity in the output, because quicksort isn't stable. So you must use something like median of medians, making the algorithm much slower and more complicated.)

Re:Imperative Haskell by lmm on Wednesday May 31, 2017 @04:46AM (#14451133)
A pure language is one in which replacing any subexpression of any expression with the evaluation of that subexpression yields an equivalent expression.

Of course this means "pure" is not an absolute term but rather relative to a given definition of "equivalent". But this isn't circular and is practically useful: if you're working in a context where precise execution time matters (e.g. cryptography) you really do need to use a different concept of "pure" language from what you would use in a more "normal" context where two programs that produce the same output are equivalent even if they take a different amount of time to execute.

Re:Imperative Haskell by skybrian on Wednesday May 31, 2017 @11:48AM (#14453423)
Performance matters for any task that has a deadline, not just crypto and real-time stuff. That's why we make performance improvements, after all.

I think it might be better to think of purity as a way of ensuring that performance and logical correctness are independent effects. Purity allows us to safely improve performance by making local substitutions of faster but logically equivalent code, without having to reason globally about correctness. It's also what allows us to tolerate variation in performance (within reason) without threatening correctness.

For crypto we can reason separately about logical effects (does the crypto work) versus information leakage. For a user interface, we can treat dropped frames as a performance problem rather than a correctness problem.

This is useful even though we still care about performance. Substituting a much slower function for a faster one probably isn't okay, but purity lets us understand the effect.

Re:Imperative Haskell by lmm on Wednesday May 31, 2017 @12:04PM (#14453539)
Performance matters a little bit - most of the time we don't make perfomance improvements. Really in a context where performance was of serious importance I would want to have an explicit model of it to be sure I could reason about it compositionally.

I find any single effect is easy to reason about in isolation, it's the interaction that gets tricky. Viewing purity as isolating performance is just one perspective on this - you can equally view it as isolating state mutation, or isolating async transitions.

Re:Imperative Haskell by skybrian on Wednesday May 31, 2017 @04:18PM (#14455568)
Yes, compositional reasoning is important, but it depends on being able to substitute equivalent code. If you're too strict about what counts as equivalent (including performance in the type system, say) it would be harder to make local changes since fewer substitutions would be possible.

You can look at const correctness in C++, Java checked exceptions, async versus non-async functions [1], and Rust's ownership model as other examples where making fine distinctions for good reason has a side effect of making substitution harder - or perhaps safer, depending on your point of view.

If you need it, you need it, but when you don't, it's a luxury to be able to substitute whatever you like and see what happens.

[1] http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...

Re:Imperative Haskell by lmm on Monday June 05, 2017 @07:08AM (#14486682)
> If you're too strict about what counts as equivalent (including performance in the type system, say) it would be harder to make local changes since fewer substitutions would be possible.

I think this is avoidable if your types/effects can decompose properly into orthogonal factors. E.g. with a Future type and a generic monad abstraction it's fairly easy to substitute a sync call for an async call - you have to be explicit about the change you're making, but only very slightly, and you'd never get confused between changing sync/async and changing the "actual" type that's returned.

Re:Imperative Haskell by cousin_it on Wednesday May 31, 2017 @12:03PM (#14453532)
Great point, I hadn't realized that purity must be performance-agnostic to make optimizations valid.
Re:Imperative Haskell by cousin_it on Wednesday May 31, 2017 @05:37AM (#14451300)
I think this is a good take. Is there a natural definition of equivalence that includes standard output effects but excludes time and space effects, short of enumerating all effects and saying which ones are ok?
Re:Imperative Haskell by lmm on Wednesday May 31, 2017 @08:46AM (#14452024)
From a denotational/lambda-calculus perspective it's very natural to consider all expressions that reduce to the same expression as equivalent, at which point you have that definition of equivalence. Of course in the pure lambda calculus all "programs" are just (equivalent to) values. To get any effects like input/output you have to add extra values, and at that point you decide what the equivalence semantics of these "special" values are. That's kind of the Haskell perspective, and where I think it falls down is in the interaction between sequenced I/O and pure functions; the pure subset of Haskell can be fully evaluated at compile time (if you work in e.g. Idris this becomes true in a very direct sense: your (total) pure functions can be used in types), but as soon as a value depends on user input that obviously ceases to be the case, whereas expect to be able to decompose their program into pure and impure fragments and reason about these separately.

From an operational perspective a good definition of equivalence would be very difficult. E.g. if your language allows making system calls there are dozens or hundreds of those with no real model of how they interact, so it's very hard to say when one sequence of system calls is equivalent to another. Possibly the most natural notion of operational equivalence is to say two programs are equivalent if they execute the same sequence of CPU instructions, but that would mean that nontrivial programs were virtually never equvialent to each other, even if they both just printed the same string.

Re:Imperative Haskell by cstrahan on Wednesday May 31, 2017 @07:51AM (#14451738)
Maybe something like this:

Equivalence: unification of types (can't make I'll-typed substitutions)

Output only effects: have your language define only one such monad

About to depart on a flight, so can't elaborate further.

Re:Imperative Haskell by chrisdone on Wednesday May 31, 2017 @05:21AM (#14451243)
> Purity is tricky to define.

I think it's pretty easy.

Purely functional (it's important to retain the word "function") means that we have a language in which the value of a function application "f x" depends only on the body expression of "f" and the function argument "x". That's why we say "purely" functional, because it's just functions of this flavor.

In types, f : X -> Y means that for all terms in X, f x maps to a unique term in Y. In Haskell's, Elm's and PureScript's case, it's weakly normalizing (meaning it might get in an infinite loop). In Agda's case, it's strongly normalizing (modulo implementation bugs).

> if you can measure how much time a computation takes, then Haskell is impure, because a lazy value takes longer to compute the first time than the second

Time is an operational concern. "Purely functional" is concerned with what things mean, what do syntactic expressions in the language denote? If 1+1 launches missiles every time it's evaluated, or causes the machine to get hotter by 1°C, that's an implementation detail that is not observable in the language. 1+1 normalizes to 2. You can do all your regular equational reasoning and follow the substitution model. You might chide the writer of the interpreter or compiler, though.

For example, here is a substitution stepper for a dialect of Haskell http://chrisdone.com/toys/duet-gamma/, and each line in the bottom left is equivalent to the line after it. You can pick any of those lines as your starting point.

> And that raises another problem, where printing to standard output becomes "pure" if the standard output can't be observed by pure code (which is true in Haskell).

It doesn't "become" pure. You're making a category error by calling an implementation detail by names that we reserve for expressions in a language. We should use "observe" in a precise way: to observe means to write a case analysis on something.

> You could try to patch it up by saying the impurity must be observable via pure code, but that makes the definition circular.

Where exactly is the circular part?

Re:Imperative Haskell by ridiculous_fish on Wednesday May 31, 2017 @06:33AM (#14451469)
Is a function like trace pure in your definition? trace's side effects seem to fall into the "unobservable implementation detail" category next to the missile launches.
Re:Imperative Haskell by skybrian on Wednesday May 31, 2017 @12:13PM (#14453624)
It's not observable to the calling function (the return value didn't change) so it doesn't count.

Perhaps a way to think about it is as a way defining the contract between a library function and its callers regarding what substitutions are permissible. For example, substituting a faster algorithm is likely observable to the end user, but the calling function will continue to return the same value so it's a compatible substitution.

Similarly, turning on compiler optimizations or debug logging should not be a correctness issue, though it certainly matters or we wouldn't do it.

Re:Imperative Haskell by mietek on Wednesday May 31, 2017 @07:49AM (#14451734)
> You'd think that ST itself can be implemented in pure Haskell, maybe with the usual logarithmic slowdown. But unfortunately no one knows how to do it, and there's a strong suspicion that the (pure) type signature of runST has no pure implementation at all ...

This information may be out of date. There are now at least two Agda formalisations of runST (by Andrea Vezzosi and András Kovács), and a paper by Timany et al. on the subject of runST has been submitted to ICFP'17.

http://code.haskell.org/~Saizan/ST/ST.agda

http://gist.github.com/AndrasKovacs/07310be00e2a1bb9e94d7c8d...

http://iris-project.org/pdfs/2017-icfp-runST-submission.pdf

Re:Imperative Haskell by cousin_it on Wednesday May 31, 2017 @08:14AM (#14451841)
AFAIK the Agda implementations use a closed universe of types and cannot be translated to an implementation of runST in Haskell which doesn't have that restriction. Please correct me if I'm wrong. From a quick reading I can't tell if the new paper makes progress on that front, what do you think?
Re:Imperative Haskell by smilliken on Wednesday May 31, 2017 @04:22AM (#14451062)
> if you can measure how much time a computation takes, then Haskell is impure

gettimeofday requires IO, so the computation would depend on dynamic inputs, but that doesn't make it impure. If the program knows how long one of its computations took, that's only because an oracle (outer layer of abstraction) told it.

> Another problem with seeing purity as fundamental is that our physical reality isn't a persistent data structure

I like this line of reasoning, but would take the opposite conclusion. The universe is symmetric with respect to time, and indeed, information is conserved. Past states aren't easily accessible, but they have not been erased. This sounds like a fine working definition for persistent data structures.

> there's a strong suspicion that the (pure) type signature of runST has no pure implementation

Interesting, thanks for bringing it up!

Re:Imperative Haskell by cousin_it on Wednesday May 31, 2017 @04:41AM (#14451116)
> The universe is symmetric with respect to time, and indeed, information is conserved. Past states aren't easily accessible, but they have not been erased. This sounds like a fine working definition for persistent data structures.

That line of research exists, but it's different from persistent data structures. The search term is "reversible computing", and the main idea is that you never erase bits because that would irreversibly increase entropy. One difference is that persistent data structures allow access to past states in O(1), while reversible computing requires you to spend time on uncomputing. Right now it's mostly a curiosity because it requires hardware that doesn't exist, so I'd be wary of using it as a foundation of CS. It might well become important in the future though, due to lower energy requirements.

Re:Imperative Haskell by enqk on Wednesday May 31, 2017 @04:54AM (#14451162)
> The universe is symmetric with respect to time, and indeed, information is conserved.

Bringing a snowflake back to its previous shape after it has melted?

Re:Imperative Haskell by empath75 on Wednesday May 31, 2017 @09:04AM (#14452122)
Congrats, you've discovered entropy. The reason time seems to flow in one direction is only because there was an extremely low entropy state at the beginning.
Re:Imperative Haskell by AnimalMuppet on Wednesday May 31, 2017 @12:30PM (#14453779)
Yes, but entropy means that the universe is not symmetric with respect to time, doesn't it? That is, enqk's statement is in fact a refutation of smilliken's claim. Dismissing it with "Congrats, you've discovered entropy" doesn't answer it at all.
Re:Imperative Haskell by sidek on Wednesday May 31, 2017 @01:16PM (#14454184)
Entropy is not due to the equations of our universe, but rather the initial conditions. So the asymmetry might be apparent but the equations might still be symmetric. Related is the idea of spontaneous symmetry breaking.

Of course, we don't have time symmetry in the equations anyway because of the weak force. But because the weak force is weak//doesn't matter much for the physics of many systems, we can often write the equations of physics as a time-symmetric term which essentially decides the motion plus a very small time-asymmetric term. So we can deal with the small term using techniques like perturbation theory, and use time symmetry for the rest.

Re:Imperative Haskell by AnimalMuppet on Wednesday May 31, 2017 @02:07PM (#14454640)
> Of course, we don't have time symmetry in the equations anyway because of the weak force.

Could you expand on what you mean here? I've expected for a while that there was going to be something non-time-reversal-symmetric with the weak force, on the basis of parity violation (space-reversal doesn't give you the same equations) plus relativity (space and time are the same thing, at least kind of). But getting there directly from parity violation might require a faster-than-light frame of reference to observe it from, which is... let's just say it's experimentally difficult.

How does the weak force break time symmetry?

Re:Imperative Haskell by sidek on Wednesday May 31, 2017 @06:28PM (#14456439)
The answer, as you point out, is that CP violation implies T violation. Experimentally testing T violation is much, much harder, and I don't think has really been done in many systems (look at Fitch and Cronin's work for an example), but we know CP violation implies it. So it's there. Or at least, to our best knowledge, it's there -- T violation is not very well understood.
Re:Imperative Haskell by quadrupleslap on Wednesday May 31, 2017 @06:15AM (#14451418)
I think the idea is that with perfect knowledge of the current state of the universe, you could deduce that yes, that water was once a snowflake of some known configuration.
Re:Imperative Haskell by mac01021 on Wednesday May 31, 2017 @09:14AM (#14452194)
Is there any evidence that this is true?
Re:Imperative Haskell by jerf on Wednesday May 31, 2017 @09:50AM (#14452460)
Even if it is true in theory, it is not practically true, as the information required to reverse such a transform rapidly exceeds what you can collect even in theory in a single coherent system. While this is by no means the end of your problems, one simple question you can ask yourself is how exactly you intend to re-collect the photons that the snowflake emitted while melting as they head off to the ends of the universe at light speed?

If the universe is externally a pure value, it doesn't matter to us on the inside. The universe is impure from our point of view.

Also, a pure data structure doesn't just imply that you have a puddle of water that you can put back together into a snowflake; it practically (that is, in practice, not in the colloquial sense of the term) implies that you have both at the same time. When I'm working with pure data structures I don't have to laboriously translate back and forth, I have them both in hand. This is partially because pure data structures simply have no concept of time at all in them. And you can't save this argument by claiming you can transform them back and forth at will, because as I mentioned, no, you can not. Common sense can be clad with solid mathematical arguments here; you can not, in our real universe, ever do that, so even the superficially appealing theoretical answer must give way to a more sophisticated and correct theoretical analysis in which in fact you can't reverse arbitrary transforms in practice. (You can do it at a small scale for small numbers of qubits. You can do it for a constrained number of qubits specially set up and isolated for just such an occasion, aka "quantum computer".... and note how hard even that is, we've still not managed to isolate very many qubits at a time that way! But you can not do it in general.)

Whether an entity external to our universe could do it is something you'd have to take up with them.

Re:Imperative Haskell by pdpi on Wednesday May 31, 2017 @06:34AM (#14451473)
sure. As long as time is symmetrical, and advancing time by -t cancels out advancing time by t.
Re:Imperative Haskell by charlieflowers on Wednesday May 31, 2017 @01:16PM (#14454182)
> Another problem with seeing purity as fundamental is that our physical reality isn't a persistent data structure (past states aren't accessible), so the best algorithms possible won't treat it as one. Among the tons of papers describing fast algorithms, practically none are using purity, unless they required purity to begin with.

Purity is a simplification of the real world, in the same way that the abstract ideal of a circle is a simplification of objects in the real world.

But that simplification yields great benefits (kind of in the same way that ideal geometric shapes allow mathematical reasoning). It seems to describe the simplifications that the real world "approaches" (like a mathematical limit).

So it does seem fundamental. But yet not "real." I don't think those are contradictory.

It makes intuitive sense that non-purity could open up algorithmic possibilities, some of which would be faster. But that doesn't really touch on whether purity is "fundamental" in the sense described above.

Re:Imperative Haskell by platz on Wednesday May 31, 2017 @02:08PM (#14454658)
> randomized pivot selection

I don't see why this is an issue when pseudo-random generators are in fact deterministic yet for purposes of pivot selection the seed does not need to be fresh on each invocation barring an adversarial context, in which you'd just pass in the seed from the effectful part of the program.