Skip to content

Commit a2e3f66

Browse files
committed
[ re #432 #460 ] Heterogeneous version of Sublist
1 parent f92515e commit a2e3f66

4 files changed

Lines changed: 167 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ Other major changes
248248

249249
* Added new modules `Data.List.Relation.Prefix.Heterogeneous(.Properties)`
250250

251+
* Added new modules `Data.List.Sublist.Heterogeneous(.Properties)`
252+
251253
* Added new modules `Data.List.First` and `Data.List.First.Properties` for a
252254
generalization of the notion of "first element in the list to satisfy a
253255
predicate".
@@ -667,9 +669,12 @@ Other minor additions
667669
wlog : Total _R_ → Symmetric Q → (∀ a b → a R b → Q a b) → ∀ a b → Q a b
668670
```
669671

670-
* Added new definition to `Relation.Binary.Core`:
672+
* Added new definitions to `Relation.Binary.Core`:
671673
```agda
672674
Antisym R S E = ∀ {i j} → R i j → S j i → E i j
675+
676+
Max : REL A B ℓ → B → Set _
677+
Min : REL A B ℓ → A → Set _
673678
```
674679

675680
* Added new proofs to `Relation.Binary.Lattice`:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- An inductive definition of the heterogeneous sublist relation
5+
-- This is a generalisation of what is commonly known as Order
6+
-- Preserving Embeddings (OPE).
7+
------------------------------------------------------------------------
8+
9+
{-# OPTIONS --without-K --safe #-}
10+
11+
module Data.List.Relation.Sublist.Heterogeneous where
12+
13+
open import Level using (_⊔_)
14+
open import Data.List.Base using (List; []; _∷_)
15+
open import Function
16+
open import Relation.Binary
17+
open import Relation.Binary.PropositionalEquality as P using (_≡_)
18+
19+
------------------------------------------------------------------------
20+
-- Type and basic combinators
21+
22+
module _ {a b r} {A : Set a} {B : Set b} (R : REL A B r) where
23+
24+
data Sublist : REL (List A) (List B) (a ⊔ b ⊔ r) where
25+
[] : Sublist [] []
26+
_∷ʳ_ : {xs ys} y Sublist xs ys Sublist xs (y ∷ ys)
27+
_∷_ : {x xs y ys} R x y Sublist xs ys Sublist (x ∷ xs) (y ∷ ys)
28+
29+
module _ {a b r s} {A : Set a} {B : Set b} {R : REL A B r} {S : REL A B s} where
30+
31+
map : R ⇒ S Sublist R ⇒ Sublist S
32+
map f [] = []
33+
map f (y ∷ʳ rs) = y ∷ʳ map f rs
34+
map f (r ∷ rs) = f r ∷ map f rs
35+
36+
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where
37+
38+
minimum : Min (Sublist R) []
39+
minimum [] = []
40+
minimum (x ∷ xs) = x ∷ʳ minimum xs
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
-----------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of the heterogeneous sublist relation
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.List.Relation.Sublist.Heterogeneous.Properties where
10+
11+
open import Data.Empty
12+
open import Data.List.Any using (Any; here; there)
13+
open import Data.List.Base as List using (List; []; _∷_; _++_; [_]; length)
14+
open import Data.List.Relation.Pointwise using (Pointwise; []; _∷_)
15+
open import Data.List.Relation.Sublist.Heterogeneous
16+
open import Data.Nat using (ℕ; _≤_); open ; open _≤_
17+
open import Data.Nat.Properties
18+
using (suc-injective; ≤-step; n≤1+n; <-irrefl; module ≤-Reasoning)
19+
open import Function
20+
open import Relation.Binary
21+
open import Relation.Binary.PropositionalEquality as P using (_≡_)
22+
23+
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where
24+
25+
fromAny : {a bs} Any (R a) bs Sublist R [ a ] bs
26+
fromAny (here r) = r ∷ minimum _
27+
fromAny (there p) = _ ∷ʳ fromAny p
28+
29+
toAny : {a bs} Sublist R [ a ] bs Any (R a) bs
30+
toAny (y ∷ʳ rs) = there (toAny rs)
31+
toAny (r ∷ rs) = here r
32+
33+
length-mono-Sublist-≤ : {as bs} Sublist R as bs length as ≤ length bs
34+
length-mono-Sublist-≤ [] = z≤n
35+
length-mono-Sublist-≤ (y ∷ʳ rs) = ≤-step (length-mono-Sublist-≤ rs)
36+
length-mono-Sublist-≤ (r ∷ rs) = s≤s (length-mono-Sublist-≤ rs)
37+
38+
fromPointwise : Pointwise R ⇒ Sublist R
39+
fromPointwise [] = []
40+
fromPointwise (r ∷ rs) = r ∷ fromPointwise rs
41+
42+
toPointwise : {as bs} length as ≡ length bs
43+
Sublist R as bs Pointwise R as bs
44+
toPointwise {bs = []} eq [] = []
45+
toPointwise {bs = b ∷ bs} eq (r ∷ rs) = r ∷ toPointwise (suc-injective eq) rs
46+
toPointwise {bs = b ∷ bs} eq (.b ∷ʳ rs) =
47+
⊥-elim $ <-irrefl eq (s≤s (length-mono-Sublist-≤ rs))
48+
49+
module _ {a b c r s t} {A : Set a} {B : Set b} {C : Set c}
50+
{R : REL A B r} {S : REL B C s} {T : REL A C t} where
51+
52+
trans : Trans R S T Trans (Sublist R) (Sublist S) (Sublist T)
53+
trans rs⇒t [] [] = []
54+
trans rs⇒t rs (y ∷ʳ ss) = y ∷ʳ trans rs⇒t rs ss
55+
trans rs⇒t (y ∷ʳ rs) (s ∷ ss) = _ ∷ʳ trans rs⇒t rs ss
56+
trans rs⇒t (r ∷ rs) (s ∷ ss) = rs⇒t r s ∷ trans rs⇒t rs ss
57+
58+
module _ {a b r s e} {A : Set a} {B : Set b}
59+
{R : REL A B r} {S : REL B A s} {E : REL A B e} where
60+
61+
antisym : Antisym R S E Antisym (Sublist R) (Sublist S) (Pointwise E)
62+
antisym rs⇒e [] [] = []
63+
antisym rs⇒e (r ∷ rs) (s ∷ ss) = rs⇒e r s ∷ antisym rs⇒e rs ss
64+
-- impossible cases
65+
antisym rs⇒e (_∷ʳ_ {xs} {ys₁} y rs) (_∷ʳ_ {ys₂} {zs} z ss) =
66+
⊥-elim $ <-irrefl P.refl $ begin
67+
length (y ∷ ys₁) ≤⟨ length-mono-Sublist-≤ ss ⟩
68+
length zs ≤⟨ n≤1+n (length zs) ⟩
69+
length (z ∷ zs) ≤⟨ length-mono-Sublist-≤ rs ⟩
70+
length ys₁ ∎ where open ≤-Reasoning
71+
antisym rs⇒e (_∷ʳ_ {xs} {ys₁} y rs) (_∷_ {y} {ys₂} {z} {zs} s ss) =
72+
⊥-elim $ <-irrefl P.refl $ begin
73+
length (z ∷ zs) ≤⟨ length-mono-Sublist-≤ rs ⟩
74+
length ys₁ ≤⟨ length-mono-Sublist-≤ ss ⟩
75+
length zs ∎ where open ≤-Reasoning
76+
antisym rs⇒e (_∷_ {x} {xs} {y} {ys₁} r rs) (_∷ʳ_ {ys₂} {zs} z ss) =
77+
⊥-elim $ <-irrefl P.refl $ begin
78+
length (y ∷ ys₁) ≤⟨ length-mono-Sublist-≤ ss ⟩
79+
length xs ≤⟨ length-mono-Sublist-≤ rs ⟩
80+
length ys₁ ∎ where open ≤-Reasoning
81+
82+
------------------------------------------------------------------------
83+
-- _++_
84+
85+
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where
86+
87+
++⁺ : {as bs cs ds} Sublist R as bs Sublist R cs ds
88+
Sublist R (as ++ cs) (bs ++ ds)
89+
++⁺ [] cds = cds
90+
++⁺ (y ∷ʳ abs) cds = y ∷ʳ ++⁺ abs cds
91+
++⁺ (ab ∷ abs) cds = ab ∷ ++⁺ abs cds
92+
93+
------------------------------------------------------------------------
94+
-- map
95+
96+
module _ {a b c d r} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
97+
{R : REL C D r} where
98+
99+
map⁺ : {as bs} (f : A C) (g : B D)
100+
Sublist (λ a b R (f a) (g b)) as bs
101+
Sublist R (List.map f as) (List.map g bs)
102+
map⁺ f g [] = []
103+
map⁺ f g (y ∷ʳ rs) = g y ∷ʳ map⁺ f g rs
104+
map⁺ f g (r ∷ rs) = r ∷ map⁺ f g rs
105+
106+
map⁻ : {as bs} (f : A C) (g : B D)
107+
Sublist R (List.map f as) (List.map g bs)
108+
Sublist (λ a b R (f a) (g b)) as bs
109+
map⁻ {[]} {bs} f g rs = minimum _
110+
map⁻ {a ∷ as} {[]} f g ()
111+
map⁻ {a ∷ as} {b ∷ bs} f g (_ ∷ʳ rs) = b ∷ʳ map⁻ f g rs
112+
map⁻ {a ∷ as} {b ∷ bs} f g (r ∷ rs) = r ∷ map⁻ f g rs

src/Relation/Binary/Core.agda

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,18 @@ Trichotomous : ∀ {a ℓ₁ ℓ₂} {A : Set a} → Rel A ℓ₁ → Rel A ℓ
122122
Trichotomous _≈_ _<_ = x y Tri (x < y) (x ≈ y) (x > y)
123123
where _>_ = flip _<_
124124

125+
126+
Max : {a ℓ} {A : Set a} {b} {B : Set b} REL A B ℓ B Set _
127+
Max _≤_ T = x x ≤ T
128+
125129
Maximum : {a ℓ} {A : Set a} Rel A ℓ A Set _
126-
Maximum _≤_ ⊤ = x x ≤ ⊤
130+
Maximum = Max
131+
132+
Min : {a ℓ} {A : Set a} {b} {B : Set b} REL A B ℓ A Set _
133+
Min R = Max (flip R)
127134

128135
Minimum : {a ℓ} {A : Set a} Rel A ℓ A Set _
129-
Minimum _≤_ = Maximum (flip _≤_)
136+
Minimum = Min
130137

131138
_Respects_ : {a ℓ₁ ℓ₂} {A : Set a} (A Set ℓ₁) Rel A ℓ₂ Set _
132139
P Respects _∼_ = {x y} x ∼ y P x P y

0 commit comments

Comments
 (0)