Skip to content

Linking and sites

This page states what holes and links mean, when a linked program is well founded, how sample sites and learned parameters are addressed and identified across module instances, and why the target spelling of qualified names is injective. The surface is described in the reference; the identities and the two programs are those of the semantics of functors.

Holes are parameters of the package

A hole \(h\) with signature \(S_h\) is a module identity of its own, written \(?h\). A module \(m\) that imports \(h\) resolves \(h.x\) to the symbol \((?h, x)\) exactly as it resolves a functor parameter's items, through the interface of \(S_h\). Unlike a functor parameter, \(?h\) belongs to no functor: every module of the graph that imports \(h\) names the same identity, so a value of \(h\)'s abstract family that one module produces is the value another module expects.

The needs of a module are the least sets with

\[ \mathrm{needs}(m) \;=\; \{\, h \mid m \text{ imports } h \,\} \;\cup\; \bigcup_{m \text{ imports } m'} \mathrm{needs}(m') \;\cup\; \bigcup_{m \text{ imports } h,\ \lambda(h) = f} \mathrm{needs}(f) \]

where \(\lambda\) is the link map, a partial function from holes to modules. Read through the semantics of functors, a module with needs \(h_1, \ldots, h_k\) (sorted by path) is a functor of those holes, and the package is checked as generic code is: once, in the checking program, where every \(?h\) has fresh declarations of the items of \(S_h\) (a skolem per abstract family, a postulate per computation). Since the needs are fixed by the imports, checking the package does not depend on any filling; this is the sense in which the package checks once (Kilpatrick et al., 2014).

Linking builds identities

A link map \(\lambda\) that is defined on every hole the root needs determines the run-time program. Each module \(m\) has the linked identity

\[ \llbracket m \rrbracket_\lambda \;=\; \begin{cases} m & \mathrm{needs}(m) = \emptyset \\ m(\lambda(h_1), \ldots, \lambda(h_k)) & \mathrm{needs}(m) = \{h_1, \ldots, h_k\} \end{cases} \]

an applicative application in the notation of module identities, whose arguments are the fillers' source identities. Static interpretation renders each linked identity once, with the substitution that maps \(?h\) to \(\llbracket \lambda(h) \rrbracket_\lambda\) and every module \(m'\) with needs to \(\llbracket m' \rrbracket_\lambda\). Two modules with equal needs and equal fillings have one identity, so each distinct filling compiles once; this is the unification of module identities that Backpack's linking performs (Yang, 2017). The root is rendered under its own bare names, since a program has one instance of it. The arguments name source identities rather than linked ones, so an identity is finite even when \(\lambda\) is recursive: \(\lambda\) restricted to the needs of \(m\) already says which instance \(m\) is.

A filling \(\lambda(h) = f\) is accepted only if \(f\) matches \(S_h\). Matching is the coercion of the semantics of functors: items that are missing are refused at once, and every value item is witnessed by generated code in the checking program, so a mistyped item is refused when that program is compiled. Because the package was checked against \(S_h\) alone and the filler matches \(S_h\), every module of the linked program is well typed: abstraction safety for functor arguments carries over to fillings (Rossberg et al., 2014).

The import graph is acyclic, but the graph whose edges also include \(h \to \lambda(h)\) need not be: a filler may need a hole whose filler needs the first hole. MixML admits such recursive linking when the program it elaborates to is well founded (Rossberg and Dreyer, 2013). In QVR, a module's declarations are defined by its statements, so the relevant graph is that of declarations: an edge \((m, x) \to (m', y)\) whenever the statement declaring \(x\) in \(m\) refers to \(y\) of \(m'\), where a reference to \((?h, y)\) is read as a reference to the filler's declaration of \(y\). A linked program is well founded when no declaration reaches itself along a path that visits another module. Within one module the language's own rules for recursion apply; across modules, a recursive link adds no recursion that the modules did not have, so every declaration of the linked program is defined from declarations defined before it in some order. A link that is not well founded is refused with the shortest cycle through the first declaration (in path order) that has one, and with the holes the cycle passes through.

Site addresses and identities

A sample site is declared by a program step, or by a site("...") in a computation; a learned parameter by a declaration that owns it (a deduction's learnable lexicon and rules). A site has

  • an address: the declaring module's identity rendered with its instantiation, the declaring declaration, and the site's label. A drawn program's sites keep their place in the drawing program, under the draw's binder: a$prior is the site prior of the program drawn as a;
  • an identity: the digest of the declaring declaration's content hash \(\eta\), the ordinal \(k\) of that declaration among the declarations of the flat program with hash \(\eta\), and the ordinal of the site's label among the declaration's free names (the content hash's free-name interface). Content hashes are invariant under renaming, and ordinals are positions, so no spelling enters an identity. Two generative instances of one functor declare alpha-equivalent programs with one hash \(\eta\); the ordinal \(k\) tells them apart.

A learned parameter's name is a ParameterName: its owner and its member path, both names of the core, which content identity reads as ordinals. Its identity is the digest of the owner's own content hash (a deduction is identified by content, see content identity), the owner's ordinal among owners with that content, and the member path. Renaming a deduction or one of its rules therefore changes no content hash, while changing what a rule derives does.

Sharing

share x with y declares two sites (or two owners) to be one. For sample sites the declaration is a relation \(\approx\) on pairs \((d, \ell)\) of a declaration and a label, and each class has one representative, the end of its chain of ties. Instantiating a drawn program \(d\) in a caller reads a site \((d, \ell)\) from an earlier draw in the same caller whenever that earlier draw is of a different site in the same class: the step is not repeated and the program reads the caller's value. Drawing one program twice still draws its sites twice, since a site is shared only with the sites it is tied to. A tie is accepted only when the two sites are drawn from one family over one plate and axes, so the shared variable has one type and one support. A tied site takes the identity and address of its class's representative, so stored runs of either instance read the same site. For parameters, a tied owner is replaced by the owner it is tied to in every ParameterName it owns, which is only admitted when the two deductions declare the same productions, so every parameter has one counterpart.

Target spelling

A target identifier spells a qualified name \(n\) by reading \(n\) as alternating runs of ASCII letters and digits (kept) and maximal runs \(r\) of other characters, each spelled by its position:

\[ \mathrm{sp}(r) \;=\; \begin{cases} r & r \in \{\texttt{\_}, \texttt{\_\_}, \texttt{\_\_\_}\} \text{ at the start or the end of } n \\ \texttt{\_}^{\,j} & r \text{ between two word runs, } r = \texttt{\_}, \texttt{.}, \texttt{:}, \texttt{\$} \text{ for } j = 1, 2, 3, 4 \\ \texttt{\_}^{5}\, c(r)\, \texttt{\_}^{5} & \text{otherwise} \end{cases} \]

where \(c\) codes each character of \(r\) by a letter (or a bracketed hexadecimal code point). Every underscore run of the result has length \(1, 2, 3, 4\), or \(5\), and the position of the run and its length determine which case produced it; inside an escape, codes are letters and digits and the escape ends at the next run of five. Decoding is therefore a left-to-right scan with no choice, so \(\mathrm{sp}\) is injective and source_name is its inverse on its image. A run record's reverse map is the restriction of that inverse to the program's sites.

Cited sources

Scott Kilpatrick, Derek Dreyer, Simon Peyton Jones, and Simon Marlow. Backpack: Retrofitting Haskell with Interfaces. In Proceedings of the 41st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 19–31. 2014. doi:10.1145/2535838.2535884.

Edward Z. Yang. Backpack: Towards Practical Mix-in Linking in Haskell. PhD thesis, Stanford University, 2017. URL: https://purl.stanford.edu/zf998bz8716.

Andreas Rossberg, Claudio V. Russo, and Derek Dreyer. F-ing Modules. Journal of Functional Programming, 24(5):529–607, 2014. doi:10.1017/S0956796814000264.

Andreas Rossberg and Derek Dreyer. Mixin' Up the ML Module System. ACM Transactions on Programming Languages and Systems, 35(1):1–84, 2013. doi:10.1145/2450136.2450137.