Relational Reasoning for Markov Chains in a Probabilistic Guarded Lambda Calculus
Alejandro Aguirre Gilles Barthe Lars Birkedal Aleš Bizjak Marco Gaboardi Deepak Garg




IMDEA Software Institute
           
 
           Aarhus University
           University at Buffalo, SUNY
           MPI-SWS







Relational Reasoning for Markov Chains in a Probabilistic Guarded Lambda Calculus
    Alejandro Aguirre1^()
        Gilles Barthe1
        Lars Birkedal2
        Ales̆ Bizjak2
        
 Marco Gaboardi3
        Deepak Garg4
    
============================================================================================================================================






We extend the simply-typed guarded λ-calculus with discrete
probabilities and endow it with a program logic for reasoning about
relational properties of guarded probabilistic computations.  This
provides a framework for programming and reasoning about infinite
stochastic processes like Markov chains.  We demonstrate the logic
sound by interpreting its judgements in the topos of trees and by
using probabilistic couplings for the semantics of relational
assertions over distributions on discrete types.

The program logic is designed to support syntax-directed proofs in the
style of relational refinement types, but retains the expressiveness
of higher-order logic extended with discrete distributions, and the
ability to reason relationally about expressions that have different
types or syntactic structure. In addition, our proof system leverages
a well-known theorem from the coupling literature to justify better
proof rules for relational reasoning about probabilistic
expressions. We illustrate these benefits with a broad range of
examples that were beyond the scope of previous systems, including
shift couplings and lump couplings between random walks.






§ INTRODUCTION


Stochastic processes are often used in mathematics, physics, biology
or finance to model evolution of systems with uncertainty. In
particular, Markov chains are “memoryless” stochastic processes, in
the sense that the evolution of the system depends only on the current
state and not on its history. Perhaps the most emblematic example of a
(discrete time) Markov chain is the simple random walk over the
integers, that starts at 0, and that on each step moves one position
either left or right with uniform probability.  Let p_i be the
position at time i. Then, this Markov chain can be described as:

    p_0 = 0     
         p_i+1 = 
           p_i + 1 with probability 1/2 
    
           p_i - 1 with probability 1/2


The goal of this paper is to develop a programming and reasoning
framework for probabilistic computations over infinite objects, such
as Markov chains. Although programming and reasoning frameworks for
infinite objects and probabilistic computations are well-understood in
isolation, their combination is challenging. In particular, one must
develop a proof system that is powerful enough for proving interesting
properties of probabilistic computations over infinite objects, and
practical enough to support effective verification of these
properties.




  
*Modelling probabilistic infinite objects
A first challenge is to model probabilistic infinite objects. We focus
on the case of Markov chains, due to its importance. A (discrete-time)
Markov chain is a sequence of random variables {X_i} over some
fixed type T satisfying some independence property. Thus, the
straightforward way of modelling a Markov chain is as a stream
  of distributions over T. Going back to the simple example
outlined above, it is natural to think about this kind of
discrete-time Markov chain as characterized by the sequence of
positions {p_i}_i ∈, which in turn can be described as an
infinite set indexed by the natural numbers. This suggests that a
natural way to model such a Markov chain is to use streams in
which each element is produced probabilistically from the
previous one. However, there are some downsides to this
representation. First of all, it requires explicit reasoning about
probabilistic dependency, since X_i+1 depends on X_i. Also, we
might be interested in global properties of the executions of the
Markov chain, such as “The probability of passing through the initial
state infinitely many times is 1”. These properties are naturally
expressed as properties of the whole stream.  For these reasons, we
want to represent Markov chains as distributions over
  streams. Seemingly, one downside of this representation is that the
set of streams is not countable, which suggests the need for
introducing heavy measure-theoretic machinery in the semantics of the
programming language, even when the underlying type is discrete or
finite.

Fortunately, measure-theoretic machinery can be avoided
(for discrete distributions) by developing a probabilistic extension
of the simply-typed guarded λ-calculus and giving a semantic
interpretation in the topos of trees <cit.>. Informally, the
simply-typed guarded λ-calculus <cit.> extends the
simply-typed lambda calculus with a later modality, denoted by
. The type A ascribes expressions that are available
one unit of logical time in the future. The  modality allows
one to model infinite types by using finite approximations. For example, a stream of natural numbers
is represented by the sequence of its (increasing) prefixes in the
topos of trees. The prefix containing the first i elements has the
type S_i ××…×^(i-1), representing that the first element is
available now, the second element a unit time in the future, and so
on. This is the key to representing probability distributions over
infinite objects without measure-theoretic semantics: We model
probability distributions over non-discrete sets as discrete
distributions over their (the sets') approximations. For example, a
distribution over streams of natural numbers (which a priori would
be non-discrete since the set of streams is uncountable) would be
modelled by a sequence of distributions over the finite
approximations S_1, S_2, … of streams. Importantly, since each
S_i is countable, each of these distributions can be
discrete.


As Alejandro also noted after his example that I commented out,
  it's unclear what's so specific or special about the 
  modality. It seems that we could use any approximating
  sequence of discrete sets to avoid the issue with
  measure-theory. What's special about  and guards in our
  context?
the use of  and guards just allow us to use types to
  ensure productivity (and contractiveness to ensure existence of
  fixed points)
My understanding is that there is nothing special but we need in the logic to have a way to talk about these approximations, and
   and guards gives us a natural way to do this. no?











































































  
*Reasoning about probabilistic computations
Probabilistic computations exhibit a rich set of properties. One
natural class of properties is related to probabilities of events,
saying, for instance, that the probability of some event E (or of an
indexed family of events) increases at every iteration. However,
several interesting properties of probabilistic computation, such as
stochastic dominance or convergence (defined below) are relational, in
the sense that they refer to two runs of two processes. In principle,
both classes of properties can be proved using a higher-order logic
for probabilistic expressions, e.g.  the internal logic of the topos
of trees, suitably extended with an axiomatization of finite
distributions. However, we contend that an alternative approach
inspired from refinement types is desirable and provides better
support for effective verification. More specifically, reasoning in a
higher-order logic, e.g.  in the internal logic of the topos of trees,
does not exploit the structure of programs for non-relational
reasoning, nor the structural similarities between programs for
relational reasoning. As a consequence, reasoning is more involved.
To address this issue, we define a relational proof system that
exploits the structure of the expressions and supports syntax-directed
proofs, with necessary provisions for escaping the syntax-directed
discipline when the expressions do not have the same structure. The
proof system manipulates judgements of the form:


    ΔΣΓΨt_1A_1t_2A_2ϕ


where Δ and Γ are two typing contexts, Σ and
Ψ respectively denote sets of assertions over variables in these
two contexts, t_1 and t_2 are well-typed expressions of type A_1
and A_2, and ϕ is an assertion that may contain the special
variables  and  that respectively correspond to
the values of t_1 and t_2. The context Δ and Γ, the
terms t_1 and t_2 and the types A_1 and A_2 provide a
specification, while Σ, Ψ, and ϕ are useful for
reasoning about relational properties over t_1,t_2, their inputs and
their outputs. This form of judgement is similar to that of Relational
Higher-Order Logic <cit.>, from which our system draws inspiration.

In more detail, our relational logic comes with typing rules that
allow one to reason about relational properties by exploiting as much
as possible the syntactic similarities between t_1 and t_2, and to
fall back on pure logical reasoning when these are not available. In
order to apply relational reasoning to guarded computations the logic
provides relational rules for the later modality  and for a
related modality □, called “constant”. These rules allow
the relational verification of general relational properties that go
beyond the traditional notion of program equivalence and, moreover,
they allow the verification of properties of guarded computations over
different types. The ability to reason about computations of different
types provides significant benefits over alternative formalisms for
relational reasoning. For example, it enables reasoning about
relations between programs working on different data structures,
e.g. a relation between a program working on a stream of natural
numbers, and a program working on a stream of pairs of natural
numbers, or having different structures, e.g. a relation between
an application and a case expression.


Importantly, our approach for reasoning formally about probabilistic
computations is based on probabilistic couplings, a standard
tool from the analysis of Markov chains <cit.>.
From a verification perspective, probabilistic couplings go beyond
equivalence properties of probabilistic programs, which have been
studied extensively in the verification literature, and yet support
compositional reasoning <cit.>. The main
attractive feature of coupling-based reasoning is that it limits the
need of explicitly reasoning about the probabilities—this avoids
complex verification conditions. We provide sound proof rules for
reasoning about probabilistic couplings. Our rules make several
improvements over prior relational verification logics based on
couplings. First, we support reasoning over probabilistic processes of
different types. Second, we use Strassen's
theorem <cit.> a remarkable result about
probabilistic couplings, to achieve greater expressivity. Previous
systems required to prove a bijection between the sampling spaces to
show the existence of a coupling <cit.>,
Strassen's theorem gives a way to show their existence which is
applicable in settings where the bijection-based approach cannot be
applied.  And third, we support reasoning with what are called shift
couplings, coupling which permits to relate the states of two Markov
chains at possibly different times (more explanations below).













  
*Case studies
We show the flexibility of our formalism by verifying several examples
of relational properties of probabilistic computations, and Markov
chains in particular. These examples cannot be verified with existing
approaches.

First, we verify a classic example of probabilistic non-interference
which requires the reasoning about computations at different
types. Second, in the context of Markov chains, we verify an example
about stochastic dominance which exercises our more general rule for
proving the existence of couplings modelled by expressions of
different types. Finally, we verify an example involving shift
relations in an infinite computation. This style of reasoning is
motivated by shift couplings in Markov chains. In
contrast to a standard coupling, which relates the states of two
Markov chains at the same time t, a shift coupling relates the
states of two Markov chains at possibly different times. Our specific
example relates a standard random walk (described earlier) to a
variant called a lazy random walk; the verification requires relating
the state of standard random walk at time t to the state of the lazy
random walk at time 2t. We note that this kind of reasoning is
impossible with conventional relational proof rules even in a
non-probabilistic setting. Therefore, we provide a novel family of
proof rules for reasoning about shift relations. At a high level, the
rules combine a careful treatment of the later and constant modalities
with a refined treatment of fixpoint operators, allowing us to relate
different iterates of function bodies.




 §.§ Summary of contributions

With the aim of providing a general framework for programming and
reasoning about Markov chains, the three main contributions of this work are:

  
  * A probabilistic extension of the guarded λ-calculus,
    that enables the definition of Markov chains as discrete
    probability distributions over streams.
  
  * A relational logic based on coupling to reason in a
    syntax-directed manner about (relational) properties of Markov
    chains. This logic supports reasoning about programs that have
    different types and structures. Additionally, this logic uses results from the
    coupling literature to achieve greater expressivity than previous
    systems.
  
  * An extension of the relational logic that allows to relate the
    states of two streams at possibly different times. This extension
    supports reasoning principles, such as shift couplings, that escape
    conventional relational logics.
  
 


























































































































































§ MATHEMATICAL PRELIMINARIES

This section reviews the definition of discrete probability
sub-distributions and introduces mathematical couplings.

Let C be a discrete (i.e., finite or countable) set. A (total)
distribution over C is a function μ : C → [0,1] such that ∑_x∈ Cμ(x) = 1 . The support of a distribution μ is the
set of points with non-zero probability, μ{x ∈ C
|μ(x) > 0 } . We denote the set of distributions over C as
(C). Given a subset E ⊆ C, the probability of
sampling from μ a point in E is denoted _x←μ[x
  ∈ E], and is equal to ∑_x ∈ Eμ(x).




Let μ be a distribution over a product space C_1× C_2. The
first (second) marginal of μ is another distribution
(π_1)(μ) ((π_2)(μ)) over C_1 (C_2) defined
as:

    (π_1)(μ)(x) = ∑_y ∈ C_2μ(x,y)     ((π_2)(μ)(y) = ∑_x ∈ C_1μ(x,y) )





  §.§.§ Probabilistic couplings

Probabilistic couplings are a fundamental tool in the analysis of
Markov chains. When analyzing
a relation between two probability distributions it is sometimes useful to
consider instead a distribution over the product space that somehow “couples”
the randomness in a convenient manner.

Consider for instance the case of the following Markov chain, which
counts the total amount of tails observed when
tossing repeatedly a biased coin with probability of tails p:

    n_0 = 0     
         n_i+1 = {[ n_i + 1 with probability p; n_i with probability (1-p) ].

If we have two biased coins with probabilities of tails p and q with p≤ q and we
respectively observe {n_i} and {m_i} we would expect that, in
some sense, n_i ≤ m_i should hold for all i (this property is
known as stochastic dominance).  A formal proof of this fact using
elementary tools from probability theory would require to compute the
cumulative distribution functions for n_i and m_i and then to
compare them. The coupling method reduces this proof to showing a way
to pair the coin flips so that if the first coin shows tails, so does
the second coin.















We now review the definition of couplings and state relevant
properties.

Let μ_1∈(C_1) and μ_2∈(C_2), and 
R⊆ C_1× C_2.


  * A distribution μ∈(C_1× C_2) is a coupling for
  μ_1 and μ_2 iff its first and second marginals coincide with
  μ_1 and μ_2 respectively, i.e.  (π_1)(μ)=μ_1
  and (π_2)(μ)=μ_2.


  * A distribution μ∈(C_1× C_2) is a R-coupling
  for μ_1 and μ_2 if it is a coupling for μ_1 and μ_2
  and, moreover, _(x_1,x_2)←μ [R x_1 x_2]=1, i.e., if
  the support of the distribution μ is included in R.


Moreover, we write μ_1μ_2R iff there exists a
R-coupling for μ_1 and μ_2.






Couplings always exist. For instance, the product distribution of two
distributions is always a coupling.





Going back to the example about the two coins, it can be proven by computation
that the following is a coupling that lifts the less-or-equal relation
(0 indicating heads and 1 indicating tails):

    {[                      (0,0)  w/ prob (1-q) (0,1)  w/ prob (q-p);                          (1,0)  w/ prob 0     (1,1)  w/ prob p ].


The following theorem in <cit.> gives a
necessary and sufficient condition for the existence of R-couplings
between two distributions. The theorem is remarkable in the sense that
it proves an equivalence between an existential property (namely the
existence of a particular coupling) and a universal property
(checking, for each event, an inequality between probabilities).

  Consider μ_1∈(C_1) and μ_2∈(C_2), and R⊆
  C_1 × C_2. Then μ_1μ_2R iff for every
  X ⊆ C_1, _x_1←μ_1[x_1∈ X] ≤_x_2←μ_2[x_2∈ R(X)], where R(X) is the image
  of X under R, i.e.  R(X) ={ y ∈ C_2 |∃ x ∈
  X. R x y}.



























An important property of couplings is closure under sequential
composition.





Let μ_1∈(C_1), μ_2∈(C_2), M_1:C_1→(D_1) and M_2:C_2→(D_2). Moreover, let
R⊆ C_1× C_2 and S⊆ D_1× D_2. Assume:
(1) μ_1μ_2R; and
(2) for every x_1∈ C_1 and x_2∈ C_2 such that R x_1 x_2,
  we have M_1(x_1)M_2(x_2)S.
Then (𝖻𝗂𝗇𝖽 μ_1 M_1)(𝖻𝗂𝗇𝖽 μ_2 M_2)S,
where 𝖻𝗂𝗇𝖽 μ M is defined as

    (𝖻𝗂𝗇𝖽 μ M)(y) =∑_x μ(x) · M(x)(y)











We conclude this section with the following lemma, which follows from
Strassen's theorem:

Let R⊆ C_1 × C_2, E_1⊆ C_1 and E_2⊆
C_2 such that for every x_1∈ E_1 and x_2∈ C_2, R x_1 x_2
implies x_2∈ E_2, i.e.  R(E_1)⊆ E_2. Moreover, let
μ_1∈(C_1) and μ_2∈(C_2) such that
μ_1μ_2R.  Then

    _x_1←μ_1 [x_1∈ E_1] ≤_x_2←μ_2 [x_2∈ E_2]



This lemma can be used to prove probabilistic inequalities
from the existence of suitable couplings:


Let μ_1,μ_2∈(C):

  
  * If μ_1μ_2(=), then for all x∈ C, μ_1(x) = μ_2(x).
    
  
  * If C = and μ_1μ_2(≥), then for all n∈,
    _x←μ_1[x≥ n] ≥_x←μ_2[x≥ n]



In the example at the beginning of the section, the property we want
to prove is precisely that, for every k and i, the following
holds:

    _x_1← n_i [x_1 ≥ k] ≤_x_2← m_i [x_2 ≥ k]

Since we have a ≤-coupling, this proof is immediate.
This example is formalized in <ref>.




§ OVERVIEW OF THE SYSTEM



In this section we give a high-level overview of our system, with the
details on sections <ref>, <ref> and <ref>.
We start by presenting the base logic, and then we show how to extend it with
probabilities and how to build a relational reasoning system on top of it.



 §.§ Base logic: Guarded Higher-Order Logic


Our starting point is the Guarded Higher-Order Logic <cit.>
(Guarded HOL)
inspired by the topos of
trees. In addition to the usual constructs of HOL to reason about lambda
terms, this logic features the  and □ modalities to reason
about infinite terms, in particular streams. The  modality is used
to reason about objects that will be available in the future, such as tails
of streams. For instance, suppose we want to define an (s,ϕ) predicate, expressing that
all elements of a stream s ≡nxs satisfy a property ϕ. This can be axiomatized as
follows:


    ∀ (xs: ) (n : ). ϕ n ⇒[s
      xs](s, x. ϕ)⇒(nxs, x. ϕ)


We use x. ϕ to denote that the formula ϕ depends on a free variable
x, which will get replaced by the first argument of .
We have two antecedents. The first one states that the head n satisfies
ϕ. The second one, [s xs](s, x. ϕ), states that all
elements of xs satisfy ϕ. Formally, xs is the tail of the stream and will
be available in the future, so it has type .




The delayed substitution ▹[s xs] replaces s of type
 with xs of type  inside  and shifts the
whole formula one step into the future.
In other words, [s xs](s, x. ϕ)
states that (-, x.ϕ) will be satisfied by xs in the future, once
it is available.

















 §.§ A system for relational reasoning




When proving relational properties it
is often convenient to build proofs guided by the syntactic structure
of the two expressions to be related. This style of reasoning is
particularly appealing when the two expressions have the same
structure and control-flow, and is appealingly close to the
traditional style of reasoning supported by refinement types. At the
same time, 
a strict
adherence to the syntax-directed discipline is detrimental to the
expressiveness of the system; for instance, it makes it difficult or
even impossible to reason about structurally dissimilar terms. To
achieve the best of both worlds, we present a relational proof system built
on top of Guarded HOL, which we call Guarded RHOL. Judgements have the shape:


    ΔΣΓΨt_1A_1t_2A_2ϕ


where ϕ is a logical formula that may contain two distinguished
variables  and  that respectively represent the
expressions t_1 and t_2. This judgement subsumes two typing
judgements on t_1 and t_2 and a relation ϕ on these two
expressions. However, this form of judgement does not tie the logical
property to the type of the expressions, and is key to achieving
flexibility while supporting syntax-directed proofs whenever needed.
The proof system combines rules of two different flavours: two-sided
rules, which relate expressions with the same top-level constructs,
and one-sided rules, which operate on a single expression.

We then extend Guarded HOL with a modality ♢
that lifts assertions over discrete types C_1 and C_2 to assertions over
(C_1) and (C_2).






Concretely, we define for every assertion ϕ, variables
x_1 and x_2 of type C_1 and C_2 respectively, and expressions
t_1 and t_2 of type (C_1) and (C_2) respectively,
the modal assertion ♢_ [x_1← t_1,x_2← t_2]ϕ which holds iff the interpretations of t_1 and t_2 are
related by the probabilistic lifting of the interpretation of
ϕ. We call this new logic Probabilistic Guarded HOL.








We accordingly extend the relational proof system to support reasoning
about probabilistic expressions by adding judgements of the form:

    ΔΣΓΨt_1(C_1)t_2(C_2)♢_[x_1←, x_2←]ϕ

expressing that t_1 and t_2 are distributions related by a
ϕ-coupling. We call this proof system Probabilistic Guarded RHOL.


These judgements can be built by using the following rule,
that lifts relational judgements over discrete types
C_1 and C_2 to judgements over distribution types (C_1)
and (C_2) when the premises of Strassen's theorem are
satisfied.

    [ COUPLING]ΔΣΓΨt_1(C_1)t_2(C_2)♢_[y_1←_1, y_2←_2]ϕΔΣΓΨ∀ X_1 ⊆ C_1.
            _y_1 t_1 [y_1∈ X_1] ≤_y_2 t_2 [∃ y_1 ∈ X_1. ϕ]





Recall that (discrete time) Markov chains are “memoryless”
probabilistic processes, whose specification is given by a (discrete)
set C of states, an initial state s_0 and a probabilistic
transition function 𝗌𝗍𝖾𝗉:C →(C), where
(S) represents the set of discrete distributions over C.  As
explained in the introduction, a convenient modelling of Markov chains
is by means of probabilistic streams, i.e.  to model a Markov chain
as an element of (S), where S is its underlying state
space. To model Markov chains, we introduce a  operator with
type C → (C →(C)) →(C) that, given an
initial state and a transition function, returns a Markov chain. We
can reason about Markov chains by the Markov rule (the context, omitted,
does not change):


    *[right= Markov]
          t_1C_1t_2C_2ϕ
    
    h_1C_1 →(C_1)h_2C_2 →(C_2)ψ_3
    
    ⊢ψ_4markov(t_1,h_1)(D_1)markov(t_2,h_2)(D_2)♢_[y_1 
     y_2 ]ϕ'
     where ψ_3 ≡∀ x_1 x_2. ϕx_1x_2⇒♢_[ y_1  x_1, y_2  x_2]ϕy_1y_2
    ψ_4 ≡∀ x_1 x_2 xs_1 xs_2. ϕx_1x_2⇒[y_1  xs_1, y_2  xs_2]ϕ'⇒
          ϕ'y_1x_1xs_1y_2x_2xs_2

Informally, the rule stipulates the existence of an invariant ϕ
over states. The first premise insists that the invariant hold on the
initial states, the condition ψ_3 states that the transition
functions preserve the invariant, and ψ_4 states that the
invariant ϕ over pairs of states can be lifted to a stream
property ϕ'.





Other rules of the logic are given in
Figure <ref>. 




The language construct 𝗆𝗎𝗇𝗂𝗍 creates a point distribution
whose entire mass is at its argument. Accordingly, the [] rule creates a
straightforward coupling.

The [] rule internalizes sequential composition of
couplings (Lemma <ref>) into
the proof system. The construct xtt' composes a
distribution t with a probabilistic computation t' with one free
variable x by sampling x from t and running t'.

The [] rule supports one-sided reasoning about
xtt' and relies on the fact that couplings are closed
under convex combinations. Note that one premise of the rule uses a
unary judgement, with a non-relational modality
♢_[x←]ϕ whose informal meaning is that
ϕ holds with probability 1 in the distribution .

The following table summarizes the different base logics we consider,the
relational systems we build on top of them, including the ones presented in <cit.>, and the equivalences between both sides:



Relational logic          Base logic 
       

  [       RHOL <cit.>; Γ|Ψ⊢ t_1 ∼ t_2 |ϕ ]
                        <cit.>⟺ 	   
  [  HOL <cit.>; Γ|Ψ⊢ϕt_1t_2 ] 


  [    Guarded RHOL <ref>; Δ|Σ|Γ|Ψ⊢ t_1 ∼ t_2 |ϕ ]
                        Thm <ref>⟺ 	   
  [ Guarded HOL <cit.>;    Δ|Σ|Γ|Ψ⊢ϕt_1t_2 ] 


  [    Probabilistic Guarded RHOL <ref>; Δ|Σ|Γ|Ψ⊢ t_1 ∼ t_2 |♢_[y_1 , y_2].ϕ ]
                        
                      Thm <ref>⟺
                       	   
  [  Probabilistic Guarded HOL <ref>; Δ|Σ|Γ|Ψ⊢♢_[y_1  t_1, y_2  t_2].ϕ ]





















 §.§ Examples

We formalize elementary examples from the literature on security and
Markov chains. None of these examples can be verified in prior
systems. Uniformity of one-time pad and lumping of random
  walks cannot even be stated in prior systems because the two
related expressions in these examples have different types. The
random walk vs lazy random walk (shift coupling) cannot be
proved in prior systems because it requires either asynchronous
reasoning or code rewriting.  Finally, the biased coin example
(stochastic dominance) cannot be proved in prior work because it
requires Strassen's formulation of the existence of coupling (rather
than a bijection-based formulation) or code rewriting.  We give
additional details below. 



  §.§.§ One-time pad/probabilistic non-interference

Non-interference <cit.> is a baseline information flow
policy that is often used to model confidentiality of computations. In
its simplest form, non-interference distinguishes between public (or
low) and private (or high) variables and expressions, and requires
that the result of a public expression not depend on the value of its
private parameters. This definition naturally extends to probabilistic
expressions, except that in this case the evaluation of an expression
yields a distribution rather than a value. There are deep connections
between probabilistic non-interference and several notions of
(information-theoretic) security from cryptography. In this paragraph,
we illustrate different flavours of security properties for one-time
pad encryption. Similar reasoning can be carried out for proving
(passive) security of secure multiparty computation algorithms in the
3-party or multi-party setting <cit.>.

One-time pad is a perfectly secure symmetric encryption scheme. Its
space of plaintexts, ciphertexts and keys is the set
{0,1}^ℓ—fixed-length bitstrings of size ℓ. The
encryption algorithm is parametrized by a key k—sampled uniformly
over the set of bitstrings { 0,1 }^ℓ—and maps every plaintext
m to the ciphertext c = k ⊕ m, where the operator ⊕
denotes bitwise exclusive-or on bitstrings. We let 𝗈𝗍𝗉
denote the expression λ
m. k𝒰_{0,1}^ℓk⊕ m, where
𝒰_X is the uniform distribution over a finite set X.

One-time pad achieves perfect security, i.e.  the distributions of
ciphertexts is independent of the plaintext. Perfect security can be
captured as a probabilistic non-interference property: 


    𝗈𝗍𝗉{ 0,1 }^ℓ→({ 0, 1
      }^ℓ)𝗈𝗍𝗉{ 0,1 }^ℓ→({ 0, 1 }^ℓ)∀ m_1m_2.
    
    
    
    
       m_1 ♢= m_2


where e_1 ♢= e_2 is used as a shorthand for
♢_[y_1← e_1, y_2← e_2] y_1 = y_2. The
crux of the proof is to establish


    m_1,m_2: { 0,1 }^ℓ𝒰_{0,1}^ℓ({ 0, 1 }^ℓ)𝒰_{0,1}^ℓ({ 0, 1 }^ℓ)⊕ m_2 ♢=⊕ m_1


using the [] rule. It suffices to observe that the
assertion induces a bijection, so the image of an arbitrary set X
under the relation has the same cardinality as X, and hence their
probabilities w.r.t.  the uniform distributions are equal. One can
then conclude the proof by applying the rules for monadic
sequenciation (MLET) and abstraction (rule ABS in appendix), using
algebraic properties of ⊕.

Interestingly, one can prove a stronger property: rather than proving
that the ciphertext is independent of the plaintext, one can prove
that the distribution of ciphertexts is uniform. This is captured by
the following judgement: 


    c_1, c_2: { 0,1 }^ℓ𝗈𝗍𝗉{ 0,1 }^ℓ→({ 0, 1 }^ℓ)𝗈𝗍𝗉{ 0,1 }^ℓ→({ 0, 1 }^ℓ)ψ

where
ψ∀ m_1 m_2. m_1=m_2⇒♢_[y_1← m_1, y_2← m_2] 
  y_1=c_1  ⇔ y_2=c_2.

This style of modelling uniformity as a relational property is
inspired from <cit.>. The proof is similar to the
previous one and omitted. However, it is arguably more natural to
model uniformity of the distribution of ciphertexts by the judgement:


    𝗈𝗍𝗉{ 0,1 }^ℓ→({ 0, 1 }^ℓ)𝒰_{0,1}^ℓ({ 0, 1 }^ℓ)∀ m.  m ♢=


This judgement is closer to the simulation-based notion of security
that is used pervasively in cryptography, and notably in Universal
Composability <cit.>. Specifically, the
statement captures the fact that the one-time pad algorithm can be
simulated without access to the message.  It is interesting to note
that the judgement above (and more generally simulation-based security)
could not be expressed in prior works, since the two expressions of
the judgement have different types—note that in this specific case,
the right expression is a distribution but in the general case the
right expression will also be a function, and its domain will be a
projection of the domain of the left expression.



The proof proceeds as follows. First, we prove

    𝒰_{0,1}^ℓ𝒰_{0,1}^ℓ∀ m. ♢_[y_1←, y_2←]
      y_1 ⊕ m = y_2


using the [] rule. Then, we apply the []
rule to obtain


    [ k𝒰_{0,1}^ℓ
    k⊕ m ][ k𝒰_{0,1}^ℓ
    k ]♢_[y_1←,
          y_2←] y_1 = y_2


We have k𝒰_{0,1}^ℓk≡𝒰_{0,1}^ℓ; hence by equivalence (rule
Equiv in appendix), this entails


    k𝒰_{0,1}^ℓk⊕ m𝒰_{0,1}^ℓ♢_[y_1←, y_2←] y_1 = y_2


We conclude by applying the one-sided rule for abstraction.





  §.§.§ Stochastic dominance

Stochastic dominance defines a partial order between random variables
whose underlying set is itself a partial order; it has many different
applications in statistical biology (e.g. in the analysis of the
birth-and-death processes), statistical physics (e.g. in percolation
theory), and economics. First-order stochastic dominance, which we
define below, is also an important application of probabilistic
couplings. We demonstrate how to use our proof system for proving
(first-order) stochastic dominance for a simple Markov process which
samples biased coins. While the example is elementary, the proof
method extends to more complex examples of stochastic dominance, and
illustrates the benefits of Strassen's formulation of the coupling
rule over alternative formulations stipulating the existence of
bijections (explained later).

We start by recalling the definition of (first-order) stochastic
dominance for the ℕ-valued case. The definition extends to
arbitrary partial orders.

Let μ_1,μ_2∈(ℕ). We say that μ_2
stochastically dominates μ_1, written μ_1≤_SDμ_2, iff for every n∈ℕ,


    _x←μ_1[x≥ n] ≤_x←μ_2[x≥ n]



The following result, equivalent to <ref>,

characterizes stochastic dominance using probabilistic couplings.

Let μ_1,μ_2∈(ℕ). Then μ_1≤_SDμ_2 iff μ_1μ_2(≤).





We now turn to the definition of the Markov chain. For p∈ [0,1],
we consider the parametric ℕ-valued Markov chain
𝖼𝗈𝗂𝗇𝗌(0,h), with initial state 0 and
(parametric) step function:


    h λ x. bpx+b


where, for p ∈ [0,1], p is the Bernoulli distribution on
{0,1} with probability p for 1 and 1-p for 0. Our goal is
to establish that 𝖼𝗈𝗂𝗇𝗌 is monotonic, i.e.  for every
p_1,p_2∈ [0,1], p_1≤ p_2 implies 𝖼𝗈𝗂𝗇𝗌 p_1
≤_SD𝖼𝗈𝗂𝗇𝗌 p_2. We formalize this statement as


    𝖼𝗈𝗂𝗇𝗌[0,1] →(ℕ)𝖼𝗈𝗂𝗇𝗌[0,1] →(ℕ)ψ

          
where ψ∀ p_1,p_2. p_1≤ p_2 ⇒♢_[y_1 , y_2 ](y_1, y_2, z_1.z_2.z_1≤ z_2).

The crux of the proof is to establish stochastic dominance for the
Bernoulli distribution:


    p_1:[0,1],p_2:[0,1]| p_1≤ p_2 p_1(ℕ)p_2(ℕ)♢≤













where we use e_1 ♢≤ e_2 as shorthand for
♢_[y_1  e_1, y_2  e_2] y_1≤ y_2. This is proved
directly by the COUPLING rule and checking by simple
calculations that the premise of the rule is valid.

We briefly explain how to conclude the proof. Let h_1 and h_2 be
the step functions for p_1 and p_2 respectively. It is clear from
the above that (context omitted):


    x_1≤ x_2 h_1 x_1()h_2 x_2()♢_[y_1 , y_2 ].
               y_1≤ y_2


and by the definition of :


    x_1 ≤ x_2 ⇒(xs_1,xs_2,z_1.z_2.z_1≤ z_2)⇒(x_1 xs_1, x_2 xs_2, z_1.z_2.z_1≤ z_2)


So, we can conclude by applying the Markov rule.

It is instructive to compare our proof with prior formalizations, and
in particular with the proof in <cit.>. Their proof is
carried out in the  logic, whose [] rule
is based on the existence of a bijection that satisfies some property,
rather than on our formalization based on Strassen's Theorem. Their
rule is motivated by applications in cryptography, and works well for
many examples, but is inconvenient for our example at hand, which
involves non-uniform probabilities. Indeed, their proof is based on
code rewriting, and is done in two steps. First, they prove equivalence between sampling and
returning x_1 from p_1; and sampling z_1 from
p_2, z_2 from p_1p_2 and returning
z= z_1  z_2. Then, they find a coupling between z and
p_2.




  §.§.§ Shift coupling: random walk vs lazy random walk

The previous example is an instance of a lockstep coupling, in that it
relates the k-th element of the first chain with the k-th element
of the second chain. Many examples from the literature follow this
lockstep pattern; however, it is not always possible to establish
lockstep couplings. Shift couplings are a relaxation of lockstep
couplings where we relate elements of the first and second chains
without the requirement that their positions coincide.

We consider a simple example that motivates the use of shift
couplings.  Consider the random walk and lazy random walk (which, at
each time step, either chooses to move or stay put), both defined as
Markov chains over ℤ. For simplicity, assume that both
walks start at position 0. It is not immediate to find a coupling
between the two walks, since the two walks necessarily get
desynchronized whenever the lazy walk stays put. Instead, the trick is
to consider a lazy random walk that moves two steps instead of
one. The random walk and the lazy random walk of step 2 are defined by
the step functions:


    [                         step                                          λ x.z𝒰_{-1,1}z+x;                       lstep2                              λ x.z𝒰_{-1,1}b𝒰_{0,1}x+2*z*b ]

After 2 iterations of step, the position has either
changed two steps to the left or to the right, or has returned to the
initial position, which is the same behaviour lstep2
has on every iteration.  Therefore, the coupling we want to find should
equate the elements at position 2i in step with the
elements at position i in lstep2. The details on how
to prove the existence of this coupling are in <ref>.



  §.§.§ Lumped coupling: random walks on 3 and 4 dimensions

A Markov chain is recurrent if it has probability 1 of
returning to its initial state, and transient otherwise. It is
relatively easy to show that the random walk over ℤ is
recurrent. One can also show that the random walk over ℤ^2
is recurrent.  However, the random walk over ℤ^3 is
transient.














For higher dimensions, we can use a coupling
argument to prove transience. Specifically, we can define a coupling
between a lazy random walk in n dimensions and a random walk in n
+m dimensions, and derive transience of the latter from transience of
the former. We define the (lazy) random walks below, and sketch the
coupling arguments.

Specifically, we show here the particular case of the transience of
the 4-dimensional random walk from the transience of the 3-dimensional
lazy random walk.  We start by defining the stepping functions:


    [                                           step_4          : ℤ^4 →(ℤ^4)
      λ z_1. x_1U_4z_1 +_4 x_1;                                          lstep_3 : ℤ^3 →(ℤ^3)
      λ z_2. x_2U_3b_234z_2 +_3 b_2*x_2 ]


where U_i={(± 1,0,… 0), …, (0,…,0,± 1)} are the
vectors of the basis of ℤ^i and their opposites.  Then, the
random walk of dimension 4 is modelled by rwalk4(0, step_4), and the lazy walk of dimension 3 is
modelled by lwalk3(0, step_3).  We want to prove:


    rwalk4(ℤ^4)lwalk3(ℤ^3)♢_[y_1←
     y_2←](y_1, y_2, z_1.z_2.pr^4_3(z_1) = z_2)


where pr^n_2_n_1 denotes the standard projection from
ℤ^n_2 to ℤ^n_1. 

We apply the Markov rule. The only interesting premise requires proving that the transition function preserves the coupling:

    p_2=pr^4_3(p_1) ⊢step_4∼lstep_3
                        |∀ x_1 x_2. x_2=pr^4_3(x_1) ⇒♢_[y_1 x_1 
     y_2 x_2]pr^4_3(y_1)=y_2













To prove this, we need to find the appropriate coupling, i.e., one
that preserves the equality.  The idea is that the step in
ℤ^3 must be the projection of the step in
ℤ^4. This corresponds to the following judgement:


    .[                λ z_1. x_1U_4
       z_1 +_4 x_1 ]∼[                             λ z_2. x_2U_3
       b_234
       z_2 +_3 b_2*x_2 ] |  [ ∀ z_1 z_2. pr^4_3(z_1) = z_2 ⇒;            pr^4_3( z_1) ♢= z_2 ]


which by simple equational reasoning is the same as


    .[                λ z_1. x_1U_4
       z_1 +_4 x_1 ]∼[                                 λ z_2. p_2U_3×34
       z_2 +_3 π_1(p_2)*π_2(p_2) ] |  [ ∀ z_1 z_2. pr^4_3(z_1) = z_2 ⇒;            pr^4_3( z_1) ♢= z_2 ]


We want to build a coupling such that if we sample (0,0,0,1) or
(0,0,0,-1) from U_3, then we sample 0 from
34, and otherwise if we sample
(x_1,x_2,x_3,0) from U_4, we sample (x_1,x_2,x_3) from
U_3. Formally, we prove this with the Coupling rule.
Given X:U_4 →, by simple computation we show that:

    _z_1 ∼U_4[z_1 ∈ X] ≤_z_2 ∼U_3×34[z_2 ∈{ y |∃ x ∈ X.  pr^4_3(x) = π_1(y)*π_2(y) }]














This concludes the proof. From the previous example, it follows that
the lazy walk in 3 dimensions is transient, since the random walk in
3 dimensions is transient. By simple reasoning, we now conclude that
the random walk in 4 dimensions is also transient.































































































































§ PROBABILISTIC GUARDED LAMBDA CALCULUS


































To ensure that a function on infinite datatypes is well-defined, one
must check that it is productive. This means that any finite
prefix of the output can be computed in finite time. For instance,
consider the following function on streams:

    𝚕𝚎𝚝𝚛𝚎𝚌 𝚋𝚊𝚍 (𝚡:𝚡𝚜) = 𝚡 : 𝚝𝚊𝚒𝚕 (𝚋𝚊𝚍 𝚡𝚜)

This function is not productive since only the first element can be computed. We
can argue this as follows:

Suppose that the tail of a stream is available one unit of time after its head,
and that that  is available at time 0. How much time does it take
for  to start outputting its tail? Assume it takes k units of
time. This means that  will be available at time k+1 ,
since  is only available at time 1. But  is
exactly the tail of , and this is a contradiction, since
 is available at time 0 and therefore the tail of
 should be available at time k. Therefore, the tail of
 will never be available.

The guarded lambda calculus solves the productivity problem by
distinguishing at type level between data that is available now and
data that will be available in the future, and restricting when
fixpoints can be defined.  Specifically, the guarded lambda calculus
extends the usual simply typed lambda calculus with two modalities:
 (pronounced later) and □
(constant).  The later modality represents data that will be
available one step in the future, and is introduced and removed by the
term formers  and  respectively.  This modality is
used to guard recursive occurrences, so for the calculus to remain
productive, we must restrict when it can be eliminated.  This is
achieved via the constant modality, which expresses that all the data
is available at all times.  


In the remainder of this section we present a probabilistic extension
of this calculus.




  
*Syntax
Types of the calculus are defined by the grammar

    A,B ::= b || 
      A × B | A + B  | A → B |A|□ A |A|(C)

where b ranges over a collection of base types.

A is the type of guarded streams of elements of type
A. Formally, the type A is isomorphic to A ×A. This isomorphism gives a way to introduce streams
with the function () : A →A→A and to eliminate them with the functions : A→ A
and : A→A.


(C) is the type of distributions over discrete types
C.  Discrete types are defined by the following grammar, where b_0
are discrete base types, e.g., .



    C,D ::= b_0 || 
      C × D | C + D |C | C.

Note that, in particular, arrow types are not discrete but streams
are. This is due to the semantics of streams as sets of finite
approximations, which we describe in the next subsection. Also note
that □A is not discrete since it makes the full infinite
streams available.

We also need to distinguish between arbitrary types A, B and
constant types S, T, which are defined by the following grammar

    S, T ::= b_C || 
      S × T | S + T  | S → T |□ A

where b_C is a collection of constant base types.  Note in
particular that for any type A the type □ A is constant.

The terms of the language t are defined by the following grammar

    t    ::=  x
            | c
            | 0 | S t |ttt|μ|t|xtt
       |⟨ t, t ⟩|π_1 t |π_2 t
            |1 t |2 t | t 1 x . t; 2 y . t
            |λ x . t | t t |xt
       |tts| t | t
            |t|xtt|xtt|[ξ]t | t

where ξ is a delayed substitution, a sequence of bindings
x_1  t_1, …, x_n  t_n.


The terms c are constants corresponding to the base types used and
t and xtt are the introduction and sequencing
construct for probability distributions.  The meta-variable μ
stands for base distributions like C and p.

Delayed substitutions were introduced in <cit.> in a dependent type theory to be able to work with types dependent on terms of type A.
In the setting of a simple type theory, such as the one considered in this paper, delayed substitutions are equivalent to having the applicative structure <cit.>  for the  modality.
However, delayed substitutions extend uniformly to the level of propositions, and thus we choose to use them in this paper in place of the applicative structure.



  
*Denotational semantics

The meaning of terms is given by a denotational model in the category  of presheaves over ω, the first infinite ordinal.
This category  is also known as the topos of trees <cit.>.
In previous work <cit.>, it was shown how to model most of the constructions of the guarded lambda calculus and its internal logic, with the notable exception of the probabilistic features.
Below we give an elementary

presentation of the semantics.

Informally, the idea behind the topos of trees is to represent
(infinite) objects from their finite approximations, which we observe
incrementally as time passes.  Given an object x, we can consider a
sequence {x_i} of its finite approximations observable at time
i. These are trivial for finite objects, such as a natural number,
since for any number n, n_i = n at every i.  But for infinite
objects such as streams, the ith approximation is the prefix of
length i+1.

Concretely, the category  consists of:

  
  * Objects X: families of sets {X_i}_i∈ together with restriction functions r_n^X : X_n+1→ X_n.
  We will write simply r_n if X is clear from the context.
  
  * Morphisms X → Y : families of functions α_n : X_n → Y_n commuting with restriction functions in the sense of r_n^Y ∘α_n+1 = α_n ∘ r_n^X.



The full interpretation of types of the calculus can be found in
<ref> in the appendix. The main points we want to
highlight are:

  
  * Streams over a type A are interpreted as sequences of finite
    prefixes of elements of A with the restriction functions of A:
    
    AA_0 ×{*}A_1 ×A_0
        A_2×A_1 ←⋯


  
  * Distributions over a discrete object C are
    defined as a sequence of distributions over each
    C_i: 
    (C)(C_0)
        (r_0)⟵(C_1)
        (r_1)⟵(C_2)
        (r_2)⟵…,
 where (C_i) is
    the set of (probability density) functions μ : C_i → [0,1] such that ∑_x_∈ Xμ
    x = 1, and (r_i) adds the probability density of all the points
    in C_i+1 that are sent by r_i to the same point in the C_i. In other words,
    (r_i)(μ)(x) = _y μ[r_i(y) = x]


































































An important property of the interpretation is that discrete types are interpreted as objects X such that X_i is finite or countably infinite for every i.
This allows us to define distributions on these objects without the need for measure theory.
In particular, the type of guarded streams A is discrete provided A is, which is clear from the interpretation of the type A.
Conceptually this holds because A_i is an approximation of real streams, consisting of only the first i+1 elements.

An object X of  is constant if all its restriction
functions are bijections.  Constant types are interpreted as constant
objects of  and for a constant type A

the objects □ A and A are isomorphic in .



  
*Typing rules

Terms are typed under a dual context Δ|Γ, where Γ is a usual context that binds variables to a type, and Δ is a constant context containing variables bound to types that are constant.
The term xut allows us to shift variables between constant and non-constant contexts. The typing rules can be found in <ref>.

The semantics of such a dual context Δ|Γ is given as the product of types in Δ and Γ, except that we implicitly add □ in front of every type in Δ.
In the particular case when both contexts are empty, the semantics of the dual context correspond to the terminal object 1, which is the singleton set {∗} at each time. 



The interpretation of the well-typed term Δ|Γ⊢ t : A is
defined by induction on the typing derivation, and can be found in
<ref> in the appendix.




























































  
*Applicative structure of the later modality

As in previous work we can define the operator  satisfying the typing rule

    Δ|Γ⊢ t : (A → B)Δ|Γ⊢ u : AΔ|Γ⊢ t  u : B

and the equation
  (t)  (u) ≡(t u)
as the term
  t  u [f  t, x  u]f x.



















  
*Example: Modelling Markov chains






































As an application of  and an example of how to use guardedness
and probabilities together, we now give the precise
definition of the  construct that we used to model Markov chains earlier:

    [                                                              : C  → (C →(C)) →(C);                                          fλ x. λ h. 
         zh xtswap_^C(f  z  h)xt ]

The guardedness condition gives f the type ( C → (C →(C)) →(C)) in the body of the
fixpoint. Therefore, it needs to be applied functorially (via )
to z and h, which gives us a term of type (C). To complete the definition we need to build a term
of type (C) and then sequence it with 
to build a term of type (C). To achieve this, we use the
primitive operator swap_^C :
(C) →( C), which witnesses the isomorphism
between (C) and ( C). For this isomorphism
to exist, it is crucial that distributions be total (i.e., we cannot
use subdistributions). Indeed, the denotation for (C) is
the sequence {∗}(C_1) (C_2) …,
while the denotation for ( C) is the sequence
({∗}) (C_1) (C_2) …, and
{∗} is isomorphic to ({∗}) in Set only if
 considers only total distributions.

























§ GUARDED HIGHER-ORDER LOGIC




We now introduce Guarded HOL (GHOL), which is a higher-order logic to reason about terms of the guarded lambda calculus.
The 
logic is essentially that of <cit.>, but presented with the dual context formulation analogous to the dual-context typing judgement of the guarded lambda calculus.
Compared to standard intuitionistic higher-order logic, the logic GHOL has two additional constructs, corresponding to additional constructs in the guarded lambda calculus.
These are the later modality () on propositions, with delayed substitutions,
which expresses that a proposition holds one time unit into the future, and the “always”
modality □, which expresses that a proposition holds at all times.
Formulas are defined by the grammar:

    ϕ, ψ ::= ⊤|ϕ∧ψ|ϕ∨ψ|ψ|∀ x. ϕ|∃ x. ϕ|[x_1  t_1 … x_n
       t_n]ϕ|□ϕ

The basic judgement of the logic is Δ|Σ|Γ|Ψ⊢ϕ where Σ is a logical context for Δ (that is, a list of formulas well-formed in Δ) and Ψ is another logical context for the dual context Δ|Γ.
The formulas in context Σ must be constant propositions.
We say that a proposition ϕ is constant if it is well-typed in context Δ|· and moreover if every occurrence of the later modality in ϕ is under the □ modality.
Selected rules are displayed in Figure <ref> on page fig:hol. We highlight Loeb induction, which is the key to reasoning about fixpoints: to prove that ϕ holds now, one
can assume that it holds in the future. 
The interpretation of the formula Δ|Γ⊢ϕ is a subobject of the interpretation Δ|Γ.
Concretely the interpretation A of Δ|Γ⊢ϕ is a family {A_i}_i=0^∞ of sets such that A_i ⊆Δ|Γ_i.
This family must satisfy the property that if x ∈ A_i+1 then r_i(x) ∈ A_i where r_i are the restriction functions of Δ|Γ.
The interpretation of formulas is defined by induction on the typing derivation.
In the interpretation of the context Δ|Σ|Γ|Ψ the formulas in Σ are interpreted with the added □ modality.
Moreover all formulas ϕ in Σ are typeable in the context Δ|·⊢ϕ and thus their interpretations are subsets of □Δ.
We treat these subsets of Δ|Γ in the obvious way.

The cases for the semantics of the judgement Δ|Γ⊢ϕ can be found in the appendix.

It can be shown that this logic is sound with respect to its model in the topos of trees.


  The semantics of guarded higher-order logic is sound: if Δ|Σ|Γ|Ψ⊢ϕ is derivable then for all n ∈, □Σ_n ∩Ψ_n ⊆ϕ.



In addition, Guarded HOL is expressive enough to axiomatize standard probabilities over discrete sets. 
This axiomatization can be used to define the ♢ modality directly in Guarded HOL (as opposed to our relational proof system,
were we use it as a primitive).
Furthermore, we can derive from this axiomatization additional rules to reason about couplings, which can be seen
in Figure <ref>. These rules will be the key to proving the soundness of the probabilistic fragment of the relational proof system, and can
be shown to be sound themselves.


  The additional rules are sound.
















§ RELATIONAL PROOF SYSTEM



We complete the formal description of the system by describing the
proof rules for the non-probabilistic fragment of the relational proof
system (the rules of the probabilistic fragment were described in
Section <ref>).



 §.§ Proof rules






































The rules for core λ-calculus constructs are
identical to those of <cit.>; for convenience, we present a selection
of the main rules in Figure <ref> in the appendix.




We briefly comment on the two-sided rules for the new constructs
(Figure <ref>).

The notation Ω abbreviates a context Δ|Σ|Γ|Ψ.
The rule Next relates two terms that have a  term
constructor at the top level. We require that both have one term in
the delayed substitutions and that they are related pairwise.  Then
this relation is used to prove another relation between the main
terms. This rule can be generalized to terms with more than one term
in the delayed substitution.

The rule Prev proves a relation between terms from the same delayed relation by
applying prev to both terms.

The rule Box proves a relation between two boxed terms if the
same relation can be proven in a constant context.

Dually, LetBox uses a relation between two boxed terms to prove
a relation between their unboxings.

LetConst is similar to LetBox, but it requires instead a relation
between two constant terms, rather than explicitly □-ed terms.

The rule Fix relates two fixpoints following the Loeb rule from Guarded HOL.
Notice that in the premise, the fixpoints need to appear in the delayed substitution so that
the inductive hypothesis is well-formed.

The rule Cons 
proves relations on streams

from relations between their heads and tails, while
Head and Tail behave as converses of Cons.




Figure <ref> contains the one-sided versions of the rules.
We only present the left-sided versions as the right-sided versions
are completely symmetric. The rule Next-L relates at ϕ a
term that has a  with a term that does not have a
. First, a unary property ϕ' is proven on the term u in
the delayed substitution, and it is then used as a premise to prove
ϕ on the terms with delays removed. Rules for proving unary
judgements can be found in the appendix.

Similarly, LetBox-L proves a unary property on the term that gets unboxed
and then uses it as a precondition.

The rule Fix-L builds a fixpoint just on the left, and relates it with
an arbitrary term t_2 at a property ϕ. Since ϕ may contain the
variable  which is not in the context, it has to be replaced when
adding ϕ to the logical context in the premise of the rule.

The remaining rules are similar to their two-sided counterparts.







































































































































 §.§ Metatheory

We review some of the most interesting metatheoretical properties of
our relational proof system, highlighting the equivalence with Guarded HOL.
 
  For all contexts Δ,Γ; types σ_1,σ_2; terms t_1,t_2; sets of assertions Σ,Ψ,;
  and assertions ϕ:
    
    ΔΣΓΨt_1σ_1t_2σ_2ϕ  ⟺  ΔΣΓΨϕt_1t_2


The forward implication follows by induction on the given derivation.
The reverse implication is immediate from the rule which allows to
fall back on Guarded HOL in relational proofs. (Rule SUB in the appendix).
The full proof is in the appendix. The consequence of this theorem is
that the syntax-directed, relational proof system we have built 
on top of Guarded HOL does not lose expressiveness.

The intended semantics of a judgement
ΔΣΓΨt_1A_1t_2A_2ϕ is
that, for every valuation δΔ, γΓ,
if Σ(δ) and Ψ(δ,γ), then


    ϕ(δ,γ[t_1(δ,γ),
      t_2(δ,γ)])


Since Guarded HOL is sound with respect to its semantics in
the topos of trees, and our relational proof system is equivalent to
Guarded HOL, we obtain that our relational proof system is also sound in
the topos of trees.


If ΔΣΓΨt_1σ_2t_2σ_2ϕ, then for every valuation
δΔ, γΓ:

    [                                           Δ⊢□Σ(δ) ∧Δ|Γ⊢Ψ(δ,γ) ⇒; Δ|Γ,:σ_1, :σ_2 ⊢ϕ
             (δ, γ[Δ|Γ⊢ t_1(δ,γ)][Δ|Γ⊢ t_2(δ,γ)]) ]

In particular, there is no proof of
Δ∅Γ∅t_1σ_1t_2σ_2.




























 §.§ Shift couplings revisited


We give further details on how to prove the example with shift couplings from Section <ref>.
(Additional examples of relational reasoning on non-probabilistic streams can be
found in the appendix.)






























































































































































Recall the step functions:


    [                         step                                          λ x.z𝒰_{-1,1}z+x;                       lstep2                              λ x.z𝒰_{-1,1}b𝒰_{0,1}x+2*z*b ]


We axiomatize the predicate _2,1, which relates the element at position 2i in one stream to the element at position i in another stream, as follows.

    [                         ∀ x_1 x_2 xs_1 xs_2 y_1. ϕx_1z_1x_2z_2⇒; [ys_1 xs_1][zs_1  ys_1, ys_2  xs_2]_2,1(zs_1, ys_2, z_1.z_2.ϕ)⇒;                            _2,1(x_1y_1xs_1,x_2xs_2, z_1.z_2. ϕ) ]

In fact, we can assume that, in general, we have a family of
_m_1, m_2 predicates relating two streams at positions
m_1· i and m_2· i for every i.

We can now express the existence of a shift coupling by the statement:


    p_1 = p_2 ⊢(p_1, step) ∼(p_2,
      lstep2) |♢_[ y_1 
     y_2 ]_2,1(y_1,y_2,z_1.z_2.z_1=z_2)





For the proof, we need to introduce an asynchronous rule for Markov chains:

    *[right= Markov-2-1]
          ΔΣΓΨt_1C_1t_2C_2ϕ
    
    [ ΔΣΓΨ(λ x_1. x'_1h_1 x_1h_1 x'_1)C_1→(C_1)h_2C_2→(C_2)
    ∀ x_1 x_2. ϕz_1x_1z_2x_2⇒♢_[ z_1  x_1, z_2  x_2]ϕ ][ ΔΣΓΨ(t_1,h_1)(C_1)(t_2,h_2)(C_2)
    ♢_[y_1 , y_2 ]_2,1(y_1,y_2,z_1.z_2.ϕ) ]























This asynchronous rule for Markov chains shares the motivations of the
rule for loops proposed in <cit.>. Note that one can define
a rule Markov-m-n for arbitrary m and n to prove a judgement
of the form _m,n on two Markov chains.






We show the proof of the shift coupling. By equational reasoning, we get:


    [                                                                λ x_1. x'_1h_1 x_1h_1 x'_1 ≡ λ x_1. z_1{-1,1}h_1 (z_1 + x_1) ≡;                                                                                 ≡ λ x_1. z_1{-1,1}z'_1{-1,1}z'_1 + z_1 + x'_1 ]


and the only interesting premise of Markov-2-1 is:


    .[                                       λ x_1. z_1{-1,1}
       z'_1{-1,1}
       z'_1 + z_1 + x'_1 ]∼[                                 λ x_2. z_2{-1,1}
       b_2{1,0}
       x_2+2*b_2*z_2 ] | [ ∀ x_1 x_2. x_1 = x_2 ⇒;             x_1 ♢= x_2 ]


Couplings between z_1 and z_2 and between z_1' and b_2 can be found by simple computations.
This completes the proof.
  







 
















§ RELATED WORK


Our probabilistic guarded λ-calculus and the associated logic
Guarded HOL build on top of the guarded λ-calculus and its internal 
logic <cit.>.  The guarded λ-calculus has been extended
to guarded dependent type theory <cit.>, which can be
understood as a theory of guarded refinement types and as a foundation
for proof assistants based on guarded type theory. These systems do
not reason about probabilities, and do not support syntax-directed
(relational) reasoning, both of which we support.

Relational models for higher-order programming languages are often
defined using logical relations. <cit.> showed how to use
second-order logic to define and reason about logical relations for
the second-order lambda calculus.  Recent work has extended this approach
to logical relations for higher-order programming languages with
computational effects such as nontermination, general references, and
concurrency <cit.>.  The
logics used in loc. cit. are related to our work in two ways:
(1) the logics in loc. cit. make use of the later modality for
reasoning about recursion, and (2) the models of the logics in
loc. cit. can in fact be defined using guarded type theory.
Our work is more closely related to Relational Higher Order
Logic <cit.>, which applies the idea of logic-enriched type
theories <cit.> to a relational setting. There
exist alternative approaches for reasoning about relational properties
of higher-order programs; for instance, <cit.> have
recently proposed to use monadic reification for reducing relational
verification of F^* to proof obligations in higher-order logic.











A series of work develops reasoning methods for probabilistic
higher-order programs for different variations of the lambda calculus.
One line of work has focused on operationally-based techniques for
reasoning about contextual equivalence of programs.  The methods are
based on probabilistic
bisimulations <cit.>
or on logical relations <cit.>.  Most of
these approaches have been developed for languages with discrete
distributions, but recently there has also been work on languages with
continuous
distributions <cit.>.
Another line of work has focused on denotational models, starting with
the seminal work in <cit.>.  Recent work
includes support for relational reasoning about equivalence of
programs with continuous distributions for a total programming
language <cit.>.  Our approach is most
closely related to prior work based on relational refinement types for
higher-order probabilistic programs. These were initially considered
by <cit.> for a stateful fragment of F^*, and later
by <cit.> for a pure language.  Both systems are
specialized to building probabilistic couplings; however, the latter
support approximate probabilistic couplings, which yield a natural
interpretation of differential privacy <cit.>, both in its
vanilla and approximate forms (i.e.  ϵ- and
(ϵ,δ)-privacy). Technically, approximate couplings are
modelled as a graded monad, where the index of the monad tracks the privacy budget (ϵ or (ϵ,δ)).
Both systems are strictly syntax-directed, and cannot reason about
computations that have different types or syntactic structures, while
our system can.









§ CONCLUSION



We have developed a probabilistic extension of the (simply typed)
guarded λ-calculus, and proposed a syntax-directed proof
system for relational verification. Moreover, we have verified a
series of examples that are beyond the reach of prior work. Finally,
we have proved the soundness of the proof system with respect to the
topos of trees.

There are several natural directions for future work. One first
direction is to enhance the expressiveness of the underlying simply
typed language. For instance, it would be interesting to introduce
clock variables and some type dependency as in <cit.>, and extend the proof system accordingly.
This would allow us, for example, to type the function taking the n-th element of a guarded stream,
which cannot be done in the current system.
Another exciting direction is to
consider approximate couplings, as in <cit.>,
and to develop differential privacy for infinite streams—preliminary
work in this direction, such as <cit.>, considers very
large lists, but not arbitrary streams. A final direction would be to
extend our approach to continuous distributions to support
other application domains.











  §.§.§ Acknowledgments.

We would like to thank the anonymous reviewers for their time and their helpful input.
This research was supported in part by the ModuRes Sapere Aude Advanced Grant from The Danish Council for Independent Research for the Natural Sciences (FNU),
 by a research grant (12386, Guarded Homotopy Type Theory) from
the VILLUM foundation, and by NSF under grant 1718220.








§ ADDITIONAL PROOF RULES






§ DENOTATIONAL SEMANTICS




 §.§ Types and terms in context


The meaning of terms is given by the denotational model in the category  of presheaves over ω, the first infinite ordinal.
This category  is also known as the topos of trees <cit.>.
In previous work <cit.> it was shown how to model most of the constructions of the guarded lambda calculus and the associated logic, with the notable exception of the probabilistic features.
Below we give an elementary and self-contained presentation of the semantics.

Concretely, objects X of  are families of sets X_i indexed over  together with functions r_n^X : X_n+1→ X_n.
These are called restriction functions.
We will write simply r_n if X is clear from the context.
Moreover if x ∈ X_i and j ≤ i we will write x_j for the element r_j(⋯ (r_i-1(x))⋯) ∈ X_j.
Morphisms X → Y are families of functions α_n : X_n → Y_n commuting with restriction functions in the sense of r_n^Y ∘α_n+1 = α_n ∘ r_n^X.
One can see the restriction function r_n : X_n+1→ X_n as mapping elements of X_n+1 to their approximations at time n.

Semantics of types can be found on <ref>, where
G(A) consists of sequences {x_n}_n ∈
such that x_i ∈A_i and r_i(x_i+1) = x_i for all i,
i.e., □A is the set of so-called global sections of
A.

 
The semantics of a dual context Δ|Γ is given as the product of types in Δ and Γ, except that we implicitly add □ in front of every type in Δ.
In the particular case when both contexts are empty, the semantics of the dual context correspond to the terminal object 1, which is the singleton set {∗} at each stage. 
A term in context Δ|Γ⊢ t : τ is interpreted as a family of functions t_n : Δ|Γ_n →τ_n commuting with restriction functions of Δ|Γ and τ.
Semantics of products,
coproducts, 
and natural numbers is pointwise as in sets, so we
omit writing it.  The cases for the other constructs are in
<ref> where 𝗆𝗎𝗇𝗂𝗍 and 𝗆𝗅𝖾𝗍
are the standard unit and bind operations on discrete probabilities,
i.e. 

    [                         c                         =                  λ y. c=y;           𝗆𝗅𝖾𝗍 x = μ 𝗂𝗇 M                         = λ y. ∑_c∈ Cμ(c) · M(c)(y) ]

The functions π_0 and π_1 are the first and second projections, respectively.
































 §.§ Equational theory of the calculus




The denotational semantics validates the following equational theory in addition to the standard equational theory of the simply typed lambda calculus with sums and natural numbers.

Rules for fixed points, always modality and streams

    ft     ≡   tf(ft)
    ( t)     ≡   t 
    x(u)t     ≡   txu
    xut     ≡   txu
     (xxs)      ≡   x 
     (xxs)      ≡   xs 
     t t     ≡   t

Rules for delayed substitutions

    [ξx t]u   ≡[ξ]u       if  x  not in  u
    [ξx t, y  sξ']u   ≡[ξy s, x  tξ']u
    [ξx[ξ]t]u   ≡[ξ](uxt)
    [x t]x    ≡ t

Monad laws for distributions

    xtu   ≡ u[t/x]
    xtx   ≡ t
    x_2(x_1t_1t_2)u   ≡x_1t_1(x_2t_2u)

In particular, notice that fix does not reduce as usual, but instead the whole term is delayed before the substitution is performed.





 §.§ Logical judgements



The cases for the semantics of the judgement Δ|Γ⊢ϕ of the non-probabilistic fragment are as follows (we omit writing the contexts if they are clear):

    ⊤_i    Δ|Γ_i
    ϕ∧ψ_i    ϕ_i ∩ψ_i
    ϕ∨ψ_i    ϕ_i ∪ψ_i
    ϕψ_i    { x ∀ j ≤ i, x_j ∈ϕ_j  x_j ∈ψ_j}
    ∀ x:A. ϕ_i    {(δ,γ) ∀ j ≤ i, ∀ x ∈A_j, (δ, (γ_j), x) ∈ϕ}
    [x_1  t_1, …, x_n  t_n]ϕ_i    { (δ, γ)  i > 0 (δ,γ_i-1, {t_k_i(δ,γ)}_k=1^n) ∈ϕ_i-1}
    □ϕ_i    { x ∀ j, x ∈ϕ_j}




§ ADDITIONAL BACKGROUND



One consequence of Strassen's theorem is
that couplings are closed under convex combinations.

Let (μ_i)_i∈ I and (ν_i)_i∈ I bet two families of
distributions on C_1 and C_2 respectively, and let (p_i)_i∈ I∈ [0,1] such that ∑_i∈ I p_i=1. If
μ_iν_iR for all i∈ I then (∑_i∈ I p_i
μ_i)(∑_i∈ I p_i ν_i)R, where the convex
combination ∑_i∈ I p_i μ_i is defined by the clause
(∑_i∈ I p_i μ_i)(x)=∑_i∈ I p_i μ_i(x).

One obtains an asymmetric version of the lemma by observing that if
μ_i=μ for every i∈ I, then (∑_i∈ I p_i μ_i)=μ.


One can also show that couplings are closed under relation
composition.

Let μ_1∈(C_1), μ_2∈(C_2), μ_3
∈(C_3). Moreover, let R⊆ C_1× C_2 and
S⊆ C_2× C_3. If μ_1μ_2R and
μ_2μ_3R then μ_1μ_3R.




§ PROOFS OF THE THEOREMS





 §.§ Proof of Theorem <ref>



The semantics of the guarded higher-order logic without the probabilistic fragment has been explained in previous work <cit.>.
Thus we focus on showing soundness of the additional rules for the diamond modality, which will be useful for proving soundness of the relational proof system.
Moreover we only describe soundness for the binary diamond modality, the soundness of the rules for the unary modality being entirely analogous.


Soundness of the rule 

  [MONO2]
  ΔΣΓΨ♢_[x_1t_1, x_2t_2]ψ
  ΔΣΓΨ♢_[x_1t_1, x_2 t_2]ϕ ΔΣΓ, x_1 : C_1, x_2 : C_2Ψ, ϕψ
  


Let n ∈ and (δ, γ) ∈Δ|Σ|Γ|Ψ_n.
Then from the first premise we have (δ, γ) ∈♢_[x_1 t_1, x_2  t_2]ϕ_n and thus there exists an {(v, u)  (δ, γ, v, u) ∈ϕ_n} coupling for the distributions t_1_n(δ,γ) and t_2_n(δ,γ).
But since (δ, γ) ∈Δ|Σ|Γ|Ψ_n we have from the second premise of the rule that

    {(v, u)  (δ, γ, v, u) ∈ϕ_n}⊆{(v, u)  (δ, γ, v, u) ∈ψ_n}

and thus any {(v, u)  (δ, γ, v, u) ∈ϕ_n} coupling is also an {(v, u)  (δ, γ, v, u) ∈ψ_n} coupling, which means there exists an {(v, u)  (δ, γ, v, u) ∈ψ_n} coupling for t_1_n(δ,γ) and t_2_n(δ,γ) as required.

Soundness of the rule 

  [UNIT2]
  ΔΣΓΨ ♢_[x_1←t_1, x_2←t_2] ϕ
  ΔΣΓΨ ϕ[t_1/x_1][t_2/x_2]

Let n ∈ and (δ, γ) ∈Δ|Σ|Γ|Ψ_n.
We need to show the existence of a

    {(v, u)  (δ, γ, v, u) ∈ϕ_n}

coupling for the point-mass distributions concentrated at t_1_n(δ,γ) and t_2_n(δ,γ).
The premise of the rule establishes the membership

    (t_1_n(δ,γ), t_2_n(δ,γ))
      ∈{(v, u)  (δ, γ, v, u) ∈ϕ_n}

and thus the point-mass distribution concentrated at (t_1_n(δ,γ), t_2_n(δ,γ)) is a required coupling.


Soundess of the rule 

[MLET2]
      ΔΣΓΨ
          ♢_[y_1←x_1t_1t'_1, y_2←x_2t_2t'_2] ψ
          ΔΣΓΨ♢_[x_1←t_1, x_2←t_2] ϕ
          ΔΣΓ,x_1:C_1,x_2:C_2Ψ,ϕ
            ♢_[y_1←t'_1, y_2←t'_2]ψ

Let n ∈ and (δ, γ) ∈Δ|Σ|Γ|Ψ_n.
Then from the first premise we have that there exists an

    {(v, u)  (δ, γ, v, u) ∈ϕ_n}

coupling for the distributions t_1_n(δ,γ) and t_2_n(δ,γ).

From the second premise we get that for every v, u such that (δ, γ, v, u) ∈ϕ_n
there exists an 

    {(v', u')  (δ, γ, v, u, v', u') ∈ψ_n}

coupling for t_1'_n(δ,γ, v) and t_2'_n(δ,γ, u).
Since x_1 and x_2 are fresh for ψ the relation

    {(v', u')  (δ, γ, v, u, v', u') ∈ψ_n}

is independent of v, u.

Thus Lemma <ref> instantiated with

    μ_1    = t_1_n(δ,γ)
    μ_2    = t_2_n(δ,γ)
    
      M_1    = t_1'_n(δ,γ, -)
    
      M_2    = t_2'_n(δ,γ, -)
    
      R    = {(v, u)  (δ, γ, v, u) ∈ϕ_n}
    
      S    = {(v', u')  (δ, γ, v, u, v', u') ∈ψ_n}

concludes the proof.

Soundness of the rule 

[MLET-L]
      ΔΣΓΨ
          ♢_[y_1←x_1t_1t'_1, y_2←t'_2] ψ
          ΔΣΓΨ♢_[x_1←t_1]ϕ
          ΔΣΓ,x_1:C_1Ψ,ϕ
            ♢_[y_1←t'_1, y_2←t'_2]ψ

Let n ∈ and (δ, γ) ∈Δ|Σ|Γ|Ψ_n.
Then from the first premise we have that the support of the distribution t_1_n(δ,γ) is included in

    {v  (δ, γ, v) ∈ϕ_n}.


From the second premise we get that for every v such that (δ, γ, v) ∈ϕ_n there exists an

    {(v', u')  (δ, γ, v, v', u') ∈ψ_n}

coupling for t_1'_n(δ,γ, v) and t_2'_n(δ,γ).
Since x_1 is fresh for ψ the relation

    R {(v', u')  (δ, γ, v, v', u') ∈ψ_n}

is independent of v.

Let ℐ = {v  (δ, γ, v) ∈ϕ_n} and for any v ∈ℐ let p_v = t_1_n(δ,γ)(v), μ_v = t_1'_n(δ,γ,v), and ν_v = t_2'_n(δ,γ).
Then we have ∑_v ∈ℐ p_v = 1 from the first premise of the rule and Rμ_vν_v for all v ∈ℐ from the second premise.
Lemma <ref> concludes the proof.
 
















 §.§ Proof of Theorem <ref>


The inverse implication follows immediately from the SUB rule and
the fact that we can always prove a judgement of the shape

    ΓΣΓΨt_1A_1t_2A_2⊤

for well-typed t_1 and t_2.

We will prove the direct implication by induction on the
derivation. We will just prove the two-sided rules.  The proofs for the one
sided rule are similar.

 

Case. *[Right= Next]
      ΔΣΓ, x_1:A_1, x_2:A_2Ψ, Φ'x_1x_2t_1A_1t_2A_2Φ
ΔΣΓΨu_1A_1u_2A_2, , Φ'ΔΣΓΨx_1 ← u_1t_1A_1x_2 ← u_2t_2A_2▹[x_1← u_1,x_2← x_2,←,←]Φ

 

By I.H. ΔΣΓ, x_1:A_1, x_2:A_nΨ, Φ'x_1x_2Φt_1t_2, (H1)


and ΔΣΓΨ[ u_1,  u_2]Φ' (H2)


To show: ΔΣΓΨ[x_1  u_1, x_2  u_2, x_1 ← u_1t_1 , x_2 ← u_2t_2]Φ. (G)


By CONV we can change the goal (G) into


ΔΣΓΨ[x_1  u_1, x_2  u_2]Φt_1t_2 (G')


and (H2) into:


ΔΣΓΨ[x_1  u_1, x_2  u_2]Φ'x_1x_2 (H2')


Finally, by applying _App to (H1) and (H2) we get (G')


































Case. *[Right= Prev]
      ΔΣ··t_1 A_1t_2 A_2,,ΦΔΣΓΨt_1A_1t_2A_2Φ

 






We just apply _E.




Case. *[Right= Box]
      ΔΣ··t_1A_1t_2A_2ΦΔΣΓΨt_1□ A_1t_2□ A_2□Φx_1x_1x_2x_2

 

By I.H. ΔΣ··Φt_1t_2


To show: ΔΣΓΨ□Φx_1t_1x_1x_2t_2x_2


By CONV we can change the goal into:


ΔΣΓΨ□Φt_1t_2


And then we can prove it by □_I.



Case.*[Right= LetBox]
      ΔΣΓΨu_1□ B_1u_2□ B_2□Φx_1x_1x_2x_2
Δ, x_1 : B_1, x_2 : B_2Σ, Φx_1x_2ΓΨt_1A_1t_2A_2Φ'ΔΣΓΨx_1u_1t_1A_1x_2u_2t_2A_2Φ'

 

By I.H. ΔΣΓΨ□Φx_1u_1x_1x_2u_2x_2 (H1)


and Δ, x_1 : B_1, x_2 : B_2Σ, Φx_1x_2ΓΨΦ't_1t_2. (H2)


To show: ΔΣΓΨΦ'x_1u_1t_1x_2u_2t_2 (G)


We instantiate (H2) into:

    [ ΔΣ, Φx_1u_1x_1x_2u_2x_2ΓΨ
    Φ't_1x_1x_1u_1x_1t_2x_2x_2u_2x_2 ]

And by the equality txxux≡xut, we get:

    [ ΔΣ, Φx_1u_1x_1x_2u_2x_2ΓΨ
    Φ'x_1u_1t_1x_2u_2t_2 ]

and then, by applying □_E to (H1) and the previous judgement we get (G).



Case. *[Right= LetConst]
      B_1,B_2,Φ constant
 FV(Φ)∩ FV(Γ) = ∅
ΔΣΓΨu_1B_1u_2B_2Φ
Δ, x_1 : B_1, x_2 : B_2Σ, Φx_1x_2ΓΨt_1A_1t_2A_2Φ'ΔΣΓΨx_1u_1t_1A_1x_2u_2t_2A_2Φ'

 

By I.H. ΔΣΓΨΦu_1u_2, (H1)


and Δ, x_1 : B_1, x_2 : B_2Σ, Φx_1x_2ΓΨΦ't_1t_2. (H2)


To show: ΔΣΓΨΦ'x_1u_1t_1x_2u_2t_2 (G)


From (H1) and the fact that Φ, B_1 and B_2 are constant, we get:


ΔΣΓΨ□Φu_1u_2


The rest of the prove is analogous to the previous case.














     
Case. *[Right= Fix]
      ΔΣΓ, f_1:A_1, f_2:A_2Ψ, ,f_1,f_2Φt_1A_1t_2A_2ΦΔΣΓΨfix f_1. t_1A_1fix f_2. t_2A_2Φ

 

By I.H. ΔΣΓ, f_1:A_1, f_2:A_2Ψ,[ f_1,  f_2]ΦΦt_1t_2.


To show: ΔΣΓΨΦf_1t_1fixf_2t_2 

Instantiating the I.H. with f_1 = f_1t_1 and f_2 = f_2t_2 we get:


ΔΣΓΨ,[f_1t_1, f_2t_2]ΦΦt_1f_1f_1t_1t_2f_2f_2t_2.


Since tfft≡ft, by CONV:


ΔΣΓΨ,[f_1t_1, f_2t_2]ΦΦf_1t_1f_2t_2.


and since [f_1t_1, f_2t_2]Φ⇔Φf_2t_2f_2t_2,


ΔΣΓΨ,Φf_1t_1fixf_2t_2Φfixf_1t_1fixf_2t_2,


and finally, by Löb we get our goal.




Case. *[Right= Cons]
      ΔΣΓΨx_1A_1x_2A_2Φ_h
ΔΣΓΨxs_1A_1xs_2A_2Φ_t
ΓΨ∀ x_1,x_2, xs_1, xs_2. Φ_hx_1x_2⇒Φ_txs_1xs_2⇒Φx_1xs_1x_2xs_2ΔΣΓΨx_1xs_1A_1x_2xs_2A_2Φ

 

Apply the I.H., ∀_E and _E.



































Case.*[Right= Head]
      ΔΣΓΨt_1A_1t_1A_1Φhd hd ΔΣΓΨhd t_1A_1hd t_2A_2Φ

 

Trivial by I.H.




Case. *[Right= Tail]
      ΔΣΓΨt_1A_1t_2A_2Φtl tl ΔΣΓΨtl t_1Str_A_1tl t_2Str_A_2Φ

 

Trivial by I.H.


  

Case. *[Right= Equiv]
      ΔΣΓΨt'_1A_1t'_2A_2Φ
 t_1 ≡ t_1' 
 t_2 ≡ t_2' 
Δ|Γ⊢ t_1 : A_1 
Δ|Γ⊢ t_2 : A_2ΔΣΓΨt_1A_1t_2A_2Φ

 

Trivial by I.H. and Conv.





Most of the proofs for the probabilistic fragment are a consequence of the proof of <ref>.
The only interesting case is Markov. We do the proof directly in RHOL by showing we can derive it from Fix.
We have the premises:



  * ΔΣΓΨt_1C_1t_2C_2ϕ


  * ΔΣΓΨh_1C_1 →(C_1)h_2C_2 →(C_2)ψ_3


  * ΔΣΓΨψ_4


where:

    ψ_3    ≡∀ x_1 x_2. ϕx_1x_2⇒♢_[ y_1  x_1, y_2  x_2]ϕy_1y_2
    ψ_4    ≡∀ x_1 x_2 xs_1 xs_2. ϕx_1x_2⇒[y_1  xs_1, y_2  xs_2]Φ⇒Φy_1x_1xs_1y_2x_2xs_2


If we inline the definition of unfold, we have to prove:


    [t]
    
            f   λ x_1. λ h_1. z_1h_1 x_1t_1swap_^C(f_1 z_1  h_1)x_1t_1∼
    f   λ x_2. λ h_2. z_2h_2 x_2t_2swap_^C(f_2 z_2  h_2)x_2t_2
    |    ∀ x_1 x_2 h_1 h_2. ϕx_1x_2⇒ψ_3h_1h_2⇒♢_[y_2 , y_2 ]Φ


We apply FIX, MLET twice, and then MUNIT. The main judgements we have to prove are: 




  * ΔΣΓΨh_1 x_1(C_1)h_2 x_2(C_2)♢_[, ]ϕ



  * ΔΣΓΨswap_^C(f_1 z_1  h_1)( C_1)swap_^C(f_2 z_2  h_2)( C_2)♢_[z_1 , z_2 ][y_1  z_1, y_2  z_2]Φ


  * ΔΣΓ, x_1,x_2,t_1,t_2Ψ, ϕx_1x_2, [y_1  t_1, y_2  t_2]Φy_1t_1(C_1)y_2t_2(C_2)Φy_1y_2


The judgement (a) is a direct consequence of premises (1) and (2), (b) is proven from the inductive hypothesis,
and (d) is a direct consequence of (3). This completes the proof.


































































§ EXAMPLES





 §.§ Proof of ZipWith


This example, taken from <cit.>, proves a
property about the ZipWith function, which takes two
streams of type A, a function on pairs of elements, and “zips” the
two streams by applying that function to the elements that are at the
same position on the two streams.  We want to show that if the
function on the elements is commutative, zipping two streams with that
function is commutative as well.

We can define the zipWith function as:

    zipWith   : (→→) →→→
    zipWith   zipWithλ f. λ xs. λ ys. (f ( xs) ( ys))(zipWith ( xs)  ( ys))

We prove (omitting types of expressions):

    zipWithzipWithΦ

where

    Φ   ∀ f_1 f_2. (f_1 = f_2 ∧∀ x y. f_1 x y = f_1 y x) ⇒∀ xs_1 xs_2. ∀ ys_1 ys_2. (xs_1 = ys_2 ∧ xs_2 = ys_1)
       ⇒ f_1 xs_1 ys_1 =  f_2 xs_2 ys_2 
    
     A         (→→) →→→

The proof proceeds by applying two-sided rules all the way. We invite
interested readers to compare this proof with the one given
in <cit.> to see how the two approaches differ.

We show how to derive the statement backwards. The derivation begins
with the Fix rule. Its premise is (omitting constant contexts):

    zipWith_1,zipWith_2: A[←zipWith_1,←zipWith_2] Φλ f_1.(⋯)Aλ f_2.(⋯)AΦ


Then we apply the ABS rule three times to introduce into the
context the logical relations on f_1,f_2,xs_1,xs_2,ys_1, and ys_2. The premise
we need to prove is then:

    [ ΓΨ(f_1 (hd xs_1) (hd ys_1))(zipWith_1  (tl xs_1)  (tl ys_1))
    (f_2 (hd xs_2) (hd ys_2))(zipWith_2  (tl xs_2)  (tl ys_2)) = ]

where

    Γ   zipWith_1,zipWith_2 : A; f_1,f_2:(→→); xs_1,xs_2,ys_1,ys_2: 
    Ψ   [←zipWith_1,←zipWith_2] Φ, (f_1 = f_2 ∧∀ x y. f x y = f y x), xs_1 = ys_2, xs_2 = ys_1

Now we can apply the Cons rule, which has three premises:

 
  * ΓΨf_1 (hd xs_1) (hd ys_1)f_2 (hd xs_2) (hd ys_2) =

 
  * ΓΨzipWith_1  (tl xs_1)  (tl ys_1)zipWith_2  (tl xs_2)  (tl ys_2) =

 
  * ΓΨ∀ x y xs ys. x=y ⇒ xs = ys ⇒xxs = yys

Premise (3) is easily provable in HOL. To prove premise (1) we first apply the App rule twice, and we have to prove the judgments:

  
  * ΓΨf_1→→f_2→→∀ vs_1 vs_2 ws_1 ws_2.
                                   
 vs_1 = hd ys_2 ∧ vs_2 = hd ys_1 ⇒
                                     ws_1 = hd xs_2 ∧ ws_2 = hd xs_1 ⇒ vs_1 ws_1 =  vs_2 ws_2

  
  * ΓΨhd xs_1hd xs_2 = hd ys_2 ∧ = hd ys_1

  
  * ΓΨhd ys_1hd ys_2 = hd xs_2 ∧ = hd xs_1

The three can be proven in HOL from the conditions imposed on f_1,f_2 and the equalities xs_1 = ys_2, xs_2 = ys_1.

All that remains to prove is premise (2) of the Cons application, which, by expanding the definition of  and
using the equational theory of delayed substitutions, can be desugared to:


    ΓΨ[ξ_1](g_1 t_1 u_1)[ξ_2](g_2 t_2 u_2)[ξ_1, ξ_2, [←, ←]]( = )

where, for i=1,2:

    ξ_i = [g_i ←zipWith_i, t_i←(tl xs_i), u_i ← (tl ys_i)]


We apply the Next rule, and we have the four following premises:

  
  * ΓΨzipWith_1 AzipWith_2 A[←, ←] ( = ∧∀ x y.  x y =  y x)

  
  * ΓΨtl xs_1tl xs_2[←, ←] ( = tl ys_2 ∧ = tl ys_1)

  
  * ΓΨtl ys_1tl ys_2[←, ←] ( = tl xs_2 ∧ = tl xs_1)

  
  * Γ; g_1,g_2: A; t_1,t_2,u_1,u_2: Ψ, g_1 = g_2 ∧∀ x y. g_1 x y = g_1 y x, t_1 = tl ys_2 ∧ t_2 = tl ys_1,
                     
 u_1 = tl xs_2 ∧ u_2 = tl xs_1g_1 t_1 u_1g_2 t_2 u_2 =



To prove the first premise we instantiate the inductive hypothesis we got
from Fix. To prove the second and the third premises we use the
equalities xs_1 = ys_2, xs_2 = xs_1. Finally, the fourth premise is a
simple derivation in HOL that follows from the same equalities plus
the refinements of g_1,g_2,t_1,t_2,u_1,u_2. This concludes the
proof.




 §.§ Proof of approximation series


We now continue with another example that, while still being fully synchronous
(i.e., uses only two-sided rules), goes beyond reasoning about equality of
streams, and showcases the flexibility of streams to represent different kinds
of information and structures.

For instance, streams can be used to represent series of numbers. In
this example, we illustrate an instance of a property about series
that can be proven in our system.  Consider the series x_0, x_1,
… for any p ≥1/2 and any a ≥ 0, where x_0
is given and:


    x_i+1 = px_i + (1-p) a/x_i


It can be easily shown that if x_0 ≥√(a), then this series
converges monotonically from the top to √(a). In
particular, lim_i →∞ x_i = √(a). (For p
= 1/2, this is the standard Newton-Raphson series for
square-root computation <cit.>)

The interesting relational property is that for smaller p, this
series converges faster. Concretely, define f(p, a, x_0, i) as the
ith element of the above series (for the given p, a and
x_0). Then, the relational property to prove is that:

    ∀ p_1  p_2  a  x_0  i.  (12≤ p_1 ≤ p_2  x_0 ≥√(a)) ⇒ |f(p_1,a,x_0,i) - √(a)| ≤ |f(p_2,a,x_0,i)- √(a)|


We outline the proof of this property. First, note that because
convergence is from the top, |f(p_2,a,x_0,i) - √(a)| =
f(p_2,a,x_0,i) - √(a). Therefore, the property above is the same
as:

    ∀ p_1  p_2  a  x_0  i.  (12≤ p_1 ≤ p_2 x_0 ≥√(a)) ⇒ f(p_2,a,x_0,i) - f(p_1,a,x_0,i) ≥ 0


This is easy to establish by induction on i.

(Note the importance of the assumption p ≥1/2: Without
this assumption, convergence is not monotonic, and this relational
property may not hold. If we start with x_0 ≤√(a) instead of
x_0 ≥√(a), we need p ≤1/2 for convergence to
be monotonic, this time from below.)

Now we see how we can encode and prove this as a relational property
of a pair of streams.  We can define a stream whose elements are the
elements of one of this series:

    approx_sqrt   : →→→
    approx_sqrt   fλ p. λ a. λ x.
       x(f  p  a  (p*x + (1-p)*a/x))

We prove:

    approx_sqrt_1→→→approx_sqrt_2→→→Φ

where

    Φ   ∀ p_1 p_2. (12≤ p_1 ≤ p_2) ⇒∀ a_1 a_2. 0 ≤ a_1 = a_2 ⇒∀ x_1 x_2. (0 ≤ x_1 ≤ x_2 ∧ a_1≤ x_1*x_1)
              
       ⇒( p_1 a_1 x_1,  p_2 a_2 x_2, λ n_1 n_2. 0 ≤ n_1 ≤ n_2 ∧ a_1 ≤ n_1 * n_1)

and  is defined axiomatically as follows:

    ∀ s_1,s_2,n_1,n_2. ϕ n_1 n_2 ⇒[s'_1 s_1, s'_2  s_2](s'_1,s'_2,λ x_1 x_2. ϕ)⇒(n_1s_1, n_2s_2, λ x_1 x_2. ϕ)


The meaning of the judgement is that, if we have two approximation series for
the square root of a (formally, we write a = a_1 = a_2), with
initial guesses x_1 ≤ x_2, and parameters 1/2 ≤ p_1 ≤
p_2, then, at every position, the first series is going to be closer
to the root than the second one. Note that we have removed the square
roots in the specification by squaring.

Let A =→→→.  We will show
how to derive the judgment backwards. The proof starts by applying
Fix which has the premise (omitting constant contexts):

    f_1,f_2 :  A[, ← f_1, f_2]Φλ p_1. λ a_1. λ x_1. …Aλ p_2. λ a_2. λ x_2. …AΦ

and after applying Abs three times:

    [ f_1,f_2 :  A; p_1,p_2,a_1,a_2,x_1,x_2 : 
    [, ← f_1, f_2]Φ, (12≤ p_1 ≤ p_2) , 0 ≤ a_1 = a_2 , 0 ≤ x_1 ≤ x_2, a_1 ≤ x_1*x_1
     (λ y_1.x_1(f_1 p_1 a_1 y_1))(p_1*x_1 + (1-p_1)*a_1/x_1)
     (λ y_2.x_2(f_2 p_2 a_2 y_2))(p_2*x_2 + (1-p_2)*a_2/x_2)
     All(, , λ n_1 n_2. n_1 ≤ n_2) ]


Let Γ and Ψ denote the typing and logical contexts in the previous judgement. Now we apply App, which has two premises:

  
  * ΓΨλ y_1.x_1(f_1 p_1 a_1 y_1)→λ y_2.x_2(f_2 p_2 a_2 y_2)→
∀ y_1, y_2. (0 ≤ y_1 ≤ y_2 ∧ a_1 ≤ y_1 * y_1) ⇒ All( y_1,  y_2, λ n_1 n_2. 0≤ n_1≤ n_2 ∧ a_1 ≤ n_1 * n_1)
        
  
  * ΓΨp_1*x_1 + (1-p_1)*a_1/x_1p_2*x_2 + (1-p_2)*a_2/x_2
 0 ≤≤∧ a_1 ≤ *


The second premise can be established in Guarded HOL as an arithmetic
property in our theory of reals. To prove the first one, we start by applying the Abs
rule, followed by the Cons rule, which has three premises:

  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1)x_1x_20 ≤≤∧ a ≤ *
  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1)(f_1 p_1 a_1 y_1)
 (f_2 p_2 a_2 y_2)[, ]All(, , λ n_1 n_2. 0 ≤ n_1 ≤ n_2 ∧ a ≤ n_1 * n_1)
  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1)∀ h_1 h_2 t_1 t_2. 0 ≤ h_1 ≤ h_2 ⇒ a_1 ≤ h_1*h_1 ⇒
[ t_1,  t_2] All(, , λ n_1 n_2. 0≤ n_1 ≤ n_2 ∧ a ≤ n_1*n_1) ⇒

          All(h_1t_1, h_2t_2, λ n_1 n_2. 0≤ n_1 ≤ n_2 ∧ a ≤ n_1*n_1))



Premise (1) is just the refinement on x_1,x_2, while premise (3) is
the axiomatization of All. To prove
premise (2) one instantiates the induction hypothesis given by the
Fix rule. In order to do so, we first rewrite the two terms we are comparing to their desugared form:

    [f_1'  f_1, p_1'  p_1, a_1'  a_1, y_1'  y_1] f_1' p_1' a_1' y_1'

and

    [f_2'  f_2, p_2'  p_2, a_2'  a_2, y_2'  y_2] f_2' p_2' a_2' y_2'

We can also add by SUB the same substitutions to the 
in the conclusion, since the substituted variables do not appear in
the formula. Then we can apply the Next rule, which has the premises:

  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1)f_1 Af_2 A[, ]Φ
  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1) p_1 p_2
[, ] 12≤≤
  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1) a_1 a_2[, ] 0 ≤ =
  
  * Γ, y_1,y_2 : Ψ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1) y_1 y_2
[, ] 0 ≤≤∧ a_1' ≤ *
  
  * Γ, y_1,y_2,p_1',p_2',a_1',a_2',y_1',y_2' : ; f_1', f_2' : AΨ, (y_1 ≤ y_2 ∧ y_1*y_1 ≥ a_1), Φf_1'f_2',
12≤ p_1' ≤ p_2', 0 ≤ a_1' = a_2', 0 ≤ y_1' ≤ y_2' ∧ a_1' y_1' * y_1'
 f_1' p_1' a_1' y_1'f_2' p_2' a_2' y_2'All(, , λ n_1 n_2. 0 ≤ n_1 ≤ n_2 ∧ a ≤ n_1 * n_1)
 


The first four can be proven simply by instantiating and then delaying
one of the axioms. The last one is proven by applying App three
times. This concludes the proof.





 §.§ Proof of Cassini's identity



We continue building on the idea from the previous example of using
streams to represent series of numbers. This time, we prove a
classical identity of the Fibonacci sequence. Since the example
requires to observe the stream at different times, we will also have
to deal with some asynchronicity on the delayed substitutions.

Let F_n be the nth Fibonacci number. Cassini's identity states
that F_n-1· F_n+1 - F_n^2 = (-1)^n. Cassini's identity
can be stated as a stream problem as follows. First, let F be the
Fibonnaci stream (1,1,2,3,5,…) and A be the stream
1,-1,1,-1,… Let ⊕ and ⊗ be infix functions that
add and multiply two streams pointwise. Cassini's identity can then be
informally written as:

    F ⊗(  F) = (F ⊗ F) ⊕ A


In order to formalize Cassini's identity in our system, we first define:

    ⊕   : →→   ⊗   : →→
    ⊕   [ fλ s.λ t
    ( x +  y)(f  ( x)  ( y)) ]       ⊗   [ fλ s.λ t
    ( x *  y)(f  ( x)  ( y)) ]

Then we define F and A as the fixpoints of the equations:

    F    F1[ F'  F] (1 [ T  F' ] ( F' ⊕ T))
    
      A    A1(-1A)



We prove (using prefix notation for ⊕ and ⊗):


    [ T_1  F] ⊗ ( F)  T_1 ⊕(F ⊗ F)  ( A) =



The proof combines applications of two-sided rules and one-sided
rules; in particular, we use the rule NEXT-L to proceed with
the proof for a judgement where the left expression is delayed twice
and the right expression is delayed once.

By conversion, in the logic we can prove the following equalities:

    Ψ{
      F    = 1(1 [ T  tl F ] ( F ⊕ T)), 
    
      A    = 1(-1 A)}


Using these equalities, and desugaring the applications, the judgment we want to prove is (omitting constant contexts):


    [ F,A: Ψ [ T_1  tl F]  [T_1'  tl T_1] (F ⊗ T_1') 
     [T_2  tl(F ⊗ F)] (T_2 ⊕ A)
    [' , T_1  tl F]  [”, ', T_1'  tl T_1, T_2'  tl(F ⊗ F)] ” = ' ]


Notice that on the left, since we want to apply tail twice to F, we
need to delay the term twice so that F and tl tl F have the same
type. On the right, we just need to delay the term once. As for the
logical conclusion,  needs to be delayed twice, while
 only once. The way to do this is by having 
appear on the two substitutions but  only on the inner one.

We start by applying NEXT-L, which has the two following premises:

  
  * F,A: Ψtl F [' ] tl F = '

  
  * F,A, T_1: Ψ, tl F =  T_1 [T_1'  tl T_1] (F ⊗ T_1') 
 [T_2  tl(F ⊗ F)] (T_2 ⊕ A) [”, ', T_1'  tl T_1, T_2'  tl(F ⊗ F)] ” = '



The first premise is trivial. We continue by applying NEXT to the second, which has the following premises:


  
  * F,A, T_1: Ψ, tl F =  T_1tl T_1tl(F ⊗ F)
[' , ' ] T_1 = ' ∧'⊗' = '

  
  * F,A, T_1', T_2: Ψ,tl F =  T_1 , tl T_1 =  T_1', T_1'⊗ T_1' = T_2F ⊗ T_1'T_2 ⊕ A =



Again, the first premise is trivial. We apply APP twice to the second, and we have to prove:


  
  * F,A, T_1', T_2: Ψ,tl F =  T_1 , tl T_1 =  T_1', T_1'⊗ T_1' = T_2FA
 = F ∧ = A

  
  * F,A, T_1', T_2: Ψ,tl F =  T_1 , tl T_1 =  T_1', T_1'⊗ T_1' = T_2T_1'T_2
 F = 1(1 T_1)∧⊗ =

  
  * F,A, T_1', T_2: Ψ,tl F =  T_1 , tl T_1 =  T_1', T_1'⊗ T_1' = T_2
⊗→→⊕→→∀ X_1 X_2 Y_1 Y_2. X_1 = F ∧ X_2 = A ⇒ F = 1(1 Y_1)∧ Y_1 ⊗ Y_1 = Y_2 ⇒ X_1 Y_1 =  X_2 Y_2


The two first premises are easy to prove. We will show how to prove the last one. 
For this, we need a stronger induction hypothesis for ⊕̂ and
⊗̂. We propose the following:

    [ ∀ g_1,g_2,b_1,G,B. G = g_1g_2(G⊕̂ (t̂l̂ G))∧ b_1 = g_1^2 + g_1 g_2 - g_2^2 ∧ B = b_1-b_1B;                                                   ⇒ G ⊗̂t̂l̂(t̂l̂ G) = t̂l̂ (G ⊗̂ G) ⊕̂ B ]


We then use the SUB rule to strengthen the inductive hypothesis, and now the new judgement to prove is:


    [ F,A, T_1', T_2: Ψ,tl F =  T_1 , tl T_1 =  T_1', T_1'⊗ T_1' = T_2
    ⊗→→⊕→→
    ∀ X_1 X_2 Y_1 Y_2. (∃ g_1, g_2, b_1, G, B. G = g_1(g_2 [ G'  tl G ] ( G ⊕ G'))∧ b_1 = g_1^2 + g_1 g_2 - g_2^2 ∧
     B = b_1(-b_1 B)∧
              X_1 = G ∧ X_2 = B ∧ X_1 = 1(1 Y_1)∧ Y_1 ⊗ Y_1 = Y_2) ⇒ X_1 Y_1 =  X_2 Y_2 ]


Let Γ', Ψ' and Φ_IH denote respectively the typing context, logical context and logical conclusion of the previous judgement. The premise
of the FIX rule is:


    [ Γ; f_1,f_2 : (→→)Ψ',[ f_1,  f_2] Φ_IH
    f_1λ X_1. λ Y_1. …→→f_2λ X_2. λ Y_2. …→→Φ_IH ]


Let Φ_E denote the existential clause in Φ_IH. After applying ABS twice, we have:


    [ Γ; f_1,f_2 : (→→); X_1,X_2,Y_1,Y_2 : Ψ',[ f_1,  f_2] Φ_IH, Φ_E
    (hd X_1)*(hd Y_1)f_1  (tl X_1)  (tl Y_1)(hd X_2)+(hd Y_2)f_2  (tl X_2)  (tl Y_2) = ]


And then we apply Cons to prove equality on the heads and the tails:



  * Γ; f_1,f_2 : (→→); X_1,X_2,Y_1,Y_2 : Ψ',[ f_1,  f_2] Φ_IH, Φ_E
 (hd X_1)*(hd Y_1)(hd X_2)+(hd Y_2) =

  * Γ; f_1,f_2 : (→→); X_1,X_2,Y_1,Y_2 : Ψ',[ f_1,  f_2] Φ_IH, Φ_E
 f_1  (tl X_1)  (tl Y_1)f_2  (tl X_2)  (tl Y_2) =


To prove the first one we notice that hd X_1 * hd Y_1 = g_1 * (g_1 + g_2) = g_1^2 + g_2*g_1 =  g_2^2 + g_1^2 + g_1 * g_2 - g_2^2 = hd X_2 * hd Y_2. To prove the second one
we need to check that tl X_1,tl Y_1, tl X_2,tl Y_2 satisfy the precondition of the inductive hypothesis. In particular, we need to check that

    -b_1 = -g_1^2 - g_1 g_2 + g_2^2 = g_2^2 + g_2 (g_1 + g_2) - (g_1 + g_2)^2

which is can be proven by arithmetic computation.






§ UNARY FRAGMENT


In this section we introduce a unary system to prove properties
about a single term of the guarded lambda calculus. We will start
by adding some definitions Guarded HOL for the unary diamond monad,
following by the derivation rules for both the non-probabilistic
and the probabilistic system, plus the metatheory and an example.



 §.§ Unary fragment of GHOL


The unary semantics of the diamond monad are:


    ♢_[x← t]ϕ_i{(δ,γ) Pr_v(t_i(δ,γ)) [(δ, (γ, v))∈ϕ_i] = 1 }


The rules are on Figure <ref>
    

        






 §.§ Guarded UHOL


We start by defining the Guarded UHOL system, which allows us to prove
logical properties of a term of the Guarded Lambda Calculus.  More
concretely, judgements have the form:

    ΔΣΓΨtσϕ

where t is a term well-typed in the dual context Δ|Γ and ϕ is a logical formula well-typed in the context
Δ|Γ,  : σ and that can refer to t via the special variable . The logical contexts Σ and
Ψ consist respectively of refinements over the contexts Δ and Γ.




 §.§ Derivation rules


The rule Next corresponds to the introduction of the later
modality. A refinement Φ_i is proven on every term in the
substitution, and using those as a premise, a refinement Φ is
proven on t. In the notation []Φ the first
 is the variable bound by the delayed substitution inside Φ
while the second  is the distinguished variable in the refinement
that refers to the term that is being typed. In other words, t satisfies
[]Φ if [ t]Φ.

The rule Prev corresponds to the elimination of the later modality.
If we can prove ϕ in a constant context, then we can also prove
ϕ.

The rule Box applies the constant modality on a formula that can
be proven on a constant context.

The rule LetBox removes the constant modality from a formula Φ
by using it as a constant premise to prove another formula Φ'.

The rule LetConst shifts constant terms between contexts.

The rule Fix introduces a fixpoint and proves a refinement on it by
Loeb induction.

The rule Cons proves a property on a stream from a refinement on its
head and its tail.

The rule ConsHat is the analogue of Cons to build constant streams.
In particular, the :̂:̂ operator can be defined as λ x. λ s. (y,t)(x,s)(yt).

Conversely the rules Head and Tail respectively prove a property
on the head and the tail of a stream from a property on the full stream.





The intended meaning for a judgment ΔΣΓΨtτϕ is:
“For every valuations δ, γ of Δ and Γ,

    Δ|Γ⊢□Σ(δ,γ) ∧Δ|Γ⊢Ψ(δ,γ) Δ|Γ, :τ⊢Σ(δ,⟨γ, Δ|Γ⊢ t(δ,γ)⟩) ”





 §.§ Metatheory


We now the most interesting metatheoretical properties of Guarded UHOL. In particular, Guarded UHOL
is equivalent to Guarded HOL:

 
  For every contexts Δ,Γ, type σ, term t, sets of assertions Σ,Ψ
  and assertion ϕ, the following are equivalent:
  
  
  * ΔΣΓΨtσϕ
  
  * ΔΣΓΨϕt
  

The proof is analogous to the relational case

The previous result allows us to lift the soundness result from Guarded HOL to
Guarded UHOL.


If ΔΣΓΨtσϕ, then for every valuations
δΔ, γΓ:

    Δ⊢Σ(δ) ∧Δ|Γ⊢Ψ(δ,γ) ⇒Δ|Γ,:σ⊢ϕ(δ, γ[Δ|Γ⊢ t(δ,γ)])

In particular, there is no proof of
Δ∅Γ∅tσ in Guarded UHOL.



















 §.§ Probabilistic extension


We comment on the rules, starting from the rules of the unary
logic. There are three new rules for the probabilistic case, and they
all establish that an expression u of type (D) satisfies the
assertion ♢_[y←]ϕ, i.e.  for every
element v in the support of (the interpretation of) u, the
interpretation of ϕ with the valuation [y ↦ v] is
true. This intuition is captured by the rule [], which
can be used in particular in case u is a primitive distribution. The
rule [] considers the case where u is of the form
t; in this case, it is clearly sufficient to know that
ϕ[t/y] is valid. The rule [] simply captures the
fact that the support of xtt' is the disjoint union of
the support of t' under all the assignments of x to values in the
support of t.





Finally, we prove an embedding lemma for Guarded UHOL. The proof can be
carried by induction on the structure of derivations, or using the
equivalence between Guarded UHOL and Guarded HOL (Theorem <ref>).
 Assume that:


  * ΔΣΓΨt_1σ_1ϕ

  * ΔΣΓΨt_2σ_2ϕ'

Then
ΔΣΓΨt_1σ_1t_2σ_2ϕϕ'.





 §.§ Unary example: Every two


We define the every2 function, which receives a stream and returns another stream consisting
of the elements at even positions in the input stream. Note that this function, while productive,
cannot be built with the type Str → Str, since we need to take twice the tail of the argument,
which would have type Str, and then a Str cannot be built. Instead, we need to use the constant modality
as follows:


    every2    : □ Str → Str 
    
    every2    every2λ s. ĥd̂(t̂l̂ s)(every2  next (t̂l̂(t̂l̂ s)))


Where the ĥd̂ and t̂l̂ functions are not the native ones, but rather they are defined as:

    ĥd̂   : □ Str →   t̂l̂   : □ Str →□ Str 
    ĥd̂   λ s. xshd x       t̂l̂   λ s. xs((tl x))


The property we want to prove is:

    ··ones : □ StrΨevery2□ Str → Str∀ s. s = ones ⇒ s = (xsx)

where ones is the constant stream containing only the number 1 defined as:

    ones (f1f)

For which we can prove the following properties:

    Ψĥd̂ ones = 1, t̂l̂ ones = ones

In the rest of the proof we omit the empty contexts Δ and Σ.

We start by applying the Fix rule, which has the premise:

    [ ones : □ Str, every2:(□ Str → Str)Ψ, [ every2]∀ s. s = ones ⇒ s = xsx
    λ s. (⋯)□ Str → Str∀ s. s = ones ⇒ ( s) = xsx ]

We apply the Abs rule inmediately after:

    [ ones : □ Str, every2:(□ Str → Str), s: □ Str
    Ψ, [ every2]∀ s. s = ones ⇒ s = xsx, s = ones
    ĥd̂(t̂l̂ s)(every2  (t̂l̂(t̂l̂ s)))Str = xsx ]


By SUB and the equivalence xsx≡xonesx, we can change the
conclusion of the judgement.
Now we use the Cons rule, which has three premises:

  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str
Ψ, [ every2]∀ s. s = ones ⇒ s = xsx, s = onesĥd̂(t̂l̂ s) = 1


  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str
Ψ, [ every2]∀ s. s = ones ⇒ s = xsx, s = ones
(every2 (t̂l̂(t̂l̂ s)))Str = xonesx


  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str
Ψ, every2∀ s. s = ones ⇒ s = xsx, s = ones
∀ y, ys. y = 1 ⇒[zs  ys](zs = xonesx)⇒yys = (xonesx)
    


Premises (1) is a consequence of the properties of ones. To prove premise (3)
we reduce the letbox with the box inside ones, and do some reasoning using the definition of the fixpoint. To prove the
premise (2) we first desugar the term we are typing:

    every2 (t̂l̂(t̂l̂ s))) [g ← every2, t ←(t̂l̂(t̂l̂ s))]g t

and then we apply Next which has the following premises:

  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str
Ψ, every2∀ s. s = ones ⇒ s = xsx, s = ones
 every2(□ Str → Str)(∀ s = ones ⇒ s = xsx)


  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str
Ψ, every2(∀ s. s = ones ⇒ s = xsx), s = ones
(t̂l̂(t̂l̂ s))□ Str( = ones)


  
  * ones : □ Str, every2:(□ Str → Str), s: □ Str, g: □ Str → Str, t: □ Str
Ψ, every2(∀ s. s = ones ⇒ s = xsx), s = ones,
                
∀ s. s = ones ⇒ g s = xsx, t = onesg tStr = (xonesx)
    


The first premise is just an application of the Var rule.  The
second premise can be proven as a consequence of the properties of
ones.  Finally, the third premise can be proven with some simple
logical reasoning in HOL.  This concludes the proof.





     







