12
12
13
13
module Data.Fin.Base where
14
14
15
+ open import Data.Bool.Base using (Bool; true; false; T; not)
15
16
open import Data.Empty using (⊥-elim)
16
17
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; z≤n; s≤s; z<s; s<s; _^_)
17
18
open import Data.Product as Product using (_×_; _,_; proj₁; proj₂)
@@ -20,6 +21,7 @@ open import Function.Base using (id; _∘_; _on_; flip)
20
21
open import Level using (0ℓ)
21
22
open import Relation.Nullary using (yes; no)
22
23
open import Relation.Nullary.Decidable.Core using (True; toWitness)
24
+ open import Relation.Nullary.Negation using (contradiction)
23
25
open import Relation.Binary.Core
24
26
open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≢_; refl; cong)
25
27
open import Relation.Binary.Indexed.Heterogeneous using (IRel)
@@ -33,6 +35,12 @@ data Fin : ℕ → Set where
33
35
zero : {n : ℕ} → Fin (suc n)
34
36
suc : {n : ℕ} (i : Fin n) → Fin (suc n)
35
37
38
+ -- Bool-valued zero test
39
+
40
+ zero? : ∀ {n} (i : Fin n) → Bool
41
+ zero? zero = true
42
+ zero? (suc i) = false
43
+
36
44
-- A conversion: toℕ "i" = i.
37
45
38
46
toℕ : ∀ {n} → Fin n → ℕ
@@ -288,6 +296,44 @@ i > j = toℕ i ℕ.> toℕ j
288
296
data _≺_ : ℕ → ℕ → Set where
289
297
_≻toℕ_ : ∀ n (i : Fin n) → toℕ i ≺ n
290
298
299
+ ------------------------------------------------------------------------
300
+ -- Simple predicates
301
+
302
+ -- Defining `NonZero` in terms of `T` and therefore ultimately `⊤` and
303
+ -- `⊥` allows Agda to automatically infer nonZero-ness for any Fin n
304
+ -- of the form `suc i`. Consequently in many circumstances this
305
+ -- eliminates the need to explicitly pass a proof when the NonZero
306
+ -- argument is either an implicit or an instance argument.
307
+ --
308
+ -- See `Data.Nat.Base` for comparison.
309
+
310
+ record NonZero {n : ℕ} (i : Fin n) : Set where
311
+ field
312
+ nonZero : T (not (zero? i))
313
+
314
+ -- Instances
315
+
316
+ instance
317
+ nonZero : ∀ {n} {i : Fin n} → NonZero (suc i)
318
+ nonZero = _
319
+
320
+ -- Constructors
321
+
322
+ ≢-nonZero : ∀ {n} {i : Fin (suc n)} → i ≢ zero → NonZero i
323
+ ≢-nonZero {n} {zero} 0≢0 = contradiction refl 0≢0
324
+ ≢-nonZero {n} {suc i} i≢0 = _
325
+
326
+ >-nonZero : ∀ {n} {i : Fin (suc n)} → i > zero {n} → NonZero i
327
+ >-nonZero {n} {suc i} _ = _
328
+
329
+ -- Destructors
330
+
331
+ ≢-nonZero⁻¹ : ∀ {n} (i : Fin (suc n)) → .{{NonZero i}} → i ≢ zero
332
+ ≢-nonZero⁻¹ (suc i) ()
333
+
334
+ >-nonZero⁻¹ : ∀ {n} (i : Fin (suc n)) → .{{NonZero i}} → i > zero {n}
335
+ >-nonZero⁻¹ (suc i) = z<s
336
+
291
337
------------------------------------------------------------------------
292
338
-- An ordering view.
293
339
0 commit comments