Signatures, functors, and their static interpretation¶
This page states what signatures, functors, sealing, sharing, and module parameters mean, how they are checked, and why removing them before code generation preserves what was checked. The surface is described in the reference; modules without parameters are described in modules and static interpretation, whose notation this page extends.
Module identities¶
The identity of a module records how the module comes to exist:
a source module (its path \(p\)), a module \(x\) declared in \(\iota\), the parameter \(x\) of the functor \(\iota\), an applicative application, a generative application at the site \(s\), and a sealing at the site \(s\). A symbol is a pair \((\iota, x)\) as before, and an identity instance \(\sigma(\iota)\) replaces the parameters \(\iota_F\#x\) of a functor \(\iota_F\) by arguments and \(\iota_F\) itself by an application. The flat name \(\phi_\rho(\iota, x)\) of the modules page is unchanged; since the display of identities is injective, so is \(\phi_\rho\) on symbols of distinct identities.
Signatures as existentials¶
Following F-ing modules (Rossberg et al., 2014), a signature denotes an existential over its abstract families. For a signature \(S\) with abstract families \(\overline{\alpha}\) and the remaining items \(\Sigma\) (computations with their types and effect rows, manifest families, effects, instances, effect laws, objects, records, index sorts, deduction headers),
A family equal to a type, family T : Type = τ, and a sharing constraint with family T = τ are
not quantified: they substitute \(\tau\) for \(T\). So S with family T = τ denotes
\(\exists \overline{\alpha} \setminus T.\, \Sigma[\tau/T]\), and sharing instantiates the
existential, as in (Leroy, 1994).
QIEC has no existential types, and it needs none: modules are second class, so every existential is opened at a known place and either kept abstract (to check) or instantiated (to run). Its families are nominal, which makes a family declared fresh an exact stand-in for a quantified type variable: a fresh family equals itself and nothing else. The two programs below use this.
Matching is a coercion¶
A module \(M\) with exports \(E\) matches \(S\) when there is a realization \(\theta\), a family of \(M\) (or a type) for each abstract family of \(S\), such that \(E\) is a subtype of \(\Sigma[\theta]\); the coercion that witnesses the subtyping is what F-ing elaborates ascription to. Elaboration computes \(\theta\) by name (\(\theta(\alpha)\) is \(M\)'s export named \(\alpha\)) and checks each item:
- an abstract family exists with the specified parameters and indices;
- a family equal to \(\tau\), and a sharing constraint \(T = \tau\), holds when the realization of \(T\) and \(\tau\) are the same type, both read with every abstract family kept abstract;
- a manifest item is the same declaration in both, read under \(\theta\);
- a computation item \(f : \forall \overline{\beta}.\, \overline{A} \to B \,!\{\varepsilon\}\) is witnessed by the generated computation
which the QIEC checker checks with the rest of the program. It checks exactly when \(M.f\) can be called at the specified type and its effects are among the specified ones, which is the subtyping of the computation item: \(w_f\) is the coercion.
Transparent ascription M : S keeps \(M\)'s identity and exports only \(S\)'s items, so the realization
stays visible (Leroy, 1995). Sealing M :> S is the coercion followed by packing: the
result is a new identity \((\iota_M :> S)\#s\) whose abstract families are known only through \(S\).
Two programs¶
Static interpretation produces two programs from one module graph.
- The run-time program names every declaration of every instance by its flat name and every item of a sealed module or of an argument by the declaration that realizes it. It is the program that is compiled and run.
- The checking program is the run-time program with three changes. The items of a functor's
parameter \(\iota_F\#x\) and of a sealing \((\iota :> S)\#s\) are skolems: fresh declarations
stated by the signature alone. An abstract family is a fresh family with one constructor no
source can name (so a value of it can be passed on, and a
caseon it never covers it); a computation is a postulate,define f(x̄) : B !{ε} = f(x̄), which has its specified type and is never run; a manifest item is its declaration; a family equal to a type or constrained by sharing is that type. Each functor's body, and each computation with module parameters, is included once at its own parameters, \(\sigma(\iota_F\#x) = \iota_F\#x\). And every match adds its witnesses.
Compiling the checking program checks generic code against its parameters' signatures alone, client code against sealed signatures alone, and every match.
Functors and the purity rule¶
A functor \(F(x_1 : S_1, \ldots, x_n : S_n) = B\) denotes a function from the parameters'
existentials to the body's. F-ing distinguishes pure functors, whose result's abstract types are
determined by the arguments (\(\exists \overline{\alpha}.\, \forall \ldots\), applicative), from
impure ones, which create fresh abstract types on each application (\(\forall \ldots \exists
\overline{\alpha}\), generative). QVR reads the distinction off effects: instantiating the body
allocates when one of its declarations is a computation whose effect row holds a Random or a
Param instance, a program, or a declaration that owns learnable parameters. A functor whose body
allocates nothing is applicative, and \(F(\iota_1, \ldots, \iota_n)\) is one identity wherever it is
written, so its families are equal across applications. A functor whose body allocates is
generative, and each application at a binding site \(s\) is \(F(\iota_1, \ldots, \iota_n)\#s\), with its
own parameters, sites, and families, unless sharing is declared.
A computation with module parameters is a functor whose body is one declaration, applied at each call. A call \(f(M, \overline{v})\) is a call of \(\phi_\rho(f(\iota_M), f)\), its specialization, with the module arguments removed. Implicit arguments are inserted first: resolution finds, for \(\{x : S\}\), the unique implicit module or implicit functor application that solves \(S\) with its constraints at the call's static arguments, which turns modular implicits into modular explicits (White et al., 2015). A static binder that a constraint mentions is determined by the module argument, so the specialization takes it at that type, and the generic check takes it at a fresh family.
Soundness¶
The checking program and the run-time program differ only in the skolems, the generic instances, and the witnesses. The argument that checking one licenses running the other has three steps.
- Instantiation preserves typing. Let \(B\) be a functor body checked at skolems for its parameters, and \(\theta\) a realization under which every argument matches its parameter's signature. Replacing each skolem family by \(\theta\) and each postulate by the argument's computation preserves typing: a skolem family occurs in \(B\) only where the signature lets it (it has no constructor \(B\) can name and no equation the signature does not state), every manifest item and sharing constraint holds of \(\theta\) by the match, and every postulate is replaced by a computation that the witness shows has a subtype of the postulate's type. This is the substitution lemma of F-ing's elaboration, restricted to second-class modules, where every quantifier is instantiated by a known module.
- Matches are checked where they occur. Every application, ascription, sealing, and implicit resolution adds its match, so every realization \(\theta\) used by the run-time program has been checked.
- Abstraction is respected. Client code of a sealed module is checked against the sealing's skolems, so it is well typed for any realization of the sealed signature, not just the one the run-time program uses. By parametricity for modules [@crary2017modules; @sterling2021logical], a client cannot distinguish two implementations of a signature that are related at it, so replacing a sealed module by another implementation of its signature does not change what the client computes.
Together these give the guarantee the module layer makes: if the checking program compiles, the run-time program compiles and means the generic code applied to the arguments it names. The checks are sound and complete for what they decide by name and by structure (missing, mismatched, and manifest items; sharing constraints), and the witnesses inherit the QIEC checker's soundness. Two things are not checked here. Effect laws stated in a signature are local theories of the instance, checked for every handler installed for it (effects); and a deduction stated by its header is matched by its header alone, so generic code can use it only through items the signature declares.
Generativity and identity¶
Two applications of a generative functor are distinct identities, so their declarations have
distinct flat names and their learnable parameters are distinct, and their abstract families are
distinct families: a value of one is refused where the other's is expected. Two applications of an
applicative functor at the same arguments are one identity, so they have one set of declarations
and equal families. Sample sites and parameter names are qualified by these identities, so the
parameters two generative instances allocate are kept apart by name as well; their rename-stable
identities and the share declarations that tie them are in linking and sites.
Cited sources¶
Andreas Rossberg, Claudio V. Russo, and Derek Dreyer. F-ing Modules. Journal of Functional Programming, 24(5):529–607, 2014. doi:10.1017/S0956796814000264.
Xavier Leroy. Manifest Types, Modules, and Separate Compilation. In Proceedings of the 21st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 109–122. 1994. doi:10.1145/174675.176926.
Xavier Leroy. Applicative Functors and Fully Transparent Higher-Order Modules. In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 142–153. 1995. doi:10.1145/199448.199476.
Leo White, Frédéric Bour, and Jeremy Yallop. Modular Implicits. Electronic Proceedings in Theoretical Computer Science, 198:22–63, 2015. doi:10.4204/EPTCS.198.2.