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