Documentation

CslibFrontier.Tactic.BindChain

Shared normalization and reordering of probabilistic bind chains #

The proof-producing core of pmf_swap, pmf_hoist, and peel_binds. It normalizes monadic notation and nested binds at the heads it visits, without unfolding named programs or rewriting the rest of a chain, matches independent samples by their distributions, and constructs the equality moving a selected sample to the front. Custom samplers supply their commutation lemmas; relation-specific peeling and support witnesses stay in the calling tactic.

Head-beta-reduce and substitute top-level let bindings (and local let fvars of PMF type) at the root of e (definitionally equal by beta/zeta/zetaDelta).

Cast a proof h : a' = b' to a = b when a ≡ a' and b ≡ b' definitionally.

Compose optional equality proofs e₀ = e₁ and e₁ = e₂.

Normalize only the outer head of a PMF expression into PMF.bind without unfolding named user definitions or traversing continuation bodies. Keep a source map intact with preserveMap so its enclosing bind can apply PMF.bind_map directly.

The head of an application, for a cheap test that an expression may be a sampler.

Instances For

    A commutation lemma μ.bind (fun b => F (k b)) = F (fun a => μ.bind fun b => k b a), which lets an independent sample of any μ move past the sampler F.

    The sampler rules shared by matching and proof-producing reordering.

    • comms : Array Comm

      Rules commuting independent distributions with custom samplers.

    • swapSamplers? : Option (Lean.Expr → Lean.Expr → Lean.Expr → Lean.MetaM Lean.Expr)

      Optional rule for commuting two custom samplers F, G, with continuation k. Its result proves F (fun a => G (k a)) = G (fun b => F (fun a => k a b)).

    Check the shape of a sampler commutation lemma, retaining its syntax for diagnostics.

    Elaborate a commutation lemma supplied by a tactic caller.

    A distribution or a sampler: the head head of a sample, and comm? when it is a sampler head, which is applied to the continuation, with the commutation lemma comm?.

    • head : Lean.Expr

      The distribution, or the sampler.

    • comm? : Option Comm

      The commutation lemma of a sampler.

    A step of a chain of samples: the distribution or sampler head and the continuation cont, so that the chain is head.bind cont, or head cont for a sampler.

    Recognize a custom sampler by a supplied commutation rule.

    The first sample of a chain, including a map or a bind exposed by head normalization.

    Run k x b with a new variable x for the argument of the function f, where b is f x reduced.

    The type of outcomes α of a distribution p : PMF α.

    p.front f = p.front g from h : f = g.

    partial def Cslib.Tactic.BindChain.findDepths (ctx : Context) (q : Pat) (e : Lean.Expr) (depth : ℕ := 0) :

    The depths at which the chain e samples q, up to reducible unfolding. A sample whose distribution mentions an earlier sample is not equal to q, which is closed. The search for a custom sampler stops at the first custom sampler unless the context supplies their commutation rule. Heads are normalized as the search descends into the chain.

    partial def Cslib.Tactic.BindChain.firstDepth? (ctx : Context) (q : Pat) (e : Lean.Expr) (depth : ℕ := 0) :

    The first depth at which the chain e samples q, assigning the metavariables of q. The same sampler boundaries as findDepths apply.

    Move the sample at depth k of the chain e, which samples q independently of the earlier samples, to the front. Normalize only the path to that sample, preserving the remaining continuation. Returns q.front g and a proof of e = q.front g.