diff --git a/PolyFun/Control/Domain/PrefixRecursion.lean b/PolyFun/Control/Domain/PrefixRecursion.lean index 6cc933d2..bae86021 100644 --- a/PolyFun/Control/Domain/PrefixRecursion.lean +++ b/PolyFun/Control/Domain/PrefixRecursion.lean @@ -12,15 +12,15 @@ public import ToCslib.Order.PrefixRecursion /-! # Recursively generated partial arrays and streams -A lazy language can define a sequence by recursion on itself, with each entry -reading earlier ones. This module computes the meaning of such a definition, -for finite arrays indexed by `Fin n` and for lazy sequences of any length `n : ℕ∞`. -Both rest on `Function.UsesPrefix`: equations in which each coordinate reads only -earlier ones have at most one solution, even when entries are bottom or errors. +A lazy language can define a sequence by allowing each entry to depend on earlier ones. +`Function.UsesPrefix` makes the resulting equations determine at most one sequence, even +when some entries are bottom or errors. The finite array and stream results use this +uniqueness to identify a recursive definition with a solution of its coordinate equations. -A generator exposes an array constructor before its elements are demanded. -If every element uses only the strict prefix, its least fixed point agrees -with finite prefix recursion. Exposing the constructor is an explicit hypothesis. +`DomainResult.generateStep body` wraps the array from `body` in `DomainResult.value`, so its +fixed point exposes the array even when an entry is bottom or an error. To identify its entries +by finite prefix recursion, it suffices to give their equations on inputs of the form `value xs` +(`DomainResult.generateStep_fix_eq_prefixRecursion`). A recursive lazy sequence is a fixed point `X = mk n fun i => step i fun j => index j X` in stream form. Its step is *productive* below `n` (`LazySeq.Productive`) when, at each position @@ -45,7 +45,8 @@ namespace DomainResult variable {n : Nat} {ε D : Type*} [OmegaCompletePartialOrder D] -/-- The equations for a recursively generated array, retaining partial entries. -/ +/-- Apply `body` to the recursive input and wrap the resulting array in `value`. +This exposes the array constructor even when individual entries are partial. -/ @[expose] noncomputable def generateStep (body : DomainResult ε (Fin n → D) →𝒄 (Fin n → D)) : DomainResult ε (Fin n → D) →𝒄 DomainResult ε (Fin n → D) := @@ -68,7 +69,8 @@ theorem generateStep_fix_eq have hfix : body (value xs) = xs := by rw [← hx] exact hx.trans (congrArg value (hprefix.fixedPoint_unique hfix hresult)) -/-- A constructor-producing, prefix-dependent recurrence has its finite tabulated meaning. -/ +/-- If `body (value xs)` follows `step` on each strict prefix of `xs`, the least fixed point +returns the array computed by finite prefix recursion. -/ theorem generateStep_fix_eq_prefixRecursion (body : DomainResult ε (Fin n → D) →𝒄 (Fin n → D)) (step : (i : Fin n) → (Fin i.val → D) →𝒄 D) @@ -88,8 +90,9 @@ namespace LazySeq variable {ε X : Type*} [OmegaCompletePartialOrder X] -/-- A stream step is productive below `n` when, at every position `i` below `n`, it returns the -same element for any two streams that agree at the positions before `i`. -/ +/-- A stream step is productive below `n` when two streams that agree before `i` give the +same result at `i`, for every `i` below `n`. That result may be a value, an error, or bottom. +For example, a step that always returns bottom is productive at every length. -/ @[expose] def Productive (n : ℕ∞) (step : ℕ → (ℕ → DomainResult ε X) → DomainResult ε X) : Prop := ∀ i, below n i → ∀ p q, (∀ j < i, p j = q j) → step i p = step i q diff --git a/PolyFunTest/Domain/LazySeq.lean b/PolyFunTest/Domain/LazySeq.lean index 4fbb4a50..ef1a34e6 100644 --- a/PolyFunTest/Domain/LazySeq.lean +++ b/PolyFunTest/Domain/LazySeq.lean @@ -174,6 +174,10 @@ example (d : DomainResult String Entry) (x : ℕ) : /-! ## Productivity over a well-founded order -/ +/-- A step that always yields bottom satisfies the prefix condition at every length. -/ +example (n : ℕ∞) : LazySeq.Productive n (fun _ (_ : ℕ → Entry) => ⊥) := + fun _ _ _ _ _ => rfl + /-- A step that reads its own position is not productive. -/ example : ¬LazySeq.Productive (1 : ℕ∞) (fun i (xs : ℕ → Entry) => xs i) := by intro h diff --git a/ToCslib/Order/PrefixRecursion.lean b/ToCslib/Order/PrefixRecursion.lean index 61288cce..8dc62c04 100644 --- a/ToCslib/Order/PrefixRecursion.lean +++ b/ToCslib/Order/PrefixRecursion.lean @@ -12,11 +12,15 @@ public import Mathlib.Order.Fin.Basic /-! # Recurrences depending on earlier coordinates -Each coordinate may inspect only its strict prefix. Over a well-founded order of coordinates, -such equations have at most one solution (`Function.UsesPrefix.fixedPoint_unique`); the -coordinates can be `Fin n`, or `ℕ` for a stream. Over `Fin n` the solution is computed by -well-founded recursion. When the coordinate operations are continuous, this solution is also -their least fixed point. The values may themselves contain partial information or errors. +Two input families that agree before `i` give the same result at `i` for a map satisfying +`Function.UsesPrefix`. If `<` is well-founded, induction on `i` then shows that any two fixed +points agree (`Function.UsesPrefix.fixedPoint_unique`). This applies to finite arrays and to +streams indexed by `ℕ`, with arbitrary entry types. + +For an array of length `n`, `Function.prefixRecursion` constructs a solution by giving step `i` +access to entries indexed by `Fin i.val`. If the entries form an omega-CPO with bottom and +each step is continuous, `ContinuousHom.prefixMap_fix` identifies the least fixed point of the +simultaneous equations with this recursive solution. -/ public section @@ -25,7 +29,8 @@ namespace Function variable {n : Nat} {ι α : Type*} -/-- Compute a finite recurrence using only already computed coordinates. -/ +/-- Compute entry `i` of a finite recurrence. The lookup supplied to `step i` accepts only +indices below `i`. -/ @[expose] def prefixRecursion (step : (i : Fin n) → (Fin i.val → α) → α) (i : Fin n) : α := step i (fun j => prefixRecursion step ⟨j.val, j.isLt.trans i.isLt⟩) termination_by i.val diff --git a/docs/guides/itree.md b/docs/guides/itree.md index 099783ca..c0fd54e4 100644 --- a/docs/guides/itree.md +++ b/docs/guides/itree.md @@ -313,16 +313,15 @@ interprets finite cutoff leaves as bottom and proves that the increasing truncation folds have the full denotation as their supremum. These theorems do not require finite branching or a uniform finite depth for a well-founded tree. -[`DomainResult.generateStep_fix_eq`](../../PolyFun/Control/Domain/PrefixRecursion.lean) -solves a finite recursive array by its strict-prefix equations. The generator -must expose its outer constructor; entries may still be bottom or errors. -`Function.UsesPrefix` expresses that coordinate `i` observes only coordinates -strictly before `i`, over any well-founded order of coordinates, and yields uniqueness -without requiring total entries. -`generateStep_fix_eq_prefixRecursion` connects the least fixed point to the -well-founded `Function.prefixRecursion` in `ToCslib/Order/PrefixRecursion.lean`. -Self-dependent equations and generators that demand the whole array before -returning its constructor do not satisfy this contract. +`DomainResult.generateStep body` wraps the generated array in `value`, so even an array with +a divergent first entry has an outer constructor. To apply `DomainResult.generateStep_fix_eq`, +supply a solution of `body (value xs) = xs` and prove that coordinate `i` depends only on entries +before `i`. Well-founded induction then makes the solution unique. An equation such as +`xs[0] = xs[0]` would leave the first entry undetermined. When the body on `value xs` is given by +continuous steps on `Fin i.val`, `generateStep_fix_eq_prefixRecursion` gives the solution directly. +The lazy-language tutorial uses this to solve `xs = [first, xs[0] + 1, xs[1] + 1]` for any +`first`, including bottom. [Generator equations](../../PolyFun/Control/Domain/PrefixRecursion.lean) +and [worked example](../../Examples/Tutorials/LazyLanguages.lean). A recursive lazy sequence is a fixed point in stream form, `X = mk n fun i => step i fun j => index j X`. Its step is productive below `n`