Hostname: page-component-6766d58669-vgfm9 Total loading time: 0 Render date: 2026-05-14T23:16:38.547Z Has data issue: false hasContentIssue false

A practical formalization of monadic equational reasoning in dependent-type theory

Published online by Cambridge University Press:  08 January 2025

REYNALD AFFELDT
Affiliation:
Digital Architecture Research Center, National Institute of Advanced Industrial Science and Technology (AIST), Tokyo, Japan (e-mail: reynald.affeldt@aist.go.jp)
JACQUES GARRIGUE
Affiliation:
Graduate School of Mathematics, Nagoya University, Nagoya, Japan (e-mail: garrigue@math.nagoya-u.ac.jp)
TAKAFUMI SAIKAWA
Affiliation:
Graduate School of Mathematics, Nagoya University, Nagoya, Japan (e-mail: tscompor@gmail.com)
Rights & Permissions [Opens in a new window]

Abstract

One can perform equational reasoning about computational effects with a purely functional programming language thanks to monads. Even though equational reasoning for effectful programs is desirable, it is not yet mainstream. This is partly because it is difficult to maintain pencil-and-paper proofs of large examples. We propose a formalization of a hierarchy of effects using monads in the Coq proof assistant that makes monadic equational reasoning practical. Our main idea is to formalize the hierarchy of effects and algebraic laws as interfaces like it is done when formalizing hierarchy of algebras in dependent-type theory. Thanks to this approach, we clearly separate equational laws from models. We can then take advantage of the sophisticated rewriting capabilities of Coq and build libraries of lemmas to achieve concise proofs of programs. We can also use the resulting framework to leverage on Coq’s mathematical theories and formalize models of monads. In this article, we explain how we formalize a rich hierarchy of effects (nondeterminism, state, probability, etc.), how we mechanize examples of monadic equational reasoning from the literature, and how we apply our framework to the design of equational laws for a subset of ML with references.

Information

Type
Research Article
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2025. Published by Cambridge University Press
Figure 0

Fig. 1. Comparison between a paper proof and a proof using Monae.

Figure 1

Table 1. Algebraic laws used in this article.6 See Monae (2018), file hierarchy.v) for the code.

Figure 2

Table 2. Algebraic laws defined in MathComp.

Figure 3

Fig. 2. Hierarchy of effects for the nondeterminism monad.

Figure 4

Table 3. The laws of the array monad.

Figure 5

Fig. 3. Hierarchy of effects for the probability monad and the geometrically convex monad.

Figure 6

Fig. 4. The complete hierarchy of effects discussed in this article (as generated by the command HB.graph of Hierarchy-Builder).

Figure 7

Table 4. Algebraic laws for monad morphisms.

Figure 8

Table 5. The third set of laws satisfies by the plus monad (see Section 4.1) ([~] is the notation for nondeterministic choice, Section 2.4).

Figure 9

Fig. 5. Hierarchy of effects for the plus monad and the plus array monad.

Figure 10

Table 6. Laws adapted from the array monad.

Figure 11

Table 7. Laws for cnew and cchk (excerpt).

Figure 12

Table 8. Derivation of rtl_tl_self.Freshly inserted subexpressions are underlined. The notation stands for Cons _ _ x y.

Submit a response

Discussions

No Discussions have been published for this article.