-
Notifications
You must be signed in to change notification settings - Fork 253
Added Z mod n as Fin #2073
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
Added Z mod n as Fin #2073
Changes from 10 commits
19566db
94d8aa9
c87e35a
af96faa
7e6a392
467b190
600035c
5cdbaf7
e64b543
d3d4770
1d28e8e
3773f2c
e853059
fccf44d
e7edaee
ab5493d
7ccd05e
644a8cb
417fa28
4d3a4ab
84cc27b
473ac6d
b539e70
aeb1c4a
bdf0651
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- ℤ module n | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.Fin.Mod where | ||
|
||
open import Function using (id; _$_; _∘_) | ||
open import Data.Bool using (true; false) | ||
open import Data.Product | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open import Data.Nat.Base as ℕ using (ℕ; z≤n; s≤s) | ||
open import Data.Nat.Properties as ℕ | ||
using (m≤n⇒m≤1+n; 1+n≰n; module ≤-Reasoning) | ||
open import Data.Fin.Base as F hiding (suc; pred; _+_; _-_) | ||
open import Data.Fin.Properties | ||
open import Data.Fin.Induction using (<-weakInduction) | ||
open import Data.Fin.Relation.Unary.Top | ||
open import Relation.Nullary.Decidable.Core using (Dec; yes; no) | ||
open import Relation.Nullary.Negation.Core using (contradiction) | ||
open import Relation.Binary.PropositionalEquality | ||
using (_≡_; _≢_; refl; sym; trans; cong; module ≡-Reasoning) | ||
import Algebra.Definitions as ADef | ||
open import Relation.Unary using (Pred) | ||
|
||
private variable | ||
m n : ℕ | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
open module AD {n} = ADef {A = Fin n} _≡_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be an anonymous module? |
||
open ≡-Reasoning | ||
|
||
infixl 6 _+_ _-_ | ||
|
||
suc : Fin n → Fin n | ||
suc i with view i | ||
... | ‵fromℕ = zero | ||
... | ‵inj₁ i = F.suc ⟦ i ⟧ | ||
|
||
pred : Fin n → Fin n | ||
pred zero = fromℕ _ | ||
pred (F.suc i) = inject₁ i | ||
|
||
_ℕ+_ : ℕ → Fin n → Fin n | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ℕ.zero ℕ+ i = i | ||
ℕ.suc n ℕ+ i = suc (n ℕ+ i) | ||
|
||
_+_ : Fin m → Fin n → Fin n | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give me an example of a use-case of this operation? Seems a little unnatural to me adding together two numbers of different mods and arbitrarily returning the result mod the right one? And why does |
||
i + j = toℕ i ℕ+ j | ||
|
||
_-_ : Fin n → Fin n → Fin n | ||
i - j = i + opposite j | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This suggests that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, for a possible alternative (re-)definition of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, it is with the alternative re-definition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except that there's still an asymmetry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it assymmetric. |
||
|
||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suc-inj≡fsuc : (i : Fin n) → suc (inject₁ i) ≡ F.suc i | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suc-inj≡fsuc i rewrite view-inject₁ i = cong F.suc (view-complete (view i)) | ||
|
||
sucFromℕ≡0 : ∀ n → suc (fromℕ n) ≡ zero | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sucFromℕ≡0 n rewrite view-fromℕ n = refl | ||
|
||
pred-fsuc≡inj : (i : Fin n) → pred (F.suc i) ≡ inject₁ i | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pred-fsuc≡inj _ = refl | ||
|
||
suc-pred≡id : (i : Fin n) → suc (pred i) ≡ i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
suc-pred≡id zero = sucFromℕ≡0 _ | ||
suc-pred≡id (F.suc i) = suc-inj≡fsuc i | ||
|
||
pred-suc≡id : (i : Fin n) → pred (suc i) ≡ i | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pred-suc≡id i with view i | ||
... | ‵fromℕ = refl | ||
... | ‵inj₁ p = cong inject₁ (view-complete p) | ||
|
||
+-identityˡ : LeftIdentity {ℕ.suc n} zero _+_ | ||
+-identityˡ _ = refl | ||
|
||
+ℕ-identityʳ-toℕ : m ℕ.≤ n → toℕ (m ℕ+ zero {n}) ≡ m | ||
+ℕ-identityʳ-toℕ {ℕ.zero} m≤n = refl | ||
+ℕ-identityʳ-toℕ {ℕ.suc m} (s≤s m≤n) = begin | ||
toℕ (suc (m ℕ+ zero)) ≡⟨ cong (toℕ ∘ suc) (toℕ-injective toℕm≡fromℕ<) ⟩ | ||
toℕ (suc (inject₁ (fromℕ< (s≤s m≤n)))) ≡⟨ cong toℕ (suc-inj≡fsuc _) ⟩ | ||
ℕ.suc (toℕ (fromℕ< (s≤s m≤n))) ≡⟨ cong ℕ.suc (toℕ-fromℕ< _) ⟩ | ||
ℕ.suc m ∎ | ||
where | ||
|
||
toℕm≡fromℕ< = begin | ||
toℕ (m ℕ+ zero) ≡⟨ +ℕ-identityʳ-toℕ (m≤n⇒m≤1+n m≤n) ⟩ | ||
m ≡˘⟨ toℕ-fromℕ< _ ⟩ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use the new symmetric operator everywhere. No superscript |
||
toℕ (fromℕ< (s≤s m≤n)) ≡˘⟨ toℕ-inject₁ _ ⟩ | ||
toℕ (inject₁ (fromℕ< (s≤s m≤n))) ∎ | ||
|
||
+ℕ-identityʳ : (m≤n : m ℕ.≤ n) → m ℕ+ zero ≡ fromℕ< (s≤s m≤n) | ||
+ℕ-identityʳ {m} m≤n = toℕ-injective (begin | ||
toℕ (m ℕ+ zero) ≡⟨ +ℕ-identityʳ-toℕ m≤n ⟩ | ||
m ≡˘⟨ toℕ-fromℕ< _ ⟩ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reasoning operators seem to be misaligned. |
||
toℕ (fromℕ< (s≤s m≤n)) ∎) | ||
|
||
+-identityʳ : RightIdentity {ℕ.suc n} zero _+_ | ||
guilhermehas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+-identityʳ {n} i rewrite +ℕ-identityʳ {m = toℕ i} {n} _ = fromℕ<-toℕ _ (toℕ≤pred[n] _) | ||
|
||
induction : ∀ {ℓ} (P : Pred (Fin (ℕ.suc n)) ℓ) | ||
→ P zero | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style guide dictates that the arrows should go on the end of lines. |
||
→ (∀ {i} → P i → P (suc i)) | ||
→ ∀ i → P i | ||
induction P P₀ Pᵢ⇒Pᵢ₊₁ i = <-weakInduction P P₀ Pᵢ⇒Pᵢ₊₁′ i | ||
where | ||
|
||
PInj : ∀ {i} → P (suc (inject₁ i)) → P (F.suc i) | ||
PInj {i} rewrite suc-inj≡fsuc i = id | ||
|
||
Pᵢ⇒Pᵢ₊₁′ : ∀ i → P (inject₁ i) → P (F.suc i) | ||
Pᵢ⇒Pᵢ₊₁′ _ Pi = PInj (Pᵢ⇒Pᵢ₊₁ Pi) |
Uh oh!
There was an error while loading. Please reload this page.