Skip to content

Commit

Permalink
adding NonZero to Fin, as per \agda#1686
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmckinna committed Feb 22, 2022
1 parent a47e6ca commit d55ce98
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
46 changes: 46 additions & 0 deletions src/Data/Fin/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

module Data.Fin.Base where

open import Data.Bool.Base using (Bool; true; false; T; not)
open import Data.Empty using (⊥-elim)
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; z≤n; s≤s; z<s; s<s; _^_)
open import Data.Product as Product using (_×_; _,_; proj₁; proj₂)
Expand All @@ -20,6 +21,7 @@ open import Function.Base using (id; _∘_; _on_; flip)
open import Level using (0ℓ)
open import Relation.Nullary using (yes; no)
open import Relation.Nullary.Decidable.Core using (True; toWitness)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary.Core
open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≢_; refl; cong)
open import Relation.Binary.Indexed.Heterogeneous using (IRel)
Expand All @@ -33,6 +35,12 @@ data Fin : ℕ → Set where
zero : {n : ℕ} Fin (suc n)
suc : {n : ℕ} (i : Fin n) Fin (suc n)

-- Bool-valued zero test

zero? : {n} (i : Fin n) Bool
zero? zero = true
zero? (suc i) = false

-- A conversion: toℕ "i" = i.

toℕ : {n} Fin n
Expand Down Expand Up @@ -288,6 +296,44 @@ i > j = toℕ i ℕ.> toℕ j
data _≺_ : Set where
_≻toℕ_ : n (i : Fin n) toℕ i ≺ n

------------------------------------------------------------------------
-- Simple predicates

-- Defining `NonZero` in terms of `T` and therefore ultimately `⊤` and
-- `⊥` allows Agda to automatically infer nonZero-ness for any Fin n
-- of the form `suc i`. Consequently in many circumstances this
-- eliminates the need to explicitly pass a proof when the NonZero
-- argument is either an implicit or an instance argument.
--
-- See `Data.Nat.Base` for comparison.

record NonZero {n : ℕ} (i : Fin n) : Set where
field
nonZero : T (not (zero? i))

-- Instances

instance
nonZero : {n} {i : Fin n} NonZero (suc i)
nonZero = _

-- Constructors

≢-nonZero : {n} {i : Fin (suc n)} i ≢ zero NonZero i
≢-nonZero {n} {zero} 0≢0 = contradiction refl 0≢0
≢-nonZero {n} {suc i} i≢0 = _

>-nonZero : {n} {i : Fin (suc n)} i > zero {n} NonZero i
>-nonZero {n} {suc i} _ = _

-- Destructors

≢-nonZero⁻¹ : {n} (i : Fin (suc n)) .{{NonZero i}} i ≢ zero
≢-nonZero⁻¹ (suc i) ()

>-nonZero⁻¹ : {n} (i : Fin (suc n)) .{{NonZero i}} i > zero {n}
>-nonZero⁻¹ (suc i) = z<s

------------------------------------------------------------------------
-- An ordering view.

Expand Down
5 changes: 2 additions & 3 deletions src/Data/Fin/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,8 @@ inject≤-injective (s≤s p) (s≤s q) (suc x) (suc y) eq =
-- pred
------------------------------------------------------------------------

pred< : {n} (i : Fin (suc n)) i ≢ zero pred i < i
pred< zero p = contradiction refl p
pred< (suc i) p = ≤̄⇒inject₁< ℕₚ.≤-refl
pred< : {n} (i : Fin (suc n)) .{{_ : NonZero i}} pred i < i
pred< (suc i) = ≤̄⇒inject₁< ℕₚ.≤-refl

------------------------------------------------------------------------
-- splitAt
Expand Down

0 comments on commit d55ce98

Please sign in to comment.