โ† back to index

Combining Effects and Coeffects via Grading

Gaboardi, Katsumata, Orchard, Sato ยท ESOP 2021 ยท arxiv arXiv:2007.06237

Prereqs: ๐Ÿž Staton 2025 (Hoare triples). 5 min.

Hoare triples with grades: a tag on every computation that tracks side effects โ€” how many resources it uses, how much noise it adds, how far it can branch. The grade algebra tells you what happens when you compose.

Grades track effects

A grade is an annotation on a computation. Plain Hoare logic says {P} c {Q}. Graded Hoare logic says {P} c : e {Q} โ€” "c has effect e." When you compose, the grades compose too: if cโ‚ has grade eโ‚ and cโ‚‚ has grade eโ‚‚, then cโ‚;cโ‚‚ has grade eโ‚ยทeโ‚‚ (monoid multiplication).

P c grade e Q graded Hoare triple: the grade tags the computation
Python
P r c₁ R s c₂ Q = P r·s grades compose: r then s becomes r·s

The grade monoid

Grades form a wpmonoid: an identity (no effect) and an associative binary operation (compose effects). Different monoids track different things.

Python

Confidence: Simplified. Real grades are semiring-indexed, not just monoids. Same composition idea.

Graded COMP โ€” the handshake with budgets

Staton's COMP rule chains postconditions. Gaboardi's graded COMP chains postconditions and grades. If {P} cโ‚ : eโ‚ {R} and {R} cโ‚‚ : eโ‚‚ {Q}, then {P} cโ‚;cโ‚‚ : eโ‚ยทeโ‚‚ {Q}. The handshake carries a budget.

Python

Graded Freyd category

The categorical setting is a graded Freyd category: a pair (C, F) where C is a category of pure values and F is a graded family of categories of effectful computations. The grading indexes which effects are allowed. Pure computations embed with grade 1 (no effect).

Python

Confidence: Analogy. Real Freyd categories are indexed by a grading semiring. This captures the composition law but not the full categorical structure.

Effects meet coeffects

Effects track what a computation does to the world (writes, exceptions, nondeterminism). Coeffects track what it needs from the world (reads, resource usage, context). Gaboardi unifies both in one grading: the effect monoid and coeffect comonoid are two sides of the same semiring.

Python

Notation reference

Paper Python Meaning
{P} c : e {Q}assert P; c(); assert Q; grade=eGraded Hoare triple
eโ‚ ยท eโ‚‚grade_op(e1, e2)Grade composition
10 or 1.0Identity grade (no effect)
T_eGradedComp(fn, e)Graded monad at grade e
(C, F)# pure + effectfulGraded Freyd category
Neighbors

Other paper pages

Foundations (Wikipedia)

Translation notes

All examples use simple numeric grades (counts, products). The paper works with graded monads indexed by a semiring, graded Freyd categories, and the interaction between effect and coeffect gradings via a distributive law. For example: the graded COMP on this page multiplies two fidelity floats. In the paper, the same rule works over a preordered semiring where the composition operation may be non-commutative and where the ordering constrains subeffecting (you can always over-approximate an effect grade). The composition structure is identical. The lattice-theoretic subtleties are not.

Ready for the real thing? arxiv Read the paper. Start at ยง3 for graded monads, ยง5 for the graded Hoare logic.

Framework connection: The Natural Framework pipeline's composition with contracts is graded COMP โ€” each stage carries an effect grade that composes through the handshake. (jkThe Handshake, jkAmbient Category)