Debian Codegraph @

src/Data/Vec/Relation/Unary/AllPairs.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors where every pair of elements are related (symmetrically)
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Core using (Rel; _⇒_)
10
11 module Data.Vec.Relation.Unary.AllPairs
12 {a ℓ} {A : Set a} {R : Rel A ℓ} where
13
14 open import Data.Nat.Base using (suc)
15 open import Data.Vec.Base as Vec using (Vec; []; _∷_)
16 open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_)
17 open import Data.Product.Base as Prod using (_,_; _×_; uncurry; <_,_>)
18 open import Function.Base using (id; _∘_)
19 open import Level using (_⊔_)
20 open import Relation.Binary.Definitions as B
21 open import Relation.Binary.Construct.Intersection renaming (_∩_ to _∩ᵇ_)
22 open import Relation.Binary.PropositionalEquality.Core using (refl; cong₂)
23 open import Relation.Unary as U renaming (_∩_ to _∩ᵘ_) hiding (_⇒_)
24 open import Relation.Nullary.Decidable as Dec using (yes; no; _×-dec_)
25
26 ------------------------------------------------------------------------
27 -- Definition
28
29 open import Data.Vec.Relation.Unary.AllPairs.Core public
30
31 ------------------------------------------------------------------------
32 -- Operations
33
34 head : ∀ {n} {xs : Vec A (suc n)} → AllPairs R xs → All (R (Vec.head xs)) (Vec.tail xs)
35 head (px ∷ pxs) = px
36
37 tail : ∀ {n} {xs : Vec A (suc n)} → AllPairs R xs → AllPairs R (Vec.tail xs)
38 tail (px ∷ pxs) = pxs
39
40 uncons : ∀ {n} {xs : Vec A (suc n)} → AllPairs R xs →
41 All (R (Vec.head xs)) (Vec.tail xs) × AllPairs R (Vec.tail xs)
42 uncons = < head , tail >
43
44 module _ {s} {S : Rel A s} where
45
46 map : ∀ {n} → R ⇒ S → AllPairs R {n} ⊆ AllPairs S {n}
47 map ~₁⇒~₂ [] = []
48 map ~₁⇒~₂ (x~xs ∷ pxs) = All.map ~₁⇒~₂ x~xs ∷ (map ~₁⇒~₂ pxs)
49
50 module _ {s t} {S : Rel A s} {T : Rel A t} where
51
52 zipWith : ∀ {n} → R ∩ᵇ S ⇒ T → AllPairs R {n} ∩ᵘ AllPairs S {n} ⊆ AllPairs T {n}
53 zipWith f ([] , []) = []
54 zipWith f (px ∷ pxs , qx ∷ qxs) = All.map f (All.zip (px , qx)) ∷ zipWith f (pxs , qxs)
55
56 unzipWith : ∀ {n} → T ⇒ R ∩ᵇ S → AllPairs T {n} ⊆ AllPairs R {n} ∩ᵘ AllPairs S {n}
57 unzipWith f [] = [] , []
58 unzipWith f (rx ∷ rxs) = Prod.zip _∷_ _∷_ (All.unzip (All.map f rx)) (unzipWith f rxs)
59
60 module _ {s} {S : Rel A s} where
61
62 zip : ∀ {n} → AllPairs R {n} ∩ᵘ AllPairs S {n} ⊆ AllPairs (R ∩ᵇ S) {n}
63 zip = zipWith id
64
65 unzip : ∀ {n} → AllPairs (R ∩ᵇ S) {n} ⊆ AllPairs R {n} ∩ᵘ AllPairs S {n}
66 unzip = unzipWith id
67
68 ------------------------------------------------------------------------
69 -- Properties of predicates preserved by AllPairs
70
71 allPairs? : ∀ {n} → B.Decidable R → U.Decidable (AllPairs R {n})
72 allPairs? R? [] = yes []
73 allPairs? R? (x ∷ xs) =
74 Dec.map′ (uncurry _∷_) uncons (All.all? (R? x) xs ×-dec allPairs? R? xs)
75
76 irrelevant : ∀ {n} → B.Irrelevant R → U.Irrelevant (AllPairs R {n})
77 irrelevant irr [] [] = refl
78 irrelevant irr (px₁ ∷ pxs₁) (px₂ ∷ pxs₂) =
79 cong₂ _∷_ (All.irrelevant irr px₁ px₂) (irrelevant irr pxs₁ pxs₂)
80
81 satisfiable : U.Satisfiable (AllPairs R)
82 satisfiable = [] , []