Debian Codegraph @

src/Relation/Binary/Construct/Closure/Transitive.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Transitive closures
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Binary.Construct.Closure.Transitive where
10
11 open import Function.Base using (id; _∘_; _∋_)
12 open import Function.Bundles using (_⇔_; mk⇔)
13 open import Induction.WellFounded
14 open import Level using (Level; _⊔_)
15 open import Relation.Binary.Core using (Rel; _=[_]⇒_; _⇒_)
16 open import Relation.Binary.Definitions
17 using (Reflexive; Symmetric; Transitive)
18 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
19
20 private
21 variable
22 a ℓ ℓ₁ ℓ₂ : Level
23 A B : Set a
24
25 ------------------------------------------------------------------------
26 -- Definition
27
28 infixr 5 _∷_
29 infix 4 TransClosure
30
31 data TransClosure {A : Set a} (_∼_ : Rel A ℓ) : Rel A (a ⊔ ℓ) where
32 [_] : ∀ {x y} (x∼y : x ∼ y) → TransClosure _∼_ x y
33 _∷_ : ∀ {x y z} (x∼y : x ∼ y) (y∼⁺z : TransClosure _∼_ y z) → TransClosure _∼_ x z
34
35 syntax TransClosure R x y = x ⟨ R ⟩⁺ y
36
37 ------------------------------------------------------------------------
38 -- Operations
39
40 module _ {_∼_ : Rel A ℓ} where
41 private
42 _∼⁺_ = TransClosure _∼_
43
44 infixr 5 _∷ʳ_
45
46 _∷ʳ_ : ∀ {x y z} → (x∼⁺y : x ∼⁺ y) (y∼z : y ∼ z) → x ∼⁺ z
47 [ x∼y ] ∷ʳ y∼z = x∼y ∷ [ y∼z ]
48 (x∼y ∷ x∼⁺y) ∷ʳ y∼z = x∼y ∷ (x∼⁺y ∷ʳ y∼z)
49
50 infixr 5 _++_
51 _++_ : ∀ {x y z} → x ∼⁺ y → y ∼⁺ z → x ∼⁺ z
52 [ x∼y ] ++ y∼⁺z = x∼y ∷ y∼⁺z
53 (x∼y ∷ y∼⁺z) ++ z∼⁺u = x∼y ∷ (y∼⁺z ++ z∼⁺u)
54
55 ------------------------------------------------------------------------
56 -- Properties
57
58 module _ (_∼_ : Rel A ℓ) where
59 private
60 _∼⁺_ = TransClosure _∼_
61 module ∼⊆∼⁺ = Subrelation {_<₂_ = _∼⁺_} [_]
62
63 reflexive : Reflexive _∼_ → Reflexive _∼⁺_
64 reflexive refl = [ refl ]
65
66 symmetric : Symmetric _∼_ → Symmetric _∼⁺_
67 symmetric sym [ x∼y ] = [ sym x∼y ]
68 symmetric sym (x∼y ∷ y∼⁺z) = symmetric sym y∼⁺z ∷ʳ sym x∼y
69
70 transitive : Transitive _∼⁺_
71 transitive = _++_
72
73 transitive⁻ : Transitive _∼_ → _∼⁺_ ⇒ _∼_
74 transitive⁻ trans [ x∼y ] = x∼y
75 transitive⁻ trans (x∼y ∷ x∼⁺y) = trans x∼y (transitive⁻ trans x∼⁺y)
76
77 accessible⁻ : ∀ {x} → Acc _∼⁺_ x → Acc _∼_ x
78 accessible⁻ = ∼⊆∼⁺.accessible
79
80 wellFounded⁻ : WellFounded _∼⁺_ → WellFounded _∼_
81 wellFounded⁻ = ∼⊆∼⁺.wellFounded
82
83 accessible : ∀ {x} → Acc _∼_ x → Acc _∼⁺_ x
84 accessible acc[x] = acc (wf-acc acc[x])
85 where
86 wf-acc : ∀ {x} → Acc _∼_ x → WfRec _∼⁺_ (Acc _∼⁺_) x
87 wf-acc (acc rec) [ y∼x ] = acc (wf-acc (rec y∼x))
88 wf-acc acc[x] (y∼z ∷ z∼⁺x) = acc-inverse (wf-acc acc[x] z∼⁺x) [ y∼z ]
89
90 wellFounded : WellFounded _∼_ → WellFounded _∼⁺_
91 wellFounded wf x = accessible (wf x)
92
93
94 ------------------------------------------------------------------------
95 -- Alternative definition of transitive closure
96
97 infix 4 Plus
98
99 syntax Plus R x y = x [ R ]⁺ y
100
101 data Plus {A : Set a} (_∼_ : Rel A ℓ) : Rel A (a ⊔ ℓ) where
102 [_] : ∀ {x y} (x∼y : x ∼ y) → x [ _∼_ ]⁺ y
103 _∼⁺⟨_⟩_ : ∀ x {y z} (x∼⁺y : x [ _∼_ ]⁺ y) (y∼⁺z : y [ _∼_ ]⁺ z) →
104 x [ _∼_ ]⁺ z
105
106 module _ {_∼_ : Rel A ℓ} where
107
108 []-injective : ∀ {x y p q} → (x [ _∼_ ]⁺ y ∋ [ p ]) ≡ [ q ] → p ≡ q
109 []-injective ≡.refl = ≡.refl
110
111 -- See also ∼⁺⟨⟩-injectiveˡ and ∼⁺⟨⟩-injectiveʳ in
112 -- Relation.Binary.Construct.Closure.Transitive.WithK.
113
114 -- "Equational" reasoning notation. Example:
115 --
116 -- lemma =
117 -- x ∼⁺⟨ [ lemma₁ ] ⟩
118 -- y ∼⁺⟨ lemma₂ ⟩∎
119 -- z ∎
120
121 finally : ∀ {_∼_ : Rel A ℓ} x y → x [ _∼_ ]⁺ y → x [ _∼_ ]⁺ y
122 finally _ _ = id
123
124 syntax finally x y x∼⁺y = x ∼⁺⟨ x∼⁺y ⟩∎ y ∎
125
126 infixr 2 _∼⁺⟨_⟩_
127 infix 3 finally
128
129 -- Map.
130
131 map : {_R₁_ : Rel A ℓ} {_R₂_ : Rel B ℓ₂} {f : A → B} →
132 _R₁_ =[ f ]⇒ _R₂_ → Plus _R₁_ =[ f ]⇒ Plus _R₂_
133 map R₁⇒R₂ [ xRy ] = [ R₁⇒R₂ xRy ]
134 map R₁⇒R₂ (x ∼⁺⟨ xR⁺z ⟩ zR⁺y) =
135 _ ∼⁺⟨ map R₁⇒R₂ xR⁺z ⟩ map R₁⇒R₂ zR⁺y
136
137 -- Plus and TransClosure are equivalent.
138 equivalent : ∀ {_∼_ : Rel A ℓ} {x y} →
139 Plus _∼_ x y ⇔ TransClosure _∼_ x y
140 equivalent {_∼_ = _∼_} = mk⇔ complete sound
141 where
142 complete : Plus _∼_ ⇒ TransClosure _∼_
143 complete [ x∼y ] = [ x∼y ]
144 complete (x ∼⁺⟨ x∼⁺y ⟩ y∼⁺z) = complete x∼⁺y ++ complete y∼⁺z
145
146 sound : TransClosure _∼_ ⇒ Plus _∼_
147 sound [ x∼y ] = [ x∼y ]
148 sound (x∼y ∷ y∼⁺z) = _ ∼⁺⟨ [ x∼y ] ⟩ sound y∼⁺z
149
150 ------------------------------------------------------------------------
151 -- Deprecations
152 ------------------------------------------------------------------------
153 -- Please use the new names as continuing support for the old names is
154 -- not guaranteed.
155
156 -- v1.5
157
158 Plus′ = TransClosure
159 {-# WARNING_ON_USAGE Plus′
160 "Warning: Plus′ was deprecated in v1.5.
161 Please use TransClosure instead."
162 #-}