| 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 --cubical-compatible --safe #-} |
| 10 |
|
| 11 |
open import Data.List.Base using (List; []; _∷_; [_]) |
| 12 |
open import Data.List.Relation.Unary.Any using (Any; here; there) |
| 13 |
open import Level using (_⊔_) |
| 14 |
open import Relation.Binary.Core using (REL; _⇒_) |
| 15 |
open import Relation.Binary.Definitions using (_⟶_Respects_; Min) |
| 16 |
open import Relation.Unary using (Pred) |
| 17 |
|
| 18 |
module Data.List.Relation.Binary.Sublist.Heterogeneous |
| 19 |
{a b r} {A : Set a} {B : Set b} {R : REL A B r} |
| 20 |
where |
| 21 |
|
| 22 |
------------------------------------------------------------------------ |
| 23 |
-- Re-export core definitions |
| 24 |
|
| 25 |
open import Data.List.Relation.Binary.Sublist.Heterogeneous.Core public |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Type and basic combinators |
| 29 |
|
| 30 |
module _ {s} {S : REL A B s} where |
| 31 |
|
| 32 |
map : R ⇒ S → Sublist R ⇒ Sublist S |
| 33 |
map f [] = [] |
| 34 |
map f (y ∷ʳ rs) = y ∷ʳ map f rs |
| 35 |
map f (r ∷ rs) = f r ∷ map f rs |
| 36 |
|
| 37 |
minimum : Min (Sublist R) [] |
| 38 |
minimum [] = [] |
| 39 |
minimum (x ∷ xs) = x ∷ʳ minimum xs |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Conversion to and from Any |
| 43 |
|
| 44 |
-- Special case: Sublist R [ a ] bs → Any (R a) bs |
| 45 |
toAny : ∀ {a as bs} → Sublist R (a ∷ as) bs → Any (R a) bs |
| 46 |
toAny (y ∷ʳ rs) = there (toAny rs) |
| 47 |
toAny (r ∷ rs) = here r |
| 48 |
|
| 49 |
fromAny : ∀ {a bs} → Any (R a) bs → Sublist R [ a ] bs |
| 50 |
fromAny (here r) = r ∷ minimum _ |
| 51 |
fromAny (there p) = _ ∷ʳ fromAny p |
| 52 |
|
| 53 |
------------------------------------------------------------------------ |
| 54 |
-- Generalised lookup based on a proof of Any |
| 55 |
|
| 56 |
module _ {p q} {P : Pred A p} {Q : Pred B q} (resp : P ⟶ Q Respects R) where |
| 57 |
|
| 58 |
lookup : ∀ {xs ys} → Sublist R xs ys → Any P xs → Any Q ys |
| 59 |
lookup (y ∷ʳ p) k = there (lookup p k) |
| 60 |
lookup (rxy ∷ p) (here px) = here (resp rxy px) |
| 61 |
lookup (rxy ∷ p) (there k) = there (lookup p k) |
| 62 |
|
| 63 |
------------------------------------------------------------------------ |
| 64 |
-- Disjoint sublists xs,ys ⊆ zs |
| 65 |
-- |
| 66 |
-- NB: This does not imply that xs and ys partition zs; |
| 67 |
-- zs may have additional elements. |
| 68 |
|
| 69 |
private |
| 70 |
infix 4 _⊆_ |
| 71 |
_⊆_ = Sublist R |
| 72 |
|
| 73 |
infixr 5 _∷ₙ_ _∷ₗ_ _∷ᵣ_ |
| 74 |
|
| 75 |
data Disjoint : ∀ {xs ys zs} (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) → Set (a ⊔ b ⊔ r) where |
| 76 |
[] : Disjoint [] [] |
| 77 |
|
| 78 |
-- Element y of zs is neither in xs nor in ys: |
| 79 |
_∷ₙ_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} → |
| 80 |
(y : B) → Disjoint τ₁ τ₂ → Disjoint (y ∷ʳ τ₁) (y ∷ʳ τ₂) |
| 81 |
|
| 82 |
-- Element y of zs is in xs as x with x≈y: |
| 83 |
_∷ₗ_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {x y} → |
| 84 |
(x≈y : R x y) → Disjoint τ₁ τ₂ → Disjoint (x≈y ∷ τ₁) (y ∷ʳ τ₂) |
| 85 |
|
| 86 |
-- Element y of zs is in ys as x with x≈y: |
| 87 |
_∷ᵣ_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {x y} → |
| 88 |
(x≈y : R x y) → Disjoint τ₁ τ₂ → Disjoint (y ∷ʳ τ₁) (x≈y ∷ τ₂) |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- Disjoint union of two sublists xs,ys ⊆ zs |
| 92 |
-- |
| 93 |
-- This is the Cover relation without overlap of Section 6 of |
| 94 |
-- Conor McBride, Everybody's Got To Be Somewhere, |
| 95 |
-- MSFP@FSCD 2018: 53-69. |
| 96 |
|
| 97 |
data DisjointUnion : ∀ {xs ys zs us} (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) (τ : us ⊆ zs) → Set (a ⊔ b ⊔ r) where |
| 98 |
[] : DisjointUnion [] [] [] |
| 99 |
|
| 100 |
-- Element y of zs is neither in xs nor in ys: skip. |
| 101 |
_∷ₙ_ : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : us ⊆ zs} → |
| 102 |
(y : B) → DisjointUnion τ₁ τ₂ τ → DisjointUnion (y ∷ʳ τ₁) (y ∷ʳ τ₂) (y ∷ʳ τ) |
| 103 |
|
| 104 |
-- Element y of zs is in xs as x with x≈y: add to us. |
| 105 |
_∷ₗ_ : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : us ⊆ zs} {x y} → |
| 106 |
(x≈y : R x y) → DisjointUnion τ₁ τ₂ τ → DisjointUnion (x≈y ∷ τ₁) (y ∷ʳ τ₂) (x≈y ∷ τ) |
| 107 |
|
| 108 |
-- Element y of zs is in ys as x with x≈y: add to us. |
| 109 |
_∷ᵣ_ : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : us ⊆ zs} {x y} → |
| 110 |
(x≈y : R x y) → DisjointUnion τ₁ τ₂ τ → DisjointUnion (y ∷ʳ τ₁) (x≈y ∷ τ₂) (x≈y ∷ τ) |