Unofficial Hacker News companion — get notified on comment replies, and more.

The need for a formally verifiable mainstream programming language (opens external site)

344 points by todsacerdoti 4 days ago | blainehansen.me | 325 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (9)

  1. If Haskell would finally give up on the disaster that is laziness (and by extension GHC) and pivot to formal verification, this could be the killer app.

    Much of what the author wants already exists!

    Replies (4)
    1. The issue with Haskell and other functional programming languages is that it doesn't reflect the hardware paradigm. Reasoning within a functional programming language is obviously simplified due to the statelessness, however you lose the benefits of memory hierarchies and other hardware improvements we have due to spatial and temporal locality. You might be able to fix this with a smart compiler, but then reasoning about the correctness will be another challenge.

      If a language cannot balance tradeoffs between hardware and software, people will still use the most performant language, especially for resource-intensive tasks, so you end up with what we currently have. We need to rethink how we can design a language that is token-efficient, offers memory safety, is close to the metal, and lastly implements formal verification guarantees. The closest what we have to this is Rust and C++, but it's not good enough.

      Replies (2)
      1. > however you lose the benefits of memory hierarchies and other hardware improvements we have due to spatial and temporal locality.

        This is ironic considering the default GC strategy for GHC's RTS is a copying collector that puts values physically close if they are used together plus bump allocation. That has good cache side effects (heh).

      2. Linear type solve a lot of this problem. Idris2 have https://github.com/stefan-hoeck/idris2-ref1 which has been proved to be efficient
    2. The haskell community has practically settled on StrictData for years now... Alexis King gave the final blow on. Old complains never die though.
    3. At that point why not just use Lean instead of Haskell? It has opt-in laziness with thunks which is arguably the better approach
    4. Laziness is fundamental to Haskell: that's its raison d'être. If you look at old Haskell code and Haskell code from nowadays, I don't think formal verification (in the sense of automated theorem proving) was ever anyone's priority. In fact, old Haskell seems even more cowboy-like in the old days compared to how it is today. People were totally fine with leaving unhandled cases because their proof on paper demonstrated that those cases were unreachable, for instance. They often abused circular or infinite lists a lot more for the sake of a short, elegant implementation. To be honest, while Haskell shares syntax with some theorem provers, Haskell itself has a certain attitude that isn't really shared by any other programming language, an attitude which favors brevity and which gives freedom to the programmer, the opposite attitude to that of a theorem prover. Laziness is a big part of that. It allows the programmer to write seemingly mathematical code without actually having to prove it formally.
      Replies (1)
      1. There's widespread agreement in the community, by the people who designed Haskell, that if there is a next Haskell, it should be strict.

        Exploring laziness was one idea behind Haskell, one of many. And we learned a lot by doing it. But we also learned that the costs (in terms of execution, complier complexity, debugging, and even internal consistency in the language) are extremely high.

        Laziness is not what lets you do equational reasoning in Haskell. The fact that you manage side effects, that's what lets you do equational reasoning. That is a feature of the type system, it has nothing to do with laziness.

        Replies (1)
        1. First, I said that laziness allows the programmer to write seemingly mathematical code without actually proving it, not that laziness is the sole thing that lets you do equational reasoning.

          But on the subject, laziness plays a considerable role in equational reasoning. Equational reasoning is simplified by it. For instance, these two expressions are equivalent in Haskell, but are not equivalent in a strict language.

             let x = <potentially non-terminating calculation>
             in if <condition> then x else <something else>
          
             if <condition> then <potentially non-terminating calculation> else <something else>
          
          Hence you can apply the rule that "let x = <expr> in <e2>" is equivalent to <e2> with x inlined, other than that the computation is only done once.

          Or if "f" is a function that is equivalent to "const 1", then "f (1 `div` 0)" should simplify to 1, not to bottom.

          It also lets you write this kind of code (taken from Rosetta Code):

            primesSA = 2 : prs
              where 
                prs = 3 : sieve 3 [] prs
                sieve x fs (p:ps) = [i*2 + x | (i,True) <- assocs a] 
                                    ++ sieve (p*p) fs2 ps
                 where
                  q     = (p*p-x)`div`2                  
                  fs2   = (p,0) : [(s, rem (y-q) s) | (s,y) <- fs]
                  a     :: UArray Int Bool
                  a     = accumArray (\ b c -> False) True (1,q-1)
                                     [(i,()) | (s,y) <- fs, i <- [y+s, y+s+s..q]]
          
          In this case, not having to prove totality makes the rest of the code easier. I deliberately chose this example and not the classic 3-line version with trial division because it's not inefficient and yet it still uses laziness to make the program simpler.

          And historically the reason why laziness was favored was because it enabled modularity better.

          That is partly why I think laziness is worth having.

          A Haskell without laziness would basically be a programming language from a different tradition. It has never been a "correctness at all costs" programming language, as I already stated in my previous comment. It may be that such a programming language is what those who design it want, but that isn't what it is.