logoalt Hacker News

dev_dan_2 • yesterday at 10:35 AM • 0 replies • view on HN

To add to the other answers, let me address the experience of "a series of steps" versus "a big single expression with a type" - real life is calling, so I have to make this quicker than I would like to; feel free to ask any followup questions / for clarification! I am sure others or later, I when I am back on the screen can answer :)

So the very short version: There is no difference between those two worlds if you squint your eyes hard enough! They are equally powerfull; just different ways of expressing the same truth. This is the whole declarative ("WHAT should the end result be") vs. implerative ("HOW to achieve the end result")stuff, but it's all the same at the end. Some examples:

  - SQL is declarative; but under the hood, it will get translated to a sequence of operations on the database by the query engine (or however it's called)
  - HTML is declarative; but there is no theoretical reason stopping you from building some sort of "imperative" HTML where you just add elements and stuff in a sequence of steps and at the end; are shown the same webpage (in fact, there are GUI frameworks that build up the GUI in an imperative manner; for the web, transmitting a declaration of what you want rather than a program that constructs it was the more practical choice I assume)
  - Haskell is often considered declarative (for good reasons), but if you think about the IO type as ("Hey I am a wrapper type and all I do is to enforce that step A happens before step B"), then you are pretty close to a imperative way of doing things again
  - Go/Java/C#/Python with types are almost always considered imperative; but you can also put on the type checking hat and pretend that the program is just one big function that will - if written correctly - have a final type. How does composition happen here? It's basically the line break, line :103 is executed before line :104, so lines :103-:104 can be viewed as: "the function that does whatever :103 does, then whatever :104 does", and it has the type "whatever input type :103 has and whatever input type :104 has", and type checking ensures that all those pieces fit together neatly.
They can even be mixed; in the proof assistant Rocq (formerly known as coq), there where both styles used often. In Lean, as I vaguely remember, you can pick both style too, so to say.

I think the pragmatic answer is that most such languages originated from the "one big function" school, and so it is more natural to program/proove in that way.

---------------------------

*Wait, does this mean that every proof I learned can also be done in this "one big function" way, instead of "a series of steps"?*

Yep! But you will find that it is not really practical to do so. A good exercise for sure though; and something I did (by that I mean: infering types on paper; not really taking proofs I know and doing them be type inference on paper) by hand when diving deeper into this topic in grad school.

Here is how that approach looks like, roughly: https://www.researchgate.net/figure/Example-of-type-inferenc... (the term I search for regarding illustration is: "type inference examples")