Skip to content

Commit e19a083

Browse files
Add various relations over non-empty lists (#2862)
* Add various relations over non-empty lists * Address James' feedback
1 parent 645e8d6 commit e19a083

File tree

7 files changed

+162
-3
lines changed

7 files changed

+162
-3
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ New modules
9393

9494
* `Effect.Monad.Random` and `Effect.Monad.Random.Instances` for an mtl-style randomness monad constraint.
9595

96+
* Various additions over non-empty lists:
97+
```
98+
Data.List.NonEmpty.Relation.Binary.Pointwise
99+
Data.List.NonEmpty.Relation.Unary.Any
100+
Data.List.NonEmpty.Membership.Propositional
101+
Data.List.NonEmpty.Membership.Setoid
102+
```
103+
96104
Additions to existing modules
97105
-----------------------------
98106

@@ -159,6 +167,11 @@ Additions to existing modules
159167
search-least⟨¬_⟩ : Decidable P → Π[ P ] ⊎ Least⟨ ∁ P ⟩
160168
```
161169

170+
* In `Data.List.NonEmpty.Relation.Unary.All`:
171+
```
172+
map : P ⊆ Q → All P xs → All Q xs
173+
```
174+
162175
* In `Data.Nat.ListAction.Properties`
163176
```agda
164177
*-distribˡ-sum : ∀ m ns → m * sum ns ≡ sum (map (m *_) ns)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Propositional membership over non-empty lists
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
module Data.List.NonEmpty.Membership.Propositional {a} {A : Set a} where
10+
11+
open import Relation.Binary.PropositionalEquality using (setoid; refl)
12+
open import Data.List.NonEmpty
13+
open import Level
14+
open import Relation.Unary using (Pred)
15+
16+
import Data.List.NonEmpty.Membership.Setoid as SetoidMembership
17+
18+
private
19+
variable
20+
p : Level
21+
P : Pred A p
22+
xs : List⁺ A
23+
24+
------------------------------------------------------------------------
25+
-- Re-export contents of setoid membership
26+
27+
open SetoidMembership (setoid A) public
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Setoid membership over non-empty lists
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
open import Relation.Binary.Bundles using (Setoid)
10+
11+
module Data.List.NonEmpty.Membership.Setoid {c ℓ} (S : Setoid c ℓ) where
12+
13+
open import Data.List.NonEmpty.Base using (List⁺)
14+
open import Data.List.NonEmpty.Relation.Unary.Any using (Any)
15+
open import Relation.Nullary.Negation using (¬_)
16+
17+
open Setoid S renaming (Carrier to A)
18+
19+
------------------------------------------------------------------------
20+
-- Definitions
21+
22+
infix 4 _∈_ _∉_
23+
24+
_∈_ : A List⁺ A Set _
25+
x ∈ xs = Any (x ≈_) xs
26+
27+
_∉_ : A List⁺ A Set _
28+
x ∉ xs = ¬ x ∈ xs
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Pointwise lifting of relations to non-empty lists
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
module Data.List.NonEmpty.Relation.Binary.Pointwise where
10+
11+
open import Data.List.Base using (List; []; _∷_)
12+
open import Data.List.NonEmpty.Base using (List⁺; _∷_)
13+
import Data.List.Relation.Binary.Pointwise.Base as List
14+
open import Level
15+
open import Relation.Binary.Core using (REL)
16+
17+
private
18+
variable
19+
a b ℓ : Level
20+
A B : Set a
21+
x y : A
22+
xs ys : List A
23+
24+
------------------------------------------------------------------------
25+
-- Definition
26+
------------------------------------------------------------------------
27+
28+
infixr 5 _∷_
29+
30+
data Pointwise {A : Set a} {B : Set b} (R : REL A B ℓ)
31+
: List⁺ A List⁺ B Set (a ⊔ b ⊔ ℓ) where
32+
_∷_ : (x∼y : R x y) (xs∼ys : List.Pointwise R xs ys)
33+
Pointwise R (x ∷ xs) (y ∷ ys)

src/Data/List/NonEmpty/Relation/Unary/All.agda

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ import Data.List.Relation.Unary.All as List
1212
open import Data.List.Relation.Unary.All using ([]; _∷_)
1313
open import Data.List.Base using ([]; _∷_)
1414
open import Data.List.NonEmpty.Base using (List⁺; _∷_; toList)
15+
open import Data.List.NonEmpty.Membership.Propositional using (_∈_)
16+
open import Data.List.NonEmpty.Relation.Unary.Any using (here; there)
1517
open import Level using (Level; _⊔_)
16-
open import Relation.Unary using (Pred)
18+
open import Relation.Unary using (Pred; _⊆_)
19+
open import Relation.Binary.PropositionalEquality.Core using (refl)
1720

1821
private
1922
variable
2023
a p : Level
2124
A : Set a
22-
P : Pred A p
25+
P Q : Pred A p
26+
xs : List⁺ A
2327

2428
------------------------------------------------------------------------
2529
-- Definition
@@ -35,5 +39,12 @@ data All {A : Set a} (P : Pred A p) : Pred (List⁺ A) (a ⊔ p) where
3539
------------------------------------------------------------------------
3640
-- Functions
3741

42+
map : P ⊆ Q All P ⊆ All Q
43+
map P⊆Q (px ∷ pxs) = P⊆Q px ∷ List.map P⊆Q pxs
44+
45+
lookup : All P xs {x} x ∈ xs P x
46+
lookup (px ∷ pxs) (here refl) = px
47+
lookup (px ∷ pxs) (there x∈xs) = List.lookup pxs x∈xs
48+
3849
toList⁺ : {xs : List⁺ A} All P xs List.All P (toList xs)
3950
toList⁺ (px ∷ pxs) = px ∷ pxs
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Non-empty lists where at least one element satisfies a given property
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
module Data.List.NonEmpty.Relation.Unary.Any where
10+
11+
open import Data.List.NonEmpty.Base using (List⁺; _∷_; toList)
12+
open import Data.List.Relation.Unary.Any as List using (here; there)
13+
open import Data.List.Base using ([]; _∷_)
14+
open import Data.Product.Base using (_,_)
15+
open import Level using (Level; _⊔_)
16+
open import Relation.Unary using (Pred; Satisfiable; _⊆_)
17+
18+
private
19+
variable
20+
a p : Level
21+
A : Set a
22+
P Q : Pred A p
23+
xs : List⁺ A
24+
25+
------------------------------------------------------------------------
26+
-- Definition
27+
28+
-- Given a predicate P, then Any P xs means that every element in xs
29+
-- satisfies P. See `Relation.Unary` for an explanation of predicates.
30+
31+
data Any {A : Set a} (P : Pred A p) : Pred (List⁺ A) (a ⊔ p) where
32+
here : {x xs} (px : P x) Any P (x ∷ xs)
33+
there : {x xs} (pxs : List.Any P xs) Any P (x ∷ xs)
34+
35+
------------------------------------------------------------------------
36+
-- Operations
37+
38+
map : P ⊆ Q Any P ⊆ Any Q
39+
map g (here px) = here (g px)
40+
map g (there pxs) = there (List.map g pxs)
41+
42+
------------------------------------------------------------------------
43+
-- Predicates
44+
45+
satisfied : Any P xs Satisfiable P
46+
satisfied (here px) = _ , px
47+
satisfied (there pxs) = List.satisfied pxs

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ xs ─ x∈xs = removeAt xs (index x∈xs)
7171

7272
-- If any element satisfies P, then P is satisfied.
7373

74-
satisfied : Any P xs P
74+
satisfied : Any P xs Satisfiable P
7575
satisfied (here px) = _ , px
7676
satisfied (there pxs) = satisfied pxs
7777

0 commit comments

Comments
 (0)