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 (3)

  1. 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.