| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Lists where all elements satisfy a given property |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Unary.All where |
| 10 |
|
| 11 |
open import Data.List.Base as List using (List; []; _∷_) |
| 12 |
open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 13 |
open import Data.List.Membership.Propositional renaming (_∈_ to _∈ₚ_) |
| 14 |
import Data.List.Membership.Setoid as SetoidMembership |
| 15 |
open import Data.Product.Base as Product |
| 16 |
using (∃; -,_; _×_; _,_; proj₁; proj₂; uncurry) |
| 17 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 18 |
open import Effect.Applicative |
| 19 |
open import Effect.Monad |
| 20 |
open import Function.Base using (_∘_; _∘′_; id; const) |
| 21 |
open import Level using (Level; _⊔_) |
| 22 |
open import Relation.Nullary.Decidable.Core as Dec |
| 23 |
using (_×-dec_; yes; no; map′) |
| 24 |
open import Relation.Unary hiding (_∈_) |
| 25 |
import Relation.Unary.Properties as Unary |
| 26 |
open import Relation.Binary.Bundles using (Setoid) |
| 27 |
open import Relation.Binary.Definitions using (_Respects_) |
| 28 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 29 |
import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 30 |
|
| 31 |
private |
| 32 |
variable |
| 33 |
a b p q r ℓ : Level |
| 34 |
A : Set a |
| 35 |
B : Set b |
| 36 |
P Q R : Pred A p |
| 37 |
x : A |
| 38 |
xs : List A |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Definitions |
| 42 |
|
| 43 |
-- Given a predicate P, then All P xs means that every element in xs |
| 44 |
-- satisfies P. See `Relation.Unary` for an explanation of predicates. |
| 45 |
|
| 46 |
infixr 5 _∷_ |
| 47 |
|
| 48 |
data All {A : Set a} (P : Pred A p) : Pred (List A) (a ⊔ p) where |
| 49 |
[] : All P [] |
| 50 |
_∷_ : ∀ {x xs} (px : P x) (pxs : All P xs) → All P (x ∷ xs) |
| 51 |
|
| 52 |
-- All P xs is a finite map from indices x ∈ xs to content P x. |
| 53 |
-- Relation pxs [ i ]= px states that, in map pxs, key i : x ∈ xs points |
| 54 |
-- to value px. |
| 55 |
|
| 56 |
infix 4 _[_]=_ |
| 57 |
|
| 58 |
data _[_]=_ {A : Set a} {P : Pred A p} : |
| 59 |
∀ {x xs} → All P xs → x ∈ₚ xs → P x → Set (a ⊔ p) where |
| 60 |
|
| 61 |
here : ∀ {x xs} {px : P x} {pxs : All P xs} → |
| 62 |
px ∷ pxs [ here refl ]= px |
| 63 |
|
| 64 |
there : ∀ {x xs y} {px : P x} {pxs : All P xs} {py : P y} {i : x ∈ₚ xs} → |
| 65 |
pxs [ i ]= px → |
| 66 |
py ∷ pxs [ there i ]= px |
| 67 |
|
| 68 |
-- A list is empty if having an element is impossible. |
| 69 |
|
| 70 |
Null : Pred (List A) _ |
| 71 |
Null = All ∅ |
| 72 |
|
| 73 |
------------------------------------------------------------------------ |
| 74 |
-- Operations on All |
| 75 |
|
| 76 |
uncons : All P (x ∷ xs) → P x × All P xs |
| 77 |
uncons (px ∷ pxs) = px , pxs |
| 78 |
|
| 79 |
head : All P (x ∷ xs) → P x |
| 80 |
head = proj₁ ∘ uncons |
| 81 |
|
| 82 |
tail : All P (x ∷ xs) → All P xs |
| 83 |
tail = proj₂ ∘ uncons |
| 84 |
|
| 85 |
reduce : (f : ∀ {x} → P x → B) → All P xs → List B |
| 86 |
reduce f [] = [] |
| 87 |
reduce f (px ∷ pxs) = f px ∷ reduce f pxs |
| 88 |
|
| 89 |
construct : (f : B → ∃ P) (xs : List B) → ∃ (All P) |
| 90 |
construct f [] = [] , [] |
| 91 |
construct f (x ∷ xs) = Product.zip _∷_ _∷_ (f x) (construct f xs) |
| 92 |
|
| 93 |
fromList : (xs : List (∃ P)) → All P (List.map proj₁ xs) |
| 94 |
fromList [] = [] |
| 95 |
fromList ((x , p) ∷ xps) = p ∷ fromList xps |
| 96 |
|
| 97 |
toList : All P xs → List (∃ P) |
| 98 |
toList pxs = reduce (λ {x} px → x , px) pxs |
| 99 |
|
| 100 |
map : P ⊆ Q → All P ⊆ All Q |
| 101 |
map g [] = [] |
| 102 |
map g (px ∷ pxs) = g px ∷ map g pxs |
| 103 |
|
| 104 |
zipWith : P ∩ Q ⊆ R → All P ∩ All Q ⊆ All R |
| 105 |
zipWith f ([] , []) = [] |
| 106 |
zipWith f (px ∷ pxs , qx ∷ qxs) = f (px , qx) ∷ zipWith f (pxs , qxs) |
| 107 |
|
| 108 |
unzipWith : R ⊆ P ∩ Q → All R ⊆ All P ∩ All Q |
| 109 |
unzipWith f [] = [] , [] |
| 110 |
unzipWith f (rx ∷ rxs) = Product.zip _∷_ _∷_ (f rx) (unzipWith f rxs) |
| 111 |
|
| 112 |
zip : All P ∩ All Q ⊆ All (P ∩ Q) |
| 113 |
zip = zipWith id |
| 114 |
|
| 115 |
unzip : All (P ∩ Q) ⊆ All P ∩ All Q |
| 116 |
unzip = unzipWith id |
| 117 |
|
| 118 |
module _ (S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where |
| 119 |
open Setoid S renaming (refl to ≈-refl) |
| 120 |
open SetoidMembership S |
| 121 |
|
| 122 |
tabulateₛ : (∀ {x} → x ∈ xs → P x) → All P xs |
| 123 |
tabulateₛ {[]} hyp = [] |
| 124 |
tabulateₛ {_ ∷ _} hyp = hyp (here ≈-refl) ∷ tabulateₛ (hyp ∘ there) |
| 125 |
|
| 126 |
tabulate : (∀ {x} → x ∈ₚ xs → P x) → All P xs |
| 127 |
tabulate = tabulateₛ (≡.setoid _) |
| 128 |
|
| 129 |
self : ∀ {xs} → All (const A) xs |
| 130 |
self = tabulate (λ {x} _ → x) |
| 131 |
|
| 132 |
------------------------------------------------------------------------ |
| 133 |
-- (weak) updateAt |
| 134 |
|
| 135 |
infixl 6 _[_]%=_ _[_]≔_ |
| 136 |
|
| 137 |
updateAt : x ∈ₚ xs → (P x → P x) → All P xs → All P xs |
| 138 |
updateAt () f [] |
| 139 |
updateAt (here refl) f (px ∷ pxs) = f px ∷ pxs |
| 140 |
updateAt (there i) f (px ∷ pxs) = px ∷ updateAt i f pxs |
| 141 |
|
| 142 |
_[_]%=_ : All P xs → x ∈ₚ xs → (P x → P x) → All P xs |
| 143 |
pxs [ i ]%= f = updateAt i f pxs |
| 144 |
|
| 145 |
_[_]≔_ : All P xs → x ∈ₚ xs → P x → All P xs |
| 146 |
pxs [ i ]≔ px = pxs [ i ]%= const px |
| 147 |
|
| 148 |
------------------------------------------------------------------------ |
| 149 |
-- Traversable-like functions |
| 150 |
|
| 151 |
module _ (p : Level) {A : Set a} {P : Pred A (a ⊔ p)} |
| 152 |
{F : Set (a ⊔ p) → Set (a ⊔ p)} |
| 153 |
(App : RawApplicative F) |
| 154 |
where |
| 155 |
|
| 156 |
open RawApplicative App |
| 157 |
|
| 158 |
sequenceA : All (F ∘′ P) ⊆ F ∘′ All P |
| 159 |
sequenceA [] = pure [] |
| 160 |
sequenceA (x ∷ xs) = _∷_ <$> x <*> sequenceA xs |
| 161 |
|
| 162 |
mapA : ∀ {Q : Pred A q} → (Q ⊆ F ∘′ P) → All Q ⊆ (F ∘′ All P) |
| 163 |
mapA f = sequenceA ∘′ map f |
| 164 |
|
| 165 |
forA : ∀ {Q : Pred A q} → All Q xs → (Q ⊆ F ∘′ P) → F (All P xs) |
| 166 |
forA qxs f = mapA f qxs |
| 167 |
|
| 168 |
module _ (p : Level) {A : Set a} {P : Pred A (a ⊔ p)} |
| 169 |
{M : Set (a ⊔ p) → Set (a ⊔ p)} |
| 170 |
(Mon : RawMonad M) |
| 171 |
where |
| 172 |
|
| 173 |
private App = RawMonad.rawApplicative Mon |
| 174 |
|
| 175 |
sequenceM : All (M ∘′ P) ⊆ M ∘′ All P |
| 176 |
sequenceM = sequenceA p App |
| 177 |
|
| 178 |
mapM : ∀ {Q : Pred A q} → (Q ⊆ M ∘′ P) → All Q ⊆ (M ∘′ All P) |
| 179 |
mapM = mapA p App |
| 180 |
|
| 181 |
forM : ∀ {Q : Pred A q} → All Q xs → (Q ⊆ M ∘′ P) → M (All P xs) |
| 182 |
forM = forA p App |
| 183 |
|
| 184 |
------------------------------------------------------------------------ |
| 185 |
-- Generalised lookup based on a proof of Any |
| 186 |
|
| 187 |
lookupAny : All P xs → (i : Any Q xs) → (P ∩ Q) (Any.lookup i) |
| 188 |
lookupAny (px ∷ pxs) (here qx) = px , qx |
| 189 |
lookupAny (px ∷ pxs) (there i) = lookupAny pxs i |
| 190 |
|
| 191 |
lookupWith : ∀[ P ⇒ Q ⇒ R ] → All P xs → (i : Any Q xs) → R (Any.lookup i) |
| 192 |
lookupWith f pxs i = Product.uncurry f (lookupAny pxs i) |
| 193 |
|
| 194 |
lookup : All P xs → (∀ {x} → x ∈ₚ xs → P x) |
| 195 |
lookup pxs = lookupWith (λ { px refl → px }) pxs |
| 196 |
|
| 197 |
module _(S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where |
| 198 |
open Setoid S renaming (sym to ≈-sym) |
| 199 |
open SetoidMembership S |
| 200 |
|
| 201 |
lookupₛ : P Respects _≈_ → All P xs → (∀ {x} → x ∈ xs → P x) |
| 202 |
lookupₛ resp pxs = lookupWith (λ py x≈y → resp (≈-sym x≈y) py) pxs |
| 203 |
|
| 204 |
------------------------------------------------------------------------ |
| 205 |
-- Properties of predicates preserved by All |
| 206 |
|
| 207 |
all? : Decidable P → Decidable (All P) |
| 208 |
all? p [] = yes [] |
| 209 |
all? p (x ∷ xs) = Dec.map′ (uncurry _∷_) uncons (p x ×-dec all? p xs) |
| 210 |
|
| 211 |
universal : Universal P → Universal (All P) |
| 212 |
universal u [] = [] |
| 213 |
universal u (x ∷ xs) = u x ∷ universal u xs |
| 214 |
|
| 215 |
universal-U : Universal (All {A = A} U) |
| 216 |
universal-U = universal Unary.U-Universal |
| 217 |
|
| 218 |
irrelevant : Irrelevant P → Irrelevant (All P) |
| 219 |
irrelevant irr [] [] = ≡.refl |
| 220 |
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) = |
| 221 |
≡.cong₂ _∷_ (irr px₁ px₂) (irrelevant irr pxs₁ pxs₂) |
| 222 |
|
| 223 |
satisfiable : Satisfiable (All P) |
| 224 |
satisfiable = [] , [] |
| 225 |
|
| 226 |
------------------------------------------------------------------------ |
| 227 |
-- Generalised decidability procedure |
| 228 |
|
| 229 |
decide : Π[ P ∪ Q ] → Π[ All P ∪ Any Q ] |
| 230 |
decide p∪q [] = inj₁ [] |
| 231 |
decide p∪q (x ∷ xs) with p∪q x |
| 232 |
... | inj₂ qx = inj₂ (here qx) |
| 233 |
... | inj₁ px = Sum.map (px ∷_) there (decide p∪q xs) |
| 234 |
|
| 235 |
search : Decidable P → ∀ xs → All (∁ P) xs ⊎ Any P xs |
| 236 |
search P? = decide (Sum.swap ∘ Dec.toSum ∘ P?) |
| 237 |
|
| 238 |
------------------------------------------------------------------------ |
| 239 |
-- DEPRECATED |
| 240 |
------------------------------------------------------------------------ |
| 241 |
-- Please use the new names as continuing support for the old names is |
| 242 |
-- not guaranteed. |
| 243 |
|
| 244 |
-- Version 1.4 |
| 245 |
|
| 246 |
all = all? |
| 247 |
{-# WARNING_ON_USAGE all |
| 248 |
"Warning: all was deprecated in v1.4. |
| 249 |
Please use all? instead." |
| 250 |
#-} |