Debian Codegraph @

src/Data/List/Relation/Unary/Linked/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties related to Linked
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.Linked.Properties where
10
11 open import Data.Bool.Base using (true; false)
12 open import Data.List.Base hiding (any)
13 open import Data.List.Relation.Unary.AllPairs as AllPairs
14 using (AllPairs; []; _∷_)
15 import Data.List.Relation.Unary.AllPairs.Properties as AllPairs
16 open import Data.List.Relation.Unary.All using (All; []; _∷_)
17 open import Data.List.Relation.Unary.Linked as Linked
18 using (Linked; []; [-]; _∷_)
19 open import Data.Nat.Base using (zero; suc; _<_; z<s; s<s)
20 open import Data.Nat.Properties using (≤-refl; m≤n⇒m≤1+n)
21 open import Data.Maybe.Relation.Binary.Connected
22 using (Connected; just; nothing; just-nothing; nothing-just)
23 open import Level using (Level)
24 open import Function.Base using (_∘_; flip; _on_)
25 open import Relation.Binary.Core using (Rel)
26 open import Relation.Binary.Bundles using (DecSetoid)
27 open import Relation.Binary.Definitions using (Transitive)
28 open import Relation.Binary.PropositionalEquality.Core using (_≢_)
29 open import Relation.Unary using (Pred; Decidable)
30 open import Relation.Nullary.Decidable using (yes; no; does)
31
32 private
33 variable
34 a b p ℓ : Level
35 A : Set a
36 B : Set b
37
38 ------------------------------------------------------------------------
39 -- Relationship to other predicates
40 ------------------------------------------------------------------------
41
42 module _ {R : Rel A ℓ} where
43
44 AllPairs⇒Linked : ∀ {xs} → AllPairs R xs → Linked R xs
45 AllPairs⇒Linked [] = []
46 AllPairs⇒Linked (px ∷ []) = [-]
47 AllPairs⇒Linked ((px ∷ _) ∷ py ∷ pxs) =
48 px ∷ (AllPairs⇒Linked (py ∷ pxs))
49
50 module _ {R : Rel A ℓ} (trans : Transitive R) where
51
52 Linked⇒All : ∀ {v x xs} → R v x →
53 Linked R (x ∷ xs) → All (R v) (x ∷ xs)
54 Linked⇒All Rvx [-] = Rvx ∷ []
55 Linked⇒All Rvx (Rxy ∷ Rxs) = Rvx ∷ Linked⇒All (trans Rvx Rxy) Rxs
56
57 Linked⇒AllPairs : ∀ {xs} → Linked R xs → AllPairs R xs
58 Linked⇒AllPairs [] = []
59 Linked⇒AllPairs [-] = [] ∷ []
60 Linked⇒AllPairs (Rxy ∷ Rxs) = Linked⇒All Rxy Rxs ∷ Linked⇒AllPairs Rxs
61
62 ------------------------------------------------------------------------
63 -- Introduction (⁺) and elimination (⁻) rules for list operations
64 ------------------------------------------------------------------------
65 -- map
66
67 module _ {R : Rel A ℓ} {f : B → A} where
68
69 map⁺ : ∀ {xs} → Linked (R on f) xs → Linked R (map f xs)
70 map⁺ [] = []
71 map⁺ [-] = [-]
72 map⁺ (Rxy ∷ Rxs) = Rxy ∷ map⁺ Rxs
73
74 map⁻ : ∀ {xs} → Linked R (map f xs) → Linked (R on f) xs
75 map⁻ {[]} [] = []
76 map⁻ {x ∷ []} [-] = [-]
77 map⁻ {x ∷ y ∷ xs} (Rxy ∷ Rxs) = Rxy ∷ map⁻ Rxs
78
79 ------------------------------------------------------------------------
80 -- _++_
81
82 module _ {R : Rel A ℓ} where
83
84 ++⁺ : ∀ {xs ys} →
85 Linked R xs →
86 Connected R (last xs) (head ys) →
87 Linked R ys →
88 Linked R (xs ++ ys)
89 ++⁺ [] _ Rys = Rys
90 ++⁺ [-] _ [] = [-]
91 ++⁺ [-] (just Rxy) [-] = Rxy ∷ [-]
92 ++⁺ [-] (just Rxy) (Ryz ∷ Rys) = Rxy ∷ Ryz ∷ Rys
93 ++⁺ (Rxy ∷ Rxs) Rxsys Rys = Rxy ∷ ++⁺ Rxs Rxsys Rys
94
95 ------------------------------------------------------------------------
96 -- applyUpTo
97
98 module _ {R : Rel A ℓ} where
99
100 applyUpTo⁺₁ : ∀ f n → (∀ {i} → suc i < n → R (f i) (f (suc i))) →
101 Linked R (applyUpTo f n)
102 applyUpTo⁺₁ f 0 Rf = []
103 applyUpTo⁺₁ f 1 Rf = [-]
104 applyUpTo⁺₁ f (suc n@(suc _)) Rf =
105 Rf (s<s z<s) ∷ (applyUpTo⁺₁ (f ∘ suc) n (Rf ∘ s<s))
106
107 applyUpTo⁺₂ : ∀ f n → (∀ i → R (f i) (f (suc i))) →
108 Linked R (applyUpTo f n)
109 applyUpTo⁺₂ f n Rf = applyUpTo⁺₁ f n (λ _ → Rf _)
110
111 ------------------------------------------------------------------------
112 -- applyDownFrom
113
114 module _ {R : Rel A ℓ} where
115
116 applyDownFrom⁺₁ : ∀ f n → (∀ {i} → suc i < n → R (f (suc i)) (f i)) →
117 Linked R (applyDownFrom f n)
118 applyDownFrom⁺₁ f 0 Rf = []
119 applyDownFrom⁺₁ f 1 Rf = [-]
120 applyDownFrom⁺₁ f (suc n@(suc _)) Rf =
121 Rf ≤-refl ∷ applyDownFrom⁺₁ f n (Rf ∘ m≤n⇒m≤1+n)
122
123 applyDownFrom⁺₂ : ∀ f n → (∀ i → R (f (suc i)) (f i)) →
124 Linked R (applyDownFrom f n)
125 applyDownFrom⁺₂ f n Rf = applyDownFrom⁺₁ f n (λ _ → Rf _)
126
127 ------------------------------------------------------------------------
128 -- filter
129
130 module _ {P : Pred A p} (P? : Decidable P)
131 {R : Rel A ℓ} (trans : Transitive R)
132 where
133
134 ∷-filter⁺ : ∀ {x xs} → Linked R (x ∷ xs) → Linked R (x ∷ filter P? xs)
135 ∷-filter⁺ [-] = [-]
136 ∷-filter⁺ {xs = y ∷ _} (r ∷ [-]) with does (P? y)
137 ... | false = [-]
138 ... | true = r ∷ [-]
139 ∷-filter⁺ {x = x} {xs = y ∷ ys} (r ∷ r′ ∷ rs)
140 with does (P? y) | ∷-filter⁺ {xs = ys} | ∷-filter⁺ (r′ ∷ rs)
141 ... | false | ihf | _ = ihf (trans r r′ ∷ rs)
142 ... | true | _ | iht = r ∷ iht
143
144 filter⁺ : ∀ {xs} → Linked R xs → Linked R (filter P? xs)
145 filter⁺ [] = []
146 filter⁺ {xs = x ∷ []} [-] with does (P? x)
147 ... | false = []
148 ... | true = [-]
149 filter⁺ {xs = x ∷ _} (r ∷ rs) with does (P? x)
150 ... | false = filter⁺ rs
151 ... | true = ∷-filter⁺ (r ∷ rs)