Debian Codegraph @

src/Data/Fin/Induction.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Induction over Fin
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8 {-# OPTIONS --warning=noUserWarning #-} -- for deprecated _≺_ (issue #1726)
9
10 module Data.Fin.Induction where
11
12 open import Data.Fin.Base using (Fin; zero; suc; _<_; toℕ; inject₁;
13 _≥_; _>_; fromℕ; _≺_)
14 open import Data.Fin.Properties using (toℕ-inject₁; ≤-refl; <-cmp;
15 toℕ≤n; toℕ-injective; toℕ-fromℕ; toℕ-lower₁; inject₁-lower₁;
16 pigeonhole; ≺⇒<′)
17 open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _∸_; s≤s)
18 open import Data.Nat.Properties using (n<1+n; ≤⇒≯)
19 import Data.Nat.Induction as ℕ using (<′-wellFounded; <-wellFounded)
20 import Data.Nat.Properties as ℕ using (<-cmp)
21 open import Data.Product.Base using (_,_)
22 open import Data.Vec.Base as Vec using (Vec; []; _∷_)
23 open import Data.Vec.Relation.Unary.Linked as Linked using (Linked; [-]; _∷_)
24 import Data.Vec.Relation.Unary.Linked.Properties as Linked
25 open import Function.Base using (flip; _$_)
26 open import Induction using (RecStruct)
27 open import Induction.WellFounded as WF using (WellFounded; WfRec;
28 module Subrelation)
29 open import Level using (Level)
30 open import Relation.Binary.Core using (Rel)
31 open import Relation.Binary.Bundles using (StrictPartialOrder)
32 open import Relation.Binary.Structures using (IsPartialOrder; IsStrictPartialOrder)
33 open import Relation.Binary.Definitions using (Decidable)
34 import Relation.Binary.Construct.Flip.EqAndOrd as EqAndOrd
35 import Relation.Binary.Construct.Flip.Ord as Ord
36 import Relation.Binary.Construct.NonStrictToStrict as ToStrict
37 import Relation.Binary.Construct.On as On using (wellFounded)
38 open import Relation.Binary.Definitions using (Tri; tri<; tri≈; tri>)
39 open import Relation.Binary.PropositionalEquality.Core
40 using (_≡_; refl; sym; subst; trans; cong)
41 open import Relation.Nullary.Decidable using (yes; no)
42 open import Relation.Nullary.Negation using (contradiction)
43 open import Relation.Unary using (Pred)
44
45 private
46 variable
47 ℓ : Level
48 n : ℕ
49
50 ------------------------------------------------------------------------
51 -- Re-export accessability
52
53 open WF public using (Acc; acc)
54
55 ------------------------------------------------------------------------
56 -- Induction over _<_
57
58 <-wellFounded : WellFounded {A = Fin n} _<_
59 <-wellFounded = On.wellFounded toℕ ℕ.<-wellFounded
60
61 <-weakInduction-startingFrom : ∀ (P : Pred (Fin (suc n)) ℓ) →
62 ∀ {i} → P i →
63 (∀ j → P (inject₁ j) → P (suc j)) →
64 ∀ {j} → j ≥ i → P j
65 <-weakInduction-startingFrom P {i} Pi Pᵢ⇒Pᵢ₊₁ {j} j≥i = induct (<-wellFounded _) (<-cmp i j) j≥i
66 where
67 induct : ∀ {j} → Acc _<_ j → Tri (i < j) (i ≡ j) (i > j) → j ≥ i → P j
68 induct (acc rs) (tri≈ _ refl _) i≤j = Pi
69 induct (acc rs) (tri> _ _ i>sj) i≤j with () ← ≤⇒≯ i≤j i>sj
70 induct {suc j} (acc rs) (tri< (s≤s i≤j) _ _) _ = Pᵢ⇒Pᵢ₊₁ j P[1+j]
71 where
72 toℕj≡toℕinjJ = sym $ toℕ-inject₁ j
73 P[1+j] = induct (rs (s≤s (subst (ℕ._≤ toℕ j) toℕj≡toℕinjJ ≤-refl)))
74 (<-cmp i $ inject₁ j) (subst (toℕ i ℕ.≤_) toℕj≡toℕinjJ i≤j)
75
76 <-weakInduction : (P : Pred (Fin (suc n)) ℓ) →
77 P zero →
78 (∀ i → P (inject₁ i) → P (suc i)) →
79 ∀ i → P i
80 <-weakInduction P P₀ Pᵢ⇒Pᵢ₊₁ i = <-weakInduction-startingFrom P P₀ Pᵢ⇒Pᵢ₊₁ ℕ.z≤n
81
82
83 ------------------------------------------------------------------------
84 -- Induction over _>_
85
86 private
87 acc-map : ∀ {x : Fin n} → Acc ℕ._<_ (n ∸ toℕ x) → Acc _>_ x
88 acc-map (acc rs) = acc λ y>x → acc-map (rs (ℕ.∸-monoʳ-< y>x (toℕ≤n _)))
89
90 >-wellFounded : WellFounded {A = Fin n} _>_
91 >-wellFounded {n} x = acc-map (ℕ.<-wellFounded (n ∸ toℕ x))
92
93 >-weakInduction : (P : Pred (Fin (suc n)) ℓ) →
94 P (fromℕ n) →
95 (∀ i → P (suc i) → P (inject₁ i)) →
96 ∀ i → P i
97 >-weakInduction {n = n} P Pₙ Pᵢ₊₁⇒Pᵢ i = induct (>-wellFounded i)
98 where
99 induct : ∀ {i} → Acc _>_ i → P i
100 induct {i} (acc rec) with n ℕ.≟ toℕ i
101 ... | yes n≡i = subst P (toℕ-injective (trans (toℕ-fromℕ n) n≡i)) Pₙ
102 ... | no n≢i = subst P (inject₁-lower₁ i n≢i) (Pᵢ₊₁⇒Pᵢ _ Pᵢ₊₁)
103 where Pᵢ₊₁ = induct (rec (ℕ.≤-reflexive (cong suc (sym (toℕ-lower₁ i n≢i)))))
104
105 ------------------------------------------------------------------------
106 -- Well-foundedness of other (strict) partial orders on Fin
107
108 module _ {_≈_ : Rel (Fin n) ℓ} where
109
110 -- Every (strict) partial order over `Fin n' is well-founded.
111
112 -- Intuition: there cannot be any infinite descending chains simply
113 -- because Fin n has only finitely many inhabitants. Thus any chain
114 -- of length > n must have a cycle (which is forbidden by
115 -- irreflexivity).
116
117 spo-wellFounded : ∀ {r} {_⊏_ : Rel (Fin n) r} →
118 IsStrictPartialOrder _≈_ _⊏_ → WellFounded _⊏_
119 spo-wellFounded {_} {_⊏_} isSPO i = go n pigeon where
120
121 module ⊏ = IsStrictPartialOrder isSPO
122
123 go : ∀ m {i} →
124 ({xs : Vec (Fin n) m} → Linked (flip _⊏_) (i ∷ xs) → WellFounded _⊏_) →
125 Acc _⊏_ i
126 go zero k = k [-] _
127 go (suc m) k = acc λ j⊏i → go m λ i∷xs↑ → k (j⊏i ∷ i∷xs↑)
128
129 pigeon : {xs : Vec (Fin n) n} → Linked (flip _⊏_) (i ∷ xs) → WellFounded _⊏_
130 pigeon {xs} i∷xs↑ =
131 let (i₁ , i₂ , i₁<i₂ , xs[i₁]≡xs[i₂]) = pigeonhole (n<1+n n) (Vec.lookup (i ∷ xs)) in
132 let xs[i₁]⊏xs[i₂] = Linked.lookup⁺ (Ord.transitive _⊏_ ⊏.trans) i∷xs↑ i₁<i₂ in
133 let xs[i₁]⊏xs[i₁] = ⊏.<-respʳ-≈ (⊏.Eq.reflexive xs[i₁]≡xs[i₂]) xs[i₁]⊏xs[i₂] in
134 contradiction xs[i₁]⊏xs[i₁] (⊏.irrefl ⊏.Eq.refl)
135
136 po-wellFounded : ∀ {r} {_⊑_ : Rel (Fin n) r} →
137 IsPartialOrder _≈_ _⊑_ → WellFounded (ToStrict._<_ _≈_ _⊑_)
138 po-wellFounded isPO =
139 spo-wellFounded (ToStrict.<-isStrictPartialOrder _≈_ _ isPO)
140
141 -- The inverse order is also well-founded, i.e. every (strict)
142 -- partial order is also Noetherian.
143
144 spo-noetherian : ∀ {r} {_⊏_ : Rel (Fin n) r} →
145 IsStrictPartialOrder _≈_ _⊏_ → WellFounded (flip _⊏_)
146 spo-noetherian isSPO = spo-wellFounded (EqAndOrd.isStrictPartialOrder isSPO)
147
148 po-noetherian : ∀ {r} {_⊑_ : Rel (Fin n) r} → IsPartialOrder _≈_ _⊑_ →
149 WellFounded (flip (ToStrict._<_ _≈_ _⊑_))
150 po-noetherian isPO =
151 spo-noetherian (ToStrict.<-isStrictPartialOrder _≈_ _ isPO)
152
153 ------------------------------------------------------------------------
154 -- DEPRECATED NAMES
155 ------------------------------------------------------------------------
156 -- Please use the new names as continuing support for the old names is
157 -- not guaranteed.
158
159 -- Version 2.0
160
161 ≺-Rec : RecStruct ℕ ℓ ℓ
162 ≺-Rec = WfRec _≺_
163
164 ≺-wellFounded : WellFounded _≺_
165 ≺-wellFounded = Subrelation.wellFounded ≺⇒<′ ℕ.<′-wellFounded
166
167 module _ {ℓ} where
168 open WF.All ≺-wellFounded ℓ public
169 renaming
170 ( wfRecBuilder to ≺-recBuilder
171 ; wfRec to ≺-rec
172 )
173 hiding (wfRec-builder)
174
175 {-# WARNING_ON_USAGE ≺-Rec
176 "Warning: ≺-Rec was deprecated in v2.0.
177 Please use <-Rec instead."
178 #-}
179 {-# WARNING_ON_USAGE ≺-wellFounded
180 "Warning: ≺-wellFounded was deprecated in v2.0.
181 Please use <-wellFounded instead."
182 #-}
183 {-# WARNING_ON_USAGE ≺-recBuilder
184 "Warning: ≺-recBuilder was deprecated in v2.0.
185 Please use <-recBuilder instead."
186 #-}
187 {-# WARNING_ON_USAGE ≺-rec
188 "Warning: ≺-rec was deprecated in v2.0.
189 Please use <-rec instead."
190 #-}
191