Repository navigation
Expand file tree
/
Copy pathLanguageChapterExample.lean
More file actions
193 lines (169 loc) · 12.8 KB
/
Copy pathLanguageChapterExample.lean
File metadata and controls
193 lines (169 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import LeanFM.SessionTools
import LeanFM.MCP
import LeanFM.Logic
import Lean.Data.Json.Elab
/-!
Generates every listing, diagram and verdict in Part I and Part II of the book (the LeanFM
language) from two example protocols, examples/sessions/PlaceOrder.leanfm and
examples/sessions/Ping.leanfm, through the same functions the MCP tools call. Nothing in those
chapters is drawn or typed by hand.
Run from the LeanFM root:
lake env lean --run LanguageChapterExample.lean
python3 scripts/svg_to_pdf.py book/generated/lang-*.svg
The second step turns each SVG (LeanFM's own renderer, light and dark themes) into a PDF the
book includes; the light PDF goes in book/main.pdf and the dark one in book/main-dark.pdf.
-/
namespace LanguageChapterExample
open Lean LeanFM.SessionTools
def out := "book/generated/"
def listing (body : String) : String :=
"\\begin{lstlisting}[language={},basicstyle=\\ttfamily\\footnotesize,breaklines=true]\n" ++ body.trimAscii.toString ++ "\n\\end{lstlisting}\n"
def str (j : Json) (k : String) : String := (j.getObjValD k).getStr?.toOption.getD ""
def strs (j : Json) (k : String) : List String :=
match j.getObjValD k with
| .arr a => a.toList.filterMap fun (x : Json) => x.getStr?.toOption
| _ => []
def withLean (text : String) (more : List (String × Json) := []) : Json :=
Json.mkObj ([("lean", Json.str text)] ++ more)
/-- Run a tool; a refusal is a generator failure, never a silent gap in the book. -/
def tool (name : String) (r : Except String Json) : IO Json :=
match r with
| .ok j => pure j
| .error e => throw <| IO.userError s!"{name}: {e}"
def write (name body : String) : IO Unit := IO.FS.writeFile (out ++ name) body
def ctlRow (text : String) (english : String) (formula : Json) (more : List (String × Json) := []) : IO String := do
let r ← tool "session_ctl" (ctl (withLean text ([("formula", formula)] ++ more)))
let report := str r "report"
return s!"{english}\n {formula.compress}\n {str r "verdict"}" ++ (if report.isEmpty then "" else s!": {report}")
def main : IO Unit := do
let order ← IO.FS.readFile "examples/sessions/PlaceOrder.leanfm"
let withOrder (more : List (String × Json) := []) : Json := withLean order more
let ping ← IO.FS.readFile "examples/sessions/Ping.leanfm"
IO.FS.createDirAll out
-- Chapter: the protocol is the program
write "lang-placeorder.tex" (listing order)
for theme in ["light", "dark"] do
let d ← tool "session_diagram" (diagram (withOrder [("theme", Json.str theme)]))
write s!"lang-placeorder-{theme}.svg" (str d "svg")
-- Chapter: what a protocol means
let c ← tool "session_check" (check (withOrder))
let proj := c.getObjValD "projections"
let locals := ["Client", "Shop", "Bank"].map fun r => s!"-- {r}\n" ++ str (proj.getObjValD r) "local"
write "lang-placeorder-locals.tex" (listing ("\n\n".intercalate locals))
write "lang-placeorder-typestate.tex" (listing (str (proj.getObjValD "Client") "typestate"))
write "lang-placeorder-calls.tex" (listing (str c "callsText"))
write "lang-placeorder-check.tex" (listing (str c "reading"))
let race := "protocol Race (Client, Shop) {\n choice {\n Cancel from Client to Shop;\n } or {\n Ship from Shop to Client;\n }\n}"
let blind := "protocol Blind (Client, Shop, Bank) {\n h: Hold from Client to Bank;\n choice {\n Capture from Shop to Bank;\n Ship from Shop to Client;\n } or {\n SoldOut from Shop to Client;\n }\n}"
let issuesOf : String → IO String := fun t => do
let r ← tool "session_check" (check (withLean t))
pure ("\n".intercalate (strs r "issues"))
write "lang-race.tex" (listing race)
write "lang-race-issues.tex" (listing (← issuesOf race))
write "lang-blind.tex" (listing blind)
write "lang-blind-issues.tex" (listing (← issuesOf blind))
let unanswered := "protocol Lookup (Browser, Api) {\n q: GetUser from Browser to Api route \"GET /users/{id}\";\n}"
let rv ← tool "session_review" (review (withLean unanswered))
write "lang-review.tex" (listing unanswered)
write "lang-review-report.tex" (listing (str rv "report"))
-- Chapter: assumptions and promises
write "lang-ping.tex" (listing ping)
for theme in ["light", "dark"] do
let m ← tool "session_mdp" (mdp (withLean ping [("theme", Json.str theme)]))
write s!"lang-ping-tree-{theme}.svg" (str m "svg")
let s ← tool "session_sla" (sla (withLean ping))
write "lang-ping-sla.tex" (listing (str s "report"))
-- Chapter: questions you ask a protocol
let rows ← [ ("Every session ends.", json% {inevitably: "stop"})
, ("A receipt is possible.", json% {possibly: {on: "Receipt"}})
, ("Never charge after sold out.", json% {always: {implies: [{on: "SoldOut"}, {always: {not: {on: "Charge"}}}]}})
, ("Every charge ends in a receipt.", json% {always: {implies: [{on: "Charge"}, {inevitably: {on: "Receipt"}}]}}) ].mapM
fun (e, f) => ctlRow order e f
write "lang-ctl.tex" (listing ("\n\n".intercalate rows))
let newer := order.replace " SoldOut from Shop to Client cause o status 409;" " SoldOut from Shop to Client cause o status 409;\n } or {\n Backorder from Shop to Client cause o status 202;"
let cp ← tool "session_compat" (compat (withLean newer [("old", Json.str order)]))
write "lang-compat.tex" (listing ("\n".intercalate ([str cp "reading"] ++ strs cp "issues")))
-- Chapter: an execution is an event stream
-- the first seed whose single session ends in a receipt, so the run shows the whole conversation
let runs ← (List.range 40).mapM fun i => do
let sim ← tool "session_simulate" (simulate (withOrder [("seed", toJson (i + 1)), ("instances", toJson 1)]))
pure (i + 1, match sim.getObjValD "events" with | .arr a => a.toList | _ => [])
let (seed, evs) := (runs.find? fun (_, es) => es.any fun e => str e "label" == "Receipt").getD (runs.headD (1, []))
let line := fun (e : Json) =>
let cause := match e.getObjValD "cause" with | .null => "" | j => s!" cause {j.compress}"
s!"id {(e.getObjValD "id").compress} t={(e.getObjValD "time").compress} ms {str e "label"}: {str e "src"} -> {str e "dst"}{cause}"
write "lang-simulate.tex" (listing ("\n".intercalate (evs.map line)))
-- a log whose shop charges before the client pays
let bad := json% [
{id: "e1", session: "42", «from»: "Client", to: "Shop", label: "CreateOrder"},
{id: "e2", session: "42", «from»: "Shop", to: "Client", label: "Quote", cause: "e1"},
{id: "e3", session: "42", «from»: "Shop", to: "Bank", label: "Charge", cause: "e2"}]
let cf ← tool "session_conform" (conform (withOrder [("events", bad)]))
write "lang-conform-log.tex" (listing ("\n".intercalate ((match bad with | .arr a => a.toList | _ => []).map Json.compress)))
write "lang-conform.tex" (listing ("\n".intercalate ([str cf "reading"] ++ strs cf "issues")))
-- the client pays 12 against a quote of 10: the where condition is run and fails
let wrongAmount := json% [
{id: "e1", session: "7", «from»: "Client", to: "Shop", label: "CreateOrder"},
{id: "e2", session: "7", «from»: "Shop", to: "Client", label: "Quote", cause: "e1", fields: {price: 10}},
{id: "e3", session: "7", «from»: "Client", to: "Shop", label: "Pay", cause: "e2", fields: {amount: 12}}]
let wf ← tool "session_conform" (conform (withOrder [("events", wrongAmount), ("allowIncomplete", Json.bool true)]))
write "lang-where-log.tex" (listing ("\n".intercalate ((match wrongAmount with | .arr a => a.toList | _ => []).map Json.compress)))
write "lang-where.tex" (listing (str wf "reading"))
-- a condition that selects a branch is drawn as that branch's guard
let tier := "structure Order where\n total : Nat\n\nprotocol Tier (Customer, Shop) {\n o: Order from Customer to Shop;\n choice {\n Gold from Shop to Customer cause o where o.total >= 100;\n } or {\n Plain from Shop to Customer cause o where o.total < 100;\n }\n}"
write "lang-tier.tex" (listing tier)
for theme in ["light", "dark"] do
let d ← tool "session_diagram" (diagram (withLean tier [("theme", Json.str theme)]))
write s!"lang-tier-{theme}.svg" (str d "svg")
-- the event envelope, and a session told apart by a key the protocol declares
let envProto ← tool "session_types" (types (withOrder))
let protoLines := (str envProto "proto").splitOn "\n"
write "lang-envelope.tex" (listing ("\n".intercalate ((protoLines.dropWhile (!·.startsWith "message Event")).filter (· != ""))))
let keyed := "structure Ask where\n caller : String\n\nprotocol Keyed (Client, Server) {\n sessions by a.caller {\n a: Ask from Client to Server;\n Reply from Server to Client cause a;\n }\n}"
write "lang-key.tex" (listing keyed)
let keyLog := json% [
{id: "e1", session: "s1", «from»: "Client", to: "Server", label: "Ask", fields: {caller: "alice"}},
{id: "e2", session: "s1", «from»: "Server", to: "Client", label: "Reply", cause: "e1"}]
let kc ← tool "session_conform" (conform (withLean keyed [("events", keyLog)]))
write "lang-key-log.tex" (listing ("\n".intercalate ((match keyLog with | .arr a => a.toList | _ => []).map Json.compress)))
write "lang-key-conform.tex" (listing (str kc "reading"))
-- Part II: what a protocol generates
let ty ← tool "session_types" (types (withOrder))
-- the session type is the source: the .proto, the Lean module and OpenAPI are generated from it
write "lang-types-proto.tex" (listing ("\n".intercalate ((str ty "proto").splitOn "\n" |>.drop 5 |>.take 22)))
let moduleLines := match ty.getObjValD "leanModule" with | .str m => m.splitOn "\n" | _ => []
write "lang-types-lean.tex" (listing ("\n".intercalate ((moduleLines.dropWhile (!·.startsWith "/-- What an event carries")).take 23)))
write "lang-lean-client.tex" (listing ("\n".intercalate (((moduleLines.dropWhile (· != "namespace Client")).takeWhile (· != "end Client")) ++ ["end Client"])))
let api ← tool "session_openapi" (openapi (withOrder [("role", "Shop")]))
-- the shop's actor file, as SessionFiles writes it: actors/shop-openapi.yaml
let orderFiles ← IO.ofExcept (sessionFiles (withOrder))
let shopYaml := (orderFiles.lookup "actors/shop-openapi.yaml").getD (str api "code")
write "lang-openapi.tex" (listing ("\n".intercalate ((shopYaml.splitOn "\n").dropWhile (· != "paths:"))))
write "lang-openapi-schemas.tex" (listing ("\n".intercalate (((shopYaml.splitOn "\n").dropWhile (· != "components:")).takeWhile (· != "info:"))))
let sk ← tool "session_skeleton" (skeleton (withOrder [("language", "typescript")]))
let code := (str sk "code").splitOn "\n"
let start := (code.findIdx? (fun l => (l.splitOn "class Client").length > 1)).getD 0
write "lang-skeleton.tex" (listing ("\n".intercalate ((code.drop start).take 36)))
-- Appendix: every tool the MCP server lists, with its title and its arguments (required ones starred)
let tex := fun (t : String) => t.foldl (fun acc ch => acc ++ match ch with
| '_' => "\\_" | '&' => "\\&" | '#' => "\\#" | '%' => "\\%" | '{' => "\\{" | '}' => "\\}" | ch => ch.toString) ""
let rowsT := LeanFM.MCP.tools.toList.map fun t =>
let req := match (t.getObjValD "inputSchema").getObjValD "required" with
| .arr a => a.toList.filterMap fun (x : Json) => x.getStr?.toOption | _ => []
let args := match (t.getObjValD "inputSchema").getObjValD "properties" with
| .obj kvs => kvs.toList.map fun (k, _) => if req.contains k then k ++ "*" else k | _ => []
s!"\\texttt\{{(tex (str t "name")).replace "\\_" "\\_\\allowbreak{}"}} & {tex (str t "title")} & \\texttt\{{tex (", ".intercalate args)}} \\\\"
write "lang-mcp-tools.tex" ("\n".intercalate (["\\begin{tabular}{@{}p{.25\\textwidth}p{.33\\textwidth}p{.36\\textwidth}@{}}",
"\\toprule", "Tool & What it does & Arguments (* required) \\\\", "\\midrule"] ++ rowsT ++ ["\\bottomrule", "\\end{tabular}"]) ++ "\n")
write "lang-provenance.txt" <| "\n".intercalate
[ "prompt: the book could use some very major simplifications. this whole software engineering method is about the leanFM language."
, "question: what does a LeanFM protocol say, what does it mean, and what is generated from it?"
, "eventSource: examples/sessions/PlaceOrder.leanfm and examples/sessions/Ping.leanfm, plus the small protocols and logs written in LanguageChapterExample.lean"
, "reducer: LeanFM.SessionTools (check, diagram, review, mdp, sla, ctl, compat, simulate (the first seed from 1 whose session ends in a receipt), conform, openapi, skeleton): the functions the MCP tools call"
, "unit: messages, sessions, events (ms), probabilities as exact fractions"
, "boundary: one session of each protocol, except simulate (one instance) and sla (per entry)"
, "generator: LanguageChapterExample.lean, then scripts/svg_to_pdf.py for the diagrams" ] ++ "\n"
write "lang-simulate-seed.tex" s!"{seed}"
IO.println s!"wrote {out}lang-*: {evs.length} simulated events (seed {seed}); compat {str cp "verdict"}"
end LanguageChapterExample
def main : IO Unit := LanguageChapterExample.main