9. Indexed data and total functions¶
An inductive family is a datatype indexed by values, whose constructors fix the index of the values they build (Dybjer, 1994). Generalized algebraic datatypes (GADTs) are the same idea in functional programming (Xi et al., 2003). Declare vectors indexed by their length and write functions over them that need no length check at run time. A shape assumption that would otherwise be a comment or a runtime assertion becomes part of a type.
1. Declare the index¶
index Nat = Z | S(Nat)
family Vec[A : Type](n : Nat) : Type
constructor Nil : Vec[A](Z)
constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))
define length[A : Type, n : Nat](xs : Vec[A](n)) : Int !{} =
case xs motive (m : Nat) => Int
Nil =>
return 0
Cons[k](head, tail) =>
let rest <- length[A, k](tail)
return 1 + rest
index Nat = Z | S(Nat) declares a closed index sort with two constructors. Its values are the natural numbers in unary: Z is zero and S(n) is the successor of n. The recursive constructor permits arbitrarily large indices. Vec[A](n) has a parameter A, the element type, which is the same in every constructor, and an index n, the length, which each constructor fixes: Nil builds only Vec[A](Z), and Cons[m] takes a tail of length m and builds a vector of length S(m). The length is thus established by the constructors and cannot disagree with the elements.
length is a computation (define) whose effect row !{} is empty: it performs no effects (chapter 10 introduces effect rows). Its case analysis states a motive, the result type of the analysis as a function of the inspected value's index (McBride, 2002). Here the motive (m : Nat) => Int returns Int whatever the length. In the Cons branch the checker learns that the input has length S(k) for some k, so tail has type Vec[A](k) and the recursive call is made at k.
This signature also illustrates QVR's parameter stages. In length[A, n](xs),
the bracketed arguments determine the type of the runtime argument xs.
Programs use the same split between static specialization and typed external
values. Family application has a distinct purpose: the parentheses in
Vec[A](n) identify a refinable static index, rather than a runtime argument.
The parameter reference
compares these forms across the language.
2. Fold without a shape check¶
A sum over a real vector:
index Nat = Z | S(Nat)
family Vec[A : Type](n : Nat) : Type
constructor Nil : Vec[A](Z)
constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))
define sum[n : Nat](xs : Vec[Real](n), acc : Real) : Real !{} =
case xs motive (m : Nat) => Real
Nil =>
return acc
Cons[k](head, tail) =>
let rest <- sum[k](tail, acc + head)
return rest
No branch tests whether the tail is empty, and no runtime counter is decremented. Matching Cons[k] gives the index k of the tail, which fixes the type of the recursive call.
3. Build a vector¶
A constructor application names the family it builds:
index Nat = Z | S(Nat)
family Vec[A : Type](n : Nat) : Type
constructor Nil : Vec[A](Z)
constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))
define triple(a : Real, b : Real, c : Real) : Vec[Real](S(S(S(Z)))) !{} =
let last = (Cons[Real, Z](c, (Nil[Real]() : Vec[Real](Z))) : Vec[Real](S(Z)))
let pair = (Cons[Real, S(Z)](b, last) : Vec[Real](S(S(Z))))
return (Cons[Real, S(S(Z))](a, pair) : Vec[Real](S(S(S(Z)))))
The bracketed arguments of Cons are the parameter (the element type) followed by the constructor's own index m, the length of the tail. The as annotation states the type of the result.
4. Run the summary¶
The complete source is indexed-vectors.qvr, whose computation summarize builds a vector of three elements and returns its sum and its length. Check the file, list its entry points (the computations and programs a module exposes to qvr run and Program.run), and run summarize:
qvr check docs/tutorials/qvr/source/indexed-vectors.qvr
qvr run docs/tutorials/qvr/source/indexed-vectors.qvr --list
qvr run docs/tutorials/qvr/source/indexed-vectors.qvr summarize 1.5 2.0 3.5 --json
The same entry point from Python:
from quivers.dsl import load
indexed = load("docs/tutorials/qvr/source/indexed-vectors.qvr")
run = indexed.run("summarize", 1.5, 2.0, 3.5)
assert run.value == (7.0, 3)
print(run.value)
run.entry.kind is "computation". The computation runs on the reference machine, the library's interpreter for the Quivers Indexed Effect Core (QIEC), the typed core language that QVR declarations elaborate to. run.result.runtime is "core", and run.result.trace records each step of the run, the recursive calls included, as an event.
5. Transfer the pattern to statistical data¶
Length is one useful index among several. An index can record whether a measurement is observed or missing (the type reference uses a family Measurement indexed by Observed and Missing), whether a value is exact or censored on the left, on the right, or to an interval, or whether a covariance parameter is raw, a Cholesky factor, or positive definite. An index earns its place when a constructor establishes an invariant that later case analysis uses.
A potential worry is that the index only moves validation into more syntax. Data from an untyped source still needs a check where it enters. But once that check has built the indexed value, every later computation receives the refined type, so the invariant is checked once and not at each use.
Next¶
Chapter 10 declares an effect and gives its operations an interpretation with a handler. The type reference gives the complete rules for families and case analysis.
Cited sources¶
Peter Dybjer. Inductive Families. Formal Aspects of Computing, 6(4):440–465, 1994. doi:10.1007/BF01211308.
Hongwei Xi, Chiyan Chen, and Gang Chen. Guarded Recursive Datatype Constructors. In Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), 224–235. 2003. doi:10.1145/604131.604150.
Conor McBride. Elimination with a Motive. In Types for Proofs and Programs (TYPES 2000), Lecture Notes in Computer Science, 197–216. Springer, 2002. doi:10.1007/3-540-45842-5_13.