205 points · by based2 · 8y ago · 44 comments
The `bisect-binary` project listed in the introduction is almost the _exact_ program I made while I was a devious high school student trying to circumvent weak (i.e. most widely-used-) antivirus software by locating a few "key" bytes in the binary that the AV used to fingerprint a virus.
I would zero out half the file, scan the file with AV, then recursively repeat the same for any zero'd regions of files that AV didn't consider a virus. Essentially a search of the file for the smallest set of bytes that needed to be changed for AV to fail detection.
Usually this would result in only a small number of bytes that needed to be slightly tweaked.
Pretty unproductive "hobby", but I must admit it was really fun. :)
I've never worked with Coq myself, and really, the process described here still looks much too convoluted for practically proving nontrivial amounts of code correct. But it looks like lots of fun too! And it is sort-of the holy grail of computer science, writing code and being able to prove it correct instead of just testing it with maybe-or-maybe-not enough test cases.
In practice I find that Haskell itself already prevents a lot of bugs, but of course in functions like the one dissected here, logical errors are still easily made.
I'll definitely look into Coq one of these weeks.
The author John Harrison who works at Intel has used his theorem proving application hol-light in doing verification on large code bases.
If you are more interested in programming, consider trying Idris (http://idris-lang.org/). Edwin Brady, the language designer, wrote a great book that I would recommend called Type-Driven Development with Idris (https://www.manning.com/books/type-driven-development-with-i...).
The documentation isn't as great but the main tutorial is decent. It also doubles as a practical programming language. And the community is active and helpful.
I've always found Coq to be a little impenetrable. I don't know what makes Lean more accessible to me but I've found it much easier to get into.
It's also not clear to me why I would use this over writing the program in the constructive logic of coq, agda or idris and then extract ("compile") the program from the proof itself. Of course, a usecase would be to formally verify "legacy" haskell code, but then my first point applies -- there is not that much important haskell code out there.
The last reason for the existence of this program I could see is that this is just experimental and the authors want to explore whether it's possible to formally prove things about haskell programs. But isn't that obviously true, given enough time put into the project?
http://hauptwerk.blogspot.co.uk/2017/04/four-openings-for-ha...
https://code.facebook.com/posts/745068642270222/fighting-spa...
Not exactly, they have their own closed source fork of GHC, which is strictly evaluated. Given that the codebase is proprietary it's unknown what other differences there are beyond having dumped lazy evaluation. It's a Haskell-like language, but far different than the Haskell that the public has access to.
Well for one thing, sometimes projects are just for fun, or to explore uncharted territory, without thinking about a specific use case. For another, there are definitely use cases for proofs in code, as verified software appears in a variety of domains. It’s not likely to make its way into the average CRUD app any time soon, but that doesn’t mean it’s not useful.
> Haskell is barely used in the first place, and even less for highly safety critical software.
“Barely used” is relative. Compared to java or python, sure. But there are untold millions of lines of Haskell code running in production right now. Besides, in my humble opinion Haskell should be used more, whether or not it is (right now). And in any case Haskell has had a big influence on many other more popular languages, and/or libraries, and this could one day be another example of that.
I agree that haskell should be used more, at least if the alternative is one of the main stream languages.
In other words, there's at least some important Haskell code, and there's Haskell code important to someone. That's a good enough reason. Or are you saying we can't have nice things?
Every new approach is initially barely used in the wild, and the initial method of such a new approach is usually quite different in details from how things end up eventually.
In this case the value lies in working out how proving software could look like, and proving program after writing it is a different direction than writing it to a proof. We can't tell which of these methods is more useful or convenient before somebody checks them out.
The core idea behind it is that statically proven assertions would be really powerful.
Loop invariants can be simply expressed as an assertion inside the loop.
Additionally, to give it a bit more power; assertions could be given the ability to act over sets (say over all ints).
This might look something like
XY = {int, int}
@assert commutative
fn add(a XY, b XY) XY {
return (a[0]+b[0], a[1] + b[1])
}
fn commutative<a,b>(f (a,a)b){
assert x,y: f(x,y) == f(y,x)
}
And then it would also be helpful to add on assertions to types so assignments to variables also checks the assertions order = enum {greater, equal, lesser}
comparator<a> = (a,a)order
fn sorted<a>(pair ([]a,comparator<a>))bool {
(l, cmp) = pair
if l.length() <= 1 {
return true
}
return cmp(l[0],l[1]) != order.greater & sorted((l[1:],cmp))
}
SortedList<a> = ([]a,(a,a)order) & sorted
There are a few major issues with my idea currently (it really requires dependent types to be ergonomic and allocation and pointers could be strange) which have prevented me from making a quick demo compiler or language spec. But I would be very excited to discuss it.The main reason why the language I am envisioning is not a functional language is because Idris, Liquid haskell, ... have already explored many of the related ideas. I want to take ideas from them and bring it into an imperative language as that hasn't been as well explored.
I took a formal verification course in college that involved writing several verified sorting, search etc. algorithms in Dafny [2]. I remember it being somewhat cumbersome to write your assertions in a way that the checker can check them, but it's looks very much like what you're suggesting.
(At the time I didn't realise that not only does the checker check the program, but compiles it into a CLR-compatible binary, hence you'll see equivalent C code for comparison)
[1] https://github.com/Microsoft/dafny [2] https://github.com/shawa/formal-verification-project
To make it practical to verify imperative programs, you're likely going to end up imitating a functional and stateless style to get anything done anyway.
Low-level pointers make everything very very annoying, though.
Here's how the assertion of commutativity would look in F*:
val f : int -> int -> Tot int
let f a b = a + b
let commutative = assert (forall a b. f a b = f b a )I think tools that enable formal reasoning about code are incredibly valuable.
Just going through the process the author was able to find and fix three bugs. Could you imagine a pipeline where people submitted code that had already been proven to be correct?
Sounds amazing!