Introduction
Program logics provide a formal way to specify what programs can or should do, and to verify that programs meet their specifications. For programs with nondeterminism, a logic usually either takes a demonic interpretation, requiring all executions to satisfy the specification, or an angelic one requiring the existence of such an execution. For divergence, a total interpretation requires termination, while a partial/liberal one allows divergence. These logics are often presented as deductive systems over triples relating a precondition p p p , a program c c c , and a postcondition q q q . The semantics of triples can often be phrased via Dijkstra’s weakest precondition (w p wp w p ) or strongest postcondition (s p sp s p ) predicate transformers [10 , 11 , 12 ] .
For example, the triple { p } c { q } \{p\}c\{q\} { p } c { q } is valid in partial correctness Hoare logic [3 , 16 ] exactly when p ⇒ d w l p ⟦ c ⟧ ( q ) p \Rightarrow\mathit{dwlp}\llbracket c\rrbracket(q) p ⇒ dwlp [ [ c ] ] ( q ) , where dwlp is the demonic, liberal wp variant [11 ] . In fact, this logic admits a second such characterization [37 ] : the triple is also valid exactly when a s p ⟦ c ⟧ ( p ) ⇒ q \mathit{asp}\llbracket c\rrbracket(p) \Rightarrow q asp [ [ c ] ] ( p ) ⇒ q , for asp , the angelic, total sp variant.
These predicate transformer characterizations of triples are not limited to reasoning about correctness. O’Hearn’s incorrectness logic [30 ] , which specifies which states a program can reach (e.g., error states), uses the same transformer with the implication reversed; the triple [ p ] c [ q ] [p]c[q] [ p ] c [ q ] is valid exactly when q ⇒ a s p ⟦ c ⟧ ( p ) q \Rightarrow\mathit{asp}\llbracket c\rrbracket(p) q ⇒ asp [ [ c ] ] ( p ) . Unlike Hoare logic, there is no equivalent wp characterization [37 ] . Incorrectness logic triples are called under-approximate , as q q q under-approximates the reachable states a s p ⟦ c ⟧ ( p ) \mathit{asp}\llbracket c\rrbracket(p) asp [ [ c ] ] ( p ) . In contrast, Hoare logic triples are over-approximate , as q q q over-approximates the reachable states. For simple imperative programs, Verscht and Kaminski [37 ] taxonomize these under- and over-approximate triples for four wp variants and four sp variants [37 , 39 ] , corresponding to the standard interpretations of nondeterminism and nontermination: angelic-demonic and total-liberal.1
Triples and wps have also been used to verify monadic programs [13 , 14 , 26 , 28 , 29 , 32 , 33 , 34 , 35 ] , which can be shallowly embedded in proof assistants, making verification convenient. A key insight was that the specifications of monadic programs also have monadic structure, so they compose with ret and bind as the programs do. Swamy et al. [33 , 34 ] showed that wps form such a monad. But how do we define wps modularly, one effect at a time? Maillard et al. [26 ] observe that if we start with a base monotone wp monad W P u r e ( A ) : = ( A → P ) → P \mathrm{WPure}(A) := (A \to\mathbb{P}) \to\mathbb{P} WPure ( A ) := ( A → P ) → P , where P \mathbb{P} P is the type of propositions, we can add effects with the same monad transformers as for computation monads. For instance, the state monad transformer turns W P u r e \mathrm{WPure} WPure into the usual monad of stateful wps , of type S t T ( W P u r e ) ( S , A ) : = S → ( A × S → P ) → P ≅ ( A × S → P ) → ( S → P ) \mathrm{StT}(\mathrm{WPure})(S,A) := S \to(A \times S \to\mathbb{P}) \to\mathbb{P} \cong(A \times S \to\mathbb{P}) \to(S \to\mathbb{P}) StT ( WPure ) ( S , A ) := S → ( A × S → P ) → P ≅ ( A × S → P ) → ( S → P ) . Such wp monads have been used in verification frameworks including F⋆ [2 , 26 , 33 , 34 ] and Loom [14 ] .
Monads of sps are less understood. For a start, monotone sps alone do not form a lawful monad. Maillard et al. obtain a base sp monad of type S P o s t ( A ) : = P → ( A → P ) \mathrm{SPost}(A) := \mathbb{P} \to(A \to\mathbb{P}) SPost ( A ) := P → ( A → P ) by further restricting the carrier to predicate transformers whose postconditions imply the precondition. We observe that the resulting monad can specify not only pure programs, but also nondeterministic and nonterminating ones, under an angelic and total interpretation. In this paper we introduce the first sp monads for all four interpretations of nondeterminism and nontermination by instead restricting the carrier to standard classes of predicate transformers [8 ] : the angelic monad variants are disjunctive (i.e., preserve potentially infinite non-empty disjunctions), demonic ones conjunctive (i.e., preserve such conjunctions), total ones strict (i.e., ⊥ \bot ⊥ precondition results in ⊥ \bot ⊥ postcondition), and liberal ones terminating (i.e., ⊤ \top ⊤ precondition results in $\top postcondition). Applying similar restrictions to the monotone wp monad gives one wp monad per interpretation. While these four wp monads are submonads of the W P u r e \mathrm{W}^{\mathrm{Pure}} W Pure monad above, the four sp monads are more interesting, since each of them needs its own ret and bind tailored to its carrier restriction.
When also considering effects such as state, a further challenge is effect modularity : adding the ability to reason about new effects by applying a monad transformer, which yields a lawful monad for free. The state monad transformer, however, does not turn these base sp monads into the monads we expect for stateful sps , producing instead: StT ( SPost ) ( S , A ) = S → P → ( A × S → P ) \operatorname{StT}(\operatorname{SPost})(S,A)=S\to\mathbb{P}\to(A\times S\to\mathbb{P}) StT ( SPost ) ( S , A ) = S → P → ( A × S → P ) . Such an sp takes one initial state and, separately, one precondition proposition, whereas a stateful sp should take a precondition that is a predicate over initial states, as in the type ( S → P ) → ( A × S → P ) (S\to\mathbb{P})\to(A\times S\to\mathbb{P}) ( S → P ) → ( A × S → P ) . Underlying this is a more basic challenge: even setting modularity aside, there was no known way to construct lawful stateful sp monads of the expected shape, which need a restricted carrier and a matching ret and bind, separately for each of the four interpretations.
We solve both challenges by generalizing our sp monads to Hughes arrows with application [17 ] , since arrow transformers then yield stateful sp monads of the expected shape in an effect-modular way. We start with a base arrow of type SPArr ( B , A ) : = ( B → P ) → ( A → P ) \operatorname{SPArr}(B,A):=(B\to\mathbb{P})\to(A\to\mathbb{P}) SPArr ( B , A ) := ( B → P ) → ( A → P ) satisfying a variant of the carrier restrictions above. By fixing the input type B B B to unit, an arrow with application yields a monad [24 ] :
SP ( A ) : = SPArr ( u n i t , A ) = ( u n i t → P ) → ( A → P ) ≅ P → ( A → P ) \operatorname{SP}(A):=\operatorname{SPArr}(\mathrm{unit},A)=(\mathrm{unit}\to\mathbb{P})\to(A\to\mathbb{P})\cong\mathbb{P}\to(A\to\mathbb{P}) SP ( A ) := SPArr ( unit , A ) = ( unit → P ) → ( A → P ) ≅ P → ( A → P )
Moreover, since arrows have an input type, which arrow transformers can extend, applying the state arrow transformer gives us StArrT ( SP ) ( S , B , A ) = ( B × S → P ) → ( A × S → P ) \operatorname{StArrT}(\operatorname{SP})(S,B,A)=(B\times S\to\mathbb{P})\to(A\times S\to\mathbb{P}) StArrT ( SP ) ( S , B , A ) = ( B × S → P ) → ( A × S → P ) , which, when the input type is fixed, provides the expected shape of a stateful sp monad. Crucially, the resulting stateful monads inherit the carrier restriction and lawfulness of the base arrow, since the state arrow transformer only adds the state to the input and output types.
This allows us to give a taxonomy of these base and stateful sp and wp monads (Figure 1 , in § 2.2 ) and show that the relationships established by Verscht and Kaminski [37 ] between under- and over-approximate triples already hold for the predicate transformers themselves. Additionally, we use the predicate transformer monads to define under- and over-approximate triples and, where possible, Dijkstra monads [2 , 26 , 33 ] . A Dijkstra monad is a monad-like structure of computations indexed by elements of a specification monad [26 ] . A specification monad is ordered by specification precision so that bind is monotone [15 , 20 ] .
Figure 1. Monadic relations between the monads of predicate transformers, with the SP monads on top and the WP monads below, (a) for the base monads and (b) for the monads with state. In (a), S P D S P \mathrm{SP}_{\mathrm{DSP}} SP DSP and S P A S L P \mathrm{SP}_{\mathrm{ASLP}} SP ASLP are isomorphic (≅ \cong ≅ ) to S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP and S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP . Unmarked implications relate the elements of the two monads; in (b), * and ⋆ mark implications that compare their aspOf and dslpOf components, respectively. An adjunction points from its left adjoint to its right adjoint.
We make the following contributions :
We introduce the first lawful sp monads covering all four interpretations of nondeterminism and nontermination. They are derived from arrows with application restricted to preserve non-empty disjunctions (angelic) or conjunctions (demonic), and ⊥ \bot ⊥ (total) or ⊤ \top ⊤ (liberal). This allows adding state using arrow transformers, whereas monad transformers do not yield the expected monads. We also obtain four base wp monads by applying similar restrictions directly to the monotone wp monad [26 ] . Unlike the wp monads, which are all submonads of the same monad, each sp monad has its own ret and bind.
We recast Verscht and Kaminski’s taxonomy of triples [37 ] as a taxonomy of the eight w p wp w p and s p sp s p monads, and show that the relationships they establish between under- and over-approximate triples already hold for predicate transformers, both for the base monads and for their versions with state.
We construct triples (precondition, monadic computation, postcondition) in terms of monad morphisms from our base computation monad of nondeterminism and nontermination into our eight w p wp w p and s p sp s p monads. We also add state and exceptions to both the computation monad and the w p wp w p and s p sp s p monads and lift the monad morphisms for free, through monad transformers as in DM4All [2 , 26 ] and through arrow transformers for the s p s sps s p s . This, for instance, allows us to define the semantics of incorrectness logic triples [ p ] c [ q ] [p]c[q] [ p ] c [ q ] for monadic programs c c c as q ⇒ a s p ⟦ c ⟧ ( p ) q \Rightarrow asp\llbracket c\rrbracket(p) q ⇒ a s p [ [ c ] ] ( p ) , analogous to O’Hearn’s original definition above [30 ] . We use the monad morphisms to prove that simple programs satisfy triples for both correctness and incorrectness in a style inspired by Loom [14 ] , though focused not on automation but on generality, since we consider both over- and under-approximation, s p s sps s p s , and general w p s wps w p s .
We show that our new a s p asp a s p and d s l p dslp d s l p monads and all w p wp w p monads form specification monads [26 ] . This lets us verify monadic programs by comparing the specification produced by one of our monad morphisms with the intended specification using the specification monad’s order. This covers both over- and under-approximation by flipping the order, and as for triples, we use this to verify simple monadic programs. As these predicate transformer monads are specification monads, our monad morphisms targeting them give rise to Dijkstra monads [26 ] . The d s p dsp d s p and a s l p aslp a s l p monads have binds that are not monotone with respect to their natural orders, so they are not specification monads, which limits how specifications can be weakened and composed.
For intended specifications expressed as triples, we show that the Loom-inspired verification style and the Dijkstra monad-inspired one are conceptually equivalent for the specification monads based on w p s wps w p s as well as over-approximate a s p asp a s p and under-approximate d s l p dslp d s l p . This requires turning triples into predicate transformers, which we show is possible for the specification monads above, but not for under-approximate a s p asp a s p and over-approximate d s l p dslp d s l p .
To study the expressive power of our stateful s p sp s p and w p wp w p specification monads, we compare them to stateful versions of existing specification monads [26 ] , such as predicates on results Pred ( A ) = A → P \operatorname{Pred}(A)=A\to\mathbb{P} Pred ( A ) = A → P and pairs of pre- and postconditions, by establishing Galois connections, some of which are isomorphisms. In particular, we show that the known isomorphism [25 ] between the predicate monad Pred ( A ) \operatorname{Pred}(A) Pred ( A ) and the base s p sp s p monad of Maillard et al. extends to isomorphisms between a s p asp a s p and d w l p dwlp d w l p and between d s l p dslp d s l p and a w p awp a w p as specification monads in the stateful setting.
We fully mechanize all definitions and proofs in Rocq.
Artifact and use of AI. The Rocq development is available at https://github.com/lastland/SpecMonad/tree/main/Code. It was developed in part interactively with the help of AI coding agents (Claude, Codex, and OpenCode), especially for proofs, which are machine-checked by Rocq. Any AI-generated code that affects the trusted computing base (definitions and theorem statements) was carefully reviewed by the authors. AI agents were used to help with \LaTeX programming and to improve some of the text of the paper; we wrote the original text, checked all proposed improvements, and take full responsibility for the paper and artifact.
Background: Weakest Precondition Monads
Monadic computations can be specified in terms of weakest precondition predicate transformers [33 , 34 ] . A w p wp w p maps predicates on the output called postconditions to predicates on the input called preconditions: if the precondition holds, then the computation is guaranteed to produce a result satisfying the postcondition. A pure computation of type A A A can be understood as having an output of type A A A and an input of type u n i t \mathrm{unit} unit . Therefore, a w p wp w p for a pure computation of type A A A is a function from predicates on A A A to predicates on u n i t \mathrm{unit} unit , specifically the following type: ( A → P ) → ( u n i t → P ) ≅ ( A → P ) → P (A \to\mathbb{P}) \to(\mathrm{unit} \to\mathbb{P}) \cong(A \to\mathbb{P}) \to\mathbb{P} ( A → P ) → ( unit → P ) ≅ ( A → P ) → P . Prior work [2 , 19 ] noted that this is the type of a continuation monad on the return type P \mathbb{P} P with the corresponding r e t \mathrm{ret} ret and b i n d \mathrm{bind} bind :
r e t v : = λ p o s t . p o s t v , b i n d m f : = λ p o s t . m ( λ a . f a p o s t ) . (1) \begin{aligned}
\mathrm{ret}\ v &:= \lambda\ \mathit{post}.\ \mathit{post}\ v,\\
\mathrm{bind}\ m\ f &:= \lambda\ \mathit{post}.\ m\ (\lambda a.\ f\ a\ \mathit{post}). \tag*{(1)}
\end{aligned} ret v bind m f := λ post . post v , := λ post . m ( λa . f a post ) . (1)
We call this base w p wp w p monad WP. This monadic structure of w p s wps w p s has been exploited to naturally compose specifications, and to map monadic computations to their predicate transformer semantics [26 ] .
Predicates over any type are ordered as subsets, or equivalently by pointwise logical implication, which we write ⊑ \sqsubseteq ⊑ : for φ , ψ : A → P \varphi,\psi: A \to\mathbb{P} φ , ψ : A → P , φ ⊑ ψ \varphi\sqsubseteq\psi φ ⊑ ψ iff ∀ ( a : A ) . φ ( a ) ⇒ ψ ( a ) \forall(a : A).\,\varphi(a) \Rightarrow\psi(a) ∀ ( a : A ) . φ ( a ) ⇒ ψ ( a ) . In general, one works with monotone predicate transformers that preserve the order relations on predicates [8 , 12 ] . The monotonicity condition on w p wp w p is, for all postconditions q 1 , q 2 : A → P q_1,q_2 : A \to\mathbb{P} q 1 , q 2 : A → P :
q 1 ⊑ q 2 ⇒ w p ( q 1 ) ⊑ w p ( q 2 ) . q_1 \sqsubseteq q_2 \Rightarrow wp(q_1) \sqsubseteq wp(q_2). q 1 ⊑ q 2 ⇒ w p ( q 1 ) ⊑ w p ( q 2 ) .
For example, r e t x \mathrm{ret}\ x ret x is a monotone predicate transformer, as the condition q 1 ⊑ q 2 ⇒ ( r e t x ) ( q 1 ) ⊑ ( r e t x ) ( q 2 ) q_1 \sqsubseteq q_2 \Rightarrow(\mathrm{ret}\ x)(q_1) \sqsubseteq(\mathrm{ret}\ x)(q_2) q 1 ⊑ q 2 ⇒ ( ret x ) ( q 1 ) ⊑ ( ret x ) ( q 2 ) unfolds into ( ∀ ( a : A ) . q 1 ( a ) ⇒ q 2 ( a ) ) ⇒ q 1 ( x ) ⇒ q 2 ( x ) (\forall(a : A).\,q_1(a) \Rightarrow q_2(a)) \Rightarrow q_1(x) \Rightarrow q_2(x) ( ∀ ( a : A ) . q 1 ( a ) ⇒ q 2 ( a )) ⇒ q 1 ( x ) ⇒ q 2 ( x ) which trivially holds. b i n d m f \mathrm{bind}\ m\ f bind m f is also a monotone predicate transformer, provided that both m m m and f f f are monotone predicate transformers.
Moreover, not only predicates are ordered, but also predicate transformers themselves can be ordered in a pointwise manner:
w p 1 ⊑ w p 2 iff ∀ ( q : A → P ) . w p 1 ( q ) ⊑ w p 2 ( q ) . wp_1 \sqsubseteq wp_2 \quad\text{iff}\quad\forall(q : A \to\mathbb{P}).\,wp_1(q) \sqsubseteq wp_2(q). w p 1 ⊑ w p 2 iff ∀ ( q : A → P ) . w p 1 ( q ) ⊑ w p 2 ( q ) .
Then, a natural question is whether the monadic structure preserves such order on predicate transformers. For weakest precondition monads, both r e t \mathrm{ret} ret and b i n d \mathrm{bind} bind preserve the order above.
For stateful programs with state type S S S , preconditions must refer to the initial state and postconditions to the result and final state. Following Maillard et al. [26 ] , we can do so systematically by applying the state monad transformer S t T \mathrm{StT} StT to the WP monad described above. Now the postcondition predicates are not only on the underlying type, but also on the state ( A × S → P ) (A \times S \to\mathbb{P}) ( A × S → P ) , while preconditions are predicates over the initial state ( S → P ) (S \to\mathbb{P}) ( S → P ) . This gives the following monadic structure on StT ( WP ) ( S , A ) : = ( A × S → P ) → S → P \operatorname{StT}(\operatorname{WP})(S,A) := (A \times S \to\mathbb{P}) \to S \to\mathbb{P} StT ( WP ) ( S , A ) := ( A × S → P ) → S → P , which is essentially the same as above, but threading around the state:
ret x : = λ s post . post ( x , s ) , bind w f : = λ s post . w s ( λ ( x , s ′ ) . f x s ′ post ) . (2) \begin{aligned}
\operatorname{ret}\ x &:= \lambda s\ \operatorname{post}.\ \operatorname{post}\ (x,s),\\
\operatorname{bind}\ w\ f &:= \lambda s\ \operatorname{post}.\ w\ s\ (\lambda(x,s').\ f\ x\ s'\ \operatorname{post}). \tag*{(2)}
\end{aligned} ret x bind w f := λ s post . post ( x , s ) , := λ s post . w s ( λ ( x , s ′ ) . f x s ′ post ) . (2)
Again, these predicates are naturally ordered, and we restrict the carrier to predicate transformers that are monotone. For this monad, the monadic structure (ret, bind) also respects the monotonicity condition.
Strongest Postcondition Monads
In contrast to w p s wps w p s , s p s sps s p s map preconditions to postconditions. In the base case, an s p sp s p is therefore a function from predicates on 1 1 1 to predicates on A A A , of type ( 1 → P ) → ( A → P ) ≅ P → ( A → P ) (1 \to\mathbb{P}) \to(A \to\mathbb{P}) \cong\mathbb{P} \to(A \to\mathbb{P}) ( 1 → P ) → ( A → P ) ≅ P → ( A → P ) . Unlike for w p s wps w p s , there is no well-known monad of a similar shape to inspire our definitions of ret and bind.
Base s p sp s p Monads
Maillard et al. [26 ] define a base s p sp s p monad, SPost ( A ) : = P → ( A → P ) \operatorname{SPost}(A) := \mathbb{P} \to(A \to\mathbb{P}) SPost ( A ) := P → ( A → P ) , with the following ret and bind, inspired by the treatment of skip and sequencing in the classical strongest postcondition calculus [12 ] :
ret v : = λ pre a . pre ∧ a = v , bind m f : = λ pre b . ∃ a . f a ( m pre a ) b . (3) \begin{aligned}
\operatorname{ret}\ v &:= \lambda\operatorname{pre}\ a.\ \operatorname{pre} \land a = v,\\
\operatorname{bind}\ m\ f &:= \lambda\operatorname{pre}\ b.\ \exists a.\ f\ a\ (m\ \operatorname{pre}\ a)\ b. \tag*{(3)}
\end{aligned} ret v bind m f := λ pre a . pre ∧ a = v , := λ pre b . ∃ a . f a ( m pre a ) b . (3)
To satisfy the monad laws, they consider monotone s p s sps s p s of the smaller type ( pre : P ) → ( A → P / pre ) (\operatorname{pre}:\mathbb{P}) \to(A \to\mathbb{P}_{/\operatorname{pre}}) ( pre : P ) → ( A → P / pre ) , where P / pre = { p : P ∣ p ⇒ pre } \mathbb{P}_{/\operatorname{pre}} = \{p : \mathbb{P} \mid p \Rightarrow\operatorname{pre}\} P / pre = { p : P ∣ p ⇒ pre } . This smaller type suffices for the monad laws, and we observe that it is, in fact, equivalent to a carrier of disjunctive, strict s p s sps s p s .
Disjunctivity and strictness (along with conjunctivity and termination, introduced shortly) are properties of predicate transformers that form classes closed under sequential composition. Therefore, they are usually called homomorphism properties. Using an s p sp s p as an example, for every non-empty family of preconditions ( p i ) i ∈ I (p_i)_{i \in I} ( p i ) i ∈ I , the disjunctivity condition on s p sp s p states that:
s p ( ⋁ i ∈ I p i ) = ⋁ i ∈ I s p ( p i ) sp\left(\bigvee_{i \in I} p_i\right) = \bigvee_{i \in I} sp(p_i) s p ( i ∈ I ⋁ p i ) = i ∈ I ⋁ s p ( p i )
where the disjunction of predicates is taken pointwise:
( ⋁ i ∈ I p i ) ( x ) ⟺ ∃ ( i : I ) , p i ( x ) . \left(\bigvee_{i \in I} p_i\right)(x) \iff\exists(i : I), p_i(x). ( i ∈ I ⋁ p i ) ( x ) ⟺ ∃ ( i : I ) , p i ( x ) .
An s p sp s p is strict when it maps ⊥ \bot ⊥ to ⊥ \bot ⊥ , i.e., preserves empty disjunctions. The disjunctivity condition is defined with respect to preconditions of any shape. In order to provide intuition, we briefly switch to the stateful setting, where computations take an initial state and (potentially) produce an output.
For disjunctivity, Back and von Wright [8 ] showed that disjunctive predicate transformers correspond to angelic interpretations of program semantics.
For now, fix an output r r r that the computation can produce, and consider the initial states from which it can produce r r r . An angelic s p sp s p includes r r r in the postcondition s p ( p ) sp(p) s p ( p ) when some of these initial states satisfy the precondition p p p . For a precondition p = p 1 ∨ p 2 p = p_1 \lor p_2 p = p 1 ∨ p 2 , some initial state that produces r r r satisfies p 1 ∨ p 2 p_1 \lor p_2 p 1 ∨ p 2 (r ∈ s p ( p 1 ∨ p 2 ) r \in sp(p_1 \lor p_2) r ∈ s p ( p 1 ∨ p 2 ) ) only if that initial state satisfies p 1 p_1 p 1 (r ∈ s p ( p 1 ) r \in sp(p_1) r ∈ s p ( p 1 ) ) or satisfies p 2 p_2 p 2 (r ∈ s p ( p 2 ) r \in sp(p_2) r ∈ s p ( p 2 ) ), with the same argument working for any non-empty family of preconditions. The precondition is checked on each initial state separately.
For strictness, Back and von Wright [8 ] showed that strict predicate transformers correspond to a total interpretation of program semantics. A total interpretation of an s p sp s p requires that the postcondition only includes outputs that the computation produces from an initial state in the precondition. Therefore, if the precondition is empty, ⊥ \bot ⊥ , then no such outputs exist, ⊥ \bot ⊥ . We therefore define S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) as the angelic, total monad, with a carrier restricted to strict, disjunctive s p s sps s p s , meaning s p s sps s p s preserving all disjunctions, whether empty or non-empty, represented by the following refinement type:
{ s p : P → ( A → P ) ∣ ∀ I ( p : I → P ) a . s p ( ∃ i . p ( i ) ) ( a ) ⟺ ∃ i . s p ( p ( i ) ) ( a ) } \{sp : \mathbb{P} \to(A \to\mathbb{P}) \mid\forall I\ (p : I \to\mathbb{P})\ a.\ sp(\exists i.\ p(i))(a) \Longleftrightarrow\exists i.\ sp(p(i))(a)\} { s p : P → ( A → P ) ∣ ∀ I ( p : I → P ) a . s p ( ∃ i . p ( i )) ( a ) ⟺ ∃ i . s p ( p ( i )) ( a )}
and with the same ret and bind as (ang). We note that the strictness condition is covered by the empty case.
Two other properties of interest are conjunctivity and termination. An s p sp s p is conjunctive when for every non-empty family of preconditions ( p i ) i ∈ I (p_i)_{i \in I} ( p i ) i ∈ I
s p ( ⋀ i ∈ I p i ) = ⋀ i ∈ I s p ( p i ) sp\left(\bigwedge_{i \in I} p_i\right) = \bigwedge_{i \in I} sp(p_i) s p ( i ∈ I ⋀ p i ) = i ∈ I ⋀ s p ( p i )
where the conjunction of predicates is taken pointwise:
( ⋀ i ∈ I p i ) ( x ) i f f ∀ ( i : I ) . p i ( x ) . \left(\bigwedge_{i \in I} p_i\right)(x) \quad\mathrm{iff} \quad\forall(i : I).\ p_i(x). ( i ∈ I ⋀ p i ) ( x ) iff ∀ ( i : I ) . p i ( x ) .
An s p sp s p is terminating when it maps ⊤ \top ⊤ to ⊤ \top ⊤ , i.e., preserves empty conjunctions.
For conjunctivity, Back and von Wright [8 ] showed that conjunctive predicate transformers correspond to demonic interpretations of program semantics. Again, fix an output r r r and consider the initial states from which the computation can produce it. A demonic s p sp s p includes r r r in s p ( p ) sp(p) s p ( p ) when all of these states satisfy p p p . Every initial state that produces r r r satisfies p 1 ∧ p 2 p_1 \land p_2 p 1 ∧ p 2 only if every such initial state satisfies p 1 p_1 p 1 and every such initial state satisfies p 2 p_2 p 2 . Here, the precondition is also checked on each initial state separately. The main difference is that an angelic s p sp s p looks for one initial state that satisfies the precondition, while a demonic s p sp s p looks for one initial state that falsifies it.
For termination, Back and von Wright [8 ] showed that terminating predicate transformers correspond to a partial interpretation of program semantics. A partial interpretation of an s p sp s p requires that the postcondition includes outputs that no initial state produces, as well as outputs produced from initial states in the precondition. Therefore, if all initial states satisfy the precondition, the ⊤ \top ⊤ precondition, then this represents all possible outputs, either produced from some initial state or produced by none, which is the ⊤ \top ⊤ postcondition.
We care about these classes for two reasons. These classes are carrier restrictions under which our other s p sp s p monads are lawful. Without disjunctivity or conjunctivity, the associativity of our stateful bind fails. We work through a counterexample in Appendix E. Additionally, they match the four interpretations of nondeterminism and nontermination, so each of our monads contains exactly the predicate transformers of programs under its interpretation.
Keeping the ret and bind from above while changing only the carrier restriction to conjunctive, terminating s p s sps s p s does not yield the corresponding S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) monad. The restricted carrier has the following refinement type, where the empty case expresses termination:
{ s p : P → ( A → P ) ∣ ∀ I ( p : I → P ) a . s p ( ∀ i . p ( i ) ) ( a ) ⟺ ∀ i . s p ( p ( i ) ) ( a ) } \{sp : \mathbb{P} \to(A \to\mathbb{P}) \mid\forall I\ (p : I \to\mathbb{P})\ a.\ sp(\forall i.\ p(i))(a) \Longleftrightarrow\forall i.\ sp(p(i))(a)\} { s p : P → ( A → P ) ∣ ∀ I ( p : I → P ) a . s p ( ∀ i . p ( i )) ( a ) ⟺ ∀ i . s p ( p ( i )) ( a )}
We obtain a lawful demonic, liberal S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) monad by also modifying the ret and bind as follows:
ret v : = λ p r e a . a = v ⇒ p r e , bind m f : = λ p r e b . ∀ a . f a ( m p r e a ) b . \begin{aligned}
\operatorname{ret}\ v &:= \lambda pre\ a.\ a = v \Rightarrow pre,\\
\operatorname{bind}\ m\ f &:= \lambda pre\ b.\ \forall a.\ f\ a\ (m\ pre\ a)\ b.
\end{aligned} ret v bind m f := λ p r e a . a = v ⇒ p r e , := λ p r e b . ∀ a . f a ( m p r e a ) b .
(dem)
The total ret says that a a a is produced and equals v v v , and p r e pre p r e holds (p r e ∧ a = v pre \land a = v p r e ∧ a = v ). The partial one says that if a a a is produced and equals v v v , then p r e pre p r e holds (a = v ⇒ p r e a = v \Rightarrow pre a = v ⇒ p r e ), which vacuously admits every a ≠ v a \ne v a = v . The angelic bind says that b b b is produced through some intermediate value a a a and p r e pre p r e holds (∃ a \exists a ∃ a ). The demonic one says that, for every intermediate value a a a , if b b b is produced through a a a , then p r e pre p r e holds (∀ a \forall a ∀ a ).
To construct S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) and S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) , we restrict their carriers to conjunctive, strict s p s sps s p s and to disjunctive, terminating s p s sps s p s , respectively. To define ret and bind, we build on the observation of Verscht and Kaminski [37 §3.2.4] that d s p dsp d s p and a s l p aslp a s l p predicate transformers do not have an inductive definition but can be defined in terms of a s p asp a s p and d s l p dslp d s l p transformers. For ret, we get:
ret D S P v : = ret A S P v ∧ ret D S L P v , \operatorname{ret}_{\mathrm{DSP}}\ v := \operatorname{ret}_{\mathrm{ASP}}\ v \land\operatorname{ret}_{\mathrm{DSLP}}\ v, ret DSP v := ret ASP v ∧ ret DSLP v ,
ret A S L P v : = ret A S P v ∨ ret D S L P v \operatorname{ret}_{\mathrm{ASLP}}\ v := \operatorname{ret}_{\mathrm{ASP}}\ v \lor\operatorname{ret}_{\mathrm{DSLP}}\ v ret ASLP v := ret ASP v ∨ ret DSLP v
Defining bind is not so straightforward though. Simply taking bind m f = bind A S P m f ( ∧ / ∨ ) bind D S L P m f \operatorname{bind}\ m\ f = \operatorname{bind}_{\mathrm{ASP}}\ m\ f\ (\land/\lor)\ \operatorname{bind}_{\mathrm{DSLP}}\ m\ f bind m f = bind ASP m f ( ∧ / ∨ ) bind DSLP m f does not type-check, since bind A S P \operatorname{bind}_{\mathrm{ASP}} bind ASP and bind D S L P \operatorname{bind}_{\mathrm{DSLP}} bind DSLP expect a s p asp a s p and d s l p dslp d s l p predicate transformers, whereas m m m and f a f\ a f a are d s p dsp d s p or a s l p aslp a s l p predicate transformers. Let us consider just S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) . We utilize the fact that a d s p dsp d s p predicate transformer can be decomposed into an a s p asp a s p component and a d s l p dslp d s l p component, whose conjunction characterizes the same set of results as the d s p dsp d s p predicate transformer itself. We calculate this a s p asp a s p component as follows:
aspOf : S P D S P ( A ) → S P A S P ( A ) \operatorname{aspOf} : \mathrm{SP}_{\mathrm{DSP}}(A) \to\mathrm{SP}_{\mathrm{ASP}}(A) aspOf : SP DSP ( A ) → SP ASP ( A )
aspOf s p : = λ p r e a . s p ⊤ a ∧ ¬ ( s p ( ¬ p r e ) a ) \operatorname{aspOf}\ sp := \lambda pre\ a.\ sp\ \top\ a \land\neg(sp\ (\neg pre)\ a) aspOf s p := λ p r e a . s p ⊤ a ∧ ¬ ( s p ( ¬ p r e ) a )
The first conjunct, s p ⊤ a sp\ \top\ a s p ⊤ a , states that a a a is produced. The second conjunct, ¬ ( s p ( ¬ p r e ) a ) \neg(sp\ (\neg pre)\ a) ¬ ( s p ( ¬ p r e ) a ) , rules out that p r e pre p r e does not hold while a a a is produced, so p r e pre p r e holds or a a a is not produced. The conjunction of the two admits a a a exactly when a a a is produced and p r e pre p r e holds, which is the behavior of an a s p asp a s p predicate transformer.
We calculate the S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) component as follows:
dslpOf : S P D S P ( A ) → S P D S L P ( A ) \operatorname{dslpOf} : \mathrm{SP}_{\mathrm{DSP}}(A) \to\mathrm{SP}_{\mathrm{DSLP}}(A) dslpOf : SP DSP ( A ) → SP DSLP ( A )
dslpOf s p : = λ p r e a . s p p r e a ∨ ¬ ( s p ⊤ a ) \operatorname{dslpOf}\ sp := \lambda pre\ a.\ sp\ pre\ a \lor\neg(sp\ \top\ a) dslpOf s p := λ p r e a . s p p r e a ∨ ¬ ( s p ⊤ a )
The first disjunct, s p p r e a sp\ pre\ a s p p r e a , states that p r e pre p r e holds and a a a is produced. The second disjunct, ¬ ( s p ⊤ a ) \neg(sp\ \top\ a) ¬ ( s p ⊤ a ) , states that a a a is not produced. The disjunction of the two admits a a a exactly when a a a being produced implies p r e pre p r e holding, which is the behavior of a d s l p dslp d s l p predicate transformer. Both “exactly when” steps are classical. Deriving p r e pre p r e from a a a being produced together with ¬ ( ¬ p r e ∧ a produced ) \neg(\neg pre \land a\ \text{produced}) ¬ ( ¬ p r e ∧ a produced ) , and turning the implication back into the disjunction, use excluded middle.
We prove that for any s p : S P D S P ( A ) sp : \mathrm{SP}_{\mathrm{DSP}}(A) s p : SP DSP ( A ) , s p sp s p admits the same outputs as the conjunction a s p O f s p ∧ d s l p O f s p \mathrm{aspOf}\ sp \land\mathrm{dslpOf}\ sp aspOf s p ∧ dslpOf s p (§F.1). We can therefore define bind as follows, for m : S P D S P ( A ) m : \mathrm{SP}_{\mathrm{DSP}}(A) m : SP DSP ( A ) and f : A → S P D S P ( B ) f : A \to\mathrm{SP}_{\mathrm{DSP}}(B) f : A → SP DSP ( B ) :
b i n d D S P m f : = λ p r e b . b i n d A S P ( a s p O f m ) ( λ a . a s p O f ( f a ) ) p r e b ∧ b i n d D S L P ( d s l p O f m ) ( λ a . d s l p O f ( f a ) ) p r e b . \mathrm{bind}_{\mathrm{DSP}}\ m\ f := \lambda\ pre\ b.\ \mathrm{bind}_{\mathrm{ASP}}\ (\mathrm{aspOf}\ m)\ (\lambda a.\ \mathrm{aspOf}\ (f\ a))\ pre\ b \land
\mathrm{bind}_{\mathrm{DSLP}}\ (\mathrm{dslpOf}\ m)\ (\lambda a.\ \mathrm{dslpOf}\ (f\ a))\ pre\ b. bind DSP m f := λ p r e b . bind ASP ( aspOf m ) ( λa . aspOf ( f a )) p r e b ∧ bind DSLP ( dslpOf m ) ( λa . dslpOf ( f a )) p r e b .
This gives us a lawful S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) monad. For S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) , we have similar functions a s p O f \mathrm{aspOf} aspOf and d s l p O f \mathrm{dslpOf} dslpOf , but we construct bind via the disjunction of the a s p asp a s p and d s l p dslp d s l p components. In §F.2, we give these function definitions and prove that the disjunction admits the same outputs as the original a s l p aslp a s l p transformer.
This gives us four “pure” base s p sp s p monads. When reasoning about monadic programs like stateful programs, we would like to apply monad transformers to pure s p sp s p monads to derive effectful s p sp s p monads, as is done on the w p wp w p side [25 ] . Unfortunately, using the state monad transformer to derive their stateful versions gives an unexpected type: S → P → ( A × S → P ) S \to\mathbb{P} \to(A \times S \to\mathbb{P}) S → P → ( A × S → P ) . Such s p s sps s p s take an initial state and a separate proposition and return a postcondition, whereas stateful s p s sps s p s should take a precondition that ranges over initial states and return a postcondition. This is a lawful monad, but it is unclear how to interpret its elements as stateful s p s sps s p s .
Base s p sp s p Arrows
To remedy this, we observe that a monad is only parameterized by an output type, and an s p sp s p that wants to lift the type of its preconditions automatically, like on the w p wp w p side, would benefit from a structure that is parameterized over both an input and output type. Based on this observation, we propose using Hughes’ arrows and arrow transformers to derive stateful s p s sps s p s [17 ] . Arrows are parameterized by both an input type and an output type, which gives precise control over how lifting affects both the input and output.
In its basic form, an arrow A r r : T y p e → T y p e → T y p e \mathrm{Arr} : \mathit{Type} \to\mathit{Type} \to\mathit{Type} Arr : Type → Type → Type can be defined as a structure with the following operations:
a r r : ( B → A ) → A r r ( B , A ) , \mathrm{arr} : (B \to A) \to\mathrm{Arr}(B,A), arr : ( B → A ) → Arr ( B , A ) ,
≫ : A r r ( B , A ) → A r r ( A , C ) → A r r ( B , C ) , \mathbin{\gg} : \mathrm{Arr}(B,A) \to\mathrm{Arr}(A,C) \to\mathrm{Arr}(B,C), ≫ : Arr ( B , A ) → Arr ( A , C ) → Arr ( B , C ) ,
f i r s t : A r r ( B , A ) → A r r ( B × C , A × C ) . \mathrm{first} : \mathrm{Arr}(B,A) \to\mathrm{Arr}(B \times C,A \times C). first : Arr ( B , A ) → Arr ( B × C , A × C ) .
Intuitively, a r r \mathrm{arr} arr lifts a function to an arrow. The x ≫ y x \mathbin{\gg} y x ≫ y operation composes two arrows x x x and y y y together by feeding the output of x x x to the input of y y y , and f i r s t \mathrm{first} first takes an arrow x x x and makes it an arrow that additionally carries an “environment”. The first operator is also known as the strength of arrows [4 ] . These arrows need to satisfy particular arrow laws, detailed in Appendix § 3 .
All monads yield arrows, but not all arrows can be turned into monads [24 ] . Given a monad M M M , we can derive an arrow as follows:
Arr ( B , A ) : = B → M ( A ) \operatorname{Arr}(B,A) := B \to M(A) Arr ( B , A ) := B → M ( A )
In Hughes [17 ] , this is known as the Kleisli operation, producing a Kleisli arrow. This means that arrows are more general, but less expressive than monads. In this paper, we are only interested in structures as expressive as monads, so we consider arrows Arr \operatorname{Arr} Arr with an additional application method, or app:
app : Arr ( Arr ( B , A ) × B , A ) \operatorname{app} : \operatorname{Arr}(\operatorname{Arr}(B,A) \times B,A) app : Arr ( Arr ( B , A ) × B , A )
This variant of arrows is also defined by Hughes. The app method itself is an arrow, which takes another arrow of type Arr ( B , A ) \operatorname{Arr}(B,A) Arr ( B , A ) and a value of type B B B as input. It applies the arrow to the value of B B B to obtain an output of type A A A . This operator gives arrows the same expressiveness as monads [7 ] . Indeed, given an arrow Arr \operatorname{Arr} Arr with application, we can turn it into a monad M M M by fixing the input: M ( A ) : = Arr ( 1 , A ) M(A) := \operatorname{Arr}(1,A) M ( A ) := Arr ( 1 , A ) . The monad operations can be derived as follows:
ret v : = arr ( λ . v ) , \operatorname{ret}\ v := \operatorname{arr}(\lambda\ _.v), ret v := arr ( λ . v ) ,
bind m f : = ( arr ( λ x . ( x , x ) ) ≫ first m ) ≫ ( arr ( λ ( x , i ) . ( f x , i ) ) ≫ app ) \operatorname{bind}\ m\ f := (\operatorname{arr}(\lambda x.(x,x)) \mathbin{\gg} \operatorname{first}\ m) \mathbin{\gg} (\operatorname{arr}(\lambda(x,i).(f\ x,i)) \mathbin{\gg} \operatorname{app}) bind m f := ( arr ( λ x . ( x , x )) ≫ first m ) ≫ ( arr ( λ ( x , i ) . ( f x , i )) ≫ app )
Fixing the input type of a lawful arrow with application gives a lawful monad [17 , 24 ] , which allows us to prove the monad laws once, generically, from the arrow laws and the laws of app.
We note that we do not use the Kleisli arrows constructed by applying the Kleisli operation to our base s p sp s p monads. Every monad has a Kleisli arrow. Applying the state arrow transformer to that arrow and then fixing its input yields the same monad as applying the state monad transformer to the original monad. Therefore, we would still end up with s p s sps s p s of an unexpected shape. Instead, we use arrows where the input type gives the type over which preconditions are defined, so threading state through the input changes the type of the preconditions. A monad, indexed only by its output, cannot thread state through the type of the preconditions the same way.
Additionally, we can construct structured maps between arrows called arrow morphisms , which need to satisfy arrow morphism laws. An arrow morphism is a parametric family θ X , Y : A ( X , Y ) → B ( X , Y ) \theta_{X,Y} : A(X,Y) \to B(X,Y) θ X , Y : A ( X , Y ) → B ( X , Y ) such that θ ( arr A f ) = arr B f \theta(\operatorname{arr}_{A}\ f) = \operatorname{arr}_{B}\ f θ ( arr A f ) = arr B f , θ ( x ≫ A y ) = θ ( x ) ≫ B θ ( y ) \theta(x \mathbin{\gg}_{A} y) = \theta(x) \mathbin{\gg}_{B} \theta(y) θ ( x ≫ A y ) = θ ( x ) ≫ B θ ( y ) , and θ ( first A x ) = first B θ ( x ) \theta(\operatorname{first}_{A}\ x) = \operatorname{first}_{B}\theta(x) θ ( first A x ) = first B θ ( x ) , along with laws for choice and application (§A).
Returning to strongest postconditions, we construct base s p sp s p arrows that mirror our base s p sp s p monads: Arr A S P \operatorname{Arr}_{ASP} Arr A S P , Arr D S L P \operatorname{Arr}_{DSLP} Arr D S L P , Arr D S P \operatorname{Arr}_{DSP} Arr D S P , and Arr A S L P \operatorname{Arr}_{ASLP} Arr A S L P . We start from monotone base s p sp s p arrows (s p s a r r sps_{\mathrm{arr}} s p s arr ) of the type ( B → P ) → ( A → P ) (B \to\mathbb{P}) \to(A \to\mathbb{P}) ( B → P ) → ( A → P ) , where ( B → P ) (B \to\mathbb{P}) ( B → P ) represents preconditions and ( A → P ) (A \to\mathbb{P}) ( A → P ) represents postconditions. Therefore, monotonicity states that for any two preconditions p 1 , p 2 : B → P p_1, p_2 : B \to\mathbb{P} p 1 , p 2 : B → P , p 1 ⊑ p 2 ⇒ s p s a r r ( p 1 ) ⊑ s p s a r r ( p 2 ) p_1 \sqsubseteq p_2 \Rightarrow sps_{\mathrm{arr}}(p_1) \sqsubseteq sps_{\mathrm{arr}}(p_2) p 1 ⊑ p 2 ⇒ s p s arr ( p 1 ) ⊑ s p s arr ( p 2 ) . We present Arr A S P \operatorname{Arr}_{ASP} Arr A S P and Arr D S L P \operatorname{Arr}_{DSLP} Arr D S L P and refer to Appendix B for Arr A S L P \operatorname{Arr}_{ASLP} Arr A S L P and Arr A S L P \operatorname{Arr}_{ASLP} Arr A S L P . Their a s p asp a s p and d s l p dslp d s l p components are the aspOf \operatorname{aspOf} aspOf and dslpOf \operatorname{dslpOf} dslpOf of § 2.1 with ⊤ \top ⊤ , ⊥ \bot ⊥ , and ¬ \neg ¬ taken pointwise. They are defined for every input and output type, so the stateful monads of § 2.1.2 need no separate definition.
For Arr A S P ( B , A ) \operatorname{Arr}_{ASP}(B,A) Arr A S P ( B , A ) , we restrict the arrow carrier to disjunctive, strict s p s a r r sps_{\mathrm{arr}} s p s arr , mirroring the carrier restriction of the S P A S P ( A ) SP_{ASP}(A) S P A S P ( A ) monad. For every family of preconditions ( p i ) i ∈ I (p_i)_{i\in I} ( p i ) i ∈ I on the input B:
s p a r r ( ⋁ i ∈ I p i ) = ⋁ i ∈ I s p a r r ( p i ) sp_{\mathrm{arr}}\left(\bigvee_{i\in I} p_i\right)=\bigvee_{i\in I} sp_{\mathrm{arr}}(p_i) s p arr ( i ∈ I ⋁ p i ) = i ∈ I ⋁ s p arr ( p i )
The empty families give strictness, and non-empty families give disjunctivity. Monotonicity and strictness alone are not enough: without disjunctivity, the arrow laws fail (§E.3). Fixing B = 1 gives a monad carrier isomorphic to the carrier of S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP (A).
We then define the arrow operations so that fixing B = 1 gives the same ret and bind as S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP (A), which we show in §C.1:
a r r f : ( B → A ) → A r r A S P ( B , A ) ≔ λ p r e a . ∃ b . p r e b ∧ f b = a , f ≫ g : A r r A S P ( B , A ) → A r r A S P ( A , C ) → A r r A S P ( B , C ) ≔ λ p r e c . g ( f p r e ) c , f i r s t f : A r r A S P ( B , A ) → A r r A S P ( B × C , A × C ) ≔ λ p r e ( a , c ) . f ( λ b . p r e ( b , c ) ) a , a p p : A r r A S P ( A r r A S P ( B , A ) × B , A ) ≔ λ p r e a . ∃ f . ∃ b . p r e ( f , b ) ∧ f ( λ x . x = b ) a \begin{aligned}
\mathop{\mathrm{arr}} f &: (B \to A) \to\mathrm{Arr}_{\mathrm{ASP}}(B,A) \coloneqq\lambda pre\ a.\ \exists b.\ pre\ b \land f\ b=a,\\
f \gg g &: \mathrm{Arr}_{\mathrm{ASP}}(B,A)\to\mathrm{Arr}_{\mathrm{ASP}}(A,C)\to\mathrm{Arr}_{\mathrm{ASP}}(B,C) \coloneqq\lambda pre\ c.\ g\ (f\ pre)\ c,\\
\mathop{\mathrm{first}} f &: \mathrm{Arr}_{\mathrm{ASP}}(B,A)\to\mathrm{Arr}_{\mathrm{ASP}}(B\times C,A\times C)\\
&\coloneqq\lambda pre\ (a,c).\ f\ (\lambda b.\ pre\ (b,c))\ a,\\
\mathop{\mathrm{app}} &: \mathrm{Arr}_{\mathrm{ASP}}(\mathrm{Arr}_{\mathrm{ASP}}(B,A)\times B,A)\coloneqq\lambda pre\ a.\ \exists f.\exists b.\ pre\ (f,b)\land f(\lambda x.\ x=b)\ a
\end{aligned} arr f f ≫ g first f app : ( B → A ) → Arr ASP ( B , A ) : = λ p r e a . ∃ b . p r e b ∧ f b = a , : Arr ASP ( B , A ) → Arr ASP ( A , C ) → Arr ASP ( B , C ) : = λ p r e c . g ( f p r e ) c , : Arr ASP ( B , A ) → Arr ASP ( B × C , A × C ) : = λ p r e ( a , c ) . f ( λb . p r e ( b , c )) a , : Arr ASP ( Arr ASP ( B , A ) × B , A ) : = λ p r e a . ∃ f .∃ b . p r e ( f , b ) ∧ f ( λ x . x = b ) a
Intuitively, an output is admitted when some input that satisfies the precondition produces it. The definitions mirror the ret and bind of (ang), except that the precondition is now a predicate on an explicit input, over which we also quantify. ret v v v admits v v v when p r e pre p r e holds, while arr f f f admits f b f\ b f b for some input b b b that satisfies p r e pre p r e . At B = 1, arr ( λ . v ) (\lambda_.v) ( λ . v ) is ret v v v . Composition passes the postcondition of f f f to g g g as its precondition. Unlike bind, it needs no quantifier over intermediate values, since g g g , being disjunctive, quantifies over its own input. first f f f runs f f f on the first component of the input and passes the second component c c c through unchanged, so f f f receives the precondition restricted to the inputs paired with c c c . Finally, app takes over the existential of bind. Where bind quantifies over an intermediate value a a a to select the continuation f a f\ a f a , app quantifies over its input, an arrow f f f and an argument b b b , and runs f f f from exactly b b b .
For A r r D S L P ( B , A ) \mathrm{Arr}_{\mathrm{DSLP}}(B,A) Arr DSLP ( B , A ) , we restrict the arrow carrier to conjunctive, terminating s p s a r r sps_{\mathrm{arr}} s p s arr , mirroring the carrier restriction of the S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP (A) monad. For every family of preconditions ( p i ) i ∈ I (p_i)_{i\in I} ( p i ) i ∈ I on the input B:
s p a r r ( ⋀ i ∈ I p i ) = ⋀ i ∈ I s p a r r ( p i ) sp_{\mathrm{arr}}\left(\bigwedge_{i\in I} p_i\right)=\bigwedge_{i\in I}sp_{\mathrm{arr}}(p_i) s p arr ( i ∈ I ⋀ p i ) = i ∈ I ⋀ s p arr ( p i )
The empty families give termination, and non-empty families give conjunctivity. Fixing B = 1 gives a monad carrier isomorphic to the carrier of S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP (A).
The relevant arrow operations are:
a r r f : ( B → A ) → A r r D S L P ( B , A ) ≔ λ p r e a . ∀ b . f b = a ⇒ p r e b , \mathop{\mathrm{arr}} f : (B\to A)\to\mathrm{Arr}_{\mathrm{DSLP}}(B,A)\coloneqq\lambda pre\ a.\ \forall b.\ f\ b=a\Rightarrow pre\ b, arr f : ( B → A ) → Arr DSLP ( B , A ) : = λ p r e a . ∀ b . f b = a ⇒ p r e b ,
a p p : A r r D S L P ( A r r D S L P ( B , A ) × B , A ) ≔ λ p r e a . ∀ f . ∀ b . ( ∀ p . f p a ⇒ p b ) ⇒ p r e ( f , b ) \mathop{\mathrm{app}}:\mathrm{Arr}_{\mathrm{DSLP}}(\mathrm{Arr}_{\mathrm{DSLP}}(B,A)\times B,A)
\coloneqq\lambda pre\ a.\ \forall f.\ \forall b.\ (\forall p.\ f\ p\ a\Rightarrow p\ b)\Rightarrow pre\ (f,b) app : Arr DSLP ( Arr DSLP ( B , A ) × B , A ) : = λ p r e a . ∀ f . ∀ b . ( ∀ p . f p a ⇒ p b ) ⇒ p r e ( f , b )
The definitions of ≫ \gg ≫ and first are the same as for A r r A S P ( B , A ) \mathrm{Arr}_{\mathrm{ASP}}(B,A) Arr ASP ( B , A ) so we omit them. Fixing B = 1 again gives the ret and bind of S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP (A) (§C.2). Intuitively, an output is admitted when every input that produces it satisfies the precondition, which holds vacuously for outputs and no input produces. As for A r r A S P \mathrm{Arr}_{\mathrm{ASP}} Arr ASP , the definitions follow the ret and bind of (dem) but quantify over the explicit input, now universally. Where ret v v v admits a a a when a = v a=v a = v implies p r e pre p r e , arr f f f admits a a a when every input b b b with f b = a f b = a f b = a satisfies p r e pre p r e ; at B = 1 \mathrm{B}=1 B = 1 , arr ( λ _ . v ) \operatorname{arr}(\lambda\_.v) arr ( λ _. v ) is ret v \operatorname{ret}\ v ret v . Composition and first are unchanged, since a conjunctive g g g quantifies universally over its own input. Here, app \operatorname{app} app takes over the universal quantifier of bind, so it admits a a a when every arrow f f f and argument b b b from which f f f produces a a a satisfy p r e pre p r e . Following [7 ] , a lawful app can also be seen as a property of the arrow rather than extra structure, reflecting that the arrow corresponds to a monad.
Stateful s p sp s p Arrows and Monads
Recall that a lawful arrow, or one that satisfies the arrow laws, with application gives a lawful monad once its input type is fixed. If we start from a lawful arrow of type Arr ( B , A ) = ( B → P ) → ( A → P ) \operatorname{Arr}(B,A)=(B \to\mathbb{P}) \to(A \to\mathbb{P}) Arr ( B , A ) = ( B → P ) → ( A → P ) , then applying the state arrow transformer [17 ] gives StArrT ( Arr ) ( S , B , A ) = ( B × S → P ) → ( A × S → P ) \operatorname{StArrT}(\operatorname{Arr})(S,B,A)=(B \times S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) StArrT ( Arr ) ( S , B , A ) = ( B × S → P ) → ( A × S → P ) . Fixing B = 1 B=1 B = 1 gives a lawful monad whose type is isomorphic to the expected shape of stateful s p sp s p s, ( 1 × S → P ) → ( A × S → P ) ≅ ( S → P ) → ( A × S → P ) (1 \times S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) \cong(S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) ( 1 × S → P ) → ( A × S → P ) ≅ ( S → P ) → ( A × S → P ) .
We apply the state arrow transformer to each of our base s p sp s p arrows and fix B = 1 B=1 B = 1 . We present the resulting S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St and S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St monad definitions here and refer to Appendix § 3.3 for the arrow definitions, along with the S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St and S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St monad definitions. We check by hand that these definitions match our intuition.
Unfolding the derived ret and bind of S P A S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}}(S,A) SP ASP St ( S , A ) and S P D S L P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}}(S,A) SP DSLP St ( S , A ) gives:
ret A S P v ≡ λ p r e ( a , s ) . p r e s ∧ a = v , bind A S P m f ≡ λ p r e ( b , s ) . ∃ a . f a ( λ s ′ . m p r e ( a , s ′ ) ) ( b , s ) (ang-st) , ret D S L P v ≡ λ p r e ( a , s ) . a = v ⇒ p r e s , bind D S L P m f ≡ λ p r e ( b , s ) . ∀ a . f a ( λ s ′ . m p r e ( a , s ′ ) ) ( b , s ) (dem-st) . \begin{aligned}
\operatorname{ret}_{\mathrm{ASP}}\ v &\equiv\lambda\ pre\ (a,s).\ pre\ s \land a=v,\\
\operatorname{bind}_{\mathrm{ASP}}\ m\ f &\equiv\lambda\ pre\ (b,s).\ \exists a.\ f\ a\ (\lambda s'.\ m\ pre\ (a,s'))\ (b,s) \qquad\text{(ang-st)},\\
\operatorname{ret}_{\mathrm{DSLP}}\ v &\equiv\lambda\ pre\ (a,s).\ a=v \Rightarrow pre\ s,\\
\operatorname{bind}_{\mathrm{DSLP}}\ m\ f &\equiv\lambda\ pre\ (b,s).\ \forall a.\ f\ a\ (\lambda s'.\ m\ pre\ (a,s'))\ (b,s) \qquad\text{(dem-st)}.
\end{aligned} ret ASP v bind ASP m f ret DSLP v bind DSLP m f ≡ λ p r e ( a , s ) . p r e s ∧ a = v , ≡ λ p r e ( b , s ) . ∃ a . f a ( λ s ′ . m p r e ( a , s ′ )) ( b , s ) (ang-st) , ≡ λ p r e ( a , s ) . a = v ⇒ p r e s , ≡ λ p r e ( b , s ) . ∀ a . f a ( λ s ′ . m p r e ( a , s ′ )) ( b , s ) (dem-st) .
These are exactly the base definitions with the precondition now a predicate on states. ret checks the precondition on the current state, and bind hands the continuation f a f\ a f a the postcondition produced by m m m at the value a a a and precondition p r e pre p r e , which is a predicate on intermediate states.
Monads vs. Arrows
We now revisit the mismatch between the stateful s p sp s p carriers obtained using monad and arrow transformers. Focusing on angelic strongest postconditions, we show how the two constructions become isomorphic after appropriately restricting their carriers. As noted in §3.1, applying the state monad transformer to the base monad S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP on A A A yields StT ( S P A S P ) ( S , A ) \operatorname{StT}(\mathrm{SP}_{\mathrm{ASP}})(S,A) StT ( SP ASP ) ( S , A ) , with carrier S → P → ( A × S → P ) S \to\mathbb{P} \to(A \times S \to\mathbb{P}) S → P → ( A × S → P ) . By contrast, applying the state arrow transformer to A r r A S P \mathrm{Arr}_{\mathrm{ASP}} Arr ASP and fixing its input type to 1 1 1 yields StArrT ( A r r A S P ) ( S , 1 , A ) \operatorname{StArrT}(\mathrm{Arr}_{\mathrm{ASP}})(S,1,A) StArrT ( Arr ASP ) ( S , 1 , A ) , with carrier ( 1 × S → P ) → ( A × S → P ) (1 \times S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) ( 1 × S → P ) → ( A × S → P ) . Since the unit component is trivial, this carrier is isomorphic to ( S → P ) → ( A × S → P ) (S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) ( S → P ) → ( A × S → P ) . Although these carriers have different shapes, restricting both to monotone, strict, and disjunctive predicate transformers makes them isomorphic:
StT ( S P A S P ) ( S , A ) ≅ StArrT ( A r r A S P ) ( S , 1 , A ) . \operatorname{StT}(\mathrm{SP}_{\mathrm{ASP}})(S,A) \cong\operatorname{StArrT}(\mathrm{Arr}_{\mathrm{ASP}})(S,1,A). StT ( SP ASP ) ( S , A ) ≅ StArrT ( Arr ASP ) ( S , 1 , A ) .
To see this isomorphism more concretely, consider the following maps:
toArrow ( m ) : = λ p r e r . ∃ s . m s ( p r e s ) r , fromArrow ( g ) : = λ s p r . g ( λ t . p ∧ t = s ) r , \begin{aligned}
\operatorname{toArrow}(m) &:= \lambda\ \mathit{pre}\ r.\ \exists s.\ m\ s\ (\mathit{pre}\ s)\ r,\\
\operatorname{fromArrow}(g) &:= \lambda\ s\ p\ r.\ g\ (\lambda t.\ p \land t=s)\ r,
\end{aligned} toArrow ( m ) fromArrow ( g ) := λ pre r . ∃ s . m s ( pre s ) r , := λ s p r . g ( λ t . p ∧ t = s ) r ,
which are inverses of each other.
Therefore, in this paper, we choose to work with the more familiar stateful s p sp s p presentation based on arrows. The w p wp w p side needs no arrows, since the state monad transformer gives the expected shape: S → ( A × S → P ) → P ≅ ( A × S → P ) → ( S → P ) S \to(A \times S \to\mathbb{P}) \to\mathbb{P} \cong(A \times S \to\mathbb{P}) \to(S \to\mathbb{P}) S → ( A × S → P ) → P ≅ ( A × S → P ) → ( S → P ) . We mainly use arrows as a construction device to automatically derive lawful stateful arrows and, correspondingly, lawful stateful monads. Our study focuses on the resulting monads. We seek to showcase the behavior of these s p sp s p monads in contexts that previously only considered w p wp w p monads, such as in forming Dijkstra monads. Additionally, working with monads keeps the treatment uniform with the w p wp w p side.
From here on, we work with w p wp w p and s p sp s p monads. The taxonomy of § 2.2 , the triples, and the ordering on predicate transformers of § 2.3 are all stated in terms of ret, bind, monad morphisms, and monadic relations. The state arrow transformer adds state to the s p sp s p monads and also lifts arrow morphisms. These maps preserve arrow structure, just as monad morphisms preserve monad structure. Since, in our setting, an arrow morphism gives a monad morphism once the input is fixed [7 ] , we obtain the monad morphisms into the s p sp s p monads from arrow morphisms. Therefore, the state arrow transformer also “lifts” our monad morphisms. The monadic relations are proved for the base and stateful cases separately, but we believe that a similar lift from the base to the stateful case via monad and arrow transformers, could be formalized in future work.
Refined WP Monads for Nondeterminism and Nontermination
As needed for our taxonomy in § 2.2 , we also construct w p wp w p monads containing exactly the w p s wps w p s that match the four interpretations of nondeterminism and nontermination. We use the same carrier restrictions to construct these w p wp w p monads: disjunctive for the angelic and conjunctive for the demonic interpretation of nondeterminism, and strict for the total and terminating for the partial interpretation of nontermination. Unlike the s p sp s p monads, WP does not need such carrier restrictions in order to be a lawful monad, but they allow us to define the monads W P A W P \mathrm{WP}_{\mathrm{AWP}} WP AWP , W P D W L P \mathrm{WP}_{\mathrm{DWLP}} WP DWLP , W P D W P \mathrm{WP}_{\mathrm{DWP}} WP DWP , and W P A W L P \mathrm{WP}_{\mathrm{AWLP}} WP AWLP , corresponding to our four s p sp s p monads. All the w p wp w p monads share the ret and bind of WP, since ret satisfies all four homomorphism properties and bind preserves each of them. This makes the four w p wp w p monads submonads of WP, each carrier is a subset of WP’s that is closed under its ret and bind, so each inherits WP’s monad structure and laws, and any two w p s wps w p s can be compared inside WP (§ 2.2.2 ).
Taxonomy of Predicate Transformer Monads
We recast the taxonomy of triples of Verscht and Kaminski [37 ] as a taxonomy of predicate transformer monads. In Figure 1 , diagram (a) details the relationships for our base effects of nondeterminism and nontermination, for which some of the sp monads are isomorphic (§ 2.2.3 )while diagram (b) details the relationships between the monads obtained after adding the state effect, which corresponds to the stateful triple taxonomy of Verscht and Kaminski. We replicate the implications, the contrapositive relations, and the Galois connections between triples as monadic relations between the predicate transformer monads.
Taxonomy of Triples
Verscht and Kaminski present a taxonomy of triples. Each triple relates a program c c c , a precondition p p p , and a postcondition q q q . Its validity is expressed using a predicate transformer variant, for example as p ⇒ dwlp ⟦ c ⟧ ( q ) p \Rightarrow\operatorname{dwlp}\llbracket c\rrbracket(q) p ⇒ dwlp [ [ c ] ] ( q ) . They relate triples using three relations: implications, contrapositivity and Galois connections. For implication, one triple implies another if validity of a triple in one logic implies its validity in the other logic. For contrapositivity, two triples are contrapositives if one triple is valid in one logic iff the other triple is valid in the other logic with the precondition and postcondition negated. Given predicate transformers f : ( X → P ) → ( Y → P ) f : (X \to\mathbb{P}) \to(Y \to\mathbb{P}) f : ( X → P ) → ( Y → P ) and g : ( Y → P ) → ( X → P ) g : (Y \to\mathbb{P}) \to(X \to\mathbb{P}) g : ( Y → P ) → ( X → P ) , f f f and g g g form a Galois connection when for any p : X → P p : X \to\mathbb{P} p : X → P and q : Y → P q : Y \to\mathbb{P} q : Y → P , f ( p ) ⇒ q f(p) \Rightarrow q f ( p ) ⇒ q is valid if and only if p ⇒ g ( q ) p \Rightarrow g(q) p ⇒ g ( q ) is valid. Here, f f f is the left adjoint and g g g is the right adjoint.
Analyzing their taxonomy of triples, which they construct by fixing the precondition, postcondition, and program, except that a contrapositive relation negates the precondition and the postcondition, leads us to the following observations, which inspire our recast.
First, for imperative programs, predicate transformers are defined recursively over the structure of the program. However, it need not be so. In our setting, predicate transformers can be studied apart from programs and constructed using the ret and bind of the monadic structure. Therefore, we claim that the relationships between triples can also be observed at the level of the bare predicate transformers, not indexed by a program, witnessed by monadic relations.
Second, Verscht and Kaminski’s taxonomy is comprised of triples that over- or under -approximate a predicate transformer, where, for example, p ⇒ d w l p ⟦ c ⟧ q p \Rightarrow\mathit{dwlp}\llbracket c\rrbracket q p ⇒ dwlp [ [ c ] ] q is under-approximate and d w l p ⟦ c ⟧ q ⇒ p \mathit{dwlp}\llbracket c\rrbracket q \Rightarrow p dwlp [ [ c ] ] q ⇒ p is over-approximate. The implications that exist among under-approximate triples are dual to the ones that exist among over-approximate triples. For example, in the under-approximate version, ( p ⇒ d w p ⟦ c ⟧ ( q ) ) ⇒ ( p ⇒ d w l p ⟦ c ⟧ ( q ) ) (p \Rightarrow\mathit{dwp}\llbracket c\rrbracket(q)) \Rightarrow(p \Rightarrow\mathit{dwlp}\llbracket c\rrbracket(q)) ( p ⇒ dwp [ [ c ] ] ( q )) ⇒ ( p ⇒ dwlp [ [ c ] ] ( q )) . However, for the over-approximate version, ( d w l p ⟦ c ⟧ ( q ) ⇒ p ) ⇒ ( d w p ⟦ c ⟧ ( q ) ⇒ p ) (\mathit{dwlp}\llbracket c\rrbracket(q) \Rightarrow p) \Rightarrow(\mathit{dwp}\llbracket c\rrbracket(q) \Rightarrow p) ( dwlp [ [ c ] ] ( q ) ⇒ p ) ⇒ ( dwp [ [ c ] ] ( q ) ⇒ p ) .
Third, for contrapositives, each relation is between an under-approximate and an over-approximate triple, and it exists in a dualized form. For example, ( p ⇒ a w p ⟦ c ⟧ ( q ) ) (p \Rightarrow\mathit{awp}\llbracket c\rrbracket(q)) ( p ⇒ awp [ [ c ] ] ( q )) is contrapositive to ( d w l p ⟦ c ⟧ ( q ) ⇒ p ) (\mathit{dwlp}\llbracket c\rrbracket(q) \Rightarrow p) ( dwlp [ [ c ] ] ( q ) ⇒ p ) . Dually, ( p ⇒ d w l p ⟦ c ⟧ ( q ) ) (p \Rightarrow\mathit{dwlp}\llbracket c\rrbracket(q)) ( p ⇒ dwlp [ [ c ] ] ( q )) is contrapositive to ( a w p ⟦ c ⟧ ( q ) ⇒ p ) (\mathit{awp}\llbracket c\rrbracket(q) \Rightarrow p) ( awp [ [ c ] ] ( q ) ⇒ p ) .
Fourth, for the adjunctions, the relations are dual with respect to which predicate transformer is the left adjoint or right adjoint, or vice versa. The Galois connection between predicate transformers when s p sp s p is the left adjoint and the w p wp w p is the right adjoint is represented by ( p ⇒ d w l p ⟦ c ⟧ ( q ) ) ⟺ ( a s p ⟦ c ⟧ ( p ) ⇒ q ) (p \Rightarrow\mathit{dwlp}\llbracket c\rrbracket(q)) \Longleftrightarrow(\mathit{asp}\llbracket c\rrbracket(p) \Rightarrow q) ( p ⇒ dwlp [ [ c ] ] ( q )) ⟺ ( asp [ [ c ] ] ( p ) ⇒ q ) . The other Galois connection when w p wp w p is the left adjoint and the s p sp s p is the right adjoint is represented by ( q ⇒ d s l p ⟦ c ⟧ ( p ) ) ⟺ ( a w p ⟦ c ⟧ ( q ) ⇒ p ) (q \Rightarrow\mathit{dslp}\llbracket c\rrbracket(p)) \Longleftrightarrow(\mathit{awp}\llbracket c\rrbracket(q) \Rightarrow p) ( q ⇒ dslp [ [ c ] ] ( p )) ⟺ ( awp [ [ c ] ] ( q ) ⇒ p ) .
We demonstrate that, for implications and contrapositives, each pair of dual relationships is characterized by a single monadic relation, and the two adjunctions are characterized by two monadic relations. Therefore, each implication or contrapositive edge of Figure 1 represents two arrows of Verscht and Kaminski’s diagram, and each Galois edge one.
Monadic Relations
Maillard et al. [26 ] introduce monadic relations as a way of relating a computation monad to a specification monad, which are monads where bind is monotone with respect to the ordering. The definition of a monadic relation constrains ret and bind but does not use an ordering, so it applies to any two monads. We use monadic relations to relate our predicate transformer monads.
Definition 1 (Monadic relation [26 ] ). A monadic relation R : W 1 ↔ W 2 \mathcal{R} : W_1 \leftrightarrow W_2 R : W 1 ↔ W 2 between monads W 1 W_1 W 1 and W 2 W_2 W 2 is a family of relations R A : W 1 A × W 2 A → P \mathcal{R}_A : W_1 A \times W_2 A \to\mathbb{P} R A : W 1 A × W 2 A → P , indexed by types A A A , satisfying the following conditions:
for every a : A a : A a : A , ( ret W 1 a ) R A ( ret W 2 a ) (\operatorname{ret}_{W_1}\ a)\ R_A\ (\operatorname{ret}_{W_2}\ a) ( ret W 1 a ) R A ( ret W 2 a ) ,
and for every w 1 : W 1 A w_1 : W_1\ A w 1 : W 1 A , w 2 : W 2 A w_2 : W_2\ A w 2 : W 2 A , k 1 : A → W 1 B k_1 : A \to W_1\ B k 1 : A → W 1 B , and k 2 : A → W 2 B k_2 : A \to W_2\ B k 2 : A → W 2 B , we have
w 1 R A w 2 ∀ ( a : A ) . ( k 1 a ) R B ( k 2 a ) ( bind W 1 w 1 k 1 ) R B ( bind W 2 w 2 k 2 ) \frac{w_1\ R_A\ w_2 \qquad\forall(a : A).\ (k_1\ a)\ R_B\ (k_2\ a)}
{(\operatorname{bind}_{W_1}\ w_1\ k_1)\ R_B\ (\operatorname{bind}_{W_2}\ w_2\ k_2)} ( bind W 1 w 1 k 1 ) R B ( bind W 2 w 2 k 2 ) w 1 R A w 2 ∀ ( a : A ) . ( k 1 a ) R B ( k 2 a )
We build the following monadic relations based on relations between the predicate transformers, as explained below. We present the monadic relations between the stateful w p wp w p and s p sp s p monads to more directly connect to the stateful setting of the taxonomy of triples [37 ] ; however, the base monadic relations can be similarly constructed for the relationships detailed in the left diagram of Figure 1 , the details of which we leave to Appendix G.
Implication Relation
The taxonomy of triples has implications among the w p wp w p triples and among the s p sp s p triples. We represent them as monadic relations among the w p wp w p monads and among the s p sp s p monads. Each monadic relation relates the elements of two monads, that is, two w p wp w p s or two s p sp s p s.
The four stateful w p wp w p monads have different carriers, but each is a submonad of StT ( W P u r e ) ( S , A ) = ( A × S → P ) → ( S → P ) \operatorname{StT}(\operatorname{W}^{\mathrm{Pure}})(S, A) = (A \times S \to\mathbb{P}) \to(S \to\mathbb{P}) StT ( W Pure ) ( S , A ) = ( A × S → P ) → ( S → P ) , the monad of stateful monotone w p wp w p s, which we call WP S t \operatorname{WP}^{\mathrm{St}} WP St and discuss in §3.5, so any two w p wp w p s can be compared within this monad. We compare them with the pointwise order:
w p 1 ⊑ w p 2 : = ∀ p o s t s . w p 1 ( p o s t ) ( s ) ⇒ w p 2 ( p o s t ) ( s ) wp_1 \sqsubseteq wp_2 := \forall post\ s.\ wp_1(post)(s) \Rightarrow wp_2(post)(s) w p 1 ⊑ w p 2 := ∀ p os t s . w p 1 ( p os t ) ( s ) ⇒ w p 2 ( p os t ) ( s )
This order is a monadic relation between any two w p wp w p monads, because they share ret \operatorname{ret} ret and bind \operatorname{bind} bind , and bind \operatorname{bind} bind is monotone, which satisfies both requirements of a monadic relation. If w p 1 ⊑ w p 2 wp_1 \sqsubseteq wp_2 w p 1 ⊑ w p 2 , then every under-approximate triple of w p 1 wp_1 w p 1 implies the same triple of w p 2 wp_2 w p 2 , and every over-approximate triple of w p 2 wp_2 w p 2 implies the same triple of w p 1 wp_1 w p 1 . So the opposite order gives the implications between under-approximate triples.
Monads of s p sp s p s are not so straightforward. Without state, the monad taxonomy collapses into the left taxonomy in Figure 1 , since SP D S P ( A ) \operatorname{SP}^{\mathrm{DSP}}(A) SP DSP ( A ) and SP A S L P ( A ) \operatorname{SP}^{\mathrm{ASLP}}(A) SP ASLP ( A ) coincide with SP A S P ( A ) \operatorname{SP}^{\mathrm{ASP}}(A) SP ASP ( A ) and SP D S L P ( A ) \operatorname{SP}^{\mathrm{DSLP}}(A) SP DSLP ( A ) , as described in §4.3. With state, the four stateful s p sp s p monads share the type of stateful monotone s p sp s p s, SP S t ( S , A ) = ( S → P ) → ( A × S → P ) \operatorname{SP}^{\mathrm{St}}(S, A) = (S \to\mathbb{P}) \to(A \times S \to\mathbb{P}) SP St ( S , A ) = ( S → P ) → ( A × S → P ) , and we, again, start from the pointwise order:
s p 1 ⊑ s p 2 : = ∀ p r e ( a , s ) . s p 1 ( p r e ) ( a , s ) ⇒ s p 2 ( p r e ) ( a , s ) sp_1 \sqsubseteq sp_2 := \forall pre\ (a, s).\ sp_1(pre)(a, s) \Rightarrow sp_2(pre)(a, s) s p 1 ⊑ s p 2 := ∀ p r e ( a , s ) . s p 1 ( p r e ) ( a , s ) ⇒ s p 2 ( p r e ) ( a , s )
However, this is not a monadic relation between any of the s p sp s p monads because the bind requirement is not satisfied. Every implication between s p sp s p monads has SP D S P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St or SP A S L P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St at one end, and their bind is not monotone for ⊑ \sqsubseteq ⊑ , as demonstrated in Figure 2 . Since bind is computed through the aspOf \operatorname{aspOf} aspOf and dslpOf \operatorname{dslpOf} dslpOf components, we instead compare components: a SP D S P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St element d s p dsp d s p is related to an SP A S P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St element a s p asp a s p when aspOf ( d s p ) ⊑ a s p \operatorname{aspOf}(dsp) \sqsubseteq asp aspOf ( d s p ) ⊑ a s p and to a SP D S L P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St element d s l p dslp d s l p when dslpOf ( d s p ) ⊑ d s l p \operatorname{dslpOf}(dsp) \sqsubseteq dslp dslpOf ( d s p ) ⊑ d s l p . Likewise, an SP A S L P S t \operatorname{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St element a s l p aslp a s l p is related to them when a s p ⊑ aspOf ( a s l p ) asp \sqsubseteq\operatorname{aspOf}(aslp) a s p ⊑ aspOf ( a s l p ) and when d s l p ⊑ dslpOf ( a s l p ) dslp \sqsubseteq\operatorname{dslpOf}(aslp) d s l p ⊑ dslpOf ( a s l p ) . These relations are monadic, and are represented by the marks ⋆ \star ⋆ (aspOf \operatorname{aspOf} aspOf ) and ⋆ \star ⋆ (dslpOf \operatorname{dslpOf} dslpOf ) in Figure 1 . Additionally, each implies ⊑ \sqsubseteq ⊑ between the s p sp s p s themselves: a d s p dsp d s p is the conjunction of its components and an a s l p aslp a s l p their disjunction (Lemma 7 and Lemma 9), so
Figure 2. Sequencing two computations, with initial state 1, intermediate states 2 and 3, and final state 4. The first computation m m m maps p r e pre p r e to m p r e m\ pre m p r e , which bind passes to the continuation as its precondition. (a) The S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St case: state 4 is reachable only from initial states in p r e pre p r e (green path), so the postcondition should contain it. A bind that only sees m p r e m\ pre m p r e excludes it, because the continuation also reaches 4 from state 3, which is outside m p r e m\ pre m p r e (orange path). (b) The S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St case: state 4 is reachable only from an initial state outside p r e pre p r e (green path), so the postcondition should not contain it. A bind that only sees m p r e m\ pre m p r e includes it, because the continuation also reaches 4 from state 3, which is inside m p r e m\ pre m p r e (orange path).
d s p ⊑ aspOf ( d s p ) dsp \sqsubseteq\operatorname{aspOf}(dsp) d s p ⊑ aspOf ( d s p ) and aspOf ( a s l p ) ⊑ a s l p \operatorname{aspOf}(aslp) \sqsubseteq aslp aspOf ( a s l p ) ⊑ a s l p , and likewise for d s l p O f dslpOf d s l pO f . By transitivity, aspOf ( d s p ) ⊑ a s p \operatorname{aspOf}(dsp) \sqsubseteq asp aspOf ( d s p ) ⊑ a s p gives d s p ⊑ a s p dsp \sqsubseteq asp d s p ⊑ a s p , and a s p ⊑ aspOf ( a s l p ) asp \sqsubseteq\operatorname{aspOf}(aslp) a s p ⊑ aspOf ( a s l p ) gives a s p ⊑ a s l p asp \sqsubseteq aslp a s p ⊑ a s l p .
Contrapositive Relation
The taxonomy of triples has contrapositives between over-approximate and under-approximate w p wp w p triples, and between the over-approximate and under-approximate s p sp s p triples. We represent them as monadic relations between w p wp w p monads and between s p sp s p monads.
For w p wp w p , we again work with W P S t ( S , A ) \mathrm{WP}^{\mathrm{St}}(S,A) WP St ( S , A ) . We relate two w p s wps w p s when each is the negation of the other applied to the negated postcondition:
w p 1 ▹ ◃ w p 2 : = ∀ p o s t s . w p 1 ( p o s t ) ( s ) ⟺ ¬ w p 2 ( λ r . ¬ p o s t ( r ) ) ( s ) wp_1 \mathbin{\triangleright\triangleleft} wp_2 := \forall post\ s.\ wp_1(post)(s) \Longleftrightarrow\neg wp_2(\lambda r.\ \neg post(r))(s) w p 1 ▹ ◃ w p 2 := ∀ p os t s . w p 1 ( p os t ) ( s ) ⟺ ¬ w p 2 ( λ r . ¬ p os t ( r )) ( s )
This is a monadic relation between the monads W P A W P S t ( S , A ) \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}}(S,A) WP AWP St ( S , A ) and W P D W L P S t ( S , A ) \mathrm{WP}^{\mathrm{St}}_{\mathrm{DWLP}}(S,A) WP DWLP St ( S , A ) and the monads W P D W P S t ( S , A ) \mathrm{WP}^{\mathrm{St}}_{\mathrm{DWP}}(S,A) WP DWP St ( S , A ) and W P A W L P S t ( S , A ) \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWLP}}(S,A) WP AWLP St ( S , A ) , pairing their elements one to one. If w p 1 ▹ ◃ w p 2 wp_1 \mathbin{\triangleright\triangleleft} wp_2 w p 1 ▹ ◃ w p 2 , then the under-approximate triple of w p 1 wp_1 w p 1 is the contrapositive of the over-approximate triple of w p 2 wp_2 w p 2 , that is, p ⇒ w p 1 ( q ) ⟺ w p 2 ( ¬ q ) ⇒ ¬ p p \Rightarrow wp_1(q) \Longleftrightarrow wp_2(\neg q) \Rightarrow\neg p p ⇒ w p 1 ( q ) ⟺ w p 2 ( ¬ q ) ⇒ ¬ p . The relation is symmetric, so the over-approximate triple of w p 2 wp_2 w p 2 is likewise the contrapositive of the under-approximate triple of w p 1 wp_1 w p 1 .
For s p sp s p , we again work in S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}(S,A) SP St ( S , A ) and use the same relation, now negating the precondition:
s p 1 ▹ ◃ s p 2 : = ∀ p r e r . s p 1 ( p r e ) ( r ) ⟺ ¬ s p 2 ( λ s . ¬ p r e ( s ) ) ( r ) sp_1 \mathbin{\triangleright\triangleleft} sp_2 := \forall pre\ r.\ sp_1(pre)(r) \Longleftrightarrow\neg sp_2(\lambda s.\ \neg pre(s))(r) s p 1 ▹ ◃ s p 2 := ∀ p r e r . s p 1 ( p r e ) ( r ) ⟺ ¬ s p 2 ( λ s . ¬ p r e ( s )) ( r )
This is a monadic relation between the monads S P A S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}}(S,A) SP ASP St ( S , A ) and S P D S L P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}}(S,A) SP DSLP St ( S , A ) and between the monads S P D S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}}(S,A) SP DSP St ( S , A ) and S P A S L P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}}(S,A) SP ASLP St ( S , A ) , pairing again their elements one to one. Unlike for implications, bind preserves this relation, so no components are needed, even for S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St and S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St . The reason is that negation exchanges the two components, ¬ a s p ( ¬ p r e ) = d s l p ( p r e ) \neg asp(\neg pre) = dslp(pre) ¬ a s p ( ¬ p r e ) = d s l p ( p r e ) and ¬ d s l p ( ¬ p r e ) = a s p ( p r e ) \neg dslp(\neg pre) = asp(pre) ¬ d s l p ( ¬ p r e ) = a s p ( p r e ) , and, by De Morgan, turns the conjunction used by S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St into the disjunction used by S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St :
¬ d s p ( ¬ p r e ) = ¬ ( a s p ( ¬ p r e ) ∧ d s l p ( ¬ p r e ) ) = ¬ a s p ( ¬ p r e ) ∨ ¬ d s l p ( ¬ p r e ) = d s l p ( p r e ) ∨ a s p ( p r e ) = a s l p ( p r e ) . \begin{aligned}
\neg dsp(\neg pre) = \neg\left(asp(\neg pre) \land dslp(\neg pre)\right) \\
&= \neg asp(\neg pre) \lor\neg dslp(\neg pre) \\
&= dslp(pre) \lor asp(pre) \\
&= aslp(pre).
\end{aligned} ¬ d s p ( ¬ p r e ) = ¬ ( a s p ( ¬ p r e ) ∧ d s l p ( ¬ p r e ) ) = ¬ a s p ( ¬ p r e ) ∨ ¬ d s l p ( ¬ p r e ) = d s l p ( p r e ) ∨ a s p ( p r e ) = a s l p ( p r e ) .
If s p 1 ⋈ s p 2 sp_1 \bowtie sp_2 s p 1 ⋈ s p 2 , then the under-approximate triple of s p 1 sp_1 s p 1 is the contrapositive of the over-approximate triple of s p 2 sp_2 s p 2 , that is, q ⇒ s p 1 ( p ) ⟺ s p 2 ( ¬ p ) ⇒ ¬ q q \Rightarrow sp_1(p) \Longleftrightarrow sp_2(\neg p) \Rightarrow\neg q q ⇒ s p 1 ( p ) ⟺ s p 2 ( ¬ p ) ⇒ ¬ q , and symmetrically.
Adjunct Relations
The taxonomy of triples has two Galois connections connecting an over-approximate w p wp w p triple and an under-approximate s p sp s p triple, and vice versa. Working with W P S t \mathrm{WP}^{\mathrm{St}} WP St and S P S t \mathrm{SP}^{\mathrm{St}} SP St , we define the relation L A : S P S t ( S , A ) → W P S t ( S , A ) → P \mathrm{L}_{\mathrm{A}} : \mathrm{SP}^{\mathrm{St}}(S,A) \to\mathrm{WP}^{\mathrm{St}}(S,A) \to\mathbb{P} L A : SP St ( S , A ) → WP St ( S , A ) → P capturing s p s sps s p s that are left adjoint to w p s wps w p s :
L A s p w p : = ∀ p r e p o s t . ( s p ( p r e ) ⊑ p o s t ) ⟺ ( p r e ⊑ w p ( p o s t ) ) \mathrm{L}_{\mathrm{A}}\ sp\ wp := \forall pre\ post.\ (sp(pre) \sqsubseteq post) \Longleftrightarrow(pre \sqsubseteq wp(post)) L A s p w p := ∀ p r e p os t . ( s p ( p r e ) ⊑ p os t ) ⟺ ( p r e ⊑ w p ( p os t ))
In a dual fashion, the relation R A : S P S t ( S , A ) → W P S t ( S , A ) → P \mathrm{R}_{\mathrm{A}} : \mathrm{SP}^{\mathrm{St}}(S,A) \to\mathrm{WP}^{\mathrm{St}}(S,A) \to\mathbb{P} R A : SP St ( S , A ) → WP St ( S , A ) → P relates s p s sps s p s right adjoint to w p s wps w p s :
R A s p w p : = ∀ p r e p o s t . ( w p ( p o s t ) ⊑ p r e ) ⟺ ( p o s t ⊑ s p ( p r e ) ) \mathrm{R}_{\mathrm{A}}\ sp\ wp := \forall pre\ post.\ (wp(post) \sqsubseteq pre) \Longleftrightarrow(post \sqsubseteq sp(pre)) R A s p w p := ∀ p r e p os t . ( w p ( p os t ) ⊑ p r e ) ⟺ ( p os t ⊑ s p ( p r e ))
Both relations are compatible with the monadic structures involved.
Lemma 1. 1. L \mathrm{L} L is a monadic relation between S P S t \mathrm{SP}^{\mathrm{St}} SP St equipped with the angelic monadic operations (a n g - s t ang\text{-}st an g - s t ) and W P S t \mathrm{WP}^{\mathrm{St}} WP St . Moreover, if L A s p w p \mathrm{L}_{\mathrm{A}}\ sp\ wp L A s p w p , then s p sp s p is disjunctive and strict while w p wp w p is conjunctive and terminating.
R \mathrm{R} R is a monadic relation between S P S t \mathrm{SP}^{\mathrm{St}} SP St equipped with the demonic monadic operations (d e m - s t dem\text{-}st d e m - s t ) and W P S t \mathrm{WP}^{\mathrm{St}} WP St . Moreover, if R A s p w p \mathrm{R}_{\mathrm{A}}\ sp\ wp R A s p w p , then s p sp s p is conjunctive and terminating while w p wp w p is disjunctive and strict.
In both cases, the left adjoint preserves all disjunctions and the right adjoint all conjunctions. As a consequence, L \mathrm{L} L restricts to a monadic relation between S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St and W P D W L P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{DWLP}} WP DWLP St , and R \mathrm{R} R to one between S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St and W P A W P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}} WP AWP St . §6 will further show that both pair elements one to one. The relations L \mathrm{L} L and R \mathrm{R} R are dual in the following sense:
( w p 1 ⋈ w p 2 ∧ s p 1 ⋈ s p 2 ) ⇒ ( L A s p 1 w p 1 ⟺ R A s p 2 w p 2 ) (wp_1 \bowtie wp_2 \land sp_1 \bowtie sp_2) \Rightarrow(\mathrm{L}_{\mathrm{A}}\ sp_1\ wp_1 \Longleftrightarrow\mathrm{R}_{\mathrm{A}}\ sp_2\ wp_2) ( w p 1 ⋈ w p 2 ∧ s p 1 ⋈ s p 2 ) ⇒ ( L A s p 1 w p 1 ⟺ R A s p 2 w p 2 )
As a result, one can traverse the above diagrams along the adjunction relation and across the diamonds along the contrapositive relation.
Base Case Collapse
Without state, S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) and S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) coincide with S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) . They have the same elements, ret, and bind, and hence the same natural order. This is why the left diagram of Figure 1 relates them by isomorphisms and two-headed implications.
Every element of these monads is the transformer of a program, a relation R R R from the initial point to results (§ 2.1 ), and for every R R R ,
d s p ( p r e ) ( a ) = ( R a ∧ p r e ) ∧ ( R a ⇒ p r e ) = R a ∧ p r e = a s p ( p r e ) ( a ) , dsp(pre)(a) = (R\,a \land pre) \land(R\,a \Rightarrow pre) = R\,a \land pre = asp(pre)(a), d s p ( p r e ) ( a ) = ( R a ∧ p r e ) ∧ ( R a ⇒ p r e ) = R a ∧ p r e = a s p ( p r e ) ( a ) ,
a s l p ( p r e ) ( a ) = ( R a ∧ p r e ) ∨ ( R a ⇒ p r e ) = R a ⇒ p r e = d s l p ( p r e ) ( a ) . aslp(pre)(a) = (R\,a \land pre) \lor(R\,a \Rightarrow pre) = R\,a \Rightarrow pre = dslp(pre)(a). a s l p ( p r e ) ( a ) = ( R a ∧ p r e ) ∨ ( R a ⇒ p r e ) = R a ⇒ p r e = d s l p ( p r e ) ( a ) .
So the carriers coincide, and since ret and bind compute the transformers of the returned and the sequenced program, they coincide too. The identity is an isomorphism of ordered monads S P D S P ( A ) ≅ S P A S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) \cong\mathrm{SP}_{\mathrm{ASP}}(A) SP DSP ( A ) ≅ SP ASP ( A ) and S P A S L P ( A ) ≅ S P D S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) \cong\mathrm{SP}_{\mathrm{DSLP}}(A) SP ASLP ( A ) ≅ SP DSLP ( A ) .
This is the reversibility collapse of Verscht and Kaminski [37 ] . For programs in which every final state has at most one initial state, angelic and demonic sps coincide. Without state, every computation has this property, so the collapse holds for the monads themselves, not only for particular programs. With state, a result can be reached from two different initial states, one inside p r e pre p r e and one outside, as in Figure 2 , so they can be differentiated.
In summary, the relations of the left diagram of Figure 1 hold for the base monads, and those of the right diagram hold with state. They are proved for the base and state case independently, but we believe that future work can introduce a way to lift these monadic relations via the state monad and arrow transformers.
Specification of Monadic Programs Using Predicate Transformer Monads
Thus far, we have considered wps and sps independent of computations. However, in order to use a predicate transformer to specify a program, a predicate transformer semantics should be fixed to interpret the behavior of the program. For monadic programs, Maillard et al. [26 ] propose to use a monad morphism from the computation monad into the monad of predicate transformers to capture a specific semantics. A monad morphism θ : M ( A ) → W ( A ) \theta: \mathrm{M}(A) \to\mathrm{W}(A) θ : M ( A ) → W ( A ) consists of a family of maps for every type A A A , satisfying the following equations:
θ ( ret v ) = ret v , \theta(\operatorname{ret} v) = \operatorname{ret} v, θ ( ret v ) = ret v ,
θ ( bind m f ) = bind θ ( m ) ( λ a . θ ( f a ) ) . \theta(\operatorname{bind} m\,f) = \operatorname{bind}\,\theta(m)\,(\lambda a.\,\theta(f\,a)). θ ( bind m f ) = bind θ ( m ) ( λa . θ ( f a )) .
The predicate transformer produced by a monad morphism represents the most precise embodiment of the program semantics with respect to the interpretation. We use monad morphisms below in one of two ways: to define a predicate transformer triple instantiated with a ( p r e , p o s t ) (pre, post) ( p r e , p os t ) specification [14 ] , or to compare the predicate transformer of a program to other predicate transformer specifications via an ordering on the predicate transformer monad [2 , 26 , 34 ] .
On the one hand, a ( p r e , p o s t ) (pre, post) ( p r e , p os t ) specification can be validated by checking:
p r e ⊑ θ W P ( m ) ( p o s t ) pre \sqsubseteq\theta_{\mathrm{WP}}(m)(post) p r e ⊑ θ WP ( m ) ( p os t )
This approach is at the core of the Loom verification framework in Lean [14 ] . Since this check works with any monad morphism, we can use all of our predicate transformer monads to construct monadic variants of all the triples from [37 ] .
On the other hand, a wp specification w w w can be validated by checking:
θ W P ( m ) ⊑ w \theta_{\mathrm{WP}}(m) \sqsubseteq w θ WP ( m ) ⊑ w
In order to facilitate a structured interaction between composition and the weakening of specifications, the bind of the monad of predicate transformers has to be monotone with respect to the ordering, as follows:
w 1 ⊑ w 2 ∀ ( x : A ) . k 1 x ⊑ k 2 x bind W P w 1 k 1 ⊑ bind W P w 2 k 2 \begin{aligned}
w_1 &\sqsubseteq w_2 \qquad\forall(x : A).\, k_1 x \sqsubseteq k_2 x\\
\operatorname{bind}^{\mathrm{WP}} w_1\,k_1 &\sqsubseteq\operatorname{bind}^{\mathrm{WP}} w_2\,k_2
\end{aligned} w 1 bind WP w 1 k 1 ⊑ w 2 ∀ ( x : A ) . k 1 x ⊑ k 2 x ⊑ bind WP w 2 k 2
Monads with this property, also called specification monads, are integral to the construction of Dijkstra monads and the heart of verification frameworks like F* [2 , 26 , 34 ] . Of our new s p sp s p monads, S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP and S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP are specification monads and can, therefore, be used to construct new Dijkstra monads based on s p s sps s p s .
The rest of this section works out and compares these two styles: §5.1 gives monad morphisms from the stateful computation monad S t T ( N D i v ) ( S , A ) \mathrm{StT}(\mathrm{NDiv})(S,A) StT ( NDiv ) ( S , A ) into our eight stateful predicate transformer monads. §5.2 derives them from morphisms from the base computation monad N D i v \mathrm{NDiv} NDiv into our base predicate transformer monads. This lifting uses the state monad and arrow transformers on the w p wp w p and s p sp s p sides, respectively. §5.3 uses these morphisms to define under- and over-approximate triples in the style of [14 ] and discusses how they compose. §5.4 turns to the Dijkstra monad-style and discusses when the two styles coincide. Finally, we build Dijkstra monads over the six specification monads, excluding S P D S P \mathrm{SP}_{\mathrm{DSP}} SP DSP and S P A S L P \mathrm{SP}_{\mathrm{ASLP}} SP ASLP , whose binds are not monotone for their natural orders.
Constructing Stateful Monad Morphisms
As stated above, a common approach to derive a predicate transformer that interprets the behavior of a program is to construct a monad morphism from the computation monad to the monad of predicate transformers. Such a morphism typically interprets the outcomes of the computation. For nondeterminism and nontermination, a computation applied to a start state gives the set of all its outcomes, each a result or divergence, which the morphism can interpret angelically (some result must satisfy the postcondition) or demonically (all of them must), forbidding divergence (total) or permitting it (liberal).
We start with a base computation monad with only nondeterminism and nontermination, which we call N D i v \mathrm{NDiv} NDiv , and construct the stateful version by applying the state monad transformer, S t T ( N D i v ) ( S , A ) \mathrm{StT}(\mathrm{NDiv})(S,A) StT ( NDiv ) ( S , A ) , which we will call State. A computation m m m of the computation monad maps a start state s s s to a non-empty set m s m\,s m s of outcomes. An outcome o o o is either a result r : A × S r : A \times S r : A × S , written val ( r ) \operatorname{val}(r) val ( r ) , or divergence, written div \operatorname{div} div . We write θ D W P S t \theta_{\mathrm{DWP}}^{\mathrm{St}} θ DWP St for the monad morphism into W P D W P S t ( S , A ) \mathrm{WP}_{\mathrm{DWP}}^{\mathrm{St}}(S,A) WP DWP St ( S , A ) , with the interpreted effect on top and the variant below, and likewise for the other seven. The four w p wp w p monad morphisms are:
θ D W P S t ( m ) : = λ p o s t s . ∀ o ∈ m s . match o with val ( r ) ↦ p o s t r ∣ div ↦ ⊥ , θ D W L P S t ( m ) : = λ p o s t s . ∀ o ∈ m s . match o with val ( r ) ↦ p o s t r ∣ div ↦ ⊤ , θ A W P S t ( m ) : = λ p o s t s . ∃ o ∈ m s . match o with val ( r ) ↦ p o s t r ∣ div ↦ ⊥ , θ A W L P S t ( m ) : = λ p o s t s . ∃ o ∈ m s . match o with val ( r ) ↦ p o s t r ∣ div ↦ ⊤ . (4) \begin{aligned}
\theta_{\mathrm{DWP}}^{\mathrm{St}}(m) &:= \lambda\,post\,s.\,\forall o \in m\,s.\,\operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(r) \mapsto post\ r \mid\operatorname{div} \mapsto\bot,\\
\theta_{\mathrm{DWLP}}^{\mathrm{St}}(m) &:= \lambda\,post\,s.\,\forall o \in m\,s.\,\operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(r) \mapsto post\ r \mid\operatorname{div} \mapsto\top,\\
\theta_{\mathrm{AWP}}^{\mathrm{St}}(m) &:= \lambda\,post\,s.\,\exists o \in m\,s.\,\operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(r) \mapsto post\ r \mid\operatorname{div} \mapsto\bot,\\
\theta_{\mathrm{AWLP}}^{\mathrm{St}}(m) &:= \lambda\,post\,s.\,\exists o \in m\,s.\,\operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(r) \mapsto post\ r \mid\operatorname{div} \mapsto\top. \tag*{(4)}
\end{aligned} θ DWP St ( m ) θ DWLP St ( m ) θ AWP St ( m ) θ AWLP St ( m ) := λ p os t s . ∀ o ∈ m s . match o with val ( r ) ↦ p os t r ∣ div ↦ ⊥ , := λ p os t s . ∀ o ∈ m s . match o with val ( r ) ↦ p os t r ∣ div ↦ ⊤ , := λ p os t s . ∃ o ∈ m s . match o with val ( r ) ↦ p os t r ∣ div ↦ ⊥ , := λ p os t s . ∃ o ∈ m s . match o with val ( r ) ↦ p os t r ∣ div ↦ ⊤. (4)
The four s p sp s p monad morphisms quantify over the start states and their outcomes:
θ A S P S t ( m ) : = λ p r e r . ∃ s . ∃ o ∈ m s . p r e s ∧ o = r , θ D S L P S t ( m ) : = λ p r e r . ∀ s . ∀ o ∈ m s . o = r ⇒ p r e s , θ D S P S t ( m ) : = λ p r e r . θ A S P S t ( m ) p r e r ∧ θ D S L P S t ( m ) p r e r , θ A S L P S t ( m ) : = λ p r e r . θ A S P S t ( m ) p r e r ∨ θ D S L P S t ( m ) p r e r . (5) \begin{aligned}
\theta^{\mathrm{St}}_{\mathrm{ASP}}(m) &:= \lambda\ pre\ r.\ \exists s.\ \exists o \in m\ s.\ pre\ s \land o = r,\\
\theta^{\mathrm{St}}_{\mathrm{DSLP}}(m) &:= \lambda\ pre\ r.\ \forall s.\ \forall o \in m\ s.\ o = r \Rightarrow pre\ s,\\
\theta^{\mathrm{St}}_{\mathrm{DSP}}(m) &:= \lambda\ pre\ r.\ \theta^{\mathrm{St}}_{\mathrm{ASP}}(m)\ pre\ r \land\theta^{\mathrm{St}}_{\mathrm{DSLP}}(m)\ pre\ r,\\
\theta^{\mathrm{St}}_{\mathrm{ASLP}}(m) &:= \lambda\ pre\ r.\ \theta^{\mathrm{St}}_{\mathrm{ASP}}(m)\ pre\ r \lor\theta^{\mathrm{St}}_{\mathrm{DSLP}}(m)\ pre\ r. \tag*{(5)}
\end{aligned} θ ASP St ( m ) θ DSLP St ( m ) θ DSP St ( m ) θ ASLP St ( m ) := λ p r e r . ∃ s . ∃ o ∈ m s . p r e s ∧ o = r , := λ p r e r . ∀ s . ∀ o ∈ m s . o = r ⇒ p r e s , := λ p r e r . θ ASP St ( m ) p r e r ∧ θ DSLP St ( m ) p r e r , := λ p r e r . θ ASP St ( m ) p r e r ∨ θ DSLP St ( m ) p r e r . (5)
As in Verscht and Kaminski [37 ] , where d s p dsp d s p and a s l p aslp a s l p are defined as the conjunction and the disjunction of a s p asp a s p and d s l p dslp d s l p , we define θ D S P S t \theta^{\mathrm{St}}_{\mathrm{DSP}} θ DSP St and θ A S L P S t \theta^{\mathrm{St}}_{\mathrm{ASLP}} θ ASLP St as the pointwise conjunction and disjunction of θ A S P S t \theta^{\mathrm{St}}_{\mathrm{ASP}} θ ASP St and θ D S L P S t \theta^{\mathrm{St}}_{\mathrm{DSLP}} θ DSLP St .
Lifting Morphisms
Now, we describe how we could have derived those same stateful monad morphisms from base monad morphisms using the state monad and arrow transformers.
For w p s wps w p s , we construct base monad morphisms from NDiv into the base w p wp w p monads, which we write without the superscript. As shown below, they largely mirror their stateful counterparts:
θ D W P ( m ) : = λ p o s t . ∀ o ∈ m . match o with val ( a ) ↦ p o s t a ∣ d i v ↦ ⊥ , θ D W L P ( m ) : = λ p o s t . ∀ o ∈ m . match o with val ( a ) ↦ p o s t a ∣ d i v ↦ ⊤ , θ A W P ( m ) : = λ p o s t . ∃ o ∈ m . match o with val ( a ) ↦ p o s t a ∣ d i v ↦ ⊥ , θ A W L P ( m ) : = λ p o s t . ∃ o ∈ m . match o with val ( a ) ↦ p o s t a ∣ d i v ↦ ⊤ . (6) \begin{aligned}
\theta_{\mathrm{DWP}}(m) &:= \lambda\ post.\ \forall o \in m.\ \operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(a) \mapsto post\ a \mid div \mapsto\bot,\\
\theta_{\mathrm{DWLP}}(m) &:= \lambda\ post.\ \forall o \in m.\ \operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(a) \mapsto post\ a \mid div \mapsto\top,\\
\theta_{\mathrm{AWP}}(m) &:= \lambda\ post.\ \exists o \in m.\ \operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(a) \mapsto post\ a \mid div \mapsto\bot,\\
\theta_{\mathrm{AWLP}}(m) &:= \lambda\ post.\ \exists o \in m.\ \operatorname{match}\ o\ \operatorname{with}\ \operatorname{val}(a) \mapsto post\ a \mid div \mapsto\top. \tag*{(6)}
\end{aligned} θ DWP ( m ) θ DWLP ( m ) θ AWP ( m ) θ AWLP ( m ) := λ p os t . ∀ o ∈ m . match o with val ( a ) ↦ p os t a ∣ d i v ↦ ⊥ , := λ p os t . ∀ o ∈ m . match o with val ( a ) ↦ p os t a ∣ d i v ↦ ⊤ , := λ p os t . ∃ o ∈ m . match o with val ( a ) ↦ p os t a ∣ d i v ↦ ⊥ , := λ p os t . ∃ o ∈ m . match o with val ( a ) ↦ p os t a ∣ d i v ↦ ⊤. (6)
The state monad transformer also lifts monad morphisms: if θ \theta θ is a monad morphism from NDiv to WP, then applying it at each initial state, λ m s . θ ( m s ) \lambda m\ s.\ \theta(m\ s) λm s . θ ( m s ) , is a monad morphism from StT(NDiv)(S, A) to StT(WP)(S, A) = WPS t ^{\mathrm{St}} St (S, A). In fact, this is how the stateful w p wp w p morphisms of Eq. (4) are defined. We also use the exception monad transformer, ExcT, below the state monad transformer, giving monad morphisms from StT(ExcT(NDiv))(S, A) to WS t E x c ^{\mathrm{StExc}} StExc (S, A), defined in Appendix H.
For s p s sps s p s , we construct base arrow morphisms from the Kleisli arrow of NDiv into the four base s p sp s p arrows. They largely mirror their stateful counterparts, and since arrows have a non-unit input type, unlike the base s p sp s p monads, the morphisms into ArrD S P _{\mathrm{DSP}} DSP and ArrA S P _{\mathrm{ASP}} ASP , and into ArrD S L P _{\mathrm{DSLP}} DSLP and ArrA S L P _{\mathrm{ASLP}} ASLP , remain distinct:
θ A r r A S P ( f ) : = λ p r e a . ∃ b . ∃ o ∈ f b . p r e b ∧ o = a , θ A r r D S L P ( f ) : = λ p r e a . ∀ b . ∀ o ∈ f b . o = a ⇒ p r e b , θ A r r D S P ( f ) : = λ p r e a . θ A r r A S P ( f ) p r e a ∧ θ A r r D S L P ( f ) p r e a , θ A r r A S L P ( f ) : = λ p r e a . θ A r r A S P ( f ) p r e a ∨ θ A r r D S L P ( f ) p r e a . (7) \begin{aligned}
\theta_{\mathrm{ArrASP}}(f) &:= \lambda\ pre\ a.\ \exists b.\ \exists o \in f\ b.\ pre\ b \land o = a,\\
\theta_{\mathrm{ArrDSLP}}(f) &:= \lambda\ pre\ a.\ \forall b.\ \forall o \in f\ b.\ o = a \Rightarrow pre\ b,\\
\theta_{\mathrm{ArrDSP}}(f) &:= \lambda\ pre\ a.\ \theta_{\mathrm{ArrASP}}(f)\ pre\ a \land\theta_{\mathrm{ArrDSLP}}(f)\ pre\ a,\\
\theta_{\mathrm{ArrASLP}}(f) &:= \lambda\ pre\ a.\ \theta_{\mathrm{ArrASP}}(f)\ pre\ a \lor\theta_{\mathrm{ArrDSLP}}(f)\ pre\ a. \tag*{(7)}
\end{aligned} θ ArrASP ( f ) θ ArrDSLP ( f ) θ ArrDSP ( f ) θ ArrASLP ( f ) := λ p r e a . ∃ b . ∃ o ∈ f b . p r e b ∧ o = a , := λ p r e a . ∀ b . ∀ o ∈ f b . o = a ⇒ p r e b , := λ p r e a . θ ArrASP ( f ) p r e a ∧ θ ArrDSLP ( f ) p r e a , := λ p r e a . θ ArrASP ( f ) p r e a ∨ θ ArrDSLP ( f ) p r e a . (7)
The state arrow transformer likewise lifts arrow morphisms: if θ \theta θ is an arrow morphism from the Kleisli arrow of NDiv, Kl(NDiv), to an s p sp s p arrow Arr, then the same θ \theta θ , at the input and output types B × S and A × S, is an arrow morphism from StArrT(Kl(NDiv))(S, B, A) to StArrT(Arr)(S, B, A). Fixing the input to 1 turns it into a monad morphism from StT(NDiv)(S, A) to the stateful s p sp s p monad, since a stateful computation m m m is the arrow λ ( ∗ , s ) . m s \lambda(*,s).\ m\ s λ ( ∗ , s ) . m s from 1 × S 1 \times S 1 × S . The stateful s p sp s p morphisms of Eq. (5) are exactly these lifts; for instance, θ A S P S t ( m ) = θ A r r A S P ( λ ( ∗ , s ) . m s ) \theta_{\mathrm{ASP}}^{\mathrm{St}}(m) = \theta_{\mathrm{ArrASP}}(\lambda(\ast,s).\,m\,s) θ ASP St ( m ) = θ ArrASP ( λ ( ∗ , s ) . m s ) . We also use the exception arrow transformer, E x c A r r T \mathrm{ExcArrT} ExcArrT , below the state arrow transformer, which in the same way gives monad morphisms from S t T ( E x c T ( N D i v ) ) ( S , A ) \mathrm{StT}(\mathrm{ExcT}(\mathrm{NDiv}))(S,A) StT ( ExcT ( NDiv )) ( S , A ) into the s p sp s p monads with state and exceptions, defined in Appendix H.
Triples from Monad Morphisms
We use the generic w p wp w p triples of Loom [14 ] , of the following form:
p r e ⊑ θ W P ( m ) ( p o s t ) pre \sqsubseteq\theta_{\mathrm{WP}}(m)(post) p r e ⊑ θ WP ( m ) ( p os t )
where the above ordering corresponds to under-approximate triples and the flipped ordering corresponds to over-approximate triples.
We define a generic s p sp s p triple analogously:
p o s t ⊑ θ S P ( m ) ( p r e ) post \sqsubseteq\theta_{\mathrm{SP}}(m)(pre) p os t ⊑ θ SP ( m ) ( p r e )
For each stateful monad morphism of § 2.3.1 , we fix a program m m m , a precondition p p p , and a postcondition q q q , and construct the corresponding over- and under-approximate w p wp w p and s p sp s p triples, where the over-approximate version uses the opposite ordering on predicates in the triple definitions above.
Each edge of diagram (b) of Figure 1 yields a connection between triples; we give one example per kind of edge. S P A S L P S t \mathrm{SP}_{\mathrm{ASLP}}^{\mathrm{St}} SP ASLP St is related to S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St by the implication relation, so we obtain an implication between the over-approximate triples θ A S L P S t ( m ) ( p ) ⊑ q \theta_{\mathrm{ASLP}}^{\mathrm{St}}(m)(p) \sqsubseteq q θ ASLP St ( m ) ( p ) ⊑ q and θ A S P S t ( m ) ( p ) ⊑ q \theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p) \sqsubseteq q θ ASP St ( m ) ( p ) ⊑ q , and the reverse one between the under-approximate triples. S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St is related to S P D S L P S t \mathrm{SP}_{\mathrm{DSLP}}^{\mathrm{St}} SP DSLP St by the contrapositive relation, so we obtain a contrapositive relation between θ A S P S t ( m ) ( p ) ⊑ q \theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p) \sqsubseteq q θ ASP St ( m ) ( p ) ⊑ q and ¬ q ⊑ θ D S L P S t ( m ) ( ¬ p ) \neg q \sqsubseteq\theta_{\mathrm{DSLP}}^{\mathrm{St}}(m)(\neg p) ¬ q ⊑ θ DSLP St ( m ) ( ¬ p ) . Finally, S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St is related to W P D W L P S t \mathrm{WP}_{\mathrm{DWLP}}^{\mathrm{St}} WP DWLP St by the adjunction relation, so we obtain an adjunction, which at this level is a Galois connection, between θ A S P S t ( m ) ( p ) ⊑ q \theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p) \sqsubseteq q θ ASP St ( m ) ( p ) ⊑ q and p ⊑ θ D W L P S t ( m ) ( q ) p \sqsubseteq\theta_{\mathrm{DWLP}}^{\mathrm{St}}(m)(q) p ⊑ θ DWLP St ( m ) ( q ) . This allows us to reconstruct all of the triples in [37 ] and derive the same relationships.
We can also use these triples to verify monadic programs with a concrete pre- and postcondition specification in a style inspired by Loom [14 ] . Notably, we can verify incorrectness logic triples, focusing here on two variants: O’Hearn’s incorrectness logic [30 ] , where triple validity is determined by q ⇒ a s p ⟦ c ⟧ ( p ) q \Rightarrow asp\llbracket c\rrbracket(p) q ⇒ a s p [ [ c ] ] ( p ) , and Lisbon logic [14 , 27 ] , where triple validity is determined by p ⇒ a w p ⟦ c ⟧ ( q ) p \Rightarrow awp\llbracket c\rrbracket(q) p ⇒ a w p [ [ c ] ] ( q ) .
Consider the following monadic program m : S t a t e ( N , 1 ) m : \mathrm{State}(\mathbb{N},1) m : State ( N , 1 ) :
m : = λ s . { { ( ( ) , 0 ) , ( ( ) , s ) } if s is even , { ( ( ) , s ) } otherwise . m := \lambda s.\begin{cases}
\{((),0),((),s)\} & \text{if } s \text{ is even},\\
\{((),s)\} & \text{otherwise}.
\end{cases} m := λ s . { {(( ) , 0 ) , (( ) , s )} {(( ) , s )} if s is even , otherwise .
Evaluating the monad morphisms targeting W P A W P S t \mathrm{WP}_{\mathrm{AWP}}^{\mathrm{St}} WP AWP St and S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St gives:
θ A W P S t ( m ) = λ p o s t s . p o s t ( ( ) , s ) ∨ ( s is even ∧ p o s t ( ( ) , 0 ) ) , \theta_{\mathrm{AWP}}^{\mathrm{St}}(m) = \lambda\,post\,s.\,post\,((),s) \lor(s \text{ is even} \land post\,((),0)), θ AWP St ( m ) = λ p os t s . p os t (( ) , s ) ∨ ( s is even ∧ p os t (( ) , 0 )) ,
θ A S P S t ( m ) = λ p r e ( ( ) , s ′ ) . p r e s ′ ∨ ( s ′ = 0 ∧ ∃ s . s is even ∧ p r e s ) . \theta_{\mathrm{ASP}}^{\mathrm{St}}(m) = \lambda\,pre\,(( ),s').\,pre\,s' \lor(s'=0 \land\exists s.\,s \text{ is even} \land pre\,s). θ ASP St ( m ) = λ p r e (( ) , s ′ ) . p r e s ′ ∨ ( s ′ = 0 ∧ ∃ s . s is even ∧ p r e s ) .
Consider the following precondition, p : = λ s . s > 0 p := \lambda s.\,s > 0 p := λ s . s > 0 , and postcondition, q : = λ ( _ , s ′ ) . s ′ = 0 q := \lambda(\_,s').\,s'=0 q := λ ( _ , s ′ ) . s ′ = 0 . Now, evaluating the triple validity as shown above gives:
θ A W P S t ( m ) ( q ) = λ s . s is even so p ⊑ θ A W P S t ( m ) ( q ) fails at s = 1 , \theta_{\mathrm{AWP}}^{\mathrm{St}}(m)(q) = \lambda s.\,s \text{ is even}\quad\text{so }p \sqsubseteq\theta_{\mathrm{AWP}}^{\mathrm{St}}(m)(q)\text{ fails at }s=1, θ AWP St ( m ) ( q ) = λ s . s is even so p ⊑ θ AWP St ( m ) ( q ) fails at s = 1 ,
θ A S P S t ( m ) ( p ) = λ ( ( ) , s ′ ) . ⊤ so q ⊑ θ A S P S t ( m ) ( p ) holds . \theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p) = \lambda((),s').\,\top\quad\text{so }q \sqsubseteq\theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p)\text{ holds}. θ ASP St ( m ) ( p ) = λ (( ) , s ′ ) . ⊤ so q ⊑ θ ASP St ( m ) ( p ) holds .
This example illustrates that the two incorrectness logics are indeed different: as noted in the introduction, O’Hearn’s incorrectness logic has no w p wp w p characterization [37 ] . Intuitively, the Lisbon triple needs every initial state in p p p to reach some final state in q q q , while incorrectness logic only needs every final state in q q q to be reached by some initial state in p p p . If we slightly modify the precondition to p ′ : = λ s . s > 0 ∧ s is even p' := \lambda s.\ s > 0 \land s\ \text{is even} p ′ := λ s . s > 0 ∧ s is even , then both triples hold:
θ A W P S t ( m ) ( q ) = λ s . s is even so p ′ ⊑ θ A W P S t ( m ) ( q ) holds , θ A S P S t ( m ) ( p ′ ) = λ ( ( ) , s ′ ) . s ′ is even so q ⊑ θ A S P S t ( m ) ( p ′ ) holds . \begin{aligned}
\theta_{\mathrm{AWP}}^{\mathrm{St}}(m)(q) &= \lambda\ s.\ s\ \text{is even} &&\text{so } p' \sqsubseteq\theta_{\mathrm{AWP}}^{\mathrm{St}}(m)(q)\ \text{holds},\\
\theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p') &= \lambda\ ((),s').\ s'\ \text{is even} &&\text{so } q \sqsubseteq\theta_{\mathrm{ASP}}^{\mathrm{St}}(m)(p')\ \text{holds}.
\end{aligned} θ AWP St ( m ) ( q ) θ ASP St ( m ) ( p ′ ) = λ s . s is even = λ (( ) , s ′ ) . s ′ is even so p ′ ⊑ θ AWP St ( m ) ( q ) holds , so q ⊑ θ ASP St ( m ) ( p ′ ) holds .
Composition
Monadic programs are sequenced using bind, where the continuation takes the returned value of the first computation. Triples can be sequenced similarly. Given p ⊑ θ W P S t ( m ) ( q 0 ) p \sqsubseteq\theta_{\mathrm{WP}}^{\mathrm{St}}(m)(q_0) p ⊑ θ WP St ( m ) ( q 0 ) and, for every returned value a a a , p a ⊑ θ W P S t ( f a ) ( q ) p_a \sqsubseteq\theta_{\mathrm{WP}}^{\mathrm{St}}(f\ a)(q) p a ⊑ θ WP St ( f a ) ( q ) , assume also that q 0 ( a , s ) ⇒ p a ( s ) q_0(a,s) \Rightarrow p_a(s) q 0 ( a , s ) ⇒ p a ( s ) for all a a a and s s s . By monotonicity of θ W P S t ( m ) \theta_{\mathrm{WP}}^{\mathrm{St}}(m) θ WP St ( m ) , we have p ⊑ θ W P S t ( m ) ( λ ( a , s ) . θ W P S t ( f a ) ( q ) ( s ) ) p \sqsubseteq\theta_{\mathrm{WP}}^{\mathrm{St}}(m)(\lambda(a,s).\ \theta_{\mathrm{WP}}^{\mathrm{St}}(f\ a)(q)(s)) p ⊑ θ WP St ( m ) ( λ ( a , s ) . θ WP St ( f a ) ( q ) ( s )) . Here we recognize WP’s bind from Eq. (1) . By the monad morphism law, this simplifies to p ⊑ θ W P S t ( bind m f ) ( q ) p \sqsubseteq\theta_{\mathrm{WP}}^{\mathrm{St}}(\operatorname{bind}\ m\ f)(q) p ⊑ θ WP St ( bind m f ) ( q ) . The over-approximate rule flips the three orderings and side condition, p a ( s ) ⇒ q 0 ( a , s ) p_a(s) \Rightarrow q_0(a,s) p a ( s ) ⇒ q 0 ( a , s ) , and holds for the same reason, so all eight kinds of w p wp w p triples compose.
The s p sp s p triples are less uniform. For S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St and S P D S L P S t \mathrm{SP}_{\mathrm{DSLP}}^{\mathrm{St}} SP DSLP St , the analogous rules hold in both directions. This covers Hoare logic, as the over-approximate S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St triple, and incorrectness logic [30 ] , as the under-approximate one. An under-approximate S P D S P S t \mathrm{SP}_{\mathrm{DSP}}^{\mathrm{St}} SP DSP St triple is the conjunction of an S P A S P S t \mathrm{SP}_{\mathrm{ASP}}^{\mathrm{St}} SP ASP St triple and a S P D S L P S t \mathrm{SP}_{\mathrm{DSLP}}^{\mathrm{St}} SP DSLP St triple, and so is an over-approximate S P A S L P S t \mathrm{SP}_{\mathrm{ASLP}}^{\mathrm{St}} SP ASLP St triple, so these compose through their components.
The two remaining kinds, over-approximate S P D S P S t \mathrm{SP}_{\mathrm{DSP}}^{\mathrm{St}} SP DSP St triples and under-approximate S P A S L P S t \mathrm{SP}_{\mathrm{ASLP}}^{\mathrm{St}} SP ASLP St triples, do not compose. Figure 2 (a) is a counterexample for S P D S P S t \mathrm{SP}_{\mathrm{DSP}}^{\mathrm{St}} SP DSP St : take p = { 1 } p = \{1\} p = { 1 } , q 0 = p a = { 2 } q_0 = p_a = \{2\} q 0 = p a = { 2 } , and q = ∅ q = \varnothing q = ∅ . The first computation takes p p p exactly to q 0 q_0 q 0 . From p a p_a p a , the second computation gives the empty postcondition, because state 4 is also reachable from state 3, which is outside of p a p_a p a . Both premises and the side condition hold, but the conclusion fails: in the sequenced computation, state 4 is reachable only from p p p , since the first computation never produces state 3. Figure 2 (b) gives the dual counterexample for S P A S L P S t \mathrm{SP}_{\mathrm{ASLP}}^{\mathrm{St}} SP ASLP St . In both cases, the intermediate assertions do not say which intermediate states the first computation can actually produce.
Predicate Transformers as Ordered Specifications
We next cover another way of specifying a monadic program: directly by a predicate transformer, which is compared against the one computed by the monad morphism. While the generic triple definitions differ between w p wp w p and s p sp s p , comparing a specification given as a predicate transformer p t pt pt against the one computed by the monad morphism takes the same form for all variants: an inequation in the order of the predicate transformer monad,
θ ( m ) ⊑ p t or p t ⊑ θ ( m ) , \theta(m) \sqsubseteq pt \qquad\text{or} \qquad pt \sqsubseteq\theta(m), θ ( m ) ⊑ pt or pt ⊑ θ ( m ) ,
depending on the direction of approximation. The first states that p t pt pt over-approximates the transformer computed for m m m , and the second that it under-approximates it.
As explained at the start of §5, for specification monads, bind must be monotone with respect to the ordering. This allows us to arbitrarily weaken specifications and compose them in the expected manner, as shown for w p s wps w p s
θ W P ( m ) ⊑ w m ∀ ( a : A ) . θ W P ( f a ) ⊑ w f a \theta_{\mathrm{WP}}(m) \sqsubseteq w_m \qquad\forall(a : A).\ \theta_{\mathrm{WP}}(f\ a) \sqsubseteq w_{f\ a} θ WP ( m ) ⊑ w m ∀ ( a : A ) . θ WP ( f a ) ⊑ w f a
θ W P ( bind m f ) ⊑ bind w m w f \theta_{\mathrm{WP}}(\operatorname{bind}\ m\ f) \sqsubseteq\operatorname{bind}\ w_m\ w_f θ WP ( bind m f ) ⊑ bind w m w f
where the conclusion follows from the monad morphism laws. The same rule holds with the order reversed, for specifications that under-approximate.
For all the w p wp w p monads, S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St , and S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St , which are specification monads, such comparisons compose as above and give rise to the corresponding Dijkstra monads, which we will shortly define. For S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St and S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St , bind is not monotone with respect to the natural ordering on s p s sps s p s defined in §4.2.
Such a comparison verifies a monadic program against a concrete predicate transformer specification. We demonstrate this technique on Lisbon logic, for the same program as before and the following W P A W P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}} WP AWP St specification: λ p o s t s . s > 0 ∧ s is even ∧ p o s t ( ( ) , 0 ) \lambda\ post\ s.\ s > 0 \mathbin{\wedge} s\ \text{is even} \mathbin{\wedge} post\ ((), 0) λ p os t s . s > 0 ∧ s is even ∧ p os t (( ) , 0 ) . Evaluating the monad morphism gives:
θ A W P S t ( m ) = λ p o s t s . p o s t ( ( ) , s ) ∨ ( s is even ∧ p o s t ( ( ) , 0 ) ) . \theta^{\mathrm{St}}_{\mathrm{AWP}}(m) = \lambda\ post\ s.\ post\ ((),s) \mathbin{\vee} (s\ \text{is even} \mathbin{\wedge} post\ ((),0)). θ AWP St ( m ) = λ p os t s . p os t (( ) , s ) ∨ ( s is even ∧ p os t (( ) , 0 )) .
Since Lisbon logic is under-approximate, the comparison is p t ⊑ θ A W P S t ( m ) pt \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{AWP}}(m) pt ⊑ θ AWP St ( m ) , which unfolds to
∀ p s . s > 0 ∧ s is even ∧ p ( ( ) , 0 ) ⇒ p ( ( ) , s ) ∨ ( s is even ∧ p ( ( ) , 0 ) ) \forall p\ s.\ s > 0 \mathbin{\wedge} s\ \text{is even} \mathbin{\wedge} p\ ((),0) \Rightarrow p\ ((),s) \mathbin{\vee} (s\ \text{is even} \mathbin{\wedge} p\ ((),0)) ∀ p s . s > 0 ∧ s is even ∧ p (( ) , 0 ) ⇒ p (( ) , s ) ∨ ( s is even ∧ p (( ) , 0 ))
and this holds by the second disjunct.
In fact, this W P A W P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}} WP AWP St predicate transformer is equivalent to the W P A W P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}} WP AWP St pre- and postcondition pair from before. This is an established result between some sets of triples and orderings over predicate transformers; for example, Swamy et al. [34 ] write pre- and postcondition specifications as abbreviations for w p s wps w p s . It is more natural to work with pairs of pre- and postconditions [22 ] . To connect these specifications to predicate transformers, Swamy et al. showed that any pre- and postcondition pair, ( p r e , p o s t ) (pre, post) ( p r e , p os t ) , can be embedded into a w p wp w p as follows
pp2wp ( p r e , p o s t ) : = λ p s . p r e s ∧ ∀ r . p o s t r ⇒ p r \operatorname{pp2wp}(pre, post) := \lambda p\ s.\ pre\ s \mathbin{\wedge} \forall r.\ post\ r \Rightarrow p\ r pp2wp ( p r e , p os t ) := λ p s . p r e s ∧ ∀ r . p os t r ⇒ p r
such that p r e ⊑ θ W P S t ( m ) ( p o s t ) pre \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{WP}}(m)(post) p r e ⊑ θ WP St ( m ) ( p os t ) if and only if pp2wp ( p r e , p o s t ) ⊑ θ W P S t ( m ) \operatorname{pp2wp}(pre, post) \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{WP}}(m) pp2wp ( p r e , p os t ) ⊑ θ WP St ( m ) . We name this embedding pp2wp u n d e r \operatorname{pp2wp}_{\mathrm{under}} pp2wp under to represent that it corresponds to the under-approximate w p wp w p triple.
Over-approximate w p wp w p triples can also be expressed in terms of the order on WP thanks to a dual embedding pp2wp o v e r \operatorname{pp2wp}_{\mathrm{over}} pp2wp over :
pp2wp o v e r ( p r e , p o s t ) : = λ p s . ( ∀ r . p r ⇒ p o s t r ) ⇒ p r e s \operatorname{pp2wp}_{\mathrm{over}}(pre, post) := \lambda\ p\ s.\ (\forall r.\ p\ r \Rightarrow post\ r) \Rightarrow pre\ s pp2wp over ( p r e , p os t ) := λ p s . ( ∀ r . p r ⇒ p os t r ) ⇒ p r e s
With both orderings flipped, we have that θ W P S t ( m ) ( p o s t ) ⊑ p r e \theta^{\mathrm{St}}_{\mathrm{WP}}(m)(post) \sqsubseteq pre θ WP St ( m ) ( p os t ) ⊑ p r e if and only if θ W P S t ( m ) ⊑ pp2wp o v e r ( p r e , p o s t ) \theta^{\mathrm{St}}_{\mathrm{WP}}(m) \sqsubseteq\operatorname{pp2wp}_{\mathrm{over}}(pre, post) θ WP St ( m ) ⊑ pp2wp over ( p r e , p os t ) . Both embeddings produce monotone w p s wps w p s , but they may lie outside the carrier of the w p wp w p monad of the triple. The comparison therefore uses the order of W P S t \mathrm{WP}^{\mathrm{St}} WP St , of which the four w p wp w p monads are submonads.
A natural question is whether similar embeddings exist for the two s p sp s p specification monads. We show that they exist for over-approximate S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St triples and under-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St triples, exactly those triples that are in a Galois connection with a w p wp w p variant:
pp2asp o v e r ( p r e , p o s t ) : = λ p r . ∃ s . p s ∧ ( p r e s ⇒ p o s t r ) , \operatorname{pp2asp}_{\mathrm{over}}(pre, post) := \lambda\ p\ r.\ \exists s.\ p\ s \land(pre\ s \Rightarrow post\ r), pp2asp over ( p r e , p os t ) := λ p r . ∃ s . p s ∧ ( p r e s ⇒ p os t r ) ,
pp2dslp u n d e r ( p r e , p o s t ) : = λ p r . ∀ s . ( p o s t r ⇒ p r e s ) ⇒ p s \operatorname{pp2dslp}_{\mathrm{under}}(pre, post) := \lambda\ p\ r.\ \forall s.\ (post\ r \Rightarrow pre\ s) \Rightarrow p\ s pp2dslp under ( p r e , p os t ) := λ p r . ∀ s . ( p os t r ⇒ p r e s ) ⇒ p s
such that θ A S P S t ( m ) ( p r e ) ⊑ p o s t \theta^{\mathrm{St}}_{\mathrm{ASP}}(m)(pre) \sqsubseteq post θ ASP St ( m ) ( p r e ) ⊑ p os t if and only if θ A S P S t ( m ) = pp2asp o v e r ( p r e , p o s t ) \theta^{\mathrm{St}}_{\mathrm{ASP}}(m) = \operatorname{pp2asp}_{\mathrm{over}}(pre, post) θ ASP St ( m ) = pp2asp over ( p r e , p os t ) and p o s t ⊑ θ D S L P S t ( m ) ( p r e ) post \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{DSL P}}(m)(pre) p os t ⊑ θ DSLP St ( m ) ( p r e ) if and only if pp2dslp u n d e r ( p r e , p o s t ) ⊑ θ D S L P S t ( m ) \operatorname{pp2dslp}_{\mathrm{under}}(pre, post) \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{DSL P}}(m) pp2dslp under ( p r e , p os t ) ⊑ θ DSLP St ( m ) .
The other two s p sp s p triples have no such embedding:
Lemma 2. Let S S S have two distinct states and let A A A be inhabited.
The under-approximate S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St triple, that is, the incorrectness triple, has no embedding: for some p r e pre p r e and p o s t post p os t , there is no element w w w of S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St such that, for every m m m , w ⊑ θ A S P S t ( m ) w \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{ASP}}(m) w ⊑ θ ASP St ( m ) holds exactly when p o s t ⊑ θ A S P S t ( m ) ( p r e ) post \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{ASP}}(m)(pre) p os t ⊑ θ ASP St ( m ) ( p r e ) does.
The over-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSL P}} SP DSLP St triple has no embedding: for some p r e pre p r e and p o s t post p os t , there is no element w w w of S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSL P}} SP DSLP St such that, for every m m m , θ D S L P S t ( m ) ⊑ w \theta^{\mathrm{St}}_{\mathrm{DSL P}}(m) \sqsubseteq w θ DSLP St ( m ) ⊑ w holds exactly when θ D S L P S t ( m ) ( p r e ) ⊑ p o s t \theta^{\mathrm{St}}_{\mathrm{DSL P}}(m)(pre) \sqsubseteq post θ DSLP St ( m ) ( p r e ) ⊑ p os t does.
Proof. We prove the first part; the second is dual, with conjunctivity in place of disjunctivity, and we prove it in Appendix I. An element w w w of S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St is strict and disjunctive, so it satisfies w ( p ) = ⋁ s ∈ p w ( { s } ) w(p) = \bigvee_{s \in p} w(\{s\}) w ( p ) = ⋁ s ∈ p w ({ s }) : it commits to the start state from which each result is reached, whereas the incorrectness triple only asks that the results in p o s t post p os t be reachable from some state of p r e pre p r e . Take distinct states s 0 s_0 s 0 and s 1 s_1 s 1 , a result r 0 r_0 r 0 , p r e = { s 0 , s 1 } pre = \{s_0, s_1\} p r e = { s 0 , s 1 } , and p o s t = { r 0 } post = \{r_0\} p os t = { r 0 } . A program m 1 m_1 m 1 whose only result is r 0 r_0 r 0 , reached only from s 0 s_0 s 0 , and a program m 2 m_2 m 2 whose only result is r 0 r_0 r 0 , reached only from s 1 s_1 s 1 , both satisfy the triple, so such a w w w would satisfy w ⊑ θ A S P S t ( m 1 ) w \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{ASP}}(m_1) w ⊑ θ ASP St ( m 1 ) and w ⊑ θ A S P S t ( m 2 ) w \sqsubseteq\theta^{\mathrm{St}}_{\mathrm{ASP}}(m_2) w ⊑ θ ASP St ( m 2 ) . At a single start state s s s , one of the two bounds is always ⊥ \bot ⊥ , so w ( { s } ) = ⊥ w(\{s\}) = \bot w ({ s }) = ⊥ , and hence w = ⊥ w = \bot w = ⊥ by disjunctivity. But ⊥ ⊑ θ A S P S t ( m ) \bot\sqsubseteq\theta^{\mathrm{St}}_{\mathrm{ASP}}(m) ⊥ ⊑ θ ASP St ( m ) holds for every m m m , so the triple would have to hold for every program. It does not hold for a program that always diverges: such a program reaches no result, so r 0 r_0 r 0 is not reachable from p r e pre p r e .
Now, this does not stop one from specifying a program with an S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St predicate transformer, and some of those predicate transformers might correspond to incorrectness logic triples. However, there does not exist an embedding that converts an incorrectness triple into a corresponding S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St predicate transformer for every triple.
Dijkstra Monads
The monad laws make specifications compose. Monotonicity of bind extends this structured composition to weakened specifications, as described above. This property is necessary for constructing Dijkstra monads.
Definition 2 (Dijkstra monad [26 §5] ). A Dijkstra monad D D D over a specification monad W W W , that is, a monad with a preorder ⊑ A \sqsubseteq_A ⊑ A on each W A W\ A W A for which bind W \operatorname{bind}^{W} bind W is monotone, is given by:
:::`
a return and a bind specified by the return and bind of W W W , satisfying the monad laws (which are well-typed because W W W satisfies them),
and a weakening along the order of W W W , compatible with bind:
v : A m : D A w 1 ∀ ( x : A ) . f x : D B ( w 2 x ) ret D v : D A ( ret W v ) bind D m f : D B ( bind W w 1 w 2 ) \begin{aligned}
v &: A & m &: D\,A\,w_1 & \forall(x : A).\, f\,x &: D\,B\,(w_2\,x) \\
\operatorname{ret}^{D}\,v &: D\,A\,(\operatorname{ret}^{W}\,v) & \operatorname{bind}^{D}\,m\,f &: D\,B\,(\operatorname{bind}^{W}\,w_1\,w_2)
\end{aligned} v ret D v : A : D A ( ret W v ) m bind D m f : D A w 1 : D B ( bind W w 1 w 2 ) ∀ ( x : A ) . f x : D B ( w 2 x )
m : D A w w ⊑ A w ′ m : D\,A\,w \qquad w \sqsubseteq_A w' m : D A w w ⊑ A w ′
weaken m : D A w ′ \operatorname{weaken}\,m : D\,A\,w' weaken m : D A w ′
The comparison rule for bind at the start of this subsection is exactly this typing rule for bind, with D A w D\,A\,w D A w being the computations m m m such that θ ( m ) ⊑ w \theta(m) \sqsubseteq w θ ( m ) ⊑ w . For specifications that under-approximate, the computations with w ⊑ θ ( m ) w \sqsubseteq\theta(m) w ⊑ θ ( m ) form a Dijkstra monad for the reversed order, for which bind is monotone as well. We construct Dijkstra monads indexed with specifications from W P S t \mathrm{WP}^{\mathrm{St}} WP St , through each of the four w p wp w p monad morphisms, and from S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St and S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St , which are specification monads, but not for S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St and S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St . We construct them from the monad morphisms from the computation monads into these specification monads. As shown in [26 ] , such monad morphisms naturally give rise to Dijkstra monads. A key benefit of Dijkstra monads is that they only contain computations that satisfy their specification, which was the foundation of verification frameworks like F⋆ ^\star ⋆ .
As demonstrated above, triples for all the w p wp w p monads, over-approximate S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St triples, and under-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St triples can be represented by predicate transformers. The corresponding Dijkstra monads can therefore contain exactly the computations that satisfy those triples. Additionally, we show that all of the examples that we verified using the corresponding triple can also be proved to be elements within the corresponding Dijkstra monad. For under-approximate S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St and over-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St , one can still specify programs by the predicate transformer, but there does not exist an embedding from the triple into a predicate transformer for an arbitrary triple.
Galois Connections Between Specification Monads
Beyond the taxonomy established for the various monads of w p s wps w p s and s p s sps s p s , one can wonder how expressive these variants are. To that end, Maillard et al. [26 ] show that WP and SPost can be related through Galois connections to two simpler monads PrePost and Pred, where WPo p ^{\mathrm{op}} op indicates that the order is flipped.
S P o s t ⇌ ⊥ ⊤ P r e d ⇌ ⊥ ⊤ P r e P o s t ⇌ ⊥ ⊤ W P o p \mathrm{SPost} \xrightleftharpoons[\perp]{\top} \mathrm{Pred} \xrightleftharpoons[\perp]{\top} \mathrm{PrePost} \xrightleftharpoons[\perp]{\top} \mathrm{WP}^{\mathrm{op}} SPost ⊤ ⊥ Pred ⊤ ⊥ PrePost ⊤ ⊥ WP op
A Galois connection f ⊣ g f \dashv g f ⊣ g between two specification monads W 1 W_1 W 1 and W 2 W_2 W 2 consists of monotone natural transformations f A : W 1 ( A ) → W 2 ( A ) f_A : W_1(A) \to W_2(A) f A : W 1 ( A ) → W 2 ( A ) and g A : W 2 ( A ) → W 1 ( A ) g_A : W_2(A) \to W_1(A) g A : W 2 ( A ) → W 1 ( A ) , such that f A w 1 ⊑ W 2 w 2 f_A\,w_1 \sqsubseteq_{W_2} w_2 f A w 1 ⊑ W 2 w 2 iff w 1 ⊑ W 1 g A w 2 w_1 \sqsubseteq_{W_1} g_A\,w_2 w 1 ⊑ W 1 g A w 2 for any w 1 : W 1 ( A ) w_1 : W_1(A) w 1 : W 1 ( A ) , w 2 : W 2 ( A ) w_2 : W_2(A) w 2 : W 2 ( A ) and A A A . PrePost consists of pairs of a precondition and a postcondition, while Pred only retains the postcondition. Maillard [25 ] further showed that in the pure case, the Galois connection between SPost and Pred on the left establishes an isomorphism of specification monads.
In the rest of this section, we extend this analysis in terms of Galois connections to the stateful case. We consider submonads of monotone, stateful backward predicate transformers W P S t \mathrm{WP}^{\mathrm{St}} WP St and monotone, stateful forward predicate transformers S P S t \mathrm{SP}^{\mathrm{St}} SP St described by healthiness properties. While the angelic (ang-st) and demonic (dem-st) operations on S P S t \mathrm{SP}^{\mathrm{St}} SP St do not satisfy all monad laws, it is still meaningful to ask when these operations are preserved by a natural transformation from or to other monads.2 Specifically, we compare it with P r e d S t ( S , A ) : = S → A × S → P \mathrm{Pred}^{\mathrm{St}}(S,A) := S \to A \times S \to\mathbb{P} Pred St ( S , A ) := S → A × S → P ordered with pointwise logical implication and endowed with the monadic operations:
ret P r e d S t a : = λ ( s 0 : S ) ( r : A × S ) . r = ( a , s 0 ) bind P r e d S t m f : = λ ( s 0 : S ) ( r : B × S ) . ∃ ( a : A ) ( s i : S ) . m s 0 ( a , s i ) ∧ f a s i r (8) \begin{aligned}
\operatorname{ret}_{\mathrm{PredSt}}\ a &:= \lambda(s_0 : S)(r : A \times S).\ r = (a,s_0) \\
\operatorname{bind}_{\mathrm{PredSt}}\ m\ f &:= \lambda(s_0 : S)(r : B \times S).\ \exists(a : A)(s_i : S).\ m\ s_0\ (a,s_i) \land f\ a\ s_i\ r \tag*{(8)}
\end{aligned} ret PredSt a bind PredSt m f := λ ( s 0 : S ) ( r : A × S ) . r = ( a , s 0 ) := λ ( s 0 : S ) ( r : B × S ) . ∃ ( a : A ) ( s i : S ) . m s 0 ( a , s i ) ∧ f a s i r (8)
ret P r e d S t a \operatorname{ret}_{\mathrm{PredSt}}\ a ret PredSt a asserts that the returned value is the given a : A a : A a : A and the state is left unchanged, while bind P r e d S t m f \operatorname{bind}_{\mathrm{PredSt}}\ m\ f bind PredSt m f for m : P r e d S t ( S , A ) m : \mathrm{Pred}^{\mathrm{St}}(S,A) m : Pred St ( S , A ) and f : A → P r e d S t ( S , B ) f : A \to\mathrm{Pred}^{\mathrm{St}}(S,B) f : A → Pred St ( S , B ) existentially quantifies over an intermediate result value a a a and state s i s_i s i , satisfying the postcondition m m m and passed to the continuation f f f .
Recall from §4.2 that L \mathrm{L} L relates s p s sps s p s that are left adjoint to w p s wps w p s . We now show that any predicate p : P r e d S t ( S , A ) p : \mathrm{Pred}^{\mathrm{St}}(S,A) p : Pred St ( S , A ) induces such a pair L A ( φ p ) ( ψ p ) \mathrm{L}_{A}\ (\varphi\ p)\ (\psi\ p) L A ( φ p ) ( ψ p ) where φ : P r e d S t ( S , A ) → S P S t ( S , A ) \varphi: \mathrm{Pred}^{\mathrm{St}}(S,A) \to\mathrm{SP}^{\mathrm{St}}(S,A) φ : Pred St ( S , A ) → SP St ( S , A ) and ψ : P r e d S t ( S , A ) → W P S t O p ( S , A ) \psi: \mathrm{Pred}^{\mathrm{St}}(S,A) \to\mathrm{WP}^{\mathrm{StOp}}(S,A) ψ : Pred St ( S , A ) → WP StOp ( S , A ) are stateful variants of the maps displayed at (†), defined as
φ p : = λ ( p r e : S → P ) ( r : A × S ) . ∃ s 0 : S . p r e s 0 ∧ p s 0 r \varphi\ p := \lambda(pre : S \to\mathbb{P})(r : A \times S).\ \exists s_0 : S.\ pre\ s_0 \land p\ s_0\ r φ p := λ ( p r e : S → P ) ( r : A × S ) . ∃ s 0 : S . p r e s 0 ∧ p s 0 r
ψ p : = λ ( p o s t : A × S → P ) ( s 0 : S ) . ∀ r : A × S . p s 0 r ⇒ p o s t r . \psi\ p := \lambda(post : A \times S \to\mathbb{P})(s_0 : S).\ \forall r : A \times S.\ p\ s_0\ r \Rightarrow post\ r. ψ p := λ ( p os t : A × S → P ) ( s 0 : S ) . ∀ r : A × S . p s 0 r ⇒ p os t r .
Equipping S P S t \mathrm{SP}^{\mathrm{St}} SP St with the angelic monadic structure (ang-st), φ \varphi φ and ψ \psi ψ are both monotone monad morphisms.
Returning to L \mathrm{L} L , given p : P r e d S t ( S , A ) p : \mathrm{Pred}^{\mathrm{St}}(S,A) p : Pred St ( S , A ) , p r e : S → P pre : S \to\mathbb{P} p r e : S → P and p o s t : A × S → P post : A \times S \to\mathbb{P} p os t : A × S → P , we have that
φ p p r e ⊑ p o s t ⟺ ( ∀ s 0 r . p r e s 0 ∧ p s 0 r ⇒ p o s t r ) ⟺ ( ∀ s 0 r . p r e s 0 ⇒ p s 0 r ⇒ p o s t r ) ⟺ ( ∀ s 0 . p r e s 0 ⇒ ∀ r . p s 0 r ⇒ p o s t r ) ⟺ p r e ⊑ ψ p p o s t , \begin{aligned}
\varphi\ p\ pre \sqsubseteq post
&\Longleftrightarrow(\forall s_0\ r.\ pre\ s_0 \land p\ s_0\ r \Rightarrow post\ r) \\
&\Longleftrightarrow(\forall s_0\ r.\ pre\ s_0 \Rightarrow p\ s_0\ r \Rightarrow post\ r) \\
&\Longleftrightarrow(\forall s_0.\ pre\ s_0 \Rightarrow\forall r.\ p\ s_0\ r \Rightarrow post\ r) \\
&\Longleftrightarrow pre \sqsubseteq\psi\ p\ post,
\end{aligned} φ p p r e ⊑ p os t ⟺ ( ∀ s 0 r . p r e s 0 ∧ p s 0 r ⇒ p os t r ) ⟺ ( ∀ s 0 r . p r e s 0 ⇒ p s 0 r ⇒ p os t r ) ⟺ ( ∀ s 0 . p r e s 0 ⇒ ∀ r . p s 0 r ⇒ p os t r ) ⟺ p r e ⊑ ψ p p os t ,
which establishes L A ( φ p ) ( ψ p ) \mathrm{L}_{A}\ (\varphi\ p)\ (\psi\ p) L A ( φ p ) ( ψ p ) . This observation yields a characterization of the relation L \mathrm{L} L in the following sense.
Lemma 3. For s p : S P S t ( S , A ) sp : \mathrm{SP}^{\mathrm{St}}(S,A) s p : SP St ( S , A ) and w p : W P S t O p ( S , A ) wp : \mathrm{WP}^{\mathrm{StOp}}(S,A) w p : WP StOp ( S , A ) , L A s p w p \mathrm{L}_{A}\ sp\ wp L A s p w p holds if and only if there exists p : P r e d S t ( S , A ) p : \mathrm{Pred}^{\mathrm{St}}(S,A) p : Pred St ( S , A ) , such that s p = φ p sp = \varphi\ p s p = φ p and w p = ψ p wp = \psi\ p w p = ψ p .
Intuitively, this characterization of L \mathrm{L} L holds because we can reconstruct a predicate out of a disjunctive and strict s p sp s p or a conjunctive and terminating w p wp w p . Indeed, paralleling the pure case (†), φ \varphi φ and ψ \psi ψ both have right adjoints φ ′ : \varphi' : φ ′ :
S P S t ( S , A ) → P r e d S t ( S , A ) and ψ ′ : W P S t o p ( S , A ) → P r e d S t ( S , A ) given by \mathrm{SP}^{\mathrm{St}}(S,A) \to\mathrm{Pred}^{\mathrm{St}}(S,A) \text{ and } \psi' : \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}}(S,A) \to\mathrm{Pred}^{\mathrm{St}}(S,A) \text{ given by} SP St ( S , A ) → Pred St ( S , A ) and ψ ′ : WP St op ( S , A ) → Pred St ( S , A ) given by
φ ′ s p : = λ ( s 0 : S ) ( r : A × S ) . s p ( λ s . s = s 0 ) r \varphi'\,sp := \lambda(s_{0}:S)(r:A \times S).\,sp\,(\lambda s.\,s=s_{0})\,r φ ′ s p := λ ( s 0 : S ) ( r : A × S ) . s p ( λ s . s = s 0 ) r
ψ ′ w p : = λ ( s 0 : S ) ( r : A × S ) . ∀ ( p : A × S → P ) . w p p s 0 ⇒ p r . \psi'\,wp := \lambda(s_{0}:S)(r:A \times S).\,\forall(p:A \times S \to\mathbb{P}).\,wp\,p\,s_{0} \Rightarrow p\,r. ψ ′ w p := λ ( s 0 : S ) ( r : A × S ) . ∀ ( p : A × S → P ) . w p p s 0 ⇒ p r .
φ ′ \varphi' φ ′ preserves the demonic monadic structure on S P S t \mathrm{SP}^{\mathrm{St}} SP St while ψ ′ \psi' ψ ′ does not preserve W P S t o p \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}} WP St op ’s bind. We observed that L L L restricted to a relation between S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St and W P D W L P S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{DWLP}} WP DWLP St . We can further this remark by inspecting the nuclei of the adjunctions φ ⊣ φ ′ \varphi\dashv\varphi' φ ⊣ φ ′ and ψ ⊣ ψ ′ \psi\dashv\psi' ψ ⊣ ψ ′ : (1) every p : P r e d S t ( S , A ) p : \mathrm{Pred}^{\mathrm{St}}(S,A) p : Pred St ( S , A ) is a fix point of both φ ′ ∘ φ \varphi' \circ\varphi φ ′ ∘ φ and ψ ′ ∘ ψ \psi' \circ\psi ψ ′ ∘ ψ , (2) the fix points of φ ∘ φ ′ \varphi\circ\varphi' φ ∘ φ ′ are exactly elements of S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St , and (3) the fix points of ψ ∘ ψ ′ \psi\circ\psi' ψ ∘ ψ ′ are exactly elements of W P D W L P S t o p \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}}_{\mathrm{DWLP}} WP DWLP St op . As a result, we obtain the characterization of L L L and a chain of correspondences
S P A S P S t ≅ P r e d S t ≅ W P D W L P S t o p . \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} \cong\mathrm{Pred}^{\mathrm{St}} \cong\mathrm{WP}^{\mathrm{St}}_{\mathrm{DWLP}}{}^{\mathrm{op}}. SP ASP St ≅ Pred St ≅ WP DWLP St op .
We can now turn to the case of R R R , where a similar story unfolds with respect to the specification monad P r e d S t o p \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}} Pred St op , with the same carrier and monadic structure as P r e d S t \mathrm{Pred}^{\mathrm{St}} Pred St but with order reversed. Consider the maps ξ : P r e d S t o p ( S , A ) → S P S t ( S , A ) \xi: \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) \to\mathrm{SP}^{\mathrm{St}}(S,A) ξ : Pred St op ( S , A ) → SP St ( S , A ) , ζ : P r e d S t o p ( S , A ) → W P S t o p ( S , A ) \zeta: \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) \to\mathrm{WP}^{\mathrm{St}^{\mathrm{op}}}(S,A) ζ : Pred St op ( S , A ) → WP St op ( S , A ) , ξ ′ : S P S t ( S , A ) → P r e d S t o p ( S , A ) \xi' : \mathrm{SP}^{\mathrm{St}}(S,A) \to\mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) ξ ′ : SP St ( S , A ) → Pred St op ( S , A ) , and ζ ′ : W P S t o p ( S , A ) → P r e d S t o p ( S , A ) \zeta' : \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}}(S,A) \to\mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) ζ ′ : WP St op ( S , A ) → Pred St op ( S , A ) , defined as
ξ p : = λ ( p r e : S → P ) ( r : A × S ) . ∀ s 0 : S . p s 0 r ⇒ p r e s 0 \xi\,p := \lambda(pre:S \to\mathbb{P})(r:A \times S).\,\forall s_{0}:S.\,p\,s_{0}\,r \Rightarrow pre\,s_{0} ξ p := λ ( p r e : S → P ) ( r : A × S ) . ∀ s 0 : S . p s 0 r ⇒ p r e s 0
ζ p : = λ ( p o s t : A × S → P ) ( s 0 : S ) . ∃ r : A × S . p s 0 r ∧ p o s t r \zeta\,p := \lambda(post:A \times S \to\mathbb{P})(s_{0}:S).\,\exists r:A \times S.\,p\,s_{0}\,r \land post\,r ζ p := λ ( p os t : A × S → P ) ( s 0 : S ) . ∃ r : A × S . p s 0 r ∧ p os t r
ξ ′ s p : = λ ( p r e : S → P ) ( r : A × S ) . ∀ ( p r e : S → P ) . s p p r e r ⇒ p r e s 0 \xi'\,sp := \lambda(pre:S \to\mathbb{P})(r:A \times S).\,\forall(pre:S \to\mathbb{P}).\,sp\,pre\,r \Rightarrow pre\,s_{0} ξ ′ s p := λ ( p r e : S → P ) ( r : A × S ) . ∀ ( p r e : S → P ) . s p p r e r ⇒ p r e s 0
ζ ′ w p : = λ ( s 0 : S ) ( r : A × S ) . w p ( λ ( r ′ : A × S ) . r = r ′ ) s 0 . \zeta'\,wp := \lambda(s_{0}:S)(r:A \times S).\,wp\,(\lambda(r':A \times S).\,r=r')\,s_{0}. ζ ′ w p := λ ( s 0 : S ) ( r : A × S ) . w p ( λ ( r ′ : A × S ) . r = r ′ ) s 0 .
We can show that all maps are monotone, natural in A A A , and the corresponding pairs form Galois connections: ξ ′ ⊣ ξ \xi' \dashv\xi ξ ′ ⊣ ξ and ζ ′ ⊣ ζ \zeta' \dashv\zeta ζ ′ ⊣ ζ . Moreover, ξ \xi ξ , ζ \zeta ζ and ξ ′ \xi' ξ ′ are monad morphisms if we equip S P S t \mathrm{SP}^{\mathrm{St}} SP St with the demonic monadic operations (dem-st), while ζ ′ \zeta' ζ ′ fails to preserve W P S t o p \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}} WP St op ’s bind.
For any p : P r e d S t o p ( S , A ) p : \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) p : Pred St op ( S , A ) , we have that R A ( ξ p ) ( ζ p ) R_{A}(\xi\,p)(\zeta\,p) R A ( ξ p ) ( ζ p ) . Indeed, given p r e : S → P pre : S \to\mathbb{P} p r e : S → P and p o s t : A × S → P post : A \times S \to\mathbb{P} p os t : A × S → P ,
ζ p p o s t ⊑ p r e ⟺ ∀ s 0 : S . ( ∃ r : A × S . p s 0 r ∧ p o s t r ) ⇒ p r e s 0 \zeta\,p\,post \sqsubseteq pre \Longleftrightarrow\forall s_{0}:S.\,(\exists r:A \times S.\,p\,s_{0}\,r \land post\,r) \Rightarrow pre\,s_{0} ζ p p os t ⊑ p r e ⟺ ∀ s 0 : S . ( ∃ r : A × S . p s 0 r ∧ p os t r ) ⇒ p r e s 0
⟺ ∀ ( s 0 : S ) ( r : A × S ) . p o s t r ⇒ p s 0 r ⇒ p r e s 0 \Longleftrightarrow\forall(s_{0}:S)(r:A \times S).\,post\,r \Rightarrow p\,s_{0}\,r \Rightarrow pre\,s_{0} ⟺ ∀ ( s 0 : S ) ( r : A × S ) . p os t r ⇒ p s 0 r ⇒ p r e s 0
⟺ ∀ r : A × S . p o s t r ⇒ ∀ s 0 : S . p s 0 r ⇒ p r e s 0 \Longleftrightarrow\forall r:A \times S.\,post\,r \Rightarrow\forall s_{0}:S.\,p\,s_{0}\,r \Rightarrow pre\,s_{0} ⟺ ∀ r : A × S . p os t r ⇒ ∀ s 0 : S . p s 0 r ⇒ p r e s 0
⟺ p o s t ⊑ ξ p p r e . \Longleftrightarrow post \sqsubseteq\xi\,p\,pre. ⟺ p os t ⊑ ξ p p r e .
We also obtain a characterization of R R R in terms of P r e d S t o p \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}} Pred St op .
Lemma 4. For s p : S P S t ( S , A ) sp : \mathrm{SP}^{\mathrm{St}}(S,A) s p : SP St ( S , A ) and w p : W P S t o p ( S , A ) wp : \mathrm{WP}^{\mathrm{St}^{\mathrm{op}}}(S,A) w p : WP St op ( S , A ) , R A s p w p R_{A}\,sp\,wp R A s p w p holds if and only if there exists p : P r e d S t o p ( S , A ) p : \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) p : Pred St op ( S , A ) , such that s p = ξ p sp=\xi\,p s p = ξ p and w p = ζ p wp=\zeta\,p w p = ζ p .
Again, we can investigate the nuclei of the adjunctions ξ ′ ⊣ ξ \xi' \dashv\xi ξ ′ ⊣ ξ and ζ ′ ⊣ ζ \zeta' \dashv\zeta ζ ′ ⊣ ζ : (1) every p : P r e d S t o p ( S , A ) p : \mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}}(S,A) p : Pred St op ( S , A ) is a fix point of both ξ ′ ∘ ξ \xi' \circ\xi ξ ′ ∘ ξ and ζ ′ ∘ ζ \zeta' \circ\zeta ζ ′ ∘ ζ , (2) the fix points of ξ ∘ ξ ′ \xi\circ\xi' ξ ∘ ξ ′ are exactly elements of S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St , and (3) the fix points of ζ ∘ ζ ′ \zeta\circ\zeta' ζ ∘ ζ ′ are exactly elements of W P A W P o p S t \mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}^{\mathrm{op}}} WP AWP op St . As a result, we get another chain of correspondences:
S P D S L P S t ≅ P r e d S t o p ≅ W P A W P S t o p \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} \cong\mathrm{Pred}^{\mathrm{St}^{\mathrm{op}}} \cong\mathrm{WP}^{\mathrm{St}}_{\mathrm{AWP}}{}^{\mathrm{op}} SP DSLP St ≅ Pred St op ≅ WP AWP St op
Formalization in Rocq
All definitions and results presented in this paper are formalized in Rocq (about 28k lines), assuming the axioms of functional extensionality, propositional extensionality, and excluded middle. We use the triples of § 2.3 to verify about 30 small example programs, including all the sequential examples of O’Hearn [30 ] . These examples cover state, state and exceptions, recursion, and loops and together use all sixteen kinds of triples. The programs are written as interaction trees [38 ] , and a monad morphism maps them to the computation monad of § 2.3 .
Related Work
Hoare-like logics. Incorrectness logic was first proposed by de Vries and Koutavas [9 ] under the name reverse Hoare logic. The term “incorrectness logic” was coined by O’Hearn [30 ] later. Since then, many variants of it have been studied, including partial incorrectness logic [39 ] , underspecified incorrectness logic [36 ] , sufficient incorrectness logic (a.k.a. Lisbon logic) [5 , 6 ] , etc. Verscht and Kaminski [37 ] taxonomize Hoare-like triples in an imperative setting and inspired our work. Starting from the classical strongest postcondition and the strongest liberal postcondition of Zhang and Kaminski [39 ] , which they call a s p asp a s p and d s l p dslp d s l p , they complete the picture with two novel variants, d s p dsp d s p and a s l p aslp a s l p . They also observe that a s l p aslp a s l p and d s p dsp d s p cannot be defined by induction on the program structure and propose that they can be represented using disjunctions and conjunctions of a s p asp a s p and d s l p dslp d s l p , respectively [37 §3.2.4] . We extend their work to monadic functional programs with different combinations of effects, and construct a predicate transformer monad for each of the four s p sp s p variants.
Outcome logics. Outcome logic [40 ] unifies correctness and incorrectness reasoning in a single program logic, whose semantics is expressed in a monad and whose assertions describe the collection of outcomes that a program produces. It expresses the d w l p dwlp d w l p and a w p awp a w p triples but not the d w p dwp d w p and a w l p awlp a w l p triples, which need nontermination as an outcome [23 ] . Total outcome logic [23 ] adds this outcome and expresses every logic of the taxonomy of Verscht and Kaminski [37 ] , and logics in between, with one proof system. It expresses the d s p dsp d s p and a s l p aslp a s l p logics as combinations of two triples, without defining the two transformers directly; here, they are elements of their own monads, S P D S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}} SP DSP St and S P A S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}} SP ASLP St . Both logics use a monad to give imperative programs their semantics, and one logic to state all the triples. We instead make the predicate transformers themselves monads. Each triple is read off a monad morphism from the computation monad, the relations of the taxonomy become relations between these monads, and monad transformers extend these monads to state and exceptions. Unlike our work, outcome logics also cover probabilistic choice.
Predicate transformers forming monads. Jacobs [18 ] studies predicate transformers as a monad, with w p wp w p as a monad morphism into it, Hasuo [15 ] derives w p s wps w p s generically from monads enriched with an order, and Aguirre et al. [1 ] develop w p s wps w p s in fibrations, where s p sp s p arises as the left adjoint of w p wp w p , as in our relation L (§ 2.4 ). Instead of deriving predicate transformers from the computation monad, we restrict the carriers of our monads to combinations of the homomorphism properties of Back and von Wright [8 ] .
Dijkstra monads and specification monads. Swamy et al. [33 ] introduced Dijkstra monads in the context of F ⋆ F^\star F ⋆ for reasoning intrinsically about stateful programs using backward predicate transformers represented as elements of a continuation monad to stateful predicates. The Dijkstra monad methodology was then extended to a variety of effects [2 , 26 ] . Maillard et al. [26 ] decompose Dijkstra monads as a triple of a computation monad M M M , a specification monad W W W and a monad morphism θ : M → W \theta: M \to W θ : M → W interpreting programs as specifications. While their work mention forward predicate transformers, their strategy to effect modularity using monad transformers does not apply directly to provide an intuitive specification monad for stateful forward predicate transformers as we show in this paper. Using arrow transformers, we obtain predicate transformers of the expected shape.
Verification using monad morphisms. Gladshtein et al. [14 ] build foundational verifiers in Lean based on predicate transformer triples. They derive w p wp w p calculi from ordered monad algebras and lift them through a library of monad transformers, for state, exceptions, divergence with total or partial correctness, and nondeterminism with demonic or angelic choice. Their incorrectness proofs use angelic w p s wps w p s , as in Lisbon logic, and they do not consider s p s sps s p s . Like us, they obtain effect modularity from transformers, but they lift algebras through monad transformers, whereas we lift monad morphisms through monad transformers and arrow morphisms through arrow transformers.
Conclusion and Future Work
This work introduces strongest postcondition monads for all combinations of angelic or demonic nondeterminism and total or liberal interpretations of nontermination, and extends them with state and exceptions by generalizing to arrows and applying arrow transformers. Together with the corresponding weakest precondition monads, these allow us to recast the taxonomy of triples presented by Verscht and Kaminski [37 ] as a taxonomy of predicate transformer monads, both for the base monads and for their versions with state. We demonstrate how they can be used to specify simple monadic programs, with and without state and exceptions, in a Loom-inspired style [14 ] and in the style of Dijkstra monads [26 ] , and relate the two styles. All our results are mechanized in Rocq. Further work is needed to investigate whether these foundational results can underpin a practical verification framework in the style of Loom [14 ] that supports s p s sps s p s , general w p s wps w p s , and both under- and over-approximation, ideally allowing these different kinds of reasoning to be meaningfully combined. To that end, considering further computational effects such as IO and continuations would widen the applicability of our findings. This would benefit from making the taxonomy and the Galois connections of §6 effect-modular, which is not straightforward: some of the maps and carriers involved lack a full monad or arrow structure, so one would need transformers that, like the state transformers, do not depend on the full structure. Finally, this work also reveals unexpected skew monadic structures on monotone forward predicate transformers SP that could be interesting to investigate on their own, inspired by the seminal work of Lack and Street [21 ] on skew monoidales.
Acknowledgments. This work was in part funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) under Germany’s Excellence Strategy – EXC 2092 CASA – 390781972. Exequiel Rivas was funded by the Estonian Research Council starting grant PSG749.
Arrow and Arrow Morphism Laws
All arrow laws regarding arrows with application are shown below:
id ≫ f ≡ f f ≫ id ≡ f f ≫ g ≫ h ≡ f ≫ ( g ≫ h ) arr f ≫ arr g ≡ arr ( g ⋅ f ) first ( arr f ) ≡ arr ( λ ( a , c ) . ( f a , c ) ) first ( f ≫ g ) ≡ first f ≫ first g first f ≫ arr fst ≡ arr fst ≫ f first ( first f ) ≫ assoc ≡ assoc ≫ first f first f ≫ arr ( λ ( b , c ) . ( b , g c ) ) ≡ arr ( λ ( a , c ) . ( a , g c ) ) ≫ first f arr ( λ a . ( f , a ) ) ≫ app ≡ f first ( arr ( λ f . g ≫ f ) ) ≫ app ≡ second g ≫ app first ( arr ( λ f . f ≫ h ) ) ≫ app ≡ app ≫ h \begin{aligned}
\operatorname{id} \gg f &\equiv f \\
f \gg\operatorname{id} &\equiv f \\
f \gg g \gg h &\equiv f \gg(g \gg h) \\
\operatorname{arr}\ f \gg\operatorname{arr}\ g &\equiv\operatorname{arr}(g \cdot f) \\
\operatorname{first}(\operatorname{arr}\ f) &\equiv\operatorname{arr}(\lambda(a,c).(f\ a,c)) \\
\operatorname{first}(f \gg g) &\equiv\operatorname{first}\ f \gg\operatorname{first}\ g \\
\operatorname{first}\ f \gg\operatorname{arr}\ \operatorname{fst} &\equiv\operatorname{arr}\ \operatorname{fst} \gg f \\
\operatorname{first}(\operatorname{first}\ f) \gg\operatorname{assoc} &\equiv\operatorname{assoc} \gg\operatorname{first}\ f \\
\operatorname{first}\ f \gg\operatorname{arr}(\lambda(b,c).(b,g\ c)) &\equiv\operatorname{arr}(\lambda(a,c).(a,g\ c)) \gg\operatorname{first}\ f \\
\operatorname{arr}(\lambda a.(f,a)) \gg\operatorname{app} &\equiv f \\
\operatorname{first}(\operatorname{arr}(\lambda f.g \gg f)) \gg\operatorname{app} &\equiv\operatorname{second}\ g \gg\operatorname{app} \\
\operatorname{first}(\operatorname{arr}(\lambda f.f \gg h)) \gg\operatorname{app} &\equiv\operatorname{app} \gg h
\end{aligned} id ≫ f f ≫ id f ≫ g ≫ h arr f ≫ arr g first ( arr f ) first ( f ≫ g ) first f ≫ arr fst first ( first f ) ≫ assoc first f ≫ arr ( λ ( b , c ) . ( b , g c )) arr ( λa . ( f , a )) ≫ app first ( arr ( λ f . g ≫ f )) ≫ app first ( arr ( λ f . f ≫ h )) ≫ app ≡ f ≡ f ≡ f ≫ ( g ≫ h ) ≡ arr ( g ⋅ f ) ≡ arr ( λ ( a , c ) . ( f a , c )) ≡ first f ≫ first g ≡ arr fst ≫ f ≡ assoc ≫ first f ≡ arr ( λ ( a , c ) . ( a , g c )) ≫ first f ≡ f ≡ second g ≫ app ≡ app ≫ h
We use ≡ \equiv ≡ to represent the equality defined on the arrow, which is defined as logical equivalence on s p s sps s p s arrows. All these laws are defined by Hughes [17 ] . We also state laws of arrow transformers defined by Hughes and prove that state and option arrow transformers satisfy these laws.
In addition, we show that all our s p s sps s p s arrows are choice arrows, and they satisfy relevant laws defined by Hughes and the additional choice arrow distribution law defined by Paterson [31 ] .
An arrow morphism θ \theta θ from an arrow A to an arrow B, both with choice and application, is a family θ X , Y : A ( X , Y ) → B ( X , Y ) \theta_{X,Y}: A(X,Y) \to B(X,Y) θ X , Y : A ( X , Y ) → B ( X , Y ) that respects ≡ \equiv ≡ , so that $f \equiv g implies θ ( f ) ≡ θ ( g ) \theta(f) \equiv\theta(g) θ ( f ) ≡ θ ( g ) , and satisfies the following laws:
θ ( arr A h ) ≡ arr B h θ ( f ⋙ A g ) ≡ θ ( f ) ⋙ B θ ( g ) θ ( first A f ) ≡ first B θ ( f ) θ ( left A f ) ≡ left B θ ( f ) θ ( app A ) ≡ arr B ( λ ( f , a ) . ( θ ( f ) , a ) ) ⋙ B app B \begin{aligned}
\theta(\operatorname{arr}_{A} h) &\equiv\operatorname{arr}_{B} h \\
\theta(f \mathbin{\ggg_{A}} g) &\equiv\theta(f) \mathbin{\ggg_{B}} \theta(g) \\
\theta(\operatorname{first}_{A} f) &\equiv\operatorname{first}_{B} \theta(f) \\
\theta(\operatorname{left}_{A} f) &\equiv\operatorname{left}_{B} \theta(f) \\
\theta(\operatorname{app}_{A}) &\equiv\operatorname{arr}_{B}(\lambda(f,a).(\theta(f),a)) \mathbin{\ggg_{B}} \operatorname{app}_{B}
\end{aligned} θ ( arr A h ) θ ( f ⋙ A g ) θ ( first A f ) θ ( left A f ) θ ( app A ) ≡ arr B h ≡ θ ( f ) ⋙ B θ ( g ) ≡ first B θ ( f ) ≡ left B θ ( f ) ≡ arr B ( λ ( f , a ) . ( θ ( f ) , a )) ⋙ B app B
The first three are the laws of Atkey [7, Def. 4.4], and the last two preserve choice and application. Since app A \operatorname{app}_{A} app A takes an arrow of A as input and app B \operatorname{app}_{B} app B an arrow of B, the law for app first maps the input arrow with θ \theta θ . We use it, together with the laws of B, to show that an arrow morphism is, at each fixed input, a monad morphism between the monads of the two arrows.
B The ArrDSP and ArrASLP Arrows
For Arr D S P ( B , A ) \operatorname{Arr}_{\mathrm{DSP}}(B,A) Arr DSP ( B , A ) , we restrict the arrow carrier to conjunctive, strict s p a r r sp_{\mathrm{arr}} s p arr , mirroring the carrier restriction of the SP D S P ( A ) \operatorname{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) monad. For every non-empty family of preconditions ( p i ) i ∈ I (p_i)_{i \in I} ( p i ) i ∈ I on the input B:
s p a r r ( ⋀ i ∈ I p i ) = ⋀ i ∈ I s p a r r ( p i ) and s p a r r ( ⊥ ) = ⊥ . sp_{\mathrm{arr}}\left(\bigwedge_{i \in I} p_i\right) = \bigwedge_{i \in I} sp_{\mathrm{arr}}(p_i)
\qquad\text{and}\qquad
sp_{\mathrm{arr}}(\bot) = \bot. s p arr ( i ∈ I ⋀ p i ) = i ∈ I ⋀ s p arr ( p i ) and s p arr ( ⊥ ) = ⊥.
As for the monad, a d s p dsp d s p arrow splits into an a s p asp a s p and a d s l p dslp d s l p component, where s p ( λ _ . ⊤ ) a sp\;(\lambda\_.\top)\;a s p ( λ _.⊤ ) a states that a a a is reachable:
aspOf : Arr D S P ( B , A ) → Arr A S P ( B , A ) \operatorname{aspOf} : \operatorname{Arr}_{\mathrm{DSP}}(B,A) \to\operatorname{Arr}_{\mathrm{ASP}}(B,A) aspOf : Arr DSP ( B , A ) → Arr ASP ( B , A )
aspOf s p : = λ p r e a . s p ( λ _ . ⊤ ) a ∧ ¬ s p ( λ b . ¬ p r e b ) a , \operatorname{aspOf}\;sp := \lambda pre\;a.\;sp\;(\lambda\_.\top)\;a \land\neg sp\;(\lambda b.\neg pre\;b)\;a, aspOf s p := λ p r e a . s p ( λ _.⊤ ) a ∧ ¬ s p ( λb .¬ p r e b ) a ,
dslpOf : Arr D S P ( B , A ) → Arr D S L P ( B , A ) \operatorname{dslpOf} : \operatorname{Arr}_{\mathrm{DSP}}(B,A) \to\operatorname{Arr}_{\mathrm{DSLP}}(B,A) dslpOf : Arr DSP ( B , A ) → Arr DSLP ( B , A )
dslpOf s p : = λ p r e a . s p p r e a ∨ ¬ s p ( λ _ . ⊤ ) a \operatorname{dslpOf}\;sp := \lambda pre\;a.\;sp\;pre\;a \lor\neg sp\;(\lambda\_.\top)\;a dslpOf s p := λ p r e a . s p p r e a ∨ ¬ s p ( λ _.⊤ ) a
Every s p sp s p admits the same outputs as aspOf s p ∧ dslpOf s p \operatorname{aspOf}\;sp \land\operatorname{dslpOf}\;sp aspOf s p ∧ dslpOf s p . Each arrow operation applies the Arr A S P \operatorname{Arr}_{\mathrm{ASP}} Arr ASP operation to the a s p asp a s p components and the Arr D S L P \operatorname{Arr}_{\mathrm{DSLP}} Arr DSLP operation to the d s l p dslp d s l p components, and conjoins the results:
arr f : ( B → A ) → Arr D S P ( B , A ) \operatorname{arr}\;f : (B \to A) \to\operatorname{Arr}_{\mathrm{DSP}}(B,A) arr f : ( B → A ) → Arr DSP ( B , A )
: = λ p r e a . arr A S P f p r e a ∧ arr D S L P f p r e a , := \lambda pre\;a.\;\operatorname{arr}_{\mathrm{ASP}}\;f\;pre\;a \land\operatorname{arr}_{\mathrm{DSLP}}\;f\;pre\;a, := λ p r e a . arr ASP f p r e a ∧ arr DSLP f p r e a ,
f ⋙ g : Arr D S P ( B , A ) → Arr D S P ( A , C ) → Arr D S P ( B , C ) f \mathbin{\ggg} g : \operatorname{Arr}_{\mathrm{DSP}}(B,A) \to\operatorname{Arr}_{\mathrm{DSP}}(A,C) \to\operatorname{Arr}_{\mathrm{DSP}}(B,C) f ⋙ g : Arr DSP ( B , A ) → Arr DSP ( A , C ) → Arr DSP ( B , C )
: = λ p r e c . ( aspOf f ⋙ A S P aspOf g ) p r e c ∧ ( dslpOf f ⋙ D S L P dslpOf g ) p r e c , := \lambda pre\;c.\;(\operatorname{aspOf}\;f \mathbin{\ggg_{\mathrm{ASP}}} \operatorname{aspOf}\;g)\;pre\;c
\land(\operatorname{dslpOf}\;f \mathbin{\ggg_{\mathrm{DSLP}}} \operatorname{dslpOf}\;g)\;pre\;c, := λ p r e c . ( aspOf f ⋙ ASP aspOf g ) p r e c ∧ ( dslpOf f ⋙ DSLP dslpOf g ) p r e c ,
first f : Arr D S P ( B , A ) → Arr D S P ( B × C , A × C ) \operatorname{first}\;f : \operatorname{Arr}_{\mathrm{DSP}}(B,A) \to\operatorname{Arr}_{\mathrm{DSP}}(B \times C,A \times C) first f : Arr DSP ( B , A ) → Arr DSP ( B × C , A × C )
: = λ p r e q . first A S P ( aspOf f ) p r e q ∧ first D S L P ( dslpOf f ) p r e q , := \lambda pre\;q.\;\operatorname{first}_{\mathrm{ASP}}(\operatorname{aspOf}\;f)\;pre\;q \land\operatorname{first}_{\mathrm{DSLP}}(\operatorname{dslpOf}\;f)\;pre\;q, := λ p r e q . first ASP ( aspOf f ) p r e q ∧ first DSLP ( dslpOf f ) p r e q ,
app : Arr D S P ( Arr D S P ( B , A ) × B , A ) \operatorname{app} : \operatorname{Arr}_{\mathrm{DSP}}(\operatorname{Arr}_{\mathrm{DSP}}(B,A) \times B,A) app : Arr DSP ( Arr DSP ( B , A ) × B , A )
: = λ p r e a . ( ∃ f b . p r e ( f , b ) ∧ aspOf f ( λ x . x = b ) a ) ∧ ( ∀ f b . aspOf f ( λ x . x = b ) a ⇒ p r e ( f , b ) ) := \lambda pre\;a.\;(\exists f\;b.\;pre\;(f,b) \land\operatorname{aspOf}\;f\;(\lambda x.x=b)\;a)
\land(\forall f\;b.\;\operatorname{aspOf}\;f\;(\lambda x.x=b)\;a \Rightarrow pre\;(f,b)) := λ p r e a . ( ∃ f b . p r e ( f , b ) ∧ aspOf f ( λ x . x = b ) a ) ∧ ( ∀ f b . aspOf f ( λ x . x = b ) a ⇒ p r e ( f , b ))
In app, aspOf f ( λ x . x = b ) a \operatorname{aspOf}\;f\;(\lambda x.x=b)\;a aspOf f ( λ x . x = b ) a states that f f f can produce a a a from b b b . Composing the d s p dsp d s p transformers directly, λ p r e c . g ( f p r e ) c \lambda pre\;c.\;g\;(f\;pre)\;c λ p r e c . g ( f p r e ) c , would not work: it breaks the law a r r f ⋙ a r r g ≡ a r r ( g ⋅ f ) arr f \ggg arr g \equiv arr(g \mathbin{\cdot} f) a r r f ⋙ a r r g ≡ a r r ( g ⋅ f ) whenever g g g maps a value produced by f f f and a value never produced by f f f to the same output, as in Figure 2 .
Dually, for Arr A S L P ( B , A ) \operatorname{Arr}_{\mathrm{ASLP}}(B,A) Arr ASLP ( B , A ) , we restrict the arrow carrier to disjunctive, terminating s p a r r sp_{\mathrm{arr}} s p arr , mirroring the carrier restriction of the SP A S L P ( A ) \operatorname{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) monad. For every non-empty family of preconditions ( p i ) i ∈ I (p_i)_{i\in I} ( p i ) i ∈ I on the input B:
s p a r r ( ⋁ i ∈ I p i ) = ⋁ i ∈ I s p a r r ( p i ) and s p a r r ( ⊤ ) = ⊤ . sp_{\mathrm{arr}}\left(\bigvee_{i\in I}p_i\right)=\bigvee_{i\in I}sp_{\mathrm{arr}}(p_i)\qquad\text{and}\qquad sp_{\mathrm{arr}}(\top)=\top. s p arr ( i ∈ I ⋁ p i ) = i ∈ I ⋁ s p arr ( p i ) and s p arr ( ⊤ ) = ⊤.
Here ¬ s p ( λ _ . ⊥ ) a \neg sp\;(\lambda\_.\bot)\;a ¬ s p ( λ _.⊥ ) a states that a a a is reachable, and the components are
aspOf : Arr A S L P ( B , A ) → Arr A S P ( B , A ) aspOf s p : = λ p r e a . s p p r e a ∧ ¬ s p ( λ _ . ⊥ ) a , dslpOf : Arr A S L P ( B , A ) → Arr D S L P ( B , A ) dslpOf s p : = λ p r e a . ¬ s p ( λ b . ¬ p r e b ) a ∨ s p ( λ _ . ⊥ ) a \begin{aligned}
\operatorname{aspOf}&:\operatorname{Arr}_{\mathrm{ASLP}}(B,A)\to\operatorname{Arr}_{\mathrm{ASP}}(B,A)\\
\operatorname{aspOf}\;sp&:=\lambda pre\;a.\;sp\;pre\;a\land\neg sp\;(\lambda\_.\bot)\;a,\\
\operatorname{dslpOf}&:\operatorname{Arr}_{\mathrm{ASLP}}(B,A)\to\operatorname{Arr}_{\mathrm{DSLP}}(B,A)\\
\operatorname{dslpOf}\;sp&:=\lambda pre\;a.\;\neg sp\;(\lambda b.\neg pre\;b)\;a\lor sp\;(\lambda\_.\bot)\;a
\end{aligned} aspOf aspOf s p dslpOf dslpOf s p : Arr ASLP ( B , A ) → Arr ASP ( B , A ) := λ p r e a . s p p r e a ∧ ¬ s p ( λ _.⊥ ) a , : Arr ASLP ( B , A ) → Arr DSLP ( B , A ) := λ p r e a . ¬ s p ( λb .¬ p r e b ) a ∨ s p ( λ _.⊥ ) a
Every s p sp s p admits the same outputs as a s p O f s p ∨ d s l p O f s p aspOf\;sp\lor dslpOf\;sp a s pO f s p ∨ d s l pO f s p , and the arrow operations are those of Arr D S P ( B , A ) \operatorname{Arr}_{\mathrm{DSP}}(B,A) Arr DSP ( B , A ) with ∨ \lor ∨ in place of ∧ \land ∧ .
Both are lawful arrows with application: they satisfy all the laws of Appendix A. Fixing B = 1 B=1 B = 1 gives the ret and bind of SP D S P ( A ) \operatorname{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) and SP A S L P ( A ) \operatorname{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) from §3.
This appendix shows that fixing B = 1 B=1 B = 1 in Arr A S P ( B , A ) \operatorname{Arr}_{\mathrm{ASP}}(B,A) Arr ASP ( B , A ) and Arr D S L P ( B , A ) \operatorname{Arr}_{\mathrm{DSLP}}(B,A) Arr DSLP ( B , A ) gives the carriers and the ret and bind of SP A S P ( A ) \operatorname{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) (ang) and SP D S L P ( A ) \operatorname{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) (dem).
A precondition p r e : 1 → P pre:1\to\mathbb{P} p r e : 1 → P is determined by the proposition p r e ∗ pre\;* p r e ∗ , where ∗ * ∗ is the unique element of 1 1 1 , and a proposition p p p is the precondition λ _ . p \lambda\_.p λ _. p . We identify the two, ( 1 → P ) ≅ P (1\to\mathbb{P})\cong\mathbb{P} ( 1 → P ) ≅ P , and write p r e pre p r e for both. The identification commutes with pointwise disjunctions and conjunctions, so an s p a r r : ( 1 → P ) → ( A → P ) sp_{\mathrm{arr}}:(1\to\mathbb{P})\to(A\to\mathbb{P}) s p arr : ( 1 → P ) → ( A → P ) preserves all disjunctions, respectively all conjunctions, exactly when the corresponding s p : P → ( A → P ) sp:\mathbb{P}\to(A\to\mathbb{P}) s p : P → ( A → P ) does. Hence the carrier of Arr A S P ( 1 , A ) \operatorname{Arr}_{\mathrm{ASP}}(1,A) Arr ASP ( 1 , A ) corresponds to that of SP A S P ( A ) \operatorname{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) , and the carrier of Arr D S L P ( 1 , A ) \operatorname{Arr}_{\mathrm{DSLP}}(1,A) Arr DSLP ( 1 , A ) to that of SP D S L P ( A ) \operatorname{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) , up to logical equivalence, which is the equality of the arrows. All these s p s sps s p s are monotone, so they respect logical equivalence of preconditions, which we use without mention.
Recall the ret and bind derived from an arrow with application (§ 2.1.1 ), where h : = λ ( a , i ) . ( f a , i ) h:=\lambda(a,i).(f\;a,i) h := λ ( a , i ) . ( f a , i ) :
r e t v : = a r r ( λ _ . v ) , ret\;v:=arr(\lambda\_.v), r e t v := a r r ( λ _. v ) ,
b i n d m f : = ( a r r ( λ x . ( x , x ) ) ⋙ f i r s t m ) ⋙ ( a r r h ⋙ a p p ) . bind\;m\;f:=(arr(\lambda x.(x,x))\ggg first\;m)\ggg(arr\;h\ggg app). bin d m f := ( a r r ( λ x . ( x , x )) ⋙ f i r s t m ) ⋙ ( a r r h ⋙ a pp ) .
Since f ⋙ g f\ggg g f ⋙ g passes the postcondition of f f f to g g g as its precondition, b i n d m f p r e = a p p P 3 bind\;m\;f\;pre=app\;P_3 bin d m f p r e = a pp P 3 , where
P 1 : = a r r ( λ x . ( x , x ) ) p r e , P 2 : = f i r s t m P 1 , P 3 : = a r r h P 2 . P_1:=arr(\lambda x.(x,x))\;pre,\qquad P_2:=first\;m\;P_1,\qquad P_3:=arr\;h\;P_2. P 1 := a r r ( λ x . ( x , x )) p r e , P 2 := f i r s t m P 1 , P 3 := a r r h P 2 .
Below, u u u , i i i , j j j , x x x , and y y y range over $1$, a a a over A A A , b b b over B B B , and g g g over the arrows from 1 1 1 to $B.
The Angelic Arrow
Lemma 5. For every s p : S P A S P ( A ) sp : \mathrm{SP}_{\mathrm{ASP}}(A) s p : SP ASP ( A ) , p r e : P pre : \mathbb{P} p r e : P , and a : A a : A a : A , s p p r e a ⟺ p r e ∧ s p ⊤ a sp\ pre\ a \Longleftrightarrow pre \land sp\ \top\ a s p p r e a ⟺ p r e ∧ s p ⊤ a .
Proof. Take the index set I : = { ∗ ∣ p r e } I := \{\ast\mid pre\} I := { ∗ ∣ p r e } , which is inhabited exactly when p r e pre p r e holds, and the constant family p ( i ) : = ⊤ p(i) := \top p ( i ) := ⊤ . Then ∃ i . p ( i ) \exists i.\ p(i) ∃ i . p ( i ) is equivalent to p r e pre p r e , so disjunctivity gives s p p r e a ⟺ ∃ i ∈ I . s p ⊤ a sp\ pre\ a \Longleftrightarrow\exists i \in I.\ sp\ \top\ a s p p r e a ⟺ ∃ i ∈ I . s p ⊤ a , which is p r e ∧ s p ⊤ a pre \land sp\ \top\ a p r e ∧ s p ⊤ a .
So an element of S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) admits a a a exactly when a a a is produced, s p ⊤ a sp\ \top\ a s p ⊤ a , and p r e pre p r e holds. For ret, unfolding arr gives
r e t v p r e a ⟺ ∃ x . p r e ∧ v = a ⟺ p r e ∧ a = v , ret\ v\ pre\ a \Longleftrightarrow\exists x.\ pre \land v = a \Longleftrightarrow pre \land a = v, r e t v p r e a ⟺ ∃ x . p r e ∧ v = a ⟺ p r e ∧ a = v ,
the ret of (ang). For bind, unfolding arr, first, arr, and app in turn gives
P 1 ( u , i ) ⟺ ∃ x . p r e ∧ ( x , x ) = ( u , i ) ⟺ p r e , P_1\ (u,i) \Longleftrightarrow\exists x.\ pre \land(x,x) = (u,i) \Longleftrightarrow pre, P 1 ( u , i ) ⟺ ∃ x . p r e ∧ ( x , x ) = ( u , i ) ⟺ p r e ,
P 2 ( a , i ) ⟺ m ( λ x . P 1 ( x , i ) ) a ⟺ m p r e a , P_2\ (a,i) \Longleftrightarrow m\ (\lambda x.\ P_1\ (x,i))\ a \Longleftrightarrow m\ pre\ a, P 2 ( a , i ) ⟺ m ( λ x . P 1 ( x , i )) a ⟺ m p r e a ,
P 3 ( g , i ) ⟺ ∃ ( a , j ) . P 2 ( a , j ) ∧ ( f a , j ) = ( g , i ) ⟺ ∃ a . m p r e a ∧ f a = g , P_3\ (g,i) \Longleftrightarrow\exists(a,j).\ P_2\ (a,j) \land(f\ a,j) = (g,i) \Longleftrightarrow\exists a.\ m\ pre\ a \land f\ a = g, P 3 ( g , i ) ⟺ ∃ ( a , j ) . P 2 ( a , j ) ∧ ( f a , j ) = ( g , i ) ⟺ ∃ a . m p r e a ∧ f a = g ,
a p p P 3 b ⟺ ∃ g . ∃ i . P 3 ( g , i ) ∧ g ( λ y . y = i ) b ⟺ ∃ a . m p r e a ∧ f a ⊤ b . app\ P_3\ b \Longleftrightarrow\exists g.\ \exists i.\ P_3\ (g,i) \land g\ (\lambda y.\ y = i)\ b \Longleftrightarrow\exists a.\ m\ pre\ a \land f\ a\ \top\ b. a pp P 3 b ⟺ ∃ g . ∃ i . P 3 ( g , i ) ∧ g ( λ y . y = i ) b ⟺ ∃ a . m p r e a ∧ f a ⊤ b .
In the last line, λ y . y = i \lambda y.\ y = i λ y . y = i is ⊤ \top ⊤ because 1 1 1 has a single element, so at B = 1 B = 1 B = 1 , app asks whether g g g produces b b b . By Lemma 5 for f a f\ a f a , the conjunction m p r e a ∧ f a ⊤ b m\ pre\ a \land f\ a\ \top\ b m p r e a ∧ f a ⊤ b is f a ( m p r e a ) b f\ a\ (m\ pre\ a)\ b f a ( m p r e a ) b , so
b i n d m f p r e b ⟺ ∃ a . f a ( m p r e a ) b , bind\ m\ f\ pre\ b \Longleftrightarrow\exists a.\ f\ a\ (m\ pre\ a)\ b, bin d m f p r e b ⟺ ∃ a . f a ( m p r e a ) b ,
the bind of (ang). Every step is constructive.
The Demonic Arrow
Lemma 6. For every s p : S P D S L P ( A ) sp : \mathrm{SP}_{\mathrm{DSLP}}(A) s p : SP DSLP ( A ) , p r e : P pre : \mathbb{P} p r e : P , and a : A a : A a : A , s p p r e a ⟺ p r e ∨ s p ⊥ a sp\ pre\ a \Longleftrightarrow pre \lor sp\ \bot\ a s p p r e a ⟺ p r e ∨ s p ⊥ a .
Proof. Take the index set J : = { ∗ ∣ ¬ p r e } J := \{\ast\mid\neg pre\} J := { ∗ ∣ ¬ p r e } and the constant family p ( j ) : = ⊥ p(j) := \bot p ( j ) := ⊥ . Then ∀ j . p ( j ) \forall j.\ p(j) ∀ j . p ( j ) is ¬ ¬ p r e \neg\neg pre ¬¬ p r e , which by excluded middle is equivalent to p r e pre p r e , so conjunctivity gives s p p r e a ⟺ ∀ j ∈ J . s p ⊥ a sp\ pre\ a \Longleftrightarrow\forall j \in J.\ sp\ \bot\ a s p p r e a ⟺ ∀ j ∈ J . s p ⊥ a , which is ¬ p r e ⇒ s p ⊥ a \neg pre \Rightarrow sp\ \bot\ a ¬ p r e ⇒ s p ⊥ a and, again by excluded middle, p r e ∨ s p ⊥ a pre \lor sp\ \bot\ a p r e ∨ s p ⊥ a .
So an element of S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) admits a a a exactly when p r e pre p r e holds or a a a is not produced, s p ⊥ a sp\ \bot\ a s p ⊥ a . For ret,
r e t v p r e a ⟺ ∀ x . v = a ⇒ p r e ⟺ a = v ⇒ p r e , ret\ v\ pre\ a \Longleftrightarrow\forall x.\ v = a \Rightarrow pre \Longleftrightarrow a = v \Rightarrow pre, r e t v p r e a ⟺ ∀ x . v = a ⇒ p r e ⟺ a = v ⇒ p r e ,
the ret of (dem). For bind, the same unfolding gives
P 1 ( u , i ) ⟺ ∀ x . ( x , x ) = ( u , i ) ⇒ p r e ⟺ p r e , P_1\ (u,i) \Longleftrightarrow\forall x.\ (x,x) = (u,i) \Rightarrow pre \Longleftrightarrow pre, P 1 ( u , i ) ⟺ ∀ x . ( x , x ) = ( u , i ) ⇒ p r e ⟺ p r e ,
P 2 ( a , i ) ⟺ m ( λ x . P 1 ( x , i ) ) a ⟺ m p r e a , P_2\ (a,i) \Longleftrightarrow m\ (\lambda x.\ P_1\ (x,i))\ a \Longleftrightarrow m\ pre\ a, P 2 ( a , i ) ⟺ m ( λ x . P 1 ( x , i )) a ⟺ m p r e a ,
P 3 ( g , i ) ⟺ ∀ ( a , j ) . ( f a , j ) = ( g , i ) ⇒ P 2 ( a , j ) ⟺ ∀ a . f a = g ⇒ m p r e a , P_3\ (g,i) \Longleftrightarrow\forall(a,j).\ (f\ a,j) = (g,i) \Rightarrow P_2\ (a,j) \Longleftrightarrow\forall a.\ f\ a = g \Rightarrow m\ pre\ a, P 3 ( g , i ) ⟺ ∀ ( a , j ) . ( f a , j ) = ( g , i ) ⇒ P 2 ( a , j ) ⟺ ∀ a . f a = g ⇒ m p r e a ,
a p p P 3 b ⟺ ∀ g . ∀ i . ( ∀ p . g p b ⇒ p i ) ⇒ P 3 ( g , i ) app\ P_3\ b \Longleftrightarrow\forall g.\ \forall i.\ (\forall p.\ g\ p\ b \Rightarrow p\ i) \Rightarrow P_3\ (g,i) a pp P 3 b ⟺ ∀ g . ∀ i . ( ∀ p . g p b ⇒ p i ) ⇒ P 3 ( g , i )
⟺ ∀ a . ( ∀ p . f a p b ⇒ p ) ⇒ m p r e a . \Longleftrightarrow\forall a.\ (\forall p.\ f\ a\ p\ b \Rightarrow p) \Rightarrow m\ pre\ a. ⟺ ∀ a . ( ∀ p . f a p b ⇒ p ) ⇒ m p r e a .
In the last two lines, p p p ranges over 1 → P ≅ P 1 \to\mathbb{P} \cong\mathbb{P} 1 → P ≅ P , so p i p\ i p i is p p p , and at B = 1 B = 1 B = 1 , app again asks whether g g g produces b b b : every precondition that g g g accepts for b b b holds.
By Lemma 6 for f a f\ a f a , this is ¬ ( f a ⊥ b ) \neg(f\ a\ \bot\ b) ¬ ( f a ⊥ b ) : taking p : = ⊥ p := \bot p := ⊥ gives one direction, and for the other, f a p b f\ a\ p\ b f a p b gives p ∨ f a ⊥ b p \lor f\ a\ \bot\ b p ∨ f a ⊥ b . Then, by excluded middle and Lemma 6 again,
bind m f pre b ⟺ ∀ a . ¬ ( f a ⊥ b ) ⇒ m pre a ⟺ ∀ a . m pre a ∨ f a ⊥ b ⟺ ∀ a . f a ( m pre a ) b , \begin{aligned}
\operatorname{bind}\ m\ f\ \operatorname{pre}\ b &\Longleftrightarrow\forall a.\neg(f\ a\ \bot\ b) \Rightarrow m\ \operatorname{pre}\ a\\
&\Longleftrightarrow\forall a.\ m\ \operatorname{pre}\ a \lor f\ a\ \bot\ b\\
&\Longleftrightarrow\forall a.\ f\ a\ (m\ \operatorname{pre}\ a)\ b,
\end{aligned} bind m f pre b ⟺ ∀ a .¬ ( f a ⊥ b ) ⇒ m pre a ⟺ ∀ a . m pre a ∨ f a ⊥ b ⟺ ∀ a . f a ( m pre a ) b ,
the bind of (dem). Unlike the angelic derivation, this one is classical.
Stateful s p sp s p Arrows and Monads
This appendix gives the stateful arrows of § 2.1.2 and the ret and bind of S P D S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}}(S,A) SP DSP St ( S , A ) and S P A S L P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}}(S,A) SP ASLP St ( S , A ) .
The Stateful Arrows
The state arrow transformer [17 ] threads a state through the input and the output of an arrow, StArrT ( Arr ) ( S , B , A ) : = Arr ( B × S , A × S ) \operatorname{StArrT}(\operatorname{Arr})(S,B,A) := \operatorname{Arr}(B \times S,A \times S) StArrT ( Arr ) ( S , B , A ) := Arr ( B × S , A × S ) , with the operations
arr S t f : = arr ( λ ( b , s ) . ( f b , s ) ) , \operatorname{arr}_{\mathrm{St}}\ f := \operatorname{arr}(\lambda(b,s).(f\ b,s)), arr St f := arr ( λ ( b , s ) . ( f b , s )) ,
f ≫ S t g : = f ≫ g , f \mathbin{\gg_{\mathrm{St}}} g := f \mathbin{\gg} g, f ≫ St g := f ≫ g ,
first S t f : = arr ( λ ( ( b , c ) , s ) . ( ( b , s ) , c ) ) ≫ first f ≫ arr ( λ ( ( a , s ) , c ) . ( ( a , c ) , s ) ) , \operatorname{first}_{\mathrm{St}}\ f := \operatorname{arr}(\lambda((b,c),s).((b,s),c)) \mathbin{\gg} \operatorname{first}\ f \mathbin{\gg} \operatorname{arr}(\lambda((a,s),c).((a,c),s)), first St f := arr ( λ (( b , c ) , s ) . (( b , s ) , c )) ≫ first f ≫ arr ( λ (( a , s ) , c ) . (( a , c ) , s )) ,
app S t : = arr ( λ ( ( f , b ) , s ) . ( f , ( b , s ) ) ) ≫ app . \operatorname{app}_{\mathrm{St}} := \operatorname{arr}(\lambda((f,b),s).(f,(b,s))) \mathbin{\gg} \operatorname{app}. app St := arr ( λ (( f , b ) , s ) . ( f , ( b , s ))) ≫ app .
The state passes through arr unchanged, first moves it next to the input of f f f and back, and app hands it to the applied arrow together with its input. For StArrT ( Arr A S P ) ( S , B , A ) \operatorname{StArrT}(\operatorname{Arr}_{\mathrm{ASP}})(S,B,A) StArrT ( Arr ASP ) ( S , B , A ) , unfolding the operations of § 2.1.1 gives
arr S t f = λ pre ( a , s ) . ∃ b . pre ( b , s ) ∧ f b = a , \operatorname{arr}_{\mathrm{St}}\ f = \lambda\operatorname{pre}\ (a,s).\ \exists b.\ \operatorname{pre}\ (b,s) \land f\ b = a, arr St f = λ pre ( a , s ) . ∃ b . pre ( b , s ) ∧ f b = a ,
first S t f = λ pre ( ( a , c ) , s ) . f ( λ ( b , s ′ ) . pre ( ( b , c ) , s ′ ) ) ( a , s ) , \operatorname{first}_{\mathrm{St}}\ f = \lambda\operatorname{pre}\ ((a,c),s).\ f\ (\lambda(b,s').\operatorname{pre}\ ((b,c),s'))\ (a,s), first St f = λ pre (( a , c ) , s ) . f ( λ ( b , s ′ ) . pre (( b , c ) , s ′ )) ( a , s ) ,
app S t = λ pre ( a , s ) . ∃ f b s ′ . pre ( ( f , b ) , s ′ ) ∧ f ( λ x . x = ( b , s ′ ) ) ( a , s ) , \operatorname{app}_{\mathrm{St}} = \lambda\operatorname{pre}\ (a,s).\ \exists f\ b\ s'.\ \operatorname{pre}\ ((f,b),s') \land f\ (\lambda x.\ x = (b,s'))\ (a,s), app St = λ pre ( a , s ) . ∃ f b s ′ . pre (( f , b ) , s ′ ) ∧ f ( λ x . x = ( b , s ′ )) ( a , s ) ,
and for StArrT ( Arr D S L P ) ( S , B , A ) \operatorname{StArrT}(\operatorname{Arr}_{\mathrm{DSLP}})(S,B,A) StArrT ( Arr DSLP ) ( S , B , A )
arr S t f = λ pre ( a , s ) . ∀ b . f b = a ⇒ pre ( b , s ) , \operatorname{arr}_{\mathrm{St}}\ f = \lambda\operatorname{pre}\ (a,s).\ \forall b.\ f\ b = a \Rightarrow\operatorname{pre}\ (b,s), arr St f = λ pre ( a , s ) . ∀ b . f b = a ⇒ pre ( b , s ) ,
app S t = λ pre ( a , s ) . ∀ f b s ′ . ( ∀ p . f p ( a , s ) ⇒ p ( b , s ′ ) ) ⇒ pre ( ( f , b ) , s ′ ) . \operatorname{app}_{\mathrm{St}} = \lambda\operatorname{pre}\ (a,s).\ \forall f\ b\ s'.\ (\forall p.\ f\ p\ (a,s) \Rightarrow p\ (b,s')) \Rightarrow\operatorname{pre}\ ((f,b),s'). app St = λ pre ( a , s ) . ∀ f b s ′ . ( ∀ p . f p ( a , s ) ⇒ p ( b , s ′ )) ⇒ pre (( f , b ) , s ′ ) .
In both stateful arrows, ≫ S t \mathbin{\gg_{\mathrm{St}}} ≫ St uses the corresponding base-arrow composition. Their definitions of first S t \operatorname{first}_{\mathrm{St}} first St coincide because the base definitions of first coincide and the stateful definitions apply arr only to bijections. For such bijections h h h , both arrows give arr h = λ pre y . pre ( h − 1 y ) \operatorname{arr}\ h = \lambda\operatorname{pre}\ y.\operatorname{pre}\ (h^{-1}y) arr h = λ pre y . pre ( h − 1 y ) . Applying the transformer to the operations of Arr D S P ( B , A ) \operatorname{Arr}_{\mathrm{DSP}}(B,A) Arr DSP ( B , A ) and Arr A S L P ( B , A ) \operatorname{Arr}_{\mathrm{ASLP}}(B,A) Arr ASLP ( B , A ) (Appendix B) gives their stateful arrows in the same way.
The Stateful d s p dsp d s p and a s l p aslp a s l p Monads
Fixing B = 1 B = 1 B = 1 gives the monads of § 2.1.2 , whose elements are d s p dsp d s p , respectively a s l p aslp a s l p , arrows from 1 × S 1 \times S 1 × S to A × S A \times S A × S , with preconditions over states, ( 1 × S → P ) ≅ ( S → P ) (1 \times S \to\mathbb{P}) \cong(S \to\mathbb{P}) ( 1 × S → P ) ≅ ( S → P ) . Their a s p asp a s p and d s l p dslp d s l p components are therefore the aspOf and dslpOf of Appendix B at input 1 × S 1 \times S 1 × S , and, as for the base monads, ret and bind are computed through them. For S P D S P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSP}}(S,A) SP DSP St ( S , A ) ,
ret D S P v ≡ ret A S P v ∧ ret D S L P v , \operatorname{ret}_{\mathrm{DSP}}\,v \equiv\operatorname{ret}_{\mathrm{ASP}}\,v \land\operatorname{ret}_{\mathrm{DSLP}}\,v, ret DSP v ≡ ret ASP v ∧ ret DSLP v ,
bind D S P m f ≡ λ p r e r . bind A S P ( aspOf m ) ( λ a . aspOf ( f a ) ) p r e r ∧ \operatorname{bind}_{\mathrm{DSP}}\,m\,f \equiv\lambda\,\mathit{pre}\ r.\ \operatorname{bind}_{\mathrm{ASP}}\left(\operatorname{aspOf}\,m\right)\left(\lambda a.\ \operatorname{aspOf}\,(f\,a)\right)\mathit{pre}\ r \land bind DSP m f ≡ λ pre r . bind ASP ( aspOf m ) ( λa . aspOf ( f a ) ) pre r ∧
bind D S L P ( dslpOf m ) ( λ a . dslpOf ( f a ) ) p r e r , \operatorname{bind}_{\mathrm{DSLP}}\left(\operatorname{dslpOf}\,m\right)\left(\lambda a.\ \operatorname{dslpOf}\,(f\,a)\right)\mathit{pre}\ r, bind DSLP ( dslpOf m ) ( λa . dslpOf ( f a ) ) pre r ,
where ret A S P \operatorname{ret}_{\mathrm{ASP}} ret ASP , bind A S P \operatorname{bind}_{\mathrm{ASP}} bind ASP , ret D S L P \operatorname{ret}_{\mathrm{DSLP}} ret DSLP , and bind D S L P \operatorname{bind}_{\mathrm{DSLP}} bind DSLP are those of (ang-st) and (dem-st). For S P A S L P S t ( S , A ) \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASLP}}(S,A) SP ASLP St ( S , A ) , ret and bind are the same with ∨ \lor ∨ in place of ∧ \land ∧ . Since ret A S P v \operatorname{ret}_{\mathrm{ASP}}\,v ret ASP v implies ret D S L P v \operatorname{ret}_{\mathrm{DSLP}}\,v ret DSLP v , these simplify to ret D S P v ≡ ret A S P v \operatorname{ret}_{\mathrm{DSP}}\,v \equiv\operatorname{ret}_{\mathrm{ASP}}\,v ret DSP v ≡ ret ASP v and ret A S L P v ≡ ret D S L P v \operatorname{ret}_{\mathrm{ASLP}}\,v \equiv\operatorname{ret}_{\mathrm{DSLP}}\,v ret ASLP v ≡ ret DSLP v .
E Counterexamples to the Monad and Arrow Laws
This appendix works through the counterexamples behind the carrier restrictions of § 2 . Each keeps the operations of one of our monads or arrows but drops its homomorphism restriction:
§E.1: the stateful ret and bind of S P A S P ( S , A ) \mathrm{SP}_{\mathrm{ASP}}(S,A) SP ASP ( S , A ) on monotone, strict sps, without disjunctivity;
§E.2: the stateful ret and bind of S P D S L P ( S , A ) \mathrm{SP}_{\mathrm{DSLP}}(S,A) SP DSLP ( S , A ) on monotone, terminating sps, without conjunctivity;
§E.3: the operations of A r r A S P ( B , A ) \mathrm{Arr}_{\mathrm{ASP}}(B,A) Arr ASP ( B , A ) and A r r D S L P ( B , A ) \mathrm{Arr}_{\mathrm{DSLP}}(B,A) Arr DSLP ( B , A ) on monotone, strict, respectively terminating, spsarr.
The two monads fail associativity and the two arrows the arrow law first f ≫ arr fst ≡ arr fst ≫ f f \gg\operatorname{arr}\,\operatorname{fst} \equiv\operatorname{arr}\,\operatorname{fst} \gg f f ≫ arr fst ≡ arr fst ≫ f . Each counterexample uses a transformer that inspects two inputs at once, which disjunctivity, respectively conjunctivity, rules out.
Associativity Without Disjunctivity
Consider S P o s t a n g ( S , A ) \mathrm{SPostang}(S,A) SPostang ( S , A ) , whose elements are monotone stateful sps of type ( p r e : S → P ) → ( A × S → P / p r e ) (\mathit{pre}:S\to\mathbb{P})\to(A\times S\to\mathbb{P}_{/\mathit{pre}}) ( pre : S → P ) → ( A × S → P / pre ) . Here P / p r e = { p : P ∣ p ⇒ ∃ s . p r e s } \mathbb{P}_{/\mathit{pre}}=\{p:\mathbb{P}\mid p\Rightarrow\exists s.\mathit{pre}\ s\} P / pre = { p : P ∣ p ⇒ ∃ s . pre s } is the codomain condition of Spost ( A ) \operatorname{Spost}(A) Spost ( A ) ; for monotone sps, it amounts to strictness. We use the ret and bind of S P A S P ( S , A ) \mathrm{SP}_{\mathrm{ASP}}(S,A) SP ASP ( S , A ) from (ang-st). The unit laws hold, but bind is not associative as soon as there are two distinct states s 1 , s 2 s_1,s_2 s 1 , s 2 and two distinct values a 1 , a 2 a_1,a_2 a 1 , a 2 . Take, for any inhabited types B and C,
m : S P o s t a n g ( S , A ) : = λ p r e _ . ∃ s . p r e s , m:\mathrm{SPostang}(S,A):=\lambda\,\mathit{pre}\ \_.\ \exists s.\mathit{pre}\ s, m : SPostang ( S , A ) := λ pre _. ∃ s . pre s ,
f : A → S P o s t a n g ( S , B ) : = λ a . λ p r e ( b , s ) . ( ∃ s ′ . p r e s ′ ) ∧ ( ( a = a 1 ∧ s = s 1 ) ∨ ( a = a 2 ∧ s = s 2 ) ) , f:A\to\mathrm{SPostang}(S,B):=\lambda a.\lambda\,\mathit{pre}\ (b,s).\left(\exists s'.\mathit{pre}\ s'\right)\land\left((a=a_1\land s=s_1)\lor(a=a_2\land s=s_2)\right), f : A → SPostang ( S , B ) := λa . λ pre ( b , s ) . ( ∃ s ′ . pre s ′ ) ∧ ( ( a = a 1 ∧ s = s 1 ) ∨ ( a = a 2 ∧ s = s 2 ) ) ,
g : S P o s t a n g ( S , C ) : = λ p r e _ . p r e s 1 ∧ p r e s 2 . g:\mathrm{SPostang}(S,C):=\lambda\,\mathit{pre}\ \_.\mathit{pre}\ s_1\land\mathit{pre}\ s_2. g : SPostang ( S , C ) := λ pre _. pre s 1 ∧ pre s 2 .
When its precondition is satisfiable, m m m admits every result; f a f\ a f a ends in s 1 s_1 s 1 when a = a 1 a=a_1 a = a 1 , in s 2 s_2 s 2 when a = a 2 a=a_2 a = a 2 , and has no result otherwise; and g g g has a result only if its precondition contains both s 1 s_1 s 1 and s 2 s_2 s 2 . All three are monotone and have the restricted type. For p r e : = λ _ . ⊤ \mathit{pre}:=\lambda\_.\top pre := λ _.⊤ and r : = ( c 0 , s 1 ) r:=(c_0,s_1) r := ( c 0 , s 1 ) , compare
bind ( bind m f ) ( λ _ . g ) p r e r and bind m ( λ a . bind ( f a ) ( λ _ . g ) ) p r e r . \operatorname{bind}\,(\operatorname{bind}\,m\,f)\,(\lambda\_.g)\,\mathit{pre}\ r
\quad\text{and}\quad
\operatorname{bind}\,m\,(\lambda a.\operatorname{bind}\,(f\,a)\,(\lambda\_.g))\,\mathit{pre}\ r. bind ( bind m f ) ( λ _. g ) pre r and bind m ( λa . bind ( f a ) ( λ _. g )) pre r .
Since m p r e ( a , s ′ ′ ) m\ \mathit{pre}\ (a,s'') m pre ( a , s ′′ ) holds for all a a a and s ′ ′ s'' s ′′ , the intermediate states that f a f\ a f a passes on to g g g are P a : = λ s ′ . ( a = a 1 ∧ s ′ = s 1 ) ∨ ( a = a 2 ∧ s ′ = s 2 ) P_a := \lambda s'.(a = a_1 \land s' = s_1) \lor(a = a_2 \land s' = s_2) P a := λ s ′ . ( a = a 1 ∧ s ′ = s 1 ) ∨ ( a = a 2 ∧ s ′ = s 2 ) , so P a 1 = { s 1 } P_{a_1} = \{s_1\} P a 1 = { s 1 } , P a 2 = { s 2 } P_{a_2} = \{s_2\} P a 2 = { s 2 } , and P a = ∅ P_a = \varnothing P a = ∅ otherwise. Unfolding bind, the left-hand side is equivalent to ∃ b . g ( λ s ′ . ∃ a . P a s ′ ) r \exists b.\ g\ (\lambda s'.\exists a.\ P_a\ s')\ r ∃ b . g ( λ s ′ .∃ a . P a s ′ ) r and the right-hand side to ∃ a . ∃ b . g ( λ s ′ . P a s ′ ) r \exists a.\exists b.\ g\ (\lambda s'.P_a\ s')\ r ∃ a .∃ b . g ( λ s ′ . P a s ′ ) r . On the left, g g g receives the union ⋃ a P a = { s 1 , s 2 } \bigcup_a P_a = \{s_1,s_2\} ⋃ a P a = { s 1 , s 2 } and admits r r r . On the right, a a a is chosen first and g g g receives a single P a P_a P a , none of which contains both states, so it does not. A disjunctive g g g would satisfy g ( ⋃ a P a ) = ⋃ a g ( P a ) g(\bigcup_a P_a) = \bigcup_a g(P_a) g ( ⋃ a P a ) = ⋃ a g ( P a ) and make the two sides agree.
Associativity Without Conjunctivity
The demonic monad S P o s t d e m ( S , A ) \mathrm{SPost}_{\mathrm{dem}}(S,A) SPost dem ( S , A ) has the dual codomain, which amounts to termination, ( p r e : S → P ) → ( A × S → P / p r e ∨ ) (\mathit{pre}:S\to\mathbb{P})\to(A\times S\to\mathbb{P}^{\vee}_{/\mathit{pre}}) ( pre : S → P ) → ( A × S → P / pre ∨ ) with P / p r e ∨ = { p : P ∣ ( ∀ s . p r e s ) ⇒ p } \mathbb{P}^{\vee}_{/\mathit{pre}} = \{p:\mathbb{P}\mid(\forall s.\ \mathit{pre}\ s)\Rightarrow p\} P / pre ∨ = { p : P ∣ ( ∀ s . pre s ) ⇒ p } , and the ret and bind (dem-st) of S P D S L P ( S , A ) \mathrm{SP}_{\mathrm{DSLP}}(S,A) SP DSLP ( S , A ) . Again the unit laws hold and associativity fails, now with the dual transformers
m : = λ p r e _ . ∀ s . p r e s , m := \lambda\ \mathit{pre}\ \_.\forall s.\ \mathit{pre}\ s, m := λ pre _.∀ s . pre s ,
f : = λ a . λ p r e ( b , s ) . ( ( a = a 1 ∧ s = s 1 ) ∨ ( a = a 2 ∧ s = s 2 ) ) ⇒ ( ∀ s ′ . p r e s ′ ) , f := \lambda a.\lambda\ \mathit{pre}\ (b,s).((a = a_1 \land s = s_1)\lor(a = a_2 \land s = s_2))\Rightarrow(\forall s'.\ \mathit{pre}\ s'), f := λa . λ pre ( b , s ) . (( a = a 1 ∧ s = s 1 ) ∨ ( a = a 2 ∧ s = s 2 )) ⇒ ( ∀ s ′ . pre s ′ ) ,
g : = λ p r e _ . p r e s 1 ∨ p r e s 2 , g := \lambda\ \mathit{pre}\ \_.\mathit{pre}\ s_1\lor\mathit{pre}\ s_2, g := λ pre _. pre s 1 ∨ pre s 2 ,
where g g g accepts a precondition that contains at least one of s 1 s_1 s 1 and s 2 s_2 s 2 . Take p r e : = λ _ . ⊥ \mathit{pre}:=\lambda\_.\bot pre := λ _.⊥ and r : = ( c 0 , s 1 ) r:=(c_0,s_1) r := ( c 0 , s 1 ) . Since m p r e ( a , s ′ ′ ) m\ \mathit{pre}\ (a,s'') m pre ( a , s ′′ ) is false, the intermediate states passed on to g g g are P a : = λ s ′ . ¬ ( a = a 1 ∧ s ′ = s 1 ) ∧ ¬ ( a = a 2 ∧ s ′ = s 2 ) P_a:=\lambda s'.\neg(a = a_1\land s' = s_1)\land\neg(a = a_2\land s' = s_2) P a := λ s ′ .¬ ( a = a 1 ∧ s ′ = s 1 ) ∧ ¬ ( a = a 2 ∧ s ′ = s 2 ) , so P a 1 = S ∖ { s 1 } P_{a_1}=S\setminus\{s_1\} P a 1 = S ∖ { s 1 } , P a 2 = S ∖ { s 2 } P_{a_2}=S\setminus\{s_2\} P a 2 = S ∖ { s 2 } , and P a = S P_a=S P a = S otherwise. The left-hand side is equivalent to ∀ b . g ( λ s ′ . ∀ a . P a s ′ ) r \forall b.\ g\ (\lambda s'.\forall a.\ P_a\ s')\ r ∀ b . g ( λ s ′ .∀ a . P a s ′ ) r , which fails because ⋂ a P a = S ∖ { s 1 , s 2 } \bigcap_a P_a=S\setminus\{s_1,s_2\} ⋂ a P a = S ∖ { s 1 , s 2 } contains neither state. The right-hand side is equivalent to ∀ a . ∀ b . g ( λ s ′ . P a s ′ ) r \forall a.\forall b.\ g\ (\lambda s'.P_a\ s')\ r ∀ a .∀ b . g ( λ s ′ . P a s ′ ) r , which holds because every P a P_a P a contains s 1 s_1 s 1 or s 2 s_2 s 2 . A conjunctive g g g would make the two sides agree.
Arrow Laws Without Disjunctivity or Conjunctivity
The same phenomenon breaks the arrow laws. Let S P A r r a n g ( B , A ) \mathrm{SPArr}_{\mathrm{ang}}(B,A) SPArr ang ( B , A ) consist of the monotone, strict s p s a r r sp\!s_{\mathrm{arr}} s p s arr of type ( p r e : B → P ) → ( A → P / p r e ) (\mathit{pre}:B\to\mathbb{P})\to(A\to\mathbb{P}_{/\mathit{pre}}) ( pre : B → P ) → ( A → P / pre ) , where P / p r e = { p : P ∣ p ⇒ ∃ b . p r e b } \mathbb{P}_{/\mathit{pre}}=\{p:\mathbb{P}\mid p\Rightarrow\exists b.\ \mathit{pre}\ b\} P / pre = { p : P ∣ p ⇒ ∃ b . pre b } , with the operations of A r r A S P ( B , A ) \mathrm{Arr}_{\mathrm{ASP}}(B,A) Arr ASP ( B , A ) . Fixing B = 1 B=1 B = 1 gives the lawful monad S P o s t a n g ( A ) \mathrm{SPost}_{\mathrm{ang}}(A) SPost ang ( A ) , yet the arrow is not lawful: the law first f ≫ a r r f s t ≡ a r r f s t ≫ f f\mathbin{\gg} \mathrm{arr}\ \mathrm{fst}\equiv\mathrm{arr}\ \mathrm{fst}\mathbin{\gg}f f ≫ arr fst ≡ arr fst ≫ f fails. Take distinct b 1 , b 2 : B b_1,b_2:B b 1 , b 2 : B and c 1 , c 2 : C c_1,c_2:C c 1 , c 2 : C , and
g : S P A r r a n g ( B , A ) : = λ p r e _ . p r e b 1 ∧ p r e b 2 , g:\mathrm{SPArr}_{\mathrm{ang}}(B,A):=\lambda\ \mathit{pre}\ \_.\mathit{pre}\ b_1\land\mathit{pre}\ b_2, g : SPArr ang ( B , A ) := λ pre _. pre b 1 ∧ pre b 2 ,
p r e : = λ ( b , c ) . ( b = b 1 ∧ c = c 1 ) ∨ ( b = b 2 ∧ c = c 2 ) . \mathit{pre}:=\lambda(b,c).(b=b_1\land c=c_1)\lor(b=b_2\land c=c_2). pre := λ ( b , c ) . ( b = b 1 ∧ c = c 1 ) ∨ ( b = b 2 ∧ c = c 2 ) .
Writing P c : = λ b . p r e ( b , c ) P_c:=\lambda b.\ \mathit{pre}\ (b,c) P c := λb . pre ( b , c ) , we have P c 1 = { b 1 } P_{c_1}=\{b_1\} P c 1 = { b 1 } , P c 2 = { b 2 } P_{c_2}=\{b_2\} P c 2 = { b 2 } , and P c = ∅ P_c=\varnothing P c = ∅ otherwise. Unfolding the operations, for any a : A a:A a : A ,
( f i r s t g ≫ a r r f s t ) p r e a ⟺ ∃ c . g P c a , (\mathrm{first}\ g\mathbin{\gg}\mathrm{arr}\ \mathrm{fst})\ \mathit{pre}\ a\Longleftrightarrow\exists c.\ g\ P_c\ a, ( first g ≫ arr fst ) pre a ⟺ ∃ c . g P c a ,
( a r r f s t ≫ g ) p r e a ⟺ g ( ⋃ c P c ) a . (\mathrm{arr}\ \mathrm{fst}\mathbin{\gg}g)\ \mathit{pre}\ a\Longleftrightarrow g\left(\bigcup_c P_c\right)a. ( arr fst ≫ g ) pre a ⟺ g ( c ⋃ P c ) a .
The left-hand side is false, since no P c P_c P c contains both b 1 b_1 b 1 and b 2 b_2 b 2 , and the right-hand side is true, since ⋃ c P c = { b 1 , b 2 } \bigcup_c P_c=\{b_1,b_2\} ⋃ c P c = { b 1 , b 2 } . A disjunctive g g g would make the two sides agree, which is why A r r A S P ( B , A ) \mathrm{Arr}_{\mathrm{ASP}}(B,A) Arr ASP ( B , A ) restricts its carrier to disjunctive s p s a r r sp\!s_{\mathrm{arr}} s p s arr . Dually, for the demonic arrow SPArr d e m ( B , A ) \operatorname{SPArr}_{\mathrm{dem}}(B,A) SPArr dem ( B , A ) of monotone, terminating s p s a r r sp s_{\mathrm{arr}} s p s arr with the operations of Arr D S L P ( B , A ) \operatorname{Arr}_{\mathrm{DSLP}}(B,A) Arr DSLP ( B , A ) , the same law fails for g : = λ p r e _ . p r e b 1 ∨ p r e b 2 g := \lambda\, pre\, \_.\, pre\, b_{1} \lor pre\, b_{2} g := λ p r e _. p r e b 1 ∨ p r e b 2 and the complementary precondition p r e : = λ ( b , c ) . ¬ ( ( b = b 1 ∧ c = c 1 ) ∨ ( b = b 2 ∧ c = c 2 ) ) pre := \lambda(b,c).\,\neg((b=b_{1}\land c=c_{1})\lor(b=b_{2}\land c=c_{2})) p r e := λ ( b , c ) . ¬ (( b = b 1 ∧ c = c 1 ) ∨ ( b = b 2 ∧ c = c 2 )) , now with the left-hand side true and the right-hand side false.
Decomposing d s p dsp d s p and a s l p aslp a s l p Predicate Transformers
This appendix proves that an element of S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) admits the same outputs as the conjunction of its a s p asp a s p and d s l p dslp d s l p components, that an element of S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) admits the same outputs as their disjunction, and that in both cases the components lie in S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) .
Components of S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A )
Recall the components of an element s p sp s p of S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) from §3.1,
aspOf s p : = λ p r e a . s p ⊤ a ∧ ¬ ( s p ( ¬ p r e ) a ) , dslpOf s p : = λ p r e a . s p p r e a ∨ ¬ ( s p ⊤ a ) , \begin{aligned}
\operatorname{aspOf}\ sp &:= \lambda\, pre\, a.\, sp\ \top\ a\land\neg(sp\ (\neg pre)\ a),\\
\operatorname{dslpOf}\ sp &:= \lambda\, pre\, a.\, sp\ pre\ a\lor\neg(sp\ \top\ a),
\end{aligned} aspOf s p dslpOf s p := λ p r e a . s p ⊤ a ∧ ¬ ( s p ( ¬ p r e ) a ) , := λ p r e a . s p p r e a ∨ ¬ ( s p ⊤ a ) ,
and that the carrier of S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) consists of the strict, conjunctive, and hence monotone, s p s sps s p s .
Lemma 7. For every s p : S P D S P ( A ) sp : \mathrm{SP}_{\mathrm{DSP}}(A) s p : SP DSP ( A ) , p r e : P pre : \mathbb{P} p r e : P , and a : A a : A a : A ,
s p p r e a ⟺ aspOf s p p r e a ∧ dslpOf s p p r e a . sp\ pre\ a\Longleftrightarrow\operatorname{aspOf}\ sp\ pre\ a\land\operatorname{dslpOf}\ sp\ pre\ a. s p p r e a ⟺ aspOf s p p r e a ∧ dslpOf s p p r e a . Proof. Suppose s p p r e a sp\ pre\ a s p p r e a . Then dslpOf s p p r e a \operatorname{dslpOf}\ sp\ pre\ a dslpOf s p p r e a holds by its first disjunct, and s p ⊤ a sp\ \top\ a s p ⊤ a holds by monotonicity. If s p ( ¬ p r e ) a sp\ (\neg pre)\ a s p ( ¬ p r e ) a also held, conjunctivity would give s p ( p r e ∧ ¬ p r e ) a sp\ (pre\land\neg pre)\ a s p ( p r e ∧ ¬ p r e ) a , and monotonicity s p ⊥ a sp\ \bot\ a s p ⊥ a , which strictness rules out. So ¬ ( s p ( ¬ p r e ) a ) \neg(sp\ (\neg pre)\ a) ¬ ( s p ( ¬ p r e ) a ) , and aspOf s p p r e a \operatorname{aspOf}\ sp\ pre\ a aspOf s p p r e a holds. Conversely, aspOf s p p r e a \operatorname{aspOf}\ sp\ pre\ a aspOf s p p r e a gives s p ⊤ a sp\ \top\ a s p ⊤ a , which rules out the second disjunct of dslpOf s p p r e a \operatorname{dslpOf}\ sp\ pre\ a dslpOf s p p r e a , so its first disjunct, s p p r e a sp\ pre\ a s p p r e a , holds.
This proof is constructive. Classical reasoning is needed only to show that the components satisfy the carrier restrictions of S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) .
Lemma 8. For every s p : S P D S P ( A ) sp : \mathrm{SP}_{\mathrm{DSP}}(A) s p : SP DSP ( A ) , aspOf s p \operatorname{aspOf}\ sp aspOf s p is strict and disjunctive, and dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p is terminating and conjunctive.
Proof. Both components are monotone because s p sp s p is and negation reverses implication. aspOf s p ⊥ a \operatorname{aspOf}\ sp\ \bot\ a aspOf s p ⊥ a is s p ⊤ a ∧ ¬ ( s p ⊤ a ) sp\ \top\ a\land\neg(sp\ \top\ a) s p ⊤ a ∧ ¬ ( s p ⊤ a ) , which is false, so aspOf s p \operatorname{aspOf}\ sp aspOf s p is strict. For a non-empty family ( p i ) i ∈ I (p_i)_{i\in I} ( p i ) i ∈ I , ¬ ∃ i . p i \neg\exists i.\,p_i ¬∃ i . p i is ∀ i . ¬ p i \forall i.\,\neg p_i ∀ i . ¬ p i , and conjunctivity of s p sp s p turns ¬ ( s p ( ¬ ∃ i . p i ) a ) \neg(sp\ (\neg\exists i.\,p_i)\ a) ¬ ( s p ( ¬∃ i . p i ) a ) into ¬ ∀ i . s p ( ¬ p i ) a \neg\forall i.\,sp\ (\neg p_i)\ a ¬∀ i . s p ( ¬ p i ) a . By excluded middle, this is ∃ i . ¬ ( s p ( ¬ p i ) a ) \exists i.\,\neg(sp\ (\neg p_i)\ a) ∃ i . ¬ ( s p ( ¬ p i ) a ) , which makes aspOf s p \operatorname{aspOf}\ sp aspOf s p disjunctive. dslpOf s p ⊤ a \operatorname{dslpOf}\ sp\ \top\ a dslpOf s p ⊤ a is s p ⊤ a ∨ ¬ ( s p ⊤ a ) sp\ \top\ a\lor\neg(sp\ \top\ a) s p ⊤ a ∨ ¬ ( s p ⊤ a ) , which holds by excluded middle, so dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p is terminating. Its conjunctivity follows from that of s p sp s p by a case split, again by excluded middle, on whether s p ⊤ a sp\ \top\ a s p ⊤ a holds.
Components of SPASLP(A)
The carrier of S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) SP ASLP ( A ) consists of the terminating, disjunctive, and hence monotone, s p s sps s p s . For an element s p sp s p , the output a a a is admitted under the ⊥ \bot ⊥ precondition, s p ⊥ a sp\mathbin{\bot}a s p ⊥ a , exactly when a a a is not produced, and we calculate the two components as follows:
aspOf : S P A S L P ( A ) → S P A S P ( A ) \operatorname{aspOf} : \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) \to\mathrm{SP}_{\mathrm{ASP}}(\mathrm{A}) aspOf : SP ASLP ( A ) → SP ASP ( A )
aspOf s p : = λ p r e a . s p p r e a ∧ ¬ ( s p ⊥ a ) \operatorname{aspOf}\ sp := \lambda\ pre\ a.\ sp\ pre\ a \land\neg(sp\mathbin{\bot}a) aspOf s p := λ p r e a . s p p r e a ∧ ¬ ( s p ⊥ a )
dslpOf : S P A S L P ( A ) → S P D S L P ( A ) \operatorname{dslpOf} : \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) \to\mathrm{SP}_{\mathrm{DSLP}}(\mathrm{A}) dslpOf : SP ASLP ( A ) → SP DSLP ( A )
dslpOf s p : = λ p r e a . ¬ ( s p ( ¬ p r e ) a ) ∨ s p ⊥ a \operatorname{dslpOf}\ sp := \lambda\ pre\ a.\neg(sp\ (\neg pre)\ a) \lor sp\mathbin{\bot}a dslpOf s p := λ p r e a .¬ ( s p ( ¬ p r e ) a ) ∨ s p ⊥ a
In aspOf s p \operatorname{aspOf}\ sp aspOf s p , the second conjunct, ¬ ( s p ⊥ a ) \neg(sp\mathbin{\bot}a) ¬ ( s p ⊥ a ) , states that a a a is produced, and the first, s p p r e a sp\ pre\ a s p p r e a , states that a a a being produced implies p r e pre p r e holding; together they admit a a a exactly when a a a is produced and p r e pre p r e holds, which is the behavior of an a s p asp a s p predicate transformer. In dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p , the first disjunct, ¬ ( s p ( ¬ p r e ) a ) \neg(sp\ (\neg pre)\ a) ¬ ( s p ( ¬ p r e ) a ) , negates the statement that if a a a is produced, then ¬ p r e \neg pre ¬ p r e holds. Classically, this means that a a a is produced and p r e pre p r e holds. The second disjunct, s p ⊥ a sp\mathbin{\bot}a s p ⊥ a , states that a a a is not produced. Together, the two disjuncts admit a a a exactly when the implication “if a a a is produced, then p r e pre p r e holds” is true, as required by a d s l p dslp d s l p predicate transformer. As for S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(\mathrm{A}) SP DSP ( A ) , these readings are classical. The bind of S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) SP ASLP ( A ) is the disjunction of the binds of the components:
bind A S L P m f : = λ p r e b . bind A S P ( aspOf m ) ( λ a . aspOf ( f a ) ) p r e b ∨ bind D S L P ( dslpOf m ) ( λ a . dslpOf ( f a ) ) p r e b . \operatorname{bind}_{\mathrm{ASLP}}\ m\ f := \lambda\ pre\ b.\ \operatorname{bind}_{\mathrm{ASP}}\ (\operatorname{aspOf}\ m)\ (\lambda a.\operatorname{aspOf}\ (f\ a))\ pre\ b \lor
\operatorname{bind}_{\mathrm{DSLP}}\ (\operatorname{dslpOf}\ m)\ (\lambda a.\operatorname{dslpOf}\ (f\ a))\ pre\ b. bind ASLP m f := λ p r e b . bind ASP ( aspOf m ) ( λa . aspOf ( f a )) p r e b ∨ bind DSLP ( dslpOf m ) ( λa . dslpOf ( f a )) p r e b .
Lemma 9. For every s p : S P A S L P ( A ) sp : \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) s p : SP ASLP ( A ) , p r e : P pre : \mathbb{P} p r e : P , and a : A a : \mathrm{A} a : A ,
s p p r e a ⟺ aspOf s p p r e a ∨ dslpOf s p p r e a . sp\ pre\ a \Longleftrightarrow\operatorname{aspOf}\ sp\ pre\ a \lor\operatorname{dslpOf}\ sp\ pre\ a. s p p r e a ⟺ aspOf s p p r e a ∨ dslpOf s p p r e a .
Proof. Suppose s p p r e a sp\ pre\ a s p p r e a . By excluded middle, either s p ⊥ a sp\mathbin{\bot}a s p ⊥ a holds, and then so does the second disjunct of dslpOf s p p r e a \operatorname{dslpOf}\ sp\ pre\ a dslpOf s p p r e a , or it does not, and then aspOf s p p r e a \operatorname{aspOf}\ sp\ pre\ a aspOf s p p r e a holds. Conversely, aspOf s p p r e a \operatorname{aspOf}\ sp\ pre\ a aspOf s p p r e a contains s p p r e a sp\ pre\ a s p p r e a as its first conjunct, and s p ⊥ a sp\mathbin{\bot}a s p ⊥ a gives s p p r e a sp\ pre\ a s p p r e a by monotonicity. It remains to derive s p p r e a sp\ pre\ a s p p r e a from ¬ ( s p ( ¬ p r e ) a ) \neg(sp\ (\neg pre)\ a) ¬ ( s p ( ¬ p r e ) a ) . Termination gives s p ⊤ a sp\ \top\ a s p ⊤ a , and by excluded middle ⊤ \top ⊤ is p r e ∨ ¬ p r e pre\lor\neg pre p r e ∨ ¬ p r e , so disjunctivity gives s p p r e a ∨ s p ( ¬ p r e ) a sp\ pre\ a\lor sp\ (\neg pre)\ a s p p r e a ∨ s p ( ¬ p r e ) a , whose second disjunct is ruled out.
Unlike the decomposition of S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(\mathrm{A}) SP DSP ( A ) , this one is classical in both directions.
Lemma 10. For every s p : S P A S L P ( A ) sp : \mathrm{SP}_{\mathrm{ASLP}}(\mathrm{A}) s p : SP ASLP ( A ) , aspOf s p \operatorname{aspOf}\ sp aspOf s p is strict and disjunctive, and dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p is terminating and conjunctive.
Proof. Both components are monotone because s p sp s p is and negation reverses implication. aspOf s p ⊥ a \operatorname{aspOf}\ sp\mathbin{\bot}a aspOf s p ⊥ a is s p ⊥ a ∧ ¬ ( s p ⊥ a ) sp\mathbin{\bot}a\land\neg(sp\mathbin{\bot}a) s p ⊥ a ∧ ¬ ( s p ⊥ a ) , which is false, so aspOf s p \operatorname{aspOf}\ sp aspOf s p is strict, and it is disjunctive because s p sp s p is and ¬ ( s p ⊥ a ) \neg(sp\mathbin{\bot}a) ¬ ( s p ⊥ a ) does not depend on the precondition; both arguments are constructive. dslpOf s p ⊤ a \operatorname{dslpOf}\ sp\ \top\ a dslpOf s p ⊤ a is ¬ ( s p ⊥ a ) ∨ s p ⊥ a \neg(sp\mathbin{\bot}a)\lor sp\mathbin{\bot}a ¬ ( s p ⊥ a ) ∨ s p ⊥ a , which holds by excluded middle, so dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p is terminating. For a non-empty family ( p i ) i ∈ I (p_i)_{i\in I} ( p i ) i ∈ I , excluded middle turns ∀ i . p i \forall i.\,p_i ∀ i . p i into ∃ i . ¬ p i \exists i.\,\neg p_i ∃ i . ¬ p i , so disjunctivity of s p sp s p turns ¬ ( s p ( ¬ ∀ i . p i ) a ) \neg(sp\ (\neg\forall i.\,p_i)\ a) ¬ ( s p ( ¬∀ i . p i ) a ) into ∀ i . ¬ ( s p ( ¬ p i ) a ) \forall i.\,\neg(sp\ (\neg p_i)\ a) ∀ i . ¬ ( s p ( ¬ p i ) a ) , and a case split, again by excluded middle, on whether s p ⊥ a sp\mathbin{\bot}a s p ⊥ a holds makes dslpOf s p \operatorname{dslpOf}\ sp dslpOf s p conjunctive.
The same definitions and proofs apply to the arrow components of Appendix B, with ⊤ \top ⊤ , ⊥ \bot ⊥ , and ¬ \lnot ¬ taken pointwise.
Monadic Relations Between the Base Monads
This appendix gives the monadic relations of the left diagram of Figure 1 . They are the relations of § 2.2.2 with propositions in place of predicates on states: the base w p wp w p monads are submonads of WPure ( A ) = ( A → P ) → P \operatorname{WPure}(A) = (A \to\mathbb{P}) \to\mathbb{P} WPure ( A ) = ( A → P ) → P , and the elements of the base s p sp s p monads have type P → ( A → P ) \mathbb{P} \to(A \to\mathbb{P}) P → ( A → P ) .
Implication. For w p wp w p , the pointwise order
w p 1 ⊑ w p 2 ⟺ ∀ p o s t . w p 1 ( p o s t ) ⇒ w p 2 ( p o s t ) wp_1 \sqsubseteq wp_2 \Longleftrightarrow\forall post.\ wp_1(post) \Rightarrow wp_2(post) w p 1 ⊑ w p 2 ⟺ ∀ p os t . w p 1 ( p os t ) ⇒ w p 2 ( p os t )
is a monadic relation between any two base w p wp w p monads, because they share ret \operatorname{ret} ret and their bind is monotone. For s p sp s p , the pointwise order
s p 1 ⊑ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⇒ s p 2 ( p r e ) ( a ) sp_1 \sqsubseteq sp_2 \Longleftrightarrow\forall pre\ a.\ sp_1(pre)(a) \Rightarrow sp_2(pre)(a) s p 1 ⊑ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⇒ s p 2 ( p r e ) ( a )
is a monadic relation without the components that it needs with state. Each implication of the left diagram goes from S P A S P \mathrm{SP}_{\mathrm{ASP}} SP ASP or S P D S P \mathrm{SP}_{\mathrm{DSP}} SP DSP to S P A S L P \mathrm{SP}_{\mathrm{ASLP}} SP ASLP or S P D S L P \mathrm{SP}_{\mathrm{DSLP}} SP DSLP , and without state every element of the former is below every element of the latter: by Lemma 5 , s p 1 ( p r e ) ( a ) sp_1(pre)(a) s p 1 ( p r e ) ( a ) implies p r e pre p r e , and by Lemma 6 , p r e pre p r e implies s p 2 ( p r e ) ( a ) sp_2(pre)(a) s p 2 ( p r e ) ( a ) . Both lemmas apply to S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) and S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) as well, since these coincide with S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) , as described next. The relation is therefore total between these monads, and so it is monadic.
Isomorphism. Without state, S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) and S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) have the same elements, ret \operatorname{ret} ret , and bind, and so do S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) (§ 2.2.3 ). The equality of s p s sps s p s ,
s p 1 ≡ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⟺ s p 2 ( p r e ) ( a ) , sp_1 \equiv sp_2 \Longleftrightarrow\forall pre\ a.\ sp_1(pre)(a) \Longleftrightarrow sp_2(pre)(a), s p 1 ≡ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⟺ s p 2 ( p r e ) ( a ) ,
is therefore a monadic relation between each pair, and it relates their elements one to one. It gives the implication in both directions: the double-headed arrows marked ≅ \cong ≅ in Figure 1 .
Contrapositive. For w p wp w p , the monadic relation
w p 1 ⋈ w p 2 ⟺ ∀ p o s t . w p 1 ( p o s t ) ⟺ ¬ w p 2 ( λ a . ¬ p o s t ( a ) ) wp_1 \mathbin{\bowtie} wp_2 \Longleftrightarrow\forall post.\ wp_1(post) \Longleftrightarrow\lnot wp_2(\lambda a.\ \lnot post(a)) w p 1 ⋈ w p 2 ⟺ ∀ p os t . w p 1 ( p os t ) ⟺ ¬ w p 2 ( λa . ¬ p os t ( a ))
relates W P A W P ( A ) \mathrm{WPA}_{\mathrm{WP}}(A) WPA WP ( A ) to W P D W L P ( A ) \mathrm{WPD}_{\mathrm{WLP}}(A) WPD WLP ( A ) , and also W P D W P ( A ) \mathrm{WPD}_{\mathrm{WP}}(A) WPD WP ( A ) to W P A W L P ( A ) \mathrm{WPA}_{\mathrm{WLP}}(A) WPA WLP ( A ) . For s p sp s p , the monadic relation
s p 1 ⋈ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⟺ ¬ s p 2 ( ¬ p r e ) ( a ) sp_1 \mathbin{\bowtie} sp_2 \Longleftrightarrow\forall pre\ a.\ sp_1(pre)(a) \Longleftrightarrow\lnot sp_2(\lnot pre)(a) s p 1 ⋈ s p 2 ⟺ ∀ p r e a . s p 1 ( p r e ) ( a ) ⟺ ¬ s p 2 ( ¬ p r e ) ( a )
relates S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) to S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) , and also S P D S P ( A ) \mathrm{SP}_{\mathrm{DSP}}(A) SP DSP ( A ) to S P A S L P ( A ) \mathrm{SP}_{\mathrm{ASLP}}(A) SP ASLP ( A ) . As with state, both pair elements one to one, and no components are needed.
Adjunction. The relations
L A s p w p : = ∀ p r e p o s t . ( s p ( p r e ) ⊑ p o s t ) ⟺ ( p r e ⇒ w p ( p o s t ) ) , \mathrm{L}_{\mathrm{A}}\ sp\ wp := \forall pre\ post.\ (sp(pre) \sqsubseteq post) \Longleftrightarrow(pre \Rightarrow wp(post)), L A s p w p := ∀ p r e p os t . ( s p ( p r e ) ⊑ p os t ) ⟺ ( p r e ⇒ w p ( p os t )) ,
R A s p w p : = ∀ p r e p o s t . ( w p ( p o s t ) ⇒ p r e ) ⟺ ( p o s t ⊑ s p ( p r e ) ) \mathrm{R}_{\mathrm{A}}\ sp\ wp := \forall pre\ post.\ (wp(post) \Rightarrow pre) \Longleftrightarrow(post \sqsubseteq sp(pre)) R A s p w p := ∀ p r e p os t . ( w p ( p os t ) ⇒ p r e ) ⟺ ( p os t ⊑ s p ( p r e ))
relate an s p sp s p and a w p wp w p that form a Galois connection, with the s p sp s p as the left adjoint in L L L and as the right adjoint in R R R . They are monadic relations between S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) and W P D W L P ( A ) \mathrm{WP}_{\mathrm{DWLP}}(A) WP DWLP ( A ) , and between W P A W P ( A ) \mathrm{WP}_{\mathrm{AWP}}(A) WP AWP ( A ) and S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) , respectively. Without state, an element of S P A S P ( A ) \mathrm{SP}_{\mathrm{ASP}}(A) SP ASP ( A ) or S P D S L P ( A ) \mathrm{SP}_{\mathrm{DSLP}}(A) SP DSLP ( A ) is determined by the outputs it produces, an element of Pred ( A ) \operatorname{Pred}(A) Pred ( A ) , so we prove both relations between W P u r e ( A ) \mathrm{W}^{\mathrm{Pure}}(A) W Pure ( A ) and Pred ( A ) \operatorname{Pred}(A) Pred ( A ) (§ 2.2.3 ).
Monad Morphisms with State and Exceptions
The exception monad transformer adds an exceptional outcome to a monad, ExcT ( M ) ( A ) : = M ( option A ) \operatorname{ExcT}(M)(A) := M(\operatorname{option} A) ExcT ( M ) ( A ) := M ( option A ) , where None \operatorname{None} None is the exception. We apply it below the state monad transformer, on both sides:
StT ( ExcT ( NDiv ) ) ( S , A ) = S → NDiv ( option ( A × S ) ) , \operatorname{StT}(\operatorname{ExcT}(\operatorname{NDiv}))(S,A) = S \to\operatorname{NDiv}(\operatorname{option}(A \times S)), StT ( ExcT ( NDiv )) ( S , A ) = S → NDiv ( option ( A × S )) ,
W S t E x c ( S , A ) = StT ( ExcT ( W P u r e ) ) ( S , A ) ≅ ( option ( A × S ) → P ) → ( S → P ) . \mathrm{WStExc}(S,A) = \operatorname{StT}(\operatorname{ExcT}(\mathrm{W}^{\mathrm{Pure}}))(S,A) \cong(\operatorname{option}(A \times S) \to\mathbb{P}) \to(S \to\mathbb{P}). WStExc ( S , A ) = StT ( ExcT ( W Pure )) ( S , A ) ≅ ( option ( A × S ) → P ) → ( S → P ) .
A computation maps a start state to a non-empty set of outcomes, each a result Some ( a , s ′ ) \operatorname{Some}(a,s') Some ( a , s ′ ) , an exception None \operatorname{None} None , which discards the state, or divergence, and the postconditions of the specifications also constrain the exception. Lifting the base w p wp w p morphisms of § 2.3.2 through both transformers applies them at each start state, at the type option ( A × S ) \operatorname{option}(A \times S) option ( A × S ) :
θ D W P S t E x c ( m ) : = λ p o s t s . ∀ o ∈ m s . match o with v ↦ p o s t v ∣ d i v ↦ ⊥ , θ D W L P S t E x c ( m ) : = λ p o s t s . ∀ o ∈ m s . match o with v ↦ p o s t v ∣ d i v ↦ ⊤ , θ A W P S t E x c ( m ) : = λ p o s t s . ∃ o ∈ m s . match o with v ↦ p o s t v ∣ d i v ↦ ⊥ , θ A W L P S t E x c ( m ) : = λ p o s t s . ∃ o ∈ m s . match o with v ↦ p o s t v ∣ d i v ↦ ⊤ . \begin{aligned}
\theta_{\mathrm{DWP}}^{\mathrm{StExc}}(m) &:= \lambda\ post\ s.\ \forall o \in m\ s.\ \operatorname{match}\ o\ \operatorname{with}\ v \mapsto post\ v \mid div \mapsto\bot,\\
\theta_{\mathrm{DWLP}}^{\mathrm{StExc}}(m) &:= \lambda\ post\ s.\ \forall o \in m\ s.\ \operatorname{match}\ o\ \operatorname{with}\ v \mapsto post\ v \mid div \mapsto\top,\\
\theta_{\mathrm{AWP}}^{\mathrm{StExc}}(m) &:= \lambda\ post\ s.\ \exists o \in m\ s.\ \operatorname{match}\ o\ \operatorname{with}\ v \mapsto post\ v \mid div \mapsto\bot,\\
\theta_{\mathrm{AWLP}}^{\mathrm{StExc}}(m) &:= \lambda\ post\ s.\ \exists o \in m\ s.\ \operatorname{match}\ o\ \operatorname{with}\ v \mapsto post\ v \mid div \mapsto\top.
\end{aligned} θ DWP StExc ( m ) θ DWLP StExc ( m ) θ AWP StExc ( m ) θ AWLP StExc ( m ) := λ p os t s . ∀ o ∈ m s . match o with v ↦ p os t v ∣ d i v ↦ ⊥ , := λ p os t s . ∀ o ∈ m s . match o with v ↦ p os t v ∣ d i v ↦ ⊤ , := λ p os t s . ∃ o ∈ m s . match o with v ↦ p os t v ∣ d i v ↦ ⊥ , := λ p os t s . ∃ o ∈ m s . match o with v ↦ p os t v ∣ d i v ↦ ⊤.
where v v v ranges over option ( A × S ) \operatorname{option}(A \times S) option ( A × S ) , a result or the exception. The exception and the state monad transformers both lift monad morphisms, so these are monad morphisms from StT ( ExcT ( NDiv ) ) ( S , A ) \operatorname{StT}(\operatorname{ExcT}(\operatorname{NDiv}))(S,A) StT ( ExcT ( NDiv )) ( S , A ) to W S t E x c ( S , A ) \mathrm{WStExc}(S,A) WStExc ( S , A ) .
On the s p sp s p side, the exception arrow transformer changes the output of an arrow, ExcArrT ( Arr ) ( B , A ) : = Arr ( B , option A ) \operatorname{ExcArrT}(\operatorname{Arr})(B,A) := \operatorname{Arr}(B,\operatorname{option} A) ExcArrT ( Arr ) ( B , A ) := Arr ( B , option A ) , and we apply it below the state arrow transformer, so that an arrow of StArrT ( ExcArrT ( Arr ) ) ( S , B , A ) \operatorname{StArrT}(\operatorname{ExcArrT}(\operatorname{Arr}))(S,B,A) StArrT ( ExcArrT ( Arr )) ( S , B , A ) is an arrow of Arr \operatorname{Arr} Arr from B × S B \times S B × S to option ( A × S ) \operatorname{option}(A \times S) option ( A × S ) . Lifting the base arrow morphisms of § 2.3.2 through both transformers and fixing the input to 1 1 1 gives, for a computation m m m of StT ( ExcT ( NDiv ) ) ( S , A ) \operatorname{StT}(\operatorname{ExcT}(\operatorname{NDiv}))(S,A) StT ( ExcT ( NDiv )) ( S , A ) ,
θ A S P S t E x c ( m ) : = λ p r e v . ∃ s . ∃ o ∈ m s . p r e s ∧ o = v , θ D S L P S t E x c ( m ) : = λ p r e v . ∀ s . ∀ o ∈ m s . o = v ⇒ p r e s , θ D S P S t E x c ( m ) : = λ p r e v . θ A S P S t E x c ( m ) p r e v ∧ θ D S L P S t E x c ( m ) p r e v , θ A S L P S t E x c ( m ) : = λ p r e v . θ A S P S t E x c ( m ) p r e v ∨ θ D S L P S t E x c ( m ) p r e v . \begin{aligned}
\theta_{\mathrm{ASP}}^{\mathrm{StExc}}(m) &:= \lambda\ pre\ v.\ \exists s.\ \exists o \in m\ s.\ pre\ s \land o = v,\\
\theta_{\mathrm{DSLP}}^{\mathrm{StExc}}(m) &:= \lambda\ pre\ v.\ \forall s.\ \forall o \in m\ s.\ o = v \Rightarrow pre\ s,\\
\theta_{\mathrm{DSP}}^{\mathrm{StExc}}(m) &:= \lambda\ pre\ v.\ \theta_{\mathrm{ASP}}^{\mathrm{StExc}}(m)\ pre\ v \land\theta_{\mathrm{DSLP}}^{\mathrm{StExc}}(m)\ pre\ v,\\
\theta_{\mathrm{ASLP}}^{\mathrm{StExc}}(m) &:= \lambda\ pre\ v.\ \theta_{\mathrm{ASP}}^{\mathrm{StExc}}(m)\ pre\ v \lor\theta_{\mathrm{DSLP}}^{\mathrm{StExc}}(m)\ pre\ v.
\end{aligned} θ ASP StExc ( m ) θ DSLP StExc ( m ) θ DSP StExc ( m ) θ ASLP StExc ( m ) := λ p r e v . ∃ s . ∃ o ∈ m s . p r e s ∧ o = v , := λ p r e v . ∀ s . ∀ o ∈ m s . o = v ⇒ p r e s , := λ p r e v . θ ASP StExc ( m ) p r e v ∧ θ DSLP StExc ( m ) p r e v , := λ p r e v . θ ASP StExc ( m ) p r e v ∨ θ DSLP StExc ( m ) p r e v .
where v v v ranges over option ( A × S ) \operatorname{option}(A \times S) option ( A × S ) , so postconditions also constrain the exception, and divergence is never an output. These are the definitions of Eq. (5) with a result or the exception in place of a result, and they are monad morphisms because both arrow transformers lift arrow morphisms.
No Embedding for the Over-Approximate d s l p dslp d s l p Triple
We prove the second part of Lemma 2. The over-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St triple, θ D S L P S t ( m ) ( p r e ) ⊑ p o s t \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m)(pre) \sqsubseteq post θ DSLP St ( m ) ( p r e ) ⊑ p os t , has no embedding either: there is no element w w w of S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St such that, for every m m m , θ D S L P S t ( m ) ⊑ w \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m) \sqsubseteq w θ DSLP St ( m ) ⊑ w holds exactly when the triple does. The argument is dual to the one for the incorrectness triple in § 2.3.4 , with conjunctivity in place of disjunctivity. An element w w w of S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St satisfies
w ( p ) = ⋀ s ∉ p w ( S ∖ { s } ) , w(p) = \bigwedge_{s \notin p} w(S \setminus\{s\}), w ( p ) = s ∈ / p ⋀ w ( S ∖ { s }) ,
where the empty meet, at p = S p = S p = S , is ⊤ \top ⊤ by termination. So w w w commits to the start state from which each result it excludes is reached, whereas the triple only asks that each result outside p o s t post p os t be reachable from some state outside p r e pre p r e .
For example, let p r e = S ∖ { s 0 , s 1 } pre = S \setminus\{s_0, s_1\} p r e = S ∖ { s 0 , s 1 } and let p o s t post p os t contain every result except r 0 r_0 r 0 . A result whose start states all lie in p r e pre p r e must then differ from r 0 r_0 r 0 , so the triple holds exactly when r 0 r_0 r 0 is reachable from s 0 s_0 s 0 or s 1 s_1 s 1 . A program m 1 m_1 m 1 whose only result is r 0 r_0 r 0 , reached only from s 0 s_0 s 0 , and a program m 2 m_2 m 2 whose only result is r 0 r_0 r 0 , reached only from s 1 s_1 s 1 , both satisfy the triple, so such a w w w would satisfy θ D S L P S t ( m 1 ) ⊑ w \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m_1) \sqsubseteq w θ DSLP St ( m 1 ) ⊑ w and θ D S L P S t ( m 2 ) ⊑ w \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m_2) \sqsubseteq w θ DSLP St ( m 2 ) ⊑ w . At a precondition S ∖ { s } S \setminus\{s\} S ∖ { s } , one of the two lower bounds is always ⊤ \top ⊤ : at r 0 r_0 r 0 they are s 0 ≠ s s_0 \ne s s 0 = s and s 1 ≠ s s_1 \ne s s 1 = s , and at every other result both hold vacuously. So w ( S ∖ { s } ) = ⊤ w(S \setminus\{s\}) = \top w ( S ∖ { s }) = ⊤ , and hence w = ⊤ w = \top w = ⊤ by conjunctivity. But θ D S L P S t ( m ) ⊑ ⊤ \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m) \sqsubseteq\top θ DSLP St ( m ) ⊑ ⊤ holds for every m m m , so the triple would have to hold for every program. It does not: a program that always diverges reaches no result, so r 0 r_0 r 0 is not reachable from s 0 s_0 s 0 or s 1 s_1 s 1 .
This is also an instance of the contrapositive relation of § 2.2.2 . The dual w ‾ ( p ) : = ¬ w ( ¬ p ) \overline{w}(p) := \neg w(\neg p) w ( p ) := ¬ w ( ¬ p ) of an element of S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St is an element of S P A S P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{ASP}} SP ASP St , duality reverses the order and maps θ D S L P S t ( m ) \theta^{\mathrm{St}}_{\mathrm{DSLP}}(m) θ DSLP St ( m ) to θ A S P S t ( m ) \theta^{\mathrm{St}}_{\mathrm{ASP}}(m) θ ASP St ( m ) , and the over-approximate S P D S L P S t \mathrm{SP}^{\mathrm{St}}_{\mathrm{DSLP}} SP DSLP St triple for ( p r e , p o s t ) (pre, post) ( p r e , p os t ) holds exactly when the incorrectness triple for ( ¬ p r e , ¬ p o s t ) (\neg pre, \neg post) ( ¬ p r e , ¬ p os t ) does. So the dual of an embedding for the first would be an embedding for the second; with ¬ p r e = { s 0 , s 1 } \neg pre = \{s_0, s_1\} ¬ p r e = { s 0 , s 1 } and ¬ p o s t = { r 0 } \neg post = \{r_0\} ¬ p os t = { r 0 } , this is the example of § 2.3.4 .