Debian Codegraph @

src/Data/Fin/Relation/Unary/Top.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- 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
12 {-# OPTIONS --cubical-compatible --safe #-}
13
14 module Data.Fin.Relation.Unary.Top where
15
16 open import Data.Nat.Base using (ℕ; zero; suc)
17 open import Data.Fin.Base using (Fin; zero; suc; fromℕ; inject₁)
18 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
19 open import Relation.Binary.PropositionalEquality.Properties
20 using (module ≡-Reasoning)
21
22 private
23 variable
24 n : ℕ
25 i : Fin n
26
27 ------------------------------------------------------------------------
28 -- The View, considered as a unary relation on Fin n
29
30 -- NB `Data.Fin.Properties.fromℕ≢inject₁` establishes that the following
31 -- inductively defined family on `Fin n` has constructors which target
32 -- *disjoint* instances of the family (moreover at indices `n = suc _`);
33 -- hence the interpretations of the View constructors will also be disjoint.
34
35 data View : (i : Fin n) → Set where
36 ‵fromℕ : View (fromℕ n)
37 ‵inj₁ : View i → View (inject₁ i)
38
39 pattern ‵inject₁ i = ‵inj₁ {i = i} _
40
41 -- The view covering function, witnessing soundness of the view
42
43 view : (i : Fin n) → View i
44 view zero = view-zero where
45 view-zero : View (zero {n})
46 view-zero {n = zero} = ‵fromℕ
47 view-zero {n = suc _} = ‵inj₁ view-zero
48 view (suc i) with view i
49 ... | ‵fromℕ = ‵fromℕ
50 ... | ‵inject₁ i = ‵inj₁ (view (suc i))
51
52 -- Interpretation of the view constructors
53
54 ⟦_⟧ : {i : Fin n} → View i → Fin n
55 ⟦ ‵fromℕ ⟧ = fromℕ _
56 ⟦ ‵inject₁ i ⟧ = inject₁ i
57
58 -- Completeness of the view
59
60 view-complete : (v : View i) → ⟦ v ⟧ ≡ i
61 view-complete ‵fromℕ = refl
62 view-complete (‵inj₁ _) = refl
63
64 -- 'Computational' behaviour of the covering function
65
66 view-fromℕ : ∀ n → view (fromℕ n) ≡ ‵fromℕ
67 view-fromℕ zero = refl
68 view-fromℕ (suc n) rewrite view-fromℕ n = refl
69
70 view-inject₁ : (i : Fin n) → view (inject₁ i) ≡ ‵inj₁ (view i)
71 view-inject₁ zero = refl
72 view-inject₁ (suc i) rewrite view-inject₁ i = refl
73
74 -- Uniqueness of the view
75
76 view-unique : (v : View i) → view i ≡ v
77 view-unique ‵fromℕ = view-fromℕ _
78 view-unique (‵inj₁ {i = i} v) = begin
79 view (inject₁ i) ≡⟨ view-inject₁ i ⟩
80 ‵inj₁ (view i) ≡⟨ cong ‵inj₁ (view-unique v) ⟩
81 ‵inj₁ v ∎ where open ≡-Reasoning