AlgebraicJulia: a compositional approach to technical computing

Compositional Structures for Systems Engineering and Design @ NIST

Evan Patterson

Topos Institute

AlgebraicJulia

11/4/22

Outline

  1. Overview and aims
  2. Design themes
  3. Applications to scientific modeling

AlgebraicJulia core developers

James Fairbanks
University of Florida

Angeline Aguinaldo
University of Maryland & Topos Institute

Andrew Baas
George Tech Research Institute

Kris Brown
Topos Institute

Evan Patterson
Topos Institute

Xiaoyan Li
University of Saskatchewan

Sophie Libkind
Stanford University & Topos Institute

Owen Lynch
Topos Institute

What is AlgebraicJulia?

AlgebraicJulia is

  • a collaborative project to operationalize applied category theory (ACT)
  • as a family of open source software packages written in Julia
  • focused on technical computing for science and engineering

Aims of AlgebraicJulia

Present wave of ACT ongoing for 10+ years:

  • SMCs and wiring diagrams
  • Open systems via cospans
  • Functorial semantics in science
  • Categorical DBs and data migration

Our aim is to operationalize this novel math as useful technology:

  • Implement CT abstractions as highly general software
  • Instantiate these abstractions in specific scientific and engineering domains
  • Interoperate between domains and applications, functorially

Organization of AlgebraicJulia

  • Generic framework for ACT in Catlab.jl
  • Web-based interactive tools in Semagrams.jl
  • Builing on these, modeling frameworks for specific domains, such as
    • epidemiology (Petri nets, stock flow diagrams)
    • dynamical systems
    • computational physics (simplicial sets, discrete exterior calculus)

Design themes in AlgebraicJulia

Category theory gains computational content in three ways:

  1. Designing programs and programming languages
  2. Proving theorems about categories and/or programs
  3. Modeling the world, computationally

Not mutually exclusive, but Catlab is mostly about #3.

Designing vs modeling with categories

CT in progamming language theory:

  • Categorical concepts model the program or language
  • Types/functions viewed as objects/morphisms in a category (think Hask)

CT as a modeling language:

  • Categorical concepts model the subject-matter domain
  • Implies nothing about the host programming language

Having a model-program “isomorphism” is nice when it works, but

  • host type system imposes heavy constraints
  • cost of extensibility is very high: have to write a new language!

We take a pragmatic approach to designing categorical software.

Syntax and semantics

CT and categorical logic enrich the traditional syntax-semantics distinction, making both more flexible:

  • Syntax is not relegated to meta-math but becomes part of math itself
  • Semantics is then just a functor (“functorial semantics”)

In conventional progamming:

  • syntax is defined by
    • the host language
    • sometimes by DSLs
  • in either case, syntax is symbolic, based on expression trees

In AlgebraicJulia:

  • DSLs give a “frontend” for syntax, via
    • Julia macros
    • Semagrams graphical editor
  • but underlying data is combinatorial

Example: Operad of UWDs

Undirected wiring diagrams (UWDs) form an operad, a categorical gadget for hierarchical syntax.

Julia syntax

@relation macro defines UWDs:

using Catlab.Programs

uwd = @relation (x,y,z) begin
  R(x,w)
  S(y,w)
  T(z,w)
end;

Categorical syntax

UWD as combinatorial object (or visualization thereof):

using Catlab.Graphics

to_graphviz(uwd, box_labels=:name)

UWDs: One syntax, many semantics

Undirected wiring diagrams are a syntax for composing…

  • Spans
    • Data tables in a conjunctive query
  • Structured cospans
    • Open graphs
    • Open Petri nets (AlgebraicPetri)
    • Open free diagrams in a category (Decapodes)
  • Open dynamical systems via “resource sharing” (AlgebraicDynamics)

All of these algebras are implemented in AlgebraicJulia, with benefits arising from the shared abstraction.

\(\cat{C}\)-sets as generic data structure

A very useful generic data structure is provided by:

  • \(\cat{C}\)-sets, or copresheaves on a category \(\cat{C}\)
  • more generally, attributed \(\cat{C}\)-sets or “acsets”

Example: UWDs are acsets on the following schema.

to_graphviz(SchRelationDiagram, prog="dot")

Example: UWDs as acsets

Box name
1 R
2 S
3 T
Port box junction
1 1 1
2 1 4
3 2 2
4 2 4
5 3 3
6 3 4
OuterPort outer_junction
1 1
2 2
3 3
Junction variable
1 x
2 y
3 z
4 w

Operations on \(\cat{C}\)-sets

The utility of \(\cat{C}\)-sets as an abstraction are the operations you can do on them:

  • Limits
  • Colimits
  • Data migration
    • using diagram categories, not Kan extensions
  • Rule-based rewriting (AlgebraicRewriting.jl)
    • using double pushout (DPO) rewriting and related methods
  • Propositional logic of a topos

All of these are implemented in AlgebraicJulia.

Applications to scientific modeling

Current areas of focus:

  • epidemiology
  • computational physics

Motivating structured scientific modeling

Modern scientific models can be very complex, yet

  • such complexity is not accidental
  • but is the result of the combining and extending simpler models

This is by necessity, since scientific models are (still) created by humans!

So, on one hand:

Even very complicated models tend to have compositional structure, at least implicitly.

But, on the other hand:

Existing modeling tools tend to miss this structure entirely.

Example: A COVID-19 model

Toward structure scientific modeling

A major aim of AlgebraicJulia is to enable building scientific models in an explicitly structured way.

In the epidemiology domain, using compartmental models based on

we have two main notions of composition:

  1. composition of open systems
    • “just” colimits, in CT terms
  2. stratification, e.g., by demographic characteristics
    • “just” limits, in CT terms

Example: SIR-type model with vaccination

Composition pattern
(UWD)

Constituent models
(open Petri nets)

Composite model
(Petri net)

Stratification via pullback

  • Stratification partitions a population into relevant subpopulations (demographics, location, etc)
  • Products of Petri nets are not enough
  • But products of typed Petri nets are:
    • Products in slice category \(\Petri/P_{\textsf{type}}\)
    • Equivalently, pullbacks in \(\Petri\)

Pullback of Petri nets

Example: SIR-type model with quarantining

\(P_{\textsf{type}}\)

\(P_{\textsf{disease}}\)

\(P_{\textsf{strata}}\)

Turning to computational physics

Computational physics has some challenges analogous to epidemiology:

  • Complex models are needed, e.g., for engineering design
  • “Multiphysics” models compose physics of distinct phenomena, e.g., reaction-diffusion-advection

But also its own unique challenges:

  • Can involve complex geometric domains
  • Unlike for ODEs, state-of-the-art for PDEs is to write bespoke solvers!

We’ve taken some first steps in applying ACT in this domain (work ongoing).

Rigorizing Tonti diagrams

Maxwell’s equation become more “strongly typed” in exterior calculus:

Tonti diagram for classical E&M
(“Maxwell’s house” ala Bossavit)

Cartesian diagram for Maxwell’s house

PDE simulator pipeline

  1. Formulate physics diagrammatically
    • using exterior calculus
    • composing different physical principles
  2. Define geometry (CombinatorialSpaces.jl)
    • 2D semi-simplicial sets (\(\cat{C}\)-sets!)
    • discrete exterior calculus (DEC) operators
  3. Generate simulator (Decapodes.jl)
    • computation graph (directed wiring diagram)
    • Julia code generation

Example result: Conjugate heat transfer
(Heat transport by moving fluid)

Thanks!

Resources

Contributing

We welcome new contributors at all levels of experience.
Please reach out if interested!