163
|
1 {-# OPTIONS --allow-unsolved-metas #-}
|
|
2
|
|
3 module fin where
|
|
4
|
|
5 open import Data.Fin hiding (_<_ ; _≤_ )
|
|
6 open import Data.Fin.Properties hiding ( <-trans )
|
|
7 open import Data.Nat
|
|
8 open import logic
|
|
9 open import nat
|
|
10 open import Relation.Binary.PropositionalEquality
|
|
11
|
|
12
|
|
13 -- toℕ<n
|
|
14 fin<n : {n : ℕ} {f : Fin n} → toℕ f < n
|
|
15 fin<n {_} {zero} = s≤s z≤n
|
|
16 fin<n {suc n} {suc f} = s≤s (fin<n {n} {f})
|
|
17
|
|
18 -- toℕ≤n
|
|
19 fin≤n : {n : ℕ} (f : Fin (suc n)) → toℕ f ≤ n
|
|
20 fin≤n {_} zero = z≤n
|
|
21 fin≤n {suc n} (suc f) = s≤s (fin≤n {n} f)
|
|
22
|
|
23 pred<n : {n : ℕ} {f : Fin (suc n)} → n > 0 → Data.Nat.pred (toℕ f) < n
|
|
24 pred<n {suc n} {zero} (s≤s z≤n) = s≤s z≤n
|
|
25 pred<n {suc n} {suc f} (s≤s z≤n) = fin<n
|
|
26
|
|
27 fin<asa : {n : ℕ} → toℕ (fromℕ< {n} a<sa) ≡ n
|
|
28 fin<asa = toℕ-fromℕ< nat.a<sa
|
|
29
|
|
30 -- fromℕ<-toℕ
|
|
31 toℕ→from : {n : ℕ} {x : Fin (suc n)} → toℕ x ≡ n → fromℕ n ≡ x
|
|
32 toℕ→from {0} {zero} refl = refl
|
|
33 toℕ→from {suc n} {suc x} eq = cong (λ k → suc k ) ( toℕ→from {n} {x} (cong (λ k → Data.Nat.pred k ) eq ))
|
|
34
|
|
35 0≤fmax : {n : ℕ } → (# 0) Data.Fin.≤ fromℕ< {n} a<sa
|
|
36 0≤fmax = subst (λ k → 0 ≤ k ) (sym (toℕ-fromℕ< a<sa)) z≤n
|
|
37
|
|
38 0<fmax : {n : ℕ } → (# 0) Data.Fin.< fromℕ< {suc n} a<sa
|
|
39 0<fmax = subst (λ k → 0 < k ) (sym (toℕ-fromℕ< a<sa)) (s≤s z≤n)
|
|
40
|
|
41 -- toℕ-injective
|
|
42 i=j : {n : ℕ} (i j : Fin n) → toℕ i ≡ toℕ j → i ≡ j
|
|
43 i=j {suc n} zero zero refl = refl
|
|
44 i=j {suc n} (suc i) (suc j) eq = cong ( λ k → suc k ) ( i=j i j (cong ( λ k → Data.Nat.pred k ) eq) )
|
|
45
|
|
46 -- raise 1
|
|
47 fin+1 : { n : ℕ } → Fin n → Fin (suc n)
|
|
48 fin+1 zero = zero
|
|
49 fin+1 (suc x) = suc (fin+1 x)
|
|
50
|
|
51 open import Data.Nat.Properties as NatP hiding ( _≟_ )
|
|
52
|
|
53 fin+1≤ : { i n : ℕ } → (a : i < n) → fin+1 (fromℕ< a) ≡ fromℕ< (<-trans a a<sa)
|
|
54 fin+1≤ {0} {suc i} (s≤s z≤n) = refl
|
|
55 fin+1≤ {suc n} {suc (suc i)} (s≤s (s≤s a)) = cong (λ k → suc k ) ( fin+1≤ {n} {suc i} (s≤s a) )
|
|
56
|
|
57 fin+1-toℕ : { n : ℕ } → { x : Fin n} → toℕ (fin+1 x) ≡ toℕ x
|
|
58 fin+1-toℕ {suc n} {zero} = refl
|
|
59 fin+1-toℕ {suc n} {suc x} = cong (λ k → suc k ) (fin+1-toℕ {n} {x})
|
|
60
|
|
61 open import Relation.Nullary
|
|
62 open import Data.Empty
|
|
63
|
|
64 fin-1 : { n : ℕ } → (x : Fin (suc n)) → ¬ (x ≡ zero ) → Fin n
|
|
65 fin-1 zero ne = ⊥-elim (ne refl )
|
|
66 fin-1 {n} (suc x) ne = x
|
|
67
|
|
68 fin-1-sx : { n : ℕ } → (x : Fin n) → fin-1 (suc x) (λ ()) ≡ x
|
|
69 fin-1-sx zero = refl
|
|
70 fin-1-sx (suc x) = refl
|
|
71
|
|
72 fin-1-xs : { n : ℕ } → (x : Fin (suc n)) → (ne : ¬ (x ≡ zero )) → suc (fin-1 x ne ) ≡ x
|
|
73 fin-1-xs zero ne = ⊥-elim ( ne refl )
|
|
74 fin-1-xs (suc x) ne = refl
|
|
75
|
|
76 -- suc-injective
|
|
77 -- suc-eq : {n : ℕ } {x y : Fin n} → Fin.suc x ≡ Fin.suc y → x ≡ y
|
|
78 -- suc-eq {n} {x} {y} eq = subst₂ (λ j k → j ≡ k ) {!!} {!!} (cong (λ k → Data.Fin.pred k ) eq )
|
|
79
|
|
80 -- this is refl
|
|
81 lemma3 : {a b : ℕ } → (lt : a < b ) → fromℕ< (s≤s lt) ≡ suc (fromℕ< lt)
|
|
82 lemma3 (s≤s lt) = refl
|
|
83
|
|
84 -- fromℕ<-toℕ
|
|
85 lemma12 : {n m : ℕ } → (n<m : n < m ) → (f : Fin m ) → toℕ f ≡ n → f ≡ fromℕ< n<m
|
|
86 lemma12 {zero} {suc m} (s≤s z≤n) zero refl = refl
|
|
87 lemma12 {suc n} {suc m} (s≤s n<m) (suc f) refl = cong suc ( lemma12 {n} {m} n<m f refl )
|
|
88
|
|
89 open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ )
|
|
90 open import Data.Fin.Properties
|
|
91
|
|
92 -- <-irrelevant
|
|
93 <-nat=irr : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n
|
|
94 <-nat=irr {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl
|
|
95 <-nat=irr {suc i} {suc i} {suc n} refl {s≤s i<n} {s≤s j<n} = HE.cong (λ k → s≤s k ) ( <-nat=irr {i} {i} {n} refl )
|
|
96
|
|
97 lemma8 : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n
|
|
98 lemma8 {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl
|
|
99 lemma8 {suc i} {suc i} {suc n} refl {s≤s i<n} {s≤s j<n} = HE.cong (λ k → s≤s k ) ( lemma8 {i} {i} {n} refl )
|
|
100
|
|
101 -- fromℕ<-irrelevant
|
|
102 lemma10 : {n i j : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → fromℕ< i<n ≡ fromℕ< j<n
|
|
103 lemma10 {n} refl = HE.≅-to-≡ (HE.cong (λ k → fromℕ< k ) (lemma8 refl ))
|
|
104
|
|
105 lemma31 : {a b c : ℕ } → { a<b : a < b } { b<c : b < c } { a<c : a < c } → NatP.<-trans a<b b<c ≡ a<c
|
|
106 lemma31 {a} {b} {c} {a<b} {b<c} {a<c} = HE.≅-to-≡ (lemma8 refl)
|
|
107
|
|
108 -- toℕ-fromℕ<
|
|
109 lemma11 : {n m : ℕ } {x : Fin n } → (n<m : n < m ) → toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m)) ≡ toℕ x
|
|
110 lemma11 {n} {m} {x} n<m = begin
|
|
111 toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m))
|
|
112 ≡⟨ toℕ-fromℕ< _ ⟩
|
|
113 toℕ x
|
|
114 ∎ where
|
|
115 open ≡-Reasoning
|
|
116
|
|
117
|