Architecture¶
The quivers library is organized into twenty subpackages plus two
top-level modules. The categorical core (core/, categorical/,
monadic/, arrows/, enriched/, stochastic/, continuous/) is
surrounded by a DSL layer (dsl/), an inference layer (inference/),
a data layer (data/, diagnostics/, formulas/), structural and
analysis utilities (structural/, analysis/), effect handlers
(effects/), transpilers (transpile/), and the interactive tools
(cli/, kernel/, lsp/). This document describes the
package hierarchy, dependencies, and key abstractions in each module.
Package Structure¶
quivers/
├── core/ # foundational types and operations
├── categorical/ # functors, transformations, adjunctions, monoidal
├── monadic/ # typeclass tower (Functor → Applicative →
│ # Monad → MonadPlus, plus Traversable /
│ # MonadTrans), stdlib effect instances,
│ # transformers, algebraic effects,
│ # Kleisli/ArrowMonad/CoKleisli bridges,
│ # panproto theory stubs
├── arrows/ # Hughes-style arrow tower (Category_ → Arrow →
│ # ArrowApply / ArrowChoice / ArrowLoop /
│ # ArrowZero / ArrowPlus) plus VRel / Function /
│ # Stochastic instances and panproto theories
├── enriched/ # ends/coends, Kan, weighted limits, profunctors,
│ # Yoneda, Day convolution, optics
├── stochastic/ # FinStoch (Markov kernels), families, transforms
│ # (condition / mix / factor / normalize), Giry
│ # monad, deductive chart parsers (CCG, Lambek),
│ # semirings, schema, span, effect_lifts
├── continuous/ # parameterized families, spaces, boundaries,
│ # flows, monadic programs, plates, scans,
│ # deterministic transforms, inline composition
├── dsl/ # panproto-driven tree-sitter parser package,
│ # didactic AST node package, compiler package,
│ # program/deduction theory schemas, Pygments
│ # lexer, grammar introspection helpers
├── inference/ # trace, conditioning, latent registry,
│ # variational guides (mean-field, full-rank,
│ # low-rank, IAF, neural-spline, mixture,
│ # Laplace, delta), gradient estimators,
│ # objectives (ELBO / IWAE / Rényi / VR-IWAE /
│ # ChiVI / RWS / DReGs),
│ # SVI, predictive, MCMC (HMC, NUTS), DAIS,
│ # warmup, Bayesian lifts
├── analysis/ # static analysis of programs: scope, chain
│ # shape, plate graph rendering, initialiser
│ # specifications, saturation
├── data/ # data-frame encoding helpers and observation
│ # schemas
├── diagnostics/ # ArviZ-backed trace I/O and predictive checks
├── formulas/ # R-style formula DSL → quivers program
│ # compilation (formulae integration)
├── structural/ # encoder/decoder shape inference, structural
│ # signatures, losses
├── effects/ # algebraic effect handlers and reparameterizers
├── qiec/ # indexed-family/effect core, checker, codec,
│ # reference evaluator, built-in handlers
├── transpile/ # target-independent IR and eleven PPL renderers
├── cli/ # the `qvr` command, REPL, language-server
│ # entry-point, migration driver
├── kernel/ # Jupyter kernel install + runtime
├── lsp/ # pygls-based language server (document model,
│ # server)
├── program.py # nn.Module wrapper for morphisms (discrete
│ # or continuous)
└── giry.py # backwards-compat re-exports of GiryMonad and
# FinStoch from stochastic.giry
The QVR tree-sitter grammar lives at the repo root under grammars/qvr/.
Quivers packages the generated current source and overrides Panproto's qvr
protocol at runtime, so ordinary parsing and Python-side highlighting load the
same grammar independently of the QVR revision in panproto-grammars-all.
First-party editor assets are synchronized with the in-tree source.
Module Descriptions¶
core/¶
Foundational categorical types and operations. Defines the objects and morphisms that all other modules build on.
objects.py: Categorical objects asdx.TaggedUnionvariants:SetObject(root),FinSet,ProductSet,CoproductSet,FreeMonoid,EnumSet,FreeResiduated, plus the module-levelUnit: FinSet = FinSet(name="1", cardinality=1)singleton.algebras.py: Composition rules organised as a three-level hierarchy:CompositionRule(abstract) →Semigroupoid(composition only) →Algebra(composition + tensor unit + bilinear pair).BilinearFormsits besideSemigroupoidfor the tensor-product side. Concrete variants:ProductFuzzyAlgebra,BooleanAlgebra,LukasiewiczAlgebra,GodelAlgebra,TropicalAlgebra,MaxPlusAlgebra,LogProbAlgebra,RealAlgebra,ProbabilityAlgebra,CountingAlgebra,MarkovAlgebra, plusDualAlgebra,CustomAlgebra,CustomSemigroupoid,CustomBilinearForm. Module-level singletons:PRODUCT_FUZZY,BOOLEAN,LUKASIEWICZ,GODEL,TROPICAL,MAX_PLUS,LOG_PROB,REAL,PROBABILITY,COUNTING,MARKOV,REICHENBACH,BOOLEAN_DUAL,DUAL_LUKASIEWICZ,DUAL_GODEL.algebra_morphisms.py:AlgebraHomomorphism(abstract) and its concrete variants (IdentityHom,Embedding,Expectation,LogProb,MaxPlus,Threshold,MaterialImplication,ProbabilityClamp,CountingFromReal,ProbabilityToReal,CountingToReal), plus thethreshold(),embedding(), andlookup_homomorphism()helpers.morphism_transformations.py:MorphismTransformation(abstract step type) plus theSoftmax,L1Normalize,L2Normalize, andBayesInvertconcrete steps and theirsoftmax()/l1_normalize()/l2_normalize()factory wrappers. These steps driveMorphism.change_baseand theTransformedMorphismcarrier.morphisms.py: \(\mathcal{V}\)-enriched relations as tensors:Morphism(abstract),ObservedMorphism(fixed tensor data),LatentMorphism(learnable),TransformedMorphism,ComposedMorphism,ProductMorphism,MarginalizedMorphism,FunctorMorphism,RepeatMorphism,CurriedMorphism, plus the factory functionsmorphism(),observed(),identity(),cup(),cap(), and helpersas_torch_module(),extract_morphism().tensor_ops.py: Tensor operations:noisy_or_contract,noisy_or_reduce,noisy_and_reduce,componentwise_lift.trans.py:TransSeq, a composable sequence ofMorphismTransformationsteps.wiring.py: Wiring helpers for tensor-product composition._factories.py,_util.py: Internal factory helpers and numerical utilities (EPS,clamp_probs,safe_log1p_neg).
categorical/¶
Categorical structures built from objects and morphisms.
functors.py: Functors and functor composition:Functor,IdentityFunctor,ComposedFunctor,FreeMonoidFunctor.natural_transformations.py: Natural transformations:NaturalTransformation,ComponentwiseNT.adjunctions.py: Adjunctions and free-forgetful pairs:Adjunction,ForgetfulFunctor,FreeForgetfulAdjunction.monoidal.py: Monoidal structures:MonoidalStructure,CartesianMonoidal,CoproductMonoidal,EmptySet,EMPTY.base_change.py: Base change between algebras:BaseChange,BoolToFuzzy,FuzzyToBool.traced.py: Traced monoidal categories:TracedMonoidal,CartesianTrace,IterativeTrace, functionstrace(),partial_trace().
monadic/¶
A Haskell-style typeclass hierarchy with concrete monad instances plus comonads, algebras, distributive laws, transformers, and algebraic effects. Each typeclass is mirrored by a panproto theory.
Modules:
typeclasses.py:Functor,Applicative,Monad,Alternative,MonadPlus,Foldable,Traversable,MonadTransABCs.monads.py:FuzzyPowersetMonad,FreeMonoidMonad(concrete Monad instances) plusKleisliCategory.comonads.py:Comonad,CoKleisliCategory,DiagonalComonad,CofreeComonad.algebras.py: Eilenberg–Moore:Algebra,FreeAlgebra,ObservedAlgebra,Coalgebra,CofreeCoalgebra,ObservedCoalgebra,EilenbergMooreCategory.distributive_laws.py:DistributiveLaw,FreeMonoidPowersetLaw.instances.py: stdlib effect instances:Identity,Maybe,Alternative_,Continuation,State,Reader,Writer,List. Each registers against the appropriate ABC(s) viaABC.register(...).transformers.py: monad transformers:StateT,ReaderT,MaybeT,ContT,WriterT.algebraic.py: algebraic effects + handlers:Operation,EffectSignature,Handler,FreeMonad.bridges.py:Kleisli,ArrowMonad,CoKleislipluskleisli/arrow_monad/cokleislifactory helpers connecting the monad, arrow, and comonad towers.theories.py: panproto-theory stubs for each typeclass.laws.py: runtime-checkable typeclass-law scaffolding.
arrows/¶
Hughes-style arrow tower (parallel to the monad-side hierarchy).
typeclasses.py:Category_,Arrow,ArrowChoice,ArrowApply,ArrowLoop,ArrowZero,ArrowPlusABCs.instances.py: concrete arrow carriers:VRel(the \(\mathcal{V}\)-enriched relation arrow),Function(the deterministic restriction ofVRelto 0/1-valued function-graph tensors),Stochastic(Markov-kernel arrow).theories.py: panproto-theory stubs for each arrow typeclass.
enriched/¶
Enriched category theory: the calculus of \(\mathcal{V}\)-valued hom-functors.
ends_coends.py: Limits and colimits in enriched categories:coend(),end().kan_extensions.py: Kan extensions:ObjectMap,Projection,Inclusion,left_kan(),right_kan().weighted_limits.py: Weighted (co)limits:Weight,Diagram,weighted_limit(),weighted_colimit(),representable_weight(),terminal_weight().profunctors.py: Profunctors (distributor, bimodules):Profunctor.yoneda.py: Yoneda lemma and embedding:Presheaf,representable_profunctor(),corepresentable_profunctor(),yoneda_embedding(),yoneda_lemma(),yoneda_density(),verify_yoneda_fully_faithful().day_convolution.py: Day convolution for monoidal categories:day_convolution(),day_unit(),day_convolution_profunctors().optics.py: Optics and their composition:Optic,Lens,Prism,Adapter,Grate,compose_optics().
stochastic/¶
Stochastic morphisms and the category FinStoch of Markov kernels on finite sets.
morphisms.py: Row-stochastic learnable Markov kernels:StochasticMorphismand its aliasCategoricalMorphism, plus thestochastic()factory.families.py: Discretized parameterized distributions:DiscretizedNormal,DiscretizedLogitNormal,DiscretizedBeta,DiscretizedTruncatedNormal.transforms.py: Transforms on stochastic morphisms and the carrier classes they produce:condition(),mix(),factor(),normalize(), withConditionedMorphism,MixtureMorphism,FactoredMorphism,NormalizedMorphism.queries.py: Query functions:prob(),marginal_prob(),expectation().giry.py: The Giry monad on FinStoch:GiryMonad,FinStoch.categories.py,effect_lifts.py,inside.py,agenda.py,span.py,semiring.py: chart-parser infrastructure: deduction categories, agenda-based search, span representations, semiring weights, the inside algorithm, and effect lifts.schema.py:RuleSchema, a functor fromCategorySystemtoRuleSystem. Provides the binary/unary schema combinators (BinaryRuleSchema,UnaryRuleSchema,UnionSchema,WeightedSchema) and the concrete CCG/Lambek schema instances (ForwardApplication,BackwardApplication,ForwardComposition,BackwardComposition, the crossed and commutative variants,TensorIntroduction,LeftUnitElimination, …). Schemas compose via|(union) and.weighted(w).deduction/: deduction-system primitives (atoms, axioms, rules)._rule_system.py,rules.py,ccg.py,lambek.py,parsers.py: theRuleSystemcore, theccg_rules/lambek_rules/custom_ruleslibraries, and theChartParser/CCGParser/LambekParserdrivers.stdlib/: packaged grammars and rule sets.
continuous/¶
Continuous and hybrid discrete-continuous morphisms and monadic programs.
spaces.py: Continuous sample spaces asdx.TaggedUnionvariants:ContinuousSpace,Euclidean,UnitInterval(factory wrappingEuclidean),Simplex,PositiveReals,CholeskyFactor,ProductSpace,Sphere,Ball,Covariance,Correlation,Orthogonal,Stiefel,LowerTriangular,Diagonal.morphisms.py: Continuous morphism base classes and combinators.families.py: Conditional distribution families, defined either as explicit subclasses (e.g.ConditionalNormal,ConditionalBeta,ConditionalDirichlet,ConditionalMultivariateNormal,ConditionalLowRankMVN,ConditionalWishart,ConditionalInverseWishart,ConditionalMatrixNormal,ConditionalRelaxedBernoulli,ConditionalRelaxedOneHotCategorical,ConditionalLKJCholesky,ConditionalGaussianProcess,ConditionalHorseshoe,ConditionalMixture,ConditionalIndependent,ConditionalTransformed,ConditionalBernoulli,ConditionalCategorical,ConditionalBinomial,ConditionalLogisticNormal,ConditionalOneHotCategorical) or generated by the_make_familyfactory (e.g.ConditionalCauchy,ConditionalLaplace,ConditionalGumbel,ConditionalLogNormal,ConditionalStudentT,ConditionalExponential,ConditionalGamma,ConditionalChi2,ConditionalHalfCauchy,ConditionalHalfNormal,ConditionalInverseGamma,ConditionalWeibull,ConditionalPareto,ConditionalKumaraswamy,ConditionalContinuousBernoulli,ConditionalFisherSnedecor,ConditionalPoisson,ConditionalGeometric). Also exports theLKJCorrelationFactorandTruncatedhelpers.family_spec.py: Declarative specification objects backing_make_family.programs.py: Monadic programs:MonadicProgram(compositional probabilistic computations) plus the internal_StepSpec/_LetSpec/_ScoreSpecstep records.plate.py,scan.py:plate(...)andscan(...)constructs for vectorised and sequential program fragments.inline.py: inline-composition helpers used by the DSL compiler.deterministic.py: deterministic continuous morphisms (pure functions on continuous spaces).boundaries.py: Boundaries between discrete and continuous:Discretize,Embed.flows.py: Normalizing flows:AffineCouplingLayer,ConditionalFlow.
dsl/¶
Domain-specific language for quiver expressions in .qvr files. Parsing is
delegated to Panproto after Quivers installs its packaged current qvr
tree-sitter grammar; Quivers does not run a hand-written lexer. The AST
carries distinct QIEC nodes, and qiec_lowering.py constructs Didactic's
public GADT representation before completing the exact route from
qvr-source/v0.20 to qiec-core/v1alpha1. The reference .qvr
programs that drive the test suite and the tree-sitter grammar fixtures live at
docs/examples/source/, not under the package itself.
parser/: A package that walks the panproto-produced parse tree and builds a tree ofdx.ModelAST nodes. The top-level module re-exportsparse(),parse_file(),ParseError;qiec.pyowns indexed-family, effect, handler, and computation forms, while the remaining submodules partition the established declarations and expressions.ast_nodes/: A package whose every node is adx.Model. Recursive sums (ObjectExpr,Expr,LetExprNode,ProgramStep,Statement) aredx.TaggedUnionroots discriminated by akind: Literal[...]field.qiec.pydefines the complete QIEC node set; the other submodules split the probabilistic and structural nodes by category.compiler/: A package that walks the AST and produces aquivers.Program.core.pydefinesCompiler(composed via mixins fromdeclarations.py,deductions.py,expressions.py,programs.py,structural.py);resolution.pyprovides the_ResolutionMixinwhose_resolve_any_spaceroutes anObjectExprto either aSetObject(discrete) or aContinuousSpace;_options.pyparses option blocks and_prelude.pydefinesCompileErrorplus shared compiler-state primitives.program_theory.py: DefinesQVR_PROGRAM_PROTOCOL(a panproto protocol whose vertex kinds enumerate everySetObjectandContinuousSpacevariant plus the QVR declaration variants),extract_program_schema(compiler), andextract_deduction_schema(compiler). These make every.qvrprogram a schema in panproto's sense, diff, migrate, and lens-generation workflows apply.constraints.py: post-parse axiom checks:check_constraints(module)returns a list ofViolationrecords (used by the LSP, the REPL, andqvr check).emit.py: AST →.qvrsource emission (the print direction of the parser).pygments_lexer.py: The tree-sitter-backed Pygments lexer shared by documentation and notebook tooling, registered as theqvrlexer entry point._grammar_build.py,_grammar_data/,_grammar_introspection.py: loads the generated checkout grammar during development or the packaged source/native/manifest trio from a platform wheel, builds a content-addressed parser only for editable grammar work, and derives shared syntax metadata. Immutable migration snapshots remain separate underquivers.cli.migrations.qiec_lowering.py,program_elaboration.py,qiec_tooling.py: project indexed declarations into Didactic's publicGADTAPI, elaborate everyprogramto a named computation over the module's canonicalrandomandscoreinstances, check and lower the complete module, negotiate the exact route, and expose one diagnostic and symbol model to the CLI, REPL, and LSP.
Top-level DSL API (re-exported from quivers.dsl): parse(), parse_file(),
loads(), load(), Compiler, Module, QVR_PROGRAM_PROTOCOL,
extract_program_schema(), QVR_SOURCE_VERSION, QvrQiecLowerer,
lower_qvr_to_qiec(), has_qiec_surface(), non_qiec_projection(), plus
ParseError, CompileError, and QiecDiagnosticError.
Parametric programs, program name (G : FinSet, scale : Real, prior : Mor[A, B]) : dom -> cod, are stored as templates on the compiler (Compiler._program_templates) and inlined per call site by substitution + α-renaming; the runtime MonadicProgram is built from the inlined step list, so distinct call sites contribute distinct factors to the parent's joint kernel.
Program bodies use a single Kleisli-bind sigil <-. The sampling shapes are sample v <- F(args) (scalar draw), sample v : A <- F(args) (A-indexed plate), observe v <- F(args) (scored observation), observe r : N <- F(args) (N-indexed batched score), and marginalize c : A <- F(args) [over = G, reduction = R] with an indented scope body (scoped marginalization). A program declaration carries an effect signature in its option block, [effects = [Sample, Score, Marginal, Pure]]; the compiler verifies that the body's actual effects are a subset of the declared set. A posterior block is an [effects = [Pure]] program with an [over = M] option consuming the latents of model M as data parameters.
inference/¶
Inference primitives for monadic probabilistic programs: tracing, observation conditioning, automatic variational guides, gradient estimators, variational objectives, Markov-chain Monte Carlo, and posterior-predictive sampling.
trace.py: Execution tracing:SampleSite,Trace,trace().conditioning.py: Observation marking:Conditioned,condition().registry.py:LatentRegistryandLatentSite, the per-site introspection helper every guide and MCMC kernel consumes.guides/: A package of variational guide families. Public classes:Guide(base),AutoNormalGuide,AutoMultivariateNormalGuide,AutoLowRankMultivariateNormalGuide,AutoDeltaGuide,AutoLaplaceApproximation,AutoNormalizingFlow,AutoIAFGuide,AutoNeuralSplineGuide,AutoMixtureGuide,AutoGuideList, andAutoStructured.estimators.py: Gradient estimators:GradientEstimator,Reparameterized,StickingTheLanding,DoublyReparameterized,ScoreFunction.objectives.py: Variational objectives:Objective,ELBO,IWAEBound,RenyiBound,VRIWAEBound,ChiVI,RWS, andDReGsBound.svi.py: Stochastic variational inference driver:SVI.predictive.py: Posterior-predictive sampling:Predictive.mcmc/: Markov-chain Monte Carlo:MCMCKernel(base),HMCKernel,NUTSKernel, plus theMCMCdriver andMCMCResult.dais.py: Differentiable annealed importance sampling:AutoDAIS.warmup.py:WarmupThenHMC, a warmup-then-sampling composite kernel.lifts.py: Bayesian lifts that turn a deterministic program into a sampled one:bayesian_lift_parameters,lift_to_bayesian_program,lift_from_log_prob,monte_carlo_log_joint.transforms.py: Bijective flow transforms used by the normalizing-flow guides:TransformModule(base),AffineCouplingTransform,MaskedAutoregressiveTransform,InverseAutoregressiveTransform,NeuralSplineCouplingTransform,LULinearTransform,BatchNormTransform, plus theMADEautoregressive net and thealternating_mask/half_maskmask helpers.
analysis/¶
Static analysis of compiled programs: scope.py (scope-path traversal), chain_shape.py (chain-rule shape inference), plate_graph.py and plate_render.py (plate-diagram extraction and rendering), init_spec.py (initialiser specifications), saturation.py (saturation checks for the type/effect system).
data/¶
Data-frame ingestion. schema.py defines DatasetSchema (a frozen dx.Model pairing a program's declared columns with their roles, dtypes, and plate indices) plus the compose() helper that splices a DatasetSchema into a .qvr body. encoding.py defines ColumnRole, MissingPolicy, and encode_column(), which convert narwhals / pandas / polars / pyarrow frames into the tensor dictionaries the runtime consumes.
diagnostics/¶
ArviZ-backed posterior diagnostics. arviz_io.py converts an MCMCResult into an xarray.DataTree (the ArviZ 1.x container) via az.from_dict, repackaging sample tensors under the canonical ArviZ group names with user-supplied coords and dims; comparison.py and predictive_checks.py wrap the standard cross-validation and posterior-predictive-check workflows.
formulas/¶
R-style formula DSL. formula.py defines the Formula, FormulaData, FixedColumn, and RandomTerm data classes plus formula_from_data(), the entry point from a formula string + data frame to a parsed formula. compile.py defines FormulaToQVRModule (a dx.Lens[Formula, Module, FormulaData]) that lowers a parsed formula into a quivers.dsl.Module. family.py declares the conjugate response families (Family, Link, AuxParam). _fit.py provides the BayesianFit orchestration record and the fit() convenience entry point (dispatches between SVI and MCMC).
structural/¶
Structural compression: a uniform algebraic interface for encoding arbitrary structured terms to fixed-length vectors and decoding them back under a learned distribution.
signature.py: Term-level data model.Sort,Constructor,Binder,BinderArgSpec,BinderVarSpec,Term,VertexKind,EdgeKind,Signature,Context,EMPTY_CONTEXT, plus themake_term/bound_varfactories.encoder.py:Encoder(nn.Module) plus themake_default_op_fn/make_default_var_initdefaults.decoder.py:Decoder(nn.Module), the inverse direction.losses.py:LossEntry,LossRegistryfor plug-in reconstruction objectives.shapes/: shape backends (seq.py,tree.py,graph.py), one per supported shape category.
qiec/¶
The Quivers Indexed Effect Core (QIEC) is the stable kernel every executable
declaration of a module elaborates to: indexed families, algebraic effects,
and probabilistic programs. Its records represent kinds,
heterogeneous telescopes, indexed constructors, lexical effect instances,
effect rows, handler signatures, values, computations, and branch evidence.
serialization.py defines the deterministic qiec-json/v1 envelope;
checking.py and row_solver.py validate indexed terms and row constraints;
evaluator.py supplies the reference handler semantics; and builtins.py
provides the standard effect interfaces and process-local runtime attachments.
The package boundary is deliberate. Stable QIEC data may cross a process or
Panproto boundary, while host callables, state cells, samplers, and handler
implementations remain runtime attachments. The transpile layer projects that
stable data into a typed IRQiecModule: eight host-language targets consume
the complete computation tree through a common runtime ABI, while Stan, BUGS,
and JAGS accept an analyzer-proven closed scalar subset. Unsupported features
receive source-located capability diagnostics rather than being erased.
See Quivers Indexed Effect Core for the source and ABI
contracts.
effects/¶
Algebraic handlers for tracing, clamping observations, interventions,
masking, scaling, blocking, replay, lifting, collapsing, and
reparameterization, each a lexical handler of the kernel's Random, Score,
Compute, or Param interface. interpreter.py runs a program on the
reference machine under the active handler stack through the kernel
computation program_module.py encodes it to; checked_program.py binds a
program whose steps call a computation to the module's entry point; reparam/
contains the loc-scale, transform, NeuTra, and conjugate strategies.
transpile/¶
Cross-language emission for compiled QVR modules. lower.py produces a
target-independent IR, and renderers/ emits Stan, NumPyro, Pyro,
PyMC, Edward2, Turing, Gen, Church, WebPPL, BUGS, or JAGS source.
cli/¶
The qvr command (__init__.py's main) and its subcommands:
check.py (validate a program), lsp.py (launch the LSP), migrate.py and migrations/ (run grammar/AST migrations), repl.py plus the repl_* modules (REPL session, prompt, highlighting, Textual TUI, tab completion).
kernel/¶
Jupyter kernel: install.py registers the kernelspec; quivers_kernel.py is the in-process kernel; __main__.py is the launcher.
lsp/¶
pygls-based language server: server.py is the LSP entry point; document.py models open buffers and incremental edits.
Root-level modules¶
program.py:Program: wraps a morphism (discrete or continuous) as a differentiablenn.Module.giry.py: Backwards-compat re-export ofGiryMonadandFinStochfromquivers.stochastic.giry.
Dependency Graph¶
The package has a roughly layered dependency structure. Layers higher
in the list depend only on layers above them, with one documented
exception: dsl.compiler.declarations pulls a single helper
(_algebra_init_spec) out of analysis.init_spec, so the strict
analysis-then-dsl ordering bends at that one call site.
core/: foundational (no internal dependencies).categorical/,monadic/,arrows/: build oncore/.enriched/: builds oncore/andcategorical/.stochastic/: builds oncore/,categorical/,monadic/.continuous/: builds oncore/,categorical/,monadic/,stochastic/.analysis/: builds oncore/anddsl.ast_nodes/dsl.compiler._prelude(uses AST types and the compiler's algebra registry for static analysis).dsl/: synthesisescore/,categorical/,monadic/,enriched/,stochastic/,continuous/, plusanalysis.init_specfor one initialiser-checking helper.inference/: builds oncore/,categorical/,stochastic/,continuous/, andprogram.py.data/: builds ondsl/.diagnostics/: builds oninference/.formulas/: builds oncontinuous/,dsl/, andinference/.structural/: standalone PyTorch-only shape inference; no in-package dependencies.effects/: builds oncontinuous/and provides the handler-aware interpreter used by inference.transpile/: builds ondsl/and lowers compiled modules to target-specific schemas and source.cli/: consumesanalysis/,dsl/,kernel/,lsp/, andtranspile/.kernel/: usescli/to start the in-process REPL session.lsp/: usescli/anddsl/.program.py: depends oncore/andcontinuous/.giry.py: thin backwards-compat re-export ofstochastic/.
Users can enter at the layer their task requires: core algebra, categorical structures, probabilistic computations, or the full DSL.
Design Principles¶
- Tensors as morphisms: \(\mathcal{V}\)-relations are represented as PyTorch tensors, making them amenable to automatic differentiation and GPU acceleration.
- Value types are didactic Models: every record-shaped value (AST nodes,
SetObjectvariants,ContinuousSpacevariants, deduction categories,RuleSystem,Kleisli/ArrowMonad/CoKleislibridges,VRel/Function/Stochasticarrows) is a frozendx.Model. This buys uniform construction, structural equality, JSON round-trips viamodel_dump_json/model_validate_json, and cross-field axioms via__axioms__. Recursive sums aredx.TaggedUnionroots with akind: Literal[...]discriminator. - Tensor-bearing classes stay mutable:
Presheaf,Weight,SampleSite, andTraceaccumulatetorch.Tensorfields and are mutated in place during inference; they remain@dataclass. - Lazy composition: morphism composition creates a DAG; the final tensor is materialized only on evaluation.
- Type safety: categorical constraints (domain/codomain compatibility) are enforced statically in Python.
- Module transparency: all morphisms expose a
.module()method returning annn.Module, enabling integration with PyTorch training loops. - Algebra flexibility: the same morphism structure works with any algebra; swapping algebras changes semantics (fuzzy, Boolean, tropical, etc.) without code duplication.
- DSL pipeline as a panproto theory morphism: parsing is delegated to panproto's tree-sitter–driven AST registry; resolution is a compiler mixin that walks
ObjectExprtrees intoSetObjectorContinuousSpacevalues; each compiled program extracts to a panprotoSchemaagainstQVR_PROGRAM_PROTOCOL. The diff, migration, and lens-generation tools operate directly on.qvrprograms.