Controlled Markov processes, LTL goals and bounded goal-conditioned agents
DefinitionGeneralAgentsWorldModels_BasicObjects of Sections 2-3 and Appendices A-B of Richens, Abel, Bellot and Everitt (2025).
- Environment (Definition 1, Assumption 1). A controlled Markov process on a finite state set and a finite action set is a transition function , with and ; it does not depend on (stationarity). It is communicating if for all there are states () and actions with . Assumption 1: the process is communicating and .
- Goals (Definition 2). A sub-goal is with and (Now, Next, Eventually).
- Sequential and composite goals (Definitions 3, 6, eq. (4)). A sequential goal is satisfied by a trajectory from time according to the recursion (4): for , and holds from ; for , and holds from ; for , there is with for , , and holds from (strong until). The empty sequence is always satisfied. A composite goal is a disjunction of sequential goals, evaluated from . is the set of composite goals with at least one disjunct, each disjunct having between and sub-goals.
- Policies and success probability (eq. (5)). A deterministic policy maps a history to an action ; a goal-conditioned policy is a policy for each composite goal . is the probability that the trajectory generated by from satisfies ; is the supremum over deterministic history-dependent policies.
- Agents (Definitions 4, 5). is optimal for a set of goals if for all and all . is a bounded agent with failure rate and maximum depth if for all and all . is the set of goals in all of whose sub-goals use .
- Goal families of Lemma 6 and Theorem 1. For an outcome pattern , the sequential goal has if and if . Lemma 6's is the disjunction over all with ones. In the proof of Theorem 1, starts with and ranges over patterns with at most ones, starts with and ranges over patterns with more than ones, and ; its depth is .
- Binomial quantities (eqs. (36)-(38)). , , .
- Extended action space (Lemma 2). is the type of optional actions, and a goal over is read over through the inclusion .
Formalization Note The temporal operators are encoded as the three elements of . A history is the list of past pairs together with the current state. Every goal built from is satisfied at a finite time, so the event is the increasing union over of the events "the first steps of already force "; is defined as the supremum over of the probabilities of these events, each a finite sum over state paths. The paper's initial-state condition in Definition 4 refers to no specified initial distribution and is read as "for every initial state "; Definition 5 is read the same way.
import Mathlib
/-!
Richens, Abel, Bellot and Everitt, *General agents contain world models*, arXiv:2506.01622v5
(ICML 2025): controlled Markov processes and Assumption 1 (Section 2.2, Appendix A.2), goals,
sequential goals and composite goals (Definitions 2, 3, 6 and eq. (4)), the probability that a
policy achieves a goal (eq. (5)), optimal and bounded goal-conditioned agents (Definitions 4, 5,
eqs. (1), (2)), myopic goals (Theorem 2), and the goal families and binomial quantities used in
Lemma 6 and in the proof of Theorem 1 (Appendix A.6, eqs. (25), (33)-(42)).
-/
noncomputable section
namespace GeneralAgentsWorldModels
/-- Definition 1 (p. 2, p. 10): a controlled Markov process with state set `S` and action set `A`,
given by its transition function `P s a s' = P(S_{t+1} = s' | A_t = a, S_t = s)`; each `P s a` is a
probability distribution on `S`. Stationarity (Assumption 1) is built in: `P` does not depend on
the time step. -/
structure CMP (S A : Type*) [Fintype S] where
/-- The transition function `P_{ss'}(a)`. -/
P : S → A → S → ℝ
nonneg : ∀ s a s', 0 ≤ P s a s'
sum_one : ∀ s a, ∑ s', P s a s' = 1
/-- Assumption 1 (p. 2, Section 2.2; proof of Lemma 1, p. 13): the cMP is communicating, i.e. every
state `s'` is reachable from every other state `s` by a finite sequence of transitions, each of
which has positive probability under some action. -/
def CMP.IsCommunicating {S A : Type*} [Fintype S] (M : CMP S A) : Prop :=
∀ s s' : S, s ≠ s' → Relation.TransGen (fun x y => ∃ a, 0 < M.P x a y) s s'
/-- Assumption 1 (p. 2, p. 10): the environment is a communicating, stationary, finite controlled
Markov process with at least two actions. Finiteness is the `Fintype` instances on `S` and `A`;
stationarity is built into `CMP`. -/
def SatisfiesAssumption1 {S A : Type*} [Fintype S] [Fintype A] (M : CMP S A) : Prop :=
M.IsCommunicating ∧ 2 ≤ Fintype.card A
/-- Definition 2 (p. 3, p. 10): the temporal operators `O ∈ {⊤, ◯, ◇}` (Now, Next, Eventually),
encoded as `Fin 3` with the names `opNow`, `opNext`, `opEventually`. -/
abbrev TempOp := Fin 3
/-- Definition 2: the operator Now, `⊤`. -/
def opNow : TempOp := 0
/-- Definition 2: the operator Next, `◯`. -/
def opNext : TempOp := 1
/-- Definition 2: the operator Eventually, `◇`. -/
def opEventually : TempOp := 2
/-- Definition 2 (p. 3, p. 10): a goal `φ = O([(s, a) ∈ g])`, with a temporal operator `O` and a set
`g ⊆ S × A` of goal states of the environment-agent system. -/
structure SubGoal (S A : Type*) where
/-- The temporal operator `O`. -/
op : TempOp
/-- The set of goal states `g`. -/
g : Set (S × A)
/-- Definitions 3 and 6 (p. 3, p. 11): a sequential goal `⟨φ₁, …, φ_L⟩`, the list of its sub-goals;
its depth is the length of the list. -/
abbrev SeqGoal (S A : Type*) := List (SubGoal S A)
/-- Definition 3 (p. 3, p. 12): a composite goal `ψ₁ ∨ ⋯ ∨ ψ_m`, the list of its disjuncts. -/
abbrev CompGoal (S A : Type*) := List (SeqGoal S A)
/-- Eq. (4) (Definition 6, p. 11): `τ, t ⊨ ⟨φ₁, …, φ_L⟩` for a trajectory
`τ = ((s₀, a₀), (s₁, a₁), …)` evaluated from time `t`:
`⊤`: `[(s_t, a_t) ∈ g₁] ∧ ⟨φ₂, …⟩` at `t`; `◯`: `[(s_{t+1}, a_{t+1}) ∈ g₁] ∧ ⟨φ₂, …⟩` at `t + 1`;
`◇`: `[(s, a) ∉ g₁] U ([(s, a) ∈ g₁] ∧ ⟨φ₂, …⟩)` (strong until). The empty sequence is `True`,
so that `⟨φ⟩ = φ`. -/
def seqSat {S A : Type*} (τ : ℕ → S × A) : SeqGoal S A → ℕ → Prop
| [], _ => True
| φ :: rest, t =>
if φ.op = opNow then τ t ∈ φ.g ∧ seqSat τ rest t
else if φ.op = opNext then τ (t + 1) ∈ φ.g ∧ seqSat τ rest (t + 1)
else ∃ u, t ≤ u ∧ (∀ v, t ≤ v → v < u → τ v ∉ φ.g) ∧ τ u ∈ φ.g ∧ seqSat τ rest u
/-- Definition 3: `τ ⊨ ψ₁ ∨ ⋯ ∨ ψ_m`, evaluation beginning at `t = 0` (Appendix A.3, p. 10). -/
def compSat {S A : Type*} (ψ : CompGoal S A) (τ : ℕ → S × A) : Prop :=
∃ φ ∈ ψ, seqSat τ φ 0
/-- Definition 3 (p. 3): `Ψ_n`, the composite goals of depth at most `n`: a disjunction of one or
more sequential goals, each with between `1` and `n` sub-goals. -/
def InPsi {S A : Type*} (n : ℕ) (ψ : CompGoal S A) : Prop :=
ψ ≠ [] ∧ ∀ φ ∈ ψ, φ ≠ [] ∧ φ.length ≤ n
/-- Section 2.4 (p. 3), Appendix A.4: a deterministic policy, mapping the history
`h_t = (s₀, a₀, …, s_t)`, given as the list of past pairs `(s_i, a_i)`, `i < t`, and the current
state `s_t`, to the action `a_t`. -/
abbrev Policy (S A : Type*) := List (S × A) → S → A
/-- Section 2.4 (p. 3): a deterministic goal-conditioned policy `π(a_t | h_t; ψ)`. -/
abbrev GCPolicy (S A : Type*) := CompGoal S A → Policy S A
/-- The probability that, from history `h` and current state `s`, the policy `π` produces the next
states `s'₁, …, s'_m` (the list `ss`): `∏ P_{s_i s_{i+1}}(π(h_i))`. -/
def pathWeight {S A : Type*} [Fintype S] (M : CMP S A) (π : Policy S A) :
List (S × A) → S → List S → ℝ
| _, _, [] => 1
| h, s, s' :: ss => M.P s (π h s) s' * pathWeight M π (h ++ [(s, π h s)]) s' ss
/-- The finite trajectory `(s₀, a₀, …, s_m, a_m)` produced by the deterministic policy `π` from
history `h` and current state `s` when the next states are `ss`. -/
def pathPrefix {S A : Type*} (π : Policy S A) : List (S × A) → S → List S → List (S × A)
| h, s, [] => h ++ [(s, π h s)]
| h, s, s' :: ss => pathPrefix π (h ++ [(s, π h s)]) s' ss
/-- A finite trajectory `pre` already guarantees `ψ`: every infinite trajectory beginning with
`pre` satisfies `ψ`. -/
def Decides {S A : Type*} (ψ : CompGoal S A) (pre : List (S × A)) : Prop :=
∀ τ : ℕ → S × A, (∀ i (hi : i < pre.length), τ i = pre[i]) → compSat ψ τ
open Classical in
/-- The probability that the first `T + 1` steps of the trajectory of `π` from `s₀` already
guarantee `ψ`. -/
def succProbUpTo {S A : Type*} [Fintype S] (M : CMP S A) (ψ : CompGoal S A) (π : Policy S A)
(s₀ : S) (T : ℕ) : ℝ :=
∑ ss : Fin T → S, pathWeight M π [] s₀ (List.ofFn ss) *
if Decides ψ (pathPrefix π [] s₀ (List.ofFn ss)) then 1 else 0
/-- Eq. (5) (p. 12): `P(τ ⊨ ψ | π, s₀)`, the probability that the trajectory generated by `π` from
`s₀` satisfies `ψ`. Every goal built from `⊤`, `◯`, `◇` is satisfied only at a finite time, so the
event `τ ⊨ ψ` is the increasing union over `T` of the events decided by the first `T + 1` steps, and
its probability is the supremum of their probabilities. -/
def succProb {S A : Type*} [Fintype S] (M : CMP S A) (ψ : CompGoal S A) (π : Policy S A)
(s₀ : S) : ℝ :=
⨆ T : ℕ, succProbUpTo M ψ π s₀ T
/-- Eqs. (1), (2): `max_π P(τ ⊨ ψ | π, s₀)`, the supremum over deterministic history-dependent
policies. -/
def optProb {S A : Type*} [Fintype S] (M : CMP S A) (ψ : CompGoal S A) (s₀ : S) : ℝ :=
⨆ π : Policy S A, succProb M ψ π s₀
/-- Definition 4 (p. 3, p. 12), eq. (1): `π` is an optimal goal-conditioned agent for the set of
goals `Ψ`: for every `ψ ∈ Ψ` and every initial state `s₀`, `π(· ; ψ)` attains
`max_π P(τ ⊨ ψ | π, s₀)`. -/
def IsOptimalAgent {S A : Type*} [Fintype S] (M : CMP S A) (Ψ : Set (CompGoal S A))
(π : GCPolicy S A) : Prop :=
∀ ψ ∈ Ψ, ∀ s₀ : S, succProb M ψ (π ψ) s₀ = optProb M ψ s₀
/-- Definition 5 (p. 4, p. 13), eq. (2): `π` is a bounded goal-conditioned agent with failure rate
`δ ∈ [0, 1]` and maximum goal depth `n`: for all `ψ ∈ Ψ_n` and all initial states `s₀`,
`P(τ ⊨ ψ | π, s₀) ≥ max_π P(τ ⊨ ψ | π, s₀) (1 - δ)`. -/
def IsBoundedAgent {S A : Type*} [Fintype S] (M : CMP S A) (π : GCPolicy S A) (δ : ℝ) (n : ℕ) :
Prop :=
0 ≤ δ ∧ δ ≤ 1 ∧ ∀ ψ : CompGoal S A, InPsi n ψ → ∀ s₀ : S,
optProb M ψ s₀ * (1 - δ) ≤ succProb M ψ (π ψ) s₀
/-- Theorem 2 (p. 5): `Ψ_myopic`, the depth-1 composite goals all of whose sub-goals have the form
`◯[(s, a) ∈ g]`. -/
def PsiMyopic {S A : Type*} : Set (CompGoal S A) :=
{ψ | InPsi 1 ψ ∧ ∀ φ ∈ ψ, ∀ x ∈ φ, x.op = opNext}
/-- The sub-goal `⊤([A = a])`: take action `a` now. -/
def nowAct {S A : Type*} (a : A) : SubGoal S A := ⟨opNow, {x | x.2 = a}⟩
/-- The sub-goal `◇([S = s, A = a])`: eventually be in state `s` and take action `a`. -/
def evVisit {S A : Type*} (s : S) (a : A) : SubGoal S A := ⟨opEventually, {(s, a)}⟩
/-- The sub-goal `◇([S = s'])`: eventually reach state `s'` (Lemma 1). -/
def evState {S A : Type*} (s' : S) : SubGoal S A := ⟨opEventually, {x | x.1 = s'}⟩
/-- The sub-goal `◯([S ∈ Y])`: the next state lies in `Y`. -/
def nextIn {S A : Type*} (Y : Set S) : SubGoal S A := ⟨opNext, {x | x.1 ∈ Y}⟩
/-- Lemma 6 (p. 16), eq. (33): the sequential goal `⟨[A = b], ◇[S = s, A = a], O₁, …,
◇[S = s, A = a], O_m⟩` with `O_i = ◯[S = s']` if `o_i` is true and `O_i = ◯[S ≠ s']` otherwise. -/
def trialSeq {S A : Type*} (b : A) (s : S) (a : A) (s' : S) (o : List Bool) : SeqGoal S A :=
nowAct b :: o.flatMap fun c => [evVisit s a, nextIn (if c then {s'} else {s'}ᶜ)]
open Classical in
/-- Lemma 6 (p. 16) and the proof of Theorem 1 (pp. 17-18): the disjunction of the sequential goals
`trialSeq b s a s' o` over all outcome patterns `o ∈ {0,1}^m` whose number of successes satisfies
`R`. Lemma 6's `ψ(r, n)` is `R = (· = r)`. -/
def binGoal {S A : Type*} (b : A) (s : S) (a : A) (s' : S) (m : ℕ) (R : ℕ → Prop) :
CompGoal S A :=
((Finset.univ.filter fun o : Fin m → Bool =>
R (Finset.univ.filter fun i => o i = true).card).toList).map
fun o => trialSeq b s a s' (List.ofFn o)
/-- Proof of Theorem 1 (pp. 17-18), eq. (34): `ψ_{a,b}(k, m) = ψ_a(k, m) ∨ ψ_b(k, m)`, where
`ψ_a(k, m)` starts with action `a` and asks for at most `k` transitions `(s, a) → s'` in `m`
attempts, and `ψ_b(k, m)` starts with action `b` and asks for more than `k`. -/
def abGoal {S A : Type*} (a b : A) (s s' : S) (m k : ℕ) : CompGoal S A :=
binGoal a s a s' m (· ≤ k) ++ binGoal b s a s' m (k < ·)
/-- Eq. (37) (p. 18): the binomial probability mass function
`P_m(X = r) = C(m, r) p^r (1 - p)^{m - r}`. -/
def binPmf (m : ℕ) (p : ℝ) (r : ℕ) : ℝ := (m.choose r : ℝ) * p ^ r * (1 - p) ^ (m - r)
/-- Eq. (36) (p. 18): `P_m(X ≤ k) = ∑_{r = 0}^{k} P_m(X = r)`. -/
def binLE (m : ℕ) (p : ℝ) (k : ℕ) : ℝ := ∑ r ∈ Finset.range (k + 1), binPmf m p r
/-- Eqs. (38), (42) (pp. 18-19): `P_m(X ≥ k) = ∑_{r = k}^{m} P_m(X = r)`; `P_m(X > k)` is
`binGE m p (k + 1)`. -/
def binGE (m : ℕ) (p : ℝ) (k : ℕ) : ℝ := ∑ r ∈ Finset.Icc k m, binPmf m p r
/-- Lemma 2 (p. 14): a goal of the environment with action set `A`, read in the environment with the
extended action set `A' = A ∪ {ā}` (`Option A`, with `ā = none`). -/
def liftGoal {S A : Type*} (ψ : CompGoal S A) : CompGoal S (Option A) :=
ψ.map (List.map fun φ => ⟨φ.op, (fun x : S × A => (x.1, some x.2)) '' φ.g⟩)
end GeneralAgentsWorldModels
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Controlled Markov process (CMP). Let be a finite type of states and any type of actions. may be infinite or empty, and no finiteness is required of it here. A controlled Markov process on consists of a real-valued function , written for and , satisfying:
- for all ;
- for all .
has no time argument. No other data are part of the structure: there is no initial distribution and no reward. If is empty, both conditions hold vacuously.
Communicating (CMP.IsCommunicating). is communicating when, for every pair of distinct states , there is a finite chain
such that each step has positive probability under some action:
The actions may differ from step to step. Nothing is required for . If has at most one element, the condition holds vacuously.
Assumption 1 (SatisfiesAssumption1). Here both and are finite. satisfies the assumption iff:
- is communicating in the sense above; and
- has at least elements.
Temporal operators (TempOp, opNow, opNext, opEventually). A temporal operator is one of three values :
- ("Now") is ;
- ("Next") is ;
- ("Eventually") is .
Sub-goal (SubGoal). A sub-goal is a pair , where is one of the three operators and is an arbitrary set of state–action pairs. The set may be empty or all of .
Sequential goal (SeqGoal). A sequential goal is a finite list of sub-goals. The empty list () is allowed.
Composite goal (CompGoal). A composite goal is a finite list of sequential goals, read as the disjunction . The empty list () is allowed, and any may be the empty sequence.
Satisfaction of a sequential goal (seqSat). Let be an arbitrary infinite sequence of state–action pairs; it need not be compatible with any process. Satisfaction "" is defined recursively on the list, with :
- Empty list: always true, at every .
- : and .
- : and .
- : there exists such that
In the case, is therefore the first time at which is hit. The remaining goals are evaluated starting at that same time , not at , so a following sub-goal refers to itself. If , the , and clauses are all false.
Satisfaction of a composite goal (compSat). means that some listed sequential goal satisfies . Two edge cases follow:
- If is the empty list, no satisfies it.
- If some is the empty sequence, every satisfies .
Goals of depth at most (InPsi). means:
- is a nonempty list; and
- every in it is a nonempty sequence of length at most .
For no qualifies.
Policy (Policy). A policy is a deterministic function assigning an action to every finite list of state–action pairs and every state . It is defined on all such lists, not only on histories that can actually occur. If is empty and is nonempty, no policy exists.
Goal-conditioned policy (GCPolicy). A goal-conditioned policy assigns a policy to every composite goal .
Path weight (pathWeight). Fix a history , a current state , and a list of next states . Set
The weight is
For the weight is .
Path prefix (pathPrefix). With the same notation, the prefix is the finite list
It begins with the given history and has length . In particular it includes the action taken at the last state.
Decides (Decides). A finite list of state–action pairs decides iff every infinite sequence with for all satisfies . The quantification ranges over all continuations , including continuations that no policy or transition kernel could produce.
Success probability up to (succProbUpTo). For a goal , a policy , an initial state and :
Here the trajectory is generated from the empty history. For the value is if the single pair decides , and otherwise.
Success probability (succProb). The success probability is
This is a supremum of real numbers. It uses the convention that a set of reals not bounded above has supremum . No measure on infinite trajectories is constructed; the quantity is defined only through this supremum.
Optimal success probability (optProb). The optimal success probability is
taken over all deterministic, history-dependent policies . It uses the same real-supremum conventions:
- If the set of values is not bounded above, the result is .
- If there are no policies at all ( empty, nonempty), the supremum is over an empty set and is also .
Optimal agent (IsOptimalAgent). Given a set of composite goals, a goal-conditioned policy is optimal for iff, for every and every ,
The condition is equality, not merely "". If is empty, every qualifies.
Bounded agent (IsBoundedAgent). For a real and , is a bounded agent with failure rate and depth iff and, for every and every ,
Two degenerate cases:
- For , is empty, so the condition reduces to .
- For , the inequality reads .
Myopic goals (PsiMyopic). is the set of all of whose sub-goals use the operator . Equivalently, is a nonempty list of one-element sequences , read as
where the sets are arbitrary.
Named sub-goals (nowAct, evVisit, evState, nextIn).
- .
- .
- .
- for any .
Trial sequence (trialSeq). For actions , states , and a finite Boolean list , the trial sequence is the sequential goal
where
Its length is . For it is just . The actions and are not required to differ.
Binomial goal (binGoal). For actions , states , , and an arbitrary predicate on , the binomial goal is the composite goal listing once for each such that
holds. The order of the disjuncts is unspecified. If no pattern satisfies , the list is empty and no trajectory satisfies the goal.
Two-action goal (abGoal). For actions , states , and , the two-action goal is the concatenation of two binomial goals:
- The first part starts with action and has at most true outcomes.
- The second part starts with action and has more than true outcomes.
- Both parts use visits to the pair .
Binomial mass function (binPmf). For and any real (not restricted to ):
Here is truncated at and . For the value is because .
Lower tail (binLE). For :
may exceed ; the extra terms are .
Upper tail (binGE). For :
This is when .
Lifted goal (liftGoal). Given a composite goal over , its lift is a composite goal over , where adds one new action . The lift has the same list structure and the same operators. Each goal set is replaced by
so the new action appears in no goal set.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.