Signatures, functors, and module parameters¶
A module can be generic over another module. A signature states what a module provides, a functor is a module with module parameters, and a computation can take modules as parameters too. Generic code is checked once against its parameters' signatures, so it can be linked with any module that matches them, and every use is specialized before compilation: the reference machine and every transpile target see an ordinary flat program (Rossberg et al., 2014; Elsman et al., 2018). The elaboration and its soundness argument are in the semantics of functors.
Signatures¶
signature S declares a module type. Its items are declarations, and three forms state an item
by its kind or type alone:
| Item | States |
|---|---|
family T : Type |
an abstract family: matching modules choose it, generic code cannot look inside |
family T : Type = Real |
a manifest family: a family that equals a type |
define f(x : T) : U !{e} |
a computation by its type and effect row |
deduction G : A -> B |
a deduction by its header |
family F : Type with constructors, effect, instance, effect_law, object, record, index |
the declaration itself, which a matching module states identically |
signature WorldModel
family World : Type
define pick(n : Int) : World !{}
define holds(n : Int, w : World) : Bool !{}
A signature's items are not exported one by one: a module matches the signature when it exports
all of them. A family without constructors, a computation without a body, or a deduction without
rules outside a signature is refused (signature-item): it specifies an item and declares nothing.
In a module, family T : Type = U names the type U, and every use of T reads U.
Laws¶
An equations block states laws over the signature's abstract families and the computations
between them, in the equation syntax of theories. They form the signature's
theory, and every module that matches the signature is checked as a model of it once the program
is compiled: each law at every assignment when the module's families are finite, with a refused
law named together with the assignment and both sides' values (signature-mismatch). A module
whose families are not finite owes the laws as assumed, which
check_signature_laws reports.
signature Monoid
family T : Type
define unit() : T !{}
define plus(x : T, y : T) : T !{}
equations
left_unit : plus(unit, x) = x
right_unit : plus(x, unit) = x
Structures and ascription¶
module M with an indented body declares a module inside a module: its declarations are private
unless exported, exactly as a file's are, and they are reached as M.name. module M : S
ascribes a signature transparently: the module must match S, it exports only the items of S,
and its families stay what they are. module M :> S seals it: the families S leaves abstract
become abstract outside the module.
signature WorldModel
family World : Type
define pick(n : Int) : World !{}
define holds(n : Int, w : World) : Bool !{}
module Boolean : WorldModel
export family World : Type
constructor Here : World
constructor There : World
export define pick(n : Int) : World !{} =
if n > 0 then
return Here
else
return There
export define holds(n : Int, w : World) : Bool !{} =
case w motive => Bool
Here =>
return n > 2
There =>
return false
module Sealed = Boolean :> WorldModel
define judge(n : Int) : Bool !{} =
let w <- Sealed.pick(n)
Sealed.holds(n, w)
judge passes a world from Sealed.pick to Sealed.holds without knowing what a world is. Writing
Sealed.holds(n, Boolean.Here) instead is refused (module-abstraction): Sealed.World is
abstract, so Boolean.World is not one.
A module matches a signature when it exports every item with the specified kind and:
- an abstract family with the specified parameters and indices;
- a family equal to a type with exactly that type;
- a computation at a type the specified one can call it at, with no effect the specification does not allow (checked by generated code that calls it at the specified type);
- a manifest item with the same declaration (the same constructors, operations, or definition);
- a deduction with the same header.
A mismatch is refused with the missing or mismatched item named (signature-mismatch):
main.qvr:28:13: error[signature-mismatch]: module main.Partial does not match signature main.WorldModel: it exports no computation 'holds'
main.qvr:31:13: error[signature-mismatch]: module main.Wrong does not match signature main.WorldModel: computation 'holds' does not have the specified type and effect row: [qiec-call] call to 'main.Wrong:holds' argument 0: expected Real, got Int
Functors¶
A module with parameters is a functor. Each parameter is named and has a signature; inside the
body, W.holds and W.World name the parameter's items, and nothing else about the argument is
known. F(A, B) applies a functor, and module M = E names a module expression: a path, an
application, or a sealing E :> S.
family Sentence : Type
constructor Fact : Int -> Sentence
constructor Not : Sentence -> Sentence
signature WorldModel
define holds(n : Int) : Bool !{}
module Discourse(W : WorldModel)
export define truth(s : Sentence) : Bool !{} =
case s motive => Bool
Fact(n) =>
W.holds(n)
Not(p) =>
let b <- truth(p)
return not b
module Parity : WorldModel
export define holds(n : Int) : Bool !{} =
let half = n / 2
return 2 * half == n
module Experiment = Discourse(Parity)
define rate(n : Int) : Bool !{} =
Experiment.truth(Not(Fact(n)))
The body is checked once at the signatures of its parameters, whether or not the functor is ever
applied, so a body that relies on more than its parameters' signatures is refused at the functor.
Each argument must match its parameter's signature, with the item and the parameter named when it
does not. A functor cannot be named into before it is applied (module-application).
Sharing¶
S with family T = U refines a signature: its abstract family T must be U. A functor states
that two parameters share a family this way:
signature Grammar
family Entity : Type
define subject() : Entity !{}
signature WorldModel
family Entity : Type
define holds(e : Entity) : Bool !{}
module Discourse(G : Grammar, W : WorldModel with family Entity = G.Entity)
export define subject_holds() : Bool !{} =
let e <- G.subject()
W.holds(e)
An argument for W whose Entity is not the grammar's is refused (sharing-mismatch), naming the
constraint and what the family is instead. Inside the body, W.Entity and G.Entity are one type.
Applicative and generative functors¶
Whether two applications of a functor to the same arguments are the same module depends on the
functor's effects. A functor whose instantiation allocates nothing is applicative: an application
is identified by the functor and its arguments, so Wrap(Ints).Item is one type however often it
is written, and its declarations exist once. A functor that allocates learnable parameters or
sample sites is generative: each application is a module of its own, with its own declarations,
its own parameters, and its own abstract types. A declaration allocates when it is a computation
whose effect row holds an instance of Random or Param, a program, or a declaration that owns
learnable parameters (a morphism that is neither observed nor let, a deduction with learnable
weights or latent lexical categories, an encoder, a decoder).
Flat names¶
Static interpretation names each declaration by the module it belongs to. A module declared in a
module extends its parent's path (main.Boolean:World); an application names its functor and
arguments (main.Discourse(main.Parity):truth), followed by # and the application's binding for
a generative functor (main.Prior(main.Ints)#main.G1:draw). A diagnostic that names an abstract
family of the checking program spells it F#W:World for a functor parameter and
(M:>S)#site:World for a sealed module.
Module parameters of computations¶
A computation can take modules as parameters. module R : S among its parameters binds a module
that each call passes, and R.Item, R.label name its items:
signature Reader
family Item : Type
define label(i : Item) : Int !{}
module Numbers
export family Item : Type
constructor Num : Int -> Item
export define label(i : Item) : Int !{} =
case i motive => Int
Num(n) =>
return n
module Doubles
export family Item : Type
constructor Twice : Int -> Item
export define label(i : Item) : Int !{} =
case i motive => Int
Twice(n) =>
return n + n
define describe(module R : Reader, i : R.Item) : Int !{} =
let n <- R.label(i)
return n + 1
define main(n : Int) : Int !{} =
let a <- describe(Numbers, Numbers.Num(n))
let b <- describe(Doubles, Doubles.Twice(n))
return a + b
describe is checked once at Reader, and each call is a call of its specialization at the module
passed (main.describe(main.Numbers):describe), so no compiled computation takes a module. A call
at the same module reuses one specialization. A value where a module parameter expects a module,
or a module anywhere else, is refused (module-application).
Implicit module parameters¶
{module M : S} binds a module that calls leave out. It is filled by the implicit module in scope
that solves the requirement: the signature, with its sharing constraints read at the call's static
arguments. implicit module I : S ... declares one, and an implicit functor solves a requirement
through its own parameters, which are resolved in turn.
signature Eq
family T : Type
define eq(x : T, y : T) : Bool !{}
family Box[A : Type] : Type
constructor Wrap : A -> Box[A]
implicit module IntEq : Eq with family T = Int
export family T : Type = Int
export define eq(x : Int, y : Int) : Bool !{} =
return x == y
implicit module BoxEq(E : Eq) : Eq with family T = Box[E.T]
export family T : Type = Box[E.T]
export define eq(x : Box[E.T], y : Box[E.T]) : Bool !{} =
case x motive => Bool
Wrap(a) =>
case y motive => Bool
Wrap(b) =>
E.eq(a, b)
define same[A : Type]({module E : Eq with family T = A}, x : A, y : A) : Bool !{} =
E.eq(x, y)
define main(n : Int) : Bool !{} =
let a = (Wrap(n) : Box[Int])
let b = (Wrap(3) : Box[Int])
same[Box[Int]](a, b)
same[Box[Int]] resolves E to BoxEq(IntEq). Resolution is:
- coherent: when two candidates solve a requirement, the call is refused with both named
(
implicit-ambiguous), and when none does, with the requirement named (implicit-missing); - tabled: a requirement met again while it is being solved has no solution along that path, so an instance that requires itself never loops (Selsam et al., 2020);
- scoped by import: the candidates are the implicit modules declared in the calling module and around it, and those exported by the modules it imports directly.
A constraint that mentions a static binder of the computation (with family T = A) is read at the
call's static arguments. A call may leave them out: they are then inferred by unifying each value
parameter's declared type with the type the call states for its argument (a literal, an
annotation, a parameter or typed binding of the enclosing computation, a constructor, or a call),
so same(a, b) with a : Box[Int] resolves as same[Box[Int]](a, b) does. A binder the
arguments leave undetermined is refused by name (implicit-missing). The binder is then
determined by the module, so the specialization takes it at that type [@white2015modular;
@dreyer2007modular].
Diagnostics¶
| Code | Refuses |
|---|---|
signature-mismatch |
a module that lacks an item or has one of another kind or type |
sharing-mismatch |
an argument for which a sharing constraint does not hold |
signature-item |
an item a signature cannot state, or a specification outside a signature |
module-abstraction |
an abstract family of a parameter or a sealed module used as a concrete type |
module-application |
applying a non-functor, the wrong number of arguments, a functor's members before application, a module where a value is expected |
module-recursion |
modules or family aliases defined in terms of themselves |
implicit-ambiguous, implicit-missing |
an implicit module parameter with two solutions, or none |
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.
Martin Elsman, Troels Henriksen, Danil Annenkov, and Cosmin E. Oancea. Static Interpretation of Higher-Order Modules in Futhark: Functional GPU Programming in the Large. Proceedings of the ACM on Programming Languages, 2(ICFP):97:1–97:30, 2018. doi:10.1145/3236792.
Daniel Selsam, Sebastian Ullrich, and Leonardo de Moura. Tabled Typeclass Resolution. 2020. arXiv:2001.04301, doi:10.48550/arXiv.2001.04301.