Debian Codegraph @

src/Data/List/Fresh/Membership/Setoid/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the membership predicate for fresh lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Bundles using (Setoid)
10
11 module Data.List.Fresh.Membership.Setoid.Properties {c ℓ} (S : Setoid c ℓ)
12 where
13
14 open import Level using (Level; _⊔_)
15 open import Data.List.Fresh
16 open import Data.List.Fresh.Properties using (fresh-respectsˡ)
17 open import Data.List.Fresh.Membership.Setoid S using (_∈_; _∉_)
18 open import Data.List.Fresh.Relation.Unary.Any using (Any; here; there; _─_)
19 import Data.List.Fresh.Relation.Unary.Any.Properties as List#
20 using (length-remove)
21 open import Data.Empty using (⊥; ⊥-elim)
22 open import Data.Nat.Base using (ℕ; suc; zero; _≤_; _<_; z≤n; s≤s; z<s; s<s)
23 open import Data.Nat.Properties using (module ≤-Reasoning)
24 open import Data.Product.Base using (∃; _×_; _,_)
25 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; fromInj₂)
26 open import Function.Base using (id; _∘′_; _$_)
27 open import Relation.Binary.Core using (Rel)
28 open import Relation.Nullary.Negation.Core using (¬_)
29 open import Relation.Nullary.Irrelevant using (Irrelevant)
30 open import Relation.Unary as Unary using (Pred)
31 import Relation.Binary.Definitions as Binary using (_Respectsˡ_; Irrelevant)
32 import Relation.Binary.PropositionalEquality.Core as ≡
33 using (_≡_; cong; sym; subst)
34 open import Relation.Nary using (∀[_]; _⇒_)
35 open import Relation.Nullary.Negation.Core using (contradiction)
36
37 open Setoid S renaming (Carrier to A)
38
39 private
40 variable
41 p r : Level
42
43 ------------------------------------------------------------------------
44 -- transport
45
46 module _ {R : Rel A r} where
47
48 ≈-subst-∈ : ∀ {x y} {xs : List# A R} → x ≈ y → x ∈ xs → y ∈ xs
49 ≈-subst-∈ x≈y (here x≈x′) = here (trans (sym x≈y) x≈x′)
50 ≈-subst-∈ x≈y (there x∈xs) = there (≈-subst-∈ x≈y x∈xs)
51
52 ------------------------------------------------------------------------
53 -- relationship to fresh
54
55 module _ {R : Rel A r} (R⇒≉ : ∀[ R ⇒ _≉_ ]) where
56
57 fresh⇒∉ : ∀ {x} {xs : List# A R} → x # xs → x ∉ xs
58 fresh⇒∉ (r , _) (here x≈y) = R⇒≉ r x≈y
59 fresh⇒∉ (_ , x#xs) (there x∈xs) = fresh⇒∉ x#xs x∈xs
60
61 ------------------------------------------------------------------------
62 -- disjointness
63
64 module _ {R : Rel A r} where
65
66 distinct : ∀ {x y} {xs : List# A R} → x ∈ xs → y ∉ xs → x ≉ y
67 distinct x∈xs y∉xs x≈y = y∉xs (≈-subst-∈ x≈y x∈xs)
68
69 ------------------------------------------------------------------------
70 -- remove
71
72 module _ {R : Rel A r} where
73
74 remove-inv : ∀ {x y} {xs : List# A R} (x∈xs : x ∈ xs) →
75 y ∈ xs → x ≈ y ⊎ y ∈ (xs ─ x∈xs)
76 remove-inv (here x≈z) (here y≈z) = inj₁ (trans x≈z (sym y≈z))
77 remove-inv (here _) (there y∈xs) = inj₂ y∈xs
78 remove-inv (there _) (here y≈z) = inj₂ (here y≈z)
79 remove-inv (there x∈xs) (there y∈xs) = Sum.map₂ there (remove-inv x∈xs y∈xs)
80
81 ∈-remove : ∀ {x y} {xs : List# A R} (x∈xs : x ∈ xs) → y ∈ xs → x ≉ y → y ∈ (xs ─ x∈xs)
82 ∈-remove x∈xs y∈xs x≉y = fromInj₂ (⊥-elim ∘′ x≉y) (remove-inv x∈xs y∈xs)
83
84 module _ {R : Rel A r} (R⇒≉ : ∀[ R ⇒ _≉_ ]) (≉⇒R : ∀[ _≉_ ⇒ R ]) where
85
86 private
87 R≈ : R Binary.Respectsˡ _≈_
88 R≈ x≈y Rxz = ≉⇒R (R⇒≉ Rxz ∘′ trans x≈y)
89
90 fresh-remove : ∀ {x} {xs : List# A R} (x∈xs : x ∈ xs) → x # (xs ─ x∈xs)
91 fresh-remove {xs = cons x xs pr} (here x≈y) = fresh-respectsˡ R≈ (sym x≈y) pr
92 fresh-remove {xs = cons x xs pr} (there x∈xs) =
93 ≉⇒R (distinct x∈xs (fresh⇒∉ R⇒≉ pr)) , fresh-remove x∈xs
94
95 ∉-remove : ∀ {x} {xs : List# A R} (x∈xs : x ∈ xs) → x ∉ (xs ─ x∈xs)
96 ∉-remove x∈xs = fresh⇒∉ R⇒≉ (fresh-remove x∈xs)
97
98 ------------------------------------------------------------------------
99 -- injection
100
101 module _ {R : Rel A r} (R⇒≉ : ∀[ R ⇒ _≉_ ]) where
102
103 injection : ∀ {xs ys : List# A R} (inj : ∀ {x} → x ∈ xs → x ∈ ys) →
104 length xs ≤ length ys
105 injection {[]} {ys} inj = z≤n
106 injection {xxs@(cons x xs pr)} {ys} inj = begin
107 length xxs ≤⟨ s≤s (injection step) ⟩
108 suc (length (ys ─ x∈ys)) ≡⟨ ≡.sym (List#.length-remove x∈ys) ⟩
109 length ys ∎
110
111 where
112
113 open ≤-Reasoning
114
115 x∉xs : x ∉ xs
116 x∉xs = fresh⇒∉ R⇒≉ pr
117
118 x∈ys : x ∈ ys
119 x∈ys = inj (here refl)
120
121 step : ∀ {y} → y ∈ xs → y ∈ (ys ─ x∈ys)
122 step {y} y∈xs = ∈-remove x∈ys (inj (there y∈xs)) (distinct y∈xs x∉xs ∘′ sym)
123
124 strict-injection : ∀ {xs ys : List# A R} (inj : ∀ {x} → x ∈ xs → x ∈ ys) →
125 (∃ λ x → x ∈ ys × x ∉ xs) → length xs < length ys
126 strict-injection {xs} {ys} inj (x , x∈ys , x∉xs) = begin
127 suc (length xs) ≤⟨ s≤s (injection step) ⟩
128 suc (length (ys ─ x∈ys)) ≡⟨ ≡.sym (List#.length-remove x∈ys) ⟩
129 length ys ∎
130
131 where
132
133 open ≤-Reasoning
134
135 step : ∀ {y} → y ∈ xs → y ∈ (ys ─ x∈ys)
136 step {y} y∈xs = fromInj₂ (λ x≈y → contradiction ((≈-subst-∈ (sym x≈y) y∈xs)) x∉xs)
137 $ remove-inv x∈ys (inj y∈xs)
138
139
140 ------------------------------------------------------------------------
141 -- proof irrelevance
142
143 module _ {R : Rel A r} (R⇒≉ : ∀[ R ⇒ _≉_ ]) (≈-irrelevant : Binary.Irrelevant _≈_) where
144
145 ∈-irrelevant : ∀ {x} {xs : List# A R} → Irrelevant (x ∈ xs)
146 -- positive cases
147 ∈-irrelevant (here x≈y₁) (here x≈y₂) = ≡.cong here (≈-irrelevant x≈y₁ x≈y₂)
148 ∈-irrelevant (there x∈xs₁) (there x∈xs₂) = ≡.cong there (∈-irrelevant x∈xs₁ x∈xs₂)
149 -- absurd cases
150 ∈-irrelevant {xs = cons x xs pr} (here x≈y) (there x∈xs₂) =
151 contradiction x≈y (distinct x∈xs₂ (fresh⇒∉ R⇒≉ pr))
152 ∈-irrelevant {xs = cons x xs pr} (there x∈xs₁) (here x≈y) =
153 contradiction x≈y (distinct x∈xs₁ (fresh⇒∉ R⇒≉ pr))