Skip to content

catRgory/catlab

Repository files navigation

catlab

Lifecycle: experimental R-CMD-check codecov

An R port of core Catlab.jl functionality — categorical algebra for applied category theory. Provides limits/colimits, graph rewriting (DPO/SPO/SqPO), undirected wiring diagrams, data migration, typed ACSets, and structured cospans.

Built on top of the acsets package.

Installation

Install from GitHub (requires the acsets dependency):

# install.packages("remotes")
remotes::install_github("catRgory/acsets")
remotes::install_github("catRgory/catlab")

Quick example

library(catlab)

# Create two graphs and glue them along a shared vertex
g1 <- path_graph(3)   # 1 → 2 → 3
g2 <- path_graph(2)   # 1 → 2

# Identify vertex 3 of g1 with vertex 1 of g2
interface <- Graph()
add_vertex(interface)
f <- ACSetTransformation(list(V = 3L, E = integer(0)), interface, g1)
g <- ACSetTransformation(list(V = 1L, E = integer(0)), interface, g2)
result <- pushout(f, g)
nv(result$pushout)  # 4 vertices: the two paths joined end-to-end
ne(result$pushout)  # 3 edges

# DPO graph rewriting: add an edge between two vertices
I <- Graph(); add_vertices(I, 2)
L <- Graph(); add_vertices(L, 2)
R <- path_graph(2)  # 1 → 2
l <- ACSetTransformation(list(V = c(1L, 2L), E = integer(0)), I, L)
r <- ACSetTransformation(list(V = c(1L, 2L), E = integer(0)), I, R)
add_edge_rule <- rule(l, r)

triangle <- cycle_graph(3)
result <- rewrite(add_edge_rule, triangle)
ne(result)  # 4 edges (added one to the triangle)

Features

  • Graphs — Directed, symmetric, reflexive, weighted, and labelled graph schemas with constructors (path_graph, cycle_graph, complete_graph) and query functions (nv, ne, neighbors)
  • Finite categories & functorsFinCat, FinFunctor, and ACSetTransformation with naturality checking
  • Limits & colimitsproduct, pullback, equalizer, coproduct, pushout, coequalizer, plus terminal/initial objects
  • Homomorphism search — Backtracking search for graph homomorphisms (find_homomorphism, find_all_homomorphisms, is_homomorphic) with monic and initial-match support
  • Graph rewriting — DPO, SPO, and SqPO semantics via rule(), rewrite(), get_matches(), and rewrite_match()
  • Data migration — Pull back (delta_migrate) and push forward (sigma_migrate) data along schema functors
  • Undirected wiring diagrams — Compositional system specification with a concise DSL (uwd(), relation())
  • Typed ACSets — Type-stratified graphs with typed_product (pullback over types) and typed_coproduct
  • Structured cospans — Open systems with interfaces; compose via compose_cospans, otimes_cospans, and oapply_cospans
  • Visualization — Graphviz/DOT output for graphs, Petri nets, and UWDs via to_dot() / to_graphviz()

Vignettes

Detailed tutorials are available as package vignettes:

Author

Simon Frost (@sdwfrost) — ORCID 0000-0002-5207-9879

License

MIT — see LICENSE for details.

Part of the catRgory ecosystem

catlab is part of catRgory, an R ecosystem for applied category theory. It builds on:

  • acsets — Acyclic C-Sets (schemas, instances, and morphisms)

About

Categorical algebra for R — limits, colimits, graph rewriting, wiring diagrams

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages