202 points · by quickthrower2 · 6y ago · 117 comments
There's a saying in mathematics that when any field matures it turns into algebra. Life crossed a threshold from chemistry to biology on Earth, and developed exponentially from there. Order in mathematics crosses a similar threshold, as it becomes sufficiently structured to support algebraic reasoning. The subjective experience is like ice melting into a churning liquid, or a land-locked creature learning to fly. Once one has this experience, one cannot imagine thinking any other way. In the case of functional programming, programs written in other languages feel like ad hoc pre-civilization constructions, doing arithmetic by counting pebbles.
Advocates of Haskell don't tend to express this, because from the outside it can come off like trolling, but this algebraic sense of wonder is at the core of many Haskeller's experiences. We all have the example of Lisp in our minds, its "we found God" advocacy did much to hinder its adoption. Nevertheless, understanding this explains much about Haskell. The real point of lazy evaluation is that it best supports this algebraic reasoning, as carbon best supports life. The 47,000 compiler options reflect a primary goal of being a research bed for language constructs derived from mathematical category theory, despite its success as a practical language for those free to choose it.
The killer app for Haskell is parallelism. To this day it has the best implementation of parallelism; one can achieve a 7x speedup on 8 cores by adding a handful of lines to a program. This ease is a consequence of the functional model, and of considerable effort by Haskell's developers.
Idris 2 is itself a joy to learn, if one wants a smaller, cleaner Haskell without the 47,000 compiler options. One gets to learn dependent types. Alas, it doesn't offer commercial-grade parallelism.
It's funny, because I think this metaphor cuts exactly the opposite direction -- code reaching sufficient complexity, the pure math approach doesn't survive an encounter with the real world.
I love static analysis and think that there are core business domains that are best expressed with a rich type system, but there is a whole world of absolutely critical programming that cannot simply eliminate messiness, due to interfacing with humans and our flawed practices.
With sufficient time and an expressive enough type system, even the messiest business process can be represented, but I'm not convinced that's an example of beauty but rather stubbornness.
I understand what you're saying, but I don't think you'll find many programmers that will argue that a programmer shouldn't use the best tool for the job.
Booch: That’s a problem you wrestled with for literally years.
Backus: Yeah, and unsuccessfully.
http://archive.computerhistory.org/resources/access/text/201...
Functional Reactive Programming seems promising. (Elm ditched it in favor of some simpler metaphor on didactic rather than technical grounds, I think.)
> Idris 2 is itself a joy to learn, if one wants a smaller, cleaner Haskell without the 47,000 compiler options.
From a conceptual point of view, Idris makes Haskell look similarly crude.
Eh, you can achieve the same in Rust using rayon, this isn't exclusive to Haskell or FP. Do you have an example in Haskell, where you get an effortless speedup which wouldn't be easy in another language?
> let colors = fmap computeColor coordinates
to
> let colors = fmap computeColor coordinates `using` parListChunk 32 rseq
So half a line added to parallelize the mapping a pure computation including using a sensible threadpool implementation. How impressive you find this depends on how you define "easy in another language". Rayon comes pretty close but you can encode some properties about your computation in the Haskell type system that Rust does not yet support and so the safety of some computations can be statically proven in Haskell but not in Rust.
var colours = coordinates.stream().map(this::computeColour).collect(toList());
var colours = coordinates.stream().parallel().map(this::computeColour).collect(toList());
Java doesn't do as much (well, anything) to help you make sure the implementation of computeColour is suitable for parallelisation, but that comes with the territory. On the other hand, it will automatically split the stream up appropriately for the number of cores you have.Also, succinct expression of parallel algorithms isn't a particularly unique feature. Like half of the named algorithms in the C++ standard library can be parallelized by adding a single parameter.
Hi, this seems wrong to me.
First, as far as I can tell, such a saying does not exist. I've never heard it, despite having a fair amount of experience with mathematics (though not as much as you), and I can't find it through Googling.
Second, and more substantively, it is not the case that mathematical fields inevitably turn into algebra. One only has to look at PDE, probability, and analytic number theory to see this is the case (to give just a few examples). All are highly mature fields and essentially non-algebraic. This is not to say that ideas from algebra are not occasionally useful, just that it should be obvious to anyone who opens an introductory graduate text in PDE that the subject has not "turn[ed] into algebra."
> The killer app for Haskell is parallelism. To this day it has the best implementation of parallelism; one can achieve a 7x speedup on 8 cores by adding a handful of lines to a program. This ease is a consequence of the functional model, and of considerable effort by Haskell's developers.
I don't think this statement is entirely wrong, but I don't think it's entirely right either. There are plenty of languages where one can get parallelism with just a few extra lines of code; Julia comes to mind immediately. Easy parallelism is hardly a Haskell-specific, or even FP-specific, feature. I would of course agree that designing things without mutable state makes writing parallel code easier, but this can be done in many high-level languages these days.
But that is a nitpick. A more substantive objection is that the sole purpose of parallelism is performance, and idiomatic Haskell is generally speaking slower, sometimes a lot slower, than the same algorithm written in C++ or similar languages. (The adjective idiomatic is important here – I'm aware that one can with enough contortions write C-like code in Haskell, but this response undercuts of the point of highlighting Haskell-specific features like immutability, laziness, etc.) In particular, a few years ago it seemed like the people implementing parallel Haskell features didn't understand or care about fundamental things like cache locality. Maybe this has changed?
There is also an amusing story about the "ease" of writing parallel quicksort in Haskell [1].
> The real point of lazy evaluation is that it best supports this algebraic reasoning, as carbon best supports life.
It's worth noting that Simon Peyton Jones is on record as saying that any "Haskell 2" should not be lazy [2].
[1] https://sudonull.com/post/75314-Parallel-quick-sort-on-Haske...
To expand on your main point: A loose, but appropriate analogy is that FP appeals to programmers in the way analytic solutions appeal to mathematicians and scientists: Neat, ideal, beautiful... but a poor fit for many practical problems.
That's the main issue which FP-for-parallel-execution proponents don't seem to get: Identifying independent (and therefore parallelisable) computations isn't the hard part, but planning the data layout and partitioning so that all your parallelism isn't eaten up by synchronization and data movement between cores.
Not to fully justify the OP (though it rings both true and not quite true to me), but I think topology is a good positive example. It's impressive how much of continuity can be captured purely algebraically. For example, topologies are a kind of lattice, which cross both algebra and order theory. And then you get literal algebraic topology, which captures useful and interesting properties of individual topological spaces effectively.
> A more substantive objection is that the sole purpose of parallelism is performance.
I disagree a lot more strongly here, though I will twist your words a little bit. Yes, parallelizing a program is about making it run faster; but parallelism is also a base fact of many problem domains, where you have multiple agents (up to and including humans) collaborating and interacting simultaneously.
Abstracting over the speed-up aspects of parallelism gets you to concurrency -- the independence of knowledge held by distinct agents that is not directly knowable by others -- which is far more fundamental than simply making programs run faster. In my experience, most properties of modular systems can be stated in terms of concurrency. Parallelism is an exploitation of that concurrency structure to schedule things efficiently, but it is by no means the only application.
The fact that "one can achieve a 7x speedup on 8 cores by adding a handful of lines to a program" tells me that Haskell is extremely good at letting you expose the concurrent structures of your problem domain.
I don't think parallelism is the word for that. More like concurrency
Parallel computing is closely related to concurrent computing—they are frequently used together, and often conflated, though the two are distinct: it is possible to have parallelism without concurrency (such as bit-level parallelism), and concurrency without parallelism (such as multitasking by time-sharing on a single-core CPU).[5][6] In parallel computing, a computational task is typically broken down into several, often many, very similar sub-tasks that can be processed independently and whose results are combined afterwards, upon completion. In contrast, in concurrent computing, the various processes often do not address related tasks; when they do, as is typical in distributed computing, the separate tasks may have a varied nature and often require some inter-process communication during execution.
https://en.wikipedia.org/wiki/Parallel_computing
What you're describing sounds like it would be solved by primitives such as the actor model, not by Haskell-style FP.
> Yes, parallelizing a program is about making it run faster; but parallelism is also a base fact of many problem domains, where you have multiple agents (up to and including humans) collaborating and interacting simultaneously.
Maybe I should say what I meant more clearly. The claim "one can achieve a 7x speedup on 8 cores by adding a handful of lines to a program" implicitly has a "and this is good because being fast is good" at the end. Why care about a speedup if you don't care about speed in the first place?
It follows that when evaluating a language's ability to gain speed through parallelization, we should also evaluate how fast that language is in the first place. If you can get a 7x speedup on 8 cores from a few lines of code, but the original algorithm is, say, 10x slower than a similar implementation in C/Rust/Go on one core, then who cares? The ultimate criterion is always absolute speed, not relative improvement.
I agree that being able to deal with concurrency is important, but as the Go people always remind me, concurrency and parallelism are distinct concepts.
Also, you seem to be overstating the maturity of PDE (people are pretty much still trying to turn it into algebra, but it may not be possible) and number theory (that's an active research area).
I have never seen that saying either, but it seems quite correct, with the exception of the areas where an algebra is impossible, so the field matures without becoming one.
Obviously, I'm forgetting some, but none of these are primarily algebraic. Some algebraic methods are used (orthogonal polynomials in random matrix theory, and determinants and a whole host of other things to study integrable models in the KPZ universality class, etc.), but clearly groups/rings/fields are not playing a major role.
For a more systematic approach you could look at recent issues of Annals of Probability, Annals of Applied Probability, and similar journals. There's not going to be a lot of "modern algebra" (of the flavor you see in algebraic geometry) there.
The same comments apply to PDE and analytic number theory. Both are obviously mature fields (worked on for a long time by many people, with a lot of great discoveries), but again algebra does not play a central role in either. In particular I am not aware of any PDE specialists whose research agenda consists of "trying to turn it into algebra."
Julia is a multi-paradigm language, but it is heavily Erlang inspired (another multi-paradigm language, but primarily functional.)
I strongly identify with this view. Have always loved math. I remember writing a paper in middle school on how all of nature is built on math which ties closely with this quote "algebraic sense of wonder". My B.S. degree is in mathematics and I loved it.
And yet... I very strongly - no, extremely - dislike functional programming. When building concrete things, I love pragmatism above all else and to me functional is the diametrical opposite of that. A CPU has registers, instructions and addressable memory. Any abstraction that strays too far from that reality is a distraction (and maintenance burden) I don't want to deal with.
Haskell is a pretty awesome language, but it doesn't make it easy to incrementally optimize programs.
Mutations and IO are very critical parts of programming and the two areas where FP breaks down. Even the IO Monad leaks the imperative nature of the program over to the programmer.
The only way pure FP can sort of work is if there are heavy frameworks abstracting IO and mutation away from the programmer. If you're not doing IO or mutating something then your caching framework, database or Haskell runtime is doing it for you. Additionally like I said earlier, Even if your database is handling mutation for you, you still end up embedding mutation commands into the strings of your pure FP function. Updating a database in haskell still necitates the haskell user to place the mutation command in a SQL string.
My point is, that math is not the complete solution to the programming problem. What FP allows the programmer to do is to use the framework to segregate combinatorial logic away from mutation and IO. Your combinators will always be more composeable and modular but your IO and mutation functions will be less modular but they still have to exist.
This is completely untrue. You may not be familiar with the math, but that doesn't mean it doesn't exist. If you can model something well enough to understand it, then you can model it mathematically. There really isn't any domain of knowledge that math is unsuitable for, excepting if you don't know any relevant math to do.
I'm speaking in less technical terms. For example in general mathematical equations or axioms represent immutable concepts. In programming, variables mutate and change... very different from what math traditionally represents. Haskell is an attempt to segregate the immutability (the math part) away from the less "mathy" part (the mutations/IO).
Maybe math is too broad of a term. I probably meant to say "algebra" can't model all of programming, or whatever more suitable word that may or may not exist.
The interpretation of the io monad? That's another story.
Once you realize that mutation itself is a side effect then why not use the single most powerful idiom we have for abstraction over side effects? (Monads with do notation; for blocks in f#)
For the record io is st with an opaque realworld type. And st is fully pure with a neat type trick to prevent leaking of st references.
This is also the same reason why I like SQL, it describes properties of the result set--if your thinking of it as procedural steps it's more difficult. When it comes down to performance tuning however, you do have to consider what really happens in terms of index choices, loop nesting, etc. I never got into FP problems large/complex enough to hit this point where I have to understand how FP sausages are made.
To go into another timeless paradigm, logical programming is famous for unexpected exponential execution times. It's rare that those get past tests, but it's something that developers are constantly having to fix (this or ensuring the type system detects determinism). SQL has a much milder version or this with underspecified joins.
Getting ride of time makes a lot of things easier, but is not trouble-free.
I have never heard of those famous "unexpected exponential times" in logic (not "logical") programming. Could you please give an example of what you mean?
I'm also not sure what you mean by "this or ensuring the type system detects determinism". What logic programming languages with a type system do you mean? Why would a type system be required to detect determinism?
Non-buggy performance follows different rules.
1. Interested in Haskell. Learn the language, read papers, enjoy the PL concepts and interesting academic work.
2. Does it scale? Implement small program in in C++ and Haskell. Optimize as best I can. Talk to people who work on ghc who happen to be in my lab. Further optimize. Everybody agrees, no further clear way to optimize the Haskell program.
C++ implementation remains several orders of magnitude faster.
If it were a time and space trade off, laziness would lead to good performance. But it doesn’t. It tends to produce stupendously bad cache behavior.
Haskell is a lovely platform for understanding some recent PL theory. It is possible to write the sort of obviously correct programs that make it so desirable.
It isn’t resource efficient. Worse, laziness means that performance tends to be chaotic. Small seemingly unrelated changes can change the execution order and change your profile a lot. Similar challenges exist in logical programming.
Of course I prefer writing in a functional style, because pure functions are just simpler. But all of computation can’t be reduced to just pure functions, which is why even Haskell has to call out to its runtime eventually.
All of computation provably can, but not everything computers do, despite the name, is computation.
(1) As mentioned in the article, values are immutable and side effects are reified so you do not have to reason about time. Instead, you can reason equationally, which unlocks some nice benefits related to refactoring, reasoning about correctness and concurrency, testing, reproducibility, etc.
(2) Functional programming is based on well-behaved mathematical foundations and so is more stable than the other programming paradigms. Object-oriented programming is a constantly moving target because it doesn't have a principled formal foundation (despite many attempts to establish one on an ex post facto basis), whereas functions are "timeless" in the sense that they're here to stay.
It's not without tradeoffs of course, but if you're willing to have an open mind about how computers should be programmed and forego some things that are so pervasive in traditional programming (e.g., unrestricted side effects), the payoff is huge.
While I too mostly prefer functional programming principles, the argument seems weak to me. Even if those features are timeless and programs written with those can be ported to most other languages, it doesn't follow that an extra feature (even if invented in the future) won't make you 10x more productive thus desirable.
I prefer the functional programming style (in a non-FP language) for the productivity gains. I move faster, the codebase has a smaller footprint 'in my brain', it's easier to reason about it, so overall I'm more productive when using it.
They have generally had them for over a decade now.
I don't know if it's the case any more as I mainly work alone, but I often saw new programmers had been taught about recursion, but didn't actually understand it. So they never used the concept until you pointed it out, even if it could vastly simplify their code.
No, not in practice. For example, in an average imperative language, you'd have no tail-call optimisation, and so would run out of stack space pretty quickly. There'd also be no persistent collections, meaning you'd have to keep copying any sets of values for every recursive call with absymal complexity. And so on...
To add, I find the strong push for purity of functions helps free up the part of my mind which seems to constantly worry about potential problems or unknown/unexpected behaviors. With that background noise out of the way, I can create more elegant (by my own judgement :) ) solutions to needs. I also sleep better.
Now I prefer imperative, eager computation with (safe) mutability and even some lightweight OOP (the way Rust does it with Traits/Type-classes). It seems that it much better reflects the real world. Rust makes the mutability concerns obsolete, as you can have safety in the imperative/mutable world.
Otherwise Rust is very much functional in all the great ways explained in the article.
Rust takes best of both worlds. You can both mutate, and you are sure that this is compile-time guaranteed, so you can't just try to mess with a value that this part of the code does not own.
To first order, Haskell users self-select as uninterested in writing useful programs. Anybody who would like Haskell to be seen as a language that is useful for writing useful programs would better publish programs in Haskell that do useful things than explain what they like about monads.
I can confidently advise against choosing to add to the list of IRC clients, window managers or greps, or stuff only Haskell coders use. A program that does something no other program does, as pandoc does, that is badly needed, as pandoc was, would be the best choice.
A second course would be to find ways to make Haskell code run fast without need to jump through hoops that corrupt the design of the program. (It might be that it is too late for Haskell to move in this direction.) There appears to be room in the world for slow languages (Python, PHP), so slowness is not deadly, but it does limit a language's appeal. Haskell doesn't need that. Prima facie, Haskell should not need to be slow.
A Haskell program that generates a program in (e.g.) LLVM IR to do a useful thing quickly would serve in lieu of an actually fast program. FFTW is a fast C library for evaluating FFTs generated by an OCaml program that does not need to be very fast.
FFTW relies for its speed on a cache-oblivious array traversal order (re-)invented[0] by Todd Veldhuizen for use in a C++ template library Blitz++ that, in effect, generates programs that perform matrix operations. In Veldhuizen's library, the program was in (pretty slow) C++ template meta-language that ran during compilation of the user's program, but by generating C++ in place it produced a fast program in one step.
A Haskell or descendant not dependent on GC could find use in places where present Haskell is not welcome today.
[0] Cache-oblivious traversal was, apparently, originally invented in the '70s to speed database operations where the the storage cached in main memory ("core") was on disk and tapes, and was then mostly forgotten. It may be re-invented again, in another generation.
You have to go through great lengths to avoid mutability in Haskell, which is why they are developing linear types. I tried it for a few months was like nope.
Flax is currently by far my favorite neural net library, despite being pretty small, because it lets you write a neural net module as a function and decorate that with @nn.module — the decorator wraps your function in a class instance to manage the params and state. This can shave 10 lines per module, and if you’re like me, you have like 50 bajillion ideas for different neural net modules, it really adds up.
TLDR: Check out Flax if you like functional programming and neural networks!
Monads... they're just a way of remaining pure (meaning immutable) but still doing imperative-ish stuff. You don't need monads to make software: both pure-FP and imperative languages can be Turing-complete, so you can do it all. For doing IO you can just use an imperative shell [1], that "calls" the main function of your pure-FP program (aka: a runtime).
Monads for IO make it easier to communicate with this "imperative shell" while still remaining pure. It communicates with it by requiring you to return an IO object all the way to your main function.
But Monads can also be used for other things that don't need IO, like "internal mutable state", "maybe monad" or even for simulating an exception system. Those don't require you returning anything.
Monads are nothing special for real... but you have to use to understand them, since most explanations just make it harder to understand, so I won't attempt one.
[1] https://www.destroyallsoftware.com/screencasts/catalog/funct...
Haskell has a decent system for building up complex data structures. You can write functions for those. Those data structures + those functions are kind of "an object" from the OO world (data + methods).
The differences are:
* Inheritance - well Haskell has many kinds of polymorphism so it doesn't need it.
* Hiding data - well Haskell can hide data using smart constructors, you don't need an explicit private keyword
* Interfaces - covered by typeclasses but they do a lot more!
Also without any of the above you can create object-like things with just closures. If a scope has access to 3 variables in scope, it's "kinda" like an OO object with 3 member variables. You can create new scopes in a for loop, which is like creating many new object. It's a stretch but the underlying concept of something owning other things is still there.
And in this setting, the type of that datastructure containing the functions can act as an interface.
A trap that OO programmers sometimes fall into with Haskell is to try to emulate OO interfaces using existential types + typeclass interfaces but that is a bit of an antipattern.
HTML DOM as a crude example, is a big 'tree of state' whereupon OO would lend well to the nature of the noes on that tree for example.
When you're crunching data and the entire process is literally some kind of transform FP I think more intuitively maps to the situation.
On the DB side, there is a niche of OODBs that exist and used particularly in networking because the persisted state there is really topological, in which case it doesn't always lend well to RDBMS.
pos = pos.moveBy(x,y)
vs
pos = moveBy(pos, x, y)
Use sparingly. I would avoid altogether except for common ones like map, reduce and filter.
Functional programming promotes the idea of composition of morphisms and point free programming. I would use this in place of Object composition/dependency injection/higher order functions or whatever you want to call it.
I work in FP languages, and I find this type of code extremely intuitive and easy to understand.
I suspect that you are just not used to thinking this way, and that with more familiarity, you would not have this opinion.
But this is not unique to FP. If I'm using OOP, I can do the same thing with virtual functions. In UI frameworks I can do the same thing with callbacks.
Frankly, I don't like it, whether it's FP or not.
The complexity of higher order functions can easily be reasoned about by looking at the cardinality of the type. Sum types and product types are standard and easy to reason about.
However an exponential type that takes in an exponential type and returns another exponential type leads to cardinalities and possibilities that are much harder to reason about.
Composition of combinators is a pattern promoted by point free programming that leads not only to higher readability but greater modularity due to the reduced cardinality. Additionally cardinality of the entire type can he reduced to only analyzing the the final result of the composition.
F = A . B . C
The cardinality of F is all that needs to be known. The cardinality of the individual components can be ignored and you don't have to reason about this. This is not the case for higher order functions.In general business problems that we deal with do not Require higher order functions. Higher order functions share isomorphisms with two concepts we are already familiar with: frameworks and metaprogramming.
Frameworks are programs that call your code and metaprogramming is code that writes and calls code. These things are no different then a function that takes another function in as a parameter and calls it. All three concepts are aspects of the same thing.
Therefore all the downsides of metaprogramming and all the downsides of frameworks are applicable to higher order functions.
Most of the programming problems we solve in the real world do not require metaprogramming. It can easily be avoided and solved with much more composition of combinators. Again, not saying to avoid the usage completely but the usage of higher order functions like metaprogramming should be minimal and used sparingly.
I cannot agree here, being currently involved in modeling and simulation work that would heavily benefit from more advanced type systems. A user would usually prefer to interact with a system with basic elements, but the interactions of the underlying domain concepts really do want to be captured with a richer set of tools.
And, to head off the "in general", there is a long tail of unique problem domains. Each individually doesn't get as much press as, say, a CRUD app, but that doesn't mean that simpler problem domains are necessarily in the majority.
As a silly made up example, you pass a callback to an XML parser to tell you when a node has been parsed, so you can pass in a callback to call an API to let it know. Doing something like that just pollutes the XML parser code for no good reason, and you then debug and find you get a 400 error when trying to parse something and 'wtf' and you have to untangle the callbacks.
I'm having a hard time summing it up right now, but I think if you try to "libraritize" "specific code", i.e. make something that does a very specific job, too generic it causes confusion.
That example has 1 callback but believe me some developers want to stack 'em high.
Although your opinion is more extreme than I can get behind, I don't entirely disagree. I find monads and applicative functors are far easier to understand when presented in their non-higher-order formulation -- that is, "flatten" instead of "bind", and "merge" instead of "ap". You still need to understand "map", but that's an incredibly common pattern that you can get a lot out of for putting in just a little.
I wrote a little comment on this earlier this week.
https://news.ycombinator.com/item?id=24894849
I also agree with your opinion elsewhere about preferring combinators instead of directly passing higher-order functions, but I suspect that's much more of a style and design issue than a language feature distinction. You still need higher-order functions (and I'm frustrated weekly by Java's lack of higher-kinded types or pleasant existential types) as the basement layer to expose a beautiful API in more natural terms.