|
| 1 | +{-# OPTIONS --safe --without-K --cubical-compatible --exact-split --no-sized-types --no-guardedness #-} |
| 2 | +-- SPDX-License-Identifier: MPL-2.0 |
| 3 | +-- SPDX-FileCopyrightText: 2025-2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 4 | + |
| 5 | +-- EchoFibreIntersection: multi-witness narrowing (squeakwell rung R8). |
| 6 | +-- |
| 7 | +-- A recovery engine that has observed several modalities of one entity |
| 8 | +-- asks for the witnesses satisfying ALL the observations at once. In |
| 9 | +-- Echo terms that is the intersection of fibres over one shared |
| 10 | +-- witness: `Σ A (λ x → f x ≡ y × g x ≡ z)`. This module pins the three |
| 11 | +-- facts hyperpolymath/squeakwell's proof ladder (docs/ROUTE-PLAN.adoc, |
| 12 | +-- rung R8) needs about that intersection: |
| 13 | +-- |
| 14 | +-- * Binary narrowing is a fibre. Intersecting the fibres of `f` at |
| 15 | +-- `y` and of `g` at `z` IS the fibre of the pairing `< f , g >` at |
| 16 | +-- `(y , z)`: `fibre-intersection-iso`, a K-free `_↔_` with `refl` |
| 17 | +-- round-trips (no funext — the equations are on pairs, not on |
| 18 | +-- functions). |
| 19 | +-- * Narrowing never changes the witness. Forgetting either |
| 20 | +-- constraint is witness-preserving (`forget-left-witness`, |
| 21 | +-- `forget-right-witness`), and adding one more constraint to a |
| 22 | +-- family of `n` constraints is again witness-preserving in both |
| 23 | +-- directions (`narrow-fin-step-to`, `narrow-fin-step-from`). The |
| 24 | +-- `Fin n` family form is stated with pointwise equations, so it |
| 25 | +-- needs no funext; it is NOT packaged as a `_↔_` because the |
| 26 | +-- round-trip on the function side would. |
| 27 | +-- * Narrowing shrinks the candidate fibre. On a finite domain with |
| 28 | +-- decidable equality, the honest count of `EchoFiberCount` can |
| 29 | +-- only go down when a constraint is added: `narrow-shrinks` |
| 30 | +-- (`FiberSize-fin < f , g > (y , z) ≤ FiberSize-fin f y`). This is |
| 31 | +-- the quantitative statement behind squeakwell's review queue: |
| 32 | +-- every extra observed modality narrows the candidates. |
| 33 | +-- |
| 34 | +-- Honest scope. The survey's proposed "covering masks ⇒ subsingleton |
| 35 | +-- fibre" is NOT here: over an opaque value type two full sections that |
| 36 | +-- agree on every modality need not be equal without extensionality, and |
| 37 | +-- squeakwell's identifiers are opaque. The route plan dropped it; so |
| 38 | +-- does this module. Nothing here claims the intersection is inhabited: |
| 39 | +-- observations can be jointly unsatisfiable, and the count can be 0. |
| 40 | +-- |
| 41 | +-- Headlines (pinned in Smoke.agda): |
| 42 | +-- |
| 43 | +-- * fibre-intersection-iso -- Echo < f , g > (y , z) ↔ Narrow f g y z |
| 44 | +-- * forget-left-witness -- forgetting a constraint keeps the witness |
| 45 | +-- * forget-right-witness |
| 46 | +-- * narrow-fin-step-to -- Fin (suc n) family = one more binary step |
| 47 | +-- * narrow-fin-step-from |
| 48 | +-- * narrow-shrinks -- FiberSize-fin of the pairing ≤ FiberSize-fin of f |
| 49 | + |
| 50 | +module EchoFibreIntersection where |
| 51 | + |
| 52 | +open import Function.Base using (_∘_) |
| 53 | +open import Level using (Level; _⊔_) |
| 54 | +open import Data.Empty using (⊥; ⊥-elim) |
| 55 | +import Data.Nat.Base as ℕ |
| 56 | +open ℕ using (ℕ; _≤_; z≤n; s≤s) |
| 57 | +open import Data.Fin.Base using (Fin; zero; suc) |
| 58 | +open import Data.Product.Base using (Σ; _×_; _,_; proj₁; proj₂; <_,_>) |
| 59 | +open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 60 | +open import Relation.Nullary.Decidable.Core using (Dec; yes; no) |
| 61 | +open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong; cong₂) |
| 62 | + |
| 63 | +open import Echo using (Echo) |
| 64 | +open import EchoFiberCount using (FiberSize-fin) |
| 65 | + |
| 66 | +private |
| 67 | + variable |
| 68 | + ℓa ℓb ℓc : Level |
| 69 | + A : Set ℓa |
| 70 | + B : Set ℓb |
| 71 | + C : Set ℓc |
| 72 | + |
| 73 | +---------------------------------------------------------------------- |
| 74 | +-- Binary narrowing: two constraints on one witness. |
| 75 | +---------------------------------------------------------------------- |
| 76 | + |
| 77 | +-- The witnesses `x` with `f x ≡ y` AND `g x ≡ z`: the intersection of |
| 78 | +-- the two fibres over a shared witness. |
| 79 | +Narrow : (f : A → B) (g : A → C) → B → C → Set _ |
| 80 | +Narrow {A = A} f g y z = Σ A (λ x → (f x ≡ y) × (g x ≡ z)) |
| 81 | + |
| 82 | +pair-fibre→narrow : |
| 83 | + {f : A → B} {g : A → C} {y : B} {z : C} → |
| 84 | + Echo < f , g > (y , z) → Narrow f g y z |
| 85 | +pair-fibre→narrow (x , p) = x , cong proj₁ p , cong proj₂ p |
| 86 | + |
| 87 | +narrow→pair-fibre : |
| 88 | + {f : A → B} {g : A → C} {y : B} {z : C} → |
| 89 | + Narrow f g y z → Echo < f , g > (y , z) |
| 90 | +narrow→pair-fibre (x , p , q) = x , cong₂ _,_ p q |
| 91 | + |
| 92 | +-- Round trips. Both close by matching the equation on `refl`; the |
| 93 | +-- indices `y`, `z` are variables, so this is `--without-K`-safe. |
| 94 | +narrow-to-from : |
| 95 | + {f : A → B} {g : A → C} {y : B} {z : C} → |
| 96 | + (e : Echo < f , g > (y , z)) → |
| 97 | + narrow→pair-fibre {f = f} {g = g} (pair-fibre→narrow e) ≡ e |
| 98 | +narrow-to-from (x , refl) = refl |
| 99 | + |
| 100 | +narrow-from-to : |
| 101 | + {f : A → B} {g : A → C} {y : B} {z : C} → |
| 102 | + (w : Narrow f g y z) → |
| 103 | + pair-fibre→narrow {f = f} {g = g} (narrow→pair-fibre w) ≡ w |
| 104 | +narrow-from-to (x , refl , refl) = refl |
| 105 | + |
| 106 | +-- Headline 1: binary narrowing IS the fibre of the pairing. |
| 107 | +fibre-intersection-iso : |
| 108 | + (f : A → B) (g : A → C) (y : B) (z : C) → |
| 109 | + Echo < f , g > (y , z) ↔ Narrow f g y z |
| 110 | +fibre-intersection-iso f g y z = |
| 111 | + mk↔ₛ′ pair-fibre→narrow narrow→pair-fibre |
| 112 | + (narrow-from-to {f = f} {g = g}) (narrow-to-from {f = f} {g = g}) |
| 113 | + |
| 114 | +---------------------------------------------------------------------- |
| 115 | +-- Forgetting a constraint keeps the witness. |
| 116 | +---------------------------------------------------------------------- |
| 117 | + |
| 118 | +forget-left : |
| 119 | + {f : A → B} {g : A → C} {y : B} {z : C} → Narrow f g y z → Echo g z |
| 120 | +forget-left (x , _ , q) = x , q |
| 121 | + |
| 122 | +forget-right : |
| 123 | + {f : A → B} {g : A → C} {y : B} {z : C} → Narrow f g y z → Echo f y |
| 124 | +forget-right (x , p , _) = x , p |
| 125 | + |
| 126 | +-- Headline 2: narrowing is a refinement of the SAME candidate, never a |
| 127 | +-- different one. Both are definitional. |
| 128 | +forget-left-witness : |
| 129 | + {f : A → B} {g : A → C} {y : B} {z : C} (w : Narrow f g y z) → |
| 130 | + proj₁ (forget-left {f = f} w) ≡ proj₁ w |
| 131 | +forget-left-witness _ = refl |
| 132 | + |
| 133 | +forget-right-witness : |
| 134 | + {f : A → B} {g : A → C} {y : B} {z : C} (w : Narrow f g y z) → |
| 135 | + proj₁ (forget-right {g = g} w) ≡ proj₁ w |
| 136 | +forget-right-witness _ = refl |
| 137 | + |
| 138 | +---------------------------------------------------------------------- |
| 139 | +-- Families of constraints indexed by `Fin n`. |
| 140 | +---------------------------------------------------------------------- |
| 141 | + |
| 142 | +-- `n` observations `fs i x ≡ ys i` on one witness `x`, stated pointwise |
| 143 | +-- (no function equality, hence no funext). |
| 144 | +NarrowFin : ∀ {n} (fs : Fin n → A → B) (ys : Fin n → B) → Set _ |
| 145 | +NarrowFin {A = A} fs ys = Σ A (λ x → ∀ i → fs i x ≡ ys i) |
| 146 | + |
| 147 | +-- Headline 3: a family of `suc n` constraints is one binary narrowing |
| 148 | +-- step on top of a family of `n`, in both directions, and the witness |
| 149 | +-- is untouched (both maps are `refl` on `proj₁`). |
| 150 | +narrow-fin-step-to : |
| 151 | + ∀ {n} (fs : Fin (ℕ.suc n) → A → B) (ys : Fin (ℕ.suc n) → B) → |
| 152 | + NarrowFin fs ys → |
| 153 | + Σ A (λ x → (fs zero x ≡ ys zero) × (∀ i → fs (suc i) x ≡ ys (suc i))) |
| 154 | +narrow-fin-step-to fs ys (x , h) = x , h zero , (λ i → h (suc i)) |
| 155 | + |
| 156 | +narrow-fin-step-from : |
| 157 | + ∀ {n} (fs : Fin (ℕ.suc n) → A → B) (ys : Fin (ℕ.suc n) → B) → |
| 158 | + Σ A (λ x → (fs zero x ≡ ys zero) × (∀ i → fs (suc i) x ≡ ys (suc i))) → |
| 159 | + NarrowFin fs ys |
| 160 | +narrow-fin-step-from fs ys (x , p , h) = x , step |
| 161 | + where |
| 162 | + step : ∀ i → fs i x ≡ ys i |
| 163 | + step zero = p |
| 164 | + step (suc i) = h i |
| 165 | + |
| 166 | +narrow-fin-step-to-witness : |
| 167 | + ∀ {n} (fs : Fin (ℕ.suc n) → A → B) (ys : Fin (ℕ.suc n) → B) |
| 168 | + (w : NarrowFin fs ys) → |
| 169 | + proj₁ (narrow-fin-step-to fs ys w) ≡ proj₁ w |
| 170 | +narrow-fin-step-to-witness _ _ _ = refl |
| 171 | + |
| 172 | +narrow-fin-step-from-witness : |
| 173 | + ∀ {n} (fs : Fin (ℕ.suc n) → A → B) (ys : Fin (ℕ.suc n) → B) |
| 174 | + (w : Σ A (λ x → (fs zero x ≡ ys zero) × (∀ i → fs (suc i) x ≡ ys (suc i)))) → |
| 175 | + proj₁ (narrow-fin-step-from fs ys w) ≡ proj₁ w |
| 176 | +narrow-fin-step-from-witness _ _ _ = refl |
| 177 | + |
| 178 | +---------------------------------------------------------------------- |
| 179 | +-- Narrowing shrinks the honest fibre count. |
| 180 | +---------------------------------------------------------------------- |
| 181 | + |
| 182 | +-- Decidable equality on pairs from decidable equality on the components. |
| 183 | +dec-× : |
| 184 | + ((b₁ b₂ : B) → Dec (b₁ ≡ b₂)) → |
| 185 | + ((c₁ c₂ : C) → Dec (c₁ ≡ c₂)) → |
| 186 | + (p q : B × C) → Dec (p ≡ q) |
| 187 | +dec-× _≟B_ _≟C_ (b , c) (b′ , c′) with b ≟B b′ | c ≟C c′ |
| 188 | +... | yes refl | yes refl = yes refl |
| 189 | +... | yes _ | no ¬q = no (λ e → ¬q (cong proj₂ e)) |
| 190 | +... | no ¬p | yes _ = no (λ e → ¬p (cong proj₁ e)) |
| 191 | +... | no ¬p | no _ = no (λ e → ¬p (cong proj₁ e)) |
| 192 | + |
| 193 | +private |
| 194 | + ≤-step : ∀ {m n} → m ≤ n → m ≤ ℕ.suc n |
| 195 | + ≤-step z≤n = z≤n |
| 196 | + ≤-step (s≤s p) = s≤s (≤-step p) |
| 197 | + |
| 198 | +-- Headline 4: adding an observation never enlarges the enumerated |
| 199 | +-- candidate fibre. Walks the `FiberSize-fin` recursion on both sides in |
| 200 | +-- lock-step: a hit for the pair is a hit for `f`; a hit for `f` alone |
| 201 | +-- is a candidate the extra observation has ruled out. |
| 202 | +narrow-shrinks : |
| 203 | + ∀ {n} (f : Fin n → B) (g : Fin n → C) (y : B) (z : C) |
| 204 | + (_≟B_ : (b₁ b₂ : B) → Dec (b₁ ≡ b₂)) |
| 205 | + (_≟C_ : (c₁ c₂ : C) → Dec (c₁ ≡ c₂)) → |
| 206 | + FiberSize-fin < f , g > (y , z) (dec-× _≟B_ _≟C_) ≤ FiberSize-fin f y _≟B_ |
| 207 | +narrow-shrinks {n = ℕ.zero} f g y z _≟B_ _≟C_ = z≤n |
| 208 | +narrow-shrinks {n = ℕ.suc m} f g y z _≟B_ _≟C_ |
| 209 | + with f zero ≟B y | dec-× _≟B_ _≟C_ (< f , g > zero) (y , z) |
| 210 | +... | yes _ | yes _ = s≤s (narrow-shrinks (f ∘ suc) (g ∘ suc) y z _≟B_ _≟C_) |
| 211 | +... | yes _ | no _ = ≤-step (narrow-shrinks (f ∘ suc) (g ∘ suc) y z _≟B_ _≟C_) |
| 212 | +... | no ¬p | yes q = ⊥-elim (¬p (cong proj₁ q)) |
| 213 | +... | no _ | no _ = narrow-shrinks (f ∘ suc) (g ∘ suc) y z _≟B_ _≟C_ |
| 214 | + |
| 215 | +---------------------------------------------------------------------- |
| 216 | +-- Companion remark. |
| 217 | +-- |
| 218 | +-- What is deliberately NOT claimed: |
| 219 | +-- |
| 220 | +-- * Inhabitation. `Narrow f g y z` may be empty; joint observations |
| 221 | +-- can be inconsistent. squeakwell reports that as an empty |
| 222 | +-- candidate fibre, routed to review (rung R13), not as a theorem. |
| 223 | +-- * Covering ⇒ subsingleton. The survey's "if the observed masks |
| 224 | +-- cover every modality the fibre is a subsingleton" needs equality |
| 225 | +-- of full sections from pointwise equality, i.e. extensionality, and |
| 226 | +-- opaque identifiers. Dropped in the route plan; not here. |
| 227 | +-- * A `_↔_` for the `Fin n` family step. The pointwise maps exist and |
| 228 | +-- preserve the witness; the function-side round trip would need |
| 229 | +-- funext and is left out rather than taken as a hypothesis. |
| 230 | +---------------------------------------------------------------------- |
0 commit comments