Debian Codegraph @

doc/README/Data/Fin/Relation/Unary/Top.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Example use of the 'top' view of Fin
5 --
6 -- This is an example of a view of (elements of) a datatype,
7 -- here i : Fin (suc n), which exhibits every such i as
8 -- * either, i = fromℕ n
9 -- * or, i = inject₁ j for a unique j : Fin n
10 --
11 -- Using this view, we can redefine certain operations in `Data.Fin.Base`,
12 -- together with their corresponding properties in `Data.Fin.Properties`.
13 ------------------------------------------------------------------------
14
15 {-# OPTIONS --cubical-compatible --safe #-}
16
17 module README.Data.Fin.Relation.Unary.Top where
18
19 open import Data.Nat.Base using (ℕ; zero; suc; _∸_; _≤_)
20 open import Data.Nat.Properties using (n∸n≡0; +-∸-assoc; ≤-reflexive)
21 open import Data.Fin.Base using (Fin; zero; suc; toℕ; fromℕ; inject₁; _>_)
22 open import Data.Fin.Properties using (toℕ-fromℕ; toℕ<n; toℕ-inject₁)
23 open import Data.Fin.Induction hiding (>-weakInduction)
24 open import Data.Fin.Relation.Unary.Top
25 import Induction.WellFounded as WF
26 open import Level using (Level)
27 open import Relation.Binary.PropositionalEquality
28 open import Relation.Unary using (Pred)
29
30 private
31 variable
32 ℓ : Level
33 n : ℕ
34
35 ------------------------------------------------------------------------
36 -- Reimplementation of `Data.Fin.Base.opposite`, and its properties
37
38 -- Definition
39
40 opposite : Fin n → Fin n
41 opposite {suc n} i with view i
42 ... | ‵fromℕ = zero
43 ... | ‵inject₁ j = suc (opposite {n} j)
44
45 -- Properties
46
47 opposite-zero≡fromℕ : ∀ n → opposite {suc n} zero ≡ fromℕ n
48 opposite-zero≡fromℕ zero = refl
49 opposite-zero≡fromℕ (suc n) = cong suc (opposite-zero≡fromℕ n)
50
51 opposite-fromℕ≡zero : ∀ n → opposite {suc n} (fromℕ n) ≡ zero
52 opposite-fromℕ≡zero n rewrite view-fromℕ n = refl
53
54 opposite-suc≡inject₁-opposite : (j : Fin n) →
55 opposite (suc j) ≡ inject₁ (opposite j)
56 opposite-suc≡inject₁-opposite {suc n} i with view i
57 ... | ‵fromℕ = refl
58 ... | ‵inject₁ j = cong suc (opposite-suc≡inject₁-opposite {n} j)
59
60 opposite-involutive : (j : Fin n) → opposite (opposite j) ≡ j
61 opposite-involutive {suc n} zero
62 rewrite opposite-zero≡fromℕ n
63 | view-fromℕ n = refl
64 opposite-involutive {suc n} (suc i)
65 rewrite opposite-suc≡inject₁-opposite i
66 | view-inject₁ (opposite i) = cong suc (opposite-involutive i)
67
68 opposite-suc : (j : Fin n) → toℕ (opposite (suc j)) ≡ toℕ (opposite j)
69 opposite-suc j = begin
70 toℕ (opposite (suc j)) ≡⟨ cong toℕ (opposite-suc≡inject₁-opposite j) ⟩
71 toℕ (inject₁ (opposite j)) ≡⟨ toℕ-inject₁ (opposite j) ⟩
72 toℕ (opposite j) ∎ where open ≡-Reasoning
73
74 opposite-prop : (j : Fin n) → toℕ (opposite j) ≡ n ∸ suc (toℕ j)
75 opposite-prop {suc n} i with view i
76 ... | ‵fromℕ rewrite toℕ-fromℕ n | n∸n≡0 n = refl
77 ... | ‵inject₁ j = begin
78 suc (toℕ (opposite j)) ≡⟨ cong suc (opposite-prop j) ⟩
79 suc (n ∸ suc (toℕ j)) ≡⟨ +-∸-assoc 1 (toℕ<n j) ⟨
80 n ∸ toℕ j ≡⟨ cong (n ∸_) (toℕ-inject₁ j) ⟨
81 n ∸ toℕ (inject₁ j) ∎ where open ≡-Reasoning
82
83 ------------------------------------------------------------------------
84 -- Reimplementation of `Data.Fin.Induction.>-weakInduction`
85
86 open WF using (Acc; acc)
87
88 >-weakInduction : (P : Pred (Fin (suc n)) ℓ) →
89 P (fromℕ n) →
90 (∀ i → P (suc i) → P (inject₁ i)) →
91 ∀ i → P i
92 >-weakInduction P Pₙ Pᵢ₊₁⇒Pᵢ i = induct (>-wellFounded i)
93 where
94 induct : ∀ {i} → Acc _>_ i → P i
95 induct {i} (acc rec) with view i
96 ... | ‵fromℕ = Pₙ
97 ... | ‵inject₁ j = Pᵢ₊₁⇒Pᵢ j (induct (rec _ inject₁[j]+1≤[j+1]))
98 where
99 inject₁[j]+1≤[j+1] : suc (toℕ (inject₁ j)) ≤ toℕ (suc j)
100 inject₁[j]+1≤[j+1] = ≤-reflexive (toℕ-inject₁ (suc j))