50 points · by ddougj · 3y ago · 67 comments
* It messes with tooling. If you're lucky then your editor will be smart enough to syntax highlight inside code blocks, or can be taught to do so easily. It's unlikely that more advanced IDE-style features will work. Obviously tools could be adapted with custom plugins but literate programming isn't popular enough for these to already exist as far as I'm aware.
* It adds overhead to refactoring. Refactoring code now means editing and rewriting a book, doing a good job requires significantly more effort. If you have designed the program thoroughly ahead of time you can avoid this, but that's not generally the way most programmers work.
* The "includes" problem. Most language require you to include/import/require packages used in a file. These are usually all placed at the start. This means a lot of chapters will start with "here are all the includes we'll need" if you're using a linear format. More complex formats that rearrange the code to generate outputs can do a better job but it's still a little clunky.
Overall my conclusion was that literate programming works best if you write code like Donald Knuth: work solo, have a good idea of the entire design and a fixed scope which you can complete to declare both book and program "done forever". Unfortunately that doesn't cover most real world programming. I don't think it works well in the most common commercial/programming team style settings.
In my opinion, this is a good thing. The problem with ordinary refactoring is that it's too easy to just shuffle things around until they work/seem nicer/do whatever you want to. However, with literate programming, you have to think why you're refactoring, and how does it reflect in the explanation.
I do this with Typescript (deno) and a neovim plugin called sniprun.
Probably a niche example and the exception that proves the rule, but in Racket, the included literate programming (Scribble/LP2) is itself a language implemented in Racket. Racket’s IDE and tools for exposing and inspecting syntax work just as well in that environment as in any other Racket-implemented language.
I’ve heard that org-babel [https://orgmode.org/worg/org-contrib/babel/intro.html] works pretty well for this, though I’ve never used it myself.
See https://emacs-lsp.github.io/lsp-mode/manual-language-docs/ls...
Real literate programming, rather than rich text comments, can order the code blocks in any order. You can add then at the very end of the book/chapter/section if you feel like it.
I did some literary programs about a previous generation of our program. Every new hire reads them and asks me for copies.
And yet Knuth still does it the other way. <http://akkartik.name/post/literate-programming>
When I've written literate programs, I almost always shuffle long lists of includes to the end. They add little or no value at the top and distract from the material I want to present. It's also a simple cut and paste to move them back to the top if I wanted to. The only time I leave them at the top is if there's something actually informative about having them at the top, or it's a short program, or I've not actually finished working on it (a lot of my literate programs start as traditional "live in source files" programs that I slurp into org files).
Note the end of the sentence you quoted: “if you feel like it” — Knuth clearly doesn't feel like it in these programs. (BTW, it appears he generally adds a comment next to each #include mentioning why it's being included.)
Literate programming, for Knuth, is supposed to be a fun way to write programs that leads to them being easier to write and read and debug and maintain; it's not some sort of purity discipline that requires everything to be maximally expository.
The idea is more to break the code into parts you would explain and grok easily. Not every atomic part of the code.
Web and its derivatives are sufficiently advanced that they suffer from the lisp curse. What in other systems are major fundamental engineering problems - try adding an include in the middle of a c file - in Web derivates are a matter of taste. Do you have a chunk that picks up includes as you need them? Is it a big one at the front or back? It's up to you.
Since all of this work is in a git repo, it is nice to pass around documents so folks understand the thinking behind things.
Also, (and the main reason I went towards it) is that when I pull what I need from the markdown files, I have steps built around to lint, test, and do additional checks to ensure the required naming, labeling, etc.
It has worked well and reduced many `silly' errors when other folks use the images.
Is it for everybody? Nope. But it works here.
I also did LP many years ago when I worked at a place that produces custom software for embedded systems. Providing them with a friggin' novel about the program was also fun. But, we never .. once .. were told we needed to provide more documentation.
LP has good points, but it isn't a one-size-fits-all.
When asked to send some coding samples, it is fun to have a PDF file that starts 'Chapter 3: And Now, Security! Encryption and Other Pain Points!'
Is it "notebook" style programming, like Mathematica, matlab, Jupyter, and markdown-based stuff like R/python + Quarto? In this mode, I think the focus is more on creating a document or communicating stuff typically with data analysis. And actually, "literate" is a very apt adjective for something that's ultimately intended to be a document of some kind.
Or is it more general intended for writing arbitrary software applications and having everything in the source code? In other words, to understand the source, you just read the source and there are no other documents.
The latter mode, I think, is still very much not around as far as I know (academic proof-of-concepts for papers don't count :-) ). Will it ever really exist?
There is Stanford GraphBase, and a really good MP3 book that are full programs. Not to mention TeX.
The overwhelming popularity of TeX all too often makes people forget about METAFONT. The type of computer fonts advocated by the latter has unfortunately failed to gain popularity, and its descendant MetaPost has a lot of competition, but I think the language itself is much nicer (while remaining true to its macroexpander roots), and it’s another literate program of about the same size.
[1] Gratis at https://pbr-book.org/
[2] Gratis at http://craftinginterpreters.com/contents.html
You're thinking of LCC. PCC is AT&T's compiler and the work of Johnson who also did yacc.
Then we might hope for support in most tooling to appear quite quickly.
I think one reason why literate programming is less attractive nowadays (compared to the days of Pascal) is that programming languages have got better at letting you arrange your code in whatever way you like. So maybe something like the above is enough to get 90% of the advantages of a full literate programming system.
Jef Raskin wrote and thought about this. The sentiment sounds like it would fit best in his piece "Comments are More Important than Code" (2005), but there's another Raskin article "The Woes of IDEs" that contains the most explicit agreement:
> In my method, program segments are embedded in the midst of a word processor document--like raisins in cake--so that the emphasis is on the explanation rather than the code.
<https://queue.acm.org/detail.cfm?id=864034>
> maybe something like the above is enough to get 90% of the advantages of a full literate programming system
If you're doing JS, then you can actually get pretty far with browser-based notebooks that just use vanilla HTML (and CSS), which notably doesn't require any ex ante setup/configuration for runtime support in the vein of e.g. Jupyter notebooks. (The problem being that not everyone writes JS and even most programmers these days who think they're writing JS really aren't and have no idea how to, so this won't work for them, either.)
Pandoc is a nice swiss army knife that can do a similar conversion with some custom lua filters. It could even do wackier stuff like convert word or odt files to code if you were really diving into prose-first coding.
I've also heard it referred to as 'semi-literate programming' because it skips the reorganization functionality and just gives you nice prose to code conversion.
VS code is getting pretty smart about code notebooks too, it has native support for a few different formats of markdown + code blocks (hydrogen, jupyter, etc) and applies all the right syntax highlighting, analysis, etc. to each block automatically.
It's not especially useful. For literate programming you really need the ability to move blocks of code around without the ceremony of function signatures.
[0] https://wiki.haskell.org/Literate_programming#Haskell_and_li...
[1] https://okmij.org/ftp/Haskell/class-based-dispatch.lhs
[2] https://okmij.org/ftp/Computation/lightweight-guarantees/eli...
I wrote more about this at <https://news.ycombinator.com/item?id=35985835> and <https://john-millikin.com/software/anansi#history>.
Similarly, if I preview such a file on github I get syntax highlighting for the code but I don't get rendering for the markdown.
So I think the advantage of an 'official' way of saying you're using this arrangement would be communicating intent to more-or-less generic tools.
You could probably even do it with Markdown directly, using fenced code blocks as part of your begin/end lines.
I think you'll find that this approach offers very little benefit over either traditional commentary or linear notebook-style inline code blocks.
The tooling can be a bit clunky -- though JupyterLab and some plugins can go a long way. Importing notebooks into other notebooks or modules is still kind of weird even with the `ipynb` library.
However, on the otherside of it all, you have a collection of documents that contain documentation, code, and tests, all side by side and storable with proof of past execution.
I'll take this arrangement over someone's unit test and CI suite every day of the week.
For decades, I searched for literate tooling for personal projects, but nothing stuck. Something about the design space seems to make it easy to hit design choices that are uncomfortable fits for one's use case, or personal taste, or preferences for tooling extension and customization. It reminds me of the personal information manager space.
My most recent foray seemed more promising. poly-markdown permits editing polylingual md files, and is permissive towards markdown dialects. So I could use a custom literate markdown, normalize it for rich-but-restrictive pandoc, then get the pandoc ast and emit custom html. With Julia's MLStyle[3] for pattern matching, and a bit of front-end pretty, it was unexpectedly easy to reach something comfortable.
Perhaps for literate tooling to gain greater traction, it helps that software dev progress permits folks to more easily cook their own?
[1] https://github.com/polymode/polymode [2] https://pandoc.org/filters.html [3] https://thautwarm.github.io/MLStyle.jl/latest/
Knuth's Programming Pearls entries are good reading as well.
> In general, literate programs combine source and documentation in a single file. Literate programming tools then parse the file to produce either readable documentation or compilable source.
http://literateprogramming.com/lpfaq.pdf#page11
By this litmus test, the Go standard library qualifies as literate programming. Document production tools are go doc or godoc. The .go files are themselves already compilable source.
It might be worth checking through the PDF the sister comment to yours linked - http://literateprogramming.com/knuthweb.pdf - you'll see what I mean.
https://en.wikipedia.org/wiki/Axiom_(computer_algebra_system...
That is an excellent resource. Thanks very much for posting [and thanks to everyone involved in the original compilation].