SPTree: Syntactic Computation Trees for SPComp #
This file defines SPTree α, a syntactic (free-monad-like) representation of
stateful probabilistic computations. Unlike SPComp α = Heap → SDistr (α × Heap)
(which is denotational), SPTree preserves syntactic structure, enabling cost
analysis by structural recursion.
Main definitions #
SPTree— syntactic computation tree (ret, sample, get, set, fail)SPTree.compile— denotational semantics (→ SPComp)SPTree.queryCost— worst-case operation count (structural recursion)SPTree.bind— monadic composition (substitution at leaves)
Main results #
compile_bind— compilation commutes with bindqueryCost_bind— cost of bind ≤ cost of tree + bound on continuation
SPTree Definition #
Syntactic computation tree for stateful probabilistic programs.
Lives in Type 1 because sample quantifies over β : Type.
- ret {α : Type} : α → SPTree α
- sample {α : Type} (β : Type) (finβ : Fintype β) (neβ : Nonempty β) : (β → SPTree α) → SPTree α
- get {α : Type} (l : Location) : (l.ty → SPTree α) → SPTree α
- set {α : Type} (l : Location) : l.ty → SPTree α → SPTree α
- fail {α : Type} : SPTree α
Instances For
Compilation to SPComp #
Compile a syntax tree to its denotational semantics.
Equations
- (CatCrypt.Core.SPTree.ret a).compile = CatCrypt.Core.SPComp.pure a
- (CatCrypt.Core.SPTree.sample β finβ neβ k).compile = (CatCrypt.Core.SPComp.sample β).bind fun (b : β) => (k b).compile
- (CatCrypt.Core.SPTree.get l k).compile = (CatCrypt.Core.SPComp.get l).bind fun (v : l.ty) => (k v).compile
- (CatCrypt.Core.SPTree.set l v t).compile = (CatCrypt.Core.SPComp.set l v).bind fun (x : Unit) => t.compile
- CatCrypt.Core.SPTree.fail.compile = CatCrypt.Core.SPComp.fail
Instances For
Query Cost #
Worst-case operation count: counts sample + get + set operations.
Uses Finset.univ.sup for branching to take the maximum cost
across all possible continuations. Structurally recursive.
Equations
- (CatCrypt.Core.SPTree.ret a).queryCost = 0
- (CatCrypt.Core.SPTree.sample β finβ neβ k).queryCost = 1 + Finset.univ.sup fun (b : β) => (k b).queryCost
- (CatCrypt.Core.SPTree.get l k).queryCost = 1 + Finset.univ.sup fun (v : l.ty) => (k v).queryCost
- (CatCrypt.Core.SPTree.set l v t).queryCost = 1 + t.queryCost
- CatCrypt.Core.SPTree.fail.queryCost = 0
Instances For
Monadic Bind (Substitution at Leaves) #
Monadic bind: substitute f at every ret leaf of s.
Equations
- (CatCrypt.Core.SPTree.ret a).bind x✝ = x✝ a
- (CatCrypt.Core.SPTree.sample γ finγ neγ k).bind x✝ = CatCrypt.Core.SPTree.sample γ finγ neγ fun (c : γ) => (k c).bind x✝
- (CatCrypt.Core.SPTree.get l k).bind x✝ = CatCrypt.Core.SPTree.get l fun (v : l.ty) => (k v).bind x✝
- (CatCrypt.Core.SPTree.set l v t).bind x✝ = CatCrypt.Core.SPTree.set l v (t.bind x✝)
- CatCrypt.Core.SPTree.fail.bind x✝ = CatCrypt.Core.SPTree.fail
Instances For
Simp Lemmas #
@[simp]