-
Notifications
You must be signed in to change notification settings - Fork 249
a recursive view of Fin n
#1923
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
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
75fa9d0
a view of `Fin (suc n)`
jamesmckinna 629b415
documentation
jamesmckinna c6a9994
theory of `opposite` also
jamesmckinna d6e7b9c
tweak
jamesmckinna d2173fb
first of Jacques' review changes
jamesmckinna 6e0c4be
refactored according to Jacques' prescriptions; renamed the inverse
jamesmckinna 1021deb
fix whitespace
jamesmckinna a870d69
tightened imports
jamesmckinna c766cc7
tightened imports
jamesmckinna f43a21f
tightened imports
jamesmckinna 66bc0c7
cosmetics
jamesmckinna 8eff5d6
rmeoved redundant
jamesmckinna 6576fe5
made `Instances` not public
jamesmckinna c18173c
slight refactor
jamesmckinna d4069e9
symmetry typo
jamesmckinna 6d4cc58
imports
jamesmckinna 0e6db5e
other use of `lower₁` in `Data.Fin.Induction`
jamesmckinna fa28f4c
previously uncommitted comment on the `View` itself
jamesmckinna c51dd63
tidied up imports for `Top` view
jamesmckinna 904fdbe
made `--cubical-compatible`
jamesmckinna f3550ad
better use of `variable`s
jamesmckinna 9a0d6ad
(vertical) whitespace)
jamesmckinna 1dd382e
made the view recursive; with simplifications
jamesmckinna f10cb3f
final tweaks; use of variable `{n}` still needed in places
jamesmckinna d878b2c
streamlined use of instances
jamesmckinna c0d5651
revised names, as per discusison
jamesmckinna 2c94427
factored out `Instances`; revised `README` and `CHANGELOG`
jamesmckinna 9bce07e
followed Matthew's suggestion better; added uniqueness proof for the …
jamesmckinna 9012225
typo
jamesmckinna c7e7c9e
revised `Instances`
jamesmckinna 1f1f9f3
fresh round of `variable` review comments
jamesmckinna a88d39c
sharpened use of `instance`s in response to review comments
jamesmckinna c962e14
reordering
jamesmckinna 6ba18be
`fromℕ≢inject₁` now takes implicit argument
jamesmckinna c7b3acf
`CHANGELOG`!
jamesmckinna eae3f83
tidy up comments and binders
jamesmckinna 4a4a4e2
Change to use i rather than j in Top
MatthewDaggitt c8451d7
uniform naming scheme
jamesmckinna 6d8979d
Merge branch 'top-view' of github.com:jamesmckinna/agda-stdlib into t…
jamesmckinna 21500e9
removed `Instances`
jamesmckinna c5ed38f
removed `inject₁⁻¹`
jamesmckinna cb3eb95
removed `Top.Instances`
jamesmckinna 7207c6e
drastic `import` simplification
jamesmckinna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Example use of the 'top' view of Fin | ||
-- | ||
-- This is an example of a view of (elements of) a datatype, | ||
-- here i : Fin (suc n), which exhibits every such i as | ||
-- * either, i = fromℕ n | ||
-- * or, i = inject₁ j for a unique j : Fin n | ||
-- | ||
-- Using this view, we can redefine certain operations in `Data.Fin.Base`, | ||
-- together with their corresponding properties in `Data.Fin.Properties`. | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module README.Data.Fin.Relation.Unary.Top where | ||
|
||
open import Data.Nat.Base using (ℕ; zero; suc; _∸_; _≤_) | ||
open import Data.Nat.Properties using (n∸n≡0; +-∸-assoc; ≤-reflexive) | ||
open import Data.Fin.Base using (Fin; zero; suc; toℕ; fromℕ; inject₁; _>_) | ||
open import Data.Fin.Properties using (toℕ-fromℕ; toℕ<n; toℕ-inject₁) | ||
open import Data.Fin.Induction hiding (>-weakInduction) | ||
open import Data.Fin.Relation.Unary.Top | ||
import Induction.WellFounded as WF | ||
open import Level using (Level) | ||
open import Relation.Binary.PropositionalEquality | ||
open import Relation.Unary using (Pred) | ||
|
||
private | ||
variable | ||
ℓ : Level | ||
n : ℕ | ||
|
||
------------------------------------------------------------------------ | ||
-- Reimplementation of `Data.Fin.Base.opposite`, and its properties | ||
|
||
-- Definition | ||
|
||
opposite : Fin n → Fin n | ||
opposite {suc n} i with view i | ||
... | ‵fromℕ = zero | ||
... | ‵inject₁ j = suc (opposite {n} j) | ||
|
||
-- Properties | ||
|
||
opposite-zero≡fromℕ : ∀ n → opposite {suc n} zero ≡ fromℕ n | ||
opposite-zero≡fromℕ zero = refl | ||
opposite-zero≡fromℕ (suc n) = cong suc (opposite-zero≡fromℕ n) | ||
|
||
opposite-fromℕ≡zero : ∀ n → opposite {suc n} (fromℕ n) ≡ zero | ||
opposite-fromℕ≡zero n rewrite view-fromℕ n = refl | ||
|
||
opposite-suc≡inject₁-opposite : (j : Fin n) → | ||
opposite (suc j) ≡ inject₁ (opposite j) | ||
opposite-suc≡inject₁-opposite {suc n} i with view i | ||
... | ‵fromℕ = refl | ||
... | ‵inject₁ j = cong suc (opposite-suc≡inject₁-opposite {n} j) | ||
|
||
opposite-involutive : (j : Fin n) → opposite (opposite j) ≡ j | ||
opposite-involutive {suc n} zero | ||
rewrite opposite-zero≡fromℕ n | ||
| view-fromℕ n = refl | ||
opposite-involutive {suc n} (suc i) | ||
rewrite opposite-suc≡inject₁-opposite i | ||
| view-inject₁ (opposite i) = cong suc (opposite-involutive i) | ||
|
||
opposite-suc : (j : Fin n) → toℕ (opposite (suc j)) ≡ toℕ (opposite j) | ||
opposite-suc j = begin | ||
toℕ (opposite (suc j)) ≡⟨ cong toℕ (opposite-suc≡inject₁-opposite j) ⟩ | ||
toℕ (inject₁ (opposite j)) ≡⟨ toℕ-inject₁ (opposite j) ⟩ | ||
toℕ (opposite j) ∎ where open ≡-Reasoning | ||
|
||
opposite-prop : (j : Fin n) → toℕ (opposite j) ≡ n ∸ suc (toℕ j) | ||
opposite-prop {suc n} i with view i | ||
... | ‵fromℕ rewrite toℕ-fromℕ n | n∸n≡0 n = refl | ||
... | ‵inject₁ j = begin | ||
suc (toℕ (opposite j)) ≡⟨ cong suc (opposite-prop j) ⟩ | ||
suc (n ∸ suc (toℕ j)) ≡˘⟨ +-∸-assoc 1 (toℕ<n j) ⟩ | ||
n ∸ toℕ j ≡˘⟨ cong (n ∸_) (toℕ-inject₁ j) ⟩ | ||
n ∸ toℕ (inject₁ j) ∎ where open ≡-Reasoning | ||
|
||
------------------------------------------------------------------------ | ||
-- Reimplementation of `Data.Fin.Induction.>-weakInduction` | ||
|
||
open WF using (Acc; acc) | ||
|
||
>-weakInduction : (P : Pred (Fin (suc n)) ℓ) → | ||
P (fromℕ n) → | ||
(∀ i → P (suc i) → P (inject₁ i)) → | ||
∀ i → P i | ||
>-weakInduction P Pₙ Pᵢ₊₁⇒Pᵢ i = induct (>-wellFounded i) | ||
where | ||
induct : ∀ {i} → Acc _>_ i → P i | ||
induct {i} (acc rec) with view i | ||
... | ‵fromℕ = Pₙ | ||
... | ‵inject₁ j = Pᵢ₊₁⇒Pᵢ j (induct (rec _ inject₁[j]+1≤[j+1])) | ||
where | ||
inject₁[j]+1≤[j+1] : suc (toℕ (inject₁ j)) ≤ toℕ (suc j) | ||
inject₁[j]+1≤[j+1] = ≤-reflexive (toℕ-inject₁ (suc j)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- The 'top' view of Fin. | ||
-- | ||
-- This is an example of a view of (elements of) a datatype, | ||
-- here i : Fin (suc n), which exhibits every such i as | ||
-- * either, i = fromℕ n | ||
-- * or, i = inject₁ j for a unique j : Fin n | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.Fin.Relation.Unary.Top where | ||
|
||
open import Data.Nat.Base using (ℕ; zero; suc) | ||
open import Data.Fin.Base using (Fin; zero; suc; fromℕ; inject₁) | ||
open import Relation.Binary.PropositionalEquality.Core | ||
|
||
private | ||
variable | ||
n : ℕ | ||
i : Fin n | ||
|
||
------------------------------------------------------------------------ | ||
-- The View, considered as a unary relation on Fin n | ||
|
||
-- NB `Data.Fin.Properties.fromℕ≢inject₁` establishes that the following | ||
-- inductively defined family on `Fin n` has constructors which target | ||
-- *disjoint* instances of the family (moreover at indices `n = suc _`); | ||
-- hence the interpretations of the View constructors will also be disjoint. | ||
|
||
data View : (i : Fin n) → Set where | ||
‵fromℕ : View (fromℕ n) | ||
‵inj₁ : View i → View (inject₁ i) | ||
|
||
pattern ‵inject₁ i = ‵inj₁ {i = i} _ | ||
|
||
-- The view covering function, witnessing soundness of the view | ||
|
||
view : (i : Fin n) → View i | ||
view zero = view-zero where | ||
view-zero : View (zero {n}) | ||
view-zero {n = zero} = ‵fromℕ | ||
view-zero {n = suc _} = ‵inj₁ view-zero | ||
view (suc i) with view i | ||
... | ‵fromℕ = ‵fromℕ | ||
... | ‵inject₁ i = ‵inj₁ (view (suc i)) | ||
|
||
-- Interpretation of the view constructors | ||
|
||
⟦_⟧ : {i : Fin n} → View i → Fin n | ||
⟦ ‵fromℕ ⟧ = fromℕ _ | ||
⟦ ‵inject₁ i ⟧ = inject₁ i | ||
|
||
-- Completeness of the view | ||
|
||
view-complete : (v : View i) → ⟦ v ⟧ ≡ i | ||
view-complete ‵fromℕ = refl | ||
view-complete (‵inj₁ _) = refl | ||
|
||
-- 'Computational' behaviour of the covering function | ||
|
||
view-fromℕ : ∀ n → view (fromℕ n) ≡ ‵fromℕ | ||
view-fromℕ zero = refl | ||
view-fromℕ (suc n) rewrite view-fromℕ n = refl | ||
|
||
view-inject₁ : (i : Fin n) → view (inject₁ i) ≡ ‵inj₁ (view i) | ||
view-inject₁ zero = refl | ||
view-inject₁ (suc i) rewrite view-inject₁ i = refl | ||
|
||
-- Uniqueness of the view | ||
|
||
view-unique : (v : View i) → view i ≡ v | ||
view-unique ‵fromℕ = view-fromℕ _ | ||
view-unique (‵inj₁ {i = i} v) = begin | ||
view (inject₁ i) ≡⟨ view-inject₁ i ⟩ | ||
‵inj₁ (view i) ≡⟨ cong ‵inj₁ (view-unique v) ⟩ | ||
‵inj₁ v ∎ where open ≡-Reasoning |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.