How to associate proofs with code?

Status
Not open for further replies.
The work I'm doing is in the intersection of math and programming and I'm often coding functions or computations that I've proven to satisfy certain mathematical properties or follow from certain axioms. The math, and thus the code, is usually sufficiently opaque that just reading it, you wouldn't necessarily guess why it is the way it is, but the proofs are typically simple (5-10 steps) and upon reading them, the code makes sense. The question is where to put the proofs.

On the one hand I'm tempted to put the proofs into block comments preceding the code so that they're immediately available when reading the code. However, it's hard to present math well in plain text (although Unicode math symbols help) - especially when it's indented several levels and thus horizontally challenged. It would be far more readable in LaTeX and the code could point readers to the external documentation, which would be in version control. In this latter case, I could put all external documentation in a folder separate from the code, perhaps organized into subfolders paralleling the organization of the code, or I could place it in the same subfolders as the code, which wouldn't interfere with the build process and would obviate the need to navigate to two different places, but which some devs seem to feel uncomfortable about. If I go with LaTeX, I'm not sure whether to check in rendered PDFs in case other people on the project (me) don't have LaTeX or whether to keep the VCS repository clean and require all devs (me) to have LaTeX.

Any thoughts/ideas/experience?
 

Jonathon

Ars Legatus Legionis
17,647
Subscriptor
I'm not a huge fan of the concept for general, day-to-day use, but this seems like an appropriate use-case for literate programming. Specifically, a tool like Noweb would allow you to combine your source and documentation (in LaTeX format). When processed one way, you get compilable source code; when processed another way, you get the documentation, with pretty-printed math and nicely formatted code interspersed within.
 

MilleniX

Ars Tribunus Angusticlavius
8,141
Subscriptor++
The most prominent and furthest-advanced research project in this area, seL4, used a subset of Haskell that they could directly tie to their automated theorem prover, and then used that as a formal specification of their C implementation. So, if you're looking to actually come out with provably correct code, that would seem to be the way to do it.

Your words seem to suggest that's not really the plan, but it's worth examining one's assumptions.
 
Jonathon":2y6unm6j said:
I'm not a huge fan of the concept for general, day-to-day use, but this seems like an appropriate use-case for literate programming. Specifically, a tool like Noweb would allow you to combine your source and documentation (in LaTeX format). When processed one way, you get compilable source code; when processed another way, you get the documentation, with pretty-printed math and nicely formatted code interspersed within.
Thanks for the suggestion, but I don't think it's a good idea to mark up code files in a way that renders them invalid unless passed through an obscure tool. It wouldn't have IDE support, possibly breaking refactoring, finding usages, and jumping to definitions, among other things, and would add extra complication in the build process. Also, if people have to run markup through a tool in order to view it, there's not much value in the markup being alongside the code because the markup isn't especially readable. So if I put the math in the code, I'd prefer to use my best textual math notation; otherwise, I'd use separate LaTeX files.
 
MilleniX":2na6w4np said:
The most prominent and furthest-advanced research project in this area, seL4, used a subset of Haskell that they could directly tie to their automated theorem prover, and then used that as a formal specification of their C implementation. So, if you're looking to actually come out with provably correct code, that would seem to be the way to do it.

Your words seem to suggest that's not really the plan, but it's worth examining one's assumptions.
I don't care to create provably correct code (I've done it before and it's extremely laborious and tedious - I don't have that kind of time now) - unit and manual tests are sufficient. I want (possibly flawed) implementations of provably correct math, where developers can see the math that inspired the code so that they can understand how the code works.
 

rain shadow

Ars Tribunus Angusticlavius
6,687
Subscriptor++
it's been a while since I wrote anything rigorous but the direction I was headed at the time was to just put the normal amount of comments in the code to explain things about the code itself, then add a URL to point to the documentation about the algorithm. various IDE's will let you clickthrough, and pasting it into a browser isn't a large burden.

if the documentation is in outline format, you can use anchored links (http://www.example.com/foo.html#subsection) to parallel between the layout of the code and the organization of the proof, if appropriate.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
If there is 1:1 correspondence between code files and proofs (or close enough), put them in the same dir. If the relationship is more vague -- lots of variation in the # of files needed to implement each proof, divergent naming conventions, etc -- then put the proofs in their own tree.

In both cases:
* your build process should render LaTeX -> PDF or HTML
* your code comments should contain URIs to the reader-friendly proofs @ the canonical build-output host
 
Status
Not open for further replies.