Skip to main content

Computational category theory as differentiable tensor programs

Project description

Quivers

CI Docs PyPI Python 3.14+ License: MIT

Computational category theory as differentiable tensor programs.

Quivers is a Python library for building categorical and probabilistic models as differentiable PyTorch programs. It represents morphisms between finite sets as tensors valued in a quantale (a lattice with a monoidal product), then extends this to stochastic morphisms (Markov kernels), continuous distribution families, monadic probabilistic programs, and variational inference. A built-in functional DSL compiles .qvr specifications into trainable nn.Module instances.

Features

  • Core categorical algebra: finite sets, product/coproduct constructions, and free monoids as objects; quantales (Boolean, product fuzzy, Łukasiewicz, Gödel, tropical) as enrichment algebras; $\mathcal{V}$-enriched relations as parametrized tensors with composition via quantale operations.
  • Categorical structures: functors, natural transformations, adjunctions, monoidal categories, traced monoidal categories, base change between quantales.
  • Enriched category theory: ends, coends, Kan extensions, weighted limits/colimits, profunctors, Yoneda embedding, Day convolution, optics (lenses, prisms, adapters, grates).
  • Monadic constructs: monads, comonads, Kleisli/coKleisli categories, algebras, coalgebras, Eilenberg-Moore categories, distributive laws.
  • Stochastic morphisms: the FinStoch category of Markov kernels; discretized distribution families (normal, logit-normal, beta, truncated normal); conditioning, mixing, and normalization transforms; the Giry monad; query functions (prob, marginal_prob, expectation).
  • Continuous morphisms: 30+ parameterized conditional distribution families; continuous spaces (Euclidean, simplex, unit interval, positive reals); sampled composition; normalizing flows; discrete-continuous boundaries (discretize/embed).
  • Monadic programs: probabilistic programs with draw, observe, and return statements; ancestral sampling; log-joint computation; hybrid discrete-continuous random variables.
  • QVR DSL: a .qvr file format whose tree-sitter grammar is registered in panproto and whose AST nodes, value types, and resolution lenses are built on didactic Models; supports object/morphism declarations, program blocks, let bindings, type expressions, and grammar-based parsers (PCFG, CCG, Lambek, multimodal type-logical). Each .qvr program also extracts to a panproto Schema for diff/migrate workflows.
  • Variational inference: execution traces, conditioning, automatic variational guides (normal, delta), ELBO computation, stochastic variational inference (SVI), posterior predictive sampling.

Installation

pip install quivers

Or install from source:

git clone https://github.com/FACTSlab/quivers
cd quivers
pip install -e .

For development (includes pytest, ruff, pyright):

pip install -e ".[dev]"

Quick Start

Discrete morphisms and composition

Define finite sets, create learnable and observed morphisms, and compose them:

from quivers import FinSet, morphism, observed, Program
import torch

X = FinSet("X", 3)
Y = FinSet("Y", 4)
Z = FinSet("Z", 2)

f = morphism(X, Y)                # learnable (sigmoid over raw params)
g = observed(Y, Z, torch.rand(4, 2))  # fixed tensor

h = f >> g                         # V-enriched composition: X -> Z
program = Program(h)
output = program()                 # shape (3, 2), values in [0, 1]

Composition uses the product fuzzy quantale by default: AND is multiplication, OR is noisy-OR ($1 - \prod(1 - x_i)$). The result is a differentiable tensor, trainable via program.parameters().

Stochastic morphisms

Work with Markov kernels in the FinStoch category:

from quivers import FinSet, stochastic, condition, prob

S = FinSet("S", 3)
O = FinSet("O", 5)

transition = stochastic(S, S)   # learnable row-stochastic matrix
emission = stochastic(S, O)     # learnable row-stochastic matrix

# condition on an observation
conditioned = condition(emission, obs_index=2)

# query probabilities
p = prob(transition, domain_idx=0, codomain_idx=1)

The QVR DSL

Write probabilistic programs in .qvr syntax and compile to nn.Module:

from quivers.dsl import loads

source = """
object Predictor : 1
object Response : 1

program regression : Predictor -> Response
    sigma <- HalfCauchy(2.0)
    beta_0 <- Normal(0.0, 5.0)
    beta_1 <- Normal(0.0, 2.0)
    x <- Normal(0.0, 1.0)
    let mu = beta_0 + beta_1 * x
    observe y ~ Normal(mu, sigma)
    return y

output regression
"""

model = loads(source)

Project Structure

src/quivers/
├── core/           # objects, quantales, morphisms, tensor ops
├── categorical/    # functors, natural transformations, adjunctions, monoidal, traced
├── monadic/        # monads, comonads, algebras, distributive laws
├── enriched/       # ends/coends, Kan extensions, profunctors, Yoneda, Day, optics
├── stochastic/     # Markov kernels, Giry monad, grammar parsers, chart algorithms
├── continuous/     # distribution families, spaces, flows, monadic programs
├── dsl/            # panproto-driven parser, AST (didactic Models),
│                   # compiler, resolution lenses, Program Theory
├── inference/      # traces, conditioning, guides, ELBO, SVI, predictive
├── program.py      # Program: wraps morphisms as nn.Module
└── giry.py         # GiryMonad, FinStoch

The tree-sitter grammar lives at the repo root under grammars/qvr/ and is vendored by panproto's panproto-grammars-all distribution.

Documentation

Full documentation: https://FACTSlab.github.io/quivers

Requirements

  • Python 3.14+
  • PyTorch 2.0+
  • didactic 0.6.0+ (typed-data layer over panproto)
  • panproto 0.45.0+ and panproto-grammars-all 0.45.0+ (QVR tree-sitter parser)

Contributing

See CONTRIBUTING.md for development setup, code style conventions, and the git workflow.

License

MIT. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quivers-0.3.0.tar.gz (509.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quivers-0.3.0-py3-none-any.whl (280.0 kB view details)

Uploaded Python 3

File details

Details for the file quivers-0.3.0.tar.gz.

File metadata

  • Download URL: quivers-0.3.0.tar.gz
  • Upload date:
  • Size: 509.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for quivers-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c20e38d37379eb648826d24e5229bf7a047704a25b62de12b376e6fee4868ebc
MD5 a8221a467e022b45becdd343c317fe57
BLAKE2b-256 292db4bd1a63b3bbc5492d6f01023f34063db56cc66e23562440f0f291d66a31

See more details on using hashes here.

Provenance

The following attestation bundles were made for quivers-0.3.0.tar.gz:

Publisher: release.yml on FACTSlab/quivers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quivers-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: quivers-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 280.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for quivers-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44a79c2022e5e45ad33172b57277380d2ba325007a568b674c1fecd04f40d493
MD5 ec3f783cb487641d213756466e8bd709
BLAKE2b-256 b75eb1bd02e48a3016b08fc02610ebbebb3ee554018b3d19009e26dd2e1b809f

See more details on using hashes here.

Provenance

The following attestation bundles were made for quivers-0.3.0-py3-none-any.whl:

Publisher: release.yml on FACTSlab/quivers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page