quilt/langs/lean/meta.rs
1//! The Lean meta-language: Lean as a *host* that drives generation.
2//!
3//! A `.lean.quilt` file is a Lean 4 metaprogram. Where the Rust/Python hosts
4//! emit builder calls into a `QTerm` runtime, this host has **no runtime
5//! library**: it represents generated code as plain Lean strings (see
6//! [`super::ops`]). A quote `↖ … ↗` expands to an interpolated string literal
7//! `s!" … "` and a host unquote `↙x↘` to Lean's own `{x}` interpolation, so
8//! evaluating the expanded program (e.g. `#eval`, or `lean file.lean`) yields
9//! the generated code as a `String`. See issue #132 for the rationale and the
10//! path to a real `QTerm` runtime.
11//!
12//! Because everything is a string, a Lean host can generate *any* target
13//! language — `lean↖…↗`, `rs↖…↗`, `wgsl↖…↗` all reconstruct the same way.
14
15use miette::Result;
16
17use super::ops::{build_quote_str, build_str_code, build_unquote_str};
18use crate::lang::Arity;
19use crate::meta::OuterKind;
20use crate::prelude::{Index, *};
21use crate::{meta::MetaLanguage, qterm::QTerm, term::CmdOrHole};
22
23/**************************************************************/
24
25#[derive(Default)]
26pub struct LeanMetaLanguage;
27
28impl MetaLanguage for LeanMetaLanguage {
29 fn expand_tuple(
30 &self,
31 _lang1: &str,
32 _tag: &str,
33 qterms: &[Arc<QTerm>],
34 cmds: &[CmdOrHole],
35 _arity: Arity,
36 ) -> Result<Arc<QTerm>> {
37 // Strings have no builder/accumulator, so variadic and fixed nodes
38 // reconstruct identically: literal text plus spliced children.
39 Ok(build_str_code(cmds, qterms))
40 }
41
42 fn expand_quote(
43 &self,
44 _lang1: &str,
45 _tag: &str,
46 _i: Index,
47 lang2: &str,
48 qterm: &Arc<QTerm>,
49 _cmds: &[CmdOrHole],
50 ) -> Result<Arc<QTerm>> {
51 Ok(build_quote_str(lang2, qterm))
52 }
53
54 fn expand_unquote(
55 &self,
56 _lang1: &str,
57 _tag: &str,
58 _i: Index,
59 lang2: &str,
60 qterm: &Arc<QTerm>,
61 _cmds: &[CmdOrHole],
62 ) -> Result<Arc<QTerm>> {
63 Ok(build_unquote_str(lang2, qterm))
64 }
65
66 /// Identity: the string model has no `b_` accumulator to emit/splice into,
67 /// so a child is woven into its parent purely by `expand_tuple`'s
68 /// concatenation. Emit/splice in *ground* loops is therefore unsupported —
69 /// build sequences functionally instead (`List.map`, `String.intercalate`).
70 fn wrap_child(&self, qterm: Arc<QTerm>, _okind: OuterKind) -> Result<Arc<QTerm>> {
71 Ok(qterm)
72 }
73
74 /// `↑` renders a host value as text for interpolation. There is no runtime
75 /// `QTerm`, so the only spelling is Lean's own `toString`; cross-language
76 /// lifts have no spelling.
77 ///
78 /// (Inside a splice Lean already applies `ToString`, so `↑` is only needed
79 /// where a value is used as a `String` in host position.)
80 fn lift_str(&self, target: &str) -> Result<&'static str> {
81 match target {
82 // `lean4` is a registered alias of `lean` (see `langs/omni.rs`), so
83 // it has to be accepted here too — otherwise the homogeneous lift
84 // works in a `lean↖…↗` quote and fails in a `lean4↖…↗` one.
85 "" | "lean" | "lean4" => Ok("toString"),
86 _ => miette::bail!("lean can't lift into {target:?}: only homogeneous `toString`"),
87 }
88 }
89
90 /// No spelling: `←` needs a `b_` accumulator to emit into, which the string
91 /// model doesn't have (see [`Self::wrap_child`]). Fail here rather than let
92 /// the `__EMIT__` placeholder leak into the generated Lean.
93 fn emit_str(&self) -> Result<&'static str> {
94 miette::bail!(
95 "lean can't emit `←`: the string-based meta has no `b_` accumulator to emit into — \
96 build sequences functionally instead (`List.map` + `String.intercalate`)"
97 )
98 }
99
100 /// No spelling: `↓` compiles a term and deserializes the result back, which
101 /// needs the `QTerm` runtime this host doesn't have. Generation-time
102 /// evaluation is ordinary Lean — run it outside the quote and splice the
103 /// value with `↙…↘`.
104 fn reduce_str(&self, target: &str) -> Result<&'static str> {
105 miette::bail!(
106 "lean can't reduce `{target}↓`: the string-based meta has no `QTerm` runtime to \
107 evaluate a fragment — compute the value in ordinary Lean and splice it with `↙…↘`"
108 )
109 }
110
111 /// A generated fragment *is* a `String` here, so that is the type `⟨T⟩`
112 /// names — the annotation `examples/lean_host.lean.quilt` writes by hand.
113 fn type_str(&self) -> Result<&'static str> {
114 Ok("String")
115 }
116
117 /// In the string model a name is its own text, so `⟨N⟩` is Lean's identity.
118 fn name_str(&self) -> Result<&'static str> {
119 Ok("id")
120 }
121}