|
| 1 | +(*********************************************************************************) |
| 2 | +(* mdexp - Literate Programming with Embedded Snapshots *) |
| 3 | +(* SPDX-FileCopyrightText: 2025-2026 Mathieu Barbin <mathieu.barbin@gmail.com> *) |
| 4 | +(* SPDX-License-Identifier: LGPL-3.0-or-later WITH LGPL-3.0-linking-exception *) |
| 5 | +(*********************************************************************************) |
| 6 | + |
| 7 | +(* @mdexp |
| 8 | +
|
| 9 | +# Broadening the View |
| 10 | +
|
| 11 | +Now that you have a feel for how mdexp works, let's step back and |
| 12 | +consider where the approach leads and where you might take it. |
| 13 | +
|
| 14 | +The previous chapters focused on mechanics: directives, snapshots, |
| 15 | +and the EDSL pattern. Here we broaden the view to explore |
| 16 | +additional use cases, discuss design choices, and invite you to |
| 17 | +think about how this pattern of *compilable, verified documentation* |
| 18 | +might apply to your own projects, whether or not you use mdexp |
| 19 | +itself. |
| 20 | +
|
| 21 | +## Cram-style tests in OCaml |
| 22 | +
|
| 23 | +OCaml's ecosystem uses `.t` cram files to test command-line tools: |
| 24 | +you write a shell session with expected output and the test runner |
| 25 | +checks it. mdexp offers a path to express the same tests in OCaml |
| 26 | +and we are experimenting with a `Mdexp_cram` library (wip, unpublished). |
| 27 | +The "background" part of the process becomes the evaluation environment |
| 28 | +(setting up state, running commands, capturing output) while the visible |
| 29 | +part reads as a tutorial showing the session step by step. |
| 30 | +
|
| 31 | +This means the same file can serve as both a functional test and a |
| 32 | +user-facing walkthrough: the test runner verifies correctness, and |
| 33 | +mdexp extracts the narrative. |
| 34 | +
|
| 35 | +## Client/server application testing |
| 36 | +
|
| 37 | +The pattern extends naturally to networked applications. The |
| 38 | +background spins up a running server, and the visible part scripts a |
| 39 | +series of CLI client calls that exercise the service. Each call and |
| 40 | +its output become a snapshot in the document; the full session builds |
| 41 | +up an end-to-end functional test. |
| 42 | +
|
| 43 | +The reader sees a coherent tutorial --- "first create a resource, then |
| 44 | +query it, then update it" --- while the build system verifies that |
| 45 | +every response matches expectations. The documentation becomes a live |
| 46 | +integration test. |
| 47 | +
|
| 48 | +## Your own EDSLs |
| 49 | +
|
| 50 | +The [EDSL chapter](edsl.md) showed a math expression language and a |
| 51 | +toy proof assistant. But there is nothing special about math --- the |
| 52 | +same pattern applies wherever you want to both **render** and **verify** |
| 53 | +structured content. |
| 54 | +
|
| 55 | +The key point is that mdexp does not provide or prescribe any of |
| 56 | +these EDSLs. It provides the infrastructure --- directives, snapshot |
| 57 | +capture, the `pp` pipeline --- and you build whatever domain-specific |
| 58 | +layer makes sense for your project. It is just OCaml; there is no |
| 59 | +plugin architecture to learn. Your EDSL lives in your codebase, uses |
| 60 | +your types, and evolves with your project. |
| 61 | +
|
| 62 | +## Host and output languages |
| 63 | +
|
| 64 | +Neither the host language nor the output format is a hard constraint |
| 65 | +of the system. mdexp's directive syntax is designed to work with |
| 66 | +any language that has block comments, and we have prepared support |
| 67 | +for host-language expect-test frameworks written in **Zig** and |
| 68 | +**Rust** alongside OCaml. Similarly, the output is not limited to |
| 69 | +Markdown --- it could just as well be Typst, reStructuredText, or |
| 70 | +any other text format. |
| 71 | +
|
| 72 | +That said, we have focused our effort on the **OCaml + Markdown** |
| 73 | +combination for practical reasons: |
| 74 | +
|
| 75 | +- **OCaml** is well suited to the kind of work mdexp encourages. |
| 76 | + The examples in this book --- symbolic computation, algebraic |
| 77 | + rewriting, proof construction --- benefit from a language where |
| 78 | + you can manipulate structured data at a high level without |
| 79 | + worrying about memory management. Rust and Zig are equally |
| 80 | + appealing here --- each handles memory in its own way without |
| 81 | + burdening documentation-oriented code. Which language fits best |
| 82 | + may depend on the libraries you need to pull into your document. |
| 83 | + We would not be surprised if mdexp found a place in |
| 84 | + multi-language codebases, with different chapters written in |
| 85 | + different host languages. |
| 86 | +- **Markdown** integrates well with the rest of our tooling: static |
| 87 | + site generators, mdbook, slipshow, and the broader ecosystem of |
| 88 | + tools that consume Markdown as input. |
| 89 | +
|
| 90 | +Other combinations are possible and welcome; the architecture does |
| 91 | +not privilege any particular pairing. |
| 92 | +
|
| 93 | +## Closing thoughts |
| 94 | +
|
| 95 | +Beyond the specific tool, the underlying pattern is worth |
| 96 | +reflecting on: write documentation as a compilable source file, |
| 97 | +use the type system and test framework to keep it honest, and let |
| 98 | +a thin preprocessor extract the readable output. This is not a new |
| 99 | +idea --- literate programming is decades old --- but the combination |
| 100 | +with snapshot testing and embedded DSLs gives it a practical edge |
| 101 | +that we believe makes it click in interesting ways. |
| 102 | +
|
| 103 | +mdexp is one implementation of this pattern, intentionally minimal. |
| 104 | +It reads directives, extracts content, and produces output. |
| 105 | +Everything else --- the types, the tests, the rendering logic, the |
| 106 | +EDSLs --- is ordinary code in your project. The tool stays small |
| 107 | +and predictable; the power comes from what you build on top of it. |
| 108 | +
|
| 109 | +We hope this introductory book has given you both a working |
| 110 | +understanding of mdexp and a sense of the broader possibilities. |
| 111 | +For deeper coverage --- reference material, how-to guides, and |
| 112 | +additional examples --- see the main documentation site. *) |
0 commit comments