| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Vectors where all elements satisfy a given property |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Relation.Unary.All where |
| 10 |
|
| 11 |
open import Data.Nat.Base using (ℕ; zero; suc; NonZero) |
| 12 |
open import Data.Product.Base as Product using (_×_; _,_; uncurry; <_,_>) |
| 13 |
open import Data.Sum.Base as Sum using (inj₁; inj₂) |
| 14 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 15 |
open import Data.Vec.Relation.Unary.Any as Any using (Any; here; there) |
| 16 |
open import Data.Vec.Membership.Propositional renaming (_∈_ to _∈ₚ_) |
| 17 |
import Data.Vec.Membership.Setoid as SetoidMembership |
| 18 |
open import Function.Base using (_∘_) |
| 19 |
open import Level using (Level; _⊔_) |
| 20 |
open import Relation.Nullary.Decidable as Dec using (_×-dec_; yes; no) |
| 21 |
open import Relation.Unary hiding (_∈_) |
| 22 |
open import Relation.Binary.Bundles using (Setoid) |
| 23 |
open import Relation.Binary.Definitions using (_Respects_) |
| 24 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong₂) |
| 25 |
|
| 26 |
private |
| 27 |
variable |
| 28 |
a b c p q r ℓ : Level |
| 29 |
A : Set a |
| 30 |
B : Set b |
| 31 |
C : Set c |
| 32 |
P : Pred A p |
| 33 |
Q : Pred A q |
| 34 |
R : Pred A r |
| 35 |
n : ℕ |
| 36 |
x : A |
| 37 |
xs : Vec A n |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- All P xs means that all elements in xs satisfy P. |
| 41 |
|
| 42 |
infixr 5 _∷_ |
| 43 |
|
| 44 |
data All {A : Set a} (P : Pred A p) : Vec A n → Set (p ⊔ a) where |
| 45 |
[] : All P [] |
| 46 |
_∷_ : (px : P x) (pxs : All P xs) → All P (x ∷ xs) |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
-- Operations on All |
| 50 |
|
| 51 |
head : All P (x ∷ xs) → P x |
| 52 |
head (px ∷ pxs) = px |
| 53 |
|
| 54 |
tail : All P (x ∷ xs) → All P xs |
| 55 |
tail (px ∷ pxs) = pxs |
| 56 |
|
| 57 |
reduce : (f : ∀ {x} → P x → B) → ∀ {n} {xs : Vec A n} → All P xs → Vec B n |
| 58 |
reduce f [] = [] |
| 59 |
reduce f (px ∷ pxs) = f px ∷ reduce f pxs |
| 60 |
|
| 61 |
uncons : All P (x ∷ xs) → P x × All P xs |
| 62 |
uncons = < head , tail > |
| 63 |
|
| 64 |
map : P ⊆ Q → All P ⊆ All Q {n} |
| 65 |
map g [] = [] |
| 66 |
map g (px ∷ pxs) = g px ∷ map g pxs |
| 67 |
|
| 68 |
zip : All P ∩ All Q ⊆ All (P ∩ Q) {n} |
| 69 |
zip ([] , []) = [] |
| 70 |
zip (px ∷ pxs , qx ∷ qxs) = (px , qx) ∷ zip (pxs , qxs) |
| 71 |
|
| 72 |
unzip : All (P ∩ Q) {n} ⊆ All P ∩ All Q |
| 73 |
unzip [] = [] , [] |
| 74 |
unzip (pqx ∷ pqxs) = Product.zip _∷_ _∷_ pqx (unzip pqxs) |
| 75 |
|
| 76 |
module _ {P : Pred A p} {Q : Pred B q} {R : Pred C r} where |
| 77 |
|
| 78 |
zipWith : ∀ {_⊕_ : A → B → C} → |
| 79 |
(∀ {x y} → P x → Q y → R (x ⊕ y)) → |
| 80 |
∀ {n xs ys} → All P {n} xs → All Q {n} ys → |
| 81 |
All R {n} (Vec.zipWith _⊕_ xs ys) |
| 82 |
zipWith _⊕_ {xs = []} {[]} [] [] = [] |
| 83 |
zipWith _⊕_ {xs = x ∷ xs} {y ∷ ys} (px ∷ pxs) (qy ∷ qys) = |
| 84 |
px ⊕ qy ∷ zipWith _⊕_ pxs qys |
| 85 |
|
| 86 |
------------------------------------------------------------------------ |
| 87 |
-- Generalised lookup based on a proof of Any |
| 88 |
|
| 89 |
lookupAny : All P xs → (i : Any Q xs) → (P ∩ Q) (Any.lookup i) |
| 90 |
lookupAny (px ∷ pxs) (here qx) = px , qx |
| 91 |
lookupAny (px ∷ pxs) (there i) = lookupAny pxs i |
| 92 |
|
| 93 |
lookupWith : ∀[ P ⇒ Q ⇒ R ] → All P xs → (i : Any Q xs) → R (Any.lookup i) |
| 94 |
lookupWith f pxs i = Product.uncurry f (lookupAny pxs i) |
| 95 |
|
| 96 |
lookup : All P xs → (∀ {x} → x ∈ₚ xs → P x) |
| 97 |
lookup pxs = lookupWith (λ { px refl → px }) pxs |
| 98 |
|
| 99 |
module _(S : Setoid a ℓ) {P : Pred (Setoid.Carrier S) p} where |
| 100 |
open Setoid S renaming (sym to sym₁) |
| 101 |
open SetoidMembership S |
| 102 |
|
| 103 |
lookupₛ : P Respects _≈_ → All P xs → (∀ {x} → x ∈ xs → P x) |
| 104 |
lookupₛ resp pxs = lookupWith (λ py x=y → resp (sym₁ x=y) py) pxs |
| 105 |
|
| 106 |
------------------------------------------------------------------------ |
| 107 |
-- Properties of predicates preserved by All |
| 108 |
|
| 109 |
all? : ∀ {n} → Decidable P → Decidable (All P {n}) |
| 110 |
all? P? [] = yes [] |
| 111 |
all? P? (x ∷ xs) = Dec.map′ (uncurry _∷_) uncons (P? x ×-dec all? P? xs) |
| 112 |
|
| 113 |
universal : Universal P → ∀ {n} → Universal (All P {n}) |
| 114 |
universal u [] = [] |
| 115 |
universal u (x ∷ xs) = u x ∷ universal u xs |
| 116 |
|
| 117 |
irrelevant : Irrelevant P → ∀ {n} → Irrelevant (All P {n}) |
| 118 |
irrelevant irr [] [] = refl |
| 119 |
irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) = |
| 120 |
cong₂ _∷_ (irr px₁ px₂) (irrelevant irr pxs₁ pxs₂) |
| 121 |
|
| 122 |
satisfiable : Satisfiable P → ∀ {n} → Satisfiable (All P {n}) |
| 123 |
satisfiable (x , p) {zero} = [] , [] |
| 124 |
satisfiable (x , p) {suc n} = Product.map (x ∷_) (p ∷_) (satisfiable (x , p)) |
| 125 |
|
| 126 |
------------------------------------------------------------------------ |
| 127 |
-- Generalised decidability procedure |
| 128 |
|
| 129 |
decide : Π[ P ∪ Q ] → Π[ All P {n} ∪ Any Q ] |
| 130 |
decide p∪q [] = inj₁ [] |
| 131 |
decide p∪q (x ∷ xs) with p∪q x |
| 132 |
... | inj₂ qx = inj₂ (here qx) |
| 133 |
... | inj₁ px = Sum.map (px ∷_) there (decide p∪q xs) |
| 134 |
|
| 135 |
|
| 136 |
all = all? |
| 137 |
{-# WARNING_ON_USAGE all |
| 138 |
"Warning: all was deprecated in v1.4. |
| 139 |
Please use all? instead." |
| 140 |
#-} |