| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- A data structure which keeps track of an upper bound on the number |
| 5 |
-- of elements /not/ in a given list |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
open import Level using (0ℓ) |
| 11 |
open import Relation.Binary.Bundles using (DecSetoid) |
| 12 |
|
| 13 |
module Data.List.Countdown (D : DecSetoid 0ℓ 0ℓ) where |
| 14 |
|
| 15 |
open import Data.Fin.Base using (Fin; zero; suc; punchOut) |
| 16 |
open import Data.Fin.Properties using (suc-injective; punchOut-injective) |
| 17 |
open import Data.Bool.Base using (true; false) |
| 18 |
open import Data.List.Base hiding (lookup) |
| 19 |
open import Data.List.Relation.Unary.Any as Any using (here; there) |
| 20 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 21 |
open import Data.Product.Base using (∃; _,_; _×_) |
| 22 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂) |
| 23 |
open import Data.Sum.Properties using (inj₁-injective; inj₂-injective) |
| 24 |
open import Function.Base using (const; _$_; _∘_) |
| 25 |
open import Function.Bundles using (Injection; module Injection) |
| 26 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 27 |
using (_≡_; _≢_; refl; cong) |
| 28 |
open import Relation.Nullary.Reflects using (invert) |
| 29 |
open import Relation.Nullary.Decidable.Core using (Dec; yes; no; _because_) |
| 30 |
open import Relation.Nullary.Decidable using (dec-true; dec-false) |
| 31 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 32 |
|
| 33 |
import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 34 |
|
| 35 |
open ≡.≡-Reasoning |
| 36 |
|
| 37 |
private |
| 38 |
open module D = DecSetoid D |
| 39 |
hiding (refl) renaming (Carrier to Elem) |
| 40 |
open import Data.List.Membership.Setoid D.setoid |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Helper functions |
| 44 |
|
| 45 |
private |
| 46 |
|
| 47 |
-- The /first/ occurrence of x in xs. |
| 48 |
|
| 49 |
first-occurrence : ∀ {xs} x → x ∈ xs → x ∈ xs |
| 50 |
first-occurrence x (here x≈y) = here x≈y |
| 51 |
first-occurrence x (there {x = y} x∈xs) with x ≟ y |
| 52 |
... | true because [x≈y] = here (invert [x≈y]) |
| 53 |
... | false because _ = there $ first-occurrence x x∈xs |
| 54 |
|
| 55 |
-- The index of the first occurrence of x in xs. |
| 56 |
|
| 57 |
first-index : ∀ {xs} x → x ∈ xs → Fin (length xs) |
| 58 |
first-index x x∈xs = Any.index $ first-occurrence x x∈xs |
| 59 |
|
| 60 |
-- first-index preserves equality of its first argument. |
| 61 |
|
| 62 |
first-index-cong : ∀ {x₁ x₂ xs} (x₁∈xs : x₁ ∈ xs) (x₂∈xs : x₂ ∈ xs) → |
| 63 |
x₁ ≈ x₂ → first-index x₁ x₁∈xs ≡ first-index x₂ x₂∈xs |
| 64 |
first-index-cong {x₁} {x₂} x₁∈xs x₂∈xs x₁≈x₂ = helper x₁∈xs x₂∈xs |
| 65 |
where |
| 66 |
helper : ∀ {xs} (x₁∈xs : x₁ ∈ xs) (x₂∈xs : x₂ ∈ xs) → |
| 67 |
first-index x₁ x₁∈xs ≡ first-index x₂ x₂∈xs |
| 68 |
helper (here x₁≈x) (here x₂≈x) = refl |
| 69 |
helper (here x₁≈x) (there {x = x} x₂∈xs) |
| 70 |
with x₂ ≟ x | dec-true (x₂ ≟ x) (trans (sym x₁≈x₂) x₁≈x) |
| 71 |
... | _ | refl = refl |
| 72 |
helper (there {x = x} x₁∈xs) (here x₂≈x) |
| 73 |
with x₁ ≟ x | dec-true (x₁ ≟ x) (trans x₁≈x₂ x₂≈x) |
| 74 |
... | _ | refl = refl |
| 75 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) with x₁ ≟ x | x₂ ≟ x |
| 76 |
... | true because _ | true because _ = refl |
| 77 |
... | false because _ | false because _ = cong suc $ helper x₁∈xs x₂∈xs |
| 78 |
... | yes x₁≈x | no x₂≉x = contradiction (trans (sym x₁≈x₂) x₁≈x) x₂≉x |
| 79 |
... | no x₁≉x | yes x₂≈x = contradiction (trans x₁≈x₂ x₂≈x) x₁≉x |
| 80 |
|
| 81 |
-- first-index is injective in its first argument. |
| 82 |
|
| 83 |
first-index-injective |
| 84 |
: ∀ {x₁ x₂ xs} (x₁∈xs : x₁ ∈ xs) (x₂∈xs : x₂ ∈ xs) → |
| 85 |
first-index x₁ x₁∈xs ≡ first-index x₂ x₂∈xs → x₁ ≈ x₂ |
| 86 |
first-index-injective {x₁} {x₂} = helper |
| 87 |
where |
| 88 |
helper : ∀ {xs} (x₁∈xs : x₁ ∈ xs) (x₂∈xs : x₂ ∈ xs) → |
| 89 |
first-index x₁ x₁∈xs ≡ first-index x₂ x₂∈xs → x₁ ≈ x₂ |
| 90 |
helper (here x₁≈x) (here x₂≈x) _ = trans x₁≈x (sym x₂≈x) |
| 91 |
helper (here x₁≈x) (there {x = x} x₂∈xs) _ with x₂ ≟ x |
| 92 |
helper (here x₁≈x) (there {x = x} x₂∈xs) _ | yes x₂≈x = trans x₁≈x (sym x₂≈x) |
| 93 |
helper (here x₁≈x) (there {x = x} x₂∈xs) () | no x₂≉x |
| 94 |
helper (there {x = x} x₁∈xs) (here x₂≈x) _ with x₁ ≟ x |
| 95 |
helper (there {x = x} x₁∈xs) (here x₂≈x) _ | yes x₁≈x = trans x₁≈x (sym x₂≈x) |
| 96 |
helper (there {x = x} x₁∈xs) (here x₂≈x) () | no x₁≉x |
| 97 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) _ with x₁ ≟ x | x₂ ≟ x |
| 98 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) _ | yes x₁≈x | yes x₂≈x = trans x₁≈x (sym x₂≈x) |
| 99 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) () | yes x₁≈x | no x₂≉x |
| 100 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) () | no x₁≉x | yes x₂≈x |
| 101 |
helper (there {x = x} x₁∈xs) (there x₂∈xs) eq | no x₁≉x | no x₂≉x = |
| 102 |
helper x₁∈xs x₂∈xs (suc-injective eq) |
| 103 |
|
| 104 |
------------------------------------------------------------------------ |
| 105 |
-- The countdown data structure |
| 106 |
|
| 107 |
-- If counted ⊕ n is inhabited then there are at most n values of type |
| 108 |
-- Elem which are not members of counted (up to _≈_). You can read the |
| 109 |
-- symbol _⊕_ as partitioning Elem into two parts: counted and |
| 110 |
-- uncounted. |
| 111 |
|
| 112 |
infix 4 _⊕_ |
| 113 |
|
| 114 |
record _⊕_ (counted : List Elem) (n : ℕ) : Set where |
| 115 |
field |
| 116 |
-- An element can be of two kinds: |
| 117 |
-- ⑴ It is provably in counted. |
| 118 |
-- ⑵ It is one of at most n elements which may or may not be in |
| 119 |
-- counted. The "at most n" part is guaranteed by the field |
| 120 |
-- "injective". |
| 121 |
|
| 122 |
kind : ∀ x → x ∈ counted ⊎ Fin n |
| 123 |
injective : ∀ {x y i} → kind x ≡ inj₂ i → kind y ≡ inj₂ i → x ≈ y |
| 124 |
|
| 125 |
-- A countdown can be initialised by proving that Elem is finite. |
| 126 |
|
| 127 |
empty : ∀ {n} → Injection D.setoid (≡.setoid (Fin n)) → [] ⊕ n |
| 128 |
empty inj = |
| 129 |
record { kind = inj₂ ∘ to |
| 130 |
; injective = λ {x} {y} {i} eq₁ eq₂ → injective (begin |
| 131 |
to x ≡⟨ inj₂-injective eq₁ ⟩ |
| 132 |
i ≡⟨ ≡.sym $ inj₂-injective eq₂ ⟩ |
| 133 |
to y ∎) |
| 134 |
} |
| 135 |
where open Injection inj |
| 136 |
|
| 137 |
-- A countdown can also be initialised by proving that Elem is finite. |
| 138 |
|
| 139 |
emptyFromList : (counted : List Elem) → (∀ x → x ∈ counted) → |
| 140 |
[] ⊕ length counted |
| 141 |
emptyFromList counted complete = empty record |
| 142 |
{ to = λ x → first-index x (complete x) |
| 143 |
; cong = first-index-cong (complete _) (complete _) |
| 144 |
; injective = first-index-injective (complete _) (complete _) |
| 145 |
} |
| 146 |
|
| 147 |
-- Finds out if an element has been counted yet. |
| 148 |
|
| 149 |
lookup : ∀ {counted n} → counted ⊕ n → ∀ x → Dec (x ∈ counted) |
| 150 |
lookup {counted} _ x = Any.any? (_≟_ x) counted |
| 151 |
|
| 152 |
-- When no element remains to be counted all elements have been |
| 153 |
-- counted. |
| 154 |
|
| 155 |
lookup! : ∀ {counted} → counted ⊕ zero → ∀ x → x ∈ counted |
| 156 |
lookup! counted⊕0 x with _⊕_.kind counted⊕0 x |
| 157 |
... | inj₁ x∈counted = x∈counted |
| 158 |
... | inj₂ () |
| 159 |
|
| 160 |
private |
| 161 |
|
| 162 |
-- A variant of lookup!. |
| 163 |
|
| 164 |
lookup‼ : ∀ {m counted} → |
| 165 |
counted ⊕ m → ∀ x → x ∉ counted → ∃ λ n → m ≡ suc n |
| 166 |
lookup‼ {suc m} counted⊕n x x∉counted = (m , refl) |
| 167 |
lookup‼ {zero} counted⊕n x x∉counted = |
| 168 |
contradiction (lookup! counted⊕n x) x∉counted |
| 169 |
|
| 170 |
-- Counts a previously uncounted element. |
| 171 |
|
| 172 |
insert : ∀ {counted n} → |
| 173 |
counted ⊕ suc n → ∀ x → x ∉ counted → x ∷ counted ⊕ n |
| 174 |
insert {counted} {n} counted⊕1+n x x∉counted = |
| 175 |
record { kind = kind′; injective = inj } |
| 176 |
where |
| 177 |
open _⊕_ counted⊕1+n |
| 178 |
|
| 179 |
helper : ∀ x y i {j} → |
| 180 |
kind x ≡ inj₂ i → kind y ≡ inj₂ j → i ≡ j → x ≈ y |
| 181 |
helper _ _ _ eq₁ eq₂ refl = injective eq₁ eq₂ |
| 182 |
|
| 183 |
kind′ : ∀ y → y ∈ x ∷ counted ⊎ Fin n |
| 184 |
kind′ y with y ≟ x | kind x | kind y | helper x y |
| 185 |
kind′ y | yes y≈x | _ | _ | _ = inj₁ (here y≈x) |
| 186 |
kind′ y | _ | inj₁ x∈counted | _ | _ = contradiction x∈counted x∉counted |
| 187 |
kind′ y | _ | _ | inj₁ y∈counted | _ = inj₁ (there y∈counted) |
| 188 |
kind′ y | no y≉x | inj₂ i | inj₂ j | hlp = |
| 189 |
inj₂ (punchOut (y≉x ∘ sym ∘ hlp _ refl refl)) |
| 190 |
|
| 191 |
inj : ∀ {y z i} → kind′ y ≡ inj₂ i → kind′ z ≡ inj₂ i → y ≈ z |
| 192 |
inj {y} {z} eq₁ eq₂ with y ≟ x | z ≟ x | kind x | kind y | kind z |
| 193 |
| helper x y | helper x z | helper y z |
| 194 |
inj () _ | yes _ | _ | _ | _ | _ | _ | _ | _ |
| 195 |
inj _ () | _ | yes _ | _ | _ | _ | _ | _ | _ |
| 196 |
inj _ _ | no _ | no _ | inj₁ x∈counted | _ | _ | _ | _ | _ = contradiction x∈counted x∉counted |
| 197 |
inj () _ | no _ | no _ | inj₂ _ | inj₁ _ | _ | _ | _ | _ |
| 198 |
inj _ () | no _ | no _ | inj₂ _ | _ | inj₁ _ | _ | _ | _ |
| 199 |
inj eq₁ eq₂ | no _ | no _ | inj₂ i | inj₂ _ | inj₂ _ | _ | _ | hlp = |
| 200 |
hlp _ refl refl $ |
| 201 |
punchOut-injective {i = i} _ _ $ |
| 202 |
(≡.trans (inj₂-injective eq₁) (≡.sym (inj₂-injective eq₂))) |
| 203 |
|
| 204 |
-- Counts an element if it has not already been counted. |
| 205 |
|
| 206 |
lookupOrInsert : ∀ {counted m} → |
| 207 |
counted ⊕ m → |
| 208 |
∀ x → x ∈ counted ⊎ |
| 209 |
∃ λ n → m ≡ suc n × x ∷ counted ⊕ n |
| 210 |
lookupOrInsert counted⊕n x with lookup counted⊕n x |
| 211 |
... | yes x∈counted = inj₁ x∈counted |
| 212 |
... | no x∉counted with lookup‼ counted⊕n x x∉counted |
| 213 |
... | (n , refl) = inj₂ (n , refl , insert counted⊕n x x∉counted) |