Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data.Nat.Bounded #2257

Draft
wants to merge 37 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
85c485f
Add `Data.Nat.Bounded`
jamesmckinna Jan 15, 2024
92fa7e9
renamed type; removed `instance`-based field; stumbled over isomorphism
jamesmckinna Jan 20, 2024
f07cc16
got there in the end: yuk!
jamesmckinna Jan 20, 2024
dc8e53e
added `nonZeroIndex`
jamesmckinna Jan 22, 2024
3d850f4
added `ℕ<` literals
jamesmckinna Jan 23, 2024
659d892
added `DecidableEquality`
jamesmckinna Jan 24, 2024
11bb699
too hasty to push
jamesmckinna Jan 24, 2024
51b6280
added projection from Nat; additional constructors
jamesmckinna Jan 25, 2024
04fae4f
tweak to [[1]]<
jamesmckinna Jan 25, 2024
32eb86b
Merge branch 'master' into bounded-nat
jamesmckinna Jan 28, 2024
e560b6f
tweak
jamesmckinna Feb 1, 2024
0fe029e
rename projection from `Nat`
jamesmckinna Feb 4, 2024
6052c4f
added `Equivalence` proof
jamesmckinna Feb 5, 2024
821f51c
inverting `fromℕ` lemmas
jamesmckinna Feb 5, 2024
194f122
added quotient equivalence relation
jamesmckinna Feb 5, 2024
9a734c1
module refactoring
jamesmckinna Feb 9, 2024
dca2d54
cosmetic
jamesmckinna Feb 9, 2024
823016b
added two simple lemmas about underlying values
jamesmckinna Feb 9, 2024
623d854
final lemma about the retraction
jamesmckinna Feb 10, 2024
4076cc5
tweaks to accomodate #2292
jamesmckinna Feb 13, 2024
f0f4883
additions to support #2292
jamesmckinna Feb 13, 2024
e1214bd
refactored arithmetic
jamesmckinna Feb 13, 2024
10d4083
revised imports
jamesmckinna Feb 14, 2024
771b501
Merge branch 'master' into bounded-nat
jamesmckinna Feb 26, 2024
8d523c8
Merge branch 'master' into bounded-nat
jamesmckinna Mar 8, 2024
9b4ee0e
Merge branch 'master' into bounded-nat
jamesmckinna Mar 17, 2024
078d4f6
fixed up `CHANGELOG`
jamesmckinna Mar 17, 2024
97f9cf9
Merge branch 'master' into bounded-nat
jamesmckinna Mar 18, 2024
eccf88d
Merge branch 'master' into bounded-nat
jamesmckinna Mar 19, 2024
f3e2550
Merge branch 'master' into bounded-nat
jamesmckinna May 6, 2024
383529a
Merge branch 'master' into bounded-nat
jamesmckinna Jun 3, 2024
3c3cbf3
update `CHANGELOG` to v2.2
jamesmckinna Jul 28, 2024
ba4e922
Merge branch 'agda:master' into bounded-nat
jamesmckinna Jul 28, 2024
00421a6
update `CHANGELOG`
jamesmckinna Jul 28, 2024
49430b3
Merge branch 'master' into bounded-nat
jamesmckinna Aug 3, 2024
594d35c
Merge branch 'agda:master' into bounded-nat
jamesmckinna Dec 7, 2024
716d585
Merge branch 'agda:master' into bounded-nat
jamesmckinna Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Deprecated names
New modules
-----------

* Isomorphism between `Fin` and an 'obvious' definition `ℕ<` of
'bounded natural number' type, in:
```agda
Data.Nat.Bounded
```

Additions to existing modules
-----------------------------

Expand Down
81 changes: 81 additions & 0 deletions src/Data/Nat/Bounded.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Bounded natural numbers, and their equivalence to `Fin`
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

module Data.Nat.Bounded where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to touch on your question of whether this belongs in the library or a readme file, I guess my question is what circumstances is this easier to work with than Fin? In general we try to avoid duplicate but equivalent definitions unless there is clear reason that sometimes one representation is easier to work with than the other...

Copy link
Contributor Author

@jamesmckinna jamesmckinna Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Matthew! I can think of two or three, some already in the initial preamble (which was really about helping someone solve a related problem of representation, where Fin didn't seem to fit well):

  • fixed-width arithmetic (or even: its extension to variable-widths, but in a way which retains predictable/computable bounds);
  • Digit bases for arithmetic, as a specialised instance/mode of use of the above;
  • revising the 'specification' of Data.Nat.DivMod, to avoid the marshalling/unmarshalling required by the use of Fin to ensure that mod is suitably bounded.

Now, none of these could be said to be a deal-breaker, but I increasingly wonder whether Fin is better suited to its use as an indexing type (for Vec, List, de Bruijn representations, Reflection/deeply embedded syntaxes more generally, etc.), with ℕ< as an arithmetic type/Number instance... cf. #2073 which I might now think makes the 'wrong' choice in using Fin...

Perhaps other pros (and cons!) might be lurking off-stage...
... UPDATED: (pro)


open import Data.Nat.Base as ℕ
import Data.Nat.Properties as ℕ
open import Data.Fin.Base as Fin using (Fin; zero; suc; toℕ)
import Data.Fin.Properties as Fin
open import Data.Product.Base using (_,_)
open import Function.Base using (id; _∘_; _$_; _on_)
open import Function.Bundles using (_⤖_; mk⤖)
open import Function.Consequences.Propositional
using (inverseᵇ⇒bijective; strictlyInverseˡ⇒inverseˡ; strictlyInverseʳ⇒inverseʳ)
open import Relation.Binary.Core using (_⇒_)
open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong; _≗_)
open import Relation.Nullary.Decidable.Core using (recompute)
open import Relation.Nullary.Negation.Core using (¬_)

private
variable
m n : ℕ

------------------------------------------------------------------------
-- Definition

record ℕ< (n : ℕ) : Set where
constructor ⟦_⟧<_
field
value : ℕ
.bounded : value < n

open ℕ< public using () renaming (value to ⟦_⟧)

-- Constructor

⟦0⟧< : ∀ n → .{{ NonZero n }} → ℕ< n
⟦0⟧< (n@(suc _)) = ⟦ zero ⟧< z<s

-- Destructors

¬ℕ<[0] : ¬ ℕ< 0
¬ℕ<[0] ()

isBounded : (i : ℕ< n) → ⟦ i ⟧ < n
isBounded (⟦ _ ⟧< i<n) = recompute (_ ℕ.<? _) i<n

-- Equality on values is propositional equality

bounded≡⇒⟦⟧≡⟦⟧ : _≡_ {A = ℕ< n} ⇒ (_≡_ on ⟦_⟧)
bounded≡⇒⟦⟧≡⟦⟧ = cong ⟦_⟧

⟦⟧≡⟦⟧⇒bounded≡ : (_≡_ on ⟦_⟧) ⇒ _≡_ {A = ℕ< n}
⟦⟧≡⟦⟧⇒bounded≡ refl = refl

------------------------------------------------------------------------
-- Conversion to and from `Fin n`
jamesmckinna marked this conversation as resolved.
Show resolved Hide resolved

toFin : ℕ< n → Fin n
toFin (⟦ _ ⟧< i<n) = Fin.fromℕ< i<n

fromFin : Fin n → ℕ< n
fromFin i = ⟦ toℕ i ⟧< (Fin.toℕ<n i)

toFin∘fromFin≐id : toFin ∘ fromFin ≗ id {A = Fin n}
toFin∘fromFin≐id i = Fin.fromℕ<-toℕ i (Fin.toℕ<n i)

fromFin∘toFin≐id : fromFin ∘ toFin ≗ id {A = ℕ< n}
fromFin∘toFin≐id (⟦ _ ⟧< i<n) = ⟦⟧≡⟦⟧⇒bounded≡ (Fin.toℕ-fromℕ< i<n)

boundedNat⤖Fin : ℕ< n ⤖ Fin n
boundedNat⤖Fin = mk⤖ $ inverseᵇ⇒bijective $
strictlyInverseˡ⇒inverseˡ {f⁻¹ = fromFin} toFin toFin∘fromFin≐id
,
strictlyInverseʳ⇒inverseʳ {f⁻¹ = fromFin} toFin fromFin∘toFin≐id

Loading