The meta phase and deriving¶
This page states what the meta phase computes and what its checks guarantee. The surface is described in the reference; here the phase is a function from a parsed module to a run-time module.
Two phases¶
Write \(M\) for a parsed module and split its statements into the meta part \(M_1\) (meta declarations, static assertions, splices, deriving requests, meta imports) and the run-time part \(M_0\). Let \(D(M_0)\) be the index sorts and families of \(M_0\) that mention only builtin types and such data, closed under that condition. The meta module is
where \(L\) is the meta-phase library (its prelude and built-in handlers), \(M_1^{\mathrm{decl}}\) the
meta declarations and meta-imported declarations, and \(W(M_1)\) one computation per assertion,
splice, and deriving request. \(\mathcal{M}(M)\) is checked by the ordinary checker, and its
computations are evaluated by the reference machine. The phase rule is that no name of
\(M_1^{\mathrm{decl}}\) occurs in \(M_0\); a violation is refused with qiec-phase. Meta code may read
the data \(D(M_0)\), so phase one sees phase zero's data but no phase-zero computation, and phase zero
sees nothing of phase one: this is the staging discipline of multi-stage programming without
cross-stage persistence of computations (Taha and Sheard, 2000).
Assertions and splices¶
A static assertion \(a\) denotes the value \(\llbracket a \rrbracket \in \{\mathsf{true}, \mathsf{false}\}\) of its condition on the reference machine; the module checks only if every assertion denotes \(\mathsf{true}\). A splice of generator \(g\) at arguments \(\vec{v}\) denotes the declarations read back from \(\llbracket g(\vec{v}) \rrbracket\), a value of the closed syntax families. The run-time module is
with each generated declaration placed at its splice site. Quoted syntax is typed by its syntactic category, which the closed families enforce, and a generated declaration is typed by checking \(\mathcal{E}(M)\), so it is held to the same standard as hand-written code (Xie et al., 2022b). A generated declaration that is identical to one generated before it is one declaration; two generated declarations of one name that differ are refused.
Every meta computation is pure at its evaluation boundary, except that it may refuse through the
prelude's refusal instance, which the evaluation handles into an outcome; a refusal fails the
check at the request. Evaluation is bounded by a step budget, so the meta phase always terminates,
possibly by failing the check.
Provenance¶
A generated declaration \(d\) produced by generator \(g\) has the provenance digest
where \(\mathrm{reach}(g)\) is the set of module meta declarations \(g\) reaches through calls,
\(\mathrm{src}\) renders syntax without positions or comments, and the inputs are the generator's
arguments or the family description and the class's arguments. \(h(d)\) changes exactly when a meta
input or \(d\) changes, up to the collisions of the hash \(H\), which is the content-hash domain's
tagged_digest of kind meta-provenance: BLAKE3 over the canonical JSON of the four parts. The provenance stands beside a
declaration's content hash, which identifies its meaning only, so a derived
declaration and an identical hand-written one hash alike.
Descriptions and derived operations¶
A family \(F\) with parameters \(\Delta\) and indices \(\Theta\) is a sum, over its constructors \(c\), of products of field types over \(c\)'s own telescope:
a polynomial functor whose initial algebra is \(F\). Its description is the code of that functor as meta-phase data (Chapman et al., 2010; de Vries and Löh, 2014). A deriving handler is a meta computation from descriptions to declarations, and each built-in class is a generic interpreter of the code (Magalhães et al., 2010):
Eqis the least relation that relates two values built by the same constructor from related fields, with the field types' own equalities at the leaves;Ordis the lexicographic order on (constructor position, fields), with the field types' orders;Hashis a fold with the algebra \(h \mapsto 31h + h_i \bmod (2^{31} - 1)\) seeded by the constructor's position, so \(x = y\) implies equal hashes;Foldis the catamorphism \((\!|\alpha|\!)\): for an algebra \(\alpha = [\alpha_c]_c\) on a carrier \(R\), the unique map with \((\!|\alpha|\!)(c(\vec{x})) = \alpha_c(\vec{x}')\), where \(x'_i\) is \((\!|\alpha|\!)(x_i)\) for a recursive field and \(x_i\) otherwise (Meijer et al., 1991);MapandTraverseare the functorial and traversable actions on the last type parameter,Traversesequencing the effects of its function in field order;Quote[T via q]is the catamorphism whose algebra is the interpretation of a signature morphism \(q\) in \(T\): each clause \(q(c) = t_c\) is a term of \(T\) over \(c\)'s field names, and \(\mathrm{quote}(c(\vec{x})) = t_c[\vec{x}'/\vec{\mathrm{fields}}]\). Being a catamorphism, it is the unique such map.
Indices enter derived operations heterogeneously: a binary method takes its operands at separate copies of the family's indices, so its definition needs no reasoning about index equality and agrees with the homogeneous one where the indices coincide. A parametric family's instance takes the method at each type parameter as a function argument, which the first-order boundary specializes away before code generation.
Naturality along an inclusion follows from uniqueness: if \(\iota : F \to F'\) includes a core family's constructors into an extension's, and \(q' \circ \iota = q\) clause by clause, then \(\mathrm{quote}_{F'} \circ \iota = \mathrm{quote}_F\), since both sides are catamorphisms out of \(F\) with the same algebra. A deduction extension that preserves its core's clauses therefore preserves its core's reification.
What the checks refuse¶
| Check | Refuses |
|---|---|
| phase rule | a run-time reference to a meta declaration or meta-imported name |
| library names | a module declaration that reuses a meta-prelude name in a module with a meta phase |
| static assertion | a false or non-Boolean condition |
| splice | a generator that is not a meta computation, returns something other than declarations, or refuses |
| deriving | a class that names no handler, a family that does not exist, a field whose type has no instance of the class (naming field and instance), a morphism missing a constructor or naming a foreign one, Map on a family without type parameters, Arbitrary on an indexed family or one with no non-recursive constructor |
| generated code | every check of the run-time module, reported at the splice with the generator named |
Cited sources¶
Walid Taha and Tim Sheard. MetaML and Multi-Stage Programming with Explicit Annotations. Theoretical Computer Science, 248(1–2):211–242, 2000. doi:10.1016/S0304-3975(00)00053-0.
Ningning Xie, Matthew Pickering, Andres Löh, Nicolas Wu, Jeremy Yallop, and Meng Wang. Staging with Class: A Specification for Typed Template Haskell. Proceedings of the ACM on Programming Languages, 6(POPL):1–30, 2022b. doi:10.1145/3498723.
James Chapman, Pierre-Évariste Dagand, Conor McBride, and Peter Morris. The Gentle Art of Levitation. In Proceedings of the 15th ACM SIGPLAN International Conference on Functional Programming, 3–14. 2010. doi:10.1145/1863543.1863547.
Edsko de Vries and Andres Löh. True Sums of Products. In Proceedings of the 10th ACM SIGPLAN Workshop on Generic Programming, 83–94. 2014. doi:10.1145/2633628.2633634.
José Pedro Magalhães, Atze Dijkstra, Johan Jeuring, and Andres Löh. A Generic Deriving Mechanism for Haskell. In Proceedings of the Third ACM Haskell Symposium on Haskell, 37–48. 2010. doi:10.1145/1863523.1863529.
Erik Meijer, Maarten Fokkinga, and Ross Paterson. Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. In Functional Programming Languages and Computer Architecture (FPCA), Lecture Notes in Computer Science, 124–144. Springer, 1991. doi:10.1007/3540543961_7.