|
| 1 | +{-# OPTIONS --safe --without-K #-} |
| 2 | + |
| 3 | +-- AntiEchoTropicalGeneric — generic-codomain lift of |
| 4 | +-- `AntiEchoTropical.agda`'s tropical decomposition. |
| 5 | +-- |
| 6 | +-- The concrete module `AntiEchoTropical.agda` works on the specific |
| 7 | +-- candidate scoring `score : Candidate → ℕ`. This module lifts the |
| 8 | +-- same decomposition to *any* ordered codomain interface — a carrier |
| 9 | +-- `B`, an order `_≤_`, a strict order `_<_`, the bound-against-strict |
| 10 | +-- conversion `≤⇒¬<` (always available), and the not-strict-below |
| 11 | +-- conversion `¬<⇒≤` (the decidability content; concretely |
| 12 | +-- `Data.Nat.Properties.¬<⇒≥`-shape, but ANY witness suffices). |
| 13 | +-- |
| 14 | +-- Three theorems land over the interface: |
| 15 | +-- * `antiecho-tropical-decompose-gen` — Σ-reshape iso; structural, |
| 16 | +-- `refl` round-trips, does *not* need the order at all. |
| 17 | +-- * `optimality-iso-gen` — `(∀ z → y ≤ s z)` ↔ `(∀ z → s z < y → ⊥)`, |
| 18 | +-- using `≤⇒¬<` / `¬<⇒≤` from the interface. |
| 19 | +-- * `tropdecomp-antiecho-to-gen` / `-from-gen` — the composite |
| 20 | +-- `TropEcho-like ↔ Echo × Π-of-AntiEcho-flavoured-misses`. |
| 21 | +-- |
| 22 | +-- Scope. The Σ-shapes for `IsArgmin-like` and `TropEcho-like` are |
| 23 | +-- replayed locally over the interface (`IsArgminG` and `TropEchoG`) |
| 24 | +-- because the concrete `IsArgmin` / `TropEcho` in `EchoTropical.agda` |
| 25 | +-- are pinned to `Candidate → ℕ`. The concrete module is *unchanged* |
| 26 | +-- and remains the canonical landing for the ℕ-scored case; this |
| 27 | +-- module is the abstract sibling, not a replacement. |
| 28 | +-- |
| 29 | +-- The original concrete module discharged the obligation comment |
| 30 | +-- "a generic-codomain version is deferred (would need a `≤`-bearing |
| 31 | +-- ordered codomain)" — that obligation is now landed here. |
| 32 | + |
| 33 | +module AntiEchoTropicalGeneric where |
| 34 | + |
| 35 | +open import Data.Empty using (⊥) |
| 36 | +open import Data.Product.Base using (Σ; _×_; _,_; proj₁; proj₂) |
| 37 | +open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 38 | +open import Relation.Binary.PropositionalEquality using (_≡_; refl) |
| 39 | + |
| 40 | +open import Echo using (Echo) |
| 41 | + |
| 42 | +---------------------------------------------------------------------- |
| 43 | +-- Ordered codomain interface. |
| 44 | +-- |
| 45 | +-- The minimum structure needed to land the tropical decomposition at |
| 46 | +-- a generic codomain. `B` is the codomain carrier; `_≤_` is the bound |
| 47 | +-- relation used on the optimality side; `_<_` is the strict order |
| 48 | +-- used in the AntiEcho-flavoured restatement; `≤⇒¬<` and `¬<⇒≤` are |
| 49 | +-- the two order conversions. Note that `¬<⇒≤` is the entire content |
| 50 | +-- of the "decidable order" hypothesis in the concrete ℕ case — |
| 51 | +-- factored out here as a field rather than baked into the codomain. |
| 52 | + |
| 53 | +record OrderedCodomain : Set₁ where |
| 54 | + field |
| 55 | + B : Set |
| 56 | + _≤_ : B → B → Set |
| 57 | + _<_ : B → B → Set |
| 58 | + ≤⇒¬< : ∀ {y n : B} → y ≤ n → n < y → ⊥ |
| 59 | + ¬<⇒≤ : ∀ {y n : B} → (n < y → ⊥) → y ≤ n |
| 60 | + |
| 61 | +---------------------------------------------------------------------- |
| 62 | +-- The generic decomposition, parameterised by `OrderedCodomain` and |
| 63 | +-- an abstract scoring function `s : C → B` from any candidate set to |
| 64 | +-- the ordered codomain. |
| 65 | + |
| 66 | +module Generic (OC : OrderedCodomain) where |
| 67 | + open OrderedCodomain OC |
| 68 | + |
| 69 | + -- Re-introduce the Σ-shapes locally over the abstract codomain. |
| 70 | + -- These mirror `EchoTropical.IsArgmin` / `TropEcho` exactly, with |
| 71 | + -- `Candidate → ℕ` replaced by an arbitrary `s : C → B`. |
| 72 | + |
| 73 | + IsArgminG : ∀ {C : Set} (s : C → B) → C → B → Set |
| 74 | + IsArgminG s x y = s x ≡ y × (∀ z → y ≤ s z) |
| 75 | + |
| 76 | + TropEchoG : ∀ {C : Set} (s : C → B) → B → Set |
| 77 | + TropEchoG {C = C} s y = Σ C (λ x → IsArgminG s x y) |
| 78 | + |
| 79 | + ------------------------------------------------------------------ |
| 80 | + -- The structural decomposition. Pure Σ-reshape; the order is not |
| 81 | + -- used. Both round-trips `refl`. |
| 82 | + |
| 83 | + antiecho-tropical-decompose-gen-to : |
| 84 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 85 | + TropEchoG s y → Echo s y × (∀ z → y ≤ s z) |
| 86 | + antiecho-tropical-decompose-gen-to (x , p , bnd) = (x , p) , bnd |
| 87 | + |
| 88 | + antiecho-tropical-decompose-gen-from : |
| 89 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 90 | + Echo s y × (∀ z → y ≤ s z) → TropEchoG s y |
| 91 | + antiecho-tropical-decompose-gen-from ((x , p) , bnd) = x , p , bnd |
| 92 | + |
| 93 | + antiecho-tropical-decompose-gen-to-from : |
| 94 | + ∀ {C : Set} {s : C → B} {y : B} |
| 95 | + (r : Echo s y × (∀ z → y ≤ s z)) → |
| 96 | + antiecho-tropical-decompose-gen-to |
| 97 | + (antiecho-tropical-decompose-gen-from r) ≡ r |
| 98 | + antiecho-tropical-decompose-gen-to-from ((x , p) , bnd) = refl |
| 99 | + |
| 100 | + antiecho-tropical-decompose-gen-from-to : |
| 101 | + ∀ {C : Set} {s : C → B} {y : B} |
| 102 | + (t : TropEchoG s y) → |
| 103 | + antiecho-tropical-decompose-gen-from |
| 104 | + (antiecho-tropical-decompose-gen-to t) ≡ t |
| 105 | + antiecho-tropical-decompose-gen-from-to (x , p , bnd) = refl |
| 106 | + |
| 107 | + antiecho-tropical-decompose-gen : |
| 108 | + ∀ {C : Set} (s : C → B) (y : B) → |
| 109 | + TropEchoG s y ↔ (Echo s y × (∀ z → y ≤ s z)) |
| 110 | + antiecho-tropical-decompose-gen s y = |
| 111 | + mk↔ₛ′ |
| 112 | + (λ t → antiecho-tropical-decompose-gen-to {s = s} {y = y} t) |
| 113 | + (λ r → antiecho-tropical-decompose-gen-from {s = s} {y = y} r) |
| 114 | + (λ r → antiecho-tropical-decompose-gen-to-from {s = s} {y = y} r) |
| 115 | + (λ t → antiecho-tropical-decompose-gen-from-to {s = s} {y = y} t) |
| 116 | + |
| 117 | + ------------------------------------------------------------------ |
| 118 | + -- AntiEcho-flavoured restatement of the optimality factor. The |
| 119 | + -- forward direction uses `≤⇒¬<` (always available); the backward |
| 120 | + -- uses `¬<⇒≤` (the decidability content of the interface). |
| 121 | + |
| 122 | + optimality-as-antiecho-flavour-gen-to : |
| 123 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 124 | + (∀ z → y ≤ s z) → (∀ z → s z < y → ⊥) |
| 125 | + optimality-as-antiecho-flavour-gen-to bnd z lt = ≤⇒¬< (bnd z) lt |
| 126 | + |
| 127 | + optimality-as-antiecho-flavour-gen-from : |
| 128 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 129 | + (∀ z → s z < y → ⊥) → (∀ z → y ≤ s z) |
| 130 | + optimality-as-antiecho-flavour-gen-from no-miss z = ¬<⇒≤ (no-miss z) |
| 131 | + |
| 132 | + ------------------------------------------------------------------ |
| 133 | + -- Composite: TropEchoG ↔ Echo × (Π-of-AntiEcho-flavoured-misses). |
| 134 | + -- Forward + backward only, no extensionality on the Π factor (the |
| 135 | + -- two Π-shaped sides are not propositionally equal in general |
| 136 | + -- without funext; we keep them as a section/retraction pair, which |
| 137 | + -- is what the concrete module already does). |
| 138 | + |
| 139 | + tropdecomp-antiecho-gen-to : |
| 140 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 141 | + TropEchoG s y → Echo s y × (∀ z → s z < y → ⊥) |
| 142 | + tropdecomp-antiecho-gen-to t |
| 143 | + with antiecho-tropical-decompose-gen-to t |
| 144 | + ... | (e , bnd) = e , optimality-as-antiecho-flavour-gen-to bnd |
| 145 | + |
| 146 | + tropdecomp-antiecho-gen-from : |
| 147 | + ∀ {C : Set} {s : C → B} {y : B} → |
| 148 | + Echo s y × (∀ z → s z < y → ⊥) → TropEchoG s y |
| 149 | + tropdecomp-antiecho-gen-from (e , no-miss) = |
| 150 | + antiecho-tropical-decompose-gen-from |
| 151 | + (e , optimality-as-antiecho-flavour-gen-from no-miss) |
| 152 | + |
| 153 | +---------------------------------------------------------------------- |
| 154 | +-- Sanity instance: the natural numbers. Builds an `OrderedCodomain` |
| 155 | +-- record at ℕ with the same `≤⇒¬<` / `¬<⇒≤` lemmas the concrete |
| 156 | +-- `AntiEchoTropical.agda` uses internally. Pinned so the interface |
| 157 | +-- is demonstrably inhabitable; downstream users can build their own |
| 158 | +-- instances at other ordered codomains (e.g. `Float`, integers, |
| 159 | +-- abstract semirings) without modifying this module. |
| 160 | + |
| 161 | +open import Data.Nat.Base using (ℕ; zero; suc; _≤_; _<_; z≤n; s≤s) |
| 162 | +open import Data.Empty using (⊥-elim) |
| 163 | + |
| 164 | +private |
| 165 | + ℕ-≤⇒¬< : ∀ {y n : ℕ} → y ≤ n → n < y → ⊥ |
| 166 | + ℕ-≤⇒¬< z≤n () |
| 167 | + ℕ-≤⇒¬< (s≤s p) (s≤s q) = ℕ-≤⇒¬< p q |
| 168 | + |
| 169 | + ℕ-¬<⇒≤ : ∀ {y n : ℕ} → (n < y → ⊥) → y ≤ n |
| 170 | + ℕ-¬<⇒≤ {y = zero} _ = z≤n |
| 171 | + ℕ-¬<⇒≤ {y = suc _} {n = zero} ¬p = ⊥-elim (¬p (s≤s z≤n)) |
| 172 | + ℕ-¬<⇒≤ {y = suc _} {n = suc _} ¬p = s≤s (ℕ-¬<⇒≤ (λ q → ¬p (s≤s q))) |
| 173 | + |
| 174 | +ℕ-ordered-codomain : OrderedCodomain |
| 175 | +ℕ-ordered-codomain = record |
| 176 | + { B = ℕ |
| 177 | + ; _≤_ = _≤_ |
| 178 | + ; _<_ = _<_ |
| 179 | + ; ≤⇒¬< = ℕ-≤⇒¬< |
| 180 | + ; ¬<⇒≤ = ℕ-¬<⇒≤ |
| 181 | + } |
0 commit comments