Debian Codegraph @

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

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