Literate Programming

Donald Knuth’s Literate Programming
Donald Knuth’s Literate Programming

Literate Programming is a documentation first programming style pioneered by Donald Knuth. It’s That comparison doesn’t do it justice as there’s much more involved when it comes to literate programming. writing a blog post as a light specification but with the added bonus of producing a runnable program at the end.

But this isn’t a post about my hacked together attempts at literate programming — that won’t be very interesting. This is more of a small research gathering exercise on the different literate programming tools and workflows.

Ideal

In my opinion, literate programming is still an incomplete paradigm. The biggest drawback comes in the form of programming resistance. This resistance comes mostly from the fact that a problem space is unknown ahead of time and so, as the story goes — the implementation becomes the specification.

That’s the norm, but ideally anyone should be able to jump into a source tree, make changes, and reconcile those changes later on correctly with some sort of generated The RFC (Request For Comments) document style is a nice example of “for the human” language. Here’s the RFC for the Atom Syndication Format as an example. readable documentation or specification. The goal is not perfection but accurate agility, and the tooling should be super simple. What would that look like? Who knows, so here’s what I think a more complete or “agile” literate programming workflow might entail.

  1. The workflow should facilitate calculating and visualizing sources of documentation drift. Difference in the specification (documentation) and implementation (source code) should be easy to reconcile through varied means. This could be by the seat of your pants, automated diffing and fuzzing algorithms between the source and doc, or from signals that inform specificity and constraints. Test driven development and type systems are useful signals but are exposed as low level source code rather than high level human readable constraints.

  2. The initial documentation entry point should not matter. Edit source files directly or enter top down through an overarching literate system. If documentation drift is easy to identify then there is flexibility to experiment “freely” with the implementation. Primitives could be at a singular source file level and ideally any source file can act as an entry point for jump starting a code repository’s documentation in part, or in whole.

  3. Literate workflows should facilitate producing documents in whole or in part as discrete outputs based on section, topic, idea, or source file. This can be in any output format such as PDF (Portable Document Format), HTML (HyperText Markup Language), or Markdown. This is important because sometimes only select portions of a program are worth documenting or reading.

  4. Ideally multiple formats should be available as inputs and outputs. Markdown is popular but there are a range of other markup document languages that offer different advantages. reStructuredText, AsciiDoc, and Troff/Groff are a few. The shortcut approach is to leverage Pandoc, a universal document converter.

There are nigh infinite literate programming tools and workflows. I’ve tried some of them, but not in any meaningful way to write about at great length.

Literate Programming Tools and Programs

The literate programming workflow is based on a simple tangle and weave process. The literate source file contains splices of code chunks with their accompanying explanations. The code chunks are tangled to construct a complete source file or executable. The explanations around the chunks are weaved to create a document that fully explains the program.

Web [pdf]: Web is Donald Knuth’s system for literate programming. Tangling and weaving macros process different parts of a web file to produce multiple outputs. There’s cweb, nuweb, and noweb.

Documented LaTeX Files [pdf]: LaTeX Packages from CTAN (Comprehensive TeX Archive Network) achieve a literate style by weaving and tangling documentation and code with the doc and docstrip packages. Package code is mixed in with commented typesetting inside a documented LaTeX file (dtx).

Babel: Babel converts Emacs’ Org Mode into a powerful workflow more suitable for literate programming. Babel is popular in some circles and has numerous use cases including developer operations and general system crafting and architecture.

Tsodings’ lit: A simple literate implementation based on the Literate Haskell approach. The literate program reads a document written in LaTeX and comments out every line not contained in a \code{} section block. The output is an executable source file and the literate document takes advantage of LaTeX’s output formats.

Zyedidia’s Literate: A literate system based on Markdown. The literate document is written inside .lit files and outputs to HTML and CSS. Since it outputs directly to HTML, there’s a lot of leverage in refining and adapting the output.

Jupyter Notebook: A more popular kind of simple literate programming in the form of literate “computation”. These are interactive GUI (Graphical User Interface) documents for explaining and running code from a single view.

lit.sh: A literate programming preprocessor in pure shell because why not?

Conclusion

Literate programming is difficult and hasn’t caught on because in the majority of cases, programs are built with the specification made up along the way. Put a customer in the mix and your specification changes randomly. This is the status quo because often the range of inputs are unknown along with a full scope of the problem space.

Quantifying documentation drift and adding flexibility for limited but accurate documentation could assist in making literate programming much easier. Testing and type systems are becoming much more robust — perhaps Donald Knuth was just a tad bit too early for literate programming to fully catch on? My hunch is that if someone or a company figures this out, it will be super simple to use and would look like git but for documentation.

In terms of popularity, the most used literate programming tools seem to be Babel and Noweb.

29 June 2022 — Written
1 July 2022 — Updated
Thedro Neely — Creator
literate-programming.md — Article

More Content

Openring

Web Ring

Comments

References

  1. https://thedroneely.com/git/
  2. https://thedroneely.com/
  3. https://thedroneely.com/posts/
  4. https://thedroneely.com/projects/
  5. https://thedroneely.com/about/
  6. https://thedroneely.com/contact/
  7. https://thedroneely.com/abstracts/
  8. https://ko-fi.com/thedroneely
  9. https://thedroneely.com/tags/literate/
  10. https://thedroneely.com/posts/literate-programming/#isso-thread
  11. https://thedroneely.com/posts/rss.xml
  12. https://thedroneely.com/images/literate-programming.png
  13. https://en.wikipedia.org/wiki/Literate_programming
  14. https://en.wikipedia.org/wiki/Donald_Knuth
  15. http://www.literateprogramming.com/index.html
  16. https://thedroneely.com/posts/literate-programming/#ideal
  17. https://datatracker.ietf.org/doc/html/rfc4287
  18. https://git-scm.com/docs/git-diff
  19. https://en.wikipedia.org/wiki/Fuzzing
  20. https://en.wikipedia.org/wiki/Test-driven_development
  21. https://en.wikipedia.org/wiki/Type_system
  22. https://en.wikipedia.org/wiki/Markdown
  23. https://docutils.sourceforge.io/rst.html
  24. https://docs.asciidoctor.org/asciidoc/latest/
  25. https://www.gnu.org/software/groff/manual/groff.html
  26. https://pandoc.org/
  27. https://thedroneely.com/posts/literate-programming/#literate-programming-tools-and-programs
  28. http://www.literateprogramming.com/knuthweb.pdf
  29. http://www.literateprogramming.com/cweb_download.html
  30. http://nuweb.sourceforge.net/
  31. https://www.cs.tufts.edu/~nr/noweb/
  32. https://tug.org/TUGboat/tb29-2/tb92pakin.pdf
  33. https://www.latex-project.org/about/
  34. https://ctan.org/?lang=en
  35. https://orgmode.org/worg/org-contrib/babel/intro.html
  36. https://www.gnu.org/software/emacs/
  37. https://orgmode.org/org.html#Summary
  38. http://howardism.org/Technical/Emacs/literate-devops.html
  39. https://www.youtube.com/watch?v=kkqVTDbfYp4
  40. https://github.com/tsoding/lit#readme
  41. https://wiki.haskell.org/Literate_programming
  42. https://github.com/zyedidia/Literate#literate
  43. https://jupyter-notebook.readthedocs.io/en/latest/
  44. https://github.com/vijithassar/lit#readme
  45. https://thedroneely.com/posts/literate-programming/#conclusion
  46. https://git-scm.com/
  47. https://www.thedroneely.com/posts/literate-programming.md
  48. https://thedroneely.com/posts/finding-that-one-percent/
  49. https://thedroneely.com/posts/adding-headroom-with-javascript/
  50. https://thedroneely.com/projects/bulma-resume/
  51. https://git.sr.ht/~sircmpwn/openring
  52. https://drewdevault.com/2022/11/12/In-praise-of-Plan-9.html
  53. https://drewdevault.com/
  54. https://mxb.dev/blog/the-indieweb-for-everyone/
  55. https://mxb.dev/
  56. https://www.taniarascia.com/simplifying-drag-and-drop/
  57. https://www.taniarascia.com/
  58. https://thedroneely.com/posts/literate-programming#isso-thread
  59. https://thedroneely.com/posts/literate-programming#ideal
  60. https://thedroneely.com/posts/literate-programming#literate-programming-tools-and-programs
  61. https://thedroneely.com/posts/literate-programming#conclusion
  62. https://thedroneely.com/posts/a-few-abstracts/
  63. https://thedroneely.com/posts/lets-customize-gitea/
  64. https://thedroneely.com/posts/a-few-links/
  65. https://thedroneely.com/archives/abstracts/
  66. https://thedroneely.com/posts/a-javascript-executable/
  67. https://thedroneely.com/abstracts/golden-sun/
  68. https://drewdevault.com/2022/09/16/Open-source-matters.html
  69. https://mxb.dev/blog/make-free-stuff/
  70. https://thedroneely.com/sitemap.xml
  71. https://thedroneely.com/index.json
  72. https://thedroneely.com/resume/
  73. https://gitlab.com/tdro
  74. https://github.com/tdro
  75. https://codeberg.org/tdro
  76. https://thedroneely.com/analytics
  77. https://thedroneely.com/posts/literate-programming#
  78. https://creativecommons.org/licenses/by-sa/2.0/
  79. https://thedroneely.com/git/thedroneely/thedroneely.com
  80. https://opensource.org/licenses/GPL-3.0
  81. https://www.thedroneely.com/
  82. https://thedroneely.com/posts/literate-programming/#