Skip to content

Theories, views, and models

A theory declares an algebraic presentation: sorts, operations, equations, and rules, and the theories it includes. A view maps one theory into another and turns the first theory's equations into obligations. A model interprets a theory by QIEC types and computations, or is constructed from another model along a view. Theories compose by colimit, computed by panproto's theory engine, over theories they share by origin, never by a coincidence of names. The mathematics is in Theories, views, and models; this page gives the surface.

Theory declarations

theory Carrier
    sorts S

theory Monoid[C : Carrier]
    operations
        unit : -> S
        mul : S, S -> S
    equations
        left_unit : mul(unit, x) = x
        right_unit : mul(x, unit) = x
        assoc : mul(mul(x, y), z) = mul(x, mul(y, z))

A theory body has, in any order:

Entry Meaning
include e [options] include the theory e (a name or a theory expression); its symbols and laws are the including theory's, with their origin kept
sorts A, B declare sorts
operations a block of operations f : A, B -> C; a constant is c : -> C
equations a block of equations name : lhs = rhs and rules name : lhs ~> rhs

Terms are written as in the equations of a syntax declaration, and are checked by the same checker: a bare name is an operation of the theory (or of a theory it includes) when there is one, and a variable otherwise; both sides of a law have one sort; and the variables of a rule's right side occur on its left. A constant is written unit or unit(). Rules must be confluent and terminating; when panproto's path ordering does not show termination, assume terminating in the equations block records it as an assumption.

A sort may depend on terms of earlier sorts, and an operation's inputs may be named or implicit, so that its sorts can depend on them:

theory Category
    sorts Ob, Hom(a b : Ob)
    operations
        id : (a : Ob) -> Hom(a, a)
        comp : {a b c : Ob}, (f : Hom(a, b)), (g : Hom(b, c)) -> Hom(a, c)
    equations
        assoc : comp(comp(f, g), h) = comp(f, comp(g, h))
Input Meaning
S an unnamed input of sort S
(x y : S) named inputs, which later sorts may mention
{a b : S} implicit inputs: an application writes only the explicit ones, comp(f, g), and the implicit ones are inferred from their sorts

A law's variables are typed by where they occur. A law whose sides leave a variable's sort arguments undetermined, such as comp(id(a), f) = f, needs an equation context, which panproto does not support yet (panproto/panproto#438), and is refused with that reason. A model of a theory with a dependent sort or an implicit input is refused for the same reason and for want of fibered models (panproto/panproto#439); views, sums, renamings, hidings, and instantiations of dependent theories are checked as usual. Operations that bind variables are not available (panproto/panproto#432).

theory Monoid[C : Carrier] makes Monoid generic: C is a parameter that ranges over theories that fit Carrier, and Carrier is included. theory T = e defines a theory by a theory expression, and a body after it extends the expression.

Theory expressions

theory Additive = Monoid renaming (unit => zero, mul => plus)
theory Multiplicative = Monoid renaming (unit => one, mul => times)

theory Semiring
    include Additive + Multiplicative over Carrier
    equations
        plus_comm : plus(x, y) = plus(y, x)
        left_distrib : times(x, plus(y, z)) = plus(times(x, y), times(x, z))
        right_distrib : times(plus(x, y), z) = plus(times(x, z), times(y, z))
        left_zero : times(zero, x) = zero
        right_zero : times(x, zero) = zero
Expression Meaning
A + B over C, D the colimit of A and B, sharing exactly the theories they both include; over names those theories, and is required whenever the summands share one
e renaming (a => b, ...) a copy of e with the listed sorts and operations renamed; an included theory whose symbols are all left alone keeps its origin
e hiding (a, ...) e without the listed sorts and operations, without every operation over a hidden sort, and without every law that mentions a hidden symbol
G[P := X via v] the generic theory G instantiated at X, the pushout along the fitting view v from the parameter's theory to X; via v may be left out when X includes the parameter's theory
(e) grouping

Additive and Multiplicative both include Carrier, so their sum identifies their sort S, and over Carrier says so. Each renames unit and mul, so their operations are distinct. Summing two theories that declare a symbol of one name independently is refused, naming both declarations:

theory Left
    sorts S

theory Right
    sorts S

theory Both = Left + Right

The same expression written twice denotes the same theory.

Classifying an inclusion

include Monoid [conservative] declares that the including theory is a conservative extension of Monoid: every model of Monoid has an expansion. [definitional] declares that every model has exactly one. The checker establishes the declaration, or refutes it with a checked model of the included theory that has no expansion, or, when it can do neither, refuses it unless the inclusion says [conservative, assumed] (or [definitional, assumed]). Every inclusion is classified, declared or not, and program.theory_layer.extensions records the result.

[search=N] adds a bounded search: the models of the included theory with at most N elements per sort are enumerated, and one that has no expansion refutes conservativity. Without search the checker refutes only with the models the module declares. A search that finds nothing records the inclusion as undetermined within bound N, never as conservative. Commutativity is not conservative over monoids, and the search finds a three-element monoid that refutes it:

theory Carrier
    sorts S

theory Monoid[C : Carrier]
    operations
        unit : -> S
        mul : S, S -> S
    equations
        left_unit : mul(unit, x) = x
        right_unit : mul(x, unit) = x
        assoc : mul(mul(x, y), z) = mul(x, mul(y, z))

theory Commutative
    include Monoid [conservative, search=3]
    equations
        comm : mul(x, y) = mul(y, x)
theory Squares
    include Monoid [definitional]
    operations
        square : S -> S
    equations
        square_def : square(x) = mul(x, x)

Views

view additive : Monoid -> Semiring
    unit => zero
    mul => plus

A mapping sends a sort to a sort (M => S), an operation to an operation (mul => plus), or an operation, written applied to distinct variables, to a term over them (mul(x, y) => plus(y, x)). A sort or operation the view does not map is sent to itself when the codomain includes the theory that declares it: here S comes from Carrier, which Semiring includes. Any other unmapped symbol is refused.

Every equation of the domain becomes an obligation in the codomain. An obligation is established when panproto derives the image of the equation from the codomain's laws; it is checked in every model of the codomain; and assume e records an obligation the checker does not establish. A view whose obligation is neither established nor assumed is refused with the equation and its image, and a view whose obligation fails in a model of the codomain is refused with the model and the assignment.

Models

define bool_false() : Bool !{} =
    return false

define bool_true() : Bool !{} =
    return true

define bool_or(a : Bool, b : Bool) : Bool !{} =
    return a || b

define bool_and(a : Bool, b : Bool) : Bool !{} =
    return a && b

model Booleans of Semiring
    S = Bool
    zero = bool_false
    plus = bool_or
    one = bool_true
    times = bool_and

model Disjunction = reduct Booleans along additive

model M of T interprets each sort of T by a finite type (Bool, Unit, or a closed data family without parameters, indices, or recursion) and each operation by a pure computation with the operation's signature. The checker runs every computation on every tuple of arguments on the reference machine and checks every law of T at every assignment; a law that fails is reported with the assignment and the values of both sides.

model N = reduct M along v is the reduct of a model of the codomain of v: a model of its domain that interprets each symbol by its image. model N = free M along v is the free extension of a model of the domain of v: the model of its codomain generated by M.

The theory layer of a program

Program.theory_layer is a TheoryLayer with every theory (theories, by name and by expression), view (views, each with its obligations), model (models, each with its carriers and operation tables), and inclusion (extensions, each with its classification).

Refusal Reported at
an undeclared theory, view, or model the reference
a symbol declared by two theories that no shared theory relates the composite, naming both declarations
an ill-sorted law, or an operation over an unknown sort the law or operation
a rule not shown confluent, or terminating without assume terminating the rule
an unmapped symbol, or an ill-sorted image, of a view the view, or the mapping
an obligation neither established nor assumed, or refuted by a model the view, with the equation and its image
a model that misses a symbol, has an infinite carrier, or violates a law the model, with the assignment
a declared classification refuted, or not established and not assumed the inclusion

The words theory, view, model, include, operations, over, renaming, of, along, reduct, and free are keywords only where their declarations and expressions use them, so each remains an ordinary name everywhere else (a program named model, and export model).