Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Property that elements are grouped
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.Grouped where
10
11 open import Data.List.Base using (List; []; _∷_; map)
12 open import Data.List.Relation.Unary.All as All using (All; []; _∷_; all?)
13 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
14 open import Data.Product.Base using (_×_; _,_)
15 open import Relation.Binary.Core using (REL; Rel)
16 open import Relation.Binary.Definitions as B
17 open import Relation.Unary as U using (Pred)
18 open import Relation.Nullary.Negation using (¬_)
19 open import Relation.Nullary.Decidable as Dec using (yes; ¬?; _⊎-dec_; _×-dec_)
20 open import Level using (Level; _⊔_)
21
22 private
23 variable
24 a ℓ : Level
25 A : Set a
26 x y : A
27 xs : List A
28
29 ------------------------------------------------------------------------
30 -- Definition
31
32 infixr 5 _∷≉_ _∷≈_
33
34 data Grouped {A : Set a} (_≈_ : Rel A ℓ) : Pred (List A) (a ⊔ ℓ) where
35 [] : Grouped _≈_ []
36 _∷≉_ : All (λ y → ¬ (x ≈ y)) xs → Grouped _≈_ xs → Grouped _≈_ (x ∷ xs)
37 _∷≈_ : x ≈ y → Grouped _≈_ (y ∷ xs) → Grouped _≈_ (x ∷ y ∷ xs)
38
39 ------------------------------------------------------------------------
40 -- Basic properties
41
42 module _ {_≈_ : Rel A ℓ} where
43
44 grouped? : B.Decidable _≈_ → U.Decidable (Grouped _≈_)
45 grouped? _≟_ [] = yes []
46 grouped? _≟_ (x ∷ []) = yes ([] ∷≉ [])
47 grouped? _≟_ (x ∷ y ∷ xs) =
48 Dec.map′ from to ((x ≟ y ⊎-dec all? (λ z → ¬? (x ≟ z)) (y ∷ xs)) ×-dec (grouped? _≟_ (y ∷ xs)))
49 where
50 from : ((x ≈ y) ⊎ All (λ z → ¬ (x ≈ z)) (y ∷ xs)) × Grouped _≈_ (y ∷ xs) → Grouped _≈_ (x ∷ y ∷ xs)
51 from (inj₁ x≈y , grouped[y∷xs]) = x≈y ∷≈ grouped[y∷xs]
52 from (inj₂ all[x≉,y∷xs] , grouped[y∷xs]) = all[x≉,y∷xs] ∷≉ grouped[y∷xs]
53
54 to : Grouped _≈_ (x ∷ y ∷ xs) → ((x ≈ y) ⊎ All (λ z → ¬ (x ≈ z)) (y ∷ xs)) × Grouped _≈_ (y ∷ xs)
55 to (all[x≉,y∷xs] ∷≉ grouped[y∷xs]) = inj₂ all[x≉,y∷xs] , grouped[y∷xs]
56 to (x≈y ∷≈ grouped[y∷xs]) = inj₁ x≈y , grouped[y∷xs]