Skip to content

Theories, views, and models

This page states what a theory denotes, how theories compose, what a view and a model are, and what the checker guarantees about each. The surface is described in the theories reference; the module system the declarations live in is described in modules and static interpretation.

Presentations

A presentation \(T = (\Sigma_T, E_T, R_T)\) consists of a signature \(\Sigma_T\) of sorts and operations, a set \(E_T\) of equations \(l = r\) between terms of one sort over \(\Sigma_T\) and variables, and a set \(R_T\) of rules \(l \leadsto r\), which are equations with an orientation. These are the generalized algebraic theories of (Cartmell, 1986), as panproto represents them (Lynch et al., 2024): a sort may depend on terms of earlier sorts.

Signatures and their typing

A sort is declared with a telescope of parameters,

\[ \mathrm{Hom}(a : \mathrm{Ob},\ b : \mathrm{Ob}), \]

each parameter's sort a sort expression over the parameters before it. A sort expression \(S(t_1, \ldots, t_k)\) applies a sort with \(k\) parameters to \(k\) terms; a sort without parameters is its own expression. An operation is declared with a telescope of inputs and an output sort expression over them,

\[ \mathrm{comp} : \{a\ b\ c : \mathrm{Ob}\},\ (f : \mathrm{Hom}(a, b)),\ (g : \mathrm{Hom}(b, c)) \to \mathrm{Hom}(a, c), \]

where an input in braces is implicit: an application writes only the explicit inputs, \(\mathrm{comp}(f, g)\), and the implicit ones are solved by unification with the sorts of the explicit arguments. A term \(t\) has sort \(A\) in context \(\Gamma\) by the rules of a generalized algebraic theory: a variable has the sort its context gives it, and \(f(t_1, \ldots, t_n)\) has the output sort of \(f\) with the inputs (implicit ones solved) substituted, when each \(t_i\) has the sort of the \(i\)-th explicit input under the same substitution. The sort arguments a signature writes are checked when it is read, for the number of arguments of each sort and for every name being in scope or an operation; panproto typechecks the arguments against the parameters' sorts when the theory is composed.

A law's variables are not declared: panproto infers each variable's sort from the positions it occurs in. A law in which a variable's sort arguments are not determined by its sides, such as \(\mathrm{comp}(\mathrm{id}(a), f) = f\), needs an equation context \(f : \mathrm{Hom}(a, b) \vdash \ldots\), which panproto's theories do not have; the composition refuses such a law and says so. Operations that bind variables are not part of a generalized algebraic theory, and panproto's signatures have no binding operations; the theories of binding signatures are checked by their own rewriting machinery (object languages).

Every symbol and every law of a presentation has an origin: the theory declaration, or the theory expression, that declares it. A presentation is therefore a pair \((T, o)\) of the presentation above and a map \(o\) from its symbols and laws to origins. Two symbols are the same symbol exactly when they have the same origin and the same name; two symbols of one name and different origins are different symbols, and a presentation that would contain both is refused.

The equations and rules of a theory declaration are checked by the equation checker of binding signatures: both sides are terms of one sort, a bare name is an operation when the signature declares one and a variable otherwise, and every variable of a rule's right side occurs on its left. The rules must be locally confluent (every critical pair joins) and terminating (oriented by panproto's lexicographic path ordering), or the theory records termination as assumed.

The category of presentations

A theory morphism \(\sigma : S \to T\) sends each sort of \(S\) to a sort of \(T\) and each operation \(f : s_1 \times \cdots \times s_n \to s\) of \(S\) to a term \(\sigma(f)\) of \(T\) of sort \(\sigma(s)\) in variables \(x_i : \sigma(s_i)\), such that for every equation \(l = r\) of \(S\) the translated equation \(\sigma(l) = \sigma(r)\) holds in \(T\). Composition substitutes terms into terms, and theories with their morphisms form a category \(\mathbf{Pres}\). An inclusion is a morphism that sends every symbol of \(S\) to the symbol of \(T\) with the same origin and name.

Composition by colimit

A theory declaration with includes \(I_1, \ldots, I_k\) and its own sorts, operations, and laws \(L\) denotes the colimit of a diagram in \(\mathbf{Pres}\) built from its include closure \(C\), the origins its includes reach. The diagram has

  • a node \(N_X\) for each \(X \in C\), holding the signature of \(X\) and only the laws whose origin is \(X\);
  • a node \(\Sigma_X\), the signature of \(X\) without laws, for each \(X\) that some member of \(C\) includes, with an inclusion into \(N_X\) and into \(N_Y\) for every \(Y\) that includes \(X\);
  • a node for \(L\), holding the symbols of the includes that \(L\) mentions and the symbols and laws of \(L\) itself, with an inclusion from the part of each direct include that \(L\) mentions.

panproto computes the colimit in explicit-identity mode (Burstall and Goguen, 1977): two symbols are identified exactly when a chain of the diagram's morphisms relates them, so symbols are identified by origin, and a colimit that would give two distinct symbols one name is refused with both sources named. Each law enters the colimit once, from the node of its origin; two laws of one presentation that state the same equation are one law of the colimit.

The operations of the module algebra [@sannella2012foundations; @duran2007maude] are built on the same construction. Each result is a new origin, named by the text of its expression, so the same expression denotes the same theory wherever it is written.

Expression Denotes
A + B over C The colimit of the include closures of \(A\) and \(B\). The theories both reach are shared by origin, and over must declare them: a theory both reach that over does not declare is refused, and so is a declared theory fewer than two summands reach.
e renaming (a => b) A copy of \(e\) with \(a\) renamed to \(b\). A theory \(e\) includes keeps its origin when the renaming leaves all of its symbols alone; every other symbol and law becomes the copy's own. The renaming is checked as a theory morphism \(e \to e'\) that sends every law to a law.
e hiding (a) The symbols of \(e\) other than \(a\) (hiding a sort hides every operation over it), with the laws of \(e\) that do not mention a hidden symbol.
G[P := X via v] The pushout of \(G \leftarrow S_P \to X\) along the parameter's inclusion and the fitting view \(v : S_P \to X\) (Astesiano et al., 2002). Without via, \(X\) must include \(S_P\), and the leg is the inclusion.

A generic theory theory G[P : S] includes \(S\) as its parameter. The includes of \(G\) that do not reach \(S_P\) keep their origin in an instantiation; the rest of \(G\) becomes the instantiation's own presentation, its laws translated along the pushout.

Views and obligations

A view view v : S -> T is a theory morphism. A symbol of \(S\) that the view does not map is sent to the symbol of \(T\) with the same origin and name, when \(T\) reaches that origin; any other unmapped symbol is refused, so a view never relates symbols by coincidence of names. panproto checks that the assignment is a morphism of signatures and, equation by equation, whether \(\sigma(l) = \sigma(r)\) is derivable in \(T\) (Goguen and Burstall, 1992). Derivability is undecidable, and the check is sound and incomplete: it never accepts an image it cannot derive.

Each equation of \(S\) is an obligation, with one of two statuses:

  • established: panproto derives the image, and the view keeps the derivation as a witness;
  • assumed: the view writes assume e, and the checker does not derive the image.

Either way, the obligation lists the checked models of \(T\) its image was checked in, at every assignment of their carriers.

An obligation that is not established and not assumed is refused, with the equation and its image. An assumption is refuted, and the view refused, when the image fails in a checked model of \(T\); the refusal gives the model, the assignment, and the two sides' values. A view may not assume an obligation the checker establishes.

Models

A model \(M\) of a theory whose sorts take no parameters assigns each sort \(s\) a set \(M_s\) and each operation \(f\) a function \(M_{s_1} \times \cdots \times M_{s_n} \to M_s\), such that every law of \(T\) holds at every assignment. model M of T interprets each sort by a finite QIEC type and each operation by a pure computation of the module. The checker lists the carriers, runs each computation on every tuple of arguments on the reference machine, refuses a result outside its carrier, and has panproto check every law of \(T\) at every assignment.

Models of a theory form a category \(\mathbf{Mod}(T)\), and a view \(\sigma : S \to T\) induces the reduct \(\Delta_\sigma : \mathbf{Mod}(T) \to \mathbf{Mod}(S)\), which interprets each symbol of \(S\) by its image. This makes \(\mathbf{Mod}\) a functor \(\mathbf{Pres}^{\mathrm{op}} \to \mathbf{Cat}\) satisfying the satisfaction condition

\[ M' \models \sigma(\varphi) \iff \Delta_\sigma M' \models \varphi, \]

so a property proved of every model of a theory holds of the reduct of every model of an extension (Goguen and Burstall, 1992). model N = reduct M along v is \(\Delta_v M\). The reduct has a left adjoint \(\Sigma_\sigma \dashv \Delta_\sigma\), the free extension: \(\Sigma_\sigma M\) is the model of \(T\) generated by the elements of \(M\), subject to the laws of \(T\) and the relations the operations of \(M\) impose (Spivak, 2012; Schultz and Wisnesky, 2017). model N = free M along v is \(\Sigma_v M\), with its unit \(\eta : M \to \Delta_v \Sigma_v M\). panproto computes both, and the result is checked against its theory like any other model.

A model of a theory with dependent sorts interprets \(\mathrm{Hom}\) by a family of sets \(M_{\mathrm{Hom}}(x, y)\) indexed by the elements of the parameters' carriers, and its laws hold at every well-typed assignment. Checking one needs the equation contexts above and models whose elements carry their sort arguments, which panproto's models do not, so a model of a theory with a dependent sort or an implicit input is refused with that reason. For the same reason the reduct and the free extension are taken only between theories whose sorts take no parameters; panproto computes \(\Sigma\), and its right adjoint \(\Pi\), for those theories only.

Extension classification

An inclusion \(\iota : S \to T\) is, after CASL's annotations (Astesiano et al., 2002; Mossakowski et al., 2007),

  • definitional when every model of \(S\) has exactly one expansion to a model of \(T\);
  • conservative when every model of \(S\) has an expansion;
  • neither when some model of \(S\) has none.

Every inclusion of a theory declaration (a parameter, the theory it is defined as, and each include) and every hiding (the hidden theory into the visible part) is classified. The checker establishes a classification by sufficient conditions on the presentations and refutes one with a model:

  1. If \(T\) adds no sorts, each operation it adds has exactly one law \(f(x_1, \ldots, x_n) = t\) with distinct variables and \(t\) built from the operations of \(S\) and of definitions earlier in a well-founded order, and panproto derives every other law \(T\) adds in \(S\) extended by those definitions, then \(\iota\) is definitional.
  2. If every operation \(T\) adds without such a definition has a result sort \(T\) adds, and every other law \(T\) adds is derived as in 1 or relates terms of a sort \(T\) adds, then \(\iota\) is conservative: the elements of the sorts of \(S\) are built only from the operations of \(S\) and the definitions, so the new sorts can be interpreted freely over any model of \(S\) without changing it.
  3. If a checked model \(M\) of \(S\) has a free extension \(\Sigma_\iota M\) whose unit identifies two distinct elements of \(M\), then \(M\) has no expansion, since an expansion \(M'\) would factor the identity of \(M\) through the unit; \(\iota\) is neither, with \(M\) and the two elements as witness.
  4. If the inclusion writes search=N, the models of \(S\) whose carriers have at most \(N\) elements per sort are enumerated, each carrier the integers \(0, \ldots, n - 1\), and each is tried as in 3. The enumeration assigns the operations' tables cell by cell and abandons a partial table as soon as a law instance whose sides it determines fails; panproto checks every complete table against the laws of \(S\) before its free extension is taken. A search that refutes nothing establishes nothing beyond its bound: the inclusion is then undetermined within bound \(N\), never conservative. The search covers theories whose sorts take no parameters.

Anything else is undetermined. An inclusion may declare [conservative] or [definitional]; the declaration is refused when the checker refutes it, recorded as assumed when it adds assumed, and refused otherwise.

Theories elsewhere in the language

Three other declarations state theories, and each is read through the constructions above rather than by a mechanism of its own.

  1. Signatures with laws. The equations block of a module signature (signature S) presents a theory whose sorts are the signature's abstract families and whose operations are its computations between them, with the signature as their origin. A module that matches \(S\) owes that theory: once the program is compiled, the module is checked as a finite model of it, each sort interpreted by the family that realizes it and each operation by the module's computation, with check_instance; a module whose families are not finite owes the laws as assumed (module calculus §3). From Python, check_instance_view checks an instance that is itself a theory as a view from the signature's theory.
  2. Semiring instances. semiring I : T = algebra A on C is a model of one of the semiring theories of std.semiring on a kernel carrier, checked law by law, with a law established only by a decision on a finite carrier or by a proof the algebra claims (semirings as theory parameters). A deduction generic over [S : T] is generic over the models of \(T\), which is what lets one chart computed in the free instance answer for every instance.
  3. Type classes. The type classes and arrow classes of quivers.monadic are panproto theories, each composed by colimit from its parents along their signatures, so a class reached along two parents is included once, as any theory is.

Guarantees

  • No two symbols are identified unless the diagram relates them; a colimit that would merge symbols by name is refused with both sources.
  • Every law of every included theory is a law of the including theory.
  • A view is a theory morphism, and each of its obligations is established or assumed in its declaration, and holds in every checked model of its codomain; none passes silently.
  • A model satisfies every law of its theory at every assignment of its finite carriers.
  • A declared classification is established, recorded as assumed, or refused.

Every theory, view, and model is a declaration of the checked core, recorded as a structure declaration and hashed by content; a theory's name is part of its content, since it is its origin. The variables of a law and of a view's mapping are bound, so renaming them keeps the hash; a bare name of a law that some theory of the module uses as an operation is read as that operation and stays content. Theories, views, and models denote no run-time code: every transpile target checks them at its lowering boundary and renders the module's computations and programs without them.

The limits: a law's variables are typed by inference, so a dependent law whose sides do not determine its variables' sorts is refused, since panproto's theories have no equation contexts, and a dependent theory has no checked models, since panproto's models are not fibered over sort arguments. Operations bind no variables. A model's carriers are finite, so a theory whose only models are infinite has no model the checker can check, and the classification of its extensions rests on the syntactic conditions and on the models a bounded search reaches.

Cited sources

John Cartmell. Generalised Algebraic Theories and Contextual Categories. Annals of Pure and Applied Logic, 32:209–243, 1986. doi:10.1016/0168-0072(86)90053-9.

Owen Lynch, Kris Brown, James Fairbanks, and Evan Patterson. GATlab: Modeling and Programming with Generalized Algebraic Theories. Electronic Notes in Theoretical Informatics and Computer Science, 2024. doi:10.46298/entics.14666.

Rod M. Burstall and Joseph A. Goguen. Putting Theories Together to Make Specifications. In Proceedings of the 5th International Joint Conference on Artificial Intelligence (IJCAI). 1977. URL: https://www.ijcai.org/proceedings/1977/0234.pdf.

Egidio Astesiano, Michel Bidoit, Hélène Kirchner, Bernd Krieg-Brückner, Peter D. Mosses, Donald Sannella, and Andrzej Tarlecki. CASL: The Common Algebraic Specification Language. Theoretical Computer Science, 286(2):153–196, 2002. doi:10.1016/S0304-3975(01)00368-1.

Joseph A. Goguen and Rod M. Burstall. Institutions: Abstract Model Theory for Specification and Programming. Journal of the ACM, 39(1):95–146, 1992. doi:10.1145/147508.147524.

David I. Spivak. Functorial Data Migration. Information and Computation, 217:31–51, 2012. doi:10.1016/j.ic.2012.05.001.

Patrick Schultz and Ryan Wisnesky. Algebraic Data Integration. Journal of Functional Programming, 2017. doi:10.1017/S0956796817000168.

Till Mossakowski, Christian Maeder, and Klaus Lüttich. The Heterogeneous Tool Set, Hets. In Tools and Algorithms for the Construction and Analysis of Systems (TACAS), volume 4424 of Lecture Notes in Computer Science, 519–522. 2007. doi:10.1007/978-3-540-71209-1_40.