Debian Codegraph @

src/Data/List/Relation/Binary/Sublist/Propositional.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An inductive definition of the sublist relation. This is commonly
5 -- known as Order Preserving Embeddings (OPE).
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 module Data.List.Relation.Binary.Sublist.Propositional
11 {a} {A : Set a} where
12
13 open import Data.List.Base using (List)
14 open import Data.List.Relation.Binary.Equality.Propositional using (≋⇒≡)
15 import Data.List.Relation.Binary.Sublist.Setoid as SetoidSublist
16 open import Data.List.Relation.Unary.Any using (Any)
17 open import Relation.Binary.Core using (_⇒_)
18 open import Relation.Binary.Bundles using (Preorder; Poset)
19 open import Relation.Binary.Structures using (IsPreorder; IsPartialOrder)
20 open import Relation.Binary.Definitions using (Antisymmetric)
21 open import Relation.Binary.PropositionalEquality.Core
22 using (subst; _≡_; refl)
23 open import Relation.Binary.PropositionalEquality.Properties
24 using (setoid; isEquivalence)
25 open import Relation.Unary using (Pred)
26
27 ------------------------------------------------------------------------
28 -- Re-export definition and operations from setoid sublists
29
30 open SetoidSublist (setoid A) public
31 hiding
32 (lookup; ⊆-reflexive; ⊆-antisym
33 ; ⊆-isPreorder; ⊆-isPartialOrder
34 ; ⊆-preorder; ⊆-poset
35 )
36
37 ------------------------------------------------------------------------
38 -- Additional operations
39
40 module _ {p} {P : Pred A p} where
41
42 lookup : ∀ {xs ys} → xs ⊆ ys → Any P xs → Any P ys
43 lookup = SetoidSublist.lookup (setoid A) (subst _)
44
45 ------------------------------------------------------------------------
46 -- Relational properties
47
48 ⊆-reflexive : _≡_ ⇒ _⊆_
49 ⊆-reflexive refl = ⊆-refl
50
51 ⊆-antisym : Antisymmetric _≡_ _⊆_
52 ⊆-antisym xs⊆ys ys⊆xs = ≋⇒≡ (SetoidSublist.⊆-antisym (setoid A) xs⊆ys ys⊆xs)
53
54 ⊆-isPreorder : IsPreorder _≡_ _⊆_
55 ⊆-isPreorder = record
56 { isEquivalence = isEquivalence
57 ; reflexive = ⊆-reflexive
58 ; trans = ⊆-trans
59 }
60
61 ⊆-isPartialOrder : IsPartialOrder _≡_ _⊆_
62 ⊆-isPartialOrder = record
63 { isPreorder = ⊆-isPreorder
64 ; antisym = ⊆-antisym
65 }
66
67 ⊆-preorder : Preorder a a a
68 ⊆-preorder = record
69 { isPreorder = ⊆-isPreorder
70 }
71
72 ⊆-poset : Poset a a a
73 ⊆-poset = record
74 { isPartialOrder = ⊆-isPartialOrder
75 }
76
77 ------------------------------------------------------------------------
78 -- Separating two sublists
79 --
80 -- Two possibly overlapping sublists τ : xs ⊆ zs and σ : ys ⊆ zs
81 -- can be turned into disjoint lists τρ : xs ⊆ zs and τρ : ys ⊆ zs′
82 -- by duplicating all entries of zs that occur both in xs and ys,
83 -- resulting in an extension ρ : zs ⊆ zs′ of zs.
84
85 record Separation {xs ys zs} (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) : Set a where
86 field
87 {inflation} : List A
88 separator₁ : zs ⊆ inflation
89 separator₂ : zs ⊆ inflation
90 separated₁ = ⊆-trans τ₁ separator₁
91 separated₂ = ⊆-trans τ₂ separator₂
92 field
93 disjoint : Disjoint separated₁ separated₂
94
95 infixr 5 _∷ₙ-Sep_ _∷ₗ-Sep_ _∷ᵣ-Sep_
96
97 -- Empty separation
98
99 []-Sep : Separation [] []
100 []-Sep = record { separator₁ = [] ; separator₂ = [] ; disjoint = [] }
101
102 -- Weaken a separation
103
104 _∷ₙ-Sep_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
105 ∀ z → Separation τ₁ τ₂ → Separation (z ∷ʳ τ₁) (z ∷ʳ τ₂)
106 z ∷ₙ-Sep record{ separator₁ = ρ₁; separator₂ = ρ₂; disjoint = d } = record
107 { separator₁ = refl ∷ ρ₁
108 ; separator₂ = refl ∷ ρ₂
109 ; disjoint = z ∷ₙ d
110 }
111
112 -- Extend a separation by an element of the first sublist.
113 --
114 -- Note: this requires a category law from the underlying equality,
115 -- trans x=z refl = x=z, thus, separation is not available for Sublist.Setoid.
116
117 _∷ₗ-Sep_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
118 ∀ {x z} (x≡z : x ≡ z) → Separation τ₁ τ₂ → Separation (x≡z ∷ τ₁) (z ∷ʳ τ₂)
119 refl ∷ₗ-Sep record{ separator₁ = ρ₁; separator₂ = ρ₂; disjoint = d } = record
120 { separator₁ = refl ∷ ρ₁
121 ; separator₂ = refl ∷ ρ₂
122 ; disjoint = refl ∷ₗ d
123 }
124
125 -- Extend a separation by an element of the second sublist.
126
127 _∷ᵣ-Sep_ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
128 ∀ {y z} (y≡z : y ≡ z) → Separation τ₁ τ₂ → Separation (z ∷ʳ τ₁) (y≡z ∷ τ₂)
129 refl ∷ᵣ-Sep record{ separator₁ = ρ₁; separator₂ = ρ₂; disjoint = d } = record
130 { separator₁ = refl ∷ ρ₁
131 ; separator₂ = refl ∷ ρ₂
132 ; disjoint = refl ∷ᵣ d
133 }
134
135 -- Extend a separation by a common element of both sublists.
136 --
137 -- Left-biased: the left separator gets the first copy
138 -- of the common element.
139
140 ∷-Sepˡ : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
141 ∀ {x y z} (x≡z : x ≡ z) (y≡z : y ≡ z) →
142 Separation τ₁ τ₂ → Separation (x≡z ∷ τ₁) (y≡z ∷ τ₂)
143 ∷-Sepˡ refl refl record{ separator₁ = ρ₁; separator₂ = ρ₂; disjoint = d } = record
144 { separator₁ = _ ∷ʳ refl ∷ ρ₁
145 ; separator₂ = refl ∷ _ ∷ʳ ρ₂
146 ; disjoint = refl ∷ᵣ (refl ∷ₗ d)
147 }
148
149 -- Left-biased separation of two sublists. Of common elements,
150 -- the first sublist receives the first copy.
151
152 separateˡ : ∀ {xs ys zs} (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) → Separation τ₁ τ₂
153 separateˡ [] [] = []-Sep
154 separateˡ (z ∷ʳ τ₁) (z ∷ʳ τ₂) = z ∷ₙ-Sep separateˡ τ₁ τ₂
155 separateˡ (z ∷ʳ τ₁) (y≡z ∷ τ₂) = y≡z ∷ᵣ-Sep separateˡ τ₁ τ₂
156 separateˡ (x≡z ∷ τ₁) (z ∷ʳ τ₂) = x≡z ∷ₗ-Sep separateˡ τ₁ τ₂
157 separateˡ (x≡z ∷ τ₁) (y≡z ∷ τ₂) = ∷-Sepˡ x≡z y≡z (separateˡ τ₁ τ₂)