Holes, linking, and sites¶
Functors make every varying part an explicit parameter, and every module that depends on it must take it and pass it on. A hole makes the varying part a parameter of the whole package instead: the package's modules import it like any module, the package is checked once with the hole abstract, and linking fills it [@kilpatrick2014backpack; @yang2017backpack]. Sample sites and learned parameters, which modules and their instances multiply, are addressed by module paths and identified by content, so a stored run stays readable after a rename. The rules are in the semantics of linking and sites.
Holes¶
hole P : S declares the module path P a hole of the package with signature S. S is a
signature declared in the same module or in a module it
imports (L.S, or S brought in by import L (S)). Any module of the package then imports P.
The files below are one package, and each is checked as qvr check checks it:
export signature WorldModel
define weight(n : Int) : Real !{}
define bias() : Real !{}
export signature Lexicon
define base() : Real !{}
hole Worlds : WorldModel
hole Lex : Lexicon
import sigs
import Worlds
export define score(n : Int) : Real !{} =
let w <- Worlds.weight(n)
let b <- Worlds.bias()
return w * 2.0 + b
A hole is one parameter of the whole package, not of each module that imports it: every importer
sees the same abstract items. The package checks without a filling. Its checking program declares
the hole's abstract families and computations once (as ?Worlds:weight and so on) and checks
every module against them; check_module_language compiles
it, and qvr check reports what does not check against the signature. A program that needs a hole
no link fills is not compiled for running (hole-unfilled); its
FlatProgram.unfilled lists the holes.
hole statements may stand anywhere in a module. A path declared a hole in one module is a hole
for the whole module graph. Two hole statements of one path must name one signature
(hole-conflict), and a signature that is not found is refused with near misses
(hole-signature).
Linking¶
link P = M fills the hole P with the module M, a module that matches the hole's signature:
export define weight(n : Int) : Real !{} =
return 1.0
export define bias() : Real !{} =
return 0.0
import discourse
link Worlds = worlds.boolean
define run(n : Int) : Real !{} =
let s <- discourse.score(n)
return s
Every module that needs Worlds (directly or through what it imports) is then instantiated with
the filling: discourse becomes discourse(worlds.boolean), and its declarations are named
discourse(worlds.boolean):score in the flat program. A module that needs no hole is shared by
every filling, and two modules with the same filling are one module, so each distinct filling
compiles once. The root keeps its bare names.
A caller can link holes too: load_graph and the interpret_*
entry points take links={"Worlds": "worlds.boolean"}, which a package manifest or an experiment
supplies. Links from statements and from the caller are one set.
Modules with holes can be used as mixins. A module can provide definitions for other modules and import definitions that the package will link later.
The filler must match the hole's signature. A missing item is refused at link time and a mistyped
one by the checking program, each naming the item (signature-mismatch). Linking a path that is
no hole is refused with the package's holes (link-unknown-hole), as is a filler that is itself a
hole; linking one hole to two modules is refused (link-conflict).
Recursive links¶
A filler may itself need holes, so links may be mutually recursive (Rossberg and Dreyer, 2013). The
filler of Worlds below uses the hole Lex, and the filler of Lex uses the hole Worlds:
import sigs
import Lex
export define weight(n : Int) : Real !{} =
let b <- Lex.base()
return b * 3.0
export define bias() : Real !{} =
return 0.0
import sigs
import Worlds
export define base() : Real !{} =
return 0.5
export define scaled(n : Int) : Real !{} =
let w <- Worlds.weight(n)
return w + 1.0
A root that links both holes instantiates each module with both fillings:
import discourse
link Worlds = worlds.recursive
link Lex = lexicon.core
define run(n : Int) : Real !{} =
let s <- discourse.score(n)
return s
The link is accepted because it is well founded: no declaration depends on
itself through declarations of other modules (weight reads base, and scaled reads weight).
Had base read Worlds.weight, the link would be refused with the cycle's path (link-recursion):
the link is not well founded: lexicon.core.base -> worlds.recursive.weight -> lexicon.core.base
defines a declaration in terms of itself across modules (through the holes Lex, Worlds)
Sites and parameters across instances¶
A functor whose body draws or learns is generative: each
application allocates its own sample sites and learned parameters. share x with y ties them:
object Obs : FinSet 4
signature Prior
define center() : Real !{}
module Zero : Prior
export define center() : Real !{} =
return 0.0
module Model(P : Prior)
export program fit(scale : Real) : Obs -> Obs
let prior <- sample Normal(loc=0.0, scale=1.0)
let theta <- sample Normal(loc=prior, scale=scale)
return theta
module A = Model(Zero)
module B = Model(Zero)
share B.fit.prior with A.fit.prior
program joint : Obs -> Obs
let a <- program A.fit(0.5)
let b <- program B.fit(0.5)
observe y : Obs <- Normal(loc=a, scale=0.5)
return b
export joint
Without the share, joint has two priors, a$prior and b$prior. With it, B.fit.prior is
A.fit.prior: joint draws it once and both instances read it, on the reference machine, in the
compiled runtime, and in every transpile target. A side of a share is a sample site
(A.fit.prior), a whole program (every sample site, matched by label), or a deduction, whose
learned parameters are shared as a whole (share Two.G with One.G). Two sites are one only when
they are drawn from one family over one plate and axes, so they have one type and one support; two
deductions share only when they declare the same productions. A mismatch is refused naming both
sides (qiec-share).
Site addresses, identities, and run records¶
site_table describes the sites a program declares.
| address | the declaring module (with its instantiation), the declaration, and the label: main.Model(main.Zero)#main.A.fit.prior |
| identity | a digest of the declaring declaration's content hash, its ordinal among equal declarations (which tells generative instances apart), and the site's ordinal among the declaration's names, so no spelling enters it |
| key | what a run reads: the label a run conditions on, or a parameter's spelled name |
| target | the transpiled identifier, by the injective spelling of target_identifier |
qvr run --json writes the table as "sites" beside the values it conditioned on
("site_values"), so the record carries the reverse map from target spellings to addresses.
qvr run --replay RECORD conditions a program on a stored record's values, matched by identity:
renaming a module, a declaration, or a site leaves the record readable, while a record of a
declaration whose content changed is refused naming the site (qiec-run-site).
--site takes a site key or a path pattern. Within a segment * matches any characters, so
Discourse(*) matches every instantiation of Discourse; ** matches any number of segments; and
a pattern is matched against the end of an address, so fit.prior selects prior of every fit.
A draw reached inside a checked computation takes its runtime label or label@n for a single
occurrence. Replay reuses that label, and the runtime checks that it reaches the requested draw.
SiteTable.select and
SiteTable.resolve give guides and inference
configuration the same addressing.
Targets admit only plain identifiers, so a qualified name is spelled injectively: a . between
two names is __, a : is ___, a $ is ____, a run of one to three underscores at either end
stays, and any other run of punctuation is escaped between five underscores (G.Item is
G__Item, lib.arith:shift is lib__arith___shift, theta$z is theta____z).
source_name reads a spelling back.
Cited sources¶
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.